Пример #1
0
        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);
            });
        }
Пример #2
0
        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");
            }
        }
Пример #3
0
        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);
                }
            }
        }
Пример #4
0
        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");
            }
        }