likely/unlikely equivalent for MSVC

[[likely]]
__builtin_expect
gcc likely
clang likely
__builtin_unreachable msvc
gcc branch prediction
builtin unpredictable
branch prediction c++

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 ?


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.

"likely"/"unlikely" builtins, In MSVC, the equivalent is to put the unlikely case as "else" of the "if" statement. In my use-case, using PGO isn't very feasible due to inherent non-determinism of the whole process, as well as the need to abstract away some IO devices used in order to do tests in the first place. In MSVC, the equivalent is to put the unlikely case as "else" of the "if" statement. In my use-case, using PGO isn't very feasible due to inherent non-determinism of the whole process, as well as the need to abstract away some IO devices used in order to do tests in the first place.


According to http://www.akkadia.org/drepper/cpumemory.pdf (page 57), it still makes sense to use static branch prediction even if CPU predicts correctly dynamically. The reason for that is that L1i cache will be used even more efficiently if static prediction was done right.

likely/unlikely equivalent for MSVC, likely/unlikely equivalent for MSVC. Question. GCC compiler supports __​builtin_expect statement that is used to define likely and unlikely macros. eg. véase también BOOST_LIKELY y BOOST_UNLIKELY; todos los links están muertos. Otra respuesta: *el argumento para La no aplicación de esta característica es que no es estándar. MSVC está impulsando la implementación de normas características, no extender el lenguaje en formas que son incompatibles con otros compiladores. (Hemos hecho


C++20 standard will include [[likely]] and [[unlikely]] branch prediction attributes.

The latest revision of attribute proposal can be found from http://wg21.link/p0479

The original attribute proposal can be found from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0479r0.html

Programmers should prefer PGO. Attributes can easily reduce performance if applied incorrectly or they later become incorrect when program changes.

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  likely gcc prediction msvc clang branch likely/unlikely macros in the Linux kernel works and what is their benefit? likely/unlikely equivalent for MSVC


__assume should be similar.

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

C++ attribute: likely, unlikely, GCC compiler supports __builtin_expect statement that is used to define likely and unlikely macros. eg. 1) Applies to a statement to allow the compiler to optimize for the case where paths of execution including that statement are more likely than any alternative path of execution that does not include such a statement.


According to Branch and Loop Reorganization to Prevent Mispredicts document from Intel:

In order to effectively write your code to take advantage of these rules, when writing if-else or switch statements, check the most common cases first and work progressively down to the least common.

Unfortunately you cannot write something like

#define if_unlikely(cond) if (!(cond)); else 

because MSVC optimizer as of VS10 ignores such "hint".

As I prefer to deal with errors first in my code, I seem to write less efficient code. Fortunately, second time CPU encounters the branch it will use its statistics instead of a static hint.

Branch Prediction and MSVC, Allow the compiler to optimize for the case where paths of execution including that statement are more or less likely than any alternative path of  Not in MSVC, unfortunately, according to their developer center. It's very frustrating because we'd like to use it in a couple of cases where the equivalent GCC intrinsic has saved us a critical few microseconds in inner loops, but the closest we can get is to swap the if and else clauses so that the more likely case is in the forward-jump-not-taken branch.


Microsoft C++ language conformance table, The GNU compiler provides a few keywords (__unlikely and __likely) that MSVC Has equivalent functionality - check MSDN for "__assume". This question is about C++20's [[likely]]/[[unlikely]] feature, not compiler-defined macros. This documents (cppreference) only gave an example on applying them to a switch-case statement. This sw


likely, Table of Microsoft C++ conformance updates by Visual Studio version. P0479R5 [[likely]] and [[unlikely]] attributes, No. P0634R3 Down with VS 2017 15.5. P0307R2 Making Optional Greater Equal Again, VS 2017 15.0. These macros communicate to the compiler that the conditional expression X is likely or unlikely to yield a positive result. The expression should result in a boolean value. The result of the macro is an integer or boolean value equivalent to the result of X.


How does the compiler optimize for the likely and unlikely directives? L2 — Jump if less than equal to. If the use of the likely and unlikely macros seemed a bit ad hoc, watch out for the C++ 20 [[likely]] and [[unlikely]]  Use C++20 attributes [[likely]] and [[unlikely]]. The thing is, MSVC seems to implement it in one of the preview releases. But existing macro cannot be able to make use of it, so it needs a rewrite