/// <summary> /// Converts a BigInteger to int if it is small enough /// </summary> /// <param name="x">The value to convert</param> /// <returns>An int if x is small enough, otherwise x.</returns> /// <remarks> /// Use this helper to downgrade BigIntegers as necessary. /// </remarks> public static object/*!*/ Normalize(BigInteger/*!*/ x) { int result; if (x.AsInt32(out result)) { return ScriptingRuntimeHelpers.Int32ToObject(result); } return x; }
internal static int __cmp__(decimal x, BigInteger y) { if (object.ReferenceEquals(y, null)) return +1; BigInteger bx = BigInteger.Create(x); if (bx == y) { decimal mod = x % 1; if (mod == 0) return 0; if (mod > 0) return +1; else return -1; } return bx > y ? +1 : -1; }
PyInt_AsUnsignedLongMask(IntPtr valuePtr) { try { BigInteger unmasked = NumberMaker.MakeBigInteger(this.scratchContext, this.Retrieve(valuePtr)); BigInteger mask = new BigInteger(UInt32.MaxValue) + 1; BigInteger masked = BigInteger.Mod(unmasked, mask); if (masked < 0) { masked += mask; } return masked.ToUInt32(); } catch (Exception e) { this.LastException = e; return 0xFFFFFFFF; } }
private static Expression BigIntegerConstant(BigInteger value) { int ival; if (value.AsInt32(out ival)) { return Expression.Call( typeof(BigInteger).GetMethod("Create", new Type[] { typeof(int) }), Expression.Constant(ival) ); } long lval; if (value.AsInt64(out lval)) { return Expression.Call( typeof(BigInteger).GetMethod("Create", new Type[] { typeof(long) }), Expression.Constant(lval) ); } return Expression.New( typeof(BigInteger).GetConstructor(new Type[] { typeof(int), typeof(uint[]) }), Expression.Constant((int)value.Sign), CreateUIntArray(value.GetBits()) ); }
private StringBuilder/*!*/ AppendBaseBigInteger(BigInteger/*!*/ value, int radix) { StringBuilder/*!*/ str = new StringBuilder(); if (value == 0) str.Append('0'); while (value != 0) { int digit = (value % radix).ToInt32(); str.Append(_LowerDigits[digit]); value /= radix; } return str; }
public static BigInteger Subtract(int self, BigInteger other) { return (BigInteger)self - other; }
/// <summary> /// Test for shift overflow on negative BigIntegers /// </summary> /// <param name="self">Value before shifting</param> /// <param name="result">Value after shifting</param> /// <returns>-1 if we overflowed, otherwise result</returns> /// <remarks> /// Negative Bignums are supposed to look like they are stored in 2s complement infinite bit string, /// a negative number should always have spare 1s available for on the left hand side for right shifting. /// E.g. 8 == ...0001000; -8 == ...1110111, where the ... means that the left hand value is repeated indefinitely. /// The test here checks whether we have overflowed into the infinite 1s. /// [Arguably this should get factored into the BigInteger class.] /// </remarks> private static BigInteger/*!*/ ShiftOverflowCheck(BigInteger/*!*/ self, BigInteger/*!*/ result) { if (self.IsNegative() && result.IsZero()) { return -1; } return result; }
public static double ConvertToDouble(RubyContext/*!*/ context, BigInteger/*!*/ bignum) { double result; if (bignum.TryToFloat64(out result)) { return result; } context.ReportWarning("Bignum out of Float range"); return bignum.Sign > 0 ? Double.PositiveInfinity : Double.NegativeInfinity; }
private static BigInteger GetBigChecksum(MutableString/*!*/ self, int start, BigInteger/*!*/ sum, int bitCount) { BigInteger mask = (((BigInteger)1) << bitCount) - 1; int length = self.GetByteCount(); for (int i = start; i < length; i++) { sum = (sum + self.GetByte(i)) & mask; } return sum; }
private StringBuilder/*!*/ AppendBaseBigInteger(BigInteger value, int bitsToShift, bool lowerCase) { StringBuilder/*!*/ result = new StringBuilder(); bool isNegative = value.Sign == -1; BigInteger/*!*/ val = CastToUnsignedBigInteger(value); BigInteger/*!*/ limit = isNegative ? GenerateMask(value) : new BigInteger(0); uint mask = _Mask[bitsToShift]; char[] digits = lowerCase ? _LowerDigits : _UpperDigits; while (val != limit) { int t = (val & mask).ToInt32(); result.Append(digits[(val & mask).ToInt32()]); val >>= bitsToShift; limit >>= bitsToShift; } if (isNegative) result.Append(digits[mask]); return result; }
public static BigInteger Random(this Random generator, BigInteger limit) { ContractUtils.Requires(limit.Sign > 0, "limit"); ContractUtils.RequiresNotNull(generator, "generator"); // TODO: this doesn't yield a uniform distribution (small numbers will be picked more frequently): uint[] result = new uint[limit.GetWordCount() + 1]; for (int i = 0; i < result.Length; i++) { result[i] = unchecked((uint)generator.Next()); } return new BigInteger(1, result) % limit; }
internal static string/*!*/ ConvertToHostString(BigInteger/*!*/ address) { Assert.NotNull(address); ulong u; if (address.AsUInt64(out u)) { if (u <= UInt32.MaxValue) { // IPv4: return ConvertToHostString((uint)u); } else { // IPv6: byte[] bytes = new byte[8]; for (int i = bytes.Length - 1; i >= 0; --i) { bytes[i] = (byte)(u & 0xff); u >>= 8; } return new IPAddress(bytes).ToString(); } } else { throw RubyExceptions.CreateRangeError("bignum too big to convert into `quad long'"); } }
public _Array from_address(CodeContext/*!*/ context, BigInteger ptr) { _Array res = (_Array)CreateInstance(context); res.SetAddress(new IntPtr(ptr.ToInt64())); return res; }
internal void SetBigInteger(BigInteger value) { BigInteger = value; _type = TokenValueType.BigInteger; }
public _Structure from_address(CodeContext/*!*/ context, BigInteger address) { return from_address(context, new IntPtr(address.ToInt64())); }
public static HKEYType ConnectRegistry(string computerName, BigInteger key) { if (string.IsNullOrEmpty(computerName)) computerName = string.Empty; RegistryKey newKey; try { newKey = RegistryKey.OpenRemoteBaseKey(MapSystemKey(key), computerName); }catch(IOException ioe) { throw PythonExceptions.CreateThrowable(PythonExceptions.WindowsError, PythonExceptions._WindowsError.ERROR_BAD_NETPATH, ioe.Message); } catch (Exception e) { throw new ExternalException(e.Message); } return new HKEYType(newKey); }
public static object Add(BinaryOpStorage/*!*/ coercionStorage, BinaryOpStorage/*!*/ binaryOpSite, RubyContext/*!*/ context, BigInteger/*!*/ self, object other) { return Protocols.CoerceAndApply(coercionStorage, binaryOpSite, "+", context, self, other); }
private BigInteger/*!*/ CastToUnsignedBigInteger(BigInteger/*!*/ value) { return MakeBigIntegerFromByteArray(value.ToByteArray()); }
private BigInteger/*!*/ GenerateMask(BigInteger/*!*/ value) { byte[] bytes = new byte[value.ToByteArray().Length]; for (int i = 0; i < bytes.Length; i++) { bytes[i] = 0xFF; } return MakeBigIntegerFromByteArray(bytes); }
public static BigInteger Random(this Random generator, BigInteger limit) { return new BigInteger(generator.Random(limit.Value)); }
public int ioctl(BigInteger cmd, int option) { return _socket.IOControl((IOControlCode)cmd.ToInt64(), BitConverter.GetBytes(option), null); }
// Like GetBitCount(Abs(x)), except 0 maps to 0 public static int BitLength(BigInteger x) { if (x.IsZero()) { return 0; } return x.Abs().GetBitCount(); }
internal stat_result(int mode, BigInteger size, BigInteger st_atime, BigInteger st_mtime, BigInteger st_ctime) { _mode = mode; _size = size; _st_atime = _atime = TryShrinkToInt(st_atime); _st_mtime = _mtime = TryShrinkToInt(st_mtime); _st_ctime = _ctime = TryShrinkToInt(st_ctime); _ino = _dev = _nlink = _uid = _gid = ScriptingRuntimeHelpers.Int32ToObject(0); }
internal static UInt64 ToUInt64(BigInteger value) { UInt64 result; if (value.AsUInt64(out result)) { return result; } throw RubyExceptions.CreateRangeError("number too big to convert into System::UInt64"); }
public static PyHKEY ConnectRegistry(string computerName, BigInteger key) { if (string.IsNullOrEmpty(computerName)) computerName = string.Empty; RegistryKey newKey; try { newKey = RegistryKey.OpenRemoteBaseKey(MapSystemKey(key), computerName); } catch (Exception e) { throw new ExternalException(e.Message); } return new PyHKEY(newKey); }
public static void FreeLibrary(BigInteger handle) { FreeLibrary(new IntPtr(handle.ToInt64())); }
public static RubyArray/*!*/ Coerce(RubyContext/*!*/ context, BigDecimal/*!*/ self, BigInteger/*!*/ other) { return RubyOps.MakeArray2(BigDecimal.Create(GetConfig(context), other.ToString()), self); }
private static RegistryHive MapSystemKey(BigInteger hKey) { if (hKey == HKEY_CLASSES_ROOT) return RegistryHive.ClassesRoot; else if (hKey == HKEY_CURRENT_CONFIG) return RegistryHive.CurrentConfig; else if (hKey == HKEY_CURRENT_USER) return RegistryHive.CurrentUser; else if (hKey == HKEY_DYN_DATA) return RegistryHive.DynData; else if (hKey == HKEY_LOCAL_MACHINE) return RegistryHive.LocalMachine; else if (hKey == HKEY_PERFORMANCE_DATA) return RegistryHive.PerformanceData; else if (hKey == HKEY_USERS) return RegistryHive.Users; else throw new ArgumentException("Unknown system key"); }
public static object call_function(CodeContext context, BigInteger address, PythonTuple args) { return call_function(context, new IntPtr(address.ToInt64()), args); }
public static BigInteger Add(int self, BigInteger other) { return (BigInteger)self + other; }