Enable proper full C++ exception handling on MSVC
[openal-soft.git] / common / opthelpers.h
blobfff881545156c174f97e67bdf77e58c613e456b0
1 #ifndef OPTHELPERS_H
2 #define OPTHELPERS_H
4 #include <cstdint>
5 #include <utility>
6 #include <memory>
8 #ifdef __has_builtin
9 #define HAS_BUILTIN __has_builtin
10 #else
11 #define HAS_BUILTIN(x) (0)
12 #endif
14 #ifdef __has_cpp_attribute
15 #define HAS_ATTRIBUTE __has_cpp_attribute
16 #else
17 #define HAS_ATTRIBUTE(x) (0)
18 #endif
20 #ifdef __GNUC__
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)
26 #else
27 #define force_inline inline
28 #define NOINLINE
29 #endif
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)
45 #else
46 #define SIMDALIGN
47 #endif
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)
62 #else
63 #define ASSUME(x) (static_cast<void>(0))
64 #endif
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]]
72 #else
73 #define LIKELY
74 #define UNLIKELY
75 #endif
77 namespace al {
79 template<typename T>
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();
87 #else
88 ASSUME(false);
89 #endif
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)
102 return ptr;
103 __assume(0);
104 #elif defined(__ICC)
105 __assume_aligned(ptr, alignment);
106 return ptr;
107 #else
108 return ptr;
109 #endif
112 } // namespace al
114 #endif /* OPTHELPERS_H */