From f5938583a72551e5dbe94768a5fc884ebc2c0171 Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 1 Aug 2024 14:17:51 -0500 Subject: [PATCH] Add `LIBHAT_UNREACHABLE` macro --- include/libhat/Defines.hpp | 17 +++++++++++++++++ include/libhat/Scanner.hpp | 2 +- src/arch/x86/AVX2.cpp | 2 +- src/arch/x86/AVX512.cpp | 2 +- src/arch/x86/SSE.cpp | 2 +- 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/include/libhat/Defines.hpp b/include/libhat/Defines.hpp index 328ce99..aebeabc 100644 --- a/include/libhat/Defines.hpp +++ b/include/libhat/Defines.hpp @@ -73,3 +73,20 @@ #else #define LIBHAT_UNLIKELY #endif + +#if __cpp_lib_unreachable >= 202202L + #include + #define LIBHAT_UNREACHABLE() std::unreachable() +#elif defined(_MSC_VER) + #define LIBHAT_UNREACHABLE() __assume(false) +#elif defined(__GNUC__) + #define LIBHAT_UNREACHABLE() __builtin_unreachable() +#else + #include + namespace hat::detail { + [[noreturn]] inline void unreachable_impl() { + std::abort(); + } + } + #define LIBHAT_UNREACHABLE() hat::detail::unreachable_impl() +#endif diff --git a/include/libhat/Scanner.hpp b/include/libhat/Scanner.hpp index 8f3d883..818fdc4 100644 --- a/include/libhat/Scanner.hpp +++ b/include/libhat/Scanner.hpp @@ -211,7 +211,7 @@ namespace hat { case scan_alignment::X1: return &find_pattern_single; case scan_alignment::X16: return &find_pattern_single; } - std::unreachable(); + LIBHAT_UNREACHABLE(); } [[nodiscard]] constexpr auto truncate(const signature_view signature) noexcept { diff --git a/src/arch/x86/AVX2.cpp b/src/arch/x86/AVX2.cpp index 17fb134..22c7d30 100644 --- a/src/arch/x86/AVX2.cpp +++ b/src/arch/x86/AVX2.cpp @@ -117,7 +117,7 @@ namespace hat::detail { return &find_pattern_avx2; } } - std::unreachable(); + LIBHAT_UNREACHABLE(); } } #endif diff --git a/src/arch/x86/AVX512.cpp b/src/arch/x86/AVX512.cpp index 6c7a7f1..4585240 100644 --- a/src/arch/x86/AVX512.cpp +++ b/src/arch/x86/AVX512.cpp @@ -112,7 +112,7 @@ namespace hat::detail { return &find_pattern_avx512; } } - std::unreachable(); + LIBHAT_UNREACHABLE(); } } #endif diff --git a/src/arch/x86/SSE.cpp b/src/arch/x86/SSE.cpp index 7540707..146543f 100644 --- a/src/arch/x86/SSE.cpp +++ b/src/arch/x86/SSE.cpp @@ -115,7 +115,7 @@ namespace hat::detail { return &find_pattern_sse; } } - std::unreachable(); + LIBHAT_UNREACHABLE(); } } #endif