]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
include/intarith: rewrite cbits() in C++20
authorKefu Chai <tchaikov@gmail.com>
Sun, 21 Aug 2022 16:59:01 +0000 (00:59 +0800)
committerKefu Chai <tchaikov@gmail.com>
Mon, 22 Aug 2022 10:03:59 +0000 (18:03 +0800)
rewrite cbits() using concept and std::countl_zero(), for better
readability and maintanability.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/include/intarith.h

index 281aa8b33497cc9b11a06c16217afa99e91a6751..b3d76ecd5305db8d17b8e8ebd71b11e5850ef448 100644 (file)
@@ -15,6 +15,8 @@
 #ifndef CEPH_INTARITH_H
 #define CEPH_INTARITH_H
 
+#include <bit>
+#include <concepts>
 #include <type_traits>
 
 template<typename T, typename U>
@@ -150,36 +152,9 @@ template<class T>
 }
 
 // count bits (set + any 0's that follow)
-template<class T>
-  inline typename std::enable_if<
-  (std::is_integral<T>::value &&
-   sizeof(T) <= sizeof(unsigned)),
-  unsigned>::type cbits(T v) {
-  if (v == 0)
-    return 0;
-  return (sizeof(v) * 8) - __builtin_clz(v);
-}
-
-template<class T>
-  inline typename std::enable_if<
-  (std::is_integral<T>::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<class T>
-  inline typename std::enable_if<
-  (std::is_integral<T>::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<std::integral T>
+unsigned cbits(T v) {
+  return (sizeof(v) * 8) - std::countl_zero(std::make_unsigned_t<T>(v));
 }
 
 // count the bits set to 1, a.k.a. population count