jjww 发表于 2003-10-30 21:41:47

unlikely(x)怎么理解?

include/linux/compiler.h

/*
* 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)

以一个具体调用为例,
if (unlikely(skb->data<skb->head))
       skb_under_panic(skb, len, current_text_addr());
我不明白的是为什么需要unlikely?如果不用这个宏,会有什么问题?
谢谢!!

Dragonfly 发表于 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?:-D

jjww 发表于 2003-10-31 11:05:35

thanks, i think you are right.:P
页: [1]
查看完整版本: unlikely(x)怎么理解?