summaryrefslogtreecommitdiff
path: root/java/lang
diff options
context:
space:
mode:
authorAndrew John Hughes <gnu_andrew@member.fsf.org>2007-10-12 08:50:49 +0000
committerAndrew John Hughes <gnu_andrew@member.fsf.org>2007-10-12 08:50:49 +0000
commit81c05057ab6650e79de9d702716bdb7bca738e14 (patch)
tree0638b5db3a17a654c79c13ba25a72df90614a5b3 /java/lang
parentd19f4ec8006cb90e0fbfdf3f8c2f26bd28309b6a (diff)
downloadclasspath-81c05057ab6650e79de9d702716bdb7bca738e14.tar.gz
2007-10-12 Ian Rogers <ian.rogers@manchester.ac.uk>
2007-10-12 Andrew Haley <aph@redhat.com> PR classpath/33741: * java/lang/Double.java: (compare(double,double)): Increase performance of this method. * java/lang/Float.java: (compare(float,float)): Likewise.
Diffstat (limited to 'java/lang')
-rw-r--r--java/lang/Double.java31
-rw-r--r--java/lang/Float.java31
2 files changed, 40 insertions, 22 deletions
diff --git a/java/lang/Double.java b/java/lang/Double.java
index c71620314..bf965dffb 100644
--- a/java/lang/Double.java
+++ b/java/lang/Double.java
@@ -587,16 +587,25 @@ public final class Double extends Number implements Comparable<Double>
*/
public static int compare(double x, double y)
{
- if (isNaN(x))
- return isNaN(y) ? 0 : 1;
- if (isNaN(y))
- return -1;
- // recall that 0.0 == -0.0, so we convert to infinites and try again
- if (x == 0 && y == 0)
- return (int) (1 / x - 1 / y);
- if (x == y)
- return 0;
-
- return x > y ? 1 : -1;
+ // handle the easy cases:
+ if (x < y)
+ return -1;
+ if (x > y)
+ return 1;
+
+ // handle equality respecting that 0.0 != -0.0 (hence not using x == y):
+ long lx = doubleToRawLongBits(x);
+ long ly = doubleToRawLongBits(y);
+ if (lx == ly)
+ return 0;
+
+ // handle NaNs:
+ if (x != x)
+ return (y != y) ? 0 : 1;
+ else if (y != y)
+ return -1;
+
+ // handle +/- 0.0
+ return (lx < ly) ? -1 : 1;
}
}
diff --git a/java/lang/Float.java b/java/lang/Float.java
index dc39ec2af..d11045cc0 100644
--- a/java/lang/Float.java
+++ b/java/lang/Float.java
@@ -594,16 +594,25 @@ public final class Float extends Number implements Comparable<Float>
*/
public static int compare(float x, float y)
{
- if (isNaN(x))
- return isNaN(y) ? 0 : 1;
- if (isNaN(y))
- return -1;
- // recall that 0.0 == -0.0, so we convert to infinities and try again
- if (x == 0 && y == 0)
- return (int) (1 / x - 1 / y);
- if (x == y)
- return 0;
-
- return x > y ? 1 : -1;
+ // handle the easy cases:
+ if (x < y)
+ return -1;
+ if (x > y)
+ return 1;
+
+ // handle equality respecting that 0.0 != -0.0 (hence not using x == y):
+ int ix = floatToRawIntBits(x);
+ int iy = floatToRawIntBits(y);
+ if (ix == iy)
+ return 0;
+
+ // handle NaNs:
+ if (x != x)
+ return (y != y) ? 0 : 1;
+ else if (y != y)
+ return -1;
+
+ // handle +/- 0.0
+ return (ix < iy) ? -1 : 1;
}
}