github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bootstrap/internal/gc/big/int.go (about) 1 // Do not edit. Bootstrap copy of /Users/rsc/g/go/src/cmd/internal/gc/big/int.go 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // This file implements signed multi-precision integers. 8 9 package big 10 11 import ( 12 "fmt" 13 "io" 14 "math/rand" 15 "strings" 16 ) 17 18 // An Int represents a signed multi-precision integer. 19 // The zero value for an Int represents the value 0. 20 type Int struct { 21 neg bool // sign 22 abs nat // absolute value of the integer 23 } 24 25 var intOne = &Int{false, natOne} 26 27 // Sign returns: 28 // 29 // -1 if x < 0 30 // 0 if x == 0 31 // +1 if x > 0 32 // 33 func (x *Int) Sign() int { 34 if len(x.abs) == 0 { 35 return 0 36 } 37 if x.neg { 38 return -1 39 } 40 return 1 41 } 42 43 // SetInt64 sets z to x and returns z. 44 func (z *Int) SetInt64(x int64) *Int { 45 neg := false 46 if x < 0 { 47 neg = true 48 x = -x 49 } 50 z.abs = z.abs.setUint64(uint64(x)) 51 z.neg = neg 52 return z 53 } 54 55 // SetUint64 sets z to x and returns z. 56 func (z *Int) SetUint64(x uint64) *Int { 57 z.abs = z.abs.setUint64(x) 58 z.neg = false 59 return z 60 } 61 62 // NewInt allocates and returns a new Int set to x. 63 func NewInt(x int64) *Int { 64 return new(Int).SetInt64(x) 65 } 66 67 // Set sets z to x and returns z. 68 func (z *Int) Set(x *Int) *Int { 69 if z != x { 70 z.abs = z.abs.set(x.abs) 71 z.neg = x.neg 72 } 73 return z 74 } 75 76 // Bits provides raw (unchecked but fast) access to x by returning its 77 // absolute value as a little-endian Word slice. The result and x share 78 // the same underlying array. 79 // Bits is intended to support implementation of missing low-level Int 80 // functionality outside this package; it should be avoided otherwise. 81 func (x *Int) Bits() []Word { 82 return x.abs 83 } 84 85 // SetBits provides raw (unchecked but fast) access to z by setting its 86 // value to abs, interpreted as a little-endian Word slice, and returning 87 // z. The result and abs share the same underlying array. 88 // SetBits is intended to support implementation of missing low-level Int 89 // functionality outside this package; it should be avoided otherwise. 90 func (z *Int) SetBits(abs []Word) *Int { 91 z.abs = nat(abs).norm() 92 z.neg = false 93 return z 94 } 95 96 // Abs sets z to |x| (the absolute value of x) and returns z. 97 func (z *Int) Abs(x *Int) *Int { 98 z.Set(x) 99 z.neg = false 100 return z 101 } 102 103 // Neg sets z to -x and returns z. 104 func (z *Int) Neg(x *Int) *Int { 105 z.Set(x) 106 z.neg = len(z.abs) > 0 && !z.neg // 0 has no sign 107 return z 108 } 109 110 // Add sets z to the sum x+y and returns z. 111 func (z *Int) Add(x, y *Int) *Int { 112 neg := x.neg 113 if x.neg == y.neg { 114 // x + y == x + y 115 // (-x) + (-y) == -(x + y) 116 z.abs = z.abs.add(x.abs, y.abs) 117 } else { 118 // x + (-y) == x - y == -(y - x) 119 // (-x) + y == y - x == -(x - y) 120 if x.abs.cmp(y.abs) >= 0 { 121 z.abs = z.abs.sub(x.abs, y.abs) 122 } else { 123 neg = !neg 124 z.abs = z.abs.sub(y.abs, x.abs) 125 } 126 } 127 z.neg = len(z.abs) > 0 && neg // 0 has no sign 128 return z 129 } 130 131 // Sub sets z to the difference x-y and returns z. 132 func (z *Int) Sub(x, y *Int) *Int { 133 neg := x.neg 134 if x.neg != y.neg { 135 // x - (-y) == x + y 136 // (-x) - y == -(x + y) 137 z.abs = z.abs.add(x.abs, y.abs) 138 } else { 139 // x - y == x - y == -(y - x) 140 // (-x) - (-y) == y - x == -(x - y) 141 if x.abs.cmp(y.abs) >= 0 { 142 z.abs = z.abs.sub(x.abs, y.abs) 143 } else { 144 neg = !neg 145 z.abs = z.abs.sub(y.abs, x.abs) 146 } 147 } 148 z.neg = len(z.abs) > 0 && neg // 0 has no sign 149 return z 150 } 151 152 // Mul sets z to the product x*y and returns z. 153 func (z *Int) Mul(x, y *Int) *Int { 154 // x * y == x * y 155 // x * (-y) == -(x * y) 156 // (-x) * y == -(x * y) 157 // (-x) * (-y) == x * y 158 z.abs = z.abs.mul(x.abs, y.abs) 159 z.neg = len(z.abs) > 0 && x.neg != y.neg // 0 has no sign 160 return z 161 } 162 163 // MulRange sets z to the product of all integers 164 // in the range [a, b] inclusively and returns z. 165 // If a > b (empty range), the result is 1. 166 func (z *Int) MulRange(a, b int64) *Int { 167 switch { 168 case a > b: 169 return z.SetInt64(1) // empty range 170 case a <= 0 && b >= 0: 171 return z.SetInt64(0) // range includes 0 172 } 173 // a <= b && (b < 0 || a > 0) 174 175 neg := false 176 if a < 0 { 177 neg = (b-a)&1 == 0 178 a, b = -b, -a 179 } 180 181 z.abs = z.abs.mulRange(uint64(a), uint64(b)) 182 z.neg = neg 183 return z 184 } 185 186 // Binomial sets z to the binomial coefficient of (n, k) and returns z. 187 func (z *Int) Binomial(n, k int64) *Int { 188 // reduce the number of multiplications by reducing k 189 if n/2 < k && k <= n { 190 k = n - k // Binomial(n, k) == Binomial(n, n-k) 191 } 192 var a, b Int 193 a.MulRange(n-k+1, n) 194 b.MulRange(1, k) 195 return z.Quo(&a, &b) 196 } 197 198 // Quo sets z to the quotient x/y for y != 0 and returns z. 199 // If y == 0, a division-by-zero run-time panic occurs. 200 // Quo implements truncated division (like Go); see QuoRem for more details. 201 func (z *Int) Quo(x, y *Int) *Int { 202 z.abs, _ = z.abs.div(nil, x.abs, y.abs) 203 z.neg = len(z.abs) > 0 && x.neg != y.neg // 0 has no sign 204 return z 205 } 206 207 // Rem sets z to the remainder x%y for y != 0 and returns z. 208 // If y == 0, a division-by-zero run-time panic occurs. 209 // Rem implements truncated modulus (like Go); see QuoRem for more details. 210 func (z *Int) Rem(x, y *Int) *Int { 211 _, z.abs = nat(nil).div(z.abs, x.abs, y.abs) 212 z.neg = len(z.abs) > 0 && x.neg // 0 has no sign 213 return z 214 } 215 216 // QuoRem sets z to the quotient x/y and r to the remainder x%y 217 // and returns the pair (z, r) for y != 0. 218 // If y == 0, a division-by-zero run-time panic occurs. 219 // 220 // QuoRem implements T-division and modulus (like Go): 221 // 222 // q = x/y with the result truncated to zero 223 // r = x - y*q 224 // 225 // (See Daan Leijen, ``Division and Modulus for Computer Scientists''.) 226 // See DivMod for Euclidean division and modulus (unlike Go). 227 // 228 func (z *Int) QuoRem(x, y, r *Int) (*Int, *Int) { 229 z.abs, r.abs = z.abs.div(r.abs, x.abs, y.abs) 230 z.neg, r.neg = len(z.abs) > 0 && x.neg != y.neg, len(r.abs) > 0 && x.neg // 0 has no sign 231 return z, r 232 } 233 234 // Div sets z to the quotient x/y for y != 0 and returns z. 235 // If y == 0, a division-by-zero run-time panic occurs. 236 // Div implements Euclidean division (unlike Go); see DivMod for more details. 237 func (z *Int) Div(x, y *Int) *Int { 238 y_neg := y.neg // z may be an alias for y 239 var r Int 240 z.QuoRem(x, y, &r) 241 if r.neg { 242 if y_neg { 243 z.Add(z, intOne) 244 } else { 245 z.Sub(z, intOne) 246 } 247 } 248 return z 249 } 250 251 // Mod sets z to the modulus x%y for y != 0 and returns z. 252 // If y == 0, a division-by-zero run-time panic occurs. 253 // Mod implements Euclidean modulus (unlike Go); see DivMod for more details. 254 func (z *Int) Mod(x, y *Int) *Int { 255 y0 := y // save y 256 if z == y || alias(z.abs, y.abs) { 257 y0 = new(Int).Set(y) 258 } 259 var q Int 260 q.QuoRem(x, y, z) 261 if z.neg { 262 if y0.neg { 263 z.Sub(z, y0) 264 } else { 265 z.Add(z, y0) 266 } 267 } 268 return z 269 } 270 271 // DivMod sets z to the quotient x div y and m to the modulus x mod y 272 // and returns the pair (z, m) for y != 0. 273 // If y == 0, a division-by-zero run-time panic occurs. 274 // 275 // DivMod implements Euclidean division and modulus (unlike Go): 276 // 277 // q = x div y such that 278 // m = x - y*q with 0 <= m < |q| 279 // 280 // (See Raymond T. Boute, ``The Euclidean definition of the functions 281 // div and mod''. ACM Transactions on Programming Languages and 282 // Systems (TOPLAS), 14(2):127-144, New York, NY, USA, 4/1992. 283 // ACM press.) 284 // See QuoRem for T-division and modulus (like Go). 285 // 286 func (z *Int) DivMod(x, y, m *Int) (*Int, *Int) { 287 y0 := y // save y 288 if z == y || alias(z.abs, y.abs) { 289 y0 = new(Int).Set(y) 290 } 291 z.QuoRem(x, y, m) 292 if m.neg { 293 if y0.neg { 294 z.Add(z, intOne) 295 m.Sub(m, y0) 296 } else { 297 z.Sub(z, intOne) 298 m.Add(m, y0) 299 } 300 } 301 return z, m 302 } 303 304 // Cmp compares x and y and returns: 305 // 306 // -1 if x < y 307 // 0 if x == y 308 // +1 if x > y 309 // 310 func (x *Int) Cmp(y *Int) (r int) { 311 // x cmp y == x cmp y 312 // x cmp (-y) == x 313 // (-x) cmp y == y 314 // (-x) cmp (-y) == -(x cmp y) 315 switch { 316 case x.neg == y.neg: 317 r = x.abs.cmp(y.abs) 318 if x.neg { 319 r = -r 320 } 321 case x.neg: 322 r = -1 323 default: 324 r = 1 325 } 326 return 327 } 328 329 // low32 returns the least significant 32 bits of z. 330 func low32(z nat) uint32 { 331 if len(z) == 0 { 332 return 0 333 } 334 return uint32(z[0]) 335 } 336 337 // low64 returns the least significant 64 bits of z. 338 func low64(z nat) uint64 { 339 if len(z) == 0 { 340 return 0 341 } 342 v := uint64(z[0]) 343 if _W == 32 && len(z) > 1 { 344 v |= uint64(z[1]) << 32 345 } 346 return v 347 } 348 349 // Int64 returns the int64 representation of x. 350 // If x cannot be represented in an int64, the result is undefined. 351 func (x *Int) Int64() int64 { 352 v := int64(low64(x.abs)) 353 if x.neg { 354 v = -v 355 } 356 return v 357 } 358 359 // Uint64 returns the uint64 representation of x. 360 // If x cannot be represented in a uint64, the result is undefined. 361 func (x *Int) Uint64() uint64 { 362 return low64(x.abs) 363 } 364 365 // SetString sets z to the value of s, interpreted in the given base, 366 // and returns z and a boolean indicating success. If SetString fails, 367 // the value of z is undefined but the returned value is nil. 368 // 369 // The base argument must be 0 or a value between 2 and MaxBase. If the base 370 // is 0, the string prefix determines the actual conversion base. A prefix of 371 // ``0x'' or ``0X'' selects base 16; the ``0'' prefix selects base 8, and a 372 // ``0b'' or ``0B'' prefix selects base 2. Otherwise the selected base is 10. 373 // 374 func (z *Int) SetString(s string, base int) (*Int, bool) { 375 r := strings.NewReader(s) 376 _, _, err := z.scan(r, base) 377 if err != nil { 378 return nil, false 379 } 380 _, err = r.ReadByte() 381 if err != io.EOF { 382 return nil, false 383 } 384 return z, true // err == io.EOF => scan consumed all of s 385 } 386 387 // SetBytes interprets buf as the bytes of a big-endian unsigned 388 // integer, sets z to that value, and returns z. 389 func (z *Int) SetBytes(buf []byte) *Int { 390 z.abs = z.abs.setBytes(buf) 391 z.neg = false 392 return z 393 } 394 395 // Bytes returns the absolute value of x as a big-endian byte slice. 396 func (x *Int) Bytes() []byte { 397 buf := make([]byte, len(x.abs)*_S) 398 return buf[x.abs.bytes(buf):] 399 } 400 401 // BitLen returns the length of the absolute value of x in bits. 402 // The bit length of 0 is 0. 403 func (x *Int) BitLen() int { 404 return x.abs.bitLen() 405 } 406 407 // Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z. 408 // If y <= 0, the result is 1 mod |m|; if m == nil or m == 0, z = x**y. 409 // See Knuth, volume 2, section 4.6.3. 410 func (z *Int) Exp(x, y, m *Int) *Int { 411 var yWords nat 412 if !y.neg { 413 yWords = y.abs 414 } 415 // y >= 0 416 417 var mWords nat 418 if m != nil { 419 mWords = m.abs // m.abs may be nil for m == 0 420 } 421 422 z.abs = z.abs.expNN(x.abs, yWords, mWords) 423 z.neg = len(z.abs) > 0 && x.neg && len(yWords) > 0 && yWords[0]&1 == 1 // 0 has no sign 424 if z.neg && len(mWords) > 0 { 425 // make modulus result positive 426 z.abs = z.abs.sub(mWords, z.abs) // z == x**y mod |m| && 0 <= z < |m| 427 z.neg = false 428 } 429 430 return z 431 } 432 433 // GCD sets z to the greatest common divisor of a and b, which both must 434 // be > 0, and returns z. 435 // If x and y are not nil, GCD sets x and y such that z = a*x + b*y. 436 // If either a or b is <= 0, GCD sets z = x = y = 0. 437 func (z *Int) GCD(x, y, a, b *Int) *Int { 438 if a.Sign() <= 0 || b.Sign() <= 0 { 439 z.SetInt64(0) 440 if x != nil { 441 x.SetInt64(0) 442 } 443 if y != nil { 444 y.SetInt64(0) 445 } 446 return z 447 } 448 if x == nil && y == nil { 449 return z.binaryGCD(a, b) 450 } 451 452 A := new(Int).Set(a) 453 B := new(Int).Set(b) 454 455 X := new(Int) 456 Y := new(Int).SetInt64(1) 457 458 lastX := new(Int).SetInt64(1) 459 lastY := new(Int) 460 461 q := new(Int) 462 temp := new(Int) 463 464 for len(B.abs) > 0 { 465 r := new(Int) 466 q, r = q.QuoRem(A, B, r) 467 468 A, B = B, r 469 470 temp.Set(X) 471 X.Mul(X, q) 472 X.neg = !X.neg 473 X.Add(X, lastX) 474 lastX.Set(temp) 475 476 temp.Set(Y) 477 Y.Mul(Y, q) 478 Y.neg = !Y.neg 479 Y.Add(Y, lastY) 480 lastY.Set(temp) 481 } 482 483 if x != nil { 484 *x = *lastX 485 } 486 487 if y != nil { 488 *y = *lastY 489 } 490 491 *z = *A 492 return z 493 } 494 495 // binaryGCD sets z to the greatest common divisor of a and b, which both must 496 // be > 0, and returns z. 497 // See Knuth, The Art of Computer Programming, Vol. 2, Section 4.5.2, Algorithm B. 498 func (z *Int) binaryGCD(a, b *Int) *Int { 499 u := z 500 v := new(Int) 501 502 // use one Euclidean iteration to ensure that u and v are approx. the same size 503 switch { 504 case len(a.abs) > len(b.abs): 505 u.Set(b) 506 v.Rem(a, b) 507 case len(a.abs) < len(b.abs): 508 u.Set(a) 509 v.Rem(b, a) 510 default: 511 u.Set(a) 512 v.Set(b) 513 } 514 515 // v might be 0 now 516 if len(v.abs) == 0 { 517 return u 518 } 519 // u > 0 && v > 0 520 521 // determine largest k such that u = u' << k, v = v' << k 522 k := u.abs.trailingZeroBits() 523 if vk := v.abs.trailingZeroBits(); vk < k { 524 k = vk 525 } 526 u.Rsh(u, k) 527 v.Rsh(v, k) 528 529 // determine t (we know that u > 0) 530 t := new(Int) 531 if u.abs[0]&1 != 0 { 532 // u is odd 533 t.Neg(v) 534 } else { 535 t.Set(u) 536 } 537 538 for len(t.abs) > 0 { 539 // reduce t 540 t.Rsh(t, t.abs.trailingZeroBits()) 541 if t.neg { 542 v, t = t, v 543 v.neg = len(v.abs) > 0 && !v.neg // 0 has no sign 544 } else { 545 u, t = t, u 546 } 547 t.Sub(u, v) 548 } 549 550 return z.Lsh(u, k) 551 } 552 553 // ProbablyPrime performs n Miller-Rabin tests to check whether x is prime. 554 // If it returns true, x is prime with probability 1 - 1/4^n. 555 // If it returns false, x is not prime. n must be > 0. 556 func (x *Int) ProbablyPrime(n int) bool { 557 if n <= 0 { 558 panic("non-positive n for ProbablyPrime") 559 } 560 return !x.neg && x.abs.probablyPrime(n) 561 } 562 563 // Rand sets z to a pseudo-random number in [0, n) and returns z. 564 func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int { 565 z.neg = false 566 if n.neg == true || len(n.abs) == 0 { 567 z.abs = nil 568 return z 569 } 570 z.abs = z.abs.random(rnd, n.abs, n.abs.bitLen()) 571 return z 572 } 573 574 // ModInverse sets z to the multiplicative inverse of g in the ring ℤ/nℤ 575 // and returns z. If g and n are not relatively prime, the result is undefined. 576 func (z *Int) ModInverse(g, n *Int) *Int { 577 var d Int 578 d.GCD(z, nil, g, n) 579 // x and y are such that g*x + n*y = d. Since g and n are 580 // relatively prime, d = 1. Taking that modulo n results in 581 // g*x = 1, therefore x is the inverse element. 582 if z.neg { 583 z.Add(z, n) 584 } 585 return z 586 } 587 588 // Lsh sets z = x << n and returns z. 589 func (z *Int) Lsh(x *Int, n uint) *Int { 590 z.abs = z.abs.shl(x.abs, n) 591 z.neg = x.neg 592 return z 593 } 594 595 // Rsh sets z = x >> n and returns z. 596 func (z *Int) Rsh(x *Int, n uint) *Int { 597 if x.neg { 598 // (-x) >> s == ^(x-1) >> s == ^((x-1) >> s) == -(((x-1) >> s) + 1) 599 t := z.abs.sub(x.abs, natOne) // no underflow because |x| > 0 600 t = t.shr(t, n) 601 z.abs = t.add(t, natOne) 602 z.neg = true // z cannot be zero if x is negative 603 return z 604 } 605 606 z.abs = z.abs.shr(x.abs, n) 607 z.neg = false 608 return z 609 } 610 611 // Bit returns the value of the i'th bit of x. That is, it 612 // returns (x>>i)&1. The bit index i must be >= 0. 613 func (x *Int) Bit(i int) uint { 614 if i == 0 { 615 // optimization for common case: odd/even test of x 616 if len(x.abs) > 0 { 617 return uint(x.abs[0] & 1) // bit 0 is same for -x 618 } 619 return 0 620 } 621 if i < 0 { 622 panic("negative bit index") 623 } 624 if x.neg { 625 t := nat(nil).sub(x.abs, natOne) 626 return t.bit(uint(i)) ^ 1 627 } 628 629 return x.abs.bit(uint(i)) 630 } 631 632 // SetBit sets z to x, with x's i'th bit set to b (0 or 1). 633 // That is, if b is 1 SetBit sets z = x | (1 << i); 634 // if b is 0 SetBit sets z = x &^ (1 << i). If b is not 0 or 1, 635 // SetBit will panic. 636 func (z *Int) SetBit(x *Int, i int, b uint) *Int { 637 if i < 0 { 638 panic("negative bit index") 639 } 640 if x.neg { 641 t := z.abs.sub(x.abs, natOne) 642 t = t.setBit(t, uint(i), b^1) 643 z.abs = t.add(t, natOne) 644 z.neg = len(z.abs) > 0 645 return z 646 } 647 z.abs = z.abs.setBit(x.abs, uint(i), b) 648 z.neg = false 649 return z 650 } 651 652 // And sets z = x & y and returns z. 653 func (z *Int) And(x, y *Int) *Int { 654 if x.neg == y.neg { 655 if x.neg { 656 // (-x) & (-y) == ^(x-1) & ^(y-1) == ^((x-1) | (y-1)) == -(((x-1) | (y-1)) + 1) 657 x1 := nat(nil).sub(x.abs, natOne) 658 y1 := nat(nil).sub(y.abs, natOne) 659 z.abs = z.abs.add(z.abs.or(x1, y1), natOne) 660 z.neg = true // z cannot be zero if x and y are negative 661 return z 662 } 663 664 // x & y == x & y 665 z.abs = z.abs.and(x.abs, y.abs) 666 z.neg = false 667 return z 668 } 669 670 // x.neg != y.neg 671 if x.neg { 672 x, y = y, x // & is symmetric 673 } 674 675 // x & (-y) == x & ^(y-1) == x &^ (y-1) 676 y1 := nat(nil).sub(y.abs, natOne) 677 z.abs = z.abs.andNot(x.abs, y1) 678 z.neg = false 679 return z 680 } 681 682 // AndNot sets z = x &^ y and returns z. 683 func (z *Int) AndNot(x, y *Int) *Int { 684 if x.neg == y.neg { 685 if x.neg { 686 // (-x) &^ (-y) == ^(x-1) &^ ^(y-1) == ^(x-1) & (y-1) == (y-1) &^ (x-1) 687 x1 := nat(nil).sub(x.abs, natOne) 688 y1 := nat(nil).sub(y.abs, natOne) 689 z.abs = z.abs.andNot(y1, x1) 690 z.neg = false 691 return z 692 } 693 694 // x &^ y == x &^ y 695 z.abs = z.abs.andNot(x.abs, y.abs) 696 z.neg = false 697 return z 698 } 699 700 if x.neg { 701 // (-x) &^ y == ^(x-1) &^ y == ^(x-1) & ^y == ^((x-1) | y) == -(((x-1) | y) + 1) 702 x1 := nat(nil).sub(x.abs, natOne) 703 z.abs = z.abs.add(z.abs.or(x1, y.abs), natOne) 704 z.neg = true // z cannot be zero if x is negative and y is positive 705 return z 706 } 707 708 // x &^ (-y) == x &^ ^(y-1) == x & (y-1) 709 y1 := nat(nil).sub(y.abs, natOne) 710 z.abs = z.abs.and(x.abs, y1) 711 z.neg = false 712 return z 713 } 714 715 // Or sets z = x | y and returns z. 716 func (z *Int) Or(x, y *Int) *Int { 717 if x.neg == y.neg { 718 if x.neg { 719 // (-x) | (-y) == ^(x-1) | ^(y-1) == ^((x-1) & (y-1)) == -(((x-1) & (y-1)) + 1) 720 x1 := nat(nil).sub(x.abs, natOne) 721 y1 := nat(nil).sub(y.abs, natOne) 722 z.abs = z.abs.add(z.abs.and(x1, y1), natOne) 723 z.neg = true // z cannot be zero if x and y are negative 724 return z 725 } 726 727 // x | y == x | y 728 z.abs = z.abs.or(x.abs, y.abs) 729 z.neg = false 730 return z 731 } 732 733 // x.neg != y.neg 734 if x.neg { 735 x, y = y, x // | is symmetric 736 } 737 738 // x | (-y) == x | ^(y-1) == ^((y-1) &^ x) == -(^((y-1) &^ x) + 1) 739 y1 := nat(nil).sub(y.abs, natOne) 740 z.abs = z.abs.add(z.abs.andNot(y1, x.abs), natOne) 741 z.neg = true // z cannot be zero if one of x or y is negative 742 return z 743 } 744 745 // Xor sets z = x ^ y and returns z. 746 func (z *Int) Xor(x, y *Int) *Int { 747 if x.neg == y.neg { 748 if x.neg { 749 // (-x) ^ (-y) == ^(x-1) ^ ^(y-1) == (x-1) ^ (y-1) 750 x1 := nat(nil).sub(x.abs, natOne) 751 y1 := nat(nil).sub(y.abs, natOne) 752 z.abs = z.abs.xor(x1, y1) 753 z.neg = false 754 return z 755 } 756 757 // x ^ y == x ^ y 758 z.abs = z.abs.xor(x.abs, y.abs) 759 z.neg = false 760 return z 761 } 762 763 // x.neg != y.neg 764 if x.neg { 765 x, y = y, x // ^ is symmetric 766 } 767 768 // x ^ (-y) == x ^ ^(y-1) == ^(x ^ (y-1)) == -((x ^ (y-1)) + 1) 769 y1 := nat(nil).sub(y.abs, natOne) 770 z.abs = z.abs.add(z.abs.xor(x.abs, y1), natOne) 771 z.neg = true // z cannot be zero if only one of x or y is negative 772 return z 773 } 774 775 // Not sets z = ^x and returns z. 776 func (z *Int) Not(x *Int) *Int { 777 if x.neg { 778 // ^(-x) == ^(^(x-1)) == x-1 779 z.abs = z.abs.sub(x.abs, natOne) 780 z.neg = false 781 return z 782 } 783 784 // ^x == -x-1 == -(x+1) 785 z.abs = z.abs.add(x.abs, natOne) 786 z.neg = true // z cannot be zero if x is positive 787 return z 788 } 789 790 // Gob codec version. Permits backward-compatible changes to the encoding. 791 const intGobVersion byte = 1 792 793 // GobEncode implements the gob.GobEncoder interface. 794 func (x *Int) GobEncode() ([]byte, error) { 795 if x == nil { 796 return nil, nil 797 } 798 buf := make([]byte, 1+len(x.abs)*_S) // extra byte for version and sign bit 799 i := x.abs.bytes(buf) - 1 // i >= 0 800 b := intGobVersion << 1 // make space for sign bit 801 if x.neg { 802 b |= 1 803 } 804 buf[i] = b 805 return buf[i:], nil 806 } 807 808 // GobDecode implements the gob.GobDecoder interface. 809 func (z *Int) GobDecode(buf []byte) error { 810 if len(buf) == 0 { 811 // Other side sent a nil or default value. 812 *z = Int{} 813 return nil 814 } 815 b := buf[0] 816 if b>>1 != intGobVersion { 817 return fmt.Errorf("Int.GobDecode: encoding version %d not supported", b>>1) 818 } 819 z.neg = b&1 != 0 820 z.abs = z.abs.setBytes(buf[1:]) 821 return nil 822 } 823 824 // MarshalJSON implements the json.Marshaler interface. 825 func (z *Int) MarshalJSON() ([]byte, error) { 826 // TODO(gri): get rid of the []byte/string conversions 827 return []byte(z.String()), nil 828 } 829 830 // UnmarshalJSON implements the json.Unmarshaler interface. 831 func (z *Int) UnmarshalJSON(text []byte) error { 832 // TODO(gri): get rid of the []byte/string conversions 833 if _, ok := z.SetString(string(text), 0); !ok { 834 return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text) 835 } 836 return nil 837 } 838 839 // MarshalText implements the encoding.TextMarshaler interface. 840 func (z *Int) MarshalText() (text []byte, err error) { 841 return []byte(z.String()), nil 842 } 843 844 // UnmarshalText implements the encoding.TextUnmarshaler interface. 845 func (z *Int) UnmarshalText(text []byte) error { 846 if _, ok := z.SetString(string(text), 0); !ok { 847 return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text) 848 } 849 return nil 850 }