网站建设外包质量进度跟进,wordpress如何设置友情链接,商用图片的网站,注册一个新公司的流程如下看内核时总遇到if(likely( )){}或是if(unlikely( ))这样的语句#xff0c;最初不解其意#xff0c;现在有所了解#xff0c;所以也想介绍一下。likely() 与 unlikely()是内核#xff08;我看的是2.6.22.6版本#xff0c;2.6的版本应该都有#xff09;中定义的两个宏。位于…看内核时总遇到if(likely( )){}或是if(unlikely( ))这样的语句最初不解其意现在有所了解所以也想介绍一下。likely() 与 unlikely()是内核我看的是2.6.22.6版本2.6的版本应该都有中定义的两个宏。位于/include/linux/compiler.h中具体定义如下#define likely(x) __builtin_expect(!!(x), 1)#define unlikely(x) __builtin_expect(!!(x), 0)__builtin_expect是gcc版本2.96网上写的我没验证过中提供的一个预处理命令这个名词也是网上写的我想叫函数更好些有利于代码优化。gcc(version 4.4.0)具体定义如下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.它的意思是我们可以使用这个函数人为告诉编绎器一些分支预测信息“expc” 是“很可能发生的”。#define likely(x) __builtin_expect(!!(x), 1)也就是说明x1是“经常发生的”或是“很可能发生的”。使用likely 执行if后面语句的可能性大些编译器将if{}是的内容编译到前面, 使用unlikely 执行else后面语句的可能性大些,编译器将else{}里的内容编译到前面。这样有利于cpu预取,提高预取指令的正确率,因而可提高效率。举个例子(内核版本2.6.22.6)/kernel/shed.c中有一段if (likely(!active_balance)) {/* We were unbalanced, so reset the balancing interval */sd-balance_interval sd-min_interval;} else {/** If weve begun active balancing, start to back off. This* case may not be covered by the all_pinned logic if there* is only 1 task on the busy runqueue (because we dont call* move_tasks).*/if (sd-balance_interval max_interval)sd-balance_interval * 2;}编译过程中会将if后面{}里的内容编译到前面else 后面{}里的内容编译到后面。若将likely换成unlikely 则正好相反。总之,likely与unlikely互换或不用都不会影响程序的正确性。但可能会影响程序的效率。 if(likely(foo)) //认为foo通常为1 if(unlikely(foo)) //认为foo通常为0 感谢各位光顾不知道有没有写清楚望指正疑惑为什么likely或是unlikely要定义成__builtin_expect(!!(x), 1)而不直接用__builtin_expect(x, 1) !!(x) 与 x 有什么不同另外内核2.6.31.5中likely和unlikely还有一种定义:# ifndef likely# define likely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 1))# endif# ifndef unlikely# define unlikely(x) (__builtin_constant_p(x) ? !!(x) : __branch_check__(x, 0))# endif