Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 926
Franck Freiburger Avatar asked Sep 17 '09 18:09

Franck Freiburger


People also ask

Is GCC faster than Msvc?

GCC is a fine compiler, and can produce code that has pretty much the same performance, if not better, than MSVC.

What compiler does Msvc use?

Microsoft C++ Compiler (MSVC) This is the default compiler for most Visual Studio C++ projects and is recommended if you are targeting Windows.

What is __ Builtin_expect?

You can use the __builtin_expect built-in function to indicate that an expression is likely to evaluate to a specified value. The compiler can use this knowledge to direct optimizations. This built-in function is portable with the GNU C/C++ __builtin_expect function.

Is Msvc a compiler?

MSVC (Microsoft Visual C++ Compiler) is a C++ compiler that is installed with Microsoft Visual Studio. Nim is the Nim Compiler for Windows, Linux, and macOS. QCC is the interface for compiling C++ applications for QNX.


1 Answers

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.

like image 161
Pauli Nieminen Avatar answered Sep 22 '22 13:09

Pauli Nieminen