Make a couple more operator bools explicit
[openal-soft.git] / common / opthelpers.h
blob0b8b821048ea6b87621d699cdaa06cdad076fb39
1 #ifndef OPTHELPERS_H
2 #define OPTHELPERS_H
4 #ifdef __has_builtin
5 #define HAS_BUILTIN __has_builtin
6 #else
7 #define HAS_BUILTIN(x) (0)
8 #endif
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.
15 template<typename T>
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
19 * false.
21 template<typename T>
22 constexpr bool unlikely(T&& expr) noexcept
23 { return __builtin_expect(static_cast<bool>(expr), false); }
25 #else
27 template<typename T>
28 constexpr bool likely(T&& expr) noexcept { return static_cast<bool>(expr); }
29 template<typename T>
30 constexpr bool unlikely(T&& expr) noexcept { return static_cast<bool>(expr); }
31 #endif
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)
45 #else
46 #define ASSUME(x) ((void)0)
47 #endif
49 #if __cplusplus >= 201703L || defined(__cpp_if_constexpr)
50 #define if_constexpr if constexpr
51 #else
52 #define if_constexpr if
53 #endif
55 #endif /* OPTHELPERS_H */