Handle Int16 and ADPCM formats in alstreamcb
[openal-soft.git] / common / opthelpers.h
blob95ff781aa4253adbb098a2e7e42fe212deb7d1f0
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 __GNUC__
15 #define force_inline [[gnu::always_inline]] inline
16 #elif defined(_MSC_VER)
17 #define force_inline __forceinline
18 #else
19 #define force_inline inline
20 #endif
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)
35 #else
36 #define ASSUME(x) ((void)0)
37 #endif
39 namespace al {
41 template<typename T>
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();
49 #else
50 ASSUME(false);
51 #endif
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)
64 return ptr;
65 __assume(0);
66 #elif defined(__ICC)
67 __assume_aligned(ptr, alignment);
68 return ptr;
69 #else
70 return ptr;
71 #endif
74 } // namespace al
76 #endif /* OPTHELPERS_H */