visual-studiogccoptimizationlikely-unlikely

likely/unlikely equivalent for MSVC


GCC compiler supports __builtin_expect statement that is used to define likely and unlikely macros.

eg.

#define likely(expr)    (__builtin_expect(!!(expr), 1))
#define unlikely(expr)  (__builtin_expect(!!(expr), 0))

Is there an equivalent statement for the Microsoft Visual C compiler, or something equivalent ?


Solution

  • I say just punt

    There is nothing like it. There is __assume(), but don't use it, it's a different kind of optimizer directive.

    Really, the reason the gnu builtin is wrapped in a macro is so you can just get rid of it automatically if __GNUC__ is not defined. There isn't anything the least bit necessary about those macros and I bet you will not notice the run time difference.

    Summary

    Just get rid of (null out) *likely on non-GNU. You won't miss it.