src/share/classes/java/lang/Double.java

Print this page
rev 9077 : 6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
Summary: Use isNaN() to test the parameter.
Reviewed-by: darcy, psandoz


 816      *
 817      * <p>If the argument is positive infinity, the result is
 818      * {@code 0x7ff0000000000000L}.
 819      *
 820      * <p>If the argument is negative infinity, the result is
 821      * {@code 0xfff0000000000000L}.
 822      *
 823      * <p>If the argument is NaN, the result is
 824      * {@code 0x7ff8000000000000L}.
 825      *
 826      * <p>In all cases, the result is a {@code long} integer that, when
 827      * given to the {@link #longBitsToDouble(long)} method, will produce a
 828      * floating-point value the same as the argument to
 829      * {@code doubleToLongBits} (except all NaN values are
 830      * collapsed to a single "canonical" NaN value).
 831      *
 832      * @param   value   a {@code double} precision floating-point number.
 833      * @return the bits that represent the floating-point number.
 834      */
 835     public static long doubleToLongBits(double value) {
 836         long result = doubleToRawLongBits(value);
 837         // Check for NaN based on values of bit fields, maximum
 838         // exponent and nonzero significand.
 839         if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
 840               DoubleConsts.EXP_BIT_MASK) &&
 841              (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
 842             result = 0x7ff8000000000000L;
 843         return result;
 844     }
 845 
 846     /**
 847      * Returns a representation of the specified floating-point value
 848      * according to the IEEE 754 floating-point "double
 849      * format" bit layout, preserving Not-a-Number (NaN) values.
 850      *
 851      * <p>Bit 63 (the bit that is selected by the mask
 852      * {@code 0x8000000000000000L}) represents the sign of the
 853      * floating-point number. Bits
 854      * 62-52 (the bits that are selected by the mask
 855      * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
 856      * (the bits that are selected by the mask
 857      * {@code 0x000fffffffffffffL}) represent the significand
 858      * (sometimes called the mantissa) of the floating-point number.
 859      *
 860      * <p>If the argument is positive infinity, the result is
 861      * {@code 0x7ff0000000000000L}.
 862      *
 863      * <p>If the argument is negative infinity, the result is




 816      *
 817      * <p>If the argument is positive infinity, the result is
 818      * {@code 0x7ff0000000000000L}.
 819      *
 820      * <p>If the argument is negative infinity, the result is
 821      * {@code 0xfff0000000000000L}.
 822      *
 823      * <p>If the argument is NaN, the result is
 824      * {@code 0x7ff8000000000000L}.
 825      *
 826      * <p>In all cases, the result is a {@code long} integer that, when
 827      * given to the {@link #longBitsToDouble(long)} method, will produce a
 828      * floating-point value the same as the argument to
 829      * {@code doubleToLongBits} (except all NaN values are
 830      * collapsed to a single "canonical" NaN value).
 831      *
 832      * @param   value   a {@code double} precision floating-point number.
 833      * @return the bits that represent the floating-point number.
 834      */
 835     public static long doubleToLongBits(double value) {
 836         if (!isNaN(value)) {
 837             return doubleToRawLongBits(value);
 838         }
 839         return 0x7ff8000000000000L;




 840     }
 841 
 842     /**
 843      * Returns a representation of the specified floating-point value
 844      * according to the IEEE 754 floating-point "double
 845      * format" bit layout, preserving Not-a-Number (NaN) values.
 846      *
 847      * <p>Bit 63 (the bit that is selected by the mask
 848      * {@code 0x8000000000000000L}) represents the sign of the
 849      * floating-point number. Bits
 850      * 62-52 (the bits that are selected by the mask
 851      * {@code 0x7ff0000000000000L}) represent the exponent. Bits 51-0
 852      * (the bits that are selected by the mask
 853      * {@code 0x000fffffffffffffL}) represent the significand
 854      * (sometimes called the mantissa) of the floating-point number.
 855      *
 856      * <p>If the argument is positive infinity, the result is
 857      * {@code 0x7ff0000000000000L}.
 858      *
 859      * <p>If the argument is negative infinity, the result is