7

Just see this construction in the linux kernel, and I can't get what does it mean.

110         return unlikely(sl->sequence != start);

I know that likely/unlikely are made with __builtin_expect function described here: http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

You may use __builtin_expect to provide the compiler with branch prediction information.

But what kind of branch prediction hints is possible for unconditional branch??

jww
  • 97,681
  • 90
  • 411
  • 885
osgx
  • 90,338
  • 53
  • 357
  • 513

2 Answers2

11

Just guessing here, but imagine the function is inlined by the compiler, and you have this in the calling code:

if (functionUsingUnlikelyForReturn()) {
   // Do something
} else {
   // Do something different
}

then it's entirely reasonable for the branch prediction to take note of the hint.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

The "unlikely" does not give a likelyhood for returning from the function, but rather an expected value for the return value. My guess is that the function is inlineable, so this is a hint for optimizing the function's caller.

Simon Richter
  • 28,572
  • 1
  • 42
  • 64