diff --git a/include/Nazara/Core/Algorithm.hpp b/include/Nazara/Core/Algorithm.hpp index 5bb461f26..a6d5e77a7 100644 --- a/include/Nazara/Core/Algorithm.hpp +++ b/include/Nazara/Core/Algorithm.hpp @@ -22,6 +22,7 @@ namespace Nz template decltype(auto) Apply(F&& fn, Tuple&& t); template decltype(auto) Apply(O& object, F&& fn, Tuple&& t); + template constexpr std::size_t BitCount(); template ByteArray ComputeHash(HashType hash, const T& v); template ByteArray ComputeHash(AbstractHash* hash, const T& v); template constexpr std::size_t CountOf(T(&name)[N]) noexcept; diff --git a/include/Nazara/Core/Algorithm.inl b/include/Nazara/Core/Algorithm.inl index b02924446..fea1fd964 100644 --- a/include/Nazara/Core/Algorithm.inl +++ b/include/Nazara/Core/Algorithm.inl @@ -11,6 +11,7 @@ #include #include #include +#include #include namespace Nz @@ -70,6 +71,17 @@ namespace Nz return Detail::ApplyImplMethod(object, std::forward(fn), std::forward(t), std::make_index_sequence()); } + /*! + * \ingroup core + * \brief Returns the number of bits occupied by the type T + * \return Number of bits occupied by the type + */ + template + constexpr std::size_t BitCount() + { + return CHAR_BIT * sizeof(T); + } + /*! * \ingroup core * \brief Computes the hash of a hashable object diff --git a/include/Nazara/Core/Bitset.hpp b/include/Nazara/Core/Bitset.hpp index 491bf8937..2372285e5 100644 --- a/include/Nazara/Core/Bitset.hpp +++ b/include/Nazara/Core/Bitset.hpp @@ -110,7 +110,7 @@ namespace Nz Bitset& operator^=(const Bitset& bitset); static constexpr Block fullBitMask = std::numeric_limits::max(); - static constexpr std::size_t bitsPerBlock = std::numeric_limits::digits; + static constexpr std::size_t bitsPerBlock = BitCount(); static constexpr std::size_t npos = std::numeric_limits::max(); static Bitset FromPointer(const void* ptr, std::size_t bitCount, PointerSequence* sequence = nullptr); diff --git a/include/Nazara/Core/Bitset.inl b/include/Nazara/Core/Bitset.inl index 5e64bbbf4..3dc21562d 100644 --- a/include/Nazara/Core/Bitset.inl +++ b/include/Nazara/Core/Bitset.inl @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include @@ -122,13 +122,13 @@ namespace Nz { if (sizeof(T) <= sizeof(Block)) { - m_bitCount = std::numeric_limits::digits; + m_bitCount = BitCount(); m_blocks.push_back(static_cast(value)); } else { // Note: I was kinda tired when I wrote this, there's probably a much easier method than checking bits to write bits - for (std::size_t bitPos = 0; bitPos < std::numeric_limits::digits; bitPos++) + for (std::size_t bitPos = 0; bitPos < BitCount(); bitPos++) { if (value & (T(1U) << bitPos)) UnboundedSet(bitPos, true); @@ -139,9 +139,9 @@ namespace Nz /*! * \brief Appends bits to the bitset * - * This function expand the bitset with bits extracted from a numerical value + * This function extends the bitset with bits extracted from an integer value * - * \param bits A numerical value from where bits will be extracted + * \param bits An integer value from where bits will be extracted * \param bitCount Number of bits to extract from the value * * \remark This function does not require bitCount to be lower or equal to the number of bits of T, thus @@ -172,8 +172,8 @@ namespace Nz for (std::size_t block = 0; block < blockCount - 1; ++block) { m_blocks.push_back(static_cast(bits)); - bits >>= std::numeric_limits::digits; - bitCount -= std::numeric_limits::digits; + bits >>= BitCount(); + bitCount -= BitCount(); } // For the last iteration, mask out the bits we don't want @@ -331,7 +331,7 @@ namespace Nz /*! * \brief Read a byte sequence into a bitset * - * This function expand the bitset with bits read from a byte sequence + * This function extends the bitset with bits read from a byte sequence * * \param ptr A pointer to the start of the byte sequence * \param bitCount Number of bits to read from the byte sequence @@ -352,7 +352,7 @@ namespace Nz /*! * \brief Read a byte sequence into a bitset * - * This function expand the bitset with bits read from a pointer sequence (made of a pointer and a bit index) + * This function extends the bitset with bits read from a pointer sequence (made of a pointer and a bit index) * * \param sequence A pointer sequence to the start of the byte sequence * \param bitCount Number of bits to read from the byte sequence @@ -865,7 +865,7 @@ namespace Nz { static_assert(std::is_integral() && std::is_unsigned(), "T must be a unsigned integral type"); - NazaraAssert(m_bitCount <= std::numeric_limits::digits, "Bit count cannot be greater than T bit count"); + NazaraAssert(m_bitCount <= BitCount(), "Bit count cannot be greater than T bit count"); T value = 0; for (std::size_t i = 0; i < m_blocks.size(); ++i)