src/share/classes/java/lang/Float.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


 724      * the mantissa) of the floating-point number.
 725      *
 726      * <p>If the argument is positive infinity, the result is
 727      * {@code 0x7f800000}.
 728      *
 729      * <p>If the argument is negative infinity, the result is
 730      * {@code 0xff800000}.
 731      *
 732      * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
 733      *
 734      * <p>In all cases, the result is an integer that, when given to the
 735      * {@link #intBitsToFloat(int)} method, will produce a floating-point
 736      * value the same as the argument to {@code floatToIntBits}
 737      * (except all NaN values are collapsed to a single
 738      * "canonical" NaN value).
 739      *
 740      * @param   value   a floating-point number.
 741      * @return the bits that represent the floating-point number.
 742      */
 743     public static int floatToIntBits(float value) {
 744         int result = floatToRawIntBits(value);
 745         // Check for NaN based on values of bit fields, maximum
 746         // exponent and nonzero significand.
 747         if ( ((result & FloatConsts.EXP_BIT_MASK) ==
 748               FloatConsts.EXP_BIT_MASK) &&
 749              (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
 750             result = 0x7fc00000;
 751         return result;
 752     }
 753 
 754     /**
 755      * Returns a representation of the specified floating-point value
 756      * according to the IEEE 754 floating-point "single format" bit
 757      * layout, preserving Not-a-Number (NaN) values.
 758      *
 759      * <p>Bit 31 (the bit that is selected by the mask
 760      * {@code 0x80000000}) represents the sign of the floating-point
 761      * number.
 762      * Bits 30-23 (the bits that are selected by the mask
 763      * {@code 0x7f800000}) represent the exponent.
 764      * Bits 22-0 (the bits that are selected by the mask
 765      * {@code 0x007fffff}) represent the significand (sometimes called
 766      * the mantissa) of the floating-point number.
 767      *
 768      * <p>If the argument is positive infinity, the result is
 769      * {@code 0x7f800000}.
 770      *
 771      * <p>If the argument is negative infinity, the result is




 724      * the mantissa) of the floating-point number.
 725      *
 726      * <p>If the argument is positive infinity, the result is
 727      * {@code 0x7f800000}.
 728      *
 729      * <p>If the argument is negative infinity, the result is
 730      * {@code 0xff800000}.
 731      *
 732      * <p>If the argument is NaN, the result is {@code 0x7fc00000}.
 733      *
 734      * <p>In all cases, the result is an integer that, when given to the
 735      * {@link #intBitsToFloat(int)} method, will produce a floating-point
 736      * value the same as the argument to {@code floatToIntBits}
 737      * (except all NaN values are collapsed to a single
 738      * "canonical" NaN value).
 739      *
 740      * @param   value   a floating-point number.
 741      * @return the bits that represent the floating-point number.
 742      */
 743     public static int floatToIntBits(float value) {
 744         if (!isNaN(value)) {
 745             return floatToRawIntBits(value);
 746         }
 747         return 0x7fc00000;




 748     }
 749 
 750     /**
 751      * Returns a representation of the specified floating-point value
 752      * according to the IEEE 754 floating-point "single format" bit
 753      * layout, preserving Not-a-Number (NaN) values.
 754      *
 755      * <p>Bit 31 (the bit that is selected by the mask
 756      * {@code 0x80000000}) represents the sign of the floating-point
 757      * number.
 758      * Bits 30-23 (the bits that are selected by the mask
 759      * {@code 0x7f800000}) represent the exponent.
 760      * Bits 22-0 (the bits that are selected by the mask
 761      * {@code 0x007fffff}) represent the significand (sometimes called
 762      * the mantissa) of the floating-point number.
 763      *
 764      * <p>If the argument is positive infinity, the result is
 765      * {@code 0x7f800000}.
 766      *
 767      * <p>If the argument is negative infinity, the result is