Chinaunix首页 | 论坛 | 博客
  • 博客访问: 189786
  • 博文数量: 76
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 831
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-31 00:52
文章分类

全部博文(76)

文章存档

2010年(58)

2009年(18)

我的朋友

分类:

2009-10-26 13:16:33

这两个宏用于优化代码的选跳判断,属于编译器优化类。

likely(x)    条件结果为真的可能性大

unlikely(x)    条件结果为真的可能性小

Compiler.h (d:\eric\linux\linux-2.6.26\linux-2.6.26\include\linux)    5579    2008-7-14

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

Refer from GCC manual:

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
semantics of the built-in are that it is expected that exp == c. For example:
Chapter 5: Extensions to the C Language Family 359
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.

阅读(537) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Linux Interrupt Handling

给主人留下些什么吧!~~