Просмотр исходного кода

core: next_power_of_two -> std::bit_ceil

skypjack 3 месяцев назад
Родитель
Сommit
cd28e6269b

+ 2 - 1
src/entt/container/dense_map.hpp

@@ -1,6 +1,7 @@
 #ifndef ENTT_CONTAINER_DENSE_MAP_HPP
 #define ENTT_CONTAINER_DENSE_MAP_HPP
 
+#include <bit>
 #include <cmath>
 #include <cstddef>
 #include <functional>
@@ -1010,7 +1011,7 @@ public:
         const auto cap = static_cast<size_type>(static_cast<float>(size()) / max_load_factor());
         value = value > cap ? value : cap;
 
-        if(const auto sz = next_power_of_two(value); sz != bucket_count()) {
+        if(const auto sz = std::bit_ceil(value); sz != bucket_count()) {
             sparse.first().resize(sz);
 
             for(auto &&elem: sparse.first()) {

+ 2 - 1
src/entt/container/dense_set.hpp

@@ -1,6 +1,7 @@
 #ifndef ENTT_CONTAINER_DENSE_SET_HPP
 #define ENTT_CONTAINER_DENSE_SET_HPP
 
+#include <bit>
 #include <cmath>
 #include <cstddef>
 #include <functional>
@@ -879,7 +880,7 @@ public:
         const auto cap = static_cast<size_type>(static_cast<float>(size()) / max_load_factor());
         value = value > cap ? value : cap;
 
-        if(const auto sz = next_power_of_two(value); sz != bucket_count()) {
+        if(const auto sz = std::bit_ceil(value); sz != bucket_count()) {
             sparse.first().resize(sz);
 
             for(auto &&elem: sparse.first()) {

+ 0 - 20
src/entt/core/bit.hpp

@@ -9,26 +9,6 @@
 
 namespace entt {
 
-/**
- * @brief Computes the smallest power of two greater than or equal to a value
- * (waiting for C++20 and `std::bit_ceil`).
- * @tparam Type Unsigned integer type.
- * @param value A value of unsigned integer type.
- * @return The smallest power of two greater than or equal to the given value.
- */
-template<typename Type>
-[[nodiscard]] constexpr std::enable_if_t<std::is_unsigned_v<Type>, Type> next_power_of_two(const Type value) noexcept {
-    // NOLINTNEXTLINE(bugprone-assert-side-effect)
-    ENTT_ASSERT_CONSTEXPR(value < (Type{1u} << (std::numeric_limits<Type>::digits - 1)), "Numeric limits exceeded");
-    Type curr = value - (value != 0u);
-
-    for(int next = 1; next < std::numeric_limits<Type>::digits; next = next * 2) {
-        curr |= (curr >> next);
-    }
-
-    return ++curr;
-}
-
 /**
  * @brief Fast module utility function (powers of two only).
  * @tparam Type Unsigned integer type.

+ 2 - 2
test/entt/container/dense_map.cpp

@@ -1,4 +1,5 @@
 #include <array>
+#include <bit>
 #include <cmath>
 #include <cstddef>
 #include <cstdint>
@@ -11,7 +12,6 @@
 #include <vector>
 #include <gtest/gtest.h>
 #include <entt/container/dense_map.hpp>
-#include <entt/core/bit.hpp>
 #include <entt/core/iterator.hpp>
 #include <entt/core/utility.hpp>
 #include "../../common/config.h"
@@ -1160,7 +1160,7 @@ TEST(DenseMap, Reserve) {
     map.reserve(minimum_bucket_count);
 
     ASSERT_EQ(map.bucket_count(), 2 * minimum_bucket_count);
-    ASSERT_EQ(map.bucket_count(), entt::next_power_of_two(static_cast<std::size_t>(std::ceil(minimum_bucket_count / map.max_load_factor()))));
+    ASSERT_EQ(map.bucket_count(), std::bit_ceil(static_cast<std::size_t>(std::ceil(minimum_bucket_count / map.max_load_factor()))));
 }
 
 TEST(DenseMap, ThrowingAllocator) {

+ 2 - 2
test/entt/container/dense_set.cpp

@@ -1,4 +1,5 @@
 #include <array>
+#include <bit>
 #include <cmath>
 #include <cstddef>
 #include <functional>
@@ -10,7 +11,6 @@
 #include <vector>
 #include <gtest/gtest.h>
 #include <entt/container/dense_set.hpp>
-#include <entt/core/bit.hpp>
 #include <entt/core/utility.hpp>
 #include "../../common/linter.hpp"
 #include "../../common/throwing_allocator.hpp"
@@ -975,7 +975,7 @@ TEST(DenseSet, Reserve) {
     set.reserve(minimum_bucket_count);
 
     ASSERT_EQ(set.bucket_count(), 2 * minimum_bucket_count);
-    ASSERT_EQ(set.bucket_count(), entt::next_power_of_two(static_cast<std::size_t>(std::ceil(minimum_bucket_count / set.max_load_factor()))));
+    ASSERT_EQ(set.bucket_count(), std::bit_ceil(static_cast<std::size_t>(std::ceil(minimum_bucket_count / set.max_load_factor()))));
 }
 
 TEST(DenseSet, ThrowingAllocator) {

+ 1 - 20
test/entt/core/bit.cpp

@@ -5,26 +5,7 @@
 #include <entt/core/bit.hpp>
 #include "../../common/config.h"
 
-TEST(NextPowerOfTwo, Functionalities) {
-    // 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);
-    ASSERT_EQ(entt::next_power_of_two(17u), 32u);
-    ASSERT_EQ(entt::next_power_of_two(32u), 32u);
-    ASSERT_EQ(entt::next_power_of_two(33u), 64u);
-    ASSERT_EQ(entt::next_power_of_two(static_cast<std::size_t>(std::pow(2, 16))), static_cast<std::size_t>(std::pow(2, 16)));
-    ASSERT_EQ(entt::next_power_of_two(static_cast<std::size_t>(std::pow(2, 16) + 1u)), static_cast<std::size_t>(std::pow(2, 17)));
-}
-
-ENTT_DEBUG_TEST(NextPowerOfTwoDeathTest, Functionalities) {
-    ASSERT_DEATH(static_cast<void>(entt::next_power_of_two((std::size_t{1u} << (std::numeric_limits<std::size_t>::digits - 1)) + 1)), "");
-}
-
-TEST(FastMod, Functionalities) {
+TEST(Bit, FastMod) {
     // constexpr-ness guaranteed
     constexpr auto fast_mod_of_zero = entt::fast_mod(0u, 8u);