diff --git a/rpcs3/util/endian.hpp b/rpcs3/util/endian.hpp index c7b480fd48..6735198450 100644 --- a/rpcs3/util/endian.hpp +++ b/rpcs3/util/endian.hpp @@ -13,6 +13,20 @@ namespace stx { static_assert(std::endian::native == std::endian::little || std::endian::native == std::endian::big); + template + constexpr T bswap_impl(T i, std::index_sequence) + { + return static_cast(((((i >> (N * 8)) & T{UINT8_MAX}) << + ((sizeof(T) - 1 - N) * 8)) | ...)); + }; + + template::type> + constexpr U bswap(T i) + { + return bswap_impl(i, std::make_index_sequence{}); + } + + template struct se_storage { @@ -39,6 +53,11 @@ namespace stx static constexpr std::uint16_t swap(std::uint16_t src) noexcept { + if (std::is_constant_evaluated()) + { + return stx::bswap(src); + } + #if defined(__GNUG__) return __builtin_bswap16(src); #else @@ -54,6 +73,11 @@ namespace stx static constexpr std::uint32_t swap(std::uint32_t src) noexcept { + if (std::is_constant_evaluated()) + { + return stx::bswap(src); + } + #if defined(__GNUG__) return __builtin_bswap32(src); #else @@ -69,6 +93,11 @@ namespace stx static constexpr std::uint64_t swap(std::uint64_t src) noexcept { + if (std::is_constant_evaluated()) + { + return stx::bswap(src); + } + #if defined(__GNUG__) return __builtin_bswap64(src); #else