harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r771716 - in /harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java: lang/Double.java lang/Float.java util/Arrays.java
Date Tue, 05 May 2009 13:26:44 GMT
Author: tellison
Date: Tue May  5 13:26:43 2009
New Revision: 771716

URL: http://svn.apache.org/viewvc?rev=771716&view=rev
Log:
Improvement to Float/Double comparison performance.

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Float.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java?rev=771716&r1=771715&r2=771716&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Double.java Tue May
 5 13:26:43 2009
@@ -126,25 +126,7 @@
      * @since 1.2
      */
     public int compareTo(Double object) {
-        long d1, d2;
-        long NaNbits = Double.doubleToLongBits(Double.NaN);
-        if ((d1 = Double.doubleToLongBits(value)) == NaNbits) {
-            if (Double.doubleToLongBits(object.value) == NaNbits) {
-                return 0;
-            }
-            return 1;
-        }
-        if ((d2 = Double.doubleToLongBits(object.value)) == NaNbits) {
-            return -1;
-        }
-        if (value == object.value) {
-            if (d1 == d2) {
-                return 0;
-            }
-            // check for -0
-            return d1 > d2 ? 1 : -1;
-        }
-        return value > object.value ? 1 : -1;
+        return compare(value, object.value);
     }
 
     @Override
@@ -359,25 +341,35 @@
      *         value if {@code double1} is greater than {@code double2}.
      */
     public static int compare(double double1, double double2) {
-        long d1, d2;
-        long NaNbits = Double.doubleToLongBits(Double.NaN);
-        if ((d1 = Double.doubleToLongBits(double1)) == NaNbits) {
-            if (Double.doubleToLongBits(double2) == NaNbits) {
-                return 0;
-            }
+        // Non-zero, non-NaN checking.
+        if (double1 > double2) {
             return 1;
         }
-        if ((d2 = Double.doubleToLongBits(double2)) == NaNbits) {
+        if (double2 > double1) {
             return -1;
         }
-        if (double1 == double2) {
-            if (d1 == d2) {
+        if (double1 == double2 && 0.0d != double1) {
+            return 0;
+        }
+
+        // NaNs are equal to other NaNs and larger than any other float
+        if (isNaN(double1)) {
+            if (isNaN(double2)) {
                 return 0;
             }
-            // check for -0
-            return d1 > d2 ? 1 : -1;
+            return 1;
+        } else if (isNaN(double2)) {
+            return -1;
+        }
+
+        // Deal with +0.0 and -0.0
+        long d1 = doubleToRawLongBits(double1);
+        long d2 = doubleToRawLongBits(double2);
+
+        if (d1 == d2) {
+            return 0;
         }
-        return double1 > double2 ? 1 : -1;
+        return (d1 < d2) ? -1 : 1;
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Float.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Float.java?rev=771716&r1=771715&r2=771716&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Float.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Float.java Tue May
 5 13:26:43 2009
@@ -130,25 +130,7 @@
      * @since 1.2
      */
     public int compareTo(Float object) {
-        int f1, f2;
-        int NaNbits = Float.floatToIntBits(Float.NaN);
-        if ((f1 = Float.floatToIntBits(value)) == NaNbits) {
-            if (Float.floatToIntBits(object.value) == NaNbits) {
-                return 0;
-            }
-            return 1;
-        }
-        if ((f2 = Float.floatToIntBits(object.value)) == NaNbits) {
-            return -1;
-        }
-        if (value == object.value) {
-            if (f1 == f2) {
-                return 0;
-            }
-            // check for -0
-            return f1 > f2 ? 1 : -1;
-        }
-        return value > object.value ? 1 : -1;
+        return compare(value, object.value); 
     }
 
     @Override
@@ -363,25 +345,35 @@
      * @since 1.4
      */
     public static int compare(float float1, float float2) {
-        int f1, f2;
-        int NaNbits = Float.floatToIntBits(Float.NaN);
-        if ((f1 = Float.floatToIntBits(float1)) == NaNbits) {
-            if (Float.floatToIntBits(float2) == NaNbits) {
-                return 0;
-            }
+        // Non-zero, non-NaN checking.
+        if (float1 > float2) {
             return 1;
         }
-        if ((f2 = Float.floatToIntBits(float2)) == NaNbits) {
+        if (float2 > float1) {
             return -1;
         }
-        if (float1 == float2) {
-            if (f1 == f2) {
+        if (float1 == float2 && 0.0f != float1) {
+            return 0;
+        }
+
+        // NaNs are equal to other NaNs and larger than any other float
+        if (isNaN(float1)) {
+            if (isNaN(float2)) {
                 return 0;
             }
-            // check for -0
-            return f1 > f2 ? 1 : -1;
+            return 1;
+        } else if (isNaN(float2)) {
+            return -1;
+        }
+
+        // Deal with +0.0 and -0.0
+        int f1 = floatToRawIntBits(float1);
+        int f2 = floatToRawIntBits(float2);
+
+        if (f1 == f2) {
+            return 0;
         }
-        return float1 > float2 ? 1 : -1;
+        return (f1 < f2) ? -1 : 1;
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java?rev=771716&r1=771715&r2=771716&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java Tue May
 5 13:26:43 2009
@@ -1530,41 +1530,11 @@
     }
 
     private static boolean lessThan(double double1, double double2) {
-        long d1, d2;
-        long NaNbits = Double.doubleToLongBits(Double.NaN);
-        if ((d1 = Double.doubleToLongBits(double1)) == NaNbits) {
-            return false;
-        }
-        if ((d2 = Double.doubleToLongBits(double2)) == NaNbits) {
-            return true;
-        }
-        if (double1 == double2) {
-            if (d1 == d2) {
-                return false;
-            }
-            // check for -0
-            return d1 < d2;
-        }
-        return double1 < double2;
+        return Double.compare(double1, double2) < 0;
     }
 
     private static boolean lessThan(float float1, float float2) {
-        int f1, f2;
-        int NaNbits = Float.floatToIntBits(Float.NaN);
-        if ((f1 = Float.floatToIntBits(float1)) == NaNbits) {
-            return false;
-        }
-        if ((f2 = Float.floatToIntBits(float2)) == NaNbits) {
-            return true;
-        }
-        if (float1 == float2) {
-            if (f1 == f2) {
-                return false;
-            }
-            // check for -0
-            return f1 < f2;
-        }
-        return float1 < float2;
+        return Float.compare(float1, float2) < 0;
     }
 
     private static int med3(byte[] array, int a, int b, int c) {



Mime
View raw message