he gcc C compiler has a built-in directive that optimizes conditional branches as either very likely taken or very unlikely taken. The compiler uses the directive to appropriately optimize the branch. The kernel wraps the directive in very easy-to-use macros, likely() and unlikely().

For example, consider an if statement such as the following:

if (foo) {
/* ... */
}

To mark this branch as very unlikely taken (that is, likely not taken):

/* we predict foo is nearly always zero ... */
if (unlikely(foo)) {
/* ... */
}

Conversely, to mark a branch as very likely taken:

/* we predict foo is nearly always nonzero ... */
if (likely(foo)) {
/* ... */
}

You should only use these directives when the branch direction is overwhelmingly a known priori or when you want to optimize a specific case at the cost of the other case. This is an important point: These directives result in a performance boost when the branch is correctly predicted, but a performance loss when the branch is mispredicted. A very common usage for unlikely() and likely() is error conditions. As one might expect, unlikely() finds much more use in the kernel because if statements tend to indicate a special case.

from Linux Kernel Development Second Edition

---------------------------------------------------------------------------------------------

在linux中判断语句经常会看到likely和unlikely,例如:

if(likely(value)){
}
else{
}
 

简单从表面上看if(likely(value)) == if(value),if(unlikely(value)) == if(value)。

也就是likely和unlikely是一样的,但是实际上执行是不同的,加likely的意思是value的值为真的可能性更大一些,那么执行if的机会大,而unlikely表示value的值为假的可能性大一些,执行else机会大一些。加上这种修饰,编译成二进制代码时likely使得if后面的执行语句紧跟着前面的程序,unlikely使得else后面的语句紧跟着前面的程序,这样就会被cache预读取,增加程序的执行速度,likely和unlikely的实现在include/linux/compiler.h中:

      9 #if __GNUC__ == 2 && __GNUC_MINOR__ < 96 
     10 #define __builtin_expect(x, expected_value) (x) 
     11 #endif 
     12 
     13 #define likely(x)   __builtin_expect((x),1) 
     14 #define unlikely(x) __builtin_expect((x),0)

__builtin_expect是gcc的一个预处理命令,其解释如下:

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.

转自:http://www.cnblogs.com/yangzd/archive/2010/09/27/1837202.html

随机推荐

  1. 【Bootstrap】自己主动去适应PC、平面、手机Bootstrap网格系统

    酒吧格英语作为一门系统"grid systems",也有人翻译成"网络格系统".使用固定格子设计布局,其风格整齐而简洁,在二战结束后人气,流风格之中的一个. 1 ...

  2. 翻译器DIY————次序

    突然有一种冲动,想要写一个编译器. 因此,检查在网上搜索相关信息,思想direct3D 有本书叫龙,也有个龙书 Compilers Principles,Techniques, & Tool ...

  3. File类基本操作之OutputStream字节输出流

    贴代码了,已经測试,可正常编译 package org.mark.streamRW; import java.io.File; import java.io.FileOutputStream; imp ...

  4. Codeforces Beta Round #3 A. Shortest path of the king

    标题效果: 鉴于国际棋盘两点,寻求同意的操作,是什么操作的最小数量,在操作过程中输出. 解题思路: 水题一个,见代码. 以下是代码: #include <set> #include < ...

  5. Spring aop 小例子demo

    由于最近的服务项目提供接口有一个需求,所有操作都必须检查操作的服务可用,所以感觉Aop特别适合实施.完成学习的小例子. 关于spring-Aop原理:http://m.oschina.net/blog ...

  6. onethink和phpwind共享

    将onethink和phpwind数据库安装在一起.使用公用表前缀. 将onethink的member表点phpwind有user表 这是onethink在根文件夹的安装,phpwind安装在bbs的 ...

  7. php脚本生成google play url的下载链接,下载apk并自动反编译后获取android版本号

        需求:     get the offer tracking link    follow the redirect to get google play url    Go to http: ...

  8. href=&quot;javascript:void(0);&quot;与#差异

    将<a>标签设置为空链接有两种方式.第一个是href="#",第二个是href="javascript:void(0);". 两种方式都设置了标签为 ...

  9. SCM白色幼儿系列(十二) Proteus仿真软件简介

    Proteus软件是英国Labcenter electronics公司出版的EDA工具软件.经常使用于单片机等数字电路仿真,分为ISIS和ARES两个程序,前者用于仿真,后者用于设计PCB.我们常使用 ...

  10. Android适配方案小结(一)

    相关计量单位介绍: px:是屏幕的像素点,不同设备显示的效果一样. in:英寸(1英寸等于2.54cm) mm:毫米 pt:磅, 1/72英寸 dp:device independent pixels ...