如何提示gcc编译器更可能的分支 [英] how to suggest gcc compiler more probable branch
问题描述
if(almost_always_false_condition){
//做某事
}
有没有一种方法可以建议编译器在99%的条件下是错误的。
条件计算需要60个周期来检查,编译器本身无法在编译时计算。
(gcc 4.3)
如果您想向GCC建议某个条件可能具有给定值作为安排代码流的提示,则应该使用 __ builtin_expect()
:
if(__builtin_expect(almost_always_false_condition,0)){
//做某事
}
然而,这听起来像是你想找到避免评估条件的方法,其中 __ builtin_expect()
不会执行。有没有一种方法可以快速逼近条件,并且只有在近似值为真时才进行全面检查:
if( __builtin_expect(fastCheckThatIsTrueIfFullConditionIsTrue,0)){
//大部分时间,我们甚至都没有到达这里,所以你不需要
//来评估条件
if( almostAlwaysFalseCondition){
//做某事
}
}
你能告诉我们更多关于什么情况吗?
Example:
if (almost_always_false_condition) {
// do something
}
Is there a way to suggest compiler that in 99% condition will be false. The condition calculation takes ~60 cycles to be checked, and it cannot be calculated at compile time by compiler itself.
(gcc 4.3)
If you want to suggest to GCC that a condition is likely to have a given value as a hint for arranging code flow, you should use __builtin_expect( )
:
if (__builtin_expect(almost_always_false_condition,0)) {
// do something
}
However, it sounds like you want to find a way to avoid evaluating the condition, which __builtin_expect( )
won't do. Is there a way that you can quickly approximate the condition, and only do the full check when the approximation is true:
if (__builtin_expect(fastCheckThatIsTrueIfFullConditionIsTrue,0)) {
// most of the time, we don't even get to here, so you don't need
// to evaluate the condition
if (almostAlwaysFalseCondition) {
// do something
}
}
Can you tell us more about what the condition is?
这篇关于如何提示gcc编译器更可能的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!