diff --git a/src/entt/core/memory.hpp b/src/entt/core/memory.hpp index 220483c25..08cd76dc3 100644 --- a/src/entt/core/memory.hpp +++ b/src/entt/core/memory.hpp @@ -74,19 +74,13 @@ constexpr void propagate_on_container_swap(Allocator &lhs, Allocator &rhs) ENTT_ /** - * @brief Utility class to check whether a value is a power of two or not. - * @tparam Value A value that may or may not be a power of two. + * @brief Checks whether a value is a power of two or not. + * @param value A value that may or may not be a power of two. + * @return True if the value is a power of two, false otherwise. */ -template -using is_power_of_two = std::bool_constant; - - -/** - * @brief Helper variable template. - * @tparam Value A value that may or may not be a power of two. - */ -template -inline constexpr bool is_power_of_two_v = is_power_of_two::value; +[[nodiscard]] inline constexpr bool is_power_of_two(const std::size_t value) ENTT_NOEXCEPT { + return value && ((value & (value - 1)) == 0); +} /** @@ -97,7 +91,7 @@ inline constexpr bool is_power_of_two_v = is_power_of_two::value; */ template [[nodiscard]] constexpr std::size_t fast_mod(const std::size_t value) ENTT_NOEXCEPT { - static_assert(is_power_of_two_v, "Value must be a power of two"); + static_assert(is_power_of_two(Value), "Value must be a power of two"); return value & (Value - 1u); } diff --git a/test/entt/core/memory.cpp b/test/entt/core/memory.cpp index 14fe58877..5e64f0514 100644 --- a/test/entt/core/memory.cpp +++ b/test/entt/core/memory.cpp @@ -33,13 +33,13 @@ TEST(Memory, PoccaPocmaAndPocs) { } TEST(Memory, IsPowerOfTwo) { - ASSERT_FALSE(entt::is_power_of_two_v<0u>); - ASSERT_TRUE(entt::is_power_of_two_v<1u>); - ASSERT_TRUE(entt::is_power_of_two_v<2u>); - ASSERT_TRUE(entt::is_power_of_two_v<4u>); - ASSERT_FALSE(entt::is_power_of_two_v<7u>); - ASSERT_TRUE(entt::is_power_of_two_v<128u>); - ASSERT_FALSE(entt::is_power_of_two_v<200u>); + ASSERT_FALSE(entt::is_power_of_two(0u)); + ASSERT_TRUE(entt::is_power_of_two(1u)); + ASSERT_TRUE(entt::is_power_of_two(2u)); + ASSERT_TRUE(entt::is_power_of_two(4u)); + ASSERT_FALSE(entt::is_power_of_two(7u)); + ASSERT_TRUE(entt::is_power_of_two(128u)); + ASSERT_FALSE(entt::is_power_of_two(200u)); } TEST(Memory, FastMod) {