Class IntMath


  • final class IntMath
    extends java.lang.Object
    Support class for integer math.
    Since:
    1.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int EXP_BIAS
      Bias offset for the exponent of a double.
      private static int EXP_SHIFT
      Shift for the exponent of a double.
      private static double HALF
      0.5.
      private static long MASK32
      Mask for the lower 32-bits of a long.
      private static long MASK52
      Mask for the lower 52-bits of a long.
      private static long TWO_POW_53
      2^53.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private IntMath()
      No instances.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static double divide​(Int128 x, long n)
      Divide value x by the count n.
      private static double roundToInteger​(double x)
      Get the whole number that is the nearest to x, with ties rounding towards positive infinity.
      (package private) static long squareHigh​(long x)
      Square the values as if an unsigned 64-bit long to produce the high 64-bits of the 128-bit unsigned result.
      (package private) static java.math.BigInteger toBigIntegerExact​(double x)
      Return the whole number that is nearest to the double argument x as an int, with ties rounding towards positive infinity.
      (package private) static int toIntExact​(double x)
      Return the whole number that is nearest to the double argument x as an int, with ties rounding towards positive infinity.
      (package private) static long toLongExact​(double x)
      Return the whole number that is nearest to the double argument x as an long, with ties rounding towards positive infinity.
      (package private) static double uint128ToDouble​(long hi, long lo)
      Convert an unsigned 128-bit integer to a double.
      (package private) static long unsignedMultiplyHigh​(long value1, long value2)
      Multiply the two values as if unsigned 64-bit longs to produce the high 64-bits of the 128-bit unsigned result.
      (package private) static double unsignedMultiplyToDouble​(long x, long y)
      Multiply the arguments as if unsigned integers to a double result.
      • Methods inherited from class java.lang.Object

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

      • IntMath

        private IntMath()
        No instances.
    • Method Detail

      • squareHigh

        static long squareHigh​(long x)
        Square the values as if an unsigned 64-bit long to produce the high 64-bits of the 128-bit unsigned result. a

        This method computes the equivalent of:

        
         Math.multiplyHigh(x, x)
         Math.unsignedMultiplyHigh(x, x) - (((x >> 63) & x) << 1)
         

        Note: The method Math.multiplyHigh was added in JDK 9 and should be used as above when the source code targets Java 11 to exploit the intrinsic method.

        Note: The method uses the unsigned multiplication. When the input is negative it can be adjusted to the signed result by subtracting the argument twice from the result.

        Parameters:
        x - Value
        Returns:
        the high 64-bits of the 128-bit result
      • unsignedMultiplyHigh

        static long unsignedMultiplyHigh​(long value1,
                                         long value2)
        Multiply the two values as if unsigned 64-bit longs to produce the high 64-bits of the 128-bit unsigned result.

        This method computes the equivalent of:

        
         Math.multiplyHigh(a, b) + ((a >> 63) & b) + ((b >> 63) & a)
         

        Note: The method Math.multiplyHigh was added in JDK 9 and should be used as above when the source code targets Java 11 to exploit the intrinsic method.

        Note: The method Math.unsignedMultiplyHigh was added in JDK 18 and should be used when the source code target allows.

        Taken from o.a.c.rng.core.source64.LXMSupport.

        Parameters:
        value1 - the first value
        value2 - the second value
        Returns:
        the high 64-bits of the 128-bit result
      • unsignedMultiplyToDouble

        static double unsignedMultiplyToDouble​(long x,
                                               long y)
        Multiply the arguments as if unsigned integers to a double result.
        Parameters:
        x - Value.
        y - Value.
        Returns:
        the double
      • uint128ToDouble

        static double uint128ToDouble​(long hi,
                                      long lo)
        Convert an unsigned 128-bit integer to a double.
        Parameters:
        hi - High 64-bits.
        lo - Low 64-bits.
        Returns:
        the double
      • toIntExact

        static int toIntExact​(double x)
        Return the whole number that is nearest to the double argument x as an int, with ties rounding towards positive infinity.

        This will raise an ArithmeticException if the closest integer result is not within the range [-2^31, 2^31), i.e. it overflows an int; or the argument x is not finite.

        Note: This method is equivalent to:

         Math.toIntExact(Math.round(x))
         

        The behaviour has been re-implemented for consistent error handling for int, long and BigInteger types.

        Parameters:
        x - Value.
        Returns:
        rounded value
        Throws:
        java.lang.ArithmeticException - if the result overflows an int, or x is not finite
        See Also:
        Math.round(double), Math.toIntExact(long)
      • toLongExact

        static long toLongExact​(double x)
        Return the whole number that is nearest to the double argument x as an long, with ties rounding towards positive infinity.

        This will raise an ArithmeticException if the closest integer result is not within the range [-2^63, 2^63), i.e. it overflows a long; or the argument x is not finite.

        Parameters:
        x - Value.
        Returns:
        rounded value
        Throws:
        java.lang.ArithmeticException - if the result overflows a long, or x is not finite
      • toBigIntegerExact

        static java.math.BigInteger toBigIntegerExact​(double x)
        Return the whole number that is nearest to the double argument x as an int, with ties rounding towards positive infinity.

        This will raise an ArithmeticException if the argument x is not finite.

        Parameters:
        x - Value.
        Returns:
        rounded value
        Throws:
        java.lang.ArithmeticException - if x is not finite
      • roundToInteger

        private static double roundToInteger​(double x)
        Get the whole number that is the nearest to x, with ties rounding towards positive infinity.

        This method is intended to perform the equivalent of Math.round(double) without converting to a long primitive type. This allows the domain of the result to be checked against the range [-2^63, 2^63).

        Note: Adapted from o.a.c.math4.AccurateMath.rint and modified to perform rounding towards positive infinity.

        Parameters:
        x - Number from which nearest whole number is requested.
        Returns:
        a double number r such that r is an integer r - 0.5 <= x < r + 0.5
      • divide

        static double divide​(Int128 x,
                             long n)
        Divide value x by the count n.
        Parameters:
        x - Value.
        n - Count.
        Returns:
        the quotient