This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Efficient detection of signed overflow?
- From: Bob Plantz <plantz at cds1 dot net>
- To: me22 <me22 dot ca at gmail dot com>
- Cc: Mark Dickinson <dickinsm at gmail dot com>, GCC-help <gcc-help at gcc dot gnu dot org>
- Date: Wed, 02 Dec 2009 10:16:11 -0800
- Subject: Re: Efficient detection of signed overflow?
- References: <5c6f2a5d0911291151j46dc9d28h568b87f57228b28e@mail.gmail.com> <fa28b9250911291300mb968331jbd2f9682c4c2130f@mail.gmail.com>
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