Class Int128


  • final class Int128
    extends java.lang.Object
    A mutable 128-bit signed integer.

    This is a specialised class to implement an accumulator of long values.

    Note: This number uses a signed long integer representation of:

    value = 264 * hi64 + lo64

    If the high value is zero then the low value is the long representation of the number including the sign bit. Otherwise the low value corresponds to a correction term for the scaled high value which contains the sign-bit of the number.

    Since:
    1.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private long hi
      high 64-bits.
      private long lo
      low 64-bits.
      private static long MASK32
      Mask for the lower 32-bits of a long.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Int128()
      Create an instance.
      private Int128​(long x)
      Create an instance.
      (package private) Int128​(long hi, long lo)
      Create an instance using a direct binary representation.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) void add​(long x)
      Adds the value.
      (package private) void add​(Int128 x)
      Adds the value.
      (package private) static Int128 create()
      Create an instance.
      (package private) long hi64()
      Return the higher 64-bits as a long value.
      (package private) long lo64()
      Return the lower 64-bits as a long value.
      (package private) static Int128 of​(long x)
      Create an instance of the long value.
      (package private) UInt128 squareLow()
      Compute the square of the low 64-bits of this number.
      (package private) java.math.BigInteger toBigInteger()
      Convert to a BigInteger.
      (package private) org.apache.commons.numbers.core.DD toDD()
      Convert to a double-double.
      (package private) double toDouble()
      Convert to a double.
      (package private) int toIntExact()
      Convert to an int; throwing an exception if the value overflows an int.
      (package private) long toLongExact()
      Convert to a long; throwing an exception if the value overflows a long.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MASK32

        private static final long MASK32
        Mask for the lower 32-bits of a long.
        See Also:
        Constant Field Values
      • lo

        private long lo
        low 64-bits.
      • hi

        private long hi
        high 64-bits.
    • Constructor Detail

      • Int128

        private Int128()
        Create an instance.
      • Int128

        private Int128​(long x)
        Create an instance.
        Parameters:
        x - Value.
      • Int128

        Int128​(long hi,
               long lo)
        Create an instance using a direct binary representation. This is package-private for testing.
        Parameters:
        hi - High 64-bits.
        lo - Low 64-bits.
    • Method Detail

      • create

        static Int128 create()
        Create an instance. The initial value is zero.
        Returns:
        the instance
      • of

        static Int128 of​(long x)
        Create an instance of the long value.
        Parameters:
        x - Value.
        Returns:
        the instance
      • add

        void add​(long x)
        Adds the value.
        Parameters:
        x - Value.
      • add

        void add​(Int128 x)
        Adds the value.
        Parameters:
        x - Value.
      • squareLow

        UInt128 squareLow()
        Compute the square of the low 64-bits of this number.

        Warning: This ignores the upper 64-bits. Use with caution.

        Returns:
        the square
      • toBigInteger

        java.math.BigInteger toBigInteger()
        Convert to a BigInteger.
        Returns:
        the value
      • toDouble

        double toDouble()
        Convert to a double.
        Returns:
        the value
      • toDD

        org.apache.commons.numbers.core.DD toDD()
        Convert to a double-double.
        Returns:
        the value
      • toIntExact

        int toIntExact()
        Convert to an int; throwing an exception if the value overflows an int.
        Returns:
        the value
        Throws:
        java.lang.ArithmeticException - if the value overflows an int.
        See Also:
        Math.toIntExact(long)
      • toLongExact

        long toLongExact()
        Convert to a long; throwing an exception if the value overflows a long.
        Returns:
        the value
        Throws:
        java.lang.ArithmeticException - if the value overflows a long.
      • lo64

        long lo64()
        Return the lower 64-bits as a long value.

        If the high value is zero then the low value is the long representation of the number including the sign bit. Otherwise this value corresponds to a correction term for the scaled high value which contains the sign-bit of the number (see Int128).

        Returns:
        the low 64-bits
      • hi64

        long hi64()
        Return the higher 64-bits as a long value.
        Returns:
        the high 64-bits
        See Also:
        lo64()