Ejemplo n.º 1
0
 public BigIntegerBuilder(ref BigIntegerBuilder reg) {
     this = reg;
     if (this._fWritable) {
         this._fWritable = false;
         if (this._iuLast == 0) {
             this._rgu = null;
         }
         else {
             reg._fWritable = false;
         }
     }
 }
Ejemplo n.º 2
0
 public BigIntegerBuilder(ref BigIntegerBuilder reg) {
   reg.AssertValid(true);
   this = reg;
   if (_fWritable) {
     _fWritable = false;
     if (_iuLast == 0)
       _rgu = null;
     else
       reg._fWritable = false;
   }
   AssertValid(true);
 }
Ejemplo n.º 3
0
 public BigIntegerBuilder(ref BigIntegerBuilder reg)
 {
     this = reg;
     if (_fWritable)
     {
         _fWritable = false;
         if (_iuLast != 0)
         {
             reg._fWritable = false;
         }
         else
         {
             _bits = null;
         }
     }
 }
Ejemplo n.º 4
0
        public void Div(ref BigIntegerBuilder regDen)
        {
            if (regDen._iuLast == 0)
            {
                DivMod(regDen._uSmall);
                return;
            }
            if (_iuLast == 0)
            {
                _uSmall = 0;
                return;
            }
            var bigIntegerBuilder = new BigIntegerBuilder();

            ModDivCore(ref this, ref regDen, true, ref bigIntegerBuilder);
            NumericHelper.Swap(ref this, ref bigIntegerBuilder);
        }
Ejemplo n.º 5
0
        public static uint Mod(ref BigIntegerBuilder regNum, uint num5)
        {
            if (num5 == 1)
            {
                return(0);
            }
            if (regNum._iuLast == 0)
            {
                return(regNum._uSmall % num5);
            }
            var num = (ulong)0;

            for (var i = regNum._iuLast; i >= 0; i--)
            {
                num = NumericHelper.BuildUInt64((uint)num, regNum._bits[i]);
                num = num % num5;
            }
            return((uint)num);
        }
Ejemplo n.º 6
0
 public void Load(ref BigIntegerBuilder reg, int cuExtra) {
     if (reg._iuLast == 0) {
         this._uSmall = reg._uSmall;
         this._iuLast = 0;
     }
     else {
         if (!this._fWritable || (this._rgu.Length <= reg._iuLast)) {
             this._rgu = new uint[(reg._iuLast + 1) + cuExtra];
             this._fWritable = true;
         }
         this._iuLast = reg._iuLast;
         Array.Copy(reg._rgu, 0, this._rgu, 0, (int)(this._iuLast + 1));
     }
 }
 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);
     }
 }
Ejemplo n.º 8
0
 public void Load(ref BigIntegerBuilder reg) {
     this.Load(ref reg, 0);
 }
Ejemplo 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);
 }
Ejemplo n.º 10
0
    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();
    }
Ejemplo n.º 11
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);
            }
        }
Ejemplo n.º 12
0
 public static void GCD(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2) {
     if (((reg1._iuLast > 0) && (reg1._rgu[0] == 0)) || ((reg2._iuLast > 0) && (reg2._rgu[0] == 0))) {
         int num = reg1.MakeOdd();
         int num2 = reg2.MakeOdd();
         LehmerGcd(ref reg1, ref reg2);
         int cbit = Math.Min(num, num2);
         if (cbit > 0) {
             reg1.ShiftLeft(cbit);
         }
     }
     else {
         LehmerGcd(ref reg1, ref reg2);
     }
 }
Ejemplo n.º 13
0
 public void Mul(ref BigIntegerBuilder reg1, ref BigIntegerBuilder reg2) {
     if (reg1._iuLast == 0) {
         if (reg2._iuLast == 0) {
             this.Set((ulong)reg1._uSmall * (ulong)reg2._uSmall);
         }
         else {
             this.Load(ref reg2, 1);
             this.Mul(reg1._uSmall);
         }
     }
     else if (reg2._iuLast == 0) {
         this.Load(ref reg1, 1);
         this.Mul(reg2._uSmall);
     }
     else {
         uint[] numArray;
         uint[] numArray2;
         int num;
         int num2;
         this.SetSizeClear((reg1._iuLast + reg2._iuLast) + 2);
         if (reg1.CuNonZero <= reg2.CuNonZero) {
             numArray = reg1._rgu;
             num = reg1._iuLast + 1;
             numArray2 = reg2._rgu;
             num2 = reg2._iuLast + 1;
         }
         else {
             numArray = reg2._rgu;
             num = reg2._iuLast + 1;
             numArray2 = reg1._rgu;
             num2 = reg1._iuLast + 1;
         }
         for (int i = 0; i < num; i++) {
             uint num4 = numArray[i];
             if (num4 != 0) {
                 uint uCarry = 0;
                 int index = i;
                 int num7 = 0;
                 while (num7 < num2) {
                     uCarry = AddMulCarry(ref this._rgu[index], num4, numArray2[num7], uCarry);
                     num7++;
                     index++;
                 }
                 while (uCarry != 0) {
                     uCarry = AddCarry(ref this._rgu[index++], 0, uCarry);
                 }
             }
         }
         this.Trim();
     }
 }
Ejemplo n.º 14
0
 public void Mod(ref BigIntegerBuilder regDen) {
     if (regDen._iuLast == 0) {
         this.Set(Mod(ref this, regDen._uSmall));
     }
     else if (this._iuLast != 0) {
         BigIntegerBuilder regQuo = new BigIntegerBuilder();
         ModDivCore(ref this, ref regDen, false, ref regQuo);
     }
 }
Ejemplo n.º 15
0
 public void Add(ref BigIntegerBuilder reg) {
     if (reg._iuLast == 0) {
         this.Add(reg._uSmall);
     }
     else if (this._iuLast == 0) {
         uint u = this._uSmall;
         if (u == 0) {
             this = new BigIntegerBuilder(ref reg);
         }
         else {
             this.Load(ref reg, 1);
             this.Add(u);
         }
     }
     else {
         this.EnsureWritable(Math.Max(this._iuLast, reg._iuLast) + 1, 1);
         int iu = reg._iuLast + 1;
         if (this._iuLast < reg._iuLast) {
             iu = this._iuLast + 1;
             Array.Copy(reg._rgu, (int)(this._iuLast + 1), this._rgu, (int)(this._iuLast + 1), (int)(reg._iuLast - this._iuLast));
             this._iuLast = reg._iuLast;
         }
         uint uCarry = 0;
         for (int i = 0; i < iu; i++) {
             uCarry = AddCarry(ref this._rgu[i], reg._rgu[i], uCarry);
         }
         if (uCarry != 0) {
             this.ApplyCarry(iu);
         }
     }
 }
Ejemplo n.º 16
0
 private void SubRev(ref BigIntegerBuilder reg) {
     this.EnsureWritable(reg._iuLast + 1, 0);
     int iuMin = this._iuLast + 1;
     if (this._iuLast < reg._iuLast) {
         Array.Copy(reg._rgu, (int)(this._iuLast + 1), this._rgu, (int)(this._iuLast + 1), (int)(reg._iuLast - this._iuLast));
         this._iuLast = reg._iuLast;
     }
     uint uBorrow = 0;
     for (int i = 0; i < iuMin; i++) {
         uBorrow = SubRevBorrow(ref this._rgu[i], reg._rgu[i], uBorrow);
     }
     if (uBorrow != 0) {
         this.ApplyBorrow(iuMin);
     }
     this.Trim();
 }
Ejemplo n.º 17
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) {
      Contract.Assert(0 < _iuLast && _iuLast <= reg._iuLast);
      Contract.Assert(_iuLast < reg._iuLast || _rgu[_iuLast] < reg._rgu[_iuLast]);

      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);
        Contract.Assert(_iuLast > 0);
        _iuLast = reg._iuLast;
      }

      uint uBorrow = 0;
      for (int iu = 0; iu < cuSub; iu++) {
        uBorrow = SubRevBorrow(ref _rgu[iu], reg._rgu[iu], uBorrow);
        Contract.Assert(uBorrow <= 1);
      }
      if (uBorrow != 0) {
        Contract.Assert(uBorrow == 1);
        ApplyBorrow(cuSub);
      }
      Trim();
    }
Ejemplo n.º 18
0
    public void Sub(ref int sign, ref BigIntegerBuilder reg) {
      Contract.Requires(sign == +1 || sign == -1);
      AssertValid(true);
      reg.AssertValid(true);

      if (reg._iuLast == 0) {
        Sub(ref sign, reg._uSmall);
        return;
      }
      if (_iuLast == 0) {
        uint u = _uSmall;
        if (u == 0)
          this = new BigIntegerBuilder(ref reg);
        else {
          Load(ref reg);
          Sub(ref sign, u);
        }
        sign = -sign;
        return;
      }

      if (_iuLast < reg._iuLast) {
        SubRev(ref reg);
        sign = -sign;
        return;
      }

      int cuSub = reg._iuLast + 1;
      if (_iuLast == reg._iuLast) {
        // Determine which is larger.
        _iuLast = BigInteger.GetDiffLength(_rgu, reg._rgu, _iuLast + 1) - 1;
        if (_iuLast < 0) {
          _iuLast = 0;
          _uSmall = 0;
          return;
        }

        uint u1 = _rgu[_iuLast];
        uint u2 = reg._rgu[_iuLast];
        if (_iuLast == 0) {
          if (u1 < u2) {
            _uSmall = u2 - u1;
            sign = -sign;
          }
          else
            _uSmall = u1 - u2;
          AssertValid(true);
          return;
        }

        if (u1 < u2) {
          Contract.Assert(_iuLast > 0);
          reg._iuLast = _iuLast;
          SubRev(ref reg);
          reg._iuLast = cuSub - 1;
          Contract.Assert(reg._iuLast > 0);
          sign = -sign;
          return;
        }
        cuSub = _iuLast + 1;
      }

      EnsureWritable();

      // Subtract, tracking borrow.
      uint uBorrow = 0;
      for (int iu = 0; iu < cuSub; iu++) {
        uBorrow = SubBorrow(ref _rgu[iu], reg._rgu[iu], uBorrow);
        Contract.Assert(uBorrow <= 1);
      }
      if (uBorrow != 0) {
        Contract.Assert(uBorrow == 1 && cuSub <= _iuLast);
        ApplyBorrow(cuSub);
      }
      Trim();
    }
Ejemplo n.º 19
0
    public void Add(ref BigIntegerBuilder reg) {
      AssertValid(true);
      reg.AssertValid(true);

      if (reg._iuLast == 0) {
        Add(reg._uSmall);
        return;
      }
      if (_iuLast == 0) {
        uint u = _uSmall;
        if (u == 0)
          this = new BigIntegerBuilder(ref reg);
        else {
          Load(ref reg, 1);
          Add(u);
        }
        return;
      }

      EnsureWritable(Math.Max(_iuLast, reg._iuLast) + 1, 1);

      int cuAdd = reg._iuLast + 1;
      if (_iuLast < reg._iuLast) {
        cuAdd = _iuLast + 1;
        Array.Copy(reg._rgu, _iuLast + 1, _rgu, _iuLast + 1, reg._iuLast - _iuLast);
        Contract.Assert(_iuLast > 0);
        _iuLast = reg._iuLast;
      }

      // Add, tracking carry.
      uint uCarry = 0;
      for (int iu = 0; iu < cuAdd; iu++) {
        uCarry = AddCarry(ref _rgu[iu], reg._rgu[iu], uCarry);
        Contract.Assert(uCarry <= 1);
      }

      // Deal with extra carry.
      if (uCarry != 0)
        ApplyCarry(cuAdd);
      AssertValid(true);
    }
Ejemplo n.º 20
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) {
      Contract.Requires(cuExtra >= 0);
      AssertValid(false);
      reg.AssertValid(true);

      if (reg._iuLast == 0) {
        _uSmall = reg._uSmall;
        _iuLast = 0;
      }
      else {
        if (!_fWritable || _rgu.Length <= reg._iuLast) {
          _rgu = new uint[reg._iuLast + 1 + cuExtra];
          _fWritable = true;
        }
        _iuLast = reg._iuLast;
        Array.Copy(reg._rgu, _rgu, _iuLast + 1);
      }
      AssertValid(true);
    }
Ejemplo n.º 21
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 (; ; ) {
        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();
        }
      }
    }
Ejemplo n.º 22
0
 public void Sub(ref int sign, ref BigIntegerBuilder reg) {
     if (reg._iuLast == 0) {
         this.Sub(ref sign, reg._uSmall);
     }
     else if (this._iuLast == 0) {
         uint u = this._uSmall;
         if (u == 0) {
             this = new BigIntegerBuilder(ref reg);
         }
         else {
             this.Load(ref reg);
             this.Sub(ref sign, u);
         }
         sign = -sign;
     }
     else if (this._iuLast < reg._iuLast) {
         this.SubRev(ref reg);
         sign = -sign;
     }
     else {
         int iuMin = reg._iuLast + 1;
         if (this._iuLast == reg._iuLast) {
             this._iuLast = BigInteger.GetDiffLength(this._rgu, reg._rgu, this._iuLast + 1) - 1;
             if (this._iuLast < 0) {
                 this._iuLast = 0;
                 this._uSmall = 0;
                 return;
             }
             uint num3 = this._rgu[this._iuLast];
             uint num4 = reg._rgu[this._iuLast];
             if (this._iuLast == 0) {
                 if (num3 < num4) {
                     this._uSmall = num4 - num3;
                     sign = -sign;
                     return;
                 }
                 this._uSmall = num3 - num4;
                 return;
             }
             if (num3 < num4) {
                 reg._iuLast = this._iuLast;
                 this.SubRev(ref reg);
                 reg._iuLast = iuMin - 1;
                 sign = -sign;
                 return;
             }
             iuMin = this._iuLast + 1;
         }
         this.EnsureWritable();
         uint uBorrow = 0;
         for (int i = 0; i < iuMin; i++) {
             uBorrow = SubBorrow(ref this._rgu[i], reg._rgu[i], uBorrow);
         }
         if (uBorrow != 0) {
             this.ApplyBorrow(iuMin);
         }
         this.Trim();
     }
 }
Ejemplo n.º 23
0
    // This version may share memory with regMul.
    public void Mul(ref BigIntegerBuilder regMul) {
      AssertValid(true);
      regMul.AssertValid(true);

      if (regMul._iuLast == 0)
        Mul(regMul._uSmall);
      else if (_iuLast == 0) {
        uint u = _uSmall;
        if (u == 1)
          this = new BigIntegerBuilder(ref regMul);
        else if (u != 0) {
          Load(ref regMul, 1);
          Mul(u);
        }
      }
      else {
        int cuBase = _iuLast + 1;
        SetSizeKeep(cuBase + regMul._iuLast, 1);

        for (int iu = cuBase; --iu >= 0; ) {
          uint uMul = _rgu[iu];
          _rgu[iu] = 0;
          uint uCarry = 0;
          for (int iuSrc = 0; iuSrc <= regMul._iuLast; iuSrc++)
            uCarry = AddMulCarry(ref _rgu[iu + iuSrc], regMul._rgu[iuSrc], uMul, uCarry);
          if (uCarry != 0) {
            for (int iuDst = iu + regMul._iuLast + 1; uCarry != 0 && iuDst <= _iuLast; iuDst++)
              uCarry = AddCarry(ref _rgu[iuDst], 0, uCarry);
            if (uCarry != 0) {
              SetSizeKeep(_iuLast + 2, 0);
              _rgu[_iuLast] = uCarry;
            }
          }
        }
        AssertValid(true);
      }
    }
Ejemplo n.º 24
0
        public void Mul(ref BigIntegerBuilder regMul) {
            if (regMul._iuLast == 0) {
                this.Mul(regMul._uSmall);
            }
            else if (this._iuLast == 0) {
                uint u = this._uSmall;
                switch (u) {
                    case 1:
                        this = new BigIntegerBuilder(ref regMul);
                        return;

                    case 0:
                        return;
                }
                this.Load(ref regMul, 1);
                this.Mul(u);
            }
            else {
                int num2 = this._iuLast + 1;
                this.SetSizeKeep(num2 + regMul._iuLast, 1);
                int index = num2;
                while (--index >= 0) {
                    uint num4 = this._rgu[index];
                    this._rgu[index] = 0;
                    uint uCarry = 0;
                    for (int i = 0; i <= regMul._iuLast; i++) {
                        uCarry = AddMulCarry(ref this._rgu[index + i], regMul._rgu[i], num4, uCarry);
                    }
                    if (uCarry != 0) {
                        for (int j = (index + regMul._iuLast) + 1; (uCarry != 0) && (j <= this._iuLast); j++) {
                            uCarry = AddCarry(ref this._rgu[j], 0, uCarry);
                        }
                        if (uCarry != 0) {
                            this.SetSizeKeep(this._iuLast + 2, 0);
                            this._rgu[this._iuLast] = uCarry;
                        }
                    }
                }
            }
        }
Ejemplo n.º 25
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) {
      AssertValid(true);
      reg1.AssertValid(true);
      reg2.AssertValid(true);

      if (reg1._iuLast == 0) {
        if (reg2._iuLast == 0)
          Set((ulong)reg1._uSmall * reg2._uSmall);
        else {
          Load(ref reg2, 1);
          Mul(reg1._uSmall);
        }
      }
      else if (reg2._iuLast == 0) {
        Load(ref reg1, 1);
        Mul(reg2._uSmall);
      }
      else {
        Contract.Assert(reg1._iuLast > 0 && reg2._iuLast > 0);
        SetSizeClear(reg1._iuLast + reg2._iuLast + 2);

        uint[] rgu1, rgu2;
        int cu1, cu2;

        // We prefer more iterations on the inner loop and fewer on the outer.
        if (reg1.CuNonZero <= reg2.CuNonZero) {
          rgu1 = reg1._rgu; cu1 = reg1._iuLast + 1;
          rgu2 = reg2._rgu; cu2 = reg2._iuLast + 1;
        }
        else {
          rgu1 = reg2._rgu; cu1 = reg2._iuLast + 1;
          rgu2 = reg1._rgu; cu2 = reg1._iuLast + 1;
        }

        for (int iu1 = 0; iu1 < cu1; iu1++) {
          uint uCur = rgu1[iu1];
          if (uCur == 0)
            continue;

          uint uCarry = 0;
          int iuRes = iu1;
          for (int iu2 = 0; iu2 < cu2; iu2++, iuRes++)
            uCarry = AddMulCarry(ref _rgu[iuRes], uCur, rgu2[iu2], uCarry);
          while (uCarry != 0)
            uCarry = AddCarry(ref _rgu[iuRes++], 0, uCarry);
        }
        Trim();
      }
    }
Ejemplo n.º 26
0
 public static uint Mod(ref BigIntegerBuilder regNum, uint uDen) {
     if (uDen == 1) {
         return 0;
     }
     if (regNum._iuLast == 0) {
         return (regNum._uSmall % uDen);
     }
     ulong num = 0L;
     for (int i = regNum._iuLast; i >= 0; i--) {
         num = NumericsHelpers.MakeUlong((uint)num, regNum._rgu[i]) % ((ulong)uDen);
     }
     return (uint)num;
 }
Ejemplo n.º 27
0
    // Divide regNum by uDen, returning the remainder and tossing the quotient.
    public static uint Mod(ref BigIntegerBuilder regNum, uint uDen) {
      regNum.AssertValid(true);

      if (uDen == 1)
        return 0;
      if (regNum._iuLast == 0)
        return regNum._uSmall % uDen;

      ulong uu = 0;
      for (int iv = regNum._iuLast; iv >= 0; iv--) {
        uu = NumericsHelpers.MakeUlong((uint)uu, regNum._rgu[iv]);
        uu %= uDen;
      }
      return (uint)uu;
    }
Ejemplo n.º 28
0
        public void Div(ref BigIntegerBuilder regDen) {
            if (regDen._iuLast == 0) {
                this.DivMod(regDen._uSmall);
            }
            else if (this._iuLast == 0) {
                this._uSmall = 0;
            }
            else {
                BigIntegerBuilder regQuo = new BigIntegerBuilder();
                ModDivCore(ref this, ref regDen, true, ref regQuo);
#if !(dotNET10 || dotNET11 || dotNETCF10)
                NumericsHelpers.Swap<BigIntegerBuilder>(ref this, ref regQuo);
#else
                NumericsHelpers.Swap(ref this, ref regQuo);
#endif
            }
        }
Ejemplo n.º 29
0
    // Divide 'this' by regDen, leaving the remainder in 'this' and tossing the quotient.
    public void Mod(ref BigIntegerBuilder regDen) {
      AssertValid(true);
      regDen.AssertValid(true);

      if (regDen._iuLast == 0) {
        Set(Mod(ref this, regDen._uSmall));
        return;
      }
      if (_iuLast == 0)
        return;

      BigIntegerBuilder regTmp = new BigIntegerBuilder();
      ModDivCore(ref this, ref regDen, false, ref regTmp);
    }
Ejemplo n.º 30
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();
         }
     }
 }
Ejemplo n.º 31
0
    // Divide 'this' by regDen, leaving the quotient in 'this' and tossing the remainder.
    public void Div(ref BigIntegerBuilder regDen) {
      AssertValid(true);
      regDen.AssertValid(true);

      if (regDen._iuLast == 0) {
        DivMod(regDen._uSmall);
        return;
      }
      if (_iuLast == 0) {
        _uSmall = 0;
        return;
      }

      BigIntegerBuilder regTmp = new BigIntegerBuilder();
      ModDivCore(ref this, ref regDen, true, ref regTmp);
      NumericsHelpers.Swap(ref this, ref regTmp);
    }
Ejemplo n.º 32
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;
        }
Ejemplo n.º 33
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);
    }