Need reviewer: JDK 8 CR for Support Integer overflow

Florian Weimer fweimer at bfk.de
Fri Feb 3 08:11:27 UTC 2012


* Roger Riggs:

> to support addExact(), subtractExact(), negateExact(), multiplyExact(),
> and toIntExact() for int and long primitive types.

Would it make sense to replace (x ^ r) < 0 && (x ^ y) >= 0
in

+    public static int addExact(int x, int y) {
+        int r = x + y;
+        if ((x ^ r) < 0 && (x ^ y) >= 0) {
+            throw new ArithmeticException("Addition overflows an int");
+        }
+        return r;
+    }

with (x ^ y ^ r) < 0?

For substraction, you could use ((x ^ r) & (x ^ y)) < 0.

Will Hotspot be able to optimize away the string construction on the
exception path in multiplyExact() if the exception is caught locally?

-- 
Florian Weimer                <fweimer at bfk.de>
BFK edv-consulting GmbH       http://www.bfk.de/
Kriegsstraße 100              tel: +49-721-96201-1
D-76133 Karlsruhe             fax: +49-721-96201-99



More information about the core-libs-dev mailing list