6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN
authorbpb
Thu, 16 Jan 2014 15:56:40 -0800
changeset 22290 6d4551cf4b3e
parent 22289 bb9c71b84919
child 22292 9224952a7bbe
6667086: Double.doubleToLongBits(final double value) contains inefficient test for NaN Summary: Use isNaN() to test the parameter. Reviewed-by: darcy, psandoz
jdk/src/share/classes/java/lang/Double.java
jdk/src/share/classes/java/lang/Float.java
--- a/jdk/src/share/classes/java/lang/Double.java	Thu Jan 16 18:20:31 2014 +0100
+++ b/jdk/src/share/classes/java/lang/Double.java	Thu Jan 16 15:56:40 2014 -0800
@@ -833,14 +833,10 @@
      * @return the bits that represent the floating-point number.
      */
     public static long doubleToLongBits(double value) {
-        long result = doubleToRawLongBits(value);
-        // Check for NaN based on values of bit fields, maximum
-        // exponent and nonzero significand.
-        if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
-              DoubleConsts.EXP_BIT_MASK) &&
-             (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
-            result = 0x7ff8000000000000L;
-        return result;
+        if (!isNaN(value)) {
+            return doubleToRawLongBits(value);
+        }
+        return 0x7ff8000000000000L;
     }
 
     /**
--- a/jdk/src/share/classes/java/lang/Float.java	Thu Jan 16 18:20:31 2014 +0100
+++ b/jdk/src/share/classes/java/lang/Float.java	Thu Jan 16 15:56:40 2014 -0800
@@ -741,14 +741,10 @@
      * @return the bits that represent the floating-point number.
      */
     public static int floatToIntBits(float value) {
-        int result = floatToRawIntBits(value);
-        // Check for NaN based on values of bit fields, maximum
-        // exponent and nonzero significand.
-        if ( ((result & FloatConsts.EXP_BIT_MASK) ==
-              FloatConsts.EXP_BIT_MASK) &&
-             (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
-            result = 0x7fc00000;
-        return result;
+        if (!isNaN(value)) {
+            return floatToRawIntBits(value);
+        }
+        return 0x7fc00000;
     }
 
     /**