"假定" gcc中的子句 [英] "Assume" clause in gcc
问题描述
__ assume()
built-in的承担条款? 例如
__ assume(n%8 == 0);
从gcc 4.8.2开始,gcc中没有__assume()的等价物。我不知道为什么 - 这将是非常有用的。 mafso建议:
$ b $ pre> #define __assume(cond)do {if(!(cond))__builtin_unreachable(); } while(0)
这是一个古老的技巧,至少早在2010年就已知,更长的时间。编译器通常会优化'cond'的评估,因为任何cond为false的评估都是不确定的。然而,如果它包含对不透明(非内联)函数的调用,它似乎并没有优化'cond'。编译器必须假定不透明的调用可能有副作用(例如,改变全局),并且不能优化掉呼叫,尽管它可以优化结果上的任何计算和分支。出于这个原因,最好的方法是采用宏观方法。
Does gcc (latest versions: 4.8, 4.9) have an "assume" clause similar to __assume()
built-in supported by icc?
E.g., __assume( n % 8 == 0 );
As of gcc 4.8.2, there is no equivalent of __assume() in gcc. I don't know why -- it would be very useful. mafso suggested:
#define __assume(cond) do { if (!(cond)) __builtin_unreachable(); } while (0)
This is an old trick, known at least as far back as 2010 and probably longer. The compiler usually optimizes out the evaluation of 'cond' because any evaluation for which cond is false would be undefined anyway. However, it does not seem to optimize away 'cond' if it contains a call to an opaque (non-inlined) function. The compiler must assume that the opaque call might have a side-effect (e.g., change a global) and cannot optimize away the call, although it could optimize away any computations and branches on the result. For this reason, the macro approach is a partial solution, at best.
这篇关于"假定" gcc中的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!