From c543f712353e95bb2b7f606697682c28ec36c1c4 Mon Sep 17 00:00:00 2001 From: Shin-ichi MORITA Date: Sun, 9 Jul 2023 06:53:34 +0900 Subject: [PATCH] Use C++20 bit manipulation. --- include/xemmai/heap.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/include/xemmai/heap.h b/include/xemmai/heap.h index c0a90443..9f87012e 100644 --- a/include/xemmai/heap.h +++ b/include/xemmai/heap.h @@ -3,6 +3,7 @@ #include "portable/define.h" #include +#include #include #include #include @@ -16,10 +17,6 @@ namespace xemmai template class t_heap { - static constexpr size_t f_log(size_t a_x) - { - return a_x > 1 ? f_log(a_x >> 1) + 1 : 0; - } static void* f_map(size_t a_n) { #ifdef __unix__ @@ -139,10 +136,10 @@ class t_heap constexpr T* f_allocate_medium(size_t a_size); public: - static constexpr size_t V_UNIT = 2 << f_log(sizeof(T) - 1); + static constexpr size_t V_UNIT = std::bit_ceil(sizeof(T)); static_assert(V_UNIT >> 1 < sizeof(T)); static_assert(V_UNIT >= sizeof(T)); - static constexpr size_t V_RANKX = sizeof(void*) * 8 - f_log(V_UNIT); + static constexpr size_t V_RANKX = std::countl_zero(V_UNIT - 1); static_assert((V_UNIT << (V_RANKX - 1)) - 1 == ~size_t(0) >> 1); static_assert((V_UNIT << V_RANKX) - 1 == ~size_t(0));