Use InternalPreconditions.checkCriticalArithmetic in java.lang.Math
Change-Id: I08be0750a68cc1a195e835e015d5152a8abbbad9
diff --git a/user/super/com/google/gwt/emul/java/lang/Math.java b/user/super/com/google/gwt/emul/java/lang/Math.java
index 922ef5d..26bf8f2 100644
--- a/user/super/com/google/gwt/emul/java/lang/Math.java
+++ b/user/super/com/google/gwt/emul/java/lang/Math.java
@@ -15,6 +15,8 @@
*/
package java.lang;
+import static javaemul.internal.InternalPreconditions.checkCriticalArithmetic;
+
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
@@ -70,14 +72,14 @@
public static int addExact(int x, int y) {
double r = (double) x + (double) y;
- throwOverflowIf(!isSafeIntegerRange(r));
+ checkCriticalArithmetic(isSafeIntegerRange(r));
return (int) r;
}
public static long addExact(long x, long y) {
long r = x + y;
// "Hacker's Delight" 2-12 Overflow if both arguments have the opposite sign of the result
- throwOverflowIf(((x ^ r) & (y ^ r)) < 0);
+ checkCriticalArithmetic(((x ^ r) & (y ^ r)) >= 0);
return r;
}
@@ -118,12 +120,12 @@
}
public static int decrementExact(int x) {
- throwOverflowIf(x == Integer.MIN_VALUE);
+ checkCriticalArithmetic(x != Integer.MIN_VALUE);
return x - 1;
}
public static long decrementExact(long x) {
- throwOverflowIf(x == Long.MIN_VALUE);
+ checkCriticalArithmetic(x != Long.MIN_VALUE);
return x - 1;
}
@@ -140,24 +142,24 @@
}
public static int floorDiv(int dividend, int divisor) {
- throwDivByZeroIf(divisor == 0);
+ checkCriticalArithmetic(divisor != 0);
// round down division if the signs are different and modulo not zero
return ((dividend ^ divisor) >= 0 ? dividend / divisor : ((dividend + 1) / divisor) - 1);
}
public static long floorDiv(long dividend, long divisor) {
- throwDivByZeroIf(divisor == 0);
+ checkCriticalArithmetic(divisor != 0);
// round down division if the signs are different and modulo not zero
return ((dividend ^ divisor) >= 0 ? dividend / divisor : ((dividend + 1) / divisor) - 1);
}
public static int floorMod(int dividend, int divisor) {
- throwDivByZeroIf(divisor == 0);
+ checkCriticalArithmetic(divisor != 0);
return ((dividend % divisor) + divisor) % divisor;
}
public static long floorMod(long dividend, long divisor) {
- throwDivByZeroIf(divisor == 0);
+ checkCriticalArithmetic(divisor != 0);
return ((dividend % divisor) + divisor) % divisor;
}
@@ -167,12 +169,12 @@
}
public static int incrementExact(int x) {
- throwOverflowIf(x == Integer.MAX_VALUE);
+ checkCriticalArithmetic(x != Integer.MAX_VALUE);
return x + 1;
}
public static long incrementExact(long x) {
- throwOverflowIf(x == Long.MAX_VALUE);
+ checkCriticalArithmetic(x != Long.MAX_VALUE);
return x + 1;
}
@@ -222,7 +224,7 @@
public static int multiplyExact(int x, int y) {
double r = (double) x * (double) y;
- throwOverflowIf(!isSafeIntegerRange(r));
+ checkCriticalArithmetic(isSafeIntegerRange(r));
return (int) r;
}
@@ -234,17 +236,17 @@
return 0;
}
long r = x * y;
- throwOverflowIf(r / y != x);
+ checkCriticalArithmetic(r / y == x);
return r;
}
public static int negateExact(int x) {
- throwOverflowIf(x == Integer.MIN_VALUE);
+ checkCriticalArithmetic(x != Integer.MIN_VALUE);
return -x;
}
public static long negateExact(long x) {
- throwOverflowIf(x == Long.MIN_VALUE);
+ checkCriticalArithmetic(x != Long.MIN_VALUE);
return -x;
}
@@ -281,7 +283,7 @@
public static int subtractExact(int x, int y) {
double r = (double) x - (double) y;
- throwOverflowIf(!isSafeIntegerRange(r));
+ checkCriticalArithmetic(isSafeIntegerRange(r));
return (int) r;
}
@@ -289,7 +291,7 @@
long r = x - y;
// "Hacker's Delight" Overflow if the arguments have different signs and
// the sign of the result is different than the sign of x
- throwOverflowIf(((x ^ y) & (x ^ r)) < 0);
+ checkCriticalArithmetic(((x ^ y) & (x ^ r)) >= 0);
return r;
}
@@ -354,7 +356,7 @@
public static int toIntExact(long x) {
int ix = (int) x;
- throwOverflowIf(ix != x);
+ checkCriticalArithmetic(ix == x);
return ix;
}
@@ -366,18 +368,6 @@
return Integer.MIN_VALUE <= value && value <= Integer.MAX_VALUE;
}
- private static void throwDivByZeroIf(boolean condition) {
- if (condition) {
- throw new ArithmeticException("div by zero");
- }
- }
-
- private static void throwOverflowIf(boolean condition) {
- if (condition) {
- throw new ArithmeticException("overflow");
- }
- }
-
@JsType(isNative = true, name = "Math", namespace = JsPackage.GLOBAL)
private static class NativeMath {
public static double LOG10E;
diff --git a/user/test/com/google/gwt/emultest/java/lang/MathTest.java b/user/test/com/google/gwt/emultest/java/lang/MathTest.java
index 7557108..20897a4 100644
--- a/user/test/com/google/gwt/emultest/java/lang/MathTest.java
+++ b/user/test/com/google/gwt/emultest/java/lang/MathTest.java
@@ -501,7 +501,7 @@
public void testSignum() {
assertNaN(Math.signum(Double.NaN));
assertNegativeZero(Math.signum(-0.0));
- assertEquals(0.0, Math.signum(0.0));
+ assertPositiveZero(Math.signum(0.0));
assertEquals(-1, Math.signum(-2));
assertEquals(1, Math.signum(2));
assertEquals(-1.0, Math.signum(-Double.MAX_VALUE));