Need reviewer: JDK 8 CR for Support Integer overflow

Eamonn McManus eamonn at mcmanus.net
Fri Feb 3 17:52:36 UTC 2012


My initial remarks:

In negateExact, the condition x == -x should be faster to evaluate than x
== Integer.MIN_VALUE and reflects the intent just as well.

In addExact and subtractExact, I would be inclined to implement the int
versions using long arithmetic, like this:

long lr = x + y;
int r = (int) lr;
if (r == lr) {
  return r;
} else {
  throw...
}

I would use this technique of cast-and-compare in the int multiplyExact
instead of comparing against MIN_VALUE and MAX_VALUE, and especially in
toIntExact(long).

I agree with Stephen Colebourne that brief implementation comments would be
useful. But I disagree with his proposed further methods in Math
(increment, decrement, int+long variants), which I don't think would pull
their weight.

Éamonn


On 2 February 2012 12:15, Roger Riggs <Roger.Riggs at oracle.com> wrote:

> There is a need for arithmetic operations that throw exceptions
> when the results overflow the representation of int or long.
>
> The CR is 6708398: Support integer overflow <http://bugs.sun.com/**
> bugdatabase/view_bug.do?bug_**id=6708398<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6708398>
> >
>
> Please review this webrev <http://cr.openjdk.java.net/%**
> 7Erriggs/CR6708398/webrev/<http://cr.openjdk.java.net/%7Erriggs/CR6708398/webrev/>>
> to add static methods in java.lang.Math
> to support addExact(), subtractExact(), negateExact(), multiplyExact(),
> and toIntExact() for int and long primitive types.
>
> Thanks, Roger Riggs
>



More information about the core-libs-dev mailing list