Efficient detection of signed overflow?

Bob Plantz plantz@cds1.net
Wed Dec 2 21:12:00 GMT 2009


On Sun, 2009-11-29 at 16:00 -0500, me22 wrote:

> 
> What about using (long)((unsigned long)a + (unsigned long)b) or
> something to get around the UB?

Type casting may be ineffective. On x86-64 in 64-bit mode gcc (4.4.1)
generates unsigned addition. That is,

x = a + b;

is implemented as

leaq	(%rdx,%rax), %rbx

The leaq instruction has no effect on the rflags register. Hence, even
if we could test the overflow bit, it would not be accurate.

--Bob




More information about the Gcc-help mailing list