免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: zhangsuozhu
打印 上一主题 下一主题

linux应用层里有likely()、unlikely() 之类的函数吗? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2010-08-16 10:19 |只看该作者
做了二个程序:
1.

  1. #define likely(x) __builtin_expect (!!(x), 1)
  2. #define unlikely(x) __builtin_expect (!!(x), 0)
  3. int main(int argc, char *argv[])
  4. {
  5.     int i=0;
  6.     if (likely(argc == 3))
  7.         i ++;
  8.     return i;
  9. }
复制代码
2.

  1. int main(int argc, char *argv[])
  2. {
  3.     int i=0;
  4.     if (argc == 3)
  5.         i ++;
  6.     return i;
  7. }
复制代码
分别用gcc -S  -Wall  生成汇编指令。得到如下结果
1.

  1.         .file        "likely.c"
  2.         .text
  3. .globl main
  4.         .type        main, @function
  5. main:
  6.         leal        4(%esp), %ecx
  7.         andl        $-16, %esp
  8.         pushl        -4(%ecx)
  9.         pushl        %ebp
  10.         movl        %esp, %ebp
  11.         pushl        %ecx
  12.         subl        $16, %esp
  13.         movl        $0, -8(%ebp)
  14.         cmpl        $3, (%ecx)
  15.         sete        %al
  16.         movzbl        %al, %eax
  17.         testl        %eax, %eax
  18.         je        .L2
  19.         addl        $1, -8(%ebp)
  20. .L2:
  21.         movl        -8(%ebp), %eax
  22.         addl        $16, %esp
  23.         popl        %ecx
  24.         popl        %ebp
  25.         leal        -4(%ecx), %esp
  26.         ret
  27.         .size        main, .-main
  28.         .ident        "GCC: (Debian 4.3.2-1.1) 4.3.2"
  29.         .section        .note.GNU-stack,"",@progbits
复制代码
2.

  1.         .file        "likely1.c"
  2.         .text
  3. .globl main
  4.         .type        main, @function
  5. main:
  6.         leal        4(%esp), %ecx
  7.         andl        $-16, %esp
  8.         pushl        -4(%ecx)
  9.         pushl        %ebp
  10.         movl        %esp, %ebp
  11.         pushl        %ecx
  12.         subl        $16, %esp
  13.         movl        $0, -8(%ebp)
  14.         cmpl        $3, (%ecx)
  15.         jne        .L2
  16.         addl        $1, -8(%ebp)
  17. .L2:
  18.         movl        -8(%ebp), %eax
  19.         addl        $16, %esp
  20.         popl        %ecx
  21.         popl        %ebp
  22.         leal        -4(%ecx), %esp
  23.         ret
  24.         .size        main, .-main
  25.         .ident        "GCC: (Debian 4.3.2-1.1) 4.3.2"
  26.         .section        .note.GNU-stack,"",@progbits
复制代码
看不懂汇编,请大虾指点。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP