public uint DivMod(uint uDen) { if (uDen == 1) { return(0); } if (this._iuLast == 0) { uint num = this._uSmall; this._uSmall = num / uDen; return(num % uDen); } this.EnsureWritable(); ulong num2 = 0L; for (int i = this._iuLast; i >= 0; i--) { num2 = NumericsHelpers.MakeUlong((uint)num2, this._rgu[i]); this._rgu[i] = (uint)(num2 / ((ulong)uDen)); num2 = num2 % ((ulong)uDen); } this.Trim(); return((uint)num2); }
public uint DivMod(uint num5) { if (num5 == 1) { return(0); } if (_iuLast == 0) { var num = _uSmall; _uSmall = num / num5; return(num % num5); } EnsureWritable(); var num1 = (ulong)0; for (var i = _iuLast; i >= 0; i--) { num1 = NumericsHelpers.MakeUlong((uint)num1, _bits[i]); _bits[i] = (uint)(num1 / num5); num1 = num1 % num5; } Trim(); return((uint)num1); }
internal static string FormatBigInteger(BigInteger value, string format, NumberFormatInfo info) { int num3; int num9; char ch = ParseFormatSpecifier(format, out int digits); switch (ch) { case 'x': case 'X': return(FormatBigIntegerToHexString(value, ch, digits, info)); } bool flag = ((((ch == 'g') || (ch == 'G')) || ((ch == 'd') || (ch == 'D'))) || (ch == 'r')) || (ch == 'R'); if (!flag) { throw new FormatException("Format specifier was invalid."); } if (value._bits == null) { switch (ch) { case 'g': case 'G': case 'r': case 'R': if (digits > 0) { format = "D" + digits; } else { format = "D"; } break; } return(value._sign.ToString(format, info)); } int num2 = BigInteger.Length(value._bits); try { num3 = ((num2 * 10) / 9) + 2; } catch (OverflowException exception) { throw new FormatException("The value is too large to be represented by this format specifier.", exception); } uint[] numArray = new uint[num3]; int num4 = 0; int index = num2; while (--index >= 0) { uint uLo = value._bits[index]; for (int k = 0; k < num4; k++) { ulong num8 = NumericsHelpers.MakeUlong(numArray[k], uLo); numArray[k] = (uint)(num8 % 0x3b9aca00L); uLo = (uint)(num8 / 0x3b9aca00L); } if (uLo != 0) { numArray[num4++] = uLo % 0x3b9aca00; uLo /= 0x3b9aca00; if (uLo != 0) { numArray[num4++] = uLo; } } } try { num9 = num4 * 9; } catch (OverflowException exception2) { throw new FormatException("The value is too large to be represented by this format specifier.", exception2); } if (flag) { if ((digits > 0) && (digits > num9)) { num9 = digits; } if (value._sign < 0) { try { num9 += info.NegativeSign.Length; } catch (OverflowException exception3) { throw new FormatException("The value is too large to be represented by this format specifier.", exception3); } } } char[] chArray = new char[num9]; int startIndex = num9; for (int i = 0; i < (num4 - 1); i++) { uint num12 = numArray[i]; int num13 = 9; while (--num13 >= 0) { chArray[--startIndex] = (char)(0x30 + (num12 % 10)); num12 /= 10; } } for (uint j = numArray[num4 - 1]; j != 0; j /= 10) { chArray[--startIndex] = (char)(0x30 + (j % 10)); } int num15 = num9 - startIndex; while ((digits > 0) && (digits > num15)) { chArray[--startIndex] = '0'; digits--; } if (value._sign < 0) { string negativeSign = info.NegativeSign; for (int m = negativeSign.Length - 1; m > -1; m--) { chArray[--startIndex] = negativeSign[m]; } } return(new string(chArray, startIndex, num9 - startIndex)); }
internal static unsafe string FormatBigInteger(BigInteger value, string format, NumberFormatInfo info) { int num3; int num9; int digits = 0; char ch = ParseFormatSpecifier(format, out digits); switch (ch) { case 'x': case 'X': return(FormatBigIntegerToHexString(value, ch, digits, info)); } bool flag = ((((ch == 'g') || (ch == 'G')) || ((ch == 'd') || (ch == 'D'))) || (ch == 'r')) || (ch == 'R'); if (value._bits == null) { switch (ch) { case 'g': case 'G': case 'r': case 'R': if (digits > 0) { format = string.Format(CultureInfo.InvariantCulture, "D{0}", new object[] { digits.ToString(CultureInfo.InvariantCulture) }); } else { format = "D"; } break; } return(value._sign.ToString(format, info)); } int num2 = BigInteger.Length(value._bits); try { num3 = ((num2 * 10) / 9) + 2; } catch (OverflowException exception) { throw new FormatException(SR.GetString("Format_TooLarge"), exception); } uint[] numArray = new uint[num3]; int num4 = 0; int index = num2; while (--index >= 0) { uint uLo = value._bits[index]; for (int k = 0; k < num4; k++) { ulong num8 = NumericsHelpers.MakeUlong(numArray[k], uLo); numArray[k] = (uint)(num8 % ((ulong)0x3b9aca00L)); uLo = (uint)(num8 / ((ulong)0x3b9aca00L)); } if (uLo != 0) { numArray[num4++] = uLo % 0x3b9aca00; uLo /= 0x3b9aca00; if (uLo != 0) { numArray[num4++] = uLo; } } } try { num9 = num4 * 9; } catch (OverflowException exception2) { throw new FormatException(SR.GetString("Format_TooLarge"), exception2); } if (flag) { if ((digits > 0) && (digits > num9)) { num9 = digits; } if (value._sign < 0) { try { num9 += info.NegativeSign.Length; } catch (OverflowException exception3) { throw new FormatException(SR.GetString("Format_TooLarge"), exception3); } } } char[] chArray = new char[num9]; int startIndex = num9; for (int i = 0; i < (num4 - 1); i++) { uint num12 = numArray[i]; int num13 = 9; while (--num13 >= 0) { chArray[--startIndex] = (char)(0x30 + (num12 % 10)); num12 /= 10; } } for (uint j = numArray[num4 - 1]; j != 0; j /= 10) { chArray[--startIndex] = (char)(0x30 + (j % 10)); } if (!flag) { byte *stackBuffer = stackalloc byte[0x72]; Number.NumberBuffer buffer = new Number.NumberBuffer(stackBuffer) { sign = value._sign < 0, precision = 0x1d }; buffer.digits[0] = '\0'; buffer.scale = num9 - startIndex; int num15 = Math.Min(startIndex + 50, num9); for (int m = startIndex; m < num15; m++) { buffer.digits[m - startIndex] = chArray[m]; } return(Number.FormatNumberBuffer(buffer.PackForNative(), format, info)); } int num17 = num9 - startIndex; while ((digits > 0) && (digits > num17)) { chArray[--startIndex] = '0'; digits--; } if (value._sign < 0) { string negativeSign = info.NegativeSign; for (int n = info.NegativeSign.Length - 1; n > -1; n--) { chArray[--startIndex] = info.NegativeSign[n]; } } return(new string(chArray, startIndex, num9 - startIndex)); }
private static bool HexNumberToBigInteger(ref BigNumberBuffer number, out BigInteger result) { if (number.digits == null || number.digits.Length == 0) { result = default; return(false); } const int DigitsPerBlock = 8; int totalDigitCount = number.digits.Length - 1; // Ignore trailing '\0' int blockCount, partialDigitCount; blockCount = Math.DivRem(totalDigitCount, DigitsPerBlock, out int remainder); if (remainder == 0) { partialDigitCount = 0; } else { blockCount += 1; partialDigitCount = DigitsPerBlock - remainder; } bool isNegative = HexConverter.FromChar(number.digits[0]) >= 8; uint partialValue = (isNegative && partialDigitCount > 0) ? 0xFFFFFFFFu : 0; int[]? arrayFromPool = null; Span <uint> bitsBuffer = (blockCount <= BigInteger.StackallocUInt32Limit) ? stackalloc uint[blockCount] : MemoryMarshal.Cast <int, uint>((arrayFromPool = ArrayPool <int> .Shared.Rent(blockCount)).AsSpan(0, blockCount)); int bitsBufferPos = blockCount - 1; try { foreach (ReadOnlyMemory <char> digitsChunkMem in number.digits.GetChunks()) { ReadOnlySpan <char> chunkDigits = digitsChunkMem.Span; for (int i = 0; i < chunkDigits.Length; i++) { char digitChar = chunkDigits[i]; if (digitChar == '\0') { break; } int hexValue = HexConverter.FromChar(digitChar); Debug.Assert(hexValue != 0xFF); partialValue = (partialValue << 4) | (uint)hexValue; partialDigitCount++; if (partialDigitCount == DigitsPerBlock) { bitsBuffer[bitsBufferPos] = partialValue; bitsBufferPos--; partialValue = 0; partialDigitCount = 0; } } } Debug.Assert(partialDigitCount == 0 && bitsBufferPos == -1); // BigInteger requires leading zero blocks to be truncated. bitsBuffer = bitsBuffer.TrimEnd(0u); int sign; uint[]? bits; if (bitsBuffer.IsEmpty) { sign = 0; bits = null; } else if (bitsBuffer.Length == 1) { sign = (int)bitsBuffer[0]; bits = null; if ((!isNegative && sign < 0) || sign == int.MinValue) { sign = isNegative ? -1 : 1; bits = new[] { (uint)sign }; } } else { sign = isNegative ? -1 : 1; bits = bitsBuffer.ToArray(); if (isNegative) { NumericsHelpers.DangerousMakeTwosComplement(bits); } } result = new BigInteger(sign, bits); return(true); } finally { if (arrayFromPool != null) { ArrayPool <int> .Shared.Return(arrayFromPool); } } }
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(); }
// 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 ReverseStringBuilder CreateBuilder(BigInteger value, NumberFormatInfo info, bool decimalFmt, int digits) { // First convert to base 10^9. const uint NumericBase = 1000000000; // 10^9 const int NumericBaseLog10 = 9; var sourceLength = Length(value.InternalBits); int maxConvertedLength; try { maxConvertedLength = checked (sourceLength * 10 / 9 + 2); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } var converted = new uint[maxConvertedLength]; var convertedLength = 0; for (var sourceIndex = sourceLength; --sourceIndex >= 0;) { // Take a cipher from the source var carry = value.InternalBits[sourceIndex]; // Add it to converted for (var convertedIndex = 0; convertedIndex < convertedLength; convertedIndex++) { var cipherBlock = NumericsHelpers.MakeUlong(converted[convertedIndex], carry); converted[convertedIndex] = (uint)(cipherBlock % NumericBase); carry = (uint)(cipherBlock / NumericBase); } if (carry != 0) { converted[convertedLength++] = carry % NumericBase; carry /= NumericBase; if (carry != 0) { converted[convertedLength++] = carry; } } } int stringCapacity; try { // Each uint contributes at most 9 digits to the decimal representation. stringCapacity = checked (convertedLength * NumericBaseLog10); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } if (decimalFmt) { if (digits > 0 && stringCapacity < digits) { stringCapacity = digits; } if (value.InternalSign < 0) { try { // Leave an extra slot for a minus sign. stringCapacity = checked (stringCapacity + info.NegativeSign.Length); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } } } var result = new ReverseStringBuilder(stringCapacity); for (var stringIndex = 0; stringIndex < convertedLength - 1; stringIndex++) { var cipherBlock = converted[stringIndex]; for (var cch = NumericBaseLog10; --cch >= 0;) { result.Prepend((char)('0' + cipherBlock % 10)); cipherBlock /= 10; } } for (var cipherBlock = converted[convertedLength - 1]; cipherBlock != 0;) { result.Prepend((char)('0' + cipherBlock % 10)); cipherBlock /= 10; } return(result); }
internal static string FormatBigInteger(BigInteger value, string format, NumberFormatInfo info) { int digits = 0; char fmt = ParseFormatSpecifier(format, out digits); if (fmt == 'x' || fmt == 'X') { return(FormatBigIntegerToHexString(value, fmt, digits, info)); } bool decimalFmt = (fmt == 'g' || fmt == 'G' || fmt == 'd' || fmt == 'D' || fmt == 'r' || fmt == 'R'); if (value._bits == null) { if (fmt == 'g' || fmt == 'G' || fmt == 'r' || fmt == 'R') { if (digits > 0) { format = string.Format(CultureInfo.InvariantCulture, "D{0}", digits.ToString(CultureInfo.InvariantCulture)); } else { format = "D"; } } return(value._sign.ToString(format, info)); } // First convert to base 10^9. const uint kuBase = 1000000000; // 10^9 const int kcchBase = 9; int cuSrc = value._bits.Length; int cuMax; try { cuMax = checked (cuSrc * 10 / 9 + 2); } catch (OverflowException e) { throw new FormatException(SR.Format_TooLarge, e); } uint[] rguDst = new uint[cuMax]; int cuDst = 0; for (int iuSrc = cuSrc; --iuSrc >= 0;) { uint uCarry = value._bits[iuSrc]; for (int iuDst = 0; iuDst < cuDst; iuDst++) { Debug.Assert(rguDst[iuDst] < kuBase); ulong uuRes = NumericsHelpers.MakeUlong(rguDst[iuDst], uCarry); rguDst[iuDst] = (uint)(uuRes % kuBase); uCarry = (uint)(uuRes / kuBase); } if (uCarry != 0) { rguDst[cuDst++] = uCarry % kuBase; uCarry /= kuBase; if (uCarry != 0) { rguDst[cuDst++] = uCarry; } } } int cchMax; try { // Each uint contributes at most 9 digits to the decimal representation. cchMax = checked (cuDst * kcchBase); } catch (OverflowException e) { throw new FormatException(SR.Format_TooLarge, e); } if (decimalFmt) { if (digits > 0 && digits > cchMax) { cchMax = digits; } if (value._sign < 0) { try { // Leave an extra slot for a minus sign. cchMax = checked (cchMax + info.NegativeSign.Length); } catch (OverflowException e) { throw new FormatException(SR.Format_TooLarge, e); } } } int rgchBufSize; try { // We'll pass the rgch buffer to native code, which is going to treat it like a string of digits, so it needs // to be null terminated. Let's ensure that we can allocate a buffer of that size. rgchBufSize = checked (cchMax + 1); } catch (OverflowException e) { throw new FormatException(SR.Format_TooLarge, e); } char[] rgch = new char[rgchBufSize]; int ichDst = cchMax; for (int iuDst = 0; iuDst < cuDst - 1; iuDst++) { uint uDig = rguDst[iuDst]; Debug.Assert(uDig < kuBase); for (int cch = kcchBase; --cch >= 0;) { rgch[--ichDst] = (char)('0' + uDig % 10); uDig /= 10; } } for (uint uDig = rguDst[cuDst - 1]; uDig != 0;) { rgch[--ichDst] = (char)('0' + uDig % 10); uDig /= 10; } if (!decimalFmt) { // sign = true for negative and false for 0 and positive values bool sign = (value._sign < 0); // The cut-off point to switch (G)eneral from (F)ixed-point to (E)xponential form int precision = 29; int scale = cchMax - ichDst; return(FormatProvider.FormatBigInteger(precision, scale, sign, format, info, rgch, ichDst)); } // Format Round-trip decimal // This format is supported for integral types only. The number is converted to a string of // decimal digits (0-9), prefixed by a minus sign if the number is negative. The precision // specifier indicates the minimum number of digits desired in the resulting string. If required, // the number is padded with zeros to its left to produce the number of digits given by the // precision specifier. int numDigitsPrinted = cchMax - ichDst; while (digits > 0 && digits > numDigitsPrinted) { // pad leading zeros rgch[--ichDst] = '0'; digits--; } if (value._sign < 0) { string negativeSign = info.NegativeSign; for (int i = info.NegativeSign.Length - 1; i > -1; i--) { rgch[--ichDst] = info.NegativeSign[i]; } } return(new string(rgch, ichDst, cchMax - ichDst)); }
static String FormatBigInteger(BigInteger value, String format, NumberFormatInfo info) { int digits = 0; char fmt = ParseFormatSpecifier(format, out digits); if (fmt == 'x' || fmt == 'X') { return(FormatBigIntegerToHexString(value, fmt, digits, info)); } bool decimalFmt = (fmt == 'g' || fmt == 'G' || fmt == 'd' || fmt == 'D' || fmt == 'r' || fmt == 'R'); #if SILVERLIGHT || FEATURE_NETCORE || MONO if (!decimalFmt) { // Silverlight supports invariant formats only throw new FormatException(SR.GetString(SR.Format_InvalidFormatSpecifier)); } #endif //SILVERLIGHT ||FEATURE_NETCORE || MONO if (value._bits == null) { if (fmt == 'g' || fmt == 'G' || fmt == 'r' || fmt == 'R') { if (digits > 0) { format = String.Format(CultureInfo.InvariantCulture, "D{0}", digits.ToString(CultureInfo.InvariantCulture)); } else { format = "D"; } } return(value._sign.ToString(format, info)); } // First convert to base 10^9. const uint kuBase = 1000000000; // 10^9 const int kcchBase = 9; int cuSrc = BigInteger.Length(value._bits); int cuMax; try { cuMax = checked (cuSrc * 10 / 9 + 2); } catch (OverflowException e) { throw new FormatException(SR.GetString(SR.Format_TooLarge), e); } uint[] rguDst = new uint[cuMax]; int cuDst = 0; for (int iuSrc = cuSrc; --iuSrc >= 0;) { uint uCarry = value._bits[iuSrc]; for (int iuDst = 0; iuDst < cuDst; iuDst++) { Contract.Assert(rguDst[iuDst] < kuBase); ulong uuRes = NumericsHelpers.MakeUlong(rguDst[iuDst], uCarry); rguDst[iuDst] = (uint)(uuRes % kuBase); uCarry = (uint)(uuRes / kuBase); } if (uCarry != 0) { rguDst[cuDst++] = uCarry % kuBase; uCarry /= kuBase; if (uCarry != 0) { rguDst[cuDst++] = uCarry; } } } int cchMax; try { // Each uint contributes at most 9 digits to the decimal representation. cchMax = checked (cuDst * kcchBase); } catch (OverflowException e) { throw new FormatException(SR.GetString(SR.Format_TooLarge), e); } if (decimalFmt) { if (digits > 0 && digits > cchMax) { cchMax = digits; } if (value._sign < 0) { try { // Leave an extra slot for a minus sign. cchMax = checked (cchMax + info.NegativeSign.Length); } catch (OverflowException e) { throw new FormatException(SR.GetString(SR.Format_TooLarge), e); } } } int rgchBufSize; try { // We'll pass the rgch buffer to native code, which is going to treat it like a string of digits, so it needs // to be null terminated. Let's ensure that we can allocate a buffer of that size. rgchBufSize = checked (cchMax + 1); } catch (OverflowException e) { throw new FormatException(SR.GetString(SR.Format_TooLarge), e); } char[] rgch = new char[rgchBufSize]; int ichDst = cchMax; for (int iuDst = 0; iuDst < cuDst - 1; iuDst++) { uint uDig = rguDst[iuDst]; Contract.Assert(uDig < kuBase); for (int cch = kcchBase; --cch >= 0;) { rgch[--ichDst] = (char)('0' + uDig % 10); uDig /= 10; } } for (uint uDig = rguDst[cuDst - 1]; uDig != 0;) { rgch[--ichDst] = (char)('0' + uDig % 10); uDig /= 10; } #if !SILVERLIGHT || FEATURE_NETCORE if (!decimalFmt) { // // Go to the VM for GlobLoc aware formatting // Byte *numberBufferBytes = stackalloc Byte[Number.NumberBuffer.NumberBufferBytes]; Number.NumberBuffer number = new Number.NumberBuffer(numberBufferBytes); // sign = true for negative and false for 0 and positive values number.sign = (value._sign < 0); // the cut-off point to switch (G)eneral from (F)ixed-point to (E)xponential form number.precision = 29; number.digits[0] = '\0'; number.scale = cchMax - ichDst; int maxDigits = Math.Min(ichDst + 50, cchMax); for (int i = ichDst; i < maxDigits; i++) { number.digits[i - ichDst] = rgch[i]; } fixed(char *pinnedExtraDigits = rgch) { return(Number.FormatNumberBuffer(number.PackForNative(), format, info, pinnedExtraDigits + ichDst)); } } #endif //!SILVERLIGHT ||FEATURE_NETCORE // Format Round-trip decimal // This format is supported for integral types only. The number is converted to a string of // decimal digits (0-9), prefixed by a minus sign if the number is negative. The precision // specifier indicates the minimum number of digits desired in the resulting string. If required, // the number is padded with zeros to its left to produce the number of digits given by the // precision specifier. int numDigitsPrinted = cchMax - ichDst; while (digits > 0 && digits > numDigitsPrinted) { // pad leading zeros rgch[--ichDst] = '0'; digits--; } if (value._sign < 0) { String negativeSign = info.NegativeSign; for (int i = info.NegativeSign.Length - 1; i > -1; i--) { rgch[--ichDst] = info.NegativeSign[i]; } } return(new String(rgch, ichDst, cchMax - ichDst)); }
public BigInteger(ReadOnlySpan <byte> value, bool isUnsigned = false, bool isBigEndian = false) { int byteCount = value.Length; bool isNegative; if (byteCount > 0) { byte mostSignificantByte = isBigEndian ? value[0] : value[byteCount - 1]; isNegative = (mostSignificantByte & 0x80) != 0 && !isUnsigned; if (mostSignificantByte == 0) { // Try to conserve space as much as possible by checking for wasted leading byte[] entries if (isBigEndian) { int offset = 1; while (offset < byteCount && value[offset] == 0) { offset++; } value = value.Slice(offset); byteCount = value.Length; } else { byteCount -= 2; while (byteCount >= 0 && value[byteCount] == 0) { byteCount--; } byteCount++; } } } else { isNegative = false; } if (byteCount == 0) { // BigInteger.Zero _sign = 0; _bits = null; AssertValid(); return; } if (byteCount <= 4) { _sign = isNegative ? unchecked ((int)0xffffffff) : 0; if (isBigEndian) { for (int i = 0; i < byteCount; i++) { _sign = (_sign << 8) | value[i]; } } else { for (int i = byteCount - 1; i >= 0; i--) { _sign = (_sign << 8) | value[i]; } } _bits = null; if (_sign < 0 && !isNegative) { // Int32 overflow // Example: Int64 value 2362232011 (0xCB, 0xCC, 0xCC, 0x8C, 0x0) // can be naively packed into 4 bytes (due to the leading 0x0) // it overflows into the int32 sign bit _bits = new uint[1] { unchecked ((uint)_sign) }; _sign = +1; } if (_sign == int.MinValue) { this = s_bnMinInt; } } else { int unalignedBytes = byteCount % 4; int dwordCount = byteCount / 4 + (unalignedBytes == 0 ? 0 : 1); uint[] val = new uint[dwordCount]; int byteCountMinus1 = byteCount - 1; // Copy all dwords, except don't do the last one if it's not a full four bytes int curDword, curByte; if (isBigEndian) { curByte = byteCount - sizeof(int); for (curDword = 0; curDword < dwordCount - (unalignedBytes == 0 ? 0 : 1); curDword++) { for (int byteInDword = 0; byteInDword < 4; byteInDword++) { byte curByteValue = value[curByte]; val[curDword] = (val[curDword] << 8) | curByteValue; curByte++; } curByte -= 8; } } else { curByte = sizeof(int) - 1; for (curDword = 0; curDword < dwordCount - (unalignedBytes == 0 ? 0 : 1); curDword++) { for (int byteInDword = 0; byteInDword < 4; byteInDword++) { byte curByteValue = value[curByte]; val[curDword] = (val[curDword] << 8) | curByteValue; curByte--; } curByte += 8; } } // Copy the last dword specially if it's not aligned if (unalignedBytes != 0) { if (isNegative) { val[dwordCount - 1] = 0xffffffff; } if (isBigEndian) { for (curByte = 0; curByte < unalignedBytes; curByte++) { byte curByteValue = value[curByte]; val[curDword] = (val[curDword] << 8) | curByteValue; } } else { for (curByte = byteCountMinus1; curByte >= byteCount - unalignedBytes; curByte--) { byte curByteValue = value[curByte]; val[curDword] = (val[curDword] << 8) | curByteValue; } } } if (isNegative) { NumericsHelpers.DangerousMakeTwosComplement(val); // Mutates val // Pack _bits to remove any wasted space after the twos complement int len = val.Length - 1; while (len >= 0 && val[len] == 0) { len--; } len++; if (len == 1) { switch (val[0]) { case 1: // abs(-1) this = s_bnMinusOneInt; return; case kuMaskHighBit: // abs(Int32.MinValue) this = s_bnMinInt; return; default: if (unchecked ((int)val[0]) > 0) { _sign = (-1) * ((int)val[0]); _bits = null; AssertValid(); return; } break; } } if (len != val.Length) { _sign = -1; _bits = new uint[len]; Array.Copy(val, 0, _bits, 0, len); } else { _sign = -1; _bits = val; } } else { _sign = +1; _bits = val; } } AssertValid(); }
public static BigInteger ModPow(BigInteger value, BigInteger exponent, BigInteger modulus) { if (exponent.Sign < 0) { throw new ArgumentOutOfRangeException(nameof(exponent)); } value.AssertValid(); exponent.AssertValid(); modulus.AssertValid(); bool trivialValue = value._bits == null; bool trivialExponent = exponent._bits == null; bool trivialModulus = modulus._bits == null; if (trivialModulus) { uint bits = trivialValue && trivialExponent?BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), NumericsHelpers.Abs(exponent._sign), NumericsHelpers.Abs(modulus._sign)) : trivialValue?BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), exponent._bits, NumericsHelpers.Abs(modulus._sign)) : trivialExponent?BigIntegerCalculator.Pow(value._bits, NumericsHelpers.Abs(exponent._sign), NumericsHelpers.Abs(modulus._sign)) : BigIntegerCalculator.Pow(value._bits, exponent._bits, NumericsHelpers.Abs(modulus._sign)); return(value._sign < 0 && !exponent.IsEven ? -1 * bits : bits); } else { uint[] bits = trivialValue && trivialExponent?BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), NumericsHelpers.Abs(exponent._sign), modulus._bits) : trivialValue?BigIntegerCalculator.Pow(NumericsHelpers.Abs(value._sign), exponent._bits, modulus._bits) : trivialExponent?BigIntegerCalculator.Pow(value._bits, NumericsHelpers.Abs(exponent._sign), modulus._bits) : BigIntegerCalculator.Pow(value._bits, exponent._bits, modulus._bits); return(new BigInteger(bits, value._sign < 0 && !exponent.IsEven)); } }
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 = NumericsHelpers.CbitHighZero(num5); var num8 = 32 - num7; if (num7 > 0) { num5 = num5 << (num7 & 31) | num6 >> (num8 & 31); num6 = num6 << (num7 & 31); if (num1 > 2) { num6 = 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 = NumericsHelpers.MakeUlong(num11, regNum._bits[num9 + num1 - 1]); var num13 = regNum._bits[num9 + num1 - 2]; if (num7 > 0) { num12 = num12 << (num7 & 63) | num13 >> (num8 & 31); num13 = num13 << (num7 & 31); if (num9 + num1 >= 3) { num13 = 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 > NumericsHelpers.MakeUlong((uint)num15, num13)) { num14--; num15 += num5; } if (num14 > 0) { var num16 = (ulong)0; for (var i = 0; i < num1; i++) { num16 = num16 + regDen._bits[i] * num14; var num17 = (uint)num16; num16 = num16 >> 32; if (regNum._bits[num9 + i] < num17) { num16 = num16 + 1; } regNum._bits[num9 + i] = 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 = num14 - 1; } regNum._iuLast = num9 + num1 - 1; } if (fQuo) { 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 = NumericsHelpers.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 = high2 >> 1; high21 = 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 = 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; } else if (num9 < num12 || num9 + num11 > high21 - num6) { break; } else { num4 = (uint)num11; num5 = (uint)num12; high2 = num9; if (high2 > num5) { num8 = 1; num9 = high21 - high2; while (num9 >= high2 && num8 < 32) { num9 = 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(); } } }
/// <summary>Shifts a <see cref="BigInteger" /> value a specified number of bits to the right.</summary> /// <returns>A value that has been shifted to the right by the specified number of bits.</returns> /// <param name="value">The value whose bits are to be shifted.</param> /// <param name="shift">The number of bits to shift <paramref name="value" /> to the right.</param> public static BigInteger operator >>(BigInteger value, int shift) { switch (shift) { case 0: return(value); case int.MinValue: return(value << int.MaxValue << 1); default: break; } if (shift < 0) { return(value << -shift); } var digitShift = shift / 32; var smallShift = shift - (digitShift * 32); var negative = GetPartsForBitManipulation(ref value, out var xd, out var xl); if (negative) { if (shift >= 32 * xl) { return(MinusOne); } var temp = new uint[xl]; Array.Copy(xd, temp, xl); xd = temp; NumericsHelpers.DangerousMakeTwosComplement(xd); } var zl = xl - digitShift; if (zl < 0) { zl = 0; } var zd = new uint[zl]; if (smallShift != 0) { var carryShift = 32 - smallShift; uint carry = 0; for (var index = xl - 1; index >= digitShift; index--) { var rot = xd[index]; if (!negative || index != xl - 1) { zd[index - digitShift] = (rot >> (smallShift & 31)) | carry; } else { zd[index - digitShift] = (rot >> (smallShift & 31)) | (0xFFFFFFFF << (carryShift & 31)); } carry = rot << (carryShift & 31); } } else { for (var index = xl - 1; index >= digitShift; index--) { zd[index - digitShift] = xd[index]; } } if (negative) { NumericsHelpers.DangerousMakeTwosComplement(zd); } return(new BigInteger(zd, negative)); }