Need reviewer: JDK 8 CR for Support Integer overflow
Eamonn McManus
eamonn at mcmanus.net
Fri Feb 3 12:02:55 UTC 2012
On 3 February 2012 00:11, Florian Weimer <fweimer at bfk.de> wrote:
> Would it make sense to replace (x ^ r) < 0 && (x ^ y) >= 0
> with (x ^ y ^ r) < 0?
That would not be correct. For example, it would signal overflow for -1 + 1.
Éamonn
On 3 February 2012 00:11, Florian Weimer <fweimer at bfk.de> wrote:
> * 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