免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1018 | 回复: 0
打印 上一主题 下一主题

Kernel: likely & unlikely [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-03 18:34 |只看该作者 |倒序浏览

                                最近看代码看到unlikely(),有点迷糊,特上网查了下,贴在此,留作备忘,也希望能帮到和我一样迷糊的朋友:)
------------------------------------------------------------------
Kernel : likely/unlikely macros
      
      
      
      
  
  
    Submitted by
Kedar Sovani
  on February 11, 2005 - 7:46am
  
  
Applications/tools

  
Ever wondered what the likely and unlikely macros in the linux kernel are ?
The macros are defined as :
#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)
The __builtin_expect is a method that gcc (versions >=
2.96) offer for programmers to indicate branch prediction information
to the compiler. The return value of __builtin_expect is the first
argument (which could only be an integer) passed to it.
To check it out how it could be beneficial, an excerpt from "info gcc" :
     if (__builtin_expect (x, 0))
                foo ();
     [This] would indicate that we do not expect to call `foo', since we
     expect `x' to be zero.
Based on this information the compiler generates intelligent code, such that the most expected result is favored.
Let us consider it with a simple example function :
[kedar@ashwamedha ~]$ cat abc.c
int
testfun(int x)
{
        if(__builtin_expect(x, 0)) {
                              ^^^--- We instruct the compiler, "else" block is more probable
                x = 5;
                x = x * x;
        } else {
                x = 6;
        }
        return x;
}

[kedar@ashwamedha ~]$ gcc -O2 -c abc.c
[kedar@ashwamedha ~]$ objdump  -d abc.o

abc.o:     file format elf32-i386

Disassembly of section .text:

00000000 :
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 45 08                mov    0x8(%ebp),%eax
   6:   85 c0                   test   %eax,%eax
   8:   75 07                   jne    11
                                ^^^ --- The compiler branches the "if" block and keeps "else" sequential
   a:   b8 06 00 00 00          mov    $0x6,%eax
   f:   c9                      leave
  10:   c3                      ret
  11:   b8 19 00 00 00          mov    $0x19,%eax
  16:   eb f7                   jmp    f
And let us see what happens if we make the "if" block more likely.
[kedar@ashwamedha ~]$ cat abc.c
int
testfun(int x)
{
        if(__builtin_expect(x, 1)) {
                              ^^^ --- We instruct the compiler, "if" block is more probable
                x = 5;
                x = x * x;
        } else {
                x = 6;
        }
        return x;
}
                                                                                                   
[kedar@ashwamedha ~]$ gcc -O2 -c abc.c
[kedar@ashwamedha ~]$ objdump  -d abc.o
                                                                                                   
abc.o:     file format elf32-i386
                                                                                                   
Disassembly of section .text:
                                                                                                   
00000000 :
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 45 08                mov    0x8(%ebp),%eax
   6:   85 c0                   test   %eax,%eax
   8:   74 07                   je     11
                               ^^^ --- The compiler branches the "else" block and keeps "if" sequential
   a:   b8 19 00 00 00          mov    $0x19,%eax
   f:   c9                      leave
  10:   c3                      ret
  11:   b8 06 00 00 00          mov    $0x6,%eax
  16:   eb f7                   jmp    f
-------------------------------------------------------------
从上面标红的地方我们可以看到,其实likely和unlikely的作用只是用于编译器进行优化而已。而我们正常阅读中,知道likely里说的是大多数情况下都会发生的事,而unlikely里说的是大多数情况下不会发生的情况,就可以了。即 if(a)跟if(likely(a))和if(unlikely(a))所得的结果是一样的,值是相互等价的。
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/83905/showart_2110512.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP