From: Kefu Chai Date: Sun, 21 Aug 2022 16:59:01 +0000 (+0800) Subject: include/intarith: rewrite cbits() in C++20 X-Git-Tag: v18.0.0~196^2~4 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=dc92dfc9c38ffbbbcee5b9f9705c23c5d4927569;p=ceph-ci.git include/intarith: rewrite cbits() in C++20 rewrite cbits() using concept and std::countl_zero(), for better readability and maintanability. Signed-off-by: Kefu Chai --- diff --git a/src/include/intarith.h b/src/include/intarith.h index 281aa8b3349..b3d76ecd530 100644 --- a/src/include/intarith.h +++ b/src/include/intarith.h @@ -15,6 +15,8 @@ #ifndef CEPH_INTARITH_H #define CEPH_INTARITH_H +#include +#include #include template @@ -150,36 +152,9 @@ template } // count bits (set + any 0's that follow) -template - inline typename std::enable_if< - (std::is_integral::value && - sizeof(T) <= sizeof(unsigned)), - unsigned>::type cbits(T v) { - if (v == 0) - return 0; - return (sizeof(v) * 8) - __builtin_clz(v); -} - -template - inline typename std::enable_if< - (std::is_integral::value && - sizeof(T) > sizeof(unsigned int) && - sizeof(T) <= sizeof(unsigned long)), - unsigned>::type cbits(T v) { - if (v == 0) - return 0; - return (sizeof(v) * 8) - __builtin_clzl(v); -} - -template - inline typename std::enable_if< - (std::is_integral::value && - sizeof(T) > sizeof(unsigned long) && - sizeof(T) <= sizeof(unsigned long long)), - unsigned>::type cbits(T v) { - if (v == 0) - return 0; - return (sizeof(v) * 8) - __builtin_clzll(v); +template +unsigned cbits(T v) { + return (sizeof(v) * 8) - std::countl_zero(std::make_unsigned_t(v)); } // count the bits set to 1, a.k.a. population count