views:

432

answers:

2

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

eg.

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

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

+3  A: 

__assume should be similar.

However, if you want to do this really well you should use Profile Guided Optimization rather than static hints.

Michael
I think this might be dangerous. According to Microsoft: "Because the compiler generates code based on __assume, that code might not run correctly if the expression inside the __assume statement is false at run time."
DigitalRoss
@Digital - Very true, the linked to MSDN article describes the pitfalls. Again, static hints like this should be avoided and you should try to use PGO if at all possible.
Michael
Good point. I won't argue with profiling.
DigitalRoss
+3  A: 

I say just punt

The only thing close is the semi-intrinsic __assume(), but don't use it, it is dangerous. You can get incorrect code generated if the expression turns out to be different.

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.

DigitalRoss