Esempio n. 1
0
        // 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)
        {
            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
            {
                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();
            }
        }
Esempio n. 2
0
        // 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);
        }
Esempio n. 3
0
        // This version may share memory with regMul.
        public void Mul(ref BigIntegerBuilder regMul)
        {
            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;
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        // Divide 'this' by regDen, leaving the remainder in 'this' and tossing the quotient.
        public void Mod(ref BigIntegerBuilder regDen)
        {
            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);
        }
Esempio n. 5
0
 public BigIntegerBuilder(ref BigIntegerBuilder reg)
 {
     this = reg;
     if (_fWritable)
     {
         _fWritable = false;
         if (_iuLast == 0)
         {
             _rgu = null;
         }
         else
         {
             reg._fWritable = false;
         }
     }
 }
Esempio n. 6
0
        public void Add(ref BigIntegerBuilder reg)
        {
            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);
                _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);
            }

            // Deal with extra carry.
            if (uCarry != 0)
            {
                ApplyCarry(cuAdd);
            }
        }
Esempio n. 7
0
        // Divide 'this' by regDen, leaving the quotient in 'this' and tossing the remainder.
        public void Div(ref BigIntegerBuilder regDen)
        {
            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);
        }
Esempio n. 8
0
 // 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)
 {
     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);
     }
 }
Esempio n. 9
0
 public static void GCD(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2)
 {
     // Use Lehmer's GCD, with improvements, after eliminating common powers of 2.
     if (reg1._iuLast > 0 && reg1._rgu[0] == 0 || reg2._iuLast > 0 && reg2._rgu[0] == 0)
     {
         int cbit1 = reg1.MakeOdd();
         int cbit2 = reg2.MakeOdd();
         LehmerGcd(ref reg1, ref reg2);
         int cbitMin = Math.Min(cbit1, cbit2);
         if (cbitMin > 0)
         {
             reg1.ShiftLeft(cbitMin);
         }
     }
     else
     {
         LehmerGcd(ref reg1, ref reg2);
     }
 }
Esempio n. 10
0
        // Divide regNum by uDen, returning the remainder and tossing the quotient.
        public static uint Mod(ref BigIntegerBuilder regNum, uint uDen)
        {
            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);
        }
Esempio n. 11
0
        // Subtract this register from the given one and put the result in this one.
        // Asserts that reg is larger in the most significant uint.
        private void SubRev(ref BigIntegerBuilder reg)
        {
            EnsureWritable(reg._iuLast + 1, 0);

            int cuSub = _iuLast + 1;

            if (_iuLast < reg._iuLast)
            {
                Array.Copy(reg._rgu, _iuLast + 1, _rgu, _iuLast + 1, reg._iuLast - _iuLast);
                _iuLast = reg._iuLast;
            }

            uint uBorrow = 0;

            for (int iu = 0; iu < cuSub; iu++)
            {
                uBorrow = SubRevBorrow(ref _rgu[iu], reg._rgu[iu], uBorrow);
            }
            if (uBorrow != 0)
            {
                ApplyBorrow(cuSub);
            }
            Trim();
        }
Esempio n. 12
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();
        }
Esempio n. 13
0
        public void Sub(ref int sign, ref BigIntegerBuilder reg)
        {
            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;
                    }
                    return;
                }

                if (u1 < u2)
                {
                    reg._iuLast = _iuLast;
                    SubRev(ref reg);
                    reg._iuLast = cuSub - 1;
                    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);
            }
            if (uBorrow != 0)
            {
                ApplyBorrow(cuSub);
            }
            Trim();
        }
Esempio n. 14
0
 // Loads the value of reg into this register.
 public void Load(ref BigIntegerBuilder reg)
 {
     Load(ref reg, 0);
 }
Esempio n. 15
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();
                }
            }
        }