From: Kefu Chai Date: Fri, 28 Apr 2017 06:15:31 +0000 (+0800) Subject: include/intarith: templatize ctz/clz/cbits helpers X-Git-Tag: v12.0.3~174^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2623fec1cdd5fd9b15ee67c4b115385c67129ef4;p=ceph.git include/intarith: templatize ctz/clz/cbits helpers Signed-off-by: Kefu Chai --- diff --git a/src/include/denc.h b/src/include/denc.h index 97b4713ce5e6..e3a8f29e2651 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -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; diff --git a/src/include/intarith.h b/src/include/intarith.h index f46a1eb2fe28..390cdc024d7e 100644 --- a/src/include/intarith.h +++ b/src/include/intarith.h @@ -78,17 +78,33 @@ // count trailing zeros. // NOTE: the builtin is nondeterministic on 0 input -static inline unsigned ctz(unsigned v) { +template + inline typename std::enable_if< + (std::is_integral::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 + inline typename std::enable_if< + (std::is_integral::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 + inline typename std::enable_if< + (std::is_integral::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 + inline typename std::enable_if< + (std::is_integral::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 + inline typename std::enable_if< + (std::is_integral::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 + inline typename std::enable_if< + (std::is_integral::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 + 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); } -static inline unsigned cbitsl(unsigned long 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); } -static inline unsigned cbitsll(unsigned long long 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); diff --git a/src/os/bluestore/BlueStore.cc b/src/os/bluestore/BlueStore.cc index b08a7bcab8ed..a87422bd92d8 100644 --- a/src/os/bluestore/BlueStore.cc +++ b/src/os/bluestore/BlueStore.cc @@ -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; }