您的位置:首页 > 产品设计 > UI/UE

likely,unlikely宏与GCC内建函数__builtin_expect()

2015-12-24 09:39 507 查看
摘自:/article/5026953.html


likely,unlikely宏与GCC内建函数__builtin_expect()

在GCC手册中对__builtin_expect()的描述是这样的:

long __builtin_expect (long exp, long c)

You may use __builtin_expect to 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 value of exp, which should be an integral expression. The

value of c must be a compile-time constant. The semantics of the built-in are that it

is expected that exp == c. For example:

if (__builtin_expect (x, 0))

foo ();

would indicate that we do not expect to call foo, since we expect x to be zero. Since

you are limited to integral expressions for exp, you should use constructions such as

if (__builtin_expect (ptr != NULL, 1))

error ();

when testing pointer or floating-point values.

由于大部分程序员在分支预测方面做得很糟糕,所以GCC提供了这个内建函数来帮助程序员处理分支预测,优化程序。其第一个参数exp为一个整型表达式,这个内建函数的返回值也是这个exp,而c为一个编译期常量,这个函数的语义是:你期望exp表达式的值等于常量c,从而GCC为你优化程序,将符合这个条件的分支放在合适的地方。

因为这个程序只提供了整型表达式,所以如果你要优化其他类型的表达式,可以采用指针的形式。

likely和unlikely是gcc扩展的跟处理器相关的宏:

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

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

现在处理器都是流水线的,有些里面有多个逻辑运算单元,系统可以提前取多条指令进行并行处理,但遇到跳转时,则需要重新取指令,这相对于不用重新去指令就降低了速度。

所以就引入了likely和unlikely,目的是增加条件分支预测的准确性,cpu会提前装载后面的指令,遇到条件转移指令时会提前预测并装载某个分支的指令。unlikely 表示你可以确认该条件是极少发生的,相反likely表示该条件多数情况下会发生。编译器会产生相应的代码来优化cpu执行效率。

因此程序员在编写代码时可以根据判断条件发生的概率来优化处理器的取指操作。

例如:

int x, y;

if(unlikely(x > 0))

y = 1;

else

y = -1;

上面的代码中gcc编译的指令会预先读取 y = -1 这条指令,这适合 x 的值大于 0 的概率比较小的情况。

如果 x 的值在大部分情况下是大于 0 的,就应该用 likely(x > 0),这样编译出的指令是预先读取 y = 1 这条指令了。这样系统在运行时就会减少重新取指了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: