Re: Compiler inserts redundant comparison against zero
- From: "randyhyde@xxxxxxxxxxxxx" <spamtrap@xxxxxxxxxx>
- Date: 12 Nov 2005 21:03:28 -0800
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
.
- References:
- Compiler inserts redundant comparison against zero
- From: spamtrap
- Compiler inserts redundant comparison against zero
- Prev by Date: Re: Which x86 assembler for Windows/Linux/Solaris?
- Next by Date: Re: Which x86 assembler for Windows/Linux/Solaris?
- Previous by thread: Compiler inserts redundant comparison against zero
- Next by thread: Re: Compiler inserts redundant comparison against zero
- Index(es):
Relevant Pages
|