Branch Prediction Optimization in GCC
The Intel architecture provides a mechanism to influence branch prediction in generated code. With the __builtin_expect() function, GCC can force branch prediction to go a certain way.
The syntax for __builtin_expect() is:
long __builtin_expect (long exp, long c)
where:
For example, to force branch prediction to always take the "true" branch in the following C code:
if (normal) { doSomethingNormal(); } else { exceptionalCase(); }
You would use the following statement:
if (__builtin_expect(normal, 1))
To simplify the usage, it's common to define macros:
#define likely(x) __builtin_expect (!!(x), 1) #define unlikely(x) __builtin_expect (!!(x), 0)
However, it's important to note that:
The above is the detailed content of How to Enhance Branch Prediction Optimization in GCC?. For more information, please follow other related articles on the PHP Chinese website!