For Programmers: Free Programming Magazines  


Home > Archive > A86 Assembler > March 2006 > Re: Compiler inserts redundant comparison against zero









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Compiler inserts redundant comparison against zero
Scott J. McCaughrin

2006-03-07, 7:55 am

spamtrap@crayne.org wrote:
(Deleted)
: So I was surprised to find out how the C compiler
: I use, described as having "world-class
: optimization, both for speed and size", deals
: with the fragment:

: if( x >= 0 && x < 14 )...

This is the condition: 0 <= x < 14. The negation of this
condition is: x < 0 or x >= 14.

: test eax, eax ; value of variable x already in eax
: jl condition_false
: cmp eax, 0Eh
: jge condition_false
: condition_true:
: etc

Thus, your code should include:

condition_false: ; execution reaches here if x<0 or x>=14

and executes at label: condition_true iff x>=0 && x<14

: instead of the economical:

: cmp eax, 0Eh
: jae condition_false
: condition_true:
: etc

: Is this optimisation not well-known?

The economical code executes at label: condition_false
only if x>=14, not if x<0. So your code only appears
to be economical, in that it omits a test.

Frank Kotler

2006-03-07, 7:55 am

Scott J. McCaughrin wrote:
> spamtrap@crayne.org wrote:
> (Deleted)
> : So I was surprised to find out how the C compiler
> : I use, described as having "world-class
> : optimization, both for speed and size", deals
> : with the fragment:
>
> : if( x >= 0 && x < 14 )...
>
> This is the condition: 0 <= x < 14. The negation of this
> condition is: x < 0 or x >= 14.
>
> : test eax, eax ; value of variable x already in eax
> : jl condition_false
> : cmp eax, 0Eh
> : jge condition_false
> : condition_true:
> : etc
>
> Thus, your code should include:
>
> condition_false: ; execution reaches here if x<0 or x>=14
>
> and executes at label: condition_true iff x>=0 && x<14
>
> : instead of the economical:
>
> : cmp eax, 0Eh
> : jae condition_false
> : condition_true:
> : etc
>
> : Is this optimisation not well-known?
>
> The economical code executes at label: condition_false
> only if x>=14, not if x<0. So your code only appears
> to be economical, in that it omits a test.


It only appears to omit a test. I don't know what ">" and "<" mean - my
CPU hasn't got 'em. If x is "less than" zero, it's "above" 14 - "above"
0x7FFFFFFF, in fact. So condition_false *does* execute if x is "less
than" zero.

This is what comes of the compiler following the rules, while the human
sees the possibilities! :)

Best,
Frank

Tim Roberts

2006-03-09, 3:55 am

"Scott J. McCaughrin" <spamtrap@crayne.org> wrote:
>
>: instead of the economical:
>
>: cmp eax, 0Eh
>: jae condition_false
>: condition_true:
>: etc
>
>: Is this optimisation not well-known?
>
>The economical code executes at label: condition_false
>only if x>=14, not if x<0. So your code only appears
>to be economical, in that it omits a test.


The code is quite correct. "jae" is an unsigned test. If the signed value
is negative, it will appear in an unsigned context as a large positive
value. So -3 will appear to be must greater than 14 in an unsigned
comparison.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com