public static void Multiply(out Int128 c, ref Int128 a, ref Int128 b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); if (b.IsNegative) { UInt128 bneg; UInt128.Negate(out bneg, ref b.v); UInt128.Multiply(out c.v, ref aneg, ref bneg); } else { UInt128.Multiply(out c.v, ref aneg, ref b.v); UInt128.Negate(ref c.v); } } else { if (b.IsNegative) { UInt128 bneg; UInt128.Negate(out bneg, ref b.v); UInt128.Multiply(out c.v, ref a.v, ref bneg); UInt128.Negate(ref c.v); } else { UInt128.Multiply(out c.v, ref a.v, ref b.v); } } //Debug.Assert((BigInteger)c == (BigInteger)a * (BigInteger)b); }
public static Int128 operator -(Int128 a) { Int128 c; UInt128.Negate(out c.v, ref a.v); return(c); }
public static void Multiply(out Int128 c, ref Int128 a, int b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); if (b < 0) { UInt128.Multiply(out c.v, ref aneg, (uint)(-b)); } else { UInt128.Multiply(out c.v, ref aneg, (uint)b); UInt128.Negate(ref c.v); } } else { if (b < 0) { UInt128.Multiply(out c.v, ref a.v, (uint)(-b)); UInt128.Negate(ref c.v); } else { UInt128.Multiply(out c.v, ref a.v, (uint)b); } } Debug.Assert((BigInteger)c == (BigInteger)a * (BigInteger)b); }
public static void Divide(out Int128 c, ref Int128 a, long b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); if (b < 0) { UInt128.Divide(out c.v, ref aneg, (ulong)(-b)); } else { UInt128.Divide(out c.v, ref aneg, (ulong)b); UInt128.Negate(ref c.v); } } else { if (b < 0) { UInt128.Divide(out c.v, ref a.v, (ulong)(-b)); UInt128.Negate(ref c.v); } else { UInt128.Divide(out c.v, ref a.v, (ulong)b); } } Debug.Assert((BigInteger)c == (BigInteger)a / (BigInteger)b); }
public static long Remainder(ref Int128 a, long b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); if (b < 0) { return((long)UInt128.Remainder(ref aneg, (ulong)(-b))); } else { return(-(long)UInt128.Remainder(ref aneg, (ulong)b)); } } else { if (b < 0) { return(-(long)UInt128.Remainder(ref a.v, (ulong)(-b))); } else { return((long)UInt128.Remainder(ref a.v, (ulong)b)); } } }
public static void Remainder(out Int128 c, ref Int128 a, ref Int128 b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); if (b.IsNegative) { UInt128 bneg; UInt128.Negate(out bneg, ref b.v); UInt128.Remainder(out c.v, ref aneg, ref bneg); } else { UInt128.Remainder(out c.v, ref aneg, ref b.v); UInt128.Negate(ref c.v); } } else { if (b.IsNegative) { UInt128 bneg; UInt128.Negate(out bneg, ref b.v); UInt128.Remainder(out c.v, ref a.v, ref bneg); UInt128.Negate(ref c.v); } else { UInt128.Remainder(out c.v, ref a.v, ref b.v); } } Debug.Assert((BigInteger)c == (BigInteger)a % (BigInteger)b); }
public static UInt128 TwosComplement(UInt128 a) { UInt128 c; UInt128.Negate(out c, ref a); return(c); }
public static void Pow(out Int128 result, ref Int128 value, int exponent) { if (exponent < 0) { throw new ArgumentException("exponent must not be negative"); } if (value.IsNegative) { UInt128 valueneg; UInt128.Negate(out valueneg, ref value.v); if ((exponent & 1) == 0) { UInt128.Pow(out result.v, ref valueneg, (uint)exponent); } else { UInt128.Pow(out result.v, ref valueneg, (uint)exponent); UInt128.Negate(ref result.v); } } else { UInt128.Pow(out result.v, ref value.v, (uint)exponent); } }
public static void GreatestCommonDivisor(out Int128 c, ref Int128 a, ref Int128 b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); if (b.IsNegative) { UInt128 bneg; UInt128.Negate(out bneg, ref b.v); UInt128.GreatestCommonDivisor(out c.v, ref aneg, ref bneg); } else { UInt128.GreatestCommonDivisor(out c.v, ref aneg, ref b.v); } } else { if (b.IsNegative) { UInt128 bneg; UInt128.Negate(out bneg, ref b.v); UInt128.GreatestCommonDivisor(out c.v, ref a.v, ref bneg); } else { UInt128.GreatestCommonDivisor(out c.v, ref a.v, ref b.v); } } }
public static int Remainder(ref Int128 a, int b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); if (b < 0) { return((int)UInt128.Remainder(ref aneg, (uint)(-b))); } else { return(-(int)UInt128.Remainder(ref aneg, (uint)b)); } } else { if (b < 0) { return(-(int)UInt128.Remainder(ref a.v, (uint)(-b))); } else { return((int)UInt128.Remainder(ref a.v, (uint)b)); } } }
public static void Divide(out Int128 c, ref Int128 a, int b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); if (b < 0) { UInt128.Divide(out c.v, ref aneg, (uint)(-b)); } else { UInt128.Divide(out c.v, ref aneg, (uint)b); UInt128.Negate(ref c.v); } } else { if (b < 0) { UInt128.Divide(out c.v, ref a.v, (uint)(-b)); UInt128.Negate(ref c.v); } else { UInt128.Divide(out c.v, ref a.v, (uint)b); } } ////Debug.Assert //((BigInteger)c = (BigInteger)a / (BigInteger)b); ///c = a / b; }
public static Int128 Abs(Int128 a) { if (!a.IsNegative) return a; Int128 c; UInt128.Negate(out c.v, ref a.v); return c; }
public static long FloorCbrt(Int128 a) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); return -(long)UInt128.FloorCbrt(aneg); } return (long)UInt128.FloorCbrt(a.v); }
public static long CeilingCbrt(Int128 a) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); return(-(long)UInt128.CeilingCbrt(aneg)); } return((long)UInt128.CeilingCbrt(a.v)); }
public static long Remainder(ref Int128 a, ulong b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); return -(long)UInt128.Remainder(ref aneg, b); } else return (long)UInt128.Remainder(ref a.v, b); }
public static int Remainder(ref Int128 a, uint b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); return -(int)UInt128.Remainder(ref aneg, b); } else return (int)UInt128.Remainder(ref a.v, b); }
public static Int128 Abs(Int128 a) { if (!a.IsNegative) { return(a); } Int128 c; UInt128.Negate(out c.v, ref a.v); return(c); }
public static Int128 Cube(long a) { Int128 c; if (a < 0) { UInt128.Cube(out c.v, (ulong)(-a)); UInt128.Negate(ref c.v); } else UInt128.Cube(out c.v, (ulong)a); return c; }
public static void Multiply(out Int128 c, ref Int128 a, uint b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); UInt128.Multiply(out c.v, ref aneg, b); UInt128.Negate(ref c.v); } else UInt128.Multiply(out c.v, ref a.v, b); Debug.Assert((BigInteger)c == (BigInteger)a * (BigInteger)b); }
public static Int128 Square(Int128 a) { Int128 c; if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); UInt128.Square(out c.v, ref aneg); } else UInt128.Square(out c.v, ref a.v); return c; }
public static void Divide(out Int128 c, ref Int128 a, ulong b) { if (a.IsNegative) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); UInt128.Divide(out c.v, ref aneg, b); UInt128.Negate(ref c.v); } else UInt128.Divide(out c.v, ref a.v, b); Debug.Assert((BigInteger)c == (BigInteger)a / (BigInteger)b); }
public static Int128 Cube(Int128 a) { Int128 c; if (a < 0) { UInt128 aneg; UInt128.Negate(out aneg, ref a.v); UInt128.Cube(out c.v, ref aneg); UInt128.Negate(ref c.v); } else UInt128.Cube(out c.v, ref a.v); return c; }
public static Int128 Negate(Int128 a) { Int128 c; UInt128.Negate(out c.v, ref a.v); return c; }