9 #define HAS_BUILTIN __has_builtin
11 #define HAS_BUILTIN(x) (0)
15 #define force_inline [[gnu::always_inline]] inline
16 #elif defined(_MSC_VER)
17 #define force_inline __forceinline
19 #define force_inline inline
22 /* Unlike the likely attribute, ASSUME requires the condition to be true or
23 * else it invokes undefined behavior. It's essentially an assert without
24 * actually checking the condition at run-time, allowing for stronger
25 * optimizations than the likely attribute.
27 #if HAS_BUILTIN(__builtin_assume)
28 #define ASSUME __builtin_assume
29 #elif defined(_MSC_VER)
30 #define ASSUME __assume
31 #elif __has_attribute(assume)
32 #define ASSUME(x) [[assume(x)]]
33 #elif HAS_BUILTIN(__builtin_unreachable)
34 #define ASSUME(x) do { if(x) break; __builtin_unreachable(); } while(0)
36 #define ASSUME(x) ((void)0)
42 constexpr std::underlying_type_t
<T
> to_underlying(T e
) noexcept
43 { return static_cast<std::underlying_type_t
<T
>>(e
); }
45 [[noreturn
]] inline void unreachable()
47 #if HAS_BUILTIN(__builtin_unreachable)
48 __builtin_unreachable();
54 template<std::size_t alignment
, typename T
>
55 force_inline
constexpr auto assume_aligned(T
*ptr
) noexcept
57 #ifdef __cpp_lib_assume_aligned
58 return std::assume_aligned
<alignment
,T
>(ptr
);
59 #elif HAS_BUILTIN(__builtin_assume_aligned)
60 return static_cast<T
*>(__builtin_assume_aligned(ptr
, alignment
));
61 #elif defined(_MSC_VER)
62 constexpr std::size_t alignment_mask
{(1<<alignment
) - 1};
63 if((reinterpret_cast<std::uintptr_t>(ptr
)&alignment_mask
) == 0)
67 __assume_aligned(ptr
, alignment
);
76 #endif /* OPTHELPERS_H */