如何提示Visual C ++编译器优化程序,if语句的特定分支不太可能执行? [英] How to hint to Visual C++ compiler optimizer that a specific branch of an if-statement is unlikely to be executed?

查看:207
本文介绍了如何提示Visual C ++编译器优化程序,if语句的特定分支不太可能执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用于错误检查的宏,如下所示:

  #define CheckCondition(x)\ 
if(x){\
// okay,do nothing \
} else {\
CallFunctionThatThrowsException(); \
}

,通常条件必须 true ,我们希望CPU分支预测总是选择这个路径,如果它恰好是 false ,我们真的不在乎



根据CPU硬核描述,分支预测将会稍微不同地处理前向跳跃和后向跳跃(类似于总是执行向后跳转并且从不执​​行向前跳转),并且编译器可以通过生成将向CPU分支预测器给予权利提示的代码来改进分支预测。



gcc似乎有 可能不太可能提示。在Visual C ++中有什么类似的东西吗?可 __假设关键字


解决方案

不幸的是,根据他们的开发人员中心



这是非常令人沮丧的,因为我们希望在几个情况下使用它,其中等效的GCC内在已经在内部循环中节省了几个微秒,但是最接近的是交换if和else子句使得更可能的情况是在不向前跳转的分支中。


We have a macro for error-checking that goes like this:

#define CheckCondition( x ) \
    if( x ) { \
    //okay, do nothing \
    } else { \
       CallFunctionThatThrowsException(); \
    }

and normally the condition has to be true and we'd like the CPU branch prediction to always select this path, and if it happens to be false we don't really care of a misprediction - throwing an exception and massive stack unwinding will cost a fortune anyway.

According to CPU hardcore descriptions branch prediction will treat forward jumps and backward jumps slightly differently (something like a backward jump is always performed and a forward jump is never performed) and the compiler could improve branch prediction by generating code that will give right hints to the CPU branch predictor.

gcc seems to have likely and unlikely hints for that. Is there anything like that in Visual C++? Can __assume keyword be used for that?

解决方案

Not in MSVC, unfortunately, according to their developer center.

It's very frustrating because we'd like to use it in a couple of cases where the equivalent GCC intrinsic has saved us a critical few microseconds in inner loops, but the closest we can get is to swap the if and else clauses so that the more likely case is in the forward-jump-not-taken branch.

这篇关于如何提示Visual C ++编译器优化程序,if语句的特定分支不太可能执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆