From 2623fec1cdd5fd9b15ee67c4b115385c67129ef4 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 28 Apr 2017 14:15:31 +0800 Subject: [PATCH] include/intarith: templatize ctz/clz/cbits helpers Signed-off-by: Kefu Chai --- src/include/denc.h | 2 +- src/include/intarith.h | 66 ++++++++++++++++++++++++++++++----- src/os/bluestore/BlueStore.cc | 2 +- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/include/denc.h b/src/include/denc.h index 97b4713ce5e..e3a8f29e265 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 f46a1eb2fe2..390cdc024d7 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 b08a7bcab8e..a87422bd92d 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; } -- 2.39.5