diff options
Diffstat (limited to 'common/opthelpers.h')
-rw-r--r-- | common/opthelpers.h | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/common/opthelpers.h b/common/opthelpers.h index 5e6f54cf..0b8b8210 100644 --- a/common/opthelpers.h +++ b/common/opthelpers.h @@ -12,16 +12,22 @@ * 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. */ -constexpr bool likely(bool expr) { return __builtin_expect(expr, true); } +template<typename T> +constexpr bool likely(T&& expr) noexcept +{ return __builtin_expect(static_cast<bool>(expr), true); } /* The opposite of likely(), optimizing for the case where the condition is * false. */ -constexpr bool unlikely(bool expr) { return __builtin_expect(expr, false); } +template<typename T> +constexpr bool unlikely(T&& expr) noexcept +{ return __builtin_expect(static_cast<bool>(expr), false); } #else -constexpr bool likely(bool expr) { return expr; } -constexpr bool unlikely(bool expr) { return expr; } +template<typename T> +constexpr bool likely(T&& expr) noexcept { return static_cast<bool>(expr); } +template<typename T> +constexpr bool unlikely(T&& expr) noexcept { return static_cast<bool>(expr); } #endif #define LIKELY(x) (likely(x)) #define UNLIKELY(x) (unlikely(x)) |