Re: Subtraction + comparison in one asm instruction?

"Anton Ertl" <anton@mips.complang.tuwien.ac.at>
19 Sep 2002 01:12:14 -0400

          From comp.compilers

Related articles
[8 earlier articles]
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: "Anton Ertl" <anton@mips.complang.tuwien.ac.at>
Newsgroups: comp.compilers
Date: 19 Sep 2002 01:12:14 -0400
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
References: 02-09-038 02-09-076 02-09-083 02-09-093 02-09-096
Keywords: arithmetic
Posted-Date: 19 Sep 2002 01:12:13 EDT

"Vincent Lefevre" <vincent+news@vinc17.org> writes:
> Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
[optimize x-1>0 into x>1?]
>> 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.


Some people may not realize that they have done this before they try
debug the program (ok, unlikely in this case, but it's probably hard
to decide such things in general at optimizer design time), and they
may want to turn off optimization for debugging for various practical
reasons.


>> 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.


gcc-3.2 generates different, but similar-quality code from these two
cases for the 386 architecture. It surprised me that it is smart
enough to know that (x>>31)==0 can be optimized into a sign-bit test.


The 31 magic number is of course where you would pick up most
additional code size if you wanted to stay within ANSI C (but the
original code uses a wordsize integer type coming from a configure
script anyway (not necessarily int), so the configure script could
also define that magic number).


- anton
--
M. Anton Ertl
anton@mips.complang.tuwien.ac.at
http://www.complang.tuwien.ac.at/anton/home.html


Post a followup to this message

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