This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: 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 20:50:29 +1300
- Subject: Re: Inline assembly for detecting int32 overflow on IA32 and AMD64
- References: <pan.2005.01.18.04.06.12.280929@consulting.net.nz>
On Tue, 18 Jan 2005 17: 06:13 +1300, Adam Warner wrote:
On Tue, 18 Jan 2005 17:06:13 +1300, Adam Warner wrote:
> 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
I've solved the corruption by forcing the addition to the variable
sum out to memory. Improvements welcome. Optimal pseudocode would look
something like this with appropriate gcc support:
asm add
asm jump no overflow -> C_label
...
C code to handle overflow
...
C_label:
Remaining C code
...
#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] "=m" (sum)
: [src] "R" (b)
: "0");
__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;
}
Regards,
Adam