This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: if/then/else hint ?
- From: Jamie Lokier <jamie at shareable dot org>
- To: Greg Smith <rys at epaibm dot rtpnc dot epa dot gov>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 8 Jan 2004 18:05:26 +0000
- Subject: Re: if/then/else hint ?
- References: <3FFD9840.9030000@trex.rtpnc.epa.gov>
Greg Smith wrote:
> We would much prefer to fall thru to <fastpath> instead and jump
> away to <slowpath> (reducing path length for <fastpath> by an
> instruction). Is there a way to give the compiler the hint ??
Yes. Look up __builtin_expect().
Typically used like this:
#define likely(c) __builtin_expect(!!(c),1)
#define unlikely(c) __builtin_expect(!!(c),0)
Then you would use "if (likely(condition)) { ... }".
> I really don't know that much about pentium branch prediction so
> might I be worrying about something that won't make any difference ??
When a branch is not already in the branch target buffer (BTB; a small
cache of previously taken brances to predict them subsequently), a
Pentium guesses that a forward branch is not taken and a backward
branch is taken. So you're right to observe that the code is not
optimal.
After the first time that "if" statement is reached, if the branch
instruction is still in the BTB then the code is fine. That will
depend on the size and complexity of the rest of the code executed
each iteration.
-- Jamie