github.com/goplus/gop@v1.2.6/builtin/ng/int128.go (about) 1 package ng 2 3 import ( 4 "fmt" 5 "log" 6 "math/big" 7 "math/bits" 8 ) 9 10 const ( 11 Int128_Max = 1<<127 - 1 12 Int128_Min = -1 << 127 13 Int128_IsUntyped = true 14 ) 15 16 const ( 17 signBit = 0x8000000000000000 18 maxInt64 = 1<<63 - 1 19 ) 20 21 var ( 22 big1 = new(big.Int).SetUint64(1) 23 bigMaxU128 *big.Int 24 ) 25 26 func init() { 27 bigMaxU128 = new(big.Int).Lsh(big1, 128) 28 bigMaxU128.Sub(bigMaxU128, big1) // 340282366920938463463374607431768211455 29 } 30 31 // ----------------------------------------------------------------------------- 32 33 type Int128 struct { 34 hi uint64 35 lo uint64 36 } 37 38 // Int128_Init: func int128.init(v int) int128 39 func Int128_Init__0(v int) (out Int128) { 40 return Int128_Cast__2(int64(v)) 41 } 42 43 // Int128_Init: func int128.init(v untyped_bigint) int128 44 func Int128_Init__1(v UntypedBigint) (out Int128) { 45 return Int128_Cast__1(v) 46 } 47 48 // Int128_Cast: func int128(v int) int128 49 func Int128_Cast__0(v int) Int128 { 50 return Int128_Cast__2(int64(v)) 51 } 52 53 // Int128_Cast: func int128(v untyped_bigint) int128 54 func Int128_Cast__1(v UntypedBigint) (out Int128) { 55 out, inRange := Int128_Cast__9(v) 56 if !inRange { 57 log.Panicf("value %v was not in valid int128 range\n", v) 58 } 59 return 60 } 61 62 // Int128_Cast: func int128(v int64) int128 63 func Int128_Cast__2(v int64) (out Int128) { 64 var hi uint64 65 if v < 0 { 66 hi = maxUint64 67 } 68 return Int128{hi: hi, lo: uint64(v)} 69 } 70 71 // Int128_Cast: func int128(v uint64) int128 72 func Int128_Cast__3(v uint64) Int128 { 73 return Int128{lo: v} 74 } 75 76 // Int128_Cast: func int128(v int32) int128 77 func Int128_Cast__4(v int32) Int128 { 78 return Int128_Cast__2(int64(v)) 79 } 80 81 // Int128_Cast: func int128(v int16) int128 82 func Int128_Cast__5(v int16) Int128 { 83 return Int128_Cast__2(int64(v)) 84 } 85 86 // Int128_Cast: func int128(v int8) int128 87 func Int128_Cast__6(v int8) Int128 { 88 return Int128_Cast__2(int64(v)) 89 } 90 91 // Int128_Cast: func int128(v uint18) int128 92 func Int128_Cast__7(v Uint128) (out Int128) { 93 return Int128{hi: v.hi, lo: v.lo} 94 } 95 96 // Int128_Cast: func int128(v *big.Int) int128 97 func Int128_Cast__8(v *big.Int) Int128 { 98 out, _ := Int128_Cast__9(v) 99 return out 100 } 101 102 func Int128_Cast__9(v *big.Int) (out Int128, inRange bool) { 103 neg := v.Sign() < 0 104 words := v.Bits() 105 106 var u Uint128 107 inRange = true 108 switch intSize { 109 case 64: 110 lw := len(words) 111 switch lw { 112 case 0: 113 case 1: 114 u.lo = uint64(words[0]) 115 case 2: 116 u.hi = uint64(words[1]) 117 u.lo = uint64(words[0]) 118 default: 119 u, inRange = Uint128{hi: maxUint64, lo: maxUint64}, false 120 } 121 122 case 32: 123 lw := len(words) 124 switch lw { 125 case 0: 126 case 1: 127 u.lo = uint64(words[0]) 128 case 2: 129 u.lo = (uint64(words[1]) << 32) | (uint64(words[0])) 130 case 3: 131 u.hi = uint64(words[2]) 132 u.lo = (uint64(words[1]) << 32) | (uint64(words[0])) 133 case 4: 134 u.hi = (uint64(words[3]) << 32) | (uint64(words[2])) 135 u.lo = (uint64(words[1]) << 32) | (uint64(words[0])) 136 default: 137 u, inRange = Uint128{hi: maxUint64, lo: maxUint64}, false 138 } 139 140 default: 141 panic("unsupported bit size") 142 } 143 144 if neg { 145 if cmp := u.Cmp__1(Uint128{hi: 0x8000000000000000, lo: 0}); cmp > 0 { 146 out, inRange = Int128{hi: 0x8000000000000000, lo: 0}, false 147 } else { 148 out = Int128{hi: u.hi, lo: u.lo}.Gop_Neg() 149 } 150 } else { 151 if cmp := u.Cmp__1(Uint128{hi: maxInt64, lo: maxUint64}); cmp > 0 { 152 out, inRange = Int128{hi: maxInt64, lo: maxUint64}, false 153 } else { 154 out = Int128{hi: u.hi, lo: u.lo} 155 } 156 } 157 return 158 } 159 160 // Int128_Cast: func int128() int128 161 func Int128_Cast__a() Int128 { 162 return Int128{} 163 } 164 165 // Gop_Rcast: func uint128(v int128) uint128 166 func (i Int128) Gop_Rcast__0() Uint128 { 167 return Uint128{lo: i.lo, hi: i.hi} 168 } 169 170 // Gop_Rcast: func uint128(v int128) (uint128, bool) 171 func (i Int128) Gop_Rcast__1() (out Uint128, inRange bool) { 172 return Uint128{lo: i.lo, hi: i.hi}, i.hi&signBit == 0 173 } 174 175 // Gop_Rcast: func int64(v int128) int64 176 func (i Int128) Gop_Rcast__2() int64 { 177 if i.hi&signBit == 0 { 178 return int64(i.lo) 179 } 180 return -int64(^(i.lo - 1)) 181 } 182 183 // Gop_Rcast: func int64(v int128) (int64, bool) 184 func (i Int128) Gop_Rcast__3() (out int64, inRange bool) { 185 if i.hi&signBit == 0 { 186 return int64(i.lo), i.hi == 0 && i.lo <= maxInt64 187 } 188 return -int64(^(i.lo - 1)), i.hi == maxUint64 && i.lo >= 0x8000000000000000 189 } 190 191 // Gop_Rcast: func uint64(v int128) uint64 192 func (i Int128) Gop_Rcast__4() uint64 { 193 return i.lo 194 } 195 196 // Gop_Rcast: func uint64(v int128) (uint64, bool) 197 func (i Int128) Gop_Rcast__5() (out uint64, inRange bool) { 198 return i.lo, i.hi == 0 199 } 200 201 // ----------------------------------------------------------------------------- 202 203 func (i Int128) IsZero() bool { 204 return i.lo == 0 && i.hi == 0 205 } 206 207 func (i *Int128) Scan(state fmt.ScanState, verb rune) (err error) { 208 t, err := state.Token(true, nil) 209 if err != nil { 210 return 211 } 212 v, err := ParseInt128(string(t), 10) 213 if err == nil { 214 *i = v 215 } 216 return 217 } 218 219 func (i Int128) Format(s fmt.State, c rune) { 220 // TODO: not so good 221 i.BigInt().Format(s, c) 222 } 223 224 func (i Int128) String() string { 225 return i.Text(10) 226 } 227 228 func (i Int128) Text(base int) string { 229 // TODO: not so good 230 return i.BigInt().Text(base) 231 } 232 233 func (i Int128) BigInt() *big.Int { 234 var v big.Int 235 i.ToBigInt(&v) 236 return &v 237 } 238 239 func (i Int128) ToBigInt(b *big.Int) { 240 neg := i.hi&signBit != 0 241 if i.hi > 0 { 242 b.SetUint64(i.hi) 243 b.Lsh(b, 64) 244 } 245 var lo big.Int 246 lo.SetUint64(i.lo) 247 b.Add(b, &lo) 248 249 if neg { 250 b.Xor(b, bigMaxU128).Add(b, big1).Neg(b) 251 } 252 } 253 254 func (i Int128) Sign() int { 255 if i.lo == 0 && i.hi == 0 { 256 return 0 257 } else if i.hi&signBit == 0 { 258 return 1 259 } 260 return -1 261 } 262 263 func (i *Int128) Gop_Inc() { 264 i.lo++ 265 if i.lo == 0 { 266 i.hi++ 267 } 268 } 269 270 func (i *Int128) Gop_Dec() { 271 if i.lo == 0 { 272 i.hi-- 273 } 274 i.lo-- 275 } 276 277 // Gop_AddAssign: func (a *int128) += (b int128) 278 func (i *Int128) Gop_AddAssign(b Int128) { 279 *i = i.Gop_Add__1(b) 280 } 281 282 // Gop_SubAssign: func (a *int128) -= (b int128) 283 func (i *Int128) Gop_SubAssign(b Int128) { 284 *i = i.Gop_Sub__1(b) 285 } 286 287 // Gop_MulAssign: func (a *int128) *= (b int128) 288 func (i *Int128) Gop_MulAssign(b Int128) { 289 *i = i.Gop_Mul__1(b) 290 } 291 292 // Gop_QuoAssign: func (a *int128) /= (b int128) { 293 func (i *Int128) Gop_QuoAssign(b Int128) { 294 *i = i.Gop_Quo__1(b) 295 } 296 297 // Gop_RemAssign: func (a *int128) %= (b int128) 298 func (i *Int128) Gop_RemAssign(b Int128) { 299 *i = i.Gop_Rem__1(b) 300 } 301 302 // Gop_OrAssign: func (a *int128) |= (b int128) 303 func (i *Int128) Gop_OrAssign(b Int128) { 304 *i = i.Gop_Or(b) 305 } 306 307 // Gop_XorAssign: func (a *int128) ^= (b int128) 308 func (i *Int128) Gop_XorAssign(b Int128) { 309 *i = i.Gop_Xor(b) 310 } 311 312 // Gop_AndAssign: func (a *int128) &= (b int128) 313 func (i *Int128) Gop_AndAssign(b Int128) { 314 *i = i.Gop_And(b) 315 } 316 317 // Gop_AndNotAssign: func (a *int128) &^= (b int128) 318 func (i *Int128) Gop_AndNotAssign(b Int128) { 319 *i = i.Gop_AndNot(b) 320 } 321 322 // Gop_LshAssign: func (a *int128) <<= (n untyped_uint) 323 func (i *Int128) Gop_LshAssign(n Gop_ninteger) { 324 *i = i.Gop_Lsh(n) 325 } 326 327 // Gop_RshAssign: func (a *int128) >>= (n untyped_uint) 328 func (i *Int128) Gop_RshAssign(n Gop_ninteger) { 329 *i = i.Gop_Rsh(n) 330 } 331 332 func (i Int128) Gop_Add__1(n Int128) (v Int128) { 333 var carry uint64 334 v.lo, carry = bits.Add64(i.lo, n.lo, 0) 335 v.hi, _ = bits.Add64(i.hi, n.hi, carry) 336 return v 337 } 338 339 func (i Int128) Gop_Add__0(n int64) (v Int128) { 340 var carry uint64 341 v.lo, carry = bits.Add64(i.lo, uint64(n), 0) 342 if n < 0 { 343 v.hi = i.hi + maxUint64 + carry 344 } else { 345 v.hi = i.hi + carry 346 } 347 return v 348 } 349 350 func (i Int128) Gop_Sub__1(n Int128) (v Int128) { 351 var borrowed uint64 352 v.lo, borrowed = bits.Sub64(i.lo, n.lo, 0) 353 v.hi, _ = bits.Sub64(i.hi, n.hi, borrowed) 354 return v 355 } 356 357 func (i Int128) Gop_Sub__0(n int64) (v Int128) { 358 var borrowed uint64 359 if n < 0 { 360 v.lo, borrowed = bits.Sub64(i.lo, uint64(n), 0) 361 v.hi = i.hi - maxUint64 - borrowed 362 } else { 363 v.lo, borrowed = bits.Sub64(i.lo, uint64(n), 0) 364 v.hi = i.hi - borrowed 365 } 366 return v 367 } 368 369 func (i Int128) Gop_Neg() (v Int128) { 370 if i.lo == 0 && i.hi == 0 { 371 return 372 } 373 if i.hi&signBit == 0 { 374 v.hi = ^i.hi 375 v.lo = (^i.lo) + 1 376 } else { 377 v.hi = ^i.hi 378 v.lo = ^(i.lo - 1) 379 } 380 if v.lo == 0 { // handle overflow 381 v.hi++ 382 } 383 return v 384 } 385 386 func (i Int128) Gop_Dup() (v Int128) { 387 return i 388 } 389 390 // Abs returns the absolute value of i as a signed integer. 391 func (i Int128) Abs__0() Int128 { 392 if i.hi&signBit != 0 { 393 i.hi = ^i.hi 394 i.lo = ^(i.lo - 1) 395 if i.lo == 0 { // handle carry 396 i.hi++ 397 } 398 } 399 return i 400 } 401 402 func (i Int128) Abs__1() (ret Int128, inRange bool) { 403 return i.Abs__0(), i.hi != 0x8000000000000000 || i.lo != 0 404 } 405 406 // AbsU returns the absolute value of i as an unsigned integer. All 407 // values of i are representable using this function, but the type is 408 // changed. 409 func (i Int128) AbsU() Uint128 { 410 if i.hi == 0x8000000000000000 && i.lo == 0 { 411 return Uint128{hi: 0x8000000000000000} 412 } 413 if i.hi&signBit != 0 { 414 i.hi = ^i.hi 415 i.lo = ^(i.lo - 1) 416 if i.lo == 0 { // handle carry 417 i.hi++ 418 } 419 } 420 return Uint128{hi: i.hi, lo: i.lo} 421 } 422 423 // Cmp compares i to n and returns: 424 // 425 // < 0 if i < n 426 // 0 if i == n 427 // > 0 if i > n 428 // 429 // The specific value returned by Cmp is undefined, but it is guaranteed to 430 // satisfy the above constraints. 431 func (i Int128) Cmp__1(n Int128) int { 432 if i.hi == n.hi && i.lo == n.lo { 433 return 0 434 } else if i.hi&signBit == n.hi&signBit { 435 if i.hi > n.hi || (i.hi == n.hi && i.lo > n.lo) { 436 return 1 437 } 438 } else if i.hi&signBit == 0 { 439 return 1 440 } 441 return -1 442 } 443 444 // Cmp64 compares 'i' to 64-bit int 'n' and returns: 445 // 446 // < 0 if i < n 447 // 0 if i == n 448 // > 0 if i > n 449 // 450 // The specific value returned by Cmp is undefined, but it is guaranteed to 451 // satisfy the above constraints. 452 func (i Int128) Cmp__0(n int64) int { 453 var nhi uint64 454 var nlo = uint64(n) 455 if n < 0 { 456 nhi = maxUint64 457 } 458 if i.hi == nhi && i.lo == nlo { 459 return 0 460 } else if i.hi&signBit == nhi&signBit { 461 if i.hi > nhi || (i.hi == nhi && i.lo > nlo) { 462 return 1 463 } 464 } else if i.hi&signBit == 0 { 465 return 1 466 } 467 return -1 468 } 469 470 func (i Int128) Gop_EQ__1(n Int128) bool { 471 return i.hi == n.hi && i.lo == n.lo 472 } 473 474 func (i Int128) Gop_EQ__0(n int64) bool { 475 var nhi uint64 476 var nlo = uint64(n) 477 if n < 0 { 478 nhi = maxUint64 479 } 480 return i.hi == nhi && i.lo == nlo 481 } 482 483 func (i Int128) Gop_GT__1(n Int128) bool { 484 if i.hi&signBit == n.hi&signBit { 485 return i.hi > n.hi || (i.hi == n.hi && i.lo > n.lo) 486 } else if i.hi&signBit == 0 { 487 return true 488 } 489 return false 490 } 491 492 func (i Int128) Gop_GT__0(n int64) bool { 493 var nhi uint64 494 var nlo = uint64(n) 495 if n < 0 { 496 nhi = maxUint64 497 } 498 499 if i.hi&signBit == nhi&signBit { 500 return i.hi > nhi || (i.hi == nhi && i.lo > nlo) 501 } else if i.hi&signBit == 0 { 502 return true 503 } 504 return false 505 } 506 507 func (i Int128) Gop_GE__1(n Int128) bool { 508 if i.hi == n.hi && i.lo == n.lo { 509 return true 510 } 511 if i.hi&signBit == n.hi&signBit { 512 return i.hi > n.hi || (i.hi == n.hi && i.lo > n.lo) 513 } else if i.hi&signBit == 0 { 514 return true 515 } 516 return false 517 } 518 519 func (i Int128) Gop_GE__0(n int64) bool { 520 var nhi uint64 521 var nlo = uint64(n) 522 if n < 0 { 523 nhi = maxUint64 524 } 525 526 if i.hi == nhi && i.lo == nlo { 527 return true 528 } 529 if i.hi&signBit == nhi&signBit { 530 return i.hi > nhi || (i.hi == nhi && i.lo > nlo) 531 } else if i.hi&signBit == 0 { 532 return true 533 } 534 return false 535 } 536 537 func (i Int128) Gop_LT__1(n Int128) bool { 538 if i.hi&signBit == n.hi&signBit { 539 return i.hi < n.hi || (i.hi == n.hi && i.lo < n.lo) 540 } else if i.hi&signBit != 0 { 541 return true 542 } 543 return false 544 } 545 546 func (i Int128) Gop_LT__0(n int64) bool { 547 var nhi uint64 548 var nlo = uint64(n) 549 if n < 0 { 550 nhi = maxUint64 551 } 552 553 if i.hi&signBit == nhi&signBit { 554 return i.hi < nhi || (i.hi == nhi && i.lo < nlo) 555 } else if i.hi&signBit != 0 { 556 return true 557 } 558 return false 559 } 560 561 func (i Int128) Gop_LE__1(n Int128) bool { 562 if i.hi == n.hi && i.lo == n.lo { 563 return true 564 } 565 if i.hi&signBit == n.hi&signBit { 566 return i.hi < n.hi || (i.hi == n.hi && i.lo < n.lo) 567 } else if i.hi&signBit != 0 { 568 return true 569 } 570 return false 571 } 572 573 func (i Int128) Gop_LE__0(n int64) bool { 574 var nhi uint64 575 var nlo = uint64(n) 576 if n < 0 { 577 nhi = maxUint64 578 } 579 580 if i.hi == nhi && i.lo == nlo { 581 return true 582 } 583 if i.hi&signBit == nhi&signBit { 584 return i.hi < nhi || (i.hi == nhi && i.lo < nlo) 585 } else if i.hi&signBit != 0 { 586 return true 587 } 588 return false 589 } 590 591 func (i Int128) Gop_And(n Int128) Int128 { 592 i.hi &= n.hi 593 i.lo &= n.lo 594 return i 595 } 596 597 func (i Int128) Gop_AndNot(n Int128) Int128 { 598 i.hi &^= n.hi 599 i.lo &^= n.lo 600 return i 601 } 602 603 func (i Int128) Gop_Not() Int128 { 604 return Int128{hi: ^i.hi, lo: ^i.lo} 605 } 606 607 func (i Int128) Gop_Or(n Int128) Int128 { 608 i.hi |= n.hi 609 i.lo |= n.lo 610 return i 611 } 612 613 func (i Int128) Gop_Xor(v Int128) Int128 { 614 i.hi ^= v.hi 615 i.lo ^= v.lo 616 return i 617 } 618 619 func (i Int128) Gop_Lsh(n Gop_ninteger) Int128 { 620 if n < 64 { 621 i.hi = (i.hi << n) | (i.lo >> (64 - n)) 622 i.lo <<= n 623 } else { 624 i.hi = i.lo << (n - 64) 625 i.lo = 0 626 } 627 return i 628 } 629 630 func (i Int128) Gop_Rsh(n Gop_ninteger) Int128 { 631 if n < 64 { 632 i.lo = (i.lo >> n) | (i.hi << (64 - n)) 633 } else { 634 i.lo = i.hi >> (n - 64) 635 } 636 i.hi = uint64(int64(i.hi) >> n) 637 return i 638 } 639 640 // Mul returns the product of two I128s. 641 // 642 // Overflow should wrap around, as per the Go spec. 643 func (i Int128) Gop_Mul__1(n Int128) (dest Int128) { 644 hi, lo := bits.Mul64(i.lo, n.lo) 645 hi += i.hi*n.lo + i.lo*n.hi 646 return Int128{hi, lo} 647 } 648 649 func (i Int128) Gop_Mul__0(n int64) Int128 { 650 nlo := uint64(n) 651 var nhi uint64 652 if n < 0 { 653 nhi = maxUint64 654 } 655 hi, lo := bits.Mul64(i.lo, nlo) 656 hi += i.hi*nlo + i.lo*nhi 657 return Int128{hi, lo} 658 } 659 660 // QuoRem returns the quotient q and remainder r for y != 0. If y == 0, a 661 // division-by-zero run-time panic occurs. 662 // 663 // QuoRem implements T-division and modulus (like Go): 664 // 665 // q = x/y with the result truncated to zero 666 // r = x - y*q 667 // 668 // U128 does not support big.Int.DivMod()-style Euclidean division. 669 // 670 // Note: dividing MinI128 by -1 will overflow, returning MinI128, as 671 // per the Go spec (https://golang.org/ref/spec#Integer_operators): 672 // 673 // The one exception to this rule is that if the dividend x is the most 674 // negative value for the int type of x, the quotient q = x / -1 is equal to x 675 // (and r = 0) due to two's-complement integer overflow. 676 func (i Int128) QuoRem__1(by Int128) (q, r Int128) { 677 qSign, rSign := 1, 1 678 if i.Gop_LT__0(0) { 679 qSign, rSign = -1, -1 680 i = i.Gop_Neg() 681 } 682 if by.Gop_LT__0(0) { 683 qSign = -qSign 684 by = by.Gop_Neg() 685 } 686 687 qu, ru := i.Gop_Rcast__0().QuoRem__1(by.Gop_Rcast__0()) 688 q, r = Int128_Cast__7(qu), Int128_Cast__7(ru) 689 if qSign < 0 { 690 q = q.Gop_Neg() 691 } 692 if rSign < 0 { 693 r = r.Gop_Neg() 694 } 695 return q, r 696 } 697 698 func (i Int128) QuoRem__0(by int64) (q, r Int128) { 699 ineg := i.hi&signBit != 0 700 if ineg { 701 i = i.Gop_Neg() 702 } 703 byneg := by < 0 704 if byneg { 705 by = -by 706 } 707 708 n := uint64(by) 709 if i.hi < n { 710 q.lo, r.lo = bits.Div64(i.hi, i.lo, n) 711 } else { 712 q.hi, r.lo = bits.Div64(0, i.hi, n) 713 q.lo, r.lo = bits.Div64(r.lo, i.lo, n) 714 } 715 if ineg != byneg { 716 q = q.Gop_Neg() 717 } 718 if ineg { 719 r = r.Gop_Neg() 720 } 721 return q, r 722 } 723 724 // Quo returns the quotient x/y for y != 0. If y == 0, a division-by-zero 725 // run-time panic occurs. Quo implements truncated division (like Go); see 726 // QuoRem for more details. 727 func (i Int128) Gop_Quo__1(by Int128) (q Int128) { 728 qSign := 1 729 if i.Gop_LT__0(0) { 730 qSign = -1 731 i = i.Gop_Neg() 732 } 733 if by.Gop_LT__0(0) { 734 qSign = -qSign 735 by = by.Gop_Neg() 736 } 737 738 qu := i.Gop_Rcast__0().Gop_Quo__1(by.Gop_Rcast__0()) 739 q = Int128_Cast__7(qu) 740 if qSign < 0 { 741 q = q.Gop_Neg() 742 } 743 return q 744 } 745 746 func (i Int128) Gop_Quo__0(by int64) (q Int128) { 747 ineg := i.hi&signBit != 0 748 if ineg { 749 i = i.Gop_Neg() 750 } 751 byneg := by < 0 752 if byneg { 753 by = -by 754 } 755 756 n := uint64(by) 757 if i.hi < n { 758 q.lo, _ = bits.Div64(i.hi, i.lo, n) 759 } else { 760 var rlo uint64 761 q.hi, rlo = bits.Div64(0, i.hi, n) 762 q.lo, _ = bits.Div64(rlo, i.lo, n) 763 } 764 if ineg != byneg { 765 q = q.Gop_Neg() 766 } 767 return q 768 } 769 770 // Gop_Rem returns the remainder of x%y for y != 0. If y == 0, a division-by-zero 771 // run-time panic occurs. Gop_Rem implements truncated modulus (like Go); see 772 // QuoRem for more details. 773 func (i Int128) Gop_Rem__1(by Int128) (r Int128) { 774 _, r = i.QuoRem__1(by) 775 return r 776 } 777 778 func (i Int128) Gop_Rem__0(by int64) (r Int128) { 779 ineg := i.hi&signBit != 0 780 if ineg { 781 i = i.Gop_Neg() 782 } 783 if by < 0 { 784 by = -by 785 } 786 787 n := uint64(by) 788 if i.hi < n { 789 _, r.lo = bits.Div64(i.hi, i.lo, n) 790 } else { 791 _, r.lo = bits.Div64(0, i.hi, n) 792 _, r.lo = bits.Div64(r.lo, i.lo, n) 793 } 794 if ineg { 795 r = r.Gop_Neg() 796 } 797 return r 798 } 799 800 // ----------------------------------------------------------------------------- 801 802 func ParseInt128(s string, base int) (out Int128, err error) { 803 b, ok := new(big.Int).SetString(s, base) 804 if !ok { 805 err = fmt.Errorf("invalid int128 string: %q", s) 806 return 807 } 808 out, inRange := Int128_Cast__9(b) 809 if !inRange { 810 err = fmt.Errorf("string %q was not in valid int128 range", s) 811 } 812 return 813 } 814 815 func FormatInt128(i Int128, base int) string { 816 return i.Text(base) 817 } 818 819 // -----------------------------------------------------------------------------