]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
include/intarith: templatize ctz/clz/cbits helpers
authorKefu Chai <kchai@redhat.com>
Fri, 28 Apr 2017 06:15:31 +0000 (14:15 +0800)
committerKefu Chai <kchai@redhat.com>
Fri, 28 Apr 2017 06:15:31 +0000 (14:15 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/include/denc.h
src/include/intarith.h
src/os/bluestore/BlueStore.cc

index 97b4713ce5e642a5e113f45e5c38da7164e5ac80..e3a8f29e2651857eaafa4d47813f8c419a3561bb 100644 (file)
@@ -344,7 +344,7 @@ inline void denc_signed_varint_lowz(int64_t v,
     v = -v;
     negative = true;
   }
-  int lowznib = v ? (ctz(v) / 4) : 0;
+  unsigned lowznib = v ? (ctz(v) / 4) : 0u;
   if (lowznib > 3)
     lowznib = 3;
   v >>= lowznib * 4;
index f46a1eb2fe288b70e37c24aad0f45f0912f27073..390cdc024d7e8cea77838ef64c81ca948d4f7a49 100644 (file)
 
 // count trailing zeros.
 // NOTE: the builtin is nondeterministic on 0 input
-static inline unsigned ctz(unsigned v) {
+template<class T>
+  inline typename std::enable_if<
+  (std::is_integral<T>::value &&
+   sizeof(T) <= sizeof(unsigned)),
+  unsigned>::type ctz(T v) {
   if (v == 0)
     return sizeof(v) * 8;
   return __builtin_ctz(v);
 }
-static inline unsigned ctzl(unsigned long 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 ctz(T v) {
   if (v == 0)
     return sizeof(v) * 8;
   return __builtin_ctzl(v);
 }
-static inline unsigned ctzll(unsigned long long 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 ctz(T v) {
   if (v == 0)
     return sizeof(v) * 8;
   return __builtin_ctzll(v);
@@ -96,34 +112,66 @@ static inline unsigned ctzll(unsigned long long v) {
 
 // count leading zeros
 // NOTE: the builtin is nondeterministic on 0 input
-static inline unsigned clz(unsigned v) {
+template<class T>
+  inline typename std::enable_if<
+  (std::is_integral<T>::value &&
+   sizeof(T) <= sizeof(unsigned)),
+  unsigned>::type clz(T v) {
   if (v == 0)
     return sizeof(v) * 8;
   return __builtin_clz(v);
 }
-static inline unsigned clzl(unsigned long 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 clz(T v) {
   if (v == 0)
     return sizeof(v) * 8;
   return __builtin_clzl(v);
 }
-static inline unsigned clzll(unsigned long long 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 clz(T v) {
   if (v == 0)
     return sizeof(v) * 8;
   return __builtin_clzll(v);
 }
 
 // count bits (set + any 0's that follow)
-static inline unsigned cbits(unsigned v) {
+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);
 }
-static inline unsigned cbitsl(unsigned long 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);
 }
-static inline unsigned cbitsll(unsigned long long 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);
index b08a7bcab8ed3a8da966ff273a806fad8ac0076d..a87422bd92d8ea05a44dfd1b9b72e60563632966 100644 (file)
@@ -9661,7 +9661,7 @@ int BlueStore::_do_write(
     auto order = min_alloc_size_order.load();
     if (o->onode.expected_write_size) {
       wctx.csum_order = std::max(order,
-                                (size_t)ctzl(o->onode.expected_write_size));
+                                (uint8_t)ctz(o->onode.expected_write_size));
     } else {
       wctx.csum_order = order;
     }