Introduction to likely and unlikely in Linux

Source: Internet
Author: User

The likely () and unlikely () are the two macros defined in the kernel (2.6 version should be available). Located in the/include/linux/compiler.h,
The specific definition is as follows:
#define LIKELY (x) __builtin_expect (!! (x), 1)
#define UNLIKELY (x) __builtin_expect (!! (x), 0)

__builtin_expect is a preprocessing command provided in GCC (version >=2.96, written on the web, I did not verify) (This noun is also written on the web, I want to call the function better), and is good for code optimization. The GCC (version 4.4.0) is specifically defined as follows:
Long __builtin_expect (long exp, long c) [built-in Function]

Note is:
You could use __builtin_expect to provide the compiler with branch prediction information. In general, your should prefer to use actual profiles feedback for this ('-fprofile-arcs '), as programmers are notoriously b Ad at predicting how their programs actually perform. However, there are applications in which this data are hard to collect. The return value was the value of exp, which should be a integral expression. The semantics of the built-in are that it is expected that exp = c.

It means: We can use this function to tell the compiler that some branch predictive information "Exp==c" is "very likely to happen".

#define LIKELY (x) __builtin_expect (!! (x), 1) means that x==1 is "frequently occurring" or "likely to occur".
With likely, it is more likely that the following statements are executed, and the compiler compiles the contents of the if{} to the front, using unlikely, and the likelihood of executing else subsequent statements is greater, and the compiler compiles the contents of else{} to the front. This is advantageous to the CPU prefetching, enhances the prefetch instruction the correct rate, thus may enhance the efficiency.

For example (kernel version 2.6.22.6): There is a section in/KERNEL/SHED.C:
if (likely (!active_balance)) {
/* We were unbalanced, so reset the balancing interval * *
Sd->balance_interval = sd->min_interval;
} else {
/*
* If we ' ve begun active balancing, start to back off. This
* Case may is covered by the all_pinned logic if there
* are only 1 tasks on the busy Runqueue (because we don t call
* move_tasks).
*/
if (Sd->balance_interval max_interval)
Sd->balance_interval *= 2;
}

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

In the compilation process, the contents of the IF {} will be compiled into the front, or the contents of {} behind it will be compiled to the back. Replacing likely with unlikely is just the opposite.

In short, likely and unlikely interchangeably or do not affect the correctness of the program. But it may affect the efficiency of the program.

if (likely (foo))//think Foo is usually 1

if (Unlikely (foo))//think Foo is usually 0

In the final analysis, this macro works better in compiling to achieve higher execution efficiency.

In addition, likely or unlikely to be defined as __builtin_expect (!! (x), 1) instead of directly using __builtin_expect (x, 1)? " !! (x) differs from "X" in that there is no bool type in C, in order to change X to 0 or 1 true or false.

In addition, there is a definition of likely and unlikely in kernel 2.6.31.5:
# ifndef likely
# define likely (x) (__builtin_constant_p (x)?!! (x): __branch_check__ (x, 1))
# endif

# ifndef unlikely
# define unlikely (x) (__builtin_constant_p (x)?!! (x): __branch_check__ (x, 0))

# endif

Original blog.csdn.net/tommy_wxie/article/details/7384641, there are changes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.