From 617ce6787142615365c944f069fa8d7cdc3b3024 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 19 Sep 2023 11:32:35 +0200 Subject: [PATCH] classlib: follow-up for math fixes --- .../java/lang/MathNativeGenerator.java | 3 + .../org/teavm/classlib/java/lang/TMath.java | 75 ++++++++----------- .../teavm/classlib/java/lang/MathTest.java | 8 ++ 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java index 50628e33d..b54f94127 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/MathNativeGenerator.java @@ -28,6 +28,9 @@ public class MathNativeGenerator implements Generator { if (name.endsWith("Impl")) { name = name.substring(0, name.length() - 4); } + if (name.equals("signum")) { + name = "sign"; + } function(context, writer, "Math." + name, methodRef.parameterCount()); } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java index fc30307fa..09111543f 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java @@ -21,7 +21,6 @@ import org.teavm.classlib.PlatformDetector; import org.teavm.interop.Import; import org.teavm.interop.NoSideEffects; import org.teavm.interop.Unmanaged; -import org.teavm.jso.JSBody; @NoSideEffects public final class TMath extends TObject { @@ -215,26 +214,36 @@ public final class TMath extends TObject { return n >= 0 ? n : -n; } - @JSBody(params = "d", script = "return Math.abs(d);") + @GeneratedBy(MathNativeGenerator.class) @NoSideEffects - private static native float jsAbs(float d); + private static native float absImpl(float d); + + @Import(name = "fabs") + private static native float absC(float d); public static float abs(float n) { if (PlatformDetector.isJavaScript()) { - return jsAbs(n); + return absImpl(n); + } else if (PlatformDetector.isC()) { + return absC(n); } - return n <= 0.0f ? 0.0f - n : n; + return n <= 0f ? 0f - n : n; } - @JSBody(params = "d", script = "return Math.abs(d);") + @GeneratedBy(MathNativeGenerator.class) @NoSideEffects - private static native double jsAbs(double d); + private static native double absImpl(double d); + + @Import(name = "fabs") + private static native double absC(double d); public static double abs(double n) { if (PlatformDetector.isJavaScript()) { - return jsAbs(n); + return absImpl(n); + } else if (PlatformDetector.isC()) { + return absC(n); } - return n <= 0.0d ? 0.0d - n : n; + return n <= 0.0 ? 0.0 - n : n; } public static double ulp(double d) { @@ -279,7 +288,7 @@ public final class TMath extends TObject { return TFloat.intBitsToFloat(bits); } - @JSBody(params = "d", script = "return Math.sign(d);") + @GeneratedBy(MathNativeGenerator.class) @NoSideEffects private static native double sign(double d); @@ -287,13 +296,13 @@ public final class TMath extends TObject { if (PlatformDetector.isJavaScript()) { return sign(d); } - if (d == 0.0 || Double.isNaN(d)) { + if (Double.isNaN(d)) { return d; } - return d < 0.0 ? -1.0 : 1.0; + return d < 0 ? -1 : d > 0 ? 1 : d; } - @JSBody(params = "d", script = "return Math.sign(d);") + @GeneratedBy(MathNativeGenerator.class) @NoSideEffects private static native float sign(float d); @@ -301,10 +310,10 @@ public final class TMath extends TObject { if (PlatformDetector.isJavaScript()) { return sign(d); } - if (d == 0.0f || Float.isNaN(d)) { + if (Double.isNaN(d)) { return d; } - return d < 0.0f ? -1.0f : 1.0f; + return d < 0 ? -1 : d > 0 ? 1 : d; } public static double sinh(double x) { @@ -375,17 +384,11 @@ public final class TMath extends TObject { } public static double nextUp(double d) { - if (TDouble.isNaN(d)) { - return d; - } - if (d == 0.0d) { - return Double.MIN_VALUE; - } - if (d == TDouble.POSITIVE_INFINITY) { + if (TDouble.isNaN(d) || d == TDouble.POSITIVE_INFINITY) { return d; } long bits = TDouble.doubleToLongBits(d); - if (d < 0.0d) { + if (d < 0) { bits--; } else { bits++; @@ -394,17 +397,11 @@ public final class TMath extends TObject { } public static float nextUp(float d) { - if (TFloat.isNaN(d)) { - return d; - } - if (d == 0.0f) { - return Float.MIN_VALUE; - } - if (d == TFloat.POSITIVE_INFINITY) { + if (TFloat.isNaN(d) || d == TFloat.POSITIVE_INFINITY) { return d; } int bits = TFloat.floatToIntBits(d); - if (d < 0.0f) { + if (d < 0) { bits--; } else { bits++; @@ -413,17 +410,14 @@ public final class TMath extends TObject { } public static double nextDown(double d) { - if (TDouble.isNaN(d)) { + if (TDouble.isNaN(d) || d == TDouble.NEGATIVE_INFINITY) { return d; } if (d == 0.0d) { return -Double.MIN_VALUE; } - if (d == TDouble.NEGATIVE_INFINITY) { - return d; - } long bits = TDouble.doubleToLongBits(d); - if (d < 0.0d) { + if (d <= 0) { bits++; } else { bits--; @@ -432,17 +426,14 @@ public final class TMath extends TObject { } public static float nextDown(float d) { - if (TFloat.isNaN(d)) { + if (TFloat.isNaN(d) || d == TFloat.NEGATIVE_INFINITY) { return d; } - if (d == 0.0f) { + if (d == 0) { return -Float.MIN_VALUE; } - if (d == TFloat.NEGATIVE_INFINITY) { - return d; - } int bits = TFloat.floatToIntBits(d); - if (d < 0.0f) { + if (d <= 0) { bits++; } else { bits--; diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java index 38d9d2c28..e2dcebdf4 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/MathTest.java @@ -76,6 +76,11 @@ public class MathTest { @Test public void signumWorks() { + assertEquals(Double.valueOf(1.0), Double.valueOf(Math.signum(3.0))); + assertEquals(Double.valueOf(-1.0), Double.valueOf(Math.signum(-4.0))); + assertEquals(Float.valueOf(1f), Float.valueOf(Math.signum(3f))); + assertEquals(Float.valueOf(-1f), Float.valueOf(Math.signum(-4f))); + assertEquals(Double.valueOf(0.0), Double.valueOf(Math.signum(0.0))); assertEquals(Double.valueOf(-0.0), Double.valueOf(Math.signum(-0.0))); assertTrue(Double.isNaN(Math.signum(Double.NaN))); @@ -84,6 +89,9 @@ public class MathTest { assertTrue(Float.isNaN(Math.signum(Float.NaN))); assertEquals(Double.valueOf(-1.0), Double.valueOf(Math.signum(-Double.MIN_VALUE))); assertEquals(Double.valueOf(1.0), Double.valueOf(Math.signum(Double.MIN_VALUE))); + + assertEquals(Float.valueOf(-1), Float.valueOf(Math.signum(Float.NEGATIVE_INFINITY))); + assertEquals(Float.valueOf(1), Float.valueOf(Math.signum(Float.POSITIVE_INFINITY))); } @Test