Class UInt128


  • final class UInt128
    extends java.lang.Object
    A mutable 128-bit unsigned integer.

    This is a specialised class to implement an accumulator of long values generated by squaring int values.

    Since:
    1.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private long ab
      bits 128-65.
      private int c
      bits 64-33.
      private int d
      bits 32-1 (low 32-bits).
      private static long MASK32
      Mask for the lower 32-bits of a long.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private UInt128()
      Create an instance.
      private UInt128​(long hi, int mid, int lo)
      Create an instance using a direct binary representation.
      (package private) UInt128​(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​(UInt128 x)
      Adds the value in-place.
      (package private) void addPositive​(long x)
      Adds the value in place.
      (package private) static UInt128 create()
      Create an instance.
      (package private) long hi64()
      Return the higher 64-bits as a long value.
      (package private) int lo32()
      Return the low 32-bits as an int value.
      (package private) long lo64()
      Return the lower 64-bits as a long value.
      (package private) int mid32()
      Return the middle 32-bits as an int value.
      (package private) static UInt128 of​(UInt96 x)
      Create an instance of the UInt96 value.
      (package private) UInt128 subtract​(UInt128 x)
      Subtracts the value.
      (package private) java.math.BigInteger toBigInteger()
      Convert to a BigInteger.
      (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.
      (package private) UInt128 unsignedMultiply​(int x)
      Multiply by the unsigned value.
      • 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
      • d

        private int d
        bits 32-1 (low 32-bits).
      • c

        private int c
        bits 64-33.
      • ab

        private long ab
        bits 128-65.
    • Constructor Detail

      • UInt128

        private UInt128()
        Create an instance.
      • UInt128

        private UInt128​(long hi,
                        int mid,
                        int lo)
        Create an instance using a direct binary representation.
        Parameters:
        hi - High 64-bits.
        mid - Middle 32-bits.
        lo - Low 32-bits.
      • UInt128

        UInt128​(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 UInt128 create()
        Create an instance. The initial value is zero.
        Returns:
        the instance
      • of

        static UInt128 of​(UInt96 x)
        Create an instance of the UInt96 value.
        Parameters:
        x - Value.
        Returns:
        the instance
      • addPositive

        void addPositive​(long x)
        Adds the value in place. It is assumed to be positive, for example the square of an int value. However no check is performed for a negative value.

        Note: This addition handles -9223372036854775808L as an unsigned value of 2^63.

        Parameters:
        x - Value.
      • add

        void add​(UInt128 x)
        Adds the value in-place.
        Parameters:
        x - Value.
      • unsignedMultiply

        UInt128 unsignedMultiply​(int x)
        Multiply by the unsigned value. Any overflow bits are lost.
        Parameters:
        x - Value.
        Returns:
        the product
      • subtract

        UInt128 subtract​(UInt128 x)
        Subtracts the value. Any overflow bits (negative result) are lost.
        Parameters:
        x - Value.
        Returns:
        the difference
      • toBigInteger

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

        double toDouble()
        Convert to a 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.
        Returns:
        bits 64-1
      • lo32

        int lo32()
        Return the low 32-bits as an int value.
        Returns:
        bits 32-1
      • mid32

        int mid32()
        Return the middle 32-bits as an int value.
        Returns:
        bits 64-33
      • hi64

        long hi64()
        Return the higher 64-bits as a long value.
        Returns:
        bits 128-65