Jan 23, 2016

[likely or unlikely] a easy misleading.

Reference:
Using likely() and unlikely()
Clang ignores branch predictor hints using __builtin_expect
#define likely(x) __builtin_expect ((x), 1)
#define unlikely(x) __builtin_expect ((x), 0)

The rule of the thumb is: Mark branch that you want to be executed quickly as "likely" and the other branch as "unlikely".
#ifdef FOO
#define likely(x)       __builtin_expect((x),1)
#define unlikely(x)     __builtin_expect((x),0)
#else
#define likely(x)       x
#define unlikely(x)     x
#endif
 
volatile int x,y,z;
int array[100];
 
// switch like
char const* b(int e) {
    if (likely(e == 0))
    {
        // for(int i=0; i<100;i++)
        //     array[i]=x;    
        return "0";
    }    
    else if (e == 1)
    {
        for(int i=0; i<100;i++)
            array[i]=y;
        return "1";
    }
    else
    {
        for(int i=0; i<100;i++)
            array[i]=z;
        return "f";
    }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.