5 #define HAS_BUILTIN __has_builtin
7 #define HAS_BUILTIN(x) (0)
10 #if defined(__GNUC__) || HAS_BUILTIN(__builtin_expect)
11 /* likely() optimizes for the case where the condition is true. The condition
12 * is not required to be true, but it can result in more optimal code for the
13 * true path at the expense of a less optimal false path.
16 constexpr bool likely(T
&& expr
) noexcept
17 { return __builtin_expect(static_cast<bool>(expr
), true); }
18 /* The opposite of likely(), optimizing for the case where the condition is
22 constexpr bool unlikely(T
&& expr
) noexcept
23 { return __builtin_expect(static_cast<bool>(expr
), false); }
28 constexpr bool likely(T
&& expr
) noexcept
{ return static_cast<bool>(expr
); }
30 constexpr bool unlikely(T
&& expr
) noexcept
{ return static_cast<bool>(expr
); }
32 #define LIKELY(x) (likely(x))
33 #define UNLIKELY(x) (unlikely(x))
35 #if HAS_BUILTIN(__builtin_assume)
36 /* Unlike LIKELY, ASSUME requires the condition to be true or else it invokes
37 * undefined behavior. It's essentially an assert without actually checking the
38 * condition at run-time, allowing for stronger optimizations than LIKELY.
40 #define ASSUME __builtin_assume
41 #elif defined(_MSC_VER)
42 #define ASSUME __assume
43 #elif defined(__GNUC__)
44 #define ASSUME(x) do { if(!(x)) __builtin_unreachable(); } while(0)
46 #define ASSUME(x) ((void)0)
49 #if __cplusplus >= 201703L || defined(__cpp_if_constexpr)
50 #define if_constexpr if constexpr
52 #define if_constexpr if
55 #endif /* OPTHELPERS_H */