来自GCC的__builtin_expect概率

osg*_*sgx 0 gcc built-in prediction optimizer-hints

__builtin_expect来自GCC的程序员可以使用它来显示哪些变体经常出现且很少见.但 __builtin_expect只有"真实"和"假"(0%或100%概率)

对于一些大型项目来说,获取个人资料反馈很难变化(-fprofile-arcs),有时程序员确实知道,他在程序的某些方面有多大的分支概率.

可以给编译器一个提示,即分支的概率> 0%且<100%?

MCC*_*CCS 6

这里

__builtin_expect_with_probability

(long exp, long c, double probability) The function has the same semantics as __builtin_expect, but caller provides the expected probability that exp == c. Last argument, probability, is a floating-value in the inclusive range 0.0f and 1.0f. The probability argument must be constant floating-point expression.

Jesin pointed out in the comments, Clang 11 has it too.


Eug*_*ith 5

真与假的确意味着"第一种变体更有可能","第二种变体更有可能".除了这些之外,没有实际需要任何其他值.编译器将无法使用该信息.

  • “编译器将无法使用该信息”。我强烈不同意。在编译器中,更细粒度的信息在很多地方都很有价值(分支预测、寄存器分配、向量化……)。例如,LLVM 可以直接在 IR 中表示频率信息,尽管据我所知这不会向用户公开(http://llvm.org/docs/BlockFrequencyTerminology.html)。 (4认同)