GCC __builtin_expect 解析

12 篇文章 0 订阅

概念

摘自gcc的官方文档OtherBuildinFunction.

— Built-in Function: 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.

我们可以使用 __buildin_except 向编译器提供分支预测信息,从而帮助编译器进行代码优化。

引入原因

CPU 流水线技术可以提高CPU执行效率,但是程序中的跳转指令会打乱CPU流水线。因此,跳转次数少的程序拥有更高的执行效率。

示例

使用__buildin_except 定义LIKELYUNLIKELY宏,分别代表bool型变量或表达式有很大可能性为真或者很大可能性为假。以下是测试代码。

// 两个感叹号的作用是将所有的非零值转化为1
#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)

int likely(int x)
{
    if(LIKELY(x))
    {
        x = 5;
    }
    else
    {
        x = 6;
    }

    return x;
}

int unlikely(int x)
{
    if(UNLIKELY(x))
    {
        x = 5;
    }
    else
    {
        x = 6;
    }

    return x;
}

int normal(int x)
{
    if(x)
    {
        x = 5;
    }
    else
    {
        x = 6;
    }

    return x;
}

编译出.o文件,并使用objdump查看汇编代码

gcc -O2 -fprofile-arcs -c test_builtin_except.cpp
objdump -d test_builtin_except.o

结果以及解释如下图
avatar

总结

Linux 内核中大量使用LIKELY UNLIKELY宏提升程序运行效率,在C/C++工程可以引入此宏提供分支预测提示编译器进行代码优化。比如在工程中经常会存在处理程序错误的分支,但是出错分支又是不经常进入,这种场景下可以使用__build_except进行代码优化。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值