Need reviewer: JDK 8 CR for Support Integer overflow (original) (raw)

Vitaly Davidovich vitalyd at gmail.com
Fri Feb 3 18:22:19 UTC 2012


x == Integer.MIN_VALUE should be faster than x == -x as it's a cmp against a constant whereas the latter requires negating x (that's a dependency too), tying up a register to store the negation, and then doing the cmp.

Sent from my phone On Feb 3, 2012 12:53 PM, "Eamonn McManus" <eamonn at mcmanus.net> wrote:

My initial remarks:

In negateExact, the condition x == -x should be faster to evaluate than x == Integer.MINVALUE 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 MINVALUE and MAXVALUE, 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/viewbug.do?bug**id=6708398<_ _http://bugs.sun.com/bugdatabase/viewbug.do?bugid=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