likely and unlikely

本文介绍了GCC编译器中likely和unlikely宏的使用方法及其背后的原理。这些宏用于提供分支预测信息给编译器,帮助其生成更加高效的代码。文章详细解释了__builtin_expect函数的作用,并举例说明如何使用这些宏来提高代码性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在内核代码中经常见到likely和unlikely宏。

The macros are defined as :

#define likely(x)       __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
The __builtin_expect is a method that gcc (versions >= 2.96) offer for programmers to indicate branch prediction information to the compiler. The return value of __builtin_expect is the first argument (which could only be an integer) passed to it.

To check it out how it could be beneficial, an excerpt from "info gcc" :

     if (__builtin_expect (x, 0))
foo ();


[This] would indicate that we do not expect to call `foo', since we
expect `x' to be zero.

Based on this information the compiler generates intelligent code, such that the most expected result is favored.


__builtin_expect (exp, val)只是在我们实现代码时能够预见exp的值很有可能会是val,
编译时通过这个宏把我们的预期告诉编译器,编译器按照这个预期会生成更高效的可执行代码。
该宏的返回值为exp,exp只能是整型。

This happens when a goal of a plugin couldn't be configured. The most likely cause is that the contents of the <configuration> section for the plugin in your POM does not match the parameter types of the goal in question. E.g. trying to configure a numeric parameter from a string would trigger this error. So please verify that the configuration for the goal is correct by consulting the plugin documentation. The documentation for many common Maven plugins can be reached via our plugin index. While quite unlikely, it's also possible that the goal couldn't be configured because of a broken plugin class path. For instance, if the plugin class path is missing some dependencies of the plugin, this can result in linkage errors that prevent the configuration of the goal. To check whether the plugin JAR and its POM are intact, inspect your local repository which is usually located at ${user.home}/.m2/repository. For a plugin with the coordinates com.company:custom-maven-plugin:1.0, the file to check is com/company/custom-maven-plugin/1.0/custom-maven-plugin-1.0.jar and custom-maven-plugin-1.0.pom, respectively. If those files don't match the copies in the plugin repository, e.g. as noticeable by checksum mismatches, simply delete the local files and have Maven re-download it. If you have verified that your local copy of the JAR/POM and the JAR/POM in the plugin repository are bytewise identical but Maven still reports a linkage error, please report this to the maintainer of the plugin. 翻译
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值