Răsfoiți Sursa

core: is_power_of_two -> has_single_bit

Michele Caini 1 an în urmă
părinte
comite
786329c1d0
3 a modificat fișierele cu 15 adăugiri și 13 ștergeri
  1. 1 1
      docs/md/core.md
  2. 6 4
      src/entt/core/memory.hpp
  3. 8 8
      test/entt/core/memory.cpp

+ 1 - 1
docs/md/core.md

@@ -438,7 +438,7 @@ documentation to those interested in the subject.
 
 ## Power of two and fast modulus
 
-Finding out if a number is a power of two (`is_power_of_two`) or what the next
+Finding out if a number is a power of two (`has_single_bit`) or what the next
 power of two is given a random value (`next_power_of_two`) is very useful at
 times.<br/>
 For example, it helps to allocate memory in pages having a size suitable for the

+ 6 - 4
src/entt/core/memory.hpp

@@ -19,17 +19,19 @@ namespace entt {
  * @return The number of set bits in the value.
  */
 template<typename Type>
-constexpr std::enable_if_t<std::is_unsigned_v<Type>, int> popcount(Type value) noexcept {
+[[nodiscard]] constexpr std::enable_if_t<std::is_unsigned_v<Type>, int> popcount(const Type value) noexcept {
     return value ? (int(value & 1) + popcount(static_cast<Type>(value >> 1))) : 0;
 }
 
 /**
  * @brief Checks whether a value is a power of two or not (waiting for C++20 and
  * `std::has_single_bit`).
- * @param value A value that may or may not be a power of two.
+ * @tparam Type Unsigned integer type.
+ * @param value A value of unsigned integer type that may be a power of two.
  * @return True if the value is a power of two, false otherwise.
  */
-[[nodiscard]] inline constexpr bool is_power_of_two(const std::size_t value) noexcept {
+template<typename Type>
+[[nodiscard]] constexpr std::enable_if_t<std::is_unsigned_v<Type>, bool> has_single_bit(const Type value) noexcept {
     return value && ((value & (value - 1)) == 0);
 }
 
@@ -57,7 +59,7 @@ constexpr std::enable_if_t<std::is_unsigned_v<Type>, int> popcount(Type value) n
  * @return The common remainder.
  */
 [[nodiscard]] inline constexpr std::size_t fast_mod(const std::size_t value, const std::size_t mod) noexcept {
-    ENTT_ASSERT_CONSTEXPR(is_power_of_two(mod), "Value must be a power of two");
+    ENTT_ASSERT_CONSTEXPR(has_single_bit(mod), "Value must be a power of two");
     return value & (mod - 1u);
 }
 

+ 8 - 8
test/entt/core/memory.cpp

@@ -28,17 +28,17 @@ TEST(PopCount, Functionalities) {
     ASSERT_EQ(entt::popcount(201u), 4u);
 }
 
-TEST(IsPowerOfTwo, Functionalities) {
+TEST(HasSingleBit, Functionalities) {
     // constexpr-ness guaranteed
-    constexpr auto zero_is_power_of_two = entt::is_power_of_two(0u);
+    constexpr auto zero_is_power_of_two = entt::has_single_bit(0u);
 
     ASSERT_FALSE(zero_is_power_of_two);
-    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));
+    ASSERT_TRUE(entt::has_single_bit(1u));
+    ASSERT_TRUE(entt::has_single_bit(2u));
+    ASSERT_TRUE(entt::has_single_bit(4u));
+    ASSERT_FALSE(entt::has_single_bit(7u));
+    ASSERT_TRUE(entt::has_single_bit(128u));
+    ASSERT_FALSE(entt::has_single_bit(200u));
 }
 
 TEST(NextPowerOfTwo, Functionalities) {