From 7108dfbac7eceda910644d4f5fcef590a7ab2bc0 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 19 Nov 2023 20:12:44 +0100 Subject: [PATCH] classlib: fix Math exact operations in C and WebAssembly backends --- .../java/org/teavm/classlib/java/lang/TMath.java | 14 ++++++-------- .../org/teavm/classlib/java/lang/MathTest.java | 7 ++++++- .../java/time/TestDateTimesImplementation.java | 3 --- 3 files changed, 12 insertions(+), 12 deletions(-) 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 b97ad798b..587237586 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 @@ -254,7 +254,7 @@ public final class TMath extends TObject { return 0; } int total = a * b; - if (total / b != a || (a == Integer.MIN_VALUE && b == -1) || (b == Integer.MIN_VALUE && a == -1)) { + if ((a == Integer.MIN_VALUE && b == -1) || (b == Integer.MIN_VALUE && a == -1) || total / b != a) { throw new ArithmeticException(); } return total; @@ -273,26 +273,24 @@ public final class TMath extends TObject { return 0; } long total = a * b; - if (total / b != a || (a == Long.MIN_VALUE && b == -1) || (b == Long.MIN_VALUE && a == -1)) { + if ((a == Long.MIN_VALUE && b == -1) || (b == Long.MIN_VALUE && a == -1) || total / b != a) { throw new ArithmeticException(); } return total; } public static int divideExact(int a, int b) { - int q = a / b; - if ((a & b & q) < 0) { // all 3 are negative + if (a == Integer.MIN_VALUE && b == -1) { throw new ArithmeticException(); } - return q; + return a / b; } public static long divideExact(long a, long b) { - long q = a / b; - if ((a & b & q) < 0) { // all 3 are negative + if (a == Long.MIN_VALUE && b == -1) { throw new ArithmeticException(); } - return q; + return a / b; } @Unmanaged 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 1ea9542d2..6a882a9f9 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 @@ -202,7 +202,6 @@ public class MathTest { } @Test - @SkipPlatform({TestPlatform.WEBASSEMBLY, TestPlatform.WASI, TestPlatform.C}) public void exacts() { try { Math.incrementExact(Integer.MAX_VALUE); @@ -258,6 +257,12 @@ public class MathTest { } catch (ArithmeticException e) { // ok } + try { + Math.divideExact(Integer.MIN_VALUE, -1); + fail(); + } catch (ArithmeticException e) { + // ok + } IntStream.rangeClosed(-10, 10).forEach(x -> { assertEquals(x + 1, Math.incrementExact(x)); assertEquals(x - 1, Math.decrementExact(x)); diff --git a/tests/src/test/java/org/teavm/classlib/java/time/TestDateTimesImplementation.java b/tests/src/test/java/org/teavm/classlib/java/time/TestDateTimesImplementation.java index a821ebd27..0a7511159 100644 --- a/tests/src/test/java/org/teavm/classlib/java/time/TestDateTimesImplementation.java +++ b/tests/src/test/java/org/teavm/classlib/java/time/TestDateTimesImplementation.java @@ -48,9 +48,7 @@ package org.teavm.classlib.java.time; import static org.testng.Assert.assertEquals; import org.junit.runner.RunWith; -import org.teavm.junit.SkipPlatform; import org.teavm.junit.TeaVMTestRunner; -import org.teavm.junit.TestPlatform; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -309,7 +307,6 @@ public class TestDateTimesImplementation { } @Test(dataProvider = "safeMultiplyLongLongProviderOverflow", expectedExceptions = ArithmeticException.class) - @SkipPlatform({TestPlatform.C, TestPlatform.WEBASSEMBLY, TestPlatform.WASI}) public void test_safeMultiplyLongLong_overflow(long a, long b) { Math.multiplyExact(a, b); }