9 #define HAS_BUILTIN __has_builtin
11 #define HAS_BUILTIN(x) (0)
14 #ifdef __has_cpp_attribute
15 #define HAS_ATTRIBUTE __has_cpp_attribute
17 #define HAS_ATTRIBUTE(x) (0)
21 #define force_inline [[gnu::always_inline]] inline
22 #define NOINLINE [[gnu::noinline]]
23 #elif defined(_MSC_VER)
24 #define force_inline __forceinline
25 #define NOINLINE __declspec(noinline)
27 #define force_inline inline
31 #if defined(__MINGW32__) && defined(__i386__)
32 /* 32-bit MinGW targets have a bug where __STDCPP_DEFAULT_NEW_ALIGNMENT__
33 * reports 16, despite the default operator new calling standard malloc which
34 * only guarantees 8-byte alignment. As a result, structs that need and specify
35 * 16-byte alignment only get 8-byte alignment. Explicitly specifying 32-byte
36 * alignment forces the over-aligned operator new to be called, giving the
37 * correct (if larger than necessary) alignment.
39 * Technically this bug affects 32-bit GCC more generally, but typically only
40 * with fairly old glibc versions as newer versions do guarantee the 16-byte
41 * alignment as specified. MinGW is reliant on msvcrt.dll's malloc however,
42 * which can't be updated to give that guarantee.
44 #define SIMDALIGN alignas(32)
49 /* Unlike the likely attribute, ASSUME requires the condition to be true or
50 * else it invokes undefined behavior. It's essentially an assert without
51 * actually checking the condition at run-time, allowing for stronger
52 * optimizations than the likely attribute.
54 #if HAS_BUILTIN(__builtin_assume)
55 #define ASSUME __builtin_assume
56 #elif defined(_MSC_VER)
57 #define ASSUME __assume
58 #elif __has_attribute(assume)
59 #define ASSUME(x) [[assume(x)]]
60 #elif HAS_BUILTIN(__builtin_unreachable)
61 #define ASSUME(x) do { if(x) break; __builtin_unreachable(); } while(0)
63 #define ASSUME(x) (static_cast<void>(0))
66 /* This shouldn't be needed since unknown attributes are ignored, but older
67 * versions of GCC choke on the attribute syntax in certain situations.
69 #if HAS_ATTRIBUTE(likely)
70 #define LIKELY [[likely]]
71 #define UNLIKELY [[unlikely]]
80 constexpr std::underlying_type_t
<T
> to_underlying(T e
) noexcept
81 { return static_cast<std::underlying_type_t
<T
>>(e
); }
83 [[noreturn
]] inline void unreachable()
85 #if HAS_BUILTIN(__builtin_unreachable)
86 __builtin_unreachable();
92 template<std::size_t alignment
, typename T
>
93 force_inline
constexpr auto assume_aligned(T
*ptr
) noexcept
95 #ifdef __cpp_lib_assume_aligned
96 return std::assume_aligned
<alignment
,T
>(ptr
);
97 #elif HAS_BUILTIN(__builtin_assume_aligned)
98 return static_cast<T
*>(__builtin_assume_aligned(ptr
, alignment
));
99 #elif defined(_MSC_VER)
100 constexpr std::size_t alignment_mask
{(1<<alignment
) - 1};
101 if((reinterpret_cast<std::uintptr_t>(ptr
)&alignment_mask
) == 0)
105 __assume_aligned(ptr
, alignment
);
114 #endif /* OPTHELPERS_H */