What is the default probability of [[likely]]? Is it possible to change it?
Backgound: GCC has the following built-in functions:
long __builtin_expect(long exp, long c): the probability that a __builtin_expect expression is true is controlled by GCC's builtin-expect-probability parameter, which defaults to 90%.long __builtin_expect_with_probability(long exp, long c, double probability): the last argument, probability, is a floating-point value in the range 0.0 to 1.0, inclusive.What is the C++ definition of the term "likely"? 51%? 90%? What does the term "arbitrarily (un)likely" mean?
Nowadays, most architectures have pipelines. If the processor decodes a conditional jump (which if is translated to) earlier than the condition is evaluated, it's permitted to continue evaluation speculatively (without actually committing the result). [[likely]] and [[unlikely]] simply directs the compiler to generate code choosing the path to take in these speculative cases. I'm not aware of any architecture that has probabilities involved here, it's definitely evaluated that way (if speculative evaluation is possible).
Also, sometimes you might use these against probabilities. Say you have a code optimized for a specific processor. You are aware that on the actual evaluation path you'll use each and every cache line in the CPU and you've identified which conditions might cause a cache line to be loaded. In this case, you'll likely want to put [[likely]] on the path that doesn't load a new cache line, regardless of probabilities: causing speculative loading to load a new line (and therefore potentially drop a line you'd still use) likely costs more than simply planning for no new cache line loads. So it's really not about probabilities, but speculative path to be taken by CPU (until the condition is evaluated).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With