public BigIntegerBuilder(ref BigIntegerBuilder reg) { reg.AssertValid(true); this = reg; if (_fWritable) { _fWritable = false; if (_iuLast == 0) _rgu = null; else reg._fWritable = false; } AssertValid(true); }
// Divide 'this' by regDen, leaving the quotient in 'this' and tossing the remainder. public void Div(ref BigIntegerBuilder regDen) { AssertValid(true); regDen.AssertValid(true); if (regDen._iuLast == 0) { DivMod(regDen._uSmall); return; } if (_iuLast == 0) { _uSmall = 0; return; } BigIntegerBuilder regTmp = new BigIntegerBuilder(); ModDivCore(ref this, ref regDen, true, ref regTmp); NumericsHelpers.Swap(ref this, ref regTmp); }
// Divide 'this' by regDen, leaving the remainder in 'this' and tossing the quotient. public void Mod(ref BigIntegerBuilder regDen) { AssertValid(true); regDen.AssertValid(true); if (regDen._iuLast == 0) { Set(Mod(ref this, regDen._uSmall)); return; } if (_iuLast == 0) return; BigIntegerBuilder regTmp = new BigIntegerBuilder(); ModDivCore(ref this, ref regDen, false, ref regTmp); }
// Divide regNum by uDen, returning the remainder and tossing the quotient. public static uint Mod(ref BigIntegerBuilder regNum, uint uDen) { regNum.AssertValid(true); if (uDen == 1) return 0; if (regNum._iuLast == 0) return regNum._uSmall % uDen; ulong uu = 0; for (int iv = regNum._iuLast; iv >= 0; iv--) { uu = NumericsHelpers.MakeUlong((uint)uu, regNum._rgu[iv]); uu %= uDen; } return (uint)uu; }
// Multiply reg1 times reg2, putting the result in 'this'. This version never shares memory // with either of the operands. This is useful when performing a series of arithmetic operations // and large working buffers are allocated up front. public void Mul(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2) { AssertValid(true); reg1.AssertValid(true); reg2.AssertValid(true); if (reg1._iuLast == 0) { if (reg2._iuLast == 0) Set((ulong)reg1._uSmall * reg2._uSmall); else { Load(ref reg2, 1); Mul(reg1._uSmall); } } else if (reg2._iuLast == 0) { Load(ref reg1, 1); Mul(reg2._uSmall); } else { Contract.Assert(reg1._iuLast > 0 && reg2._iuLast > 0); SetSizeClear(reg1._iuLast + reg2._iuLast + 2); uint[] rgu1, rgu2; int cu1, cu2; // We prefer more iterations on the inner loop and fewer on the outer. if (reg1.CuNonZero <= reg2.CuNonZero) { rgu1 = reg1._rgu; cu1 = reg1._iuLast + 1; rgu2 = reg2._rgu; cu2 = reg2._iuLast + 1; } else { rgu1 = reg2._rgu; cu1 = reg2._iuLast + 1; rgu2 = reg1._rgu; cu2 = reg1._iuLast + 1; } for (int iu1 = 0; iu1 < cu1; iu1++) { uint uCur = rgu1[iu1]; if (uCur == 0) continue; uint uCarry = 0; int iuRes = iu1; for (int iu2 = 0; iu2 < cu2; iu2++, iuRes++) uCarry = AddMulCarry(ref _rgu[iuRes], uCur, rgu2[iu2], uCarry); while (uCarry != 0) uCarry = AddCarry(ref _rgu[iuRes++], 0, uCarry); } Trim(); } }
// This version may share memory with regMul. public void Mul(ref BigIntegerBuilder regMul) { AssertValid(true); regMul.AssertValid(true); if (regMul._iuLast == 0) Mul(regMul._uSmall); else if (_iuLast == 0) { uint u = _uSmall; if (u == 1) this = new BigIntegerBuilder(ref regMul); else if (u != 0) { Load(ref regMul, 1); Mul(u); } } else { int cuBase = _iuLast + 1; SetSizeKeep(cuBase + regMul._iuLast, 1); for (int iu = cuBase; --iu >= 0; ) { uint uMul = _rgu[iu]; _rgu[iu] = 0; uint uCarry = 0; for (int iuSrc = 0; iuSrc <= regMul._iuLast; iuSrc++) uCarry = AddMulCarry(ref _rgu[iu + iuSrc], regMul._rgu[iuSrc], uMul, uCarry); if (uCarry != 0) { for (int iuDst = iu + regMul._iuLast + 1; uCarry != 0 && iuDst <= _iuLast; iuDst++) uCarry = AddCarry(ref _rgu[iuDst], 0, uCarry); if (uCarry != 0) { SetSizeKeep(_iuLast + 2, 0); _rgu[_iuLast] = uCarry; } } } AssertValid(true); } }
public void Sub(ref int sign, ref BigIntegerBuilder reg) { Contract.Requires(sign == +1 || sign == -1); AssertValid(true); reg.AssertValid(true); if (reg._iuLast == 0) { Sub(ref sign, reg._uSmall); return; } if (_iuLast == 0) { uint u = _uSmall; if (u == 0) this = new BigIntegerBuilder(ref reg); else { Load(ref reg); Sub(ref sign, u); } sign = -sign; return; } if (_iuLast < reg._iuLast) { SubRev(ref reg); sign = -sign; return; } int cuSub = reg._iuLast + 1; if (_iuLast == reg._iuLast) { // Determine which is larger. _iuLast = BigInteger.GetDiffLength(_rgu, reg._rgu, _iuLast + 1) - 1; if (_iuLast < 0) { _iuLast = 0; _uSmall = 0; return; } uint u1 = _rgu[_iuLast]; uint u2 = reg._rgu[_iuLast]; if (_iuLast == 0) { if (u1 < u2) { _uSmall = u2 - u1; sign = -sign; } else _uSmall = u1 - u2; AssertValid(true); return; } if (u1 < u2) { Contract.Assert(_iuLast > 0); reg._iuLast = _iuLast; SubRev(ref reg); reg._iuLast = cuSub - 1; Contract.Assert(reg._iuLast > 0); sign = -sign; return; } cuSub = _iuLast + 1; } EnsureWritable(); // Subtract, tracking borrow. uint uBorrow = 0; for (int iu = 0; iu < cuSub; iu++) { uBorrow = SubBorrow(ref _rgu[iu], reg._rgu[iu], uBorrow); Contract.Assert(uBorrow <= 1); } if (uBorrow != 0) { Contract.Assert(uBorrow == 1 && cuSub <= _iuLast); ApplyBorrow(cuSub); } Trim(); }
public void Add(ref BigIntegerBuilder reg) { AssertValid(true); reg.AssertValid(true); if (reg._iuLast == 0) { Add(reg._uSmall); return; } if (_iuLast == 0) { uint u = _uSmall; if (u == 0) this = new BigIntegerBuilder(ref reg); else { Load(ref reg, 1); Add(u); } return; } EnsureWritable(Math.Max(_iuLast, reg._iuLast) + 1, 1); int cuAdd = reg._iuLast + 1; if (_iuLast < reg._iuLast) { cuAdd = _iuLast + 1; Array.Copy(reg._rgu, _iuLast + 1, _rgu, _iuLast + 1, reg._iuLast - _iuLast); Contract.Assert(_iuLast > 0); _iuLast = reg._iuLast; } // Add, tracking carry. uint uCarry = 0; for (int iu = 0; iu < cuAdd; iu++) { uCarry = AddCarry(ref _rgu[iu], reg._rgu[iu], uCarry); Contract.Assert(uCarry <= 1); } // Deal with extra carry. if (uCarry != 0) ApplyCarry(cuAdd); AssertValid(true); }
// Loads the value of reg into this register. If we need to allocate memory // to perform the load, allocate cuExtra elements. public void Load(ref BigIntegerBuilder reg, int cuExtra) { Contract.Requires(cuExtra >= 0); AssertValid(false); reg.AssertValid(true); if (reg._iuLast == 0) { _uSmall = reg._uSmall; _iuLast = 0; } else { if (!_fWritable || _rgu.Length <= reg._iuLast) { _rgu = new uint[reg._iuLast + 1 + cuExtra]; _fWritable = true; } _iuLast = reg._iuLast; Array.Copy(reg._rgu, _rgu, _iuLast + 1); } AssertValid(true); }
// This leaves the GCD in reg1 and trash in reg2. // This uses Lehmer's method, with test due to Jebelean / Belnkiy and Vidunas. // See Knuth, vol 2, page 345; Jebelean (1993) "Improving the Multiprecision Euclidean Algorithm"; // and Belenkiy & Vidunas (1998) "A Greatest Common Divisor Algorithm". private static void LehmerGcd(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2) { // This value has no real significance. Occ----ionally we want to subtract // the two registers and keep the absolute value of the difference. To do // so we need to pass a ref sign to Sub. int signTmp = +1; for (; ; ) { reg1.AssertValid(true); reg2.AssertValid(true); int cuMax = reg1._iuLast + 1; int cuMin = reg2._iuLast + 1; if (cuMax < cuMin) { NumericsHelpers.Swap(ref reg1, ref reg2); NumericsHelpers.Swap(ref cuMax, ref cuMin); } Contract.Assert(cuMax == reg1._iuLast + 1); Contract.Assert(cuMin == reg2._iuLast + 1); if (cuMin == 1) { if (cuMax == 1) reg1._uSmall = NumericsHelpers.GCD(reg1._uSmall, reg2._uSmall); else if (reg2._uSmall != 0) reg1.Set(NumericsHelpers.GCD(Mod(ref reg1, reg2._uSmall), reg2._uSmall)); return; } if (cuMax == 2) { reg1.Set(NumericsHelpers.GCD(reg1.GetHigh2(2), reg2.GetHigh2(2))); return; } if (cuMin <= cuMax - 2) { // reg1 is much larger than reg2, so just mod. reg1.Mod(ref reg2); continue; } ulong uu1 = reg1.GetHigh2(cuMax); ulong uu2 = reg2.GetHigh2(cuMax); Contract.Assert(uu1 != 0 && uu2 != 0); int cbit = NumericsHelpers.CbitHighZero(uu1 | uu2); if (cbit > 0) { uu1 = (uu1 << cbit) | (reg1._rgu[cuMax - 3] >> (kcbitUint - cbit)); // Note that [cuMax - 3] is correct, NOT [cuMin - 3]. uu2 = (uu2 << cbit) | (reg2._rgu[cuMax - 3] >> (kcbitUint - cbit)); } if (uu1 < uu2) { NumericsHelpers.Swap(ref uu1, ref uu2); NumericsHelpers.Swap(ref reg1, ref reg2); } // Make sure we don't overflow. if (uu1 == ulong.MaxValue || uu2 == ulong.MaxValue) { uu1 >>= 1; uu2 >>= 1; } Contract.Assert(uu1 >= uu2); // We ensured this above. if (uu1 == uu2) { // The high bits are the same, so we don't know which // is larger. No matter, just subtract one from the other // and keep the absolute value of the result. Contract.Assert(cuMax == cuMin); reg1.Sub(ref signTmp, ref reg2); Contract.Assert(reg1._iuLast < cuMin - 1); continue; } if (NumericsHelpers.GetHi(uu2) == 0) { // reg1 is much larger than reg2, so just mod. reg1.Mod(ref reg2); continue; } // These are the coefficients to apply to reg1 and reg2 to get // the new values, using: a * reg1 - b * reg2 and -c * reg1 + d * reg2. uint a = 1, b = 0; uint c = 0, d = 1; for (; ; ) { Contract.Assert(uu1 + a > a); // no overflow Contract.Assert(uu2 + d > d); Contract.Assert(uu1 > b); Contract.Assert(uu2 > c); Contract.Assert(uu2 + d <= uu1 - b); uint uQuo = 1; ulong uuNew = uu1 - uu2; while (uuNew >= uu2 && uQuo < 32) { uuNew -= uu2; uQuo++; } if (uuNew >= uu2) { ulong uuQuo = uu1 / uu2; if (uuQuo > uint.MaxValue) break; uQuo = (uint)uuQuo; uuNew = uu1 - uQuo * uu2; } ulong uuAdNew = a + (ulong)uQuo * c; ulong uuBcNew = b + (ulong)uQuo * d; if (uuAdNew > int.MaxValue || uuBcNew > int.MaxValue) break; // Jebelean / Belenkiy-Vidunas conditions if (uuNew < uuBcNew || uuNew + uuAdNew > uu2 - c) break; Contract.Assert(uQuo == (uu1 + a - 1) / (uu2 - c)); Contract.Assert(uQuo == (uu1 - b) / (uu2 + d)); a = (uint)uuAdNew; b = (uint)uuBcNew; uu1 = uuNew; if (uu1 <= b) { Contract.Assert(uu1 == b); break; } Contract.Assert(uu1 + a > a); // no overflow Contract.Assert(uu2 + d > d); Contract.Assert(uu2 > c); Contract.Assert(uu1 > b); Contract.Assert(uu1 + a <= uu2 - c); uQuo = 1; uuNew = uu2 - uu1; while (uuNew >= uu1 && uQuo < 32) { uuNew -= uu1; uQuo++; } if (uuNew >= uu1) { ulong uuQuo = uu2 / uu1; if (uuQuo > uint.MaxValue) break; uQuo = (uint)uuQuo; uuNew = uu2 - uQuo * uu1; } uuAdNew = d + (ulong)uQuo * b; uuBcNew = c + (ulong)uQuo * a; if (uuAdNew > int.MaxValue || uuBcNew > int.MaxValue) break; // Jebelean / Belenkiy-Vidunas conditions if (uuNew < uuBcNew || uuNew + uuAdNew > uu1 - b) break; Contract.Assert(uQuo == (uu2 + d - 1) / (uu1 - b)); Contract.Assert(uQuo == (uu2 - c) / (uu1 + a)); d = (uint)uuAdNew; c = (uint)uuBcNew; uu2 = uuNew; if (uu2 <= c) { Contract.Assert(uu2 == c); break; } } if (b == 0) { Contract.Assert(a == 1 && c == 0 && d == 1); Contract.Assert(uu1 > uu2); // We ensured this above. if (uu1 / 2 >= uu2) reg1.Mod(ref reg2); else reg1.Sub(ref signTmp, ref reg2); } else { // Replace reg1 with a * reg1 - b * reg2. // Replace reg2 with -c * reg1 + d * reg2. // Do everything mod cuMin uint's. reg1.SetSizeKeep(cuMin, 0); reg2.SetSizeKeep(cuMin, 0); int nCarry1 = 0; int nCarry2 = 0; for (int iu = 0; iu < cuMin; iu++) { uint u1 = reg1._rgu[iu]; uint u2 = reg2._rgu[iu]; long nn1 = (long)u1 * a - (long)u2 * b + nCarry1; long nn2 = (long)u2 * d - (long)u1 * c + nCarry2; nCarry1 = (int)(nn1 >> kcbitUint); nCarry2 = (int)(nn2 >> kcbitUint); reg1._rgu[iu] = (uint)nn1; reg2._rgu[iu] = (uint)nn2; } reg1.Trim(); reg2.Trim(); } } }