Preserving the Execution pipeline

半城伤御伤魂 提交于 2019-12-08 04:13:20

问题


Return types are frequently checked for errors. But, the code that will continue to execute may be specified in different ways.

if(!ret)
{
   doNoErrorCode();
}
exit(1);

or

if(ret)
{
   exit(1);
}
doNoErrorCode();

One way heavyweight CPU's can speculate about the branches taken in near proximity/locality using simple statistics - I studied a 4-bit mechanism for branch speculation (-2,-1,0,+1,+2) where zero is unknown and 2 will be considered a true branch.

Considering the simple technique above, my questions are about how to structure code. I assume that there must be a convention among major compilers and major architectures. These are my two questions

  1. When the code isn't an often-visited loop which boolean value is biased for when the pipeline is being filled ?
  2. Speculation about branching must begin at either true or false or zero ( the pipeline must be filled with something). Which is it likely to be ?

回答1:


The behavior varies among CPUs, and the compiler often reorders instructions. You will find all the information you need in these manuals: http://agner.org/optimize/.

In my opinion the only way to know what happens is to read the assembly code generated by the compiler.




回答2:


On gcc you can use __builtin_expect to provide the compiler with branch prediction information. To make it slightly easier you can then borrow the likely/unlikely macros used e.g. in the Linux kernel:

#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)

and then e.g.

if (unlikely(!some_function())
    error_handling();


来源:https://stackoverflow.com/questions/1833253/preserving-the-execution-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!