[Reprinted] Likely and unlikely in the Linux kernel

Preface
  Kernel version: linux 4.13
  In the linux kernel, you can often see if (likely (x)) or if (unlikely (x)) statements, so what does it mean likely and unlikely? This article will discuss some of the likely and unlikely.

likely和unlikely

Reference /include/linux/compiler.h * /

# define likely(x)  __builtin_expect(!!(x), 1)
# define unlikely(x)    __builtin_expect(!!(x), 0)

The above source code uses the built-in function __builtin_expect to define, that is, built in function.
  The function prototype of __builtin_expect is long __builtin_expect (long exp, long c), the return value is the value of the complete expression exp, and its role is to expect the value of the expression exp to be equal to c (if exp == c condition holds the greatest chance Most, then performance will be improved, otherwise performance will be reduced). Note that the return value of __builtin_expect (lexp, c) is still the exp value itself, and does not change the value of exp.
  The __builtin_expect function is used to guide gcc to perform conditional branch prediction. During the execution of an instruction, due to the role of the pipeline, the CPU can simultaneously complete the fetching of the next instruction, which can improve the CPU utilization. When executing a conditional branch instruction, the CPU will also prefetch the next execution, but if the result of the conditional branch is to jump to another instruction, the next instruction prefetched by the CPU is useless, which reduces the efficiency of the pipeline.
  In addition, jump instructions consume more CPU time than sequentially executed instructions. If you can skip jumps as much as possible, you can also improve CPU performance.
  Simply look on the surface if (likely (value)) == if (value), if (unlikely (value)) == if (value).
That is, likely and unlikely are the same, but in fact the execution is different, plus likely means that the value of the value is more likely to be true, then the chance of executing if is greater, and unlikely means that the value of the value is false The possibility is greater, and the opportunity to execute else is greater.
  With this modification, when compiled into binary code, it is possible to make the execution statement following if immediately follow the previous program, and uniquely make the statement following else immediately follow the previous program, which will be pre-read by the cache and increase the program's Execution speed.
  So why use it in the above definition! ! What about symbols?
  The bool logic in the computer is only 0 and 1, non-zero is 1, and it can be used when the parameter in like (x) is not a logical value! ! The sign is converted to a logical value of 1 or 0. such as:! ! (3) =! (! (3)) =! 0 = 1, so that parameter 3 is converted to logic 1.
  So the simple understanding is:
  likely (x) means that x is logically true (1) is more likely;
  unlikely (x) means that x is logically false (0) is more likely.

Transferred from the linux kernel likely and unlikely

Published 91 original articles · praised 17 · 50,000+ views

Guess you like

Origin blog.csdn.net/qq_23327993/article/details/105298509