public void Set(ulong uu) { uint hi = NumericsHelpers.GetHi(uu); if (hi == 0) { this._uSmall = NumericsHelpers.GetLo(uu); this._iuLast = 0; } else { this.SetSizeLazy(2); this._rgu[0] = (uint)uu; this._rgu[1] = hi; } }
public void Set(ulong uu) { uint uHi = NumericsHelpers.GetHi(uu); if (uHi == 0) { _uSmall = NumericsHelpers.GetLo(uu); _iuLast = 0; } else { SetSizeLazy(2); _rgu[0] = (uint)uu; _rgu[1] = uHi; } }
private static void LehmerGcd(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2) { int num2; uint num11; int sign = 1; Label_0002: num2 = reg1._iuLast + 1; int b = reg2._iuLast + 1; if (num2 < b) { #if !(dotNET10 || dotNET11 || dotNETCF10) NumericsHelpers.Swap <BigIntegerBuilder>(ref reg1, ref reg2); NumericsHelpers.Swap <int>(ref num2, ref b); #else NumericsHelpers.Swap(ref reg1, ref reg2); NumericsHelpers.Swap(ref num2, ref b); #endif } if (b == 1) { if (num2 == 1) { reg1._uSmall = NumericsHelpers.GCD(reg1._uSmall, reg2._uSmall); return; } if (reg2._uSmall != 0) { reg1.Set(NumericsHelpers.GCD(Mod(ref reg1, reg2._uSmall), reg2._uSmall)); } return; } if (num2 == 2) { reg1.Set(NumericsHelpers.GCD(reg1.GetHigh2(2), reg2.GetHigh2(2))); return; } if (b <= (num2 - 2)) { reg1.Mod(ref reg2); goto Label_0002; } ulong a = reg1.GetHigh2(num2); ulong num5 = reg2.GetHigh2(num2); int num6 = NumericsHelpers.CbitHighZero((ulong)(a | num5)); if (num6 > 0) { a = (a << num6) | (reg1._rgu[num2 - 3] >> (0x20 - num6)); num5 = (num5 << num6) | (reg2._rgu[num2 - 3] >> (0x20 - num6)); } if (a < num5) { #if !(dotNET10 || dotNET11 || dotNETCF10) NumericsHelpers.Swap <ulong>(ref a, ref num5); NumericsHelpers.Swap <BigIntegerBuilder>(ref reg1, ref reg2); #else NumericsHelpers.Swap(ref a, ref num5); NumericsHelpers.Swap(ref reg1, ref reg2); #endif } if ((a == ulong.MaxValue) || (num5 == ulong.MaxValue)) { a = a >> 1; num5 = num5 >> 1; } if (a == num5) { reg1.Sub(ref sign, ref reg2); goto Label_0002; } if (NumericsHelpers.GetHi(num5) == 0) { reg1.Mod(ref reg2); goto Label_0002; } uint num7 = 1; uint num8 = 0; uint num9 = 0; uint num10 = 1; Label_0159: num11 = 1; ulong num12 = a - num5; while ((num12 >= num5) && (num11 < 0x20)) { num12 -= num5; num11++; } if (num12 >= num5) { ulong num13 = a / num5; if (num13 > 0xffffffffL) { goto Label_029E; } num11 = (uint)num13; num12 = a - (num11 * num5); } ulong num14 = (ulong)num7 + (ulong)num11 * (ulong)num9; ulong num15 = (ulong)num8 + (ulong)num11 * (ulong)num10; if (((num14 <= 0x7fffffffL) && (num15 <= 0x7fffffffL)) && ((num12 >= num15) && ((num12 + num14) <= (num5 - num9)))) { num7 = (uint)num14; num8 = (uint)num15; a = num12; if (a > num8) { num11 = 1; num12 = num5 - a; while ((num12 >= a) && (num11 < 0x20)) { num12 -= a; num11++; } if (num12 >= a) { ulong num16 = num5 / a; if (num16 > 0xffffffffL) { goto Label_029E; } num11 = (uint)num16; num12 = num5 - (num11 * a); } num14 = (ulong)num10 + (ulong)num11 * (ulong)num8; num15 = (ulong)num9 + (ulong)num11 * (ulong)num7; if (((num14 <= 0x7fffffffL) && (num15 <= 0x7fffffffL)) && ((num12 >= num15) && ((num12 + num14) <= (a - num8)))) { num10 = (uint)num14; num9 = (uint)num15; num5 = num12; if (num5 > num9) { goto Label_0159; } } } } Label_029E: if (num8 == 0) { if ((a / ((ulong)2L)) >= num5) { reg1.Mod(ref reg2); } else { reg1.Sub(ref sign, ref reg2); } } else { reg1.SetSizeKeep(b, 0); reg2.SetSizeKeep(b, 0); int num17 = 0; int num18 = 0; for (int i = 0; i < b; i++) { uint num20 = reg1._rgu[i]; uint num21 = reg2._rgu[i]; long num22 = (long)num20 * (long)num7 - (long)num21 * (long)num8 + (long)num17; long num23 = (long)num21 * (long)num10 - (long)num20 * (long)num9 + (long)num18; num17 = (int)(num22 >> 0x20); num18 = (int)(num23 >> 0x20); reg1._rgu[i] = (uint)num22; reg2._rgu[i] = (uint)num23; } reg1.Trim(); reg2.Trim(); } goto Label_0002; }
// 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 (; ;) { 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); } 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); 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; } 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. reg1.Sub(ref signTmp, ref reg2); 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 (; ;) { 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; } a = (uint)uuAdNew; b = (uint)uuBcNew; uu1 = uuNew; if (uu1 <= b) { break; } 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; } d = (uint)uuAdNew; c = (uint)uuBcNew; uu2 = uuNew; if (uu2 <= c) { break; } } if (b == 0) { 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(); } } }