like,unlikely宏和GCC内建函数__builtin_expect()

在阅读GStreamer代码中有两个调用很频繁的宏:

#define G_LIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 1))
#define G_UNLIKELY(expr) (__builtin_expect (_G_BOOLEAN_EXPR(expr), 0))

其中 _G_BOOLEAN_EXPR宏的定义如下:

#define _G_BOOLEAN_EXPR(expr)                   /
__extension__ ({                               /
   int _g_boolean_var_;                         /
   if (expr)                                    /
      _g_boolean_var_ = 1;                      /
   else                                         /
      _g_boolean_var_ = 0;                      /
   _g_boolean_var_;                             /
})

其目的无非就是将!=0的数转化为1。而函数__builtin_expect查阅GCC手册,发现其定义如下:
long __builtin_expect (long exp, long c)                                    [Built-in Function]
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为你优化程式,将符合这个条件的分支放在合适的地方。
因为这个程式只提供了整型表达式,所以如果你要优化其他类型的表达式,能采用指针的形式。
说到底__builtin_expect函数就是为了优化可能性大的分支程式。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
__builtin_expectGCC编译器提供给程序员使用的一个指令,用于提供分支转移的信息给编译器,以便进行代码优化,减少指令跳转带来的性能下降。\[3\]一般使用的方法是将__builtin_expect指令封装为likely和unlikely,用于表示某个条件的可能性更大或更小。例如,likely(x)表示x的值为真的可能性更大,而unlikely(x)表示x的值为假的可能性更大。\[2\]通过在代码中使用likely和unlikely,编译器可以在编译过程中将可能性更大的代码紧跟在前面的代码,从而减少指令跳转带来的性能下降。\[3\]这样的优化可以提高程序的执行效率。 #### 引用[.reference_title] - *1* [__builtin_xxx指令学习【1】__builtin_expect](https://blog.csdn.net/qq_42604176/article/details/130031135)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【编程基础の基础】__builtin_expect详解(汇编级解释)](https://blog.csdn.net/weixin_42157432/article/details/115805804)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值