diff options
author | Chris Robinson <[email protected]> | 2022-12-13 06:41:01 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2022-12-13 06:41:01 -0800 |
commit | 7c3f201f2625e37807c608e718b190cc48107b9c (patch) | |
tree | 3b1ad37bf90e49836ebaa1fc6768c11b1e8be249 /common | |
parent | b0be3bf883c147a87840b880a6b0a8db36333b6a (diff) |
Implement an al::unreachable wrapper/helper
Diffstat (limited to 'common')
-rw-r--r-- | common/opthelpers.h | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/common/opthelpers.h b/common/opthelpers.h index f6d9cc00..04c5f1d0 100644 --- a/common/opthelpers.h +++ b/common/opthelpers.h @@ -30,7 +30,7 @@ #define ASSUME __assume #elif __has_attribute(assume) #define ASSUME(x) [[assume(x)]] -#elif defined(__GNUC__) +#elif HAS_BUILTIN(__builtin_unreachable) #define ASSUME(x) do { if(x) break; __builtin_unreachable(); } while(0) #else #define ASSUME(x) ((void)0) @@ -38,23 +38,34 @@ namespace al { +template<typename T> +constexpr std::underlying_type_t<T> to_underlying(T e) noexcept +{ return static_cast<std::underlying_type_t<T>>(e); } + +[[noreturn]] inline void unreachable() +{ +#if HAS_BUILTIN(__builtin_unreachable) + __builtin_unreachable(); +#else + ASSUME(false); +#endif +} + template<std::size_t alignment, typename T> force_inline constexpr auto assume_aligned(T *ptr) noexcept { #ifdef __cpp_lib_assume_aligned return std::assume_aligned<alignment,T>(ptr); -#elif defined(__clang__) || (defined(__GNUC__) && !defined(__ICC)) +#elif HAS_BUILTIN(__builtin_assume_aligned) return static_cast<T*>(__builtin_assume_aligned(ptr, alignment)); -#elif defined(_MSC_VER) - constexpr std::size_t alignment_mask{(1<<alignment) - 1}; - if((reinterpret_cast<std::uintptr_t>(ptr)&alignment_mask) == 0) - return ptr; - __assume(0); #elif defined(__ICC) __assume_aligned(ptr, alignment); return ptr; #else - return ptr; + constexpr std::size_t alignment_mask{(1<<alignment) - 1}; + if((reinterpret_cast<std::uintptr_t>(ptr)&alignment_mask) == 0) + return ptr; + unreachable(); #endif } |