Re: Compiler inserts redundant comparison against zero




spamtrap@xxxxxxxxxx wrote:
> I've been led to believe that modern optimising
> compilers are so sharp that it would be a waste
> of time writing my own assembly language code
> for general purposes.

In *general* this is probably correct. But when you talk specifics, you
start seeing the errors in such an attitude.

>
> 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 )...
>
> which is often encountered, especially in validation
> code (I mean the zero is often met with, the 14 could
> be anything).
>
> The compiler issues these instructions:
>
> test eax, eax ; value of variable x already in eax
> jl condition_false
> cmp eax, 0Eh
> jge condition_false
> condition_true:
> etc
>
> instead of the economical:
>
> cmp eax, 0Eh
> jae condition_false
> condition_true:
> etc
>
> Is this optimisation not well-known?
>
> Regards
> Chris Noonan

I'd suspect that the C/C++ compiler's view of side effects and sequence
points is what's getting in the way here (keep in mind the full effects
of the "&&" operator). That's not an excuse for this code generation,
just a suggestion for why it might be happening. It's still pretty
stupid, of course.
Cheers,
Randy Hyde

.



Relevant Pages

  • Re: Compiler inserts redundant comparison against zero
    ... > I've been led to believe that modern optimising ... > So I was surprised to find out how the C compiler ... > test eax, eax; value of variable x already in eax ...
    (comp.lang.asm.x86)
  • Compiler inserts redundant comparison against zero
    ... I've been led to believe that modern optimising ... of time writing my own assembly language code ... So I was surprised to find out how the C compiler ... test eax, eax; value of variable x already in eax ...
    (comp.lang.asm.x86)
  • Re: Compiler inserts redundant comparison against zero
    ... > I've been led to believe that modern optimising ... > of time writing my own assembly language code ... > So I was surprised to find out how the C compiler ... > test eax, eax; value of variable x already in eax ...
    (comp.lang.asm.x86)
  • Re: Compiler inserts redundant comparison against zero
    ... > So I was surprised to find out how the C compiler ... > optimization, both for speed and size", deals ... > test eax, eax; value of variable x already in eax ... > cmp eax, 0Eh ...
    (comp.lang.asm.x86)
  • Re: Release Dll Problem - Optimization Issue
    ... If optimising for speed on smaller size processors, ... This limits the compiler in using fast registers for the ... and speeds up access to the variable. ...
    (microsoft.public.pocketpc.developer)