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 | |
parent | b0be3bf883c147a87840b880a6b0a8db36333b6a (diff) |
Implement an al::unreachable wrapper/helper
-rw-r--r-- | al/buffer.cpp | 4 | ||||
-rw-r--r-- | alc/alu.cpp | 2 | ||||
-rw-r--r-- | common/opthelpers.h | 27 |
3 files changed, 22 insertions, 11 deletions
diff --git a/al/buffer.cpp b/al/buffer.cpp index 6deb3788..21520cec 100644 --- a/al/buffer.cpp +++ b/al/buffer.cpp @@ -1096,7 +1096,7 @@ START_API_FUNC ALuint align{SanitizeAlignment(usrfmt->type, unpack_align)}; if(align < 1) [[unlikely]] context->setError(AL_INVALID_VALUE, "Invalid unpack alignment %u", unpack_align); - else if(long{usrfmt->channels} != long{albuf->mChannels} + else if(al::to_underlying(usrfmt->channels) != al::to_underlying(albuf->mChannels) || usrfmt->type != albuf->OriginalType) [[unlikely]] context->setError(AL_INVALID_ENUM, "Unpacking data with mismatched format"); else if(align != albuf->OriginalAlign) [[unlikely]] @@ -1145,7 +1145,7 @@ START_API_FUNC static_cast<const al::byte*>(data), num_chans, samplen, align); else { - assert(long{usrfmt->type} == long{albuf->mType}); + assert(al::to_underlying(usrfmt->type) == al::to_underlying(albuf->mType)); memcpy(dst, data, size_t{samplen} * frame_size); } } diff --git a/alc/alu.cpp b/alc/alu.cpp index dc5ed122..fdb51f78 100644 --- a/alc/alu.cpp +++ b/alc/alu.cpp @@ -1714,7 +1714,7 @@ void SendSourceStateEvent(ContextBase *context, uint id, VChangeState state) break; /* Shouldn't happen. */ case VChangeState::Restart: - ASSUME(0); + al::unreachable(); } ring->writeAdvance(1); 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 } |