Efficient detection of signed overflow?
Florian Weimer
fw@deneb.enyo.de
Sun Nov 29 20:54:00 GMT 2009
* Mark Dickinson:
> long a, b, x;
> ...
> x = a + b;
> if ((x^a) < 0 && (x^b) < 0)
> goto deal_with_overflow;
> ...
Would this test be better? I think it's equivalent, and it saves a
comparison.
if (((x ^ a) & (x ^ b)) < 0)
> Any suggestions for improvements over this?
Use -fwrapv and your first version.
> Are there any common C code constructs that gcc would compile to a
> jump-on-overflow instruction on x86?
I don't think so. Without support throughout GCC, anything in this
area (like a builtin) would mostly act as an optimization barrier.
More information about the Gcc-help
mailing list