Re: Subtraction + comparison in one asm instruction?

"Vincent Lefevre" <vincent+news@vinc17.org>
14 Sep 2002 16:16:50 -0400

          From comp.compilers

Related articles
[6 earlier articles]
Re: Subtraction + comparison in one asm instruction? vincent+news@vinc17.org (Vincent Lefevre) (2002-09-08)
Re: Subtraction + comparison in one asm instruction? gdr@integrable-solutions.net (Gabriel Dos Reis) (2002-09-12)
Re: Subtraction + comparison in one asm instruction? vbdis@aol.com (VBDis) (2002-09-12)
Re: Subtraction + comparison in one asm instruction? gdr@integrable-solutions.net (Gabriel Dos Reis) (2002-09-12)
Re: Subtraction + comparison in one asm instruction? vincent+news@vinc17.org (Vincent Lefevre) (2002-09-12)
Re: Subtraction + comparison in one asm instruction? anton@mips.complang.tuwien.ac.at (Anton Ertl) (2002-09-14)
Re: Subtraction + comparison in one asm instruction? vincent+news@vinc17.org (Vincent Lefevre) (2002-09-14)
Re: Subtraction + comparison in one asm instruction? vbdis@aol.com (VBDis) (2002-09-19)
Re: Subtraction + comparison in one asm instruction? anton@mips.complang.tuwien.ac.at (Anton Ertl) (2002-09-19)
Re: Subtraction + comparison in one asm instruction? joachim_d@gmx.de (Joachim Durchholz) (2002-09-19)
Re: Subtraction + comparison in one asm instruction? sander@haldjas.folklore.ee (Sander Vesik) (2002-11-12)
Re: Subtraction + comparison in one asm instruction? sander@haldjas.folklore.ee (Sander Vesik) (2002-11-12)
Re: Subtraction + comparison in one asm instruction? jvorbrueggen@mediasec.de (Jan C. =?iso-8859-1?Q?Vorbr=FCggen?=) (2002-11-13)
| List of all articles for this month |

From: "Vincent Lefevre" <vincent+news@vinc17.org>
Newsgroups: comp.compilers
Date: 14 Sep 2002 16:16:50 -0400
Organization: a training zoo
References: 02-09-038 02-09-076 02-09-083 02-09-093
Keywords: arithmetic
Posted-Date: 14 Sep 2002 16:16:50 EDT

    Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:


> Do you value such an optimization more than being able to get the same
> results with and without optimization?


For those who write programs not based on non-standard 2s-complement
wraparound, yes.


> Also, do you want to be able to compile code into its intended
> equivalent that is non-standard, but nonetheless exists?


In this case, a compatibility compiler switch can been added.


> int n2;
> int olddiff = n1-nlimit;
> n2=n1+n;
> if ((olddiff^(olddiff+n))>=0 /* the limit is not crossed */
> || (olddiff^n)>=0 /* it is a wrap-around effect */) {
> ...


> You might be able to implement this in fully compliant ANSI C, but it
> probably will be longer, and probably will also run slower when
> compiled with gcc.


No, you can modify it to make it ISO C compliant, though still based
on non-portable assumptions (i.e. 2s-complement), and that should not
be slower if the compiler is smart enough (which seems to be the
case). Just change the condition to:


    (((unsigned) olddiff ^ ((unsigned) olddiff + (unsigned) n)) >> 31 == 0 ||
      ((unsigned) olddiff ^ (unsigned) n) >> 31 == 0)


i.e. cast all variables to unsigned and check the most significant
bit. I've written 31, assuming 32-bit int's, but in the real world,
you can find the correct value using INT_MAX. gcc 2.94.4 for the ARM
generates exactly the same assembly code as with your C source.


Vincent Lefèvre.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.