Likely (), unlikely () and command prefetch

Source: Internet
Author: User
Tags prefetch

InCodeThe use of likely () and unlikely () is often seen, especially in Linux kernel code.

If (likely (value) is equivalent to If (value)

If (unlikely (value) is also equivalent to If (value)

In terms of code understanding, their effects are consistent, so what are their differences?

The two macros are defined in Compiler. h as follows:

# Define likely (x) _ builtin_exact CT (!! (X), 1)
# Define unlikely (x) _ builtin_exact CT (!! (X), 0)

In the GCC document:

-Built-in function: Long_ Builtin_expect(Long exp, long c)

You may use_ Builtin_expectTo 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 valueExp, Which shoshould be an integral expression. the semantics of the built-in are that it is expected thatExp=C. For example:

 
If (_ builtin_ct (x, 0) Foo ();

Indicates that we do not have CT to callFoo, Since we recommend CTXTo be zero. Since you are limited to integral expressionsExp, You shoshould use constructions such

 
If (_ builtin_ct (PTR! = NULL, 1) Foo (* PTR );

When testing pointer or floating-point values.

 

Well, in one sentence, compilation optimization is used to make code instruction prefetch more efficient.

For specific test code, refer to the following URL:

 

Reference URL:

Http://blog.csdn.net/rstevens/article/details/1798561

Http://kernelnewbies.org/FAQ/LikelyUnlikely

Http://caisenchen.blog.163.com/blog/static/55286550200871131855311/

Http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

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.