1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-17 17:36:44 +00:00

util: update some bit utility logic

This commit is contained in:
Michael Scire 2022-04-03 10:51:46 -07:00
parent d7f89a0c31
commit 442656899f
3 changed files with 102 additions and 130 deletions

View file

@ -113,35 +113,19 @@ namespace ams::socket::impl {
} }
u32 InetHtonl(u32 host) { u32 InetHtonl(u32 host) {
if constexpr (util::IsBigEndian()) { return util::ConvertToBigEndian(host);
return host;
} else {
return util::SwapBytes(host);
}
} }
u16 InetHtons(u16 host) { u16 InetHtons(u16 host) {
if constexpr (util::IsBigEndian()) { return util::ConvertToBigEndian(host);
return host;
} else {
return util::SwapBytes(host);
}
} }
u32 InetNtohl(u32 net) { u32 InetNtohl(u32 net) {
if constexpr (util::IsBigEndian()) { return util::ConvertFromBigEndian(net);
return net;
} else {
return util::SwapBytes(net);
}
} }
u16 InetNtohs(u16 net) { u16 InetNtohs(u16 net) {
if constexpr (util::IsBigEndian()) { return util::ConvertFromBigEndian(net);
return net;
} else {
return util::SwapBytes(net);
}
} }
namespace { namespace {

View file

@ -30,108 +30,91 @@ namespace ams::util {
} }
template <typename T> requires std::integral<T> template<std::integral T>
class BitsOf { constexpr inline T ReverseBits(T x, int sw_bits = 1, int swar_words = 1) {
private: /* Check pre-conditions. */
static constexpr ALWAYS_INLINE int GetLsbPos(T v) { AMS_ASSERT(0 <= swar_words && swar_words < (BITSIZEOF(T) + 1));
return __builtin_ctzll(static_cast<u64>(v)); 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);
using U = typename std::make_unsigned<T>::type;
const int word_size = BITSIZEOF(T) / swar_words;
const int k = word_size - sw_bits;
U u = std::bit_cast<U, T>(x);
for (int i = 1; i < BITSIZEOF(T); i <<= 1) {
const U mask = static_cast<U>(static_cast<U>(-1) / ((static_cast<U>(1) << i) + 1));
if (k & i) {
u = static_cast<U>(((u & mask) << i) | ((u & static_cast<U>(~mask)) >> i));
}
} }
T m_value; return std::bit_cast<T, U>(u);
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) {
/* ... */
} }
constexpr ALWAYS_INLINE bool operator==(const BitsOf &other) const { template<std::integral T>
return m_value == other.m_value; constexpr ALWAYS_INLINE T ReverseBytes(T x, int sw_bytes = 1, int swar_words = 1) {
return ReverseBits(x, sw_bytes * BITSIZEOF(u8), swar_words);
} }
constexpr ALWAYS_INLINE bool operator!=(const BitsOf &other) const {
return m_value != other.m_value;
}
constexpr ALWAYS_INLINE int operator*() const {
return GetLsbPos(m_value);
}
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<typename T = u64, typename ...Args> requires std::integral<T> template<typename T = u64, typename ...Args> requires std::integral<T>
constexpr ALWAYS_INLINE T CombineBits(Args... args) { constexpr ALWAYS_INLINE T CombineBits(Args... args) {
return (... | (T(1u) << args)); return (... | (T(1u) << args));
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T ResetLeastSignificantOneBit(T x) { constexpr ALWAYS_INLINE T ResetLeastSignificantOneBit(T x) {
return x & (x - 1); return x & (x - 1);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T SetLeastSignificantZeroBit(T x) { constexpr ALWAYS_INLINE T SetLeastSignificantZeroBit(T x) {
return x | (x + 1); return x | (x + 1);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T LeastSignificantOneBit(T x) {
return x & ~(x - 1);
}
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T LeastSignificantZeroBit(T x) {
return ~x & (x + 1);
}
template<typename T> requires std::integral<T>
constexpr ALWAYS_INLINE T ResetTrailingOnes(T x) { constexpr ALWAYS_INLINE T ResetTrailingOnes(T x) {
return x & (x + 1); return x & (x + 1);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T SetTrailingZeros(T x) { constexpr ALWAYS_INLINE T SetTrailingZeros(T x) {
return x | (x - 1); return x | (x - 1);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T LeastSignificantOneBit(T x) {
return x & ~(x - 1);
}
template<std::integral T>
constexpr ALWAYS_INLINE T LeastSignificantZeroBit(T x) {
return ~x & (x + 1);
}
template<std::integral T>
constexpr ALWAYS_INLINE T MaskTrailingZeros(T x) { constexpr ALWAYS_INLINE T MaskTrailingZeros(T x) {
return (~x) & (x - 1); return (~x) & (x - 1);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T MaskTrailingOnes(T x) { constexpr ALWAYS_INLINE T MaskTrailingOnes(T x) {
return ~((~x) | (x + 1)); return x & ~(x + 1);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T MaskTrailingZerosAndLeastSignificantOneBit(T x) { constexpr ALWAYS_INLINE T MaskTrailingZerosAndLeastSignificantOneBit(T x) {
return x ^ (x - 1); return x ^ (x - 1);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T MaskTrailingOnesAndLeastSignificantZeroBit(T x) { constexpr ALWAYS_INLINE T MaskTrailingOnesAndLeastSignificantZeroBit(T x) {
return x ^ (x + 1); return x ^ (x + 1);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE int PopCount(T x) { constexpr ALWAYS_INLINE int PopCount(T x) {
using U = typename std::make_unsigned<T>::type; using U = typename std::make_unsigned<T>::type;
U u = static_cast<U>(x); U u = static_cast<U>(x);
@ -164,7 +147,7 @@ namespace ams::util {
} }
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE int CountLeadingZeros(T x) { constexpr ALWAYS_INLINE int CountLeadingZeros(T x) {
if (std::is_constant_evaluated()) { if (std::is_constant_evaluated()) {
for (size_t i = 0; i < impl::Log2<BITSIZEOF(T)>; ++i) { for (size_t i = 0; i < impl::Log2<BITSIZEOF(T)>; ++i) {
@ -174,9 +157,8 @@ namespace ams::util {
return PopCount(static_cast<T>(~x)); return PopCount(static_cast<T>(~x));
} else { } else {
using U = typename std::make_unsigned<T>::type; using U = typename std::make_unsigned<T>::type;
const U u = static_cast<U>(x);
if (u != 0) { if (const U u = static_cast<U>(x); u != 0) {
if constexpr (std::is_same<U, unsigned long long>::value) { if constexpr (std::is_same<U, unsigned long long>::value) {
return __builtin_clzll(u); return __builtin_clzll(u);
} else if constexpr (std::is_same<U, unsigned long>::value) { } else if constexpr (std::is_same<U, unsigned long>::value) {
@ -198,7 +180,7 @@ namespace ams::util {
static_assert(CountLeadingZeros(static_cast<u64>(1) << 5) == BITSIZEOF(u64) - 1 - 5); static_assert(CountLeadingZeros(static_cast<u64>(1) << 5) == BITSIZEOF(u64) - 1 - 5);
static_assert(CountLeadingZeros(static_cast<u64>(0)) == BITSIZEOF(u64)); static_assert(CountLeadingZeros(static_cast<u64>(0)) == BITSIZEOF(u64));
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE int CountTrailingZeros(T x) { constexpr ALWAYS_INLINE int CountTrailingZeros(T x) {
if (std::is_constant_evaluated()) { if (std::is_constant_evaluated()) {
auto count = 0; auto count = 0;
@ -209,8 +191,7 @@ namespace ams::util {
return count; return count;
} else { } else {
using U = typename std::make_unsigned<T>::type; using U = typename std::make_unsigned<T>::type;
const U u = static_cast<U>(x); if (const U u = static_cast<U>(x); u != 0) {
if (u != 0) {
if constexpr (std::is_same<U, unsigned long long>::value) { if constexpr (std::is_same<U, unsigned long long>::value) {
return __builtin_ctzll(u); return __builtin_ctzll(u);
} else if constexpr (std::is_same<U, unsigned long>::value) { } else if constexpr (std::is_same<U, unsigned long>::value) {
@ -231,31 +212,39 @@ namespace ams::util {
static_assert(CountTrailingZeros(static_cast<u64>(1) << 5) == 5); static_assert(CountTrailingZeros(static_cast<u64>(1) << 5) == 5);
static_assert(CountTrailingZeros(static_cast<u64>(0)) == BITSIZEOF(u64)); static_assert(CountTrailingZeros(static_cast<u64>(0)) == BITSIZEOF(u64));
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE bool IsPowerOfTwo(T x) { constexpr ALWAYS_INLINE bool IsPowerOfTwo(T x) {
return x > 0 && ResetLeastSignificantOneBit(x) == 0; return x > 0 && ResetLeastSignificantOneBit(x) == 0;
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T CeilingPowerOfTwo(T x) { constexpr ALWAYS_INLINE T CeilingPowerOfTwo(T x) {
AMS_ASSERT(x > 0); AMS_ASSERT(x > 0);
return T(1) << (BITSIZEOF(T) - CountLeadingZeros(T(x - 1))); return T(1) << (BITSIZEOF(T) - CountLeadingZeros(T(x - 1)));
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T FloorPowerOfTwo(T x) { constexpr ALWAYS_INLINE T FloorPowerOfTwo(T x) {
AMS_ASSERT(x > 0); AMS_ASSERT(x > 0);
return T(1) << (BITSIZEOF(T) - CountLeadingZeros(x) - 1); return T(1) << (BITSIZEOF(T) - CountLeadingZeros(x) - 1);
} }
template<typename T, typename U> template<std::integral T, std::integral U>
constexpr ALWAYS_INLINE T DivideUp(T v, U d) { constexpr ALWAYS_INLINE T DivideUp(T v, U d) {
using Unsigned = typename std::make_unsigned<U>::type; using Unsigned = typename std::make_unsigned<U>::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<Sum>(v) <= (std::numeric_limits<Sum>::max() - static_cast<Sum>(d) + static_cast<Sum>(1)));
#endif
const Unsigned add = static_cast<Unsigned>(d) - 1; const Unsigned add = static_cast<Unsigned>(d) - 1;
return static_cast<T>((v + add) / d); return static_cast<T>((static_cast<Sum>(v) + static_cast<Sum>(add)) / static_cast<Sum>(d));
} }
template<typename T, T N, T D> template<std::integral T, T N, T D>
constexpr ALWAYS_INLINE T ScaleByConstantFactorUp(const T V) { constexpr ALWAYS_INLINE T ScaleByConstantFactorUp(const T V) {
/* Multiplying and dividing by large numerator/denominator can cause error to be introduced. */ /* 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). */ /* This algorithm multiples/divides in stages, so as to mitigate this (particularly with large denominator). */

View file

@ -30,38 +30,27 @@ namespace ams::util {
static_assert(IsLittleEndian() ^ IsBigEndian()); static_assert(IsLittleEndian() ^ IsBigEndian());
template<typename U> requires std::unsigned_integral<U> template<std::unsigned_integral U>
constexpr ALWAYS_INLINE U SwapBytes(const U u) { constexpr ALWAYS_INLINE U SwapEndian(const U u) {
static_assert(BITSIZEOF(u8) == 8); static_assert(BITSIZEOF(u8) == 8);
constexpr U ByteMask = 0xFFu;
if constexpr (std::is_same<U, u64>::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<U, u32>::value) {
return ((u & (ByteMask << 24)) >> 24) |
((u & (ByteMask << 16)) >> 8) |
((u & (ByteMask << 8)) << 8) |
((u & (ByteMask << 0)) << 24);
} else if constexpr (std::is_same<U, u16>::value) { if constexpr (sizeof(U) * BITSIZEOF(u8) == 64) {
return ((u & (ByteMask << 8)) >> 8) | static_assert(__builtin_bswap64(UINT64_C(0x0123456789ABCDEF)) == UINT64_C(0xEFCDAB8967452301));
((u & (ByteMask << 0)) << 8); return __builtin_bswap64(u);
} else if constexpr (sizeof(U) * BITSIZEOF(u8) == 32) {
} else if constexpr (std::is_same<U, u8>::value) { static_assert(__builtin_bswap32(0x01234567u) == 0x67452301u);
AMS_UNUSED(ByteMask); 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; return u;
} else { } else {
static_assert(!std::is_same<U, U>::value); static_assert(!std::is_same<U, U>::value);
} }
} }
constexpr ALWAYS_INLINE u64 SwapBytes48(const u64 u) { constexpr ALWAYS_INLINE u64 SwapEndian48(const u64 u) {
using U = u64; using U = u64;
static_assert(BITSIZEOF(u8) == 8); static_assert(BITSIZEOF(u8) == 8);
constexpr U ByteMask = 0xFFu; constexpr U ByteMask = 0xFFu;
@ -74,14 +63,14 @@ namespace ams::util {
((u & (ByteMask << 0)) << 40); ((u & (ByteMask << 0)) << 40);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE void SwapBytes(T *ptr) { constexpr ALWAYS_INLINE void SwapEndian(T *ptr) {
using U = typename std::make_unsigned<T>::type; using U = typename std::make_unsigned<T>::type;
*ptr = static_cast<T>(SwapBytes<U>(static_cast<U>(*ptr))); *ptr = static_cast<T>(SwapEndian<U>(static_cast<U>(*ptr)));
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T ConvertToBigEndian(const T val) { constexpr ALWAYS_INLINE T ConvertToBigEndian(const T val) {
using U = typename std::make_unsigned<T>::type; using U = typename std::make_unsigned<T>::type;
@ -89,23 +78,33 @@ namespace ams::util {
return static_cast<T>(static_cast<U>(val)); return static_cast<T>(static_cast<U>(val));
} else { } else {
static_assert(IsLittleEndian()); static_assert(IsLittleEndian());
return static_cast<T>(SwapBytes<U>(static_cast<U>(val))); return static_cast<T>(SwapEndian<U>(static_cast<U>(val)));
} }
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T ConvertToLittleEndian(const T val) { constexpr ALWAYS_INLINE T ConvertToLittleEndian(const T val) {
using U = typename std::make_unsigned<T>::type; using U = typename std::make_unsigned<T>::type;
if constexpr (IsBigEndian()) { if constexpr (IsBigEndian()) {
return static_cast<T>(SwapBytes<U>(static_cast<U>(val))); return static_cast<T>(SwapEndian<U>(static_cast<U>(val)));
} else { } else {
static_assert(IsLittleEndian()); static_assert(IsLittleEndian());
return static_cast<T>(static_cast<U>(val)); return static_cast<T>(static_cast<U>(val));
} }
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T ConvertFromBigEndian(const T val) {
return ConvertToBigEndian(val);
}
template<std::integral T>
constexpr ALWAYS_INLINE T ConvertFromLittleEndian(const T val) {
return ConvertToLittleEndian(val);
}
template<std::integral T>
constexpr ALWAYS_INLINE T ConvertToBigEndian48(const T val) { constexpr ALWAYS_INLINE T ConvertToBigEndian48(const T val) {
using U = typename std::make_unsigned<T>::type; using U = typename std::make_unsigned<T>::type;
static_assert(sizeof(T) == sizeof(u64)); static_assert(sizeof(T) == sizeof(u64));
@ -115,17 +114,17 @@ namespace ams::util {
return static_cast<T>(static_cast<U>(val)); return static_cast<T>(static_cast<U>(val));
} else { } else {
static_assert(IsLittleEndian()); static_assert(IsLittleEndian());
return static_cast<T>(SwapBytes48(static_cast<U>(val))); return static_cast<T>(SwapEndian48(static_cast<U>(val)));
} }
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T ConvertToLittleEndian48(const T val) { constexpr ALWAYS_INLINE T ConvertToLittleEndian48(const T val) {
using U = typename std::make_unsigned<T>::type; using U = typename std::make_unsigned<T>::type;
static_assert(sizeof(T) == sizeof(u64)); static_assert(sizeof(T) == sizeof(u64));
if constexpr (IsBigEndian()) { if constexpr (IsBigEndian()) {
return static_cast<T>(SwapBytes48(static_cast<U>(val))); return static_cast<T>(SwapEndian48(static_cast<U>(val)));
} else { } else {
static_assert(IsLittleEndian()); static_assert(IsLittleEndian());
AMS_ASSERT((static_cast<U>(val) & UINT64_C(0xFFFF000000000000)) == 0); AMS_ASSERT((static_cast<U>(val) & UINT64_C(0xFFFF000000000000)) == 0);
@ -133,22 +132,22 @@ namespace ams::util {
} }
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T LoadBigEndian(const T *ptr) { constexpr ALWAYS_INLINE T LoadBigEndian(const T *ptr) {
return ConvertToBigEndian<T>(*ptr); return ConvertToBigEndian<T>(*ptr);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE T LoadLittleEndian(const T *ptr) { constexpr ALWAYS_INLINE T LoadLittleEndian(const T *ptr) {
return ConvertToLittleEndian<T>(*ptr); return ConvertToLittleEndian<T>(*ptr);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE void StoreBigEndian(T *ptr, T val) { constexpr ALWAYS_INLINE void StoreBigEndian(T *ptr, T val) {
*ptr = ConvertToBigEndian<T>(val); *ptr = ConvertToBigEndian<T>(val);
} }
template<typename T> requires std::integral<T> template<std::integral T>
constexpr ALWAYS_INLINE void StoreLittleEndian(T *ptr, T val) { constexpr ALWAYS_INLINE void StoreLittleEndian(T *ptr, T val) {
*ptr = ConvertToLittleEndian<T>(val); *ptr = ConvertToLittleEndian<T>(val);
} }