Re: Subtraction + comparison in one asm instruction?

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

          From comp.compilers

Related articles
[5 earlier articles]
Re: Subtraction + comparison in one asm instruction? gdr@soliton.integrable-solutions.net (Gabriel Dos Reis) (2002-09-03)
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)
[1 later articles]
| List of all articles for this month |

From: "Anton Ertl" <anton@mips.complang.tuwien.ac.at>
Newsgroups: comp.compilers
Date: 14 Sep 2002 00:19:44 -0400
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
References: 02-09-038 02-09-076 02-09-083
Keywords: arithmetic
Posted-Date: 14 Sep 2002 00:19:44 EDT

"Vincent Lefevre" <vincent+news@vinc17.org> writes:
>But gcc (for instance) currently optimizes x + 1 - 1 to x. So,
>under this condition, there is no reason why x - 1 > 0 shouldn't
>be optimized to x > 1 (x being a *signed* integer).


For gcc, x+1-1 gives the same result as x on gcc for all signed
integers x (gcc does not report overflows for signed integers).


In contrast, x-1>0 gives a different result from x>1 if x is the
smallest signed integer. In that case the result of x-1 is undefined
(or implementation-defined or somesuch), so the implementation has the
right to perform this optimization according to the C standard. It's
not clear if it is a good idea, though.


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


Also, do you want to be able to compile code into its intended
equivalent that is non-standard, but nonetheless exists? Here's an
example that might be affected by the optimization you propose; it
checks if adding n to n1 (giving n2) crosses the boundary between
nlimit-1 and nlimit but does not trigger on signed or unsigned
wraparound (this code assumes wrap-around 2s-complement arithmetics):


/* inputs: int n, n1, nlimit */
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.


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