From 07a44d0ff916ffb15f188ec56c7c1059296fd0f1 Mon Sep 17 00:00:00 2001 From: Eladash Date: Thu, 25 Jun 2020 20:48:42 +0300 Subject: [PATCH] Implement constexpr byteswapping --- rpcs3/util/endian.hpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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