likely() and unlikely() macros use __builtin_expect() to optimize branches with high probabilities.
In GCC manual:
long __builtin_expect (long exp, long c) [Built-in Function]
You may use __builtin_expect to provide the compiler with branch prediction information. In general, you should prefer to use actual profile feedback for this (‘-fprofile-arcs’), as programmers are notoriously bad at predicting how their programs actually perform. However, there are applications in which this data is hard to collect. The return value is the value of exp, which should be an integral expression. The value of c must be a compile-time constant. The semantics of the built-in are that it is expected that exp == c. For example:
if (__builtin_expect (x, 0))
foo ();
would indicate that we do not expect to call foo, since we expect x to be zero. Since you are limited to integral expressions for exp, you should use constructions such as
if (__builtin_expect (ptr != NULL, 1))
error ();
when testing pointer or floating-point values.
=====
With use of __builtin_expect(), we define:
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
NOTE: __builtin_expect(!!expr, 0) -- > ‘!!’ acts as booleanizers (to ensure x is either 1 or 0)
References:
http://bytes.com/groups/c/217942-replacing-builtin_expect-normal-code
http://stenlyho.blogspot.com/2007/05/likelyunlikelygccbuiltinexpect.html
Comments
Sat, 21.11.2009 00:04
Great! Thanks for sharing. By the way, I hav [...]
Fri, 20.11.2009 16:59
FILE(GLOB Mac_CPP “*_Mac.cpp”) FILE(GLOB Ma [...]
Tue, 17.11.2009 14:36
您的部落格照片很清晰、詳盡呢 剛好我們最近有 [...]
Fri, 06.11.2009 09:29
好棒我也想去~~~~~~~~~~~
Wed, 28.10.2009 18:00
Now i upgraded this problemati c PC to 9.10, [...]