Exemple #1
0
        public void CompareDivision()
        {
            Random rng = new Random(222);

            UInt128[] us = GetRandomUInt128(rng, 1000).ToArray();
            uint[]    vs = new uint[1000];
            for (int i = 0; i < vs.Length; i++)
            {
                vs[i] = (uint)rng.Next();
            }

            Stopwatch t1 = Stopwatch.StartNew();

            foreach (UInt128 u in us)
            {
                foreach (uint v in vs)
                {
                    UInt128 q1 = UInt128.DivRem(u, (UInt128)v, out _);
                }
            }
            t1.Stop();
            Console.WriteLine(t1.ElapsedMilliseconds);

            Stopwatch t2 = Stopwatch.StartNew();

            foreach (UInt128 u in us)
            {
                foreach (uint v in vs)
                {
                    UInt128 q1 = UInt128.DivRem(u, v, out _);
                }
            }
            t2.Stop();
            Console.WriteLine(t2.ElapsedMilliseconds);
        }
Exemple #2
0
        public void UInt128RandomArithmetic()
        {
            Random rng = new Random(1);

            foreach (UInt128 a in GetRandomUInt128(rng, 8))
            {
                // ToString and Parse round-trip
                UInt128 rt = UInt128.Parse(a.ToString());
                Assert.IsTrue(rt == a);

                foreach (UInt128 b in GetRandomUInt128(rng, 8))
                {
                    // Subtraction and addition are inverses
                    UInt128 d = a - b;
                    Assert.IsTrue(d + b == a);

                    UInt128 c = b - a;
                    Assert.IsTrue(a + c == b);

                    // Division and multiplication are inverses
                    UInt128 r;
                    UInt128 q = UInt128.DivRem(a, b, out r);
                    Assert.IsTrue(q * b + r == a);
                    Assert.IsTrue(a / b == q);
                    Assert.IsTrue(a % b == r);

                    UInt128 s = b / a;
                    UInt128 t = b % a;
                    Assert.IsTrue(s * a + t == b);
                }
            }
        }
Exemple #3
0
        public void CornerCaseDivision()
        {
            // This division problem is chosen to exercise the rare case
            // when our initial estimated quotient is off-by-one.

            UInt128 u = UInt128.MaxValue;
            UInt128 v = (UInt128.One << 64) + 3;
            //UInt128 v = new UInt128(1UL, 3UL);

            UInt128 q = UInt128.DivRem(u, v, out UInt128 r);

            Assert.IsTrue(q * v + r == u);
        }
Exemple #4
0
        public void BinaryLongDivisionAgreement()
        {
            Random rng = new Random(12);

            foreach (UInt128 u in GetRandomUInt128(rng, 8))
            {
                foreach (UInt128 v in GetRandomUInt128(rng, 8))
                {
                    UInt128 q = BinaryLongDivision(u, v, out UInt128 r);

                    UInt128 q1 = UInt128.DivRem(u, v, out UInt128 r1);

                    Assert.IsTrue(q == q1);
                    Assert.IsTrue(r == r1);
                }
            }
        }
Exemple #5
0
        public void UInt128TimingComparison()
        {
            UInt128[] values = GetRandomUInt128(new Random(1), 1000).ToArray();
            TimeOperations <UInt128>("Addition", (x, y) => { UInt128 z = x + y; }, values);
            TimeOperations <UInt128>("Subtraction", (x, y) => { UInt128 z = x - y; }, values);
            TimeOperations <UInt128>("Multiplication", (x, y) => { UInt128 z = x * y; }, values);
            TimeOperations <UInt128>("Division", (x, y) => { UInt128.DivRem(x, y, out _); }, values);

            BigInteger[] bigValues = values.Select(x => (BigInteger)x).ToArray();
            TimeOperations <BigInteger>("Big Addition", (x, y) => { BigInteger z = x + y; }, bigValues);
            TimeOperations <BigInteger>("Big Subtraction", (x, y) => { BigInteger z = x - y; }, bigValues);
            TimeOperations <BigInteger>("Big Multiplication", (x, y) => { BigInteger z = x * y; }, bigValues);
            TimeOperations <BigInteger>("Big Division", (x, y) => { BigInteger.DivRem(x, y, out _); }, bigValues);

            uint[]       smallValues      = GetRandomUInt32(new Random(2), 1000).ToArray();
            BigInteger[] smallAsBigValues = smallValues.Select(x => (BigInteger)x).ToArray();
            //TimeOperations2<UInt128, UInt128>("Division 2L", (x, y) => { UInt128 z = UInt128.DivRem(x, y, out _); }, values, smallValuesAsValues);
            TimeOperations2 <UInt128, uint>("Division 128/32", (x, y) => { UInt128 z = UInt128.DivRem(x, y, out _); }, values, smallValues);
            TimeOperations2 <BigInteger, uint>("Big Division 128/32", (x, y) => { BigInteger.DivRem(x, y, out _); }, bigValues, smallValues);
        }
Exemple #6
0
        public void CompareToBigInteger()
        {
            Random rng = new Random(1);

            UInt128[] values = GetRandomUInt128(rng, 1000).ToArray();

            Stopwatch t1 = Stopwatch.StartNew();

            for (int i = 0; i < values.Length; i++)
            {
                for (int j = 0; j < values.Length; j++)
                {
                    UInt128 q = UInt128.DivRem(values[i], values[j], out _);
                    //UInt128 sum = values[i] - values[j];
                }
            }
            t1.Stop();
            Console.WriteLine(t1.ElapsedMilliseconds);

            BigInteger[] bigs = new BigInteger[values.Length];
            for (int i = 0; i < bigs.Length; i++)
            {
                bigs[i] = BigInteger.Parse(values[i].ToString());
            }

            Stopwatch t2 = Stopwatch.StartNew();

            for (int i = 0; i < values.Length; i++)
            {
                for (int j = 0; j < values.Length; j++)
                {
                    BigInteger r;
                    BigInteger q = BigInteger.DivRem(bigs[i], bigs[j], out r);
                    //BigInteger q = BigInteger.Add(bigs[i], bigs[j]);
                    //BigInteger sum = bigs[i] - bigs[j];
                }
            }
            t2.Stop();
            Console.WriteLine(t2.ElapsedMilliseconds);

            UInt128B[] bValues = new UInt128B[values.Length];
            for (int i = 0; i < bValues.Length; i++)
            {
                uint s0 = (uint)rng.Next();
                uint s1 = (uint)rng.Next();
                uint s2 = (uint)rng.Next();
                uint s3 = (uint)rng.Next();
                bValues[i] = new UInt128B(s3, s2, s1, s0);
            }

            Stopwatch t3 = Stopwatch.StartNew();

            for (int i = 0; i < values.Length; i++)
            {
                for (int j = 0; j < values.Length; j++)
                {
                    //UInt128B p = bValues[i] - bValues[j];
                    UInt128B q = bValues[i] / bValues[j];
                }
            }
            t3.Stop();
            Console.WriteLine(t3.ElapsedMilliseconds);
        }