/* * Copyright (c) 2018-2020 Atmosphère-NX * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, * version 2, as published by the Free Software Foundation. * * This program is distributed in the hope it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include #include namespace ams::util { namespace impl { template constexpr inline size_t Log2 = Log2 + 1; template<> constexpr inline size_t Log2<1> = 0; } template requires std::integral class BitsOf { private: static constexpr ALWAYS_INLINE int GetLsbPos(T v) { return __builtin_ctzll(static_cast(v)); } T 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)) : value(value) { /* ... */ } constexpr ALWAYS_INLINE bool operator==(const BitsOf &other) const { return this->value == other.value; } constexpr ALWAYS_INLINE bool operator!=(const BitsOf &other) const { return this->value != other.value; } constexpr ALWAYS_INLINE int operator*() const { return GetLsbPos(this->value); } constexpr ALWAYS_INLINE BitsOf &operator++() { this->value &= ~(T(1u) << GetLsbPos(this->value)); return *this; } constexpr ALWAYS_INLINE BitsOf &operator++(int) { BitsOf ret(this->value); ++(*this); return ret; } constexpr ALWAYS_INLINE BitsOf begin() const { return *this; } constexpr ALWAYS_INLINE BitsOf end() const { return BitsOf(T(0u)); } }; template requires std::integral constexpr ALWAYS_INLINE T CombineBits(Args... args) { return (... | (T(1u) << args)); } template requires std::integral constexpr ALWAYS_INLINE T ResetLeastSignificantOneBit(T x) { return x & (x - 1); } template requires std::integral 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 constexpr ALWAYS_INLINE T ResetTrailingOnes(T x) { return x & (x + 1); } template requires std::integral constexpr ALWAYS_INLINE T SetTrailingZeros(T x) { return x | (x - 1); } template requires std::integral constexpr ALWAYS_INLINE T MaskTrailingZeros(T x) { return (~x) & (x - 1); } template requires std::integral constexpr ALWAYS_INLINE T MaskTrailingOnes(T x) { return ~((~x) | (x + 1)); } template requires std::integral constexpr ALWAYS_INLINE T MaskTrailingZerosAndLeastSignificantOneBit(T x) { return x ^ (x - 1); } template requires std::integral constexpr ALWAYS_INLINE T MaskTrailingOnesAndLeastSignificantZeroBit(T x) { return x ^ (x + 1); } template requires std::integral constexpr ALWAYS_INLINE int PopCount(T x) { /* TODO: C++20 std::bit_cast */ using U = typename std::make_unsigned::type; U u = static_cast(x); if (std::is_constant_evaluated()) { /* 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 >> 1) & m1)); u = static_cast((u & m2) + ((u >> 2) & m2)); u = static_cast((u + (u >> 4)) & m4); for (size_t i = 0; i < impl::Log2; ++i) { const size_t shift = (0x1 << i) * BITSIZEOF(u8); u += u >> shift; } return static_cast(u & 0x7Fu); } else { if constexpr (std::is_same::value) { return __builtin_popcountll(u); } else if constexpr (std::is_same::value) { return __builtin_popcountl(u); } else { static_assert(sizeof(U) <= sizeof(unsigned int)); return __builtin_popcount(static_cast(u)); } } } template requires std::integral constexpr ALWAYS_INLINE int CountLeadingZeros(T x) { if (std::is_constant_evaluated()) { for (size_t i = 0; i < impl::Log2; ++i) { const size_t shift = (0x1 << i); x |= x >> shift; } return PopCount(static_cast(~x)); } else { /* TODO: C++20 std::bit_cast */ using U = typename std::make_unsigned::type; const U u = static_cast(x); if constexpr (std::is_same::value) { return __builtin_clzll(u); } else if constexpr (std::is_same::value) { return __builtin_clzl(u); } else if constexpr(std::is_same::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(u)) - BitDiff; } } } template requires std::integral constexpr ALWAYS_INLINE bool IsPowerOfTwo(T x) { return x > 0 && ResetLeastSignificantOneBit(x) == 0; } template requires std::integral constexpr ALWAYS_INLINE T CeilingPowerOfTwo(T x) { AMS_ASSERT(x > 0); return T(1) << (BITSIZEOF(T) - CountLeadingZeros(T(x - 1))); } template requires std::integral constexpr ALWAYS_INLINE T FloorPowerOfTwo(T x) { AMS_ASSERT(x > 0); return T(1) << (BITSIZEOF(T) - CountLeadingZeros(x) - 1); } }