mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-19 01:15:08 -04:00
fs: implement PooledBuffer
This commit is contained in:
parent
50a91b1d6e
commit
496be5ecd4
5 changed files with 521 additions and 6 deletions
|
@ -20,6 +20,16 @@
|
|||
|
||||
namespace ams::util {
|
||||
|
||||
namespace impl {
|
||||
|
||||
template<size_t N>
|
||||
constexpr inline size_t Log2 = Log2<N / 2> + 1;
|
||||
|
||||
template<>
|
||||
constexpr inline size_t Log2<1> = 0;
|
||||
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class BitsOf {
|
||||
private:
|
||||
|
@ -73,4 +83,133 @@ namespace ams::util {
|
|||
return (... | (T(1u) << args));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T ResetLeastSignificantOneBit(T x) {
|
||||
return x & (x - 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T SetLeastSignificantZeroBit(T x) {
|
||||
return x | (x + 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T LeastSignificantOneBit(T x) {
|
||||
return x & ~(x - 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T LeastSignificantZeroBit(T x) {
|
||||
return ~x & (x + 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T ResetTrailingOnes(T x) {
|
||||
return x & (x + 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T SetTrailingZeros(T x) {
|
||||
return x | (x - 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T MaskTrailingZeros(T x) {
|
||||
return (~x) & (x - 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T MaskTrailingOnes(T x) {
|
||||
return ~((~x) | (x + 1));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T MaskTrailingZerosAndLeastSignificantOneBit(T x) {
|
||||
return x ^ (x - 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T MaskTrailingOnesAndLeastSignificantZeroBit(T x) {
|
||||
return x ^ (x + 1);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE int PopCount(T x) {
|
||||
/* TODO: C++20 std::bit_cast */
|
||||
using U = typename std::make_unsigned<T>::type;
|
||||
U u = static_cast<U>(x);
|
||||
|
||||
/* TODO: C++20 std::is_constant_evaluated */
|
||||
if (false) {
|
||||
/* https://en.wikipedia.org/wiki/Hamming_weight */
|
||||
constexpr U m1 = U(-1) / 0x03;
|
||||
constexpr U m2 = U(-1) / 0x05;
|
||||
constexpr U m4 = U(-1) / 0x11;
|
||||
|
||||
u = static_cast<U>(u - ((u >> 1) & m1));
|
||||
u = static_cast<U>((u & m2) + ((u >> 2) & m2));
|
||||
u = static_cast<U>((u + (u >> 4)) & m4);
|
||||
|
||||
for (size_t i = 0; i < impl::Log2<sizeof(T)>; ++i) {
|
||||
const size_t shift = (0x1 << i) * BITSIZEOF(u8);
|
||||
u += u >> shift;
|
||||
}
|
||||
|
||||
return static_cast<int>(u & 0x7Fu);
|
||||
} else {
|
||||
if constexpr (std::is_same<U, unsigned long long>::value) {
|
||||
return __builtin_popcountll(u);
|
||||
} else if constexpr (std::is_same<U, unsigned long>::value) {
|
||||
return __builtin_popcountl(u);
|
||||
} else {
|
||||
static_assert(sizeof(U) <= sizeof(unsigned int));
|
||||
return __builtin_popcount(static_cast<unsigned int>(u));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE int CountLeadingZeros(T x) {
|
||||
/* TODO: C++20 std::is_constant_evaluated */
|
||||
if (false) {
|
||||
for (size_t i = 0; i < impl::Log2<BITSIZEOF(T)>; ++i) {
|
||||
const size_t shift = (0x1 << i);
|
||||
x |= x >> shift;
|
||||
}
|
||||
return PopCount(static_cast<T>(~x));
|
||||
} else {
|
||||
/* TODO: C++20 std::bit_cast */
|
||||
using U = typename std::make_unsigned<T>::type;
|
||||
const U u = static_cast<U>(x);
|
||||
if constexpr (std::is_same<U, unsigned long long>::value) {
|
||||
return __builtin_clzll(u);
|
||||
} else if constexpr (std::is_same<U, unsigned long>::value) {
|
||||
return __builtin_clzl(u);
|
||||
} else if constexpr(std::is_same<U, unsigned int>::value) {
|
||||
return __builtin_clz(u);
|
||||
} else {
|
||||
static_assert(sizeof(U) < sizeof(unsigned int));
|
||||
constexpr size_t BitDiff = BITSIZEOF(unsigned int) - BITSIZEOF(U);
|
||||
return __builtin_clz(static_cast<unsigned int>(u)) - BitDiff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE bool IsPowerOfTwo(T x) {
|
||||
return x > 0 && ResetLeastSignificantOneBit(x) == 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T CeilingPowerOfTwo(T x) {
|
||||
AMS_ASSERT(x > 0);
|
||||
return T(1) << (BITSIZEOF(T) - CountLeadingZeros(T(x - 1)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
constexpr ALWAYS_INLINE T FloorPowerOfTwo(T x) {
|
||||
AMS_ASSERT(x > 0);
|
||||
return T(1) << (BITSIZEOF(T) - CountLeadingZeros(x) - 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue