diff --git a/core/src/openjdk/vm/java/lang/NativeStrictMath.java b/core/src/openjdk/vm/java/lang/NativeStrictMath.java index 9771e7b4e9..ffdf73c79c 100644 --- a/core/src/openjdk/vm/java/lang/NativeStrictMath.java +++ b/core/src/openjdk/vm/java/lang/NativeStrictMath.java @@ -51,8 +51,6 @@ package java.lang; -import gnu.classpath.Configuration; - /** * Helper class containing useful mathematical functions and constants. * This class mirrors {@link Math}, but is 100% portable, because it uses @@ -417,8 +415,8 @@ private static int remPiOver2(double x, double[] y) { x = abs(x); double z; int n; - if (Configuration.DEBUG && (x <= PI / 4 || x != x - || x == Double.POSITIVE_INFINITY)) + if (x <= PI / 4 || x != x + || x == Double.POSITIVE_INFINITY) throw new InternalError("Assertion failure"); if (x < 3 * PI / 4) // If |x| is small. { @@ -659,7 +657,7 @@ else if (z >= 0.5) * @return x * 2**n */ private static double scale(double x, int n) { - if (Configuration.DEBUG && abs(n) >= 2048) + if (abs(n) >= 2048) throw new InternalError("Assertion failure"); if (x == 0 || x == Double.NEGATIVE_INFINITY || !(x < Double.POSITIVE_INFINITY) || n == 0) @@ -693,7 +691,7 @@ private static double scale(double x, int n) { * @return sin(x+y) */ private static double sin(double x, double y) { - if (Configuration.DEBUG && abs(x + y) > 0.7854) + if (abs(x + y) > 0.7854) throw new InternalError("Assertion failure"); if (abs(x) < 1 / TWO_27) return x; // If |x| ~< 2**-27, already know answer. @@ -714,7 +712,7 @@ private static double sin(double x, double y) { * @return cos(x+y) */ private static double cos(double x, double y) { - if (Configuration.DEBUG && abs(x + y) > 0.7854) + if (abs(x + y) > 0.7854) throw new InternalError("Assertion failure"); x = abs(x); if (x < 1 / TWO_27) @@ -739,8 +737,7 @@ private static double cos(double x, double y) { * @return tan(x+y) */ private static double tan(double x, double y, boolean invert) { - // PI/2 is irrational, so no double is a perfect multiple of it. - if (Configuration.DEBUG && (abs(x + y) > 0.7854 || (x == 0 && invert))) + if (abs(x + y) > 0.7854 || (x == 0 && invert)) throw new InternalError("Assertion failure"); boolean negative = x < 0; if (negative) { @@ -1865,8 +1862,27 @@ public static double tanh(double x) { * @see StrictMath#hypot(double, double) */ public static double hypot(double x, double y) { - //todo improve it - return x*x + y*y; + x = abs(x); + y = abs(y); + if (x == Double.POSITIVE_INFINITY || y == Double.POSITIVE_INFINITY) + return Double.POSITIVE_INFINITY; + if (x != x || y != y) + return Double.NaN; + if (x == 0) + return y; + if (x < y) { + double t = x; + x = y; + y = t; + } + double mx = x; + int exp = (int) (Double.doubleToLongBits(mx) >> 52) - 1023; + if (exp < -20) { + x *= TWO_54; + y *= TWO_54; + } + double r = y / x; + return mx * sqrt(1 + r * r); } /** @@ -2079,28 +2095,11 @@ public static double expm1(double x) { h_bits += (k << 20); // add k to y's exponent y = buildDouble(l_bits, h_bits); - } else { - bits = Double.doubleToLongBits(t); - h_bits = (0x000003ffL - k) << 20; - l_bits = getLowDWord(bits); - - t = buildDouble(l_bits, h_bits); // t = 2^(-k) - - y = x - (e + t); - y += 1.0; - - bits = Double.doubleToLongBits(y); - h_bits = getHighDWord(bits); - l_bits = getLowDWord(bits); - - h_bits += (k << 20); // add k to y's exponent - - y = buildDouble(l_bits, h_bits); - } - } + } + } return y; - } + } /** * @see StrictMath#log1p(double)