Set() public méthode

public Set ( uint u ) : void
u uint
Résultat void
 public void ModDiv(ref BigIntegerBuilder regDen, ref BigIntegerBuilder regQuo)
 {
     if (regDen._iuLast == 0)
     {
         regQuo.Set(DivMod(regDen._uSmall));
         NumericsHelpers.Swap <BigIntegerBuilder>(ref this, ref regQuo);
     }
     else if (_iuLast != 0)
     {
         ModDivCore(ref this, ref regDen, true, ref regQuo);
     }
 }
Exemple #2
0
 public void ModDiv(ref BigIntegerBuilder regDen, ref BigIntegerBuilder regQuo)
 {
     if (regDen._iuLast == 0)
     {
         regQuo.Set(DivMod(regDen._uSmall));
         NumericHelper.Swap(ref this, ref regQuo);
         return;
     }
     if (_iuLast == 0)
     {
         return;
     }
     ModDivCore(ref this, ref regDen, true, ref regQuo);
 }
Exemple #3
0
        public void ModDiv(ref BigIntegerBuilder regDen, ref BigIntegerBuilder regQuo)
        {
            if (regDen._iuLast == 0)
            {
                regQuo.Set(this.DivMod(regDen._uSmall));
#if !(dotNET10 || dotNET11 || dotNETCF10)
                NumericsHelpers.Swap <BigIntegerBuilder>(ref this, ref regQuo);
#else
                NumericsHelpers.Swap(ref this, ref regQuo);
#endif
            }
            else if (this._iuLast != 0)
            {
                ModDivCore(ref this, ref regDen, true, ref regQuo);
            }
        }
Exemple #4
0
 private static void ModDivCore(ref BigIntegerBuilder regNum, ref BigIntegerBuilder regDen, bool fQuo, ref BigIntegerBuilder regQuo)
 {
     regQuo.Set((uint)0);
     if (regNum._iuLast >= regDen._iuLast)
     {
         int num   = regDen._iuLast + 1;
         int num2  = regNum._iuLast - regDen._iuLast;
         int cu    = num2;
         int index = regNum._iuLast;
         while (true)
         {
             if (index < num2)
             {
                 cu++;
                 break;
             }
             if (regDen._rgu[index - num2] != regNum._rgu[index])
             {
                 if (regDen._rgu[index - num2] < regNum._rgu[index])
                 {
                     cu++;
                 }
                 break;
             }
             index--;
         }
         if (cu != 0)
         {
             if (fQuo)
             {
                 regQuo.SetSizeLazy(cu);
             }
             uint u    = regDen._rgu[num - 1];
             uint num6 = regDen._rgu[num - 2];
             int  num7 = NumericsHelpers.CbitHighZero(u);
             int  num8 = 0x20 - num7;
             if (num7 > 0)
             {
                 u    = (u << num7) | (num6 >> num8);
                 num6 = num6 << num7;
                 if (num > 2)
                 {
                     num6 |= regDen._rgu[num - 3] >> num8;
                 }
             }
             regNum.EnsureWritable();
             int num9 = cu;
             while (--num9 >= 0)
             {
                 uint  uHi   = ((num9 + num) <= regNum._iuLast) ? regNum._rgu[num9 + num] : 0;
                 ulong num11 = NumericsHelpers.MakeUlong(uHi, regNum._rgu[(num9 + num) - 1]);
                 uint  uLo   = regNum._rgu[(num9 + num) - 2];
                 if (num7 > 0)
                 {
                     num11 = (num11 << num7) | (uLo >> num8);
                     uLo   = uLo << num7;
                     if ((num9 + num) >= 3)
                     {
                         uLo |= regNum._rgu[(num9 + num) - 3] >> num8;
                     }
                 }
                 ulong num13 = num11 / ((ulong)u);
                 ulong num14 = (uint)(num11 % ((ulong)u));
                 if (num13 > 0xffffffffL)
                 {
                     num14 += (ulong)u * (num13 - 0xffffffffL);
                     num13  = 0xffffffffL;
                 }
                 while ((num14 <= 0xffffffffL) && ((num13 * num6) > NumericsHelpers.MakeUlong((uint)num14, uLo)))
                 {
                     num13 -= (ulong)1L;
                     num14 += (ulong)u;
                 }
                 if (num13 > 0L)
                 {
                     ulong num15 = 0L;
                     for (int i = 0; i < num; i++)
                     {
                         num15 += regDen._rgu[i] * num13;
                         uint num17 = (uint)num15;
                         num15 = num15 >> 0x20;
                         if (regNum._rgu[num9 + i] < num17)
                         {
                             num15 += (ulong)1L;
                         }
                         regNum._rgu[num9 + i] -= num17;
                     }
                     if (uHi < num15)
                     {
                         uint uCarry = 0;
                         for (int j = 0; j < num; j++)
                         {
                             uCarry = AddCarry(ref regNum._rgu[num9 + j], regDen._rgu[j], uCarry);
                         }
                         num13 -= (ulong)1L;
                     }
                     regNum._iuLast = (num9 + num) - 1;
                 }
                 if (fQuo)
                 {
                     if (cu == 1)
                     {
                         regQuo._uSmall = (uint)num13;
                     }
                     else
                     {
                         regQuo._rgu[num9] = (uint)num13;
                     }
                 }
             }
             regNum._iuLast = num - 1;
             regNum.Trim();
         }
     }
 }
Exemple #5
0
        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;
        }
Exemple #6
0
        private static void ModDivCore(ref BigIntegerBuilder regNum, ref BigIntegerBuilder regDen, bool fQuo, ref BigIntegerBuilder regQuo)
        {
            regQuo.Set(0);
            if (regNum._iuLast < regDen._iuLast)
            {
                return;
            }

            int cuDen  = regDen._iuLast + 1;
            int cuDiff = regNum._iuLast - regDen._iuLast;

            // Determine whether the result will have cuDiff "digits" or cuDiff+1 "digits".
            int cuQuo = cuDiff;

            for (int iu = regNum._iuLast; ; iu--)
            {
                if (iu < cuDiff)
                {
                    cuQuo++;
                    break;
                }
                if (regDen._rgu[iu - cuDiff] != regNum._rgu[iu])
                {
                    if (regDen._rgu[iu - cuDiff] < regNum._rgu[iu])
                    {
                        cuQuo++;
                    }
                    break;
                }
            }

            if (cuQuo == 0)
            {
                return;
            }

            if (fQuo)
            {
                regQuo.SetSizeLazy(cuQuo);
            }

            // Get the uint to use for the trial divisions. We normalize so the high bit is set.
            uint uDen           = regDen._rgu[cuDen - 1];
            uint uDenNext       = regDen._rgu[cuDen - 2];
            int  cbitShiftLeft  = NumericsHelpers.CbitHighZero(uDen);
            int  cbitShiftRight = kcbitUint - cbitShiftLeft;

            if (cbitShiftLeft > 0)
            {
                uDen       = (uDen << cbitShiftLeft) | (uDenNext >> cbitShiftRight);
                uDenNext <<= cbitShiftLeft;
                if (cuDen > 2)
                {
                    uDenNext |= regDen._rgu[cuDen - 3] >> cbitShiftRight;
                }
            }

            // Allocate and initialize working space.
            regNum.EnsureWritable();

            for (int iu = cuQuo; --iu >= 0;)
            {
                // Get the high (normalized) bits of regNum.
                uint  uNumHi   = (iu + cuDen <= regNum._iuLast) ? regNum._rgu[iu + cuDen] : 0;
                ulong uuNum    = NumericsHelpers.MakeUlong(uNumHi, regNum._rgu[iu + cuDen - 1]);
                uint  uNumNext = regNum._rgu[iu + cuDen - 2];
                if (cbitShiftLeft > 0)
                {
                    uuNum      = (uuNum << cbitShiftLeft) | (uNumNext >> cbitShiftRight);
                    uNumNext <<= cbitShiftLeft;
                    if (iu + cuDen >= 3)
                    {
                        uNumNext |= regNum._rgu[iu + cuDen - 3] >> cbitShiftRight;
                    }
                }

                // Divide to get the quotient digit.
                ulong uuQuo = uuNum / uDen;
                ulong uuRem = (uint)(uuNum % uDen);
                if (uuQuo > uint.MaxValue)
                {
                    uuRem += uDen * (uuQuo - uint.MaxValue);
                    uuQuo  = uint.MaxValue;
                }
                while (uuRem <= uint.MaxValue && uuQuo * uDenNext > NumericsHelpers.MakeUlong((uint)uuRem, uNumNext))
                {
                    uuQuo--;
                    uuRem += uDen;
                }

                // Multiply and subtract. Note that uuQuo may be 1 too large. If we have a borrow
                // at the end, we'll add the denominator back on and decrement uuQuo.
                if (uuQuo > 0)
                {
                    ulong uuBorrow = 0;
                    for (int iu2 = 0; iu2 < cuDen; iu2++)
                    {
                        uuBorrow += regDen._rgu[iu2] * uuQuo;
                        uint uSub = (uint)uuBorrow;
                        uuBorrow >>= kcbitUint;
                        if (regNum._rgu[iu + iu2] < uSub)
                        {
                            uuBorrow++;
                        }
                        regNum._rgu[iu + iu2] -= uSub;
                    }

                    if (uNumHi < uuBorrow)
                    {
                        // Add, tracking carry.
                        uint uCarry = 0;
                        for (int iu2 = 0; iu2 < cuDen; iu2++)
                        {
                            uCarry = AddCarry(ref regNum._rgu[iu + iu2], regDen._rgu[iu2], uCarry);
                        }
                        uuQuo--;
                    }
                    regNum._iuLast = iu + cuDen - 1;
                }

                if (fQuo)
                {
                    if (cuQuo == 1)
                    {
                        regQuo._uSmall = (uint)uuQuo;
                    }
                    else
                    {
                        regQuo._rgu[iu] = (uint)uuQuo;
                    }
                }
            }

            regNum._iuLast = cuDen - 1;
            regNum.Trim();
        }
Exemple #7
0
        // 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();
                }
            }
        }
        private static void ModDivCore(ref BigIntegerBuilder regNum, ref BigIntegerBuilder regDen, bool fQuo, ref BigIntegerBuilder regQuo)
        {
            regQuo.Set(0);
            if (regNum._iuLast < regDen._iuLast)
            {
                return;
            }

            var num1 = regDen._iuLast + 1;
            var num2 = regNum._iuLast - regDen._iuLast;
            var num3 = num2;
            var num4 = regNum._iuLast;

            while (true)
            {
                if (num4 < num2)
                {
                    num3++;
                    break;
                }

                if (regDen._bits[num4 - num2] == regNum._bits[num4])
                {
                    num4--;
                }
                else
                {
                    if (regDen._bits[num4 - num2] < regNum._bits[num4])
                    {
                        num3++;
                    }

                    break;
                }
            }

            if (num3 == 0)
            {
                return;
            }

            if (fQuo)
            {
                regQuo.SetSizeLazy(num3);
            }

            var num5 = regDen._bits[num1 - 1];
            var num6 = regDen._bits[num1 - 2];
            var num7 = NumericHelper.CbitHighZero(num5);
            var num8 = 32 - num7;

            if (num7 > 0)
            {
                num5   = (num5 << (num7 & 31)) | (num6 >> (num8 & 31));
                num6 <<= num7 & 31;
                if (num1 > 2)
                {
                    num6 |= regDen._bits[num1 - 3] >> (num8 & 31);
                }
            }

            regNum.EnsureWritable();
            var num9 = num3;

            while (true)
            {
                var num10 = num9 - 1;
                num9 = num10;
                if (num10 < 0)
                {
                    break;
                }

                var num   = num9 + num1 > regNum._iuLast ? 0 : regNum._bits[num9 + num1];
                var num11 = num;
                var num12 = NumericHelper.BuildUInt64(num11, regNum._bits[num9 + num1 - 1]);
                var num13 = regNum._bits[num9 + num1 - 2];
                if (num7 > 0)
                {
                    num12   = (num12 << (num7 & 63)) | (num13 >> (num8 & 31));
                    num13 <<= num7 & 31;
                    if (num9 + num1 >= 3)
                    {
                        num13 |= regNum._bits[num9 + num1 - 3] >> (num8 & 31);
                    }
                }

                var num14 = num12 / num5;
                var num15 = (ulong)(uint)(num12 % num5);
                if (num14 > uint.MaxValue)
                {
                    num15 += num5 * (num14 - uint.MaxValue);
                    num14  = uint.MaxValue;
                }

                while (num15 <= uint.MaxValue && num14 * num6 > NumericHelper.BuildUInt64((uint)num15, num13))
                {
                    num14--;
                    num15 += num5;
                }

                if (num14 > 0)
                {
                    var num16 = (ulong)0;
                    for (var i = 0; i < num1; i++)
                    {
                        num16 += regDen._bits[i] * num14;
                        var num17 = (uint)num16;
                        num16 >>= 32;
                        if (regNum._bits[num9 + i] < num17)
                        {
                            num16++;
                        }

                        regNum._bits[num9 + i] -= num17;
                    }

                    if (num11 < num16)
                    {
                        uint num18 = 0;
                        for (var j = 0; j < num1; j++)
                        {
                            num18 = AddCarry(ref regNum._bits[num9 + j], regDen._bits[j], num18);
                        }

                        num14--;
                    }

                    regNum._iuLast = num9 + num1 - 1;
                }

                if (!fQuo)
                {
                    continue;
                }

                if (num3 != 1)
                {
                    regQuo._bits[num9] = (uint)num14;
                }
                else
                {
                    regQuo._uSmall = (uint)num14;
                }
            }

            regNum._iuLast = num1 - 1;
            regNum.Trim();
        }
        private static void LehmerGcd(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2)
        {
            var num = 1;

            while (true)
            {
                var num1 = reg1._iuLast + 1;
                var num2 = reg2._iuLast + 1;
                if (num1 < num2)
                {
                    NumericHelper.Swap(ref reg1, ref reg2);
                    NumericHelper.Swap(ref num1, ref num2);
                }

                if (num2 == 1)
                {
                    if (num1 == 1)
                    {
                        reg1._uSmall = NumericHelper.GCD(reg1._uSmall, reg2._uSmall);
                    }
                    else if (reg2._uSmall != 0)
                    {
                        reg1.Set(NumericHelper.GCD(Mod(ref reg1, reg2._uSmall), reg2._uSmall));
                    }

                    return;
                }

                if (num1 == 2)
                {
                    break;
                }

                if (num2 > num1 - 2)
                {
                    var high2  = reg1.GetHigh2(num1);
                    var high21 = reg2.GetHigh2(num1);
                    var num3   = NumericHelper.CbitHighZero(high2 | high21);
                    if (num3 > 0)
                    {
                        high2  = (high2 << (num3 & 63)) | (reg1._bits[num1 - 3] >> ((32 - num3) & 31));
                        high21 = (high21 << (num3 & 63)) | (reg2._bits[num1 - 3] >> ((32 - num3) & 31));
                    }

                    if (high2 < high21)
                    {
                        NumericHelper.Swap(ref high2, ref high21);
                        NumericHelper.Swap(ref reg1, ref reg2);
                    }

                    if (high2 == ulong.MaxValue || high21 == ulong.MaxValue)
                    {
                        high2  >>= 1;
                        high21 >>= 1;
                    }

                    if (high2 == high21)
                    {
                        reg1.Sub(ref num, ref reg2);
                    }
                    else if (NumericHelper.GetHi(high21) != 0)
                    {
                        uint num4 = 1;
                        uint num5 = 0;
                        uint num6 = 0;
                        uint num7 = 1;
                        while (true)
                        {
                            uint num8 = 1;
                            var  num9 = high2 - high21;
                            while (num9 >= high21 && num8 < 32)
                            {
                                num9 -= high21;
                                num8++;
                            }

                            if (num9 >= high21)
                            {
                                var num10 = high2 / high21;
                                if (num10 <= uint.MaxValue)
                                {
                                    num8 = (uint)num10;
                                    num9 = high2 - (num8 * high21);
                                }
                                else
                                {
                                    break;
                                }
                            }

                            var num11 = num4 + (num8 * (ulong)num6);
                            var num12 = num5 + (num8 * (ulong)num7);
                            if (num11 > 2147483647 || num12 > 2147483647)
                            {
                                break;
                            }

                            if (num9 < num12 || num9 + num11 > high21 - num6)
                            {
                                break;
                            }

                            num4  = (uint)num11;
                            num5  = (uint)num12;
                            high2 = num9;
                            if (high2 > num5)
                            {
                                num8 = 1;
                                num9 = high21 - high2;
                                while (num9 >= high2 && num8 < 32)
                                {
                                    num9 -= high2;
                                    num8++;
                                }

                                if (num9 >= high2)
                                {
                                    var num13 = high21 / high2;
                                    if (num13 <= uint.MaxValue)
                                    {
                                        num8 = (uint)num13;
                                        num9 = high21 - (num8 * high2);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                num11 = num7 + (num8 * (ulong)num5);
                                num12 = num6 + (num8 * (ulong)num4);
                                if (num11 > 2147483647 || num12 > 2147483647)
                                {
                                    break;
                                }

                                if (num9 < num12 || num9 + num11 > high2 - num5)
                                {
                                    break;
                                }

                                num7   = (uint)num11;
                                num6   = (uint)num12;
                                high21 = num9;
                                if (high21 <= num6)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (num5 != 0)
                        {
                            reg1.SetSizeKeep(num2, 0);
                            reg2.SetSizeKeep(num2, 0);
                            var num14 = 0;
                            var num15 = 0;
                            for (var i = 0; i < num2; i++)
                            {
                                var num16 = reg1._bits[i];
                                var num17 = reg2._bits[i];
                                var num18 = ((long)num16 * num4) - ((long)num17 * num5) + num14;
                                var num19 = ((long)num17 * num7) - ((long)num16 * num6) + num15;
                                num14         = (int)(num18 >> 32);
                                num15         = (int)(num19 >> 32);
                                reg1._bits[i] = (uint)num18;
                                reg2._bits[i] = (uint)num19;
                            }

                            reg1.Trim();
                            reg2.Trim();
                        }
                        else if (high2 / 2 < high21)
                        {
                            reg1.Sub(ref num, ref reg2);
                        }
                        else
                        {
                            reg1.Mod(ref reg2);
                        }
                    }
                    else
                    {
                        reg1.Mod(ref reg2);
                    }
                }
                else
                {
                    reg1.Mod(ref reg2);
                }
            }

            reg1.Set(NumericHelper.GCD(reg1.GetHigh2(2), reg2.GetHigh2(2)));
        }
        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;
        }
 private static void ModDivCore(ref BigIntegerBuilder regNum, ref BigIntegerBuilder regDen, bool fQuo, ref BigIntegerBuilder regQuo) {
     regQuo.Set((uint)0);
     if (regNum._iuLast >= regDen._iuLast) {
         int num = regDen._iuLast + 1;
         int num2 = regNum._iuLast - regDen._iuLast;
         int cu = num2;
         int index = regNum._iuLast;
         while (true) {
             if (index < num2) {
                 cu++;
                 break;
             }
             if (regDen._rgu[index - num2] != regNum._rgu[index]) {
                 if (regDen._rgu[index - num2] < regNum._rgu[index]) {
                     cu++;
                 }
                 break;
             }
             index--;
         }
         if (cu != 0) {
             if (fQuo) {
                 regQuo.SetSizeLazy(cu);
             }
             uint u = regDen._rgu[num - 1];
             uint num6 = regDen._rgu[num - 2];
             int num7 = NumericsHelpers.CbitHighZero(u);
             int num8 = 0x20 - num7;
             if (num7 > 0) {
                 u = (u << num7) | (num6 >> num8);
                 num6 = num6 << num7;
                 if (num > 2) {
                     num6 |= regDen._rgu[num - 3] >> num8;
                 }
             }
             regNum.EnsureWritable();
             int num9 = cu;
             while (--num9 >= 0) {
                 uint uHi = ((num9 + num) <= regNum._iuLast) ? regNum._rgu[num9 + num] : 0;
                 ulong num11 = NumericsHelpers.MakeUlong(uHi, regNum._rgu[(num9 + num) - 1]);
                 uint uLo = regNum._rgu[(num9 + num) - 2];
                 if (num7 > 0) {
                     num11 = (num11 << num7) | (uLo >> num8);
                     uLo = uLo << num7;
                     if ((num9 + num) >= 3) {
                         uLo |= regNum._rgu[(num9 + num) - 3] >> num8;
                     }
                 }
                 ulong num13 = num11 / ((ulong)u);
                 ulong num14 = (uint)(num11 % ((ulong)u));
                 if (num13 > 0xffffffffL) {
                     num14 += (ulong)u * (num13 - 0xffffffffL);
                     num13 = 0xffffffffL;
                 }
                 while ((num14 <= 0xffffffffL) && ((num13 * num6) > NumericsHelpers.MakeUlong((uint)num14, uLo))) {
                     num13 -= (ulong)1L;
                     num14 += (ulong)u;
                 }
                 if (num13 > 0L) {
                     ulong num15 = 0L;
                     for (int i = 0; i < num; i++) {
                         num15 += regDen._rgu[i] * num13;
                         uint num17 = (uint)num15;
                         num15 = num15 >> 0x20;
                         if (regNum._rgu[num9 + i] < num17) {
                             num15 += (ulong)1L;
                         }
                         regNum._rgu[num9 + i] -= num17;
                     }
                     if (uHi < num15) {
                         uint uCarry = 0;
                         for (int j = 0; j < num; j++) {
                             uCarry = AddCarry(ref regNum._rgu[num9 + j], regDen._rgu[j], uCarry);
                         }
                         num13 -= (ulong)1L;
                     }
                     regNum._iuLast = (num9 + num) - 1;
                 }
                 if (fQuo) {
                     if (cu == 1) {
                         regQuo._uSmall = (uint)num13;
                     }
                     else {
                         regQuo._rgu[num9] = (uint)num13;
                     }
                 }
             }
             regNum._iuLast = num - 1;
             regNum.Trim();
         }
     }
 }
        public void ModDiv(ref BigIntegerBuilder regDen, ref BigIntegerBuilder regQuo) {
            if (regDen._iuLast == 0) {
                regQuo.Set(this.DivMod(regDen._uSmall));
#if !(dotNET10 || dotNET11 || dotNETCF10)
                NumericsHelpers.Swap<BigIntegerBuilder>(ref this, ref regQuo);
#else
                NumericsHelpers.Swap(ref this, ref regQuo);
#endif
            }
            else if (this._iuLast != 0) {
                ModDivCore(ref this, ref regDen, true, ref regQuo);
            }
        }
    private static void ModDivCore(ref BigIntegerBuilder regNum, ref BigIntegerBuilder regDen, bool fQuo, ref BigIntegerBuilder regQuo) {
      Contract.Assert(regNum._iuLast > 0 && regDen._iuLast > 0);

      regQuo.Set(0);
      if (regNum._iuLast < regDen._iuLast)
        return;

      Contract.Assert(0 < regDen._iuLast && regDen._iuLast <= regNum._iuLast);
      int cuDen = regDen._iuLast + 1;
      int cuDiff = regNum._iuLast - regDen._iuLast;

      // Determine whether the result will have cuDiff "digits" or cuDiff+1 "digits".
      int cuQuo = cuDiff;
      for (int iu = regNum._iuLast; ; iu--) {
        if (iu < cuDiff) {
          cuQuo++;
          break;
        }
        if (regDen._rgu[iu - cuDiff] != regNum._rgu[iu]) {
          if (regDen._rgu[iu - cuDiff] < regNum._rgu[iu])
            cuQuo++;
          break;
        }
      }

      if (cuQuo == 0)
        return;

      if (fQuo)
        regQuo.SetSizeLazy(cuQuo);

      // Get the uint to use for the trial divisions. We normalize so the high bit is set.
      uint uDen = regDen._rgu[cuDen - 1];
      uint uDenNext = regDen._rgu[cuDen - 2];
      int cbitShiftLeft = NumericsHelpers.CbitHighZero(uDen);
      int cbitShiftRight = kcbitUint - cbitShiftLeft;
      if (cbitShiftLeft > 0) {
        uDen = (uDen << cbitShiftLeft) | (uDenNext >> cbitShiftRight);
        uDenNext <<= cbitShiftLeft;
        if (cuDen > 2)
          uDenNext |= regDen._rgu[cuDen - 3] >> cbitShiftRight;
      }
      Contract.Assert((uDen & 0x80000000) != 0);

      // Allocate and initialize working space.
      Contract.Assert(cuQuo + cuDen == regNum._iuLast + 1 || cuQuo + cuDen == regNum._iuLast + 2);
      regNum.EnsureWritable();

      for (int iu = cuQuo; --iu >= 0; ) {
        // Get the high (normalized) bits of regNum.
        uint uNumHi = (iu + cuDen <= regNum._iuLast) ? regNum._rgu[iu + cuDen] : 0;
        Contract.Assert(uNumHi <= regDen._rgu[cuDen - 1]);

        ulong uuNum = NumericsHelpers.MakeUlong(uNumHi, regNum._rgu[iu + cuDen - 1]);
        uint uNumNext = regNum._rgu[iu + cuDen - 2];
        if (cbitShiftLeft > 0) {
          uuNum = (uuNum << cbitShiftLeft) | (uNumNext >> cbitShiftRight);
          uNumNext <<= cbitShiftLeft;
          if (iu + cuDen >= 3)
            uNumNext |= regNum._rgu[iu + cuDen - 3] >> cbitShiftRight;
        }

        // Divide to get the quotient digit.
        ulong uuQuo = uuNum / uDen;
        ulong uuRem = (uint)(uuNum % uDen);
        Contract.Assert(uuQuo <= (ulong)uint.MaxValue + 2);
        if (uuQuo > uint.MaxValue) {
          uuRem += uDen * (uuQuo - uint.MaxValue);
          uuQuo = uint.MaxValue;
        }
        while (uuRem <= uint.MaxValue && uuQuo * uDenNext > NumericsHelpers.MakeUlong((uint)uuRem, uNumNext)) {
          uuQuo--;
          uuRem += uDen;
        }

        // Multiply and subtract. Note that uuQuo may be 1 too large. If we have a borrow
        // at the end, we'll add the denominator back on and decrement uuQuo.
        if (uuQuo > 0) {
          ulong uuBorrow = 0;
          for (int iu2 = 0; iu2 < cuDen; iu2++) {
            uuBorrow += regDen._rgu[iu2] * uuQuo;
            uint uSub = (uint)uuBorrow;
            uuBorrow >>= kcbitUint;
            if (regNum._rgu[iu + iu2] < uSub)
              uuBorrow++;
            regNum._rgu[iu + iu2] -= uSub;
          }

          Contract.Assert(uNumHi == uuBorrow || uNumHi == uuBorrow - 1);
          if (uNumHi < uuBorrow) {
            // Add, tracking carry.
            uint uCarry = 0;
            for (int iu2 = 0; iu2 < cuDen; iu2++) {
              uCarry = AddCarry(ref regNum._rgu[iu + iu2], regDen._rgu[iu2], uCarry);
              Contract.Assert(uCarry <= 1);
            }
            Contract.Assert(uCarry == 1);
            uuQuo--;
          }
          regNum._iuLast = iu + cuDen - 1;
        }

        if (fQuo) {
          if (cuQuo == 1)
            regQuo._uSmall = (uint)uuQuo;
          else
            regQuo._rgu[iu] = (uint)uuQuo;
        }
      }

      Contract.Assert(cuDen > 1 && regNum._iuLast > 0);
      regNum._iuLast = cuDen - 1;
      regNum.Trim();
    }
    // Divide regNum by regDen, leaving the remainder in regNum and the quotient in regQuo (if fQuo is true).
    public void ModDiv(ref BigIntegerBuilder regDen, ref BigIntegerBuilder regQuo) {
      if (regDen._iuLast == 0) {
        regQuo.Set(DivMod(regDen._uSmall));
        NumericsHelpers.Swap(ref this, ref regQuo);
        return;
      }
      if (_iuLast == 0)
        return;

      ModDivCore(ref this, ref regDen, true, ref regQuo);
    }
    // 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();
        }
      }
    }
 public void ModDiv(ref BigIntegerBuilder regDen, ref BigIntegerBuilder regQuo)
 {
     if (regDen._iuLast == 0)
     {
         regQuo.Set(this.DivMod(regDen._uSmall));
         NumericsHelpers.Swap<BigIntegerBuilder>(ref this, ref regQuo);
     }
     else if (this._iuLast != 0)
     {
         ModDivCore(ref this, ref regDen, true, ref regQuo);
     }
 }