public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Inline assembly for detecting int32 overflow on IA32 and AMD64
@ 2005-01-18  4:11 Adam Warner
  2005-01-18  7:51 ` Adam Warner
  2005-01-20  4:47 ` Adam Warner
  0 siblings, 2 replies; 3+ messages in thread
From: Adam Warner @ 2005-01-18  4:11 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Inline assembly for detecting int32 overflow on IA32 and AMD64
  2005-01-18  4:11 Inline assembly for detecting int32 overflow on IA32 and AMD64 Adam Warner
@ 2005-01-18  7:51 ` Adam Warner
  2005-01-20  4:47 ` Adam Warner
  1 sibling, 0 replies; 3+ messages in thread
From: Adam Warner @ 2005-01-18  7:51 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Inline assembly for detecting int32 overflow on IA32 and AMD64
  2005-01-18  4:11 Inline assembly for detecting int32 overflow on IA32 and AMD64 Adam Warner
  2005-01-18  7:51 ` Adam Warner
@ 2005-01-20  4:47 ` Adam Warner
  1 sibling, 0 replies; 3+ messages in thread
From: Adam Warner @ 2005-01-20  4:47 UTC (permalink / raw)
  To: gcc-help

Thanks for the brilliant response Thorsten (via email, which I just
discovered).

A fixed solution is:

#include <stdint.h>
#include <stdio.h>

inline int32_t add(int32_t a, int32_t b) {
  signed char overflow;
  int32_t sum=a;
  __asm__ __volatile__("add %[src], %[dest] \n\t" \
                       "seto %[overflow] \n"
                       : [dest] "+R" (sum), [overflow] "=m" (overflow)
                       : [src] "g" (b)
                       : "cc");

  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 made a number of mistakes:
1. The [dest] should be a read/write variable.
2. I clobbered the condition code (cc) so I should tell gcc this.
3. The condition code may change between assembly blocks so it has to be
written as a single block.

Many thanks Thorsten.

Thorsten used seto instead of branch on overflow because branch
mispredictions are usually expensive on modern CPUs.

Posting to gcc help can be a bit hit and miss. The NNTP version (via
news.individual.net) doesn't appear to propagate to the mailing list
(which I'm posting to via the news.gmane.org NNTP interface).

Regards,
Adam

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-01-20  4:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-18  4:11 Inline assembly for detecting int32 overflow on IA32 and AMD64 Adam Warner
2005-01-18  7:51 ` Adam Warner
2005-01-20  4:47 ` Adam Warner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).