QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1500|回复: 2

unlikely(x)怎么理解?

[复制链接]
发表于 2003-10-30 21:41:47 | 显示全部楼层 |阅读模式
include/linux/compiler.h
[code:1]
/*
* Generic compiler-dependent macros required for kernel
* build go below this comment. Actual compiler/compiler version
* specific implementations come from the above header files
*/

#define likely(x)        __builtin_expect(!!(x), 1)
#define unlikely(x)        __builtin_expect(!!(x), 0)
[/code:1]
以一个具体调用为例,
if (unlikely(skb->data<skb->head))
       skb_under_panic(skb, len, current_text_addr());
我不明白的是为什么需要unlikely?如果不用这个宏,会有什么问题?
谢谢!!
发表于 2003-10-31 02:19:24 | 显示全部楼层
this is because compiler want to do some optimizations.

for each if, there is a branch, cpu has branch predictor and try to choose one branch, if u know this rarely happen, then u can guide the cpu to choose a more possible branch. thus improve the performance and reduce the penalty of miss branch predictiooon.

am i right?  
回复

使用道具 举报

 楼主| 发表于 2003-10-31 11:05:35 | 显示全部楼层
thanks, i think you are right.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-9-28 04:16 , Processed in 0.065175 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表