Example #1
0
        public static Int128 operator ++(Int128 a)
        {
            Int128 c;

            UInt128.Add(out c.v, ref a.v, 1);
            return(c);
        }
Example #2
0
        public static Int128 operator +(Int128 a, Int128 b)
        {
            Int128 c;

            UInt128.Add(out c.v, ref a.v, ref b.v);
            return(c);
        }
Example #3
0
File: int128.cs Project: sgf/SCTP
 public static void Subtract(ref Int128 a, long b)
 {
     if (b < 0)
         UInt128.Add(ref a.v, (ulong)(-b));
     else
         UInt128.Subtract(ref a.v, (ulong)b);
 }
Example #4
0
        public void UInt128AddOverflowLow64Test()
        {
            var addend = new UInt128(0UL, UInt64.MaxValue);
            var result = addend.Add(UInt128.One);
            var sb     = new UInt128(1UL, 0UL);

            Assert.AreEqual(sb, result);
        }
Example #5
0
File: int128.cs Project: sgf/SCTP
 public static Int128 operator -(Int128 a, long b)
 {
     Int128 c;
     if (b < 0)
         UInt128.Add(out c.v, ref a.v, (ulong)(-b));
     else
         UInt128.Subtract(out c.v, ref a.v, (ulong)b);
     return c;
 }
Example #6
0
File: int128.cs Project: sgf/SCTP
 public static Int128 operator +(long a, Int128 b)
 {
     Int128 c;
     if (a < 0)
         UInt128.Subtract(out c.v, ref b.v, (ulong)(-a));
     else
         UInt128.Add(out c.v, ref b.v, (ulong)a);
     return c;
 }
Example #7
0
        public static Int128 operator -(Int128 a, long b)
        {
            Int128 c;

            if (b < 0)
            {
                UInt128.Add(out c.v, ref a.v, (ulong)(-b));
            }
            else
            {
                UInt128.Subtract(out c.v, ref a.v, (ulong)b);
            }
            return(c);
        }
Example #8
0
File: int128.cs Project: sgf/SCTP
 public static void SubtractProduct(ref Int128 a, ref UInt128 b, long c)
 {
     UInt128 d;
     if (c < 0)
     {
         UInt128.Multiply(out d, ref b, (ulong)(-c));
         UInt128.Add(ref a.v, ref d);
     }
     else
     {
         UInt128.Multiply(out d, ref b, (ulong)c);
         UInt128.Subtract(ref a.v, ref d);
     }
 }
Example #9
0
File: int128.cs Project: sgf/SCTP
 public static void AddProduct(ref Int128 a, ref UInt128 b, long c)
 {
     UInt128 product;
     if (c < 0)
     {
         UInt128.Multiply(out product, ref b, (ulong)(-c));
         UInt128.Subtract(ref a.v, ref product);
     }
     else
     {
         UInt128.Multiply(out product, ref b, (ulong)c);
         UInt128.Add(ref a.v, ref product);
     }
 }
Example #10
0
        public static Int128 operator +(long a, Int128 b)
        {
            Int128 c;

            if (a < 0)
            {
                UInt128.Subtract(out c.v, ref b.v, (ulong)(-a));
            }
            else
            {
                UInt128.Add(out c.v, ref b.v, (ulong)a);
            }
            return(c);
        }
Example #11
0
        public UInt128 ProcessRegionManual(int thread, ulong w, ulong a1, ulong b1, UInt128 c1, ulong a2, ulong b2, UInt128 c2)
        {
            if (w <= 1)
            {
                return(0);
            }

            var s    = (UInt128)0;
            var umax = w - 1;
            var t1   = (a1 * b2 + b1 * a2) << 1;
            var t2   = (c1 << 1) - a1 - b1;
            var t3   = (t2 << 2) + 12;
            var t4   = (a1 * b1) << 2;
            var t5   = t1 * (1 + c1) - a1 + b1 - t4 * c2;
            var t6   = UInt128.Square(t2 + 2) - t4 * n;

            var u = (ulong)1;

            while (true)
            {
                Debug.Assert((t5 - UInt128.CeilingSqrt(t6)) / t4 == VFloor(u, a1, b1, c1, a2, b2, c2));
#if false
                s += (t5 - UInt128.CeilingSqrt(t6)) / t4;
                if (u >= umax)
                {
                    break;
                }
                t5 += t1;
                t6 += t3;
                t3 += 8;
#else
                UInt128.Add(ref s, (ulong)((t5 - UInt128.CeilingSqrt(t6)) / t4));
                if (u >= umax)
                {
                    break;
                }
                UInt128.Add(ref t5, t1);
                UInt128.Add(ref t6, ref t3);
                UInt128.Add(ref t3, 8);
#endif
                ++u;
            }

            Debug.Assert(s == ProcessRegionHorizontal(w, 0, a1, b1, c1, a2, b2, c2));
#if DIAG
            Console.WriteLine("ProcessRegionManual: s = {0}", s);
#endif
            return(s);
        }
Example #12
0
        public void UInt128_Add_OverflowAndCarryOver()
        {
            var baseValue = new UInt128()
            {
                High = UInt64.MaxValue, Low = 2
            };
            var addValue = new UInt128()
            {
                High = 2, Low = UInt64.MaxValue
            };

            var expectedValue = new UInt128()
            {
                High = 2, Low = 1
            };


            Assert.Equal(
                expectedValue,
                UInt128.Add(baseValue, addValue));
        }
Example #13
0
        public void UInt128_Add_SimpleMixed()
        {
            var baseValue = new UInt128()
            {
                High = 1, Low = 2
            };
            var addValue = new UInt128()
            {
                High = 2, Low = 1
            };

            var expectedValue = new UInt128()
            {
                High = 3, Low = 3
            };


            Assert.Equal(
                expectedValue,
                UInt128.Add(baseValue, addValue));
        }
Example #14
0
        public void UInt128_Add_SimpleLow()
        {
            var baseValue = new UInt128()
            {
                Low = 1
            };
            var addValue = new UInt128()
            {
                Low = 2
            };

            var expectedValue = new UInt128()
            {
                Low = 3
            };


            Assert.Equal(
                expectedValue,
                UInt128.Add(baseValue, addValue));
        }
Example #15
0
 public static UInt128 Invoke(UInt128 arg1, UInt128 arg2)
 {
     return(UInt128.Add(arg1, arg2));
 }
Example #16
0
File: int128.cs Project: sgf/SCTP
 public static Int128 Add(Int128 a, Int128 b)
 {
     Int128 c;
     UInt128.Add(out c.v, ref a.v, ref b.v);
     return c;
 }
Example #17
0
        protected override void Blocks(byte[] m, int offset, int count)
        {
            unchecked
            {
                UInt64  hibit = m_final ? 0 : ((UInt64)1 << 40); /* 1 << 128 */
                UInt64  r0, r1, r2;
                UInt64  s1, s2;
                UInt64  h0, h1, h2;
                UInt64  c;
                UInt128 d0 = new UInt128();
                UInt128 d1 = new UInt128();
                UInt128 d2 = new UInt128();

                r0 = m_r[0];
                r1 = m_r[1];
                r2 = m_r[2];

                h0 = m_h[0];
                h1 = m_h[1];
                h2 = m_h[2];

                s1 = r1 * (5 << 2);
                s2 = r2 * (5 << 2);

                while (count >= BlockSize)
                {
                    UInt64 t0, t1;

                    /* h += m[i] */
                    t0 = BytesToInt64(m, offset);
                    t1 = BytesToInt64(m, 8 + offset);

                    h0 += ((t0) & 0xfffffffffff);
                    h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff);
                    h2 += (((t1 >> 24)) & 0x3ffffffffff) | hibit;

                    /* h *= r */
                    d0.SetMultiplyUInt64(ref h0, ref r0);
                    d0.AddMultiplyUInt64(ref h1, ref s2);
                    d0.AddMultiplyUInt64(ref h2, ref s1);

                    d1.SetMultiplyUInt64(ref h0, ref r1);
                    d1.AddMultiplyUInt64(ref h1, ref r0);
                    d1.AddMultiplyUInt64(ref h2, ref s2);

                    d2.SetMultiplyUInt64(ref h0, ref r2);
                    d2.AddMultiplyUInt64(ref h1, ref r1);
                    d2.AddMultiplyUInt64(ref h2, ref r0);

                    /* (partial) h %= p */
                    c  = (d0 >> 44).Low;
                    h0 = d0.Low & 0xfffffffffff;

                    d1.Add(c);
                    c  = (d1 >> 44).Low;
                    h1 = d1.Low & 0xfffffffffff;

                    d2.Add(c);
                    c  = (d2 >> 42).Low;
                    h2 = d2.Low & 0x3ffffffffff;

                    h0 += c * 5;
                    c   = (h0 >> 44);
                    h0  = h0 & 0xfffffffffff;
                    h1 += c;

                    offset += BlockSize;
                    count  -= BlockSize;
                }

                m_h[0] = h0;
                m_h[1] = h1;
                m_h[2] = h2;
            }
        }
Example #18
0
 public static T Invoke <T>(T arg1, T arg2)
 {
     if (typeof(Int32) == typeof(T))
     {
         return((T)(object)checked ((Int32)(object)arg1 + (Int32)(object)arg2));
     }
     if (typeof(Int64) == typeof(T))
     {
         return((T)(object)checked ((Int64)(object)arg1 + (Int64)(object)arg2));
     }
     if (typeof(UInt32) == typeof(T))
     {
         return((T)(object)checked ((UInt32)(object)arg1 + (UInt32)(object)arg2));
     }
     if (typeof(UInt64) == typeof(T))
     {
         return((T)(object)checked ((UInt64)(object)arg1 + (UInt64)(object)arg2));
     }
     if (typeof(Int16) == typeof(T))
     {
         return((T)(object)checked ((Int16) unchecked ((Int16)(object)arg1 + (Int16)(object)arg2)));
     }
     if (typeof(UInt16) == typeof(T))
     {
         return((T)(object)checked ((UInt16) unchecked ((UInt16)(object)arg1 + (UInt16)(object)arg2)));
     }
     if (typeof(Byte) == typeof(T))
     {
         return((T)(object)checked ((Byte) unchecked ((Byte)(object)arg1 + (Byte)(object)arg2)));
     }
     if (typeof(SByte) == typeof(T))
     {
         return((T)(object)checked ((SByte) unchecked ((SByte)(object)arg1 + (SByte)(object)arg2)));
     }
     if (typeof(IntPtr) == typeof(T))
     {
         return((T)(object)CilVerifiable.Add((IntPtr)(object)arg1, (IntPtr)(object)arg2));
     }
     if (typeof(UIntPtr) == typeof(T))
     {
         return((T)(object)CilVerifiable.Add((UIntPtr)(object)arg1, (UIntPtr)(object)arg2));
     }
     if (typeof(Int128) == typeof(T))
     {
         return((T)(object)Int128.Add((Int128)(object)arg1, (Int128)(object)arg2));
     }
     if (typeof(UInt128) == typeof(T))
     {
         return((T)(object)UInt128.Add((UInt128)(object)arg1, (UInt128)(object)arg2));
     }
     if (typeof(Double) == typeof(T))
     {
         return((T)(object)((Double)(object)arg1 + (Double)(object)arg2));
     }
     if (typeof(Single) == typeof(T))
     {
         return((T)(object)(Single)((Single)(object)arg1 + (Single)(object)arg2));
     }
     if (typeof(Quadruple) == typeof(T))
     {
         return((T)(object)((Quadruple)(object)arg1 + (Quadruple)(object)arg2));
     }
     return(StandardFunctor <T>().Invoke(arg1, arg2));
 }
Example #19
0
 public static void Add(ref Int128 a, Int128 b)
 {
     UInt128.Add(ref a.v, ref b.v);
 }