This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: if/then/else hint ?

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

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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]