public void LeftBlockShiftTest() { Random random = new Random(1234); for (int sft = 0; sft < BigUInt <Pow2.N32> .Length; sft++) { UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, BigUInt <Pow2.N32> .Bits - sft * UIntUtil.UInt32Bits); BigUInt <Pow2.N32> v = new BigUInt <Pow2.N32>(mantissa, enable_clone: false); BigInteger bi = v; BigUInt <Pow2.N32> v_sft = BigUInt <Pow2.N32> .LeftBlockShift(v, sft); BigInteger bi_sft = bi << (sft * UIntUtil.UInt32Bits); Console.WriteLine(sft); Console.WriteLine(v.ToHexcode()); Console.WriteLine(v_sft.ToHexcode()); Assert.AreEqual(bi_sft, v_sft); Console.Write("\n"); } Assert.ThrowsException <OverflowException>(() => { BigUInt <Pow2.N32> v = new BigUInt <Pow2.N32>(0x12345678u); BigUInt <Pow2.N32> v_sft = BigUInt <Pow2.N32> .LeftBlockShift(v, BigUInt <Pow2.N32> .Length); }); }
public void ParseTest() { Random random = new Random(1234); for (int i = 0; i <= 2500; i++) { UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N16> .Length, random.Next(BigUInt <Pow2.N16> .Bits + 1)); BigUInt <Pow2.N16> v = new BigUInt <Pow2.N16>(mantissa, enable_clone: false); BigUInt <Pow2.N16> v2 = new BigUInt <Pow2.N16>(v.ToString()); Assert.AreEqual(v, v2); } for (int i = 0; i <= 2500; i++) { UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, random.Next(BigUInt <Pow2.N32> .Bits + 1)); BigUInt <Pow2.N32> v = new BigUInt <Pow2.N32>(mantissa, enable_clone: false); BigUInt <Pow2.N32> v2 = new BigUInt <Pow2.N32>(v.ToString()); Assert.AreEqual(v, v2); } for (int i = 0; i <= 2500; i++) { UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N64> .Length, random.Next(BigUInt <Pow2.N64> .Bits + 1)); BigUInt <Pow2.N64> v = new BigUInt <Pow2.N64>(mantissa, enable_clone: false); BigUInt <Pow2.N64> v2 = new BigUInt <Pow2.N64>(v.ToString()); Assert.AreEqual(v, v2); } }
public void MulTest() { Random random = new Random(1234); for (int i = 0; i <= 50000; i++) { int bits1 = random.Next(BigUInt <Pow2.N32> .Bits + 1); int bits2 = random.Next(BigUInt <Pow2.N32> .Bits - bits1); UInt32[] value1 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits1); UInt32[] value2 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits2); if (random.Next(2) < 1) { value1[0] = 0; } if (random.Next(2) < 1) { value2[0] = 0; } BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false); BigUInt <Pow2.N32> v2 = new BigUInt <Pow2.N32>(value2, enable_clone: false); BigInteger bi1 = v1, bi2 = v2; BigUInt <Pow2.N32> v = BigUInt <Pow2.N32> .Mul(v1, v2); BigInteger bi = bi1 * bi2; Console.WriteLine(bi); Console.WriteLine(v); Assert.AreEqual(bi, v); } }
public void RoundDivTest() { Random random = new Random(1234); for (int i = 0; i <= 50000; i++) { int bits1 = random.Next(BigUInt <Pow2.N32> .Bits / 2 + 1, BigUInt <Pow2.N32> .Bits + 1); int bits2 = random.Next(BigUInt <Pow2.N32> .Bits - bits1); UInt32[] value1 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits1); UInt32[] value2 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits2); BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false); BigUInt <Pow2.N32> v2 = new BigUInt <Pow2.N32>(value2, enable_clone: false); BigInteger bi1 = v1, bi2 = v2; if (v2.IsZero) { continue; } BigUInt <Pow2.N32> vdiv = BigUInt <Pow2.N32> .RoundDiv(v1, v2); BigInteger bidiv = bi1 / bi2, birem = bi1 % bi2; if (birem * 2 >= bi2) { bidiv++; } Assert.AreEqual(bidiv, vdiv); } }
public void DivULongTest() { Random random = new Random(1234); for (int i = 0; i <= 50000; i++) { int bits1 = random.Next(BigUInt <Pow2.N32> .Bits / 2 + 1, BigUInt <Pow2.N32> .Bits + 1); int bits2 = random.Next(64); UInt32[] value1 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits1); UInt32[] value2 = UIntUtil.Random(random, 2, bits2); BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false); UInt64 v2 = UIntUtil.Pack(value2[1], value2[0]); BigInteger bi1 = v1, bi2 = v2; if (v2 == 0) { continue; } (BigUInt <Pow2.N32> vdiv, UInt64 vrem) = BigUInt <Pow2.N32> .Div(v1, v2); BigInteger bidiv = bi1 / bi2, birem = bi1 - bi2 * bidiv; Assert.IsTrue(vrem < v2); Assert.AreEqual(birem, vrem, "rem"); Assert.AreEqual(bidiv, vdiv, "div"); Assert.AreEqual(v1, BigUInt <Pow2.N32> .Add(BigUInt <Pow2.N32> .Mul(vdiv, v2), vrem)); } }
public static MultiPrecision <N> Pow(MultiPrecision <N> x, long n) { if (x.IsNaN) { return(NaN); } if (n == 0) { return(One); } ulong n_abs = UIntUtil.Abs(n); MultiPrecision <Plus1 <N> > y = 1, z = x.Convert <Plus1 <N> >(); while (n_abs > 0) { if ((n_abs & 1) == 1) { y *= z; } z *= z; n_abs >>= 1; } return(((n > 0) ? y : (1 / y)).Convert <N>()); }
public void CmpTest() { const int length = 4; Random random = new Random(1234); for (int i = 0; i <= 2500; i++) { UInt32[] mantissa1 = UIntUtil.Random(random, length, random.Next(length * UIntUtil.UInt32Bits + 1)); UInt32[] mantissa2 = UIntUtil.Random(random, length, random.Next(length * UIntUtil.UInt32Bits + 1)); BigUInt <Pow2.N4> n1 = new BigUInt <Pow2.N4>(mantissa1, enable_clone: false); BigUInt <Pow2.N4> n2 = random.Next(8) < 7 ? new BigUInt <Pow2.N4>(mantissa2, enable_clone: false) : n1.Copy(); BigInteger bi1 = n1, bi2 = n2; Console.WriteLine(n1); Console.WriteLine(n2); Assert.AreEqual(n1.Equals(n2), bi1.Equals(bi2)); Assert.AreEqual(n1 == n2, bi1 == bi2); Assert.AreEqual(n1 != n2, bi1 != bi2); Assert.AreEqual(n1 < n2, bi1 < bi2); Assert.AreEqual(n1 > n2, bi1 > bi2); Assert.AreEqual(n1 <= n2, bi1 <= bi2); Assert.AreEqual(n1 >= n2, bi1 >= bi2); Console.Write("\n"); } }
public void RandomTest() { const int length = 4; Random random = new Random(); for (int i = 0; i < length * UIntUtil.UInt32Bits; i++) { UInt32[] vs = UIntUtil.Random(random, length, i); Console.WriteLine($"{i} : {UIntUtil.ToHexcode(vs)}"); } }
public void MulDivTest() { Random random = new Random(1234); for (int i = 0; i <= 50000; i++) { int bits1 = random.Next(BigUInt <Pow2.N32> .Bits + 1); int bits2 = random.Next(BigUInt <Pow2.N32> .Bits - bits1); UInt32[] value1 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits1); UInt32[] value2 = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, bits2); BigUInt <Pow2.N32> v1 = new BigUInt <Pow2.N32>(value1, enable_clone: false); BigUInt <Pow2.N32> v2 = new BigUInt <Pow2.N32>(value2, enable_clone: false); BigUInt <Pow2.N32> v3 = new BigUInt <Pow2.N32>((UInt32)random.Next(4)); if (v2.IsZero || v2 <= v3) { continue; } if (random.Next(3) < 2 || v1.IsZero || v2.IsZero || v3.IsZero) { (BigUInt <Pow2.N32> vdiv, BigUInt <Pow2.N32> vrem) = BigUInt <Pow2.N32> .Div(BigUInt <Pow2.N32> .Add(BigUInt <Pow2.N32> .Mul(v1, v2), v3), v2); Assert.AreEqual(v1, vdiv); Assert.AreEqual(v3, vrem); } else { try { (BigUInt <Pow2.N32> vdiv, BigUInt <Pow2.N32> vrem) = BigUInt <Pow2.N32> .Div(BigUInt <Pow2.N32> .Sub(BigUInt <Pow2.N32> .Mul(v1, v2), v3), v2); Assert.AreEqual(v1 - new BigUInt <Pow2.N32>(1), vdiv); Assert.AreEqual(v2 - v3, vrem); } catch (OverflowException) { Console.WriteLine(v1); Console.WriteLine(v2); Console.WriteLine(v3); throw; } } } }
public void RightRoundBlockShiftTest() { Random random = new Random(1234); UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, BigUInt <Pow2.N32> .Bits); BigUInt <Pow2.N32> v = new BigUInt <Pow2.N32>(mantissa, enable_clone: false); BigInteger bi = v; for (int sft = 0; sft <= BigUInt <Pow2.N32> .Length + 4; sft++) { BigUInt <Pow2.N32> v_sft = BigUInt <Pow2.N32> .RightRoundBlockShift(v, sft); BigInteger bi_sft; if (sft > 0) { bi_sft = bi >> (sft * UIntUtil.UInt32Bits - 1); if ((bi_sft & 1) == 1) { bi_sft = (bi_sft >> 1) + 1; Console.WriteLine("RoundUp"); } else { bi_sft >>= 1; Console.WriteLine("RoundDown"); } } else { bi_sft = bi >> (sft * UIntUtil.UInt32Bits); Console.WriteLine("RoundDown"); } Console.WriteLine(sft); Console.WriteLine(v.ToHexcode()); Console.WriteLine(v_sft.ToHexcode()); Assert.AreEqual(bi_sft, v_sft); Console.Write("\n"); } }
public void MostSignificantDigitsTest() { BigUInt<Pow2.N4> n1 = new BigUInt<Pow2.N4>(new UInt32[] { 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu }, enable_clone: false); BigUInt<Pow2.N4> n2 = new BigUInt<Pow2.N4>(new UInt32[] { 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0x7FFFFFFFu }, enable_clone: false); BigUInt<Pow2.N4> n3 = new BigUInt<Pow2.N4>(new UInt32[] { 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0x3FFFFFFFu }, enable_clone: false); BigUInt<Pow2.N4> n4 = new BigUInt<Pow2.N4>(new UInt32[] { 0xFFFFFFFFu, 0u, 0xFFFFFFFFu, 0x3FFFFFFFu }, enable_clone: false); BigUInt<Pow2.N4> n5 = new BigUInt<Pow2.N4>(new UInt32[] { 0x7FFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu }, enable_clone: false); BigUInt<Pow2.N4> n6 = new BigUInt<Pow2.N4>(); BigUInt<Pow2.N4> n7 = new BigUInt<Pow2.N4>(1u); BigUInt<Pow2.N4> n8 = new BigUInt<Pow2.N4>(new UInt32[] { 0xFFFFFFFFu, 0xFFFFFFFFu, 0x7FFFFFFFu, 0u }, enable_clone: false); Assert.AreEqual(UIntUtil.Pack(0xFFFFFFFFu, 0xFFFFFFFFu), n1.MostSignificantDigits); Assert.AreEqual(UIntUtil.Pack(0x7FFFFFFFu, 0xFFFFFFFFu), n2.MostSignificantDigits); Assert.AreEqual(UIntUtil.Pack(0x3FFFFFFFu, 0xFFFFFFFFu), n3.MostSignificantDigits); Assert.AreEqual(UIntUtil.Pack(0x3FFFFFFFu, 0xFFFFFFFFu), n4.MostSignificantDigits); Assert.AreEqual(UIntUtil.Pack(0xFFFFFFFFu, 0xFFFFFFFFu), n5.MostSignificantDigits); Assert.AreEqual(0ul, n6.MostSignificantDigits); Assert.AreEqual(0ul, n7.MostSignificantDigits); Assert.AreEqual(0x7FFFFFFFul, n8.MostSignificantDigits); }
public void MulULongTest() { UInt64[] vs = { 0, 1, 12, 123, 4567, 0xFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFFFFFFFFFul }; foreach (UInt64 v in vs) { foreach (UInt64 u in vs) { BigUInt <Pow2.N4> x = new BigUInt <Pow2.N4>(UIntUtil.Mul(v, u), enable_clone: false); BigInteger y = (BigInteger)v * (BigInteger)u; Console.WriteLine($"{u} * {v} = {x}"); Console.WriteLine($"{u} * {v} = {y}"); Console.WriteLine($"{x.ToHexcode()}"); Assert.AreEqual(y, x); } } }
public void ToStringTest() { Random random = new Random(1234); for (int i = 0; i <= 2500; i++) { UInt32[] mantissa = UIntUtil.Random( random, BigUInt <Pow2.N32> .Length, random.Next(BigUInt <Pow2.N32> .Bits + 1) ); BigUInt <Pow2.N32> v = new BigUInt <Pow2.N32>(mantissa, enable_clone: false); BigInteger bi = v; Assert.AreEqual(bi.ToString(), v.ToString()); Console.WriteLine(v); } }
public void RightShiftTest() { Random random = new Random(1234); UInt32[] mantissa = UIntUtil.Random(random, BigUInt <Pow2.N32> .Length, BigUInt <Pow2.N32> .Bits); BigUInt <Pow2.N32> v = new BigUInt <Pow2.N32>(mantissa, enable_clone: false); BigInteger bi = v; for (int sft = 0; sft <= BigUInt <Pow2.N32> .Bits + 4; sft++) { BigUInt <Pow2.N32> v_sft = v >> sft; BigInteger bi_sft = bi >> sft; Console.WriteLine(sft); Console.WriteLine(v.ToHexcode()); Console.WriteLine(v_sft.ToHexcode()); Assert.AreEqual(bi_sft, v_sft); Console.Write("\n"); } }
public void BitTest() { const int length = 4; UInt32[] vs = new UInt32[length]; for (int i = 0; i < length * UIntUtil.UInt32Bits; i++) { Assert.AreEqual(0u, UIntUtil.GetLSB(vs, i)); UIntUtil.SetLSB(vs, i); Assert.AreEqual(1u, UIntUtil.GetLSB(vs, i)); Console.WriteLine(UIntUtil.ToHexcode(vs)); } for (int i = 0; i < length * UIntUtil.UInt32Bits; i++) { Assert.AreEqual(1u, UIntUtil.GetLSB(vs, i)); UIntUtil.ResetLSB(vs, i); Assert.AreEqual(0u, UIntUtil.GetLSB(vs, i)); Console.WriteLine(UIntUtil.ToHexcode(vs)); } for (int i = 0; i < length * UIntUtil.UInt32Bits; i++) { UInt32[] us = Enumerable.Repeat(0xFFFFFFFFu, length).ToArray(); UIntUtil.FlushLSB(us, i); Console.WriteLine(UIntUtil.ToHexcode(us)); } }