From d909f1819529a922ecac08aa0afc53cc68bdc999 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 11 Sep 2016 13:07:54 +0900 Subject: [PATCH] Common: Add likely() and unlikely() macros to hint likeliness of taking a branch. The compiled can use these hints to generate the code closer or further from the entry point depending on likeliness of taking a branch, this helps when entering the function without prior branch prediction, and on CPUs without proper branch prediction. --- src/common/common_funcs.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/common/common_funcs.h b/src/common/common_funcs.h index 4633897ce..8d40daded 100644 --- a/src/common/common_funcs.h +++ b/src/common/common_funcs.h @@ -29,6 +29,14 @@ #define FORCE_INLINE inline __attribute__((always_inline)) #endif +#if defined(__GNUC__) + #define likely(x) __builtin_expect(!!(x), 1) + #define unlikely(x) __builtin_expect(!!(x), 0) +#else + #define likely(x) (x) + #define unlikely(x) (x) +#endif + #ifndef _MSC_VER #ifdef ARCHITECTURE_x86_64