On Sep 6, 2011, at 12:58 PM, John Rose wrote:
> What's needed here is a way to get 33 bits out of a 32-bit add intrinsic.
> There's no fully natural way to do this, and about 4 kludgey ways. Because
> there are so many poor ways to shape the API, it's hard to pick the best one
> to invest in.
But, assuming the user wants to force a JO, here's one fairly clean way to do
it:
/**
* Detect 32-bit overflow if the parameters are summed.
* @return true if the inputs have the same sign, but their 32-bit sum
would have a different sign
*/
public static boolean addWouldOverflow(int x, int y) {
//int res = x + y;
//boolean overflowDetected = (SGN(x) == SGN(y) && SGN(x) != SGN(res));
//boolean overflowDetected = ((x ^ y) >= 0 & (x ^ res) < 0);
//boolean overflowDetected = ((x ^ y ^ -1) & (x ^ res)) < 0;
return (((x ^ y ^ -1) & (x ^ (x+y))) < 0);
}
That would provide a fairly stable and clear target for the JIT to aim at. No
points for ease-of-use.
-- John
_______________________________________________
mlvm-dev mailing list
mlvm-dev@openjdk.java.net
http://mail.openjdk.java.net/mailman/listinfo/mlvm-dev