"likely" keyword for D?

Peter Alexander peter.alexander.au at gmail.com
Wed Mar 23 00:30:57 PDT 2011


On 23/03/11 4:59 AM, %u wrote:
> I just thought of a (crazy) idea:
>
> Should D implement a "likely" keyword for if statements?
> Something like:
>
> if likely (x == 2)
> {
>      //do something
> }
>
> This would allow the compiler to generate branch prediction code for
> the program, allowing the programmer to prevent branch predictions.
>
> It's a crazy (and new?) idea... any thoughts?

Would be better as a compiler intrinsic instead of introducing a new 
keyword.

GCC uses __builtin_expect:

long __builtin_expect (long exp, long c)

if ( __builtin_expect(ptr != 0, 1) )
{
     // expect non-null
}

In this day and age it's rarely useful. Modern processors have branch 
prediction tables built in to them, so while you may be able to avoid 
the first branch misprediction, the CPU will probably predict the rest 
of them correctly anyway.


More information about the Digitalmars-d mailing list