Class IntMath
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intBias offset for the exponent of a double.private static final intShift for the exponent of a double.private static final double0.5.private static final longMask for the lower 32-bits of a long.private static final longMask for the lower 52-bits of a long. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static doubleroundToInteger(double x) Get the whole number that is the nearest to x, with ties rounding towards positive infinity.(package private) static longsquareHigh(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 BigIntegertoBigIntegerExact(double x) Return the whole number that is nearest to thedoubleargumentxas anint, with ties rounding towards positive infinity.(package private) static inttoIntExact(double x) Return the whole number that is nearest to thedoubleargumentxas anint, with ties rounding towards positive infinity.(package private) static longtoLongExact(double x) Return the whole number that is nearest to thedoubleargumentxas anlong, with ties rounding towards positive infinity.(package private) static doubleuint128ToDouble(long hi, long lo) Convert an unsigned 128-bit integer to adouble.(package private) static longunsignedMultiplyHigh(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 doubleunsignedMultiplyToDouble(long x, long y) Multiply the arguments as if unsigned integers to adoubleresult.
-
Field Details
-
MASK32
private static final long MASK32Mask for the lower 32-bits of a long.- See Also:
-
MASK52
private static final long MASK52Mask for the lower 52-bits of a long.- See Also:
-
EXP_BIAS
private static final int EXP_BIASBias offset for the exponent of a double.- See Also:
-
EXP_SHIFT
private static final int EXP_SHIFTShift for the exponent of a double.- See Also:
-
HALF
private static final double HALF0.5.- See Also:
-
-
Constructor Details
-
IntMath
private IntMath()No instances.
-
-
Method Details
-
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.This method computes the equivalent of:
Math.multiplyHigh(x, x) Math.unsignedMultiplyHigh(x, x) - (((x >> 63) & x) << 1)Note: The method
Math.multiplyHighwas 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 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.multiplyHighwas 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.unsignedMultiplyHighwas 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 valuevalue2- 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 adoubleresult.- Parameters:
x- Value.y- Value.- Returns:
- the double
-
uint128ToDouble
static double uint128ToDouble(long hi, long lo) Convert an unsigned 128-bit integer to adouble.- 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 thedoubleargumentxas anint, with ties rounding towards positive infinity.This will raise an
ArithmeticExceptionif the closest integer result is not within the range[-2^31, 2^31), i.e. it overflows anint; or the argumentxis 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,longandBigIntegertypes.- Parameters:
x- Value.- Returns:
- rounded value
- Throws:
ArithmeticException- if theresultoverflows anint, orxis not finite- See Also:
-
toLongExact
static long toLongExact(double x) Return the whole number that is nearest to thedoubleargumentxas anlong, with ties rounding towards positive infinity.This will raise an
ArithmeticExceptionif the closest integer result is not within the range[-2^63, 2^63), i.e. it overflows along; or the argumentxis not finite.- Parameters:
x- Value.- Returns:
- rounded value
- Throws:
ArithmeticException- if theresultoverflows along, orxis not finite
-
toBigIntegerExact
Return the whole number that is nearest to thedoubleargumentxas anint, with ties rounding towards positive infinity.This will raise an
ArithmeticExceptionif the argumentxis not finite.- Parameters:
x- Value.- Returns:
- rounded value
- Throws:
ArithmeticException- ifxis 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 alongprimitive 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.rintand 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
-