Browse Source

memory:
* use ENTT_ASSERT_CONSTEXPR
* turn the power-of-two toolset into a constexpr one as it ought to be

Michele Caini 3 years ago
parent
commit
4398c962e7
2 changed files with 18 additions and 9 deletions
  1. 6 6
      src/entt/core/memory.hpp
  2. 12 3
      test/entt/core/memory.cpp

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

@@ -16,7 +16,7 @@ namespace entt {
  * @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.
  */
-[[nodiscard]] inline bool is_power_of_two(const std::size_t value) noexcept {
+[[nodiscard]] inline constexpr bool is_power_of_two(const std::size_t value) noexcept {
     return value && ((value & (value - 1)) == 0);
 }
 
@@ -25,8 +25,8 @@ namespace entt {
  * @param value The value to use.
  * @return The smallest power of two greater than or equal to the given value.
  */
-[[nodiscard]] inline std::size_t next_power_of_two(const std::size_t value) noexcept {
-    ENTT_ASSERT(value < (std::size_t{1u} << (std::numeric_limits<std::size_t>::digits - 1)), "Numeric limits exceeded");
+[[nodiscard]] inline constexpr std::size_t next_power_of_two(const std::size_t value) noexcept {
+    ENTT_ASSERT_CONSTEXPR(value < (std::size_t{1u} << (std::numeric_limits<std::size_t>::digits - 1)), "Numeric limits exceeded");
     std::size_t curr = value - (value != 0u);
 
     for(int next = 1; next < std::numeric_limits<std::size_t>::digits; next = next * 2) {
@@ -42,8 +42,8 @@ namespace entt {
  * @param mod _Modulus_, it must be a power of two.
  * @return The common remainder.
  */
-[[nodiscard]] inline std::size_t fast_mod(const std::size_t value, const std::size_t mod) noexcept {
-    ENTT_ASSERT(is_power_of_two(mod), "Value must be a power of two");
+[[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");
     return value & (mod - 1u);
 }
 
@@ -100,7 +100,7 @@ constexpr void propagate_on_container_swap([[maybe_unused]] Allocator &lhs, [[ma
         using std::swap;
         swap(lhs, rhs);
     } else {
-        ENTT_ASSERT(lhs == rhs, "Cannot swap the containers");
+        ENTT_ASSERT_CONSTEXPR(lhs == rhs, "Cannot swap the containers");
     }
 }
 

+ 12 - 3
test/entt/core/memory.cpp

@@ -37,7 +37,10 @@ ENTT_DEBUG_TEST(PoccaPocmaAndPocsDeathTest, Functionalities) {
 }
 
 TEST(IsPowerOfTwo, Functionalities) {
-    ASSERT_FALSE(entt::is_power_of_two(0u));
+    // constexpr-ness guaranteed
+    constexpr auto zero_is_power_of_two = entt::is_power_of_two(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));
@@ -47,7 +50,10 @@ TEST(IsPowerOfTwo, Functionalities) {
 }
 
 TEST(NextPowerOfTwo, Functionalities) {
-    ASSERT_EQ(entt::next_power_of_two(0u), 1u);
+    // constexpr-ness guaranteed
+    constexpr auto next_power_of_two_of_zero = entt::next_power_of_two(0u);
+
+    ASSERT_EQ(next_power_of_two_of_zero, 1u);
     ASSERT_EQ(entt::next_power_of_two(1u), 1u);
     ASSERT_EQ(entt::next_power_of_two(2u), 2u);
     ASSERT_EQ(entt::next_power_of_two(3u), 4u);
@@ -63,7 +69,10 @@ ENTT_DEBUG_TEST(NextPowerOfTwoDeathTest, Functionalities) {
 }
 
 TEST(FastMod, Functionalities) {
-    ASSERT_EQ(entt::fast_mod(0u, 8u), 0u);
+    // constexpr-ness guaranteed
+    constexpr auto fast_mod_of_zero = entt::fast_mod(0u, 8u);
+
+    ASSERT_EQ(fast_mod_of_zero, 0u);
     ASSERT_EQ(entt::fast_mod(7u, 8u), 7u);
     ASSERT_EQ(entt::fast_mod(8u, 8u), 0u);
 }