From 442656899f3765044ab643a01cba0356ec7d97af Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 3 Apr 2022 10:51:46 -0700 Subject: [PATCH] util: update some bit utility logic --- .../socket/impl/socket_api.os.horizon.cpp | 24 +--- .../include/vapours/util/util_bitutil.hpp | 131 ++++++++---------- .../include/vapours/util/util_endian.hpp | 77 +++++----- 3 files changed, 102 insertions(+), 130 deletions(-) diff --git a/libraries/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp b/libraries/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp index 1ebf01565..97afc515e 100644 --- a/libraries/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp +++ b/libraries/libstratosphere/source/socket/impl/socket_api.os.horizon.cpp @@ -113,35 +113,19 @@ namespace ams::socket::impl { } u32 InetHtonl(u32 host) { - if constexpr (util::IsBigEndian()) { - return host; - } else { - return util::SwapBytes(host); - } + return util::ConvertToBigEndian(host); } u16 InetHtons(u16 host) { - if constexpr (util::IsBigEndian()) { - return host; - } else { - return util::SwapBytes(host); - } + return util::ConvertToBigEndian(host); } u32 InetNtohl(u32 net) { - if constexpr (util::IsBigEndian()) { - return net; - } else { - return util::SwapBytes(net); - } + return util::ConvertFromBigEndian(net); } u16 InetNtohs(u16 net) { - if constexpr (util::IsBigEndian()) { - return net; - } else { - return util::SwapBytes(net); - } + return util::ConvertFromBigEndian(net); } namespace { diff --git a/libraries/libvapours/include/vapours/util/util_bitutil.hpp b/libraries/libvapours/include/vapours/util/util_bitutil.hpp index 2d5185197..baf2af074 100644 --- a/libraries/libvapours/include/vapours/util/util_bitutil.hpp +++ b/libraries/libvapours/include/vapours/util/util_bitutil.hpp @@ -30,108 +30,91 @@ namespace ams::util { } - template requires std::integral - class BitsOf { - private: - static constexpr ALWAYS_INLINE int GetLsbPos(T v) { - return __builtin_ctzll(static_cast(v)); - } + template + constexpr inline T ReverseBits(T x, int sw_bits = 1, int swar_words = 1) { + /* Check pre-conditions. */ + AMS_ASSERT(0 <= swar_words && swar_words < (BITSIZEOF(T) + 1)); + AMS_ASSERT(BITSIZEOF(T) % swar_words == 0); + AMS_ASSERT(0 <= sw_bits && sw_bits < ((BITSIZEOF(T) / swar_words) + 1)); + AMS_ASSERT((BITSIZEOF(T) / swar_words) % sw_bits == 0); - T m_value; - public: - /* Note: GCC has a bug in constant-folding here. Workaround: wrap entire caller with constexpr. */ - constexpr ALWAYS_INLINE BitsOf(T value = T(0u)) : m_value(value) { - /* ... */ - } + using U = typename std::make_unsigned::type; + const int word_size = BITSIZEOF(T) / swar_words; + const int k = word_size - sw_bits; - constexpr ALWAYS_INLINE bool operator==(const BitsOf &other) const { - return m_value == other.m_value; - } + U u = std::bit_cast(x); + for (int i = 1; i < BITSIZEOF(T); i <<= 1) { + const U mask = static_cast(static_cast(-1) / ((static_cast(1) << i) + 1)); - constexpr ALWAYS_INLINE bool operator!=(const BitsOf &other) const { - return m_value != other.m_value; + if (k & i) { + u = static_cast(((u & mask) << i) | ((u & static_cast(~mask)) >> i)); } + } - constexpr ALWAYS_INLINE int operator*() const { - return GetLsbPos(m_value); - } + return std::bit_cast(u); + } - constexpr ALWAYS_INLINE BitsOf &operator++() { - m_value &= ~(T(1u) << GetLsbPos(m_value)); - return *this; - } - - constexpr ALWAYS_INLINE BitsOf &operator++(int) { - BitsOf ret(m_value); - ++(*this); - return ret; - } - - constexpr ALWAYS_INLINE BitsOf begin() const { - return *this; - } - - constexpr ALWAYS_INLINE BitsOf end() const { - return BitsOf(T(0u)); - } - }; + template + constexpr ALWAYS_INLINE T ReverseBytes(T x, int sw_bytes = 1, int swar_words = 1) { + return ReverseBits(x, sw_bytes * BITSIZEOF(u8), swar_words); + } template requires std::integral constexpr ALWAYS_INLINE T CombineBits(Args... args) { return (... | (T(1u) << args)); } - template requires std::integral + template constexpr ALWAYS_INLINE T ResetLeastSignificantOneBit(T x) { return x & (x - 1); } - template requires std::integral + template constexpr ALWAYS_INLINE T SetLeastSignificantZeroBit(T x) { return x | (x + 1); } - template requires std::integral - constexpr ALWAYS_INLINE T LeastSignificantOneBit(T x) { - return x & ~(x - 1); - } - - template requires std::integral - constexpr ALWAYS_INLINE T LeastSignificantZeroBit(T x) { - return ~x & (x + 1); - } - - template requires std::integral + template constexpr ALWAYS_INLINE T ResetTrailingOnes(T x) { return x & (x + 1); } - template requires std::integral + template constexpr ALWAYS_INLINE T SetTrailingZeros(T x) { return x | (x - 1); } - template requires std::integral + template + constexpr ALWAYS_INLINE T LeastSignificantOneBit(T x) { + return x & ~(x - 1); + } + + template + constexpr ALWAYS_INLINE T LeastSignificantZeroBit(T x) { + return ~x & (x + 1); + } + + template constexpr ALWAYS_INLINE T MaskTrailingZeros(T x) { return (~x) & (x - 1); } - template requires std::integral + template constexpr ALWAYS_INLINE T MaskTrailingOnes(T x) { - return ~((~x) | (x + 1)); + return x & ~(x + 1); } - template requires std::integral + template constexpr ALWAYS_INLINE T MaskTrailingZerosAndLeastSignificantOneBit(T x) { return x ^ (x - 1); } - template requires std::integral + template constexpr ALWAYS_INLINE T MaskTrailingOnesAndLeastSignificantZeroBit(T x) { return x ^ (x + 1); } - template requires std::integral + template constexpr ALWAYS_INLINE int PopCount(T x) { using U = typename std::make_unsigned::type; U u = static_cast(x); @@ -164,7 +147,7 @@ namespace ams::util { } } - template requires std::integral + template constexpr ALWAYS_INLINE int CountLeadingZeros(T x) { if (std::is_constant_evaluated()) { for (size_t i = 0; i < impl::Log2; ++i) { @@ -174,9 +157,8 @@ namespace ams::util { return PopCount(static_cast(~x)); } else { using U = typename std::make_unsigned::type; - const U u = static_cast(x); - if (u != 0) { + if (const U u = static_cast(x); u != 0) { if constexpr (std::is_same::value) { return __builtin_clzll(u); } else if constexpr (std::is_same::value) { @@ -198,7 +180,7 @@ namespace ams::util { static_assert(CountLeadingZeros(static_cast(1) << 5) == BITSIZEOF(u64) - 1 - 5); static_assert(CountLeadingZeros(static_cast(0)) == BITSIZEOF(u64)); - template requires std::integral + template constexpr ALWAYS_INLINE int CountTrailingZeros(T x) { if (std::is_constant_evaluated()) { auto count = 0; @@ -209,8 +191,7 @@ namespace ams::util { return count; } else { using U = typename std::make_unsigned::type; - const U u = static_cast(x); - if (u != 0) { + if (const U u = static_cast(x); u != 0) { if constexpr (std::is_same::value) { return __builtin_ctzll(u); } else if constexpr (std::is_same::value) { @@ -231,31 +212,39 @@ namespace ams::util { static_assert(CountTrailingZeros(static_cast(1) << 5) == 5); static_assert(CountTrailingZeros(static_cast(0)) == BITSIZEOF(u64)); - template requires std::integral + template constexpr ALWAYS_INLINE bool IsPowerOfTwo(T x) { return x > 0 && ResetLeastSignificantOneBit(x) == 0; } - template requires std::integral + template constexpr ALWAYS_INLINE T CeilingPowerOfTwo(T x) { AMS_ASSERT(x > 0); return T(1) << (BITSIZEOF(T) - CountLeadingZeros(T(x - 1))); } - template requires std::integral + template constexpr ALWAYS_INLINE T FloorPowerOfTwo(T x) { AMS_ASSERT(x > 0); return T(1) << (BITSIZEOF(T) - CountLeadingZeros(x) - 1); } - template + template constexpr ALWAYS_INLINE T DivideUp(T v, U d) { using Unsigned = typename std::make_unsigned::type; + using Sum = decltype(T{0} + U{0}); + + #if defined(ATMOSPHERE_IS_STRATOSPHERE) + AMS_ASSERT(v >= 0); + AMS_ASSERT(d > 0); + AMS_ASSERT(static_cast(v) <= (std::numeric_limits::max() - static_cast(d) + static_cast(1))); + #endif + const Unsigned add = static_cast(d) - 1; - return static_cast((v + add) / d); + return static_cast((static_cast(v) + static_cast(add)) / static_cast(d)); } - template + template constexpr ALWAYS_INLINE T ScaleByConstantFactorUp(const T V) { /* Multiplying and dividing by large numerator/denominator can cause error to be introduced. */ /* This algorithm multiples/divides in stages, so as to mitigate this (particularly with large denominator). */ diff --git a/libraries/libvapours/include/vapours/util/util_endian.hpp b/libraries/libvapours/include/vapours/util/util_endian.hpp index 8ce31338c..e71d12b3b 100644 --- a/libraries/libvapours/include/vapours/util/util_endian.hpp +++ b/libraries/libvapours/include/vapours/util/util_endian.hpp @@ -30,38 +30,27 @@ namespace ams::util { static_assert(IsLittleEndian() ^ IsBigEndian()); - template requires std::unsigned_integral - constexpr ALWAYS_INLINE U SwapBytes(const U u) { + template + constexpr ALWAYS_INLINE U SwapEndian(const U u) { static_assert(BITSIZEOF(u8) == 8); - constexpr U ByteMask = 0xFFu; - if constexpr (std::is_same::value) { - return ((u & (ByteMask << 56)) >> 56) | - ((u & (ByteMask << 48)) >> 40) | - ((u & (ByteMask << 40)) >> 24) | - ((u & (ByteMask << 32)) >> 8) | - ((u & (ByteMask << 24)) << 8) | - ((u & (ByteMask << 16)) << 24) | - ((u & (ByteMask << 8)) << 40) | - ((u & (ByteMask << 0)) << 56); - } else if constexpr (std::is_same::value) { - return ((u & (ByteMask << 24)) >> 24) | - ((u & (ByteMask << 16)) >> 8) | - ((u & (ByteMask << 8)) << 8) | - ((u & (ByteMask << 0)) << 24); - } else if constexpr (std::is_same::value) { - return ((u & (ByteMask << 8)) >> 8) | - ((u & (ByteMask << 0)) << 8); - - } else if constexpr (std::is_same::value) { - AMS_UNUSED(ByteMask); + if constexpr (sizeof(U) * BITSIZEOF(u8) == 64) { + static_assert(__builtin_bswap64(UINT64_C(0x0123456789ABCDEF)) == UINT64_C(0xEFCDAB8967452301)); + return __builtin_bswap64(u); + } else if constexpr (sizeof(U) * BITSIZEOF(u8) == 32) { + static_assert(__builtin_bswap32(0x01234567u) == 0x67452301u); + return __builtin_bswap32(u); + } else if constexpr (sizeof(U) * BITSIZEOF(u8) == 16) { + static_assert(__builtin_bswap16(0x0123u) == 0x2301u); + return __builtin_bswap16(u); + } else if constexpr (sizeof(U) * BITSIZEOF(u8) == 8) { return u; } else { static_assert(!std::is_same::value); } } - constexpr ALWAYS_INLINE u64 SwapBytes48(const u64 u) { + constexpr ALWAYS_INLINE u64 SwapEndian48(const u64 u) { using U = u64; static_assert(BITSIZEOF(u8) == 8); constexpr U ByteMask = 0xFFu; @@ -74,14 +63,14 @@ namespace ams::util { ((u & (ByteMask << 0)) << 40); } - template requires std::integral - constexpr ALWAYS_INLINE void SwapBytes(T *ptr) { + template + constexpr ALWAYS_INLINE void SwapEndian(T *ptr) { using U = typename std::make_unsigned::type; - *ptr = static_cast(SwapBytes(static_cast(*ptr))); + *ptr = static_cast(SwapEndian(static_cast(*ptr))); } - template requires std::integral + template constexpr ALWAYS_INLINE T ConvertToBigEndian(const T val) { using U = typename std::make_unsigned::type; @@ -89,23 +78,33 @@ namespace ams::util { return static_cast(static_cast(val)); } else { static_assert(IsLittleEndian()); - return static_cast(SwapBytes(static_cast(val))); + return static_cast(SwapEndian(static_cast(val))); } } - template requires std::integral + template constexpr ALWAYS_INLINE T ConvertToLittleEndian(const T val) { using U = typename std::make_unsigned::type; if constexpr (IsBigEndian()) { - return static_cast(SwapBytes(static_cast(val))); + return static_cast(SwapEndian(static_cast(val))); } else { static_assert(IsLittleEndian()); return static_cast(static_cast(val)); } } - template requires std::integral + template + constexpr ALWAYS_INLINE T ConvertFromBigEndian(const T val) { + return ConvertToBigEndian(val); + } + + template + constexpr ALWAYS_INLINE T ConvertFromLittleEndian(const T val) { + return ConvertToLittleEndian(val); + } + + template constexpr ALWAYS_INLINE T ConvertToBigEndian48(const T val) { using U = typename std::make_unsigned::type; static_assert(sizeof(T) == sizeof(u64)); @@ -115,17 +114,17 @@ namespace ams::util { return static_cast(static_cast(val)); } else { static_assert(IsLittleEndian()); - return static_cast(SwapBytes48(static_cast(val))); + return static_cast(SwapEndian48(static_cast(val))); } } - template requires std::integral + template constexpr ALWAYS_INLINE T ConvertToLittleEndian48(const T val) { using U = typename std::make_unsigned::type; static_assert(sizeof(T) == sizeof(u64)); if constexpr (IsBigEndian()) { - return static_cast(SwapBytes48(static_cast(val))); + return static_cast(SwapEndian48(static_cast(val))); } else { static_assert(IsLittleEndian()); AMS_ASSERT((static_cast(val) & UINT64_C(0xFFFF000000000000)) == 0); @@ -133,22 +132,22 @@ namespace ams::util { } } - template requires std::integral + template constexpr ALWAYS_INLINE T LoadBigEndian(const T *ptr) { return ConvertToBigEndian(*ptr); } - template requires std::integral + template constexpr ALWAYS_INLINE T LoadLittleEndian(const T *ptr) { return ConvertToLittleEndian(*ptr); } - template requires std::integral + template constexpr ALWAYS_INLINE void StoreBigEndian(T *ptr, T val) { *ptr = ConvertToBigEndian(val); } - template requires std::integral + template constexpr ALWAYS_INLINE void StoreLittleEndian(T *ptr, T val) { *ptr = ConvertToLittleEndian(val); }