diff options
Diffstat (limited to 'common/opthelpers.h')
-rw-r--r-- | common/opthelpers.h | 36 |
1 files changed, 11 insertions, 25 deletions
diff --git a/common/opthelpers.h b/common/opthelpers.h index f110303e..5fe455da 100644 --- a/common/opthelpers.h +++ b/common/opthelpers.h @@ -19,41 +19,27 @@ #define force_inline inline #endif -#if defined(__GNUC__) || HAS_BUILTIN(__builtin_expect) -/* likely() optimizes for the case where the condition is true. The condition - * is not required to be true, but it can result in more optimal code for the - * true path at the expense of a less optimal false path. - */ -template<typename T> -force_inline constexpr bool likely(T&& expr) noexcept -{ return __builtin_expect(static_cast<bool>(std::forward<T>(expr)), true); } -/* The opposite of likely(), optimizing for the case where the condition is - * false. - */ -template<typename T> -force_inline constexpr bool unlikely(T&& expr) noexcept -{ return __builtin_expect(static_cast<bool>(std::forward<T>(expr)), false); } - +#if __has_attribute(likely) +#define allikely likely +#define alunlikely unlikely #else - -template<typename T> -force_inline constexpr bool likely(T&& expr) noexcept -{ return static_cast<bool>(std::forward<T>(expr)); } -template<typename T> -force_inline constexpr bool unlikely(T&& expr) noexcept -{ return static_cast<bool>(std::forward<T>(expr)); } +#define allikely +#define alunlikely #endif -#define LIKELY(x) (likely(x)) -#define UNLIKELY(x) (unlikely(x)) -#if HAS_BUILTIN(__builtin_assume) +#define LIKELY(x) (x) [[allikely]] +#define UNLIKELY(x) (x) [[alunlikely]] + /* Unlike LIKELY, ASSUME requires the condition to be true or else it invokes * undefined behavior. It's essentially an assert without actually checking the * condition at run-time, allowing for stronger optimizations than LIKELY. */ +#if HAS_BUILTIN(__builtin_assume) #define ASSUME __builtin_assume #elif defined(_MSC_VER) #define ASSUME __assume +#elif __has_attribute(assume) +#define ASSUME(x) [[assume(x)]] #elif defined(__GNUC__) #define ASSUME(x) do { if(x) break; __builtin_unreachable(); } while(0) #else |