Esempio n. 1
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 += u * (num13 - 0xffffffffL);
                     num13  = 0xffffffffL;
                 }
                 while ((num14 <= 0xffffffffL) && ((num13 * num6) > NumericsHelpers.MakeUlong((uint)num14, uLo)))
                 {
                     num13 -= (ulong)1L;
                     num14 += 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();
         }
     }
 }
Esempio n. 2
0
        private static void LehmerGcd(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2)
        {
            int  num;
            uint num2;
            int  sign = 1;

START:
            num = reg1._iuLast + 1;
            int b = reg2._iuLast + 1;

            if (num < b)
            {
                Py.Core.Util.Swap <BigIntegerBuilder>(ref reg1, ref reg2);
                Py.Core.Util.Swap <int>(ref num, ref b);
            }
            if (b == 1)
            {
                if (num == 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 (num == 2)
            {
                reg1.Set(NumericsHelpers.GCD(reg1.GetHigh2(2), reg2.GetHigh2(2)));
                return;
            }
            if (b <= (num - 2))
            {
                reg1.Mod(ref reg2);
                goto START;
            }
            ulong a    = reg1.GetHigh2(num);
            ulong num6 = reg2.GetHigh2(num);
            int   num7 = NumericsHelpers.CbitHighZero((ulong)(a | num6));

            if (num7 > 0)
            {
                a    = (a << num7) | (reg1._rgu[num - 3] >> (0x20 - num7));
                num6 = (num6 << num7) | (reg2._rgu[num - 3] >> (0x20 - num7));
            }
            if (a < num6)
            {
                Py.Core.Util.Swap <ulong>(ref a, ref num6);
                Py.Core.Util.Swap <BigIntegerBuilder>(ref reg1, ref reg2);
            }
            if ((a == ulong.MaxValue) || (num6 == ulong.MaxValue))
            {
                a    = a >> 1;
                num6 = num6 >> 1;
            }
            if (a == num6)
            {
                reg1.Sub(ref sign, ref reg2);
                goto START;
            }
            if (NumericsHelpers.GetHi(num6) == 0)
            {
                reg1.Mod(ref reg2);
                goto START;
            }
            uint num8  = 1;
            uint num9  = 0;
            uint num10 = 0;
            uint num11 = 1;

Label_0162:
            num2 = 1;
            ulong num12 = a - num6;

            while ((num12 >= num6) && (num2 < 0x20))
            {
                num12 -= num6;
                num2++;
            }
            if (num12 >= num6)
            {
                ulong num13 = a / num6;
                if (num13 > 0xffffffffL)
                {
                    goto Label_02A3;
                }
                num2  = (uint)num13;
                num12 = a - (num2 * num6);
            }
            ulong num14 = num8 + (num2 * num10);
            ulong num15 = num9 + (num2 * num11);

            if (((num14 <= 0x7fffffffL) && (num15 <= 0x7fffffffL)) && ((num12 >= num15) && ((num12 + num14) <= (num6 - num10))))
            {
                num8 = (uint)num14;
                num9 = (uint)num15;
                a    = num12;
                if (a > num9)
                {
                    num2  = 1;
                    num12 = num6 - a;
                    while ((num12 >= a) && (num2 < 0x20))
                    {
                        num12 -= a;
                        num2++;
                    }
                    if (num12 >= a)
                    {
                        ulong num16 = num6 / a;
                        if (num16 > 0xffffffffL)
                        {
                            goto Label_02A3;
                        }
                        num2  = (uint)num16;
                        num12 = num6 - (num2 * a);
                    }
                    num14 = num11 + (num2 * num9);
                    num15 = num10 + (num2 * num8);
                    if (((num14 <= 0x7fffffffL) && (num15 <= 0x7fffffffL)) && ((num12 >= num15) && ((num12 + num14) <= (a - num9))))
                    {
                        num11 = (uint)num14;
                        num10 = (uint)num15;
                        num6  = num12;
                        if (num6 > num10)
                        {
                            goto Label_0162;
                        }
                    }
                }
            }
Label_02A3:
            if (num9 == 0)
            {
                if ((a / ((ulong)2L)) >= num6)
                {
                    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 = ((num20 * num8) - (num21 * num9)) + num17;
                    long num23 = ((num21 * num11) - (num20 * num10)) + num18;
                    num17        = (int)(num22 >> 0x20);
                    num18        = (int)(num23 >> 0x20);
                    reg1._rgu[i] = (uint)num22;
                    reg2._rgu[i] = (uint)num23;
                }
                reg1.Trim();
                reg2.Trim();
            }
            goto START;
        }