From 33c39770368495b99d652ca5e88bc7414854004b Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Fri, 11 Dec 2020 21:06:19 +0300 Subject: [PATCH] endian.hpp: minor simplification And stop pretending... --- rpcs3/stdafx.cpp | 2 ++ rpcs3/util/endian.hpp | 68 +++++++++++++++---------------------------- 2 files changed, 26 insertions(+), 44 deletions(-) diff --git a/rpcs3/stdafx.cpp b/rpcs3/stdafx.cpp index 77f28c49f2..194e83c838 100644 --- a/rpcs3/stdafx.cpp +++ b/rpcs3/stdafx.cpp @@ -1,5 +1,7 @@ // No BOM and only basic ASCII in this file, or a neko will die #include "stdafx.h" +static_assert(std::endian::native == std::endian::little || std::endian::native == std::endian::big); + static_assert(be_t(1) + be_t(2) + be_t(3) == 6); static_assert(le_t(1) + le_t(2) + le_t(3) == 6); diff --git a/rpcs3/util/endian.hpp b/rpcs3/util/endian.hpp index b7cbcd913c..5015aa6256 100644 --- a/rpcs3/util/endian.hpp +++ b/rpcs3/util/endian.hpp @@ -1,43 +1,20 @@ #pragma once // No BOM and only basic ASCII in this header, or a neko will die -#include #include "Utilities/types.h" -#if __has_include() -#include -#else -#include -#endif - namespace stx { - static_assert(std::endian::native == std::endian::little || std::endian::native == std::endian::big); - -#ifdef _MSC_VER - template - static 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> - static constexpr U bswap(T i) - { - return bswap_impl(i, std::make_index_sequence{}); - } -#endif - template struct se_storage { struct type8 { - alignas(Align > alignof(T) ? alignof(T) : Align) unsigned char data[sizeof(T)]; + alignas(Align > alignof(T) ? alignof(T) : Align) uchar data[sizeof(T)]; }; struct type64 { - alignas(8) std::uint64_t data[sizeof(T) < 8 ? 1 : sizeof(T) / 8]; + alignas(8) u64 data[sizeof(T) < 8 ? 1 : sizeof(T) / 8]; }; using type = std::conditional_t<(Align >= 8 && sizeof(T) % 8 == 0), type64, type8>; @@ -47,18 +24,18 @@ namespace stx }; template - struct se_storage + struct se_storage { - using type = std::uint16_t; + using type = u16; - static constexpr std::uint16_t swap(std::uint16_t src) noexcept + static constexpr u16 swap(u16 src) noexcept { #if defined(__GNUG__) return __builtin_bswap16(src); #else if (std::is_constant_evaluated()) { - return stx::bswap(src); + return (src >> 8) | (src << 8); } return _byteswap_ushort(src); @@ -67,18 +44,19 @@ namespace stx }; template - struct se_storage + struct se_storage { - using type = std::uint32_t; + using type = u32; - static constexpr std::uint32_t swap(std::uint32_t src) noexcept + static constexpr u32 swap(u32 src) noexcept { #if defined(__GNUG__) return __builtin_bswap32(src); #else if (std::is_constant_evaluated()) { - return stx::bswap(src); + const u32 v0 = ((src << 8) & 0xff00ff00) | ((src >> 8) & 0x00ff00ff); + return (v0 << 16) | (v0 >> 16); } return _byteswap_ulong(src); @@ -87,18 +65,20 @@ namespace stx }; template - struct se_storage + struct se_storage { - using type = std::uint64_t; + using type = u64; - static constexpr std::uint64_t swap(std::uint64_t src) noexcept + static constexpr u64 swap(u64 src) noexcept { #if defined(__GNUG__) return __builtin_bswap64(src); #else if (std::is_constant_evaluated()) { - return stx::bswap(src); + const u64 v0 = ((src << 8) & 0xff00ff00ff00ff00) | ((src >> 8) & 0x00ff00ff00ff00ff); + const u64 v1 = ((v0 << 16) & 0xffff0000ffff0000) | ((v0 >> 16) & 0x0000ffff0000ffff); + return (v1 << 32) | (v1 >> 32); } return _byteswap_uint64(src); @@ -116,15 +96,15 @@ namespace stx } else if constexpr (sizeof(T) == 2) { - return std::bit_cast(se_storage::swap(std::bit_cast(src))); + return std::bit_cast(se_storage::swap(std::bit_cast(src))); } else if constexpr (sizeof(T) == 4) { - return std::bit_cast(se_storage::swap(std::bit_cast(src))); + return std::bit_cast(se_storage::swap(std::bit_cast(src))); } else if constexpr (sizeof(T) == 8) { - return std::bit_cast(se_storage::swap(std::bit_cast(src))); + return std::bit_cast(se_storage::swap(std::bit_cast(src))); } else if constexpr (sizeof(T) % 8 == 0) { @@ -134,7 +114,7 @@ namespace stx // Swap u64 blocks for (std::size_t i = 0; i < sizeof(T) / 8; i++) { - dst.data[i] = se_storage::swap(tmp.data[sizeof(T) / 8 - 1 - i]); + dst.data[i] = se_storage::swap(tmp.data[sizeof(T) / 8 - 1 - i]); } return std::bit_cast(dst); @@ -182,13 +162,13 @@ namespace stx static constexpr auto int_or_enum() { - if constexpr (std::is_enum_v>) + if constexpr (std::is_enum_v) { - return std::underlying_type_t>{}; + return std::underlying_type_t{}; } else { - return simple_t{}; + return type{}; } }