This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Inline assembly for detecting int32 overflow on IA32 and AMD64
- From: Adam Warner <lists at consulting dot net dot nz>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 18 Jan 2005 17:06:13 +1300
- Subject: Inline assembly for detecting int32 overflow on IA32 and AMD64
Hi all,
I've been a C programmer for about a month and I'm trying to work around
some of the deficiencies of portable C. A major one is the inability to
efficiently detect signed integer overflow.
The code I've written below surprisingly works on IA32 and AMD64 at zero
optimisation with Debian GNU/Linux:
sum is 2147483647
No Overflow
sum is -2147483648
Overflow
And gives nonsense answers at higher levels of optimisation, e.g.:
sum is -1073745624
No Overflow
sum is -1789480959
No Overflow
Any tips for fixing this?
#include <stdint.h>
#include <stdio.h>
inline int32_t add(int32_t a, int32_t b) {
signed char overflow=0;
int32_t sum=a;
__asm__ __volatile__("add %[src], %[dest]"
: [dest] "=R" (sum)
: [src] "g" (b));
__asm__ __volatile__("jno 1f\n\t"
"movb $1, %[dest]\n\t"
"1:"
: [dest] "=m" (overflow));
printf("sum is %i\n", sum);
if (overflow) printf("Overflow\n");
else printf("No Overflow\n");
return sum;
}
int main(void) {
add(0x7FFFFFFF, 0);
add(0x7FFFFFFF, 1);
return 0;
}
I chose R as the register destination for the sum because it denotes a
legacy register (equivalent to r class in i386 mode).
I'm using a char as an overflow flag because there doesn't appear to be
any way to jump to a C label from within the assembly.
Tips about faster approaches are also appreciated :-)
Many thanks,
Adam