From 690d48fe2c5bbc75f2b2472c3db1a85d65da2c01 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Mon, 22 Aug 2022 00:50:43 +0800 Subject: [PATCH] denc, os: s/ctz/std::countr_zero/ prefer the facilities provided by standard library over the homebrew ones for better readability and maintainability. Signed-off-by: Kefu Chai --- src/common/histogram.h | 2 +- src/crimson/os/seastore/seastore_types.h | 3 ++- src/include/denc.h | 8 ++++---- src/os/bluestore/BlueStore.cc | 14 +++++++------- src/os/bluestore/bluestore_types.h | 2 +- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/common/histogram.h b/src/common/histogram.h index 828ed47c4e21e..cdaca61c27764 100644 --- a/src/common/histogram.h +++ b/src/common/histogram.h @@ -14,8 +14,8 @@ #define CEPH_HISTOGRAM_H #include - #include "include/encoding.h" +#include "include/intarith.h" namespace ceph { class Formatter; diff --git a/src/crimson/os/seastore/seastore_types.h b/src/crimson/os/seastore/seastore_types.h index 250c791a49141..ae6aa64ecf8d1 100644 --- a/src/crimson/os/seastore/seastore_types.h +++ b/src/crimson/os/seastore/seastore_types.h @@ -15,8 +15,9 @@ #include "include/byteorder.h" #include "include/denc.h" #include "include/buffer.h" -#include "include/uuid.h" +#include "include/intarith.h" #include "include/interval_set.h" +#include "include/uuid.h" namespace crimson::os::seastore { diff --git a/src/include/denc.h b/src/include/denc.h index 621da9e70bcc2..0f080abc678a0 100644 --- a/src/include/denc.h +++ b/src/include/denc.h @@ -25,6 +25,7 @@ #define _ENC_DEC_H #include +#include #include #include #include @@ -41,7 +42,6 @@ #include #include "include/compat.h" -#include "include/intarith.h" #include "include/int_types.h" #include "include/scope_guard.h" @@ -477,7 +477,7 @@ inline void denc_varint_lowz(uint64_t v, size_t& p) { } inline void denc_varint_lowz(uint64_t v, ceph::buffer::list::contiguous_appender& p) { - int lowznib = v ? (ctz(v) / 4) : 0; + int lowznib = v ? (std::countr_zero(v) / 4) : 0; if (lowznib > 3) lowznib = 3; v >>= lowznib * 4; @@ -514,7 +514,7 @@ inline void denc_signed_varint_lowz(int64_t v, It& p) { v = -v; negative = true; } - unsigned lowznib = v ? (ctz(v) / 4) : 0u; + unsigned lowznib = v ? (std::countr_zero(std::bit_cast(v)) / 4) : 0u; if (lowznib > 3) lowznib = 3; v >>= lowznib * 4; @@ -559,7 +559,7 @@ inline void denc_lba(uint64_t v, size_t& p) { template requires (!is_const_iterator) inline void denc_lba(uint64_t v, It& p) { - int low_zero_nibbles = v ? (int)(ctz(v) / 4) : 0; + int low_zero_nibbles = v ? std::countr_zero(v) / 4 : 0; int pos; uint32_t word; int t = low_zero_nibbles - 3; diff --git a/src/os/bluestore/BlueStore.cc b/src/os/bluestore/BlueStore.cc index e96ccbc5b4389..89748853babbd 100644 --- a/src/os/bluestore/BlueStore.cc +++ b/src/os/bluestore/BlueStore.cc @@ -4601,7 +4601,7 @@ BlueStore::BlueStore(CephContext *cct, zoned_cleaner_thread(this), #endif min_alloc_size(_min_alloc_size), - min_alloc_size_order(ctz(_min_alloc_size)), + min_alloc_size_order(std::countr_zero(_min_alloc_size)), mempool_thread(this) { _init_logger(); @@ -5531,7 +5531,7 @@ int BlueStore::_open_bdev(bool create) // initialize global block parameters block_size = bdev->get_block_size(); block_mask = ~(block_size - 1); - block_size_order = ctz(block_size); + block_size_order = std::countr_zero(block_size); ceph_assert(block_size == 1u << block_size_order); _set_max_defer_interval(); // and set cache_size based on device type @@ -12301,7 +12301,7 @@ int BlueStore::_open_super_meta() uint64_t val; decode(val, p); min_alloc_size = val; - min_alloc_size_order = ctz(val); + min_alloc_size_order = std::countr_zero(val); min_alloc_size_mask = min_alloc_size - 1; ceph_assert(min_alloc_size == 1u << min_alloc_size_order); @@ -15587,7 +15587,7 @@ int BlueStore::_do_alloc_write( if (wi.compressed) { final_length = wi.compressed_bl.length(); csum_length = final_length; - unsigned csum_order = ctz(csum_length); + unsigned csum_order = std::countr_zero(csum_length); l = &wi.compressed_bl; dblob.set_compressed(wi.blob_length, wi.compressed_len); if (csum != Checksummer::CSUM_NONE) { @@ -15611,7 +15611,7 @@ int BlueStore::_do_alloc_write( << block_size_order << dendl; csum_order = block_size_order; } else { - csum_order = std::min(wctx->csum_order, ctz(l->length())); + csum_order = std::min(wctx->csum_order, std::countr_zero(l->length())); } // try to align blob with max_blob_size to improve // its reuse ratio, e.g. in case of reverse write @@ -15944,7 +15944,7 @@ void BlueStore::_choose_write_options( if (o->onode.expected_write_size) { wctx->csum_order = std::max(min_alloc_size_order, - (uint8_t)ctz(o->onode.expected_write_size)); + (uint8_t)std::countr_zero(o->onode.expected_write_size)); } else { wctx->csum_order = min_alloc_size_order; } @@ -19007,7 +19007,7 @@ int BlueStore::reconstruct_allocations(SimpleBitmap *sbmap, read_alloc_stats_t & //----------------------------------------------------------------------------------- static void copy_simple_bitmap_to_allocator(SimpleBitmap* sbmap, Allocator* dest_alloc, uint64_t alloc_size) { - int alloc_size_shift = ctz(alloc_size); + int alloc_size_shift = std::countr_zero(alloc_size); uint64_t offset = 0; extent_t ext = sbmap->get_next_clr_extent(offset); while (ext.length != 0) { diff --git a/src/os/bluestore/bluestore_types.h b/src/os/bluestore/bluestore_types.h index 84461daf2ae73..9c4fc5bfdc3d4 100644 --- a/src/os/bluestore/bluestore_types.h +++ b/src/os/bluestore/bluestore_types.h @@ -1222,7 +1222,7 @@ public: : ref_counter_2hash_tracker_t(mem_cap) { ceph_assert(alloc_unit); ceph_assert(std::has_single_bit(alloc_unit)); - au_void_bits = ctz(alloc_unit); + au_void_bits = std::countr_zero(alloc_unit); } void inc(uint64_t sbid, uint64_t offset, int n); void inc_range(uint64_t sbid, uint64_t offset, uint32_t len, int n); -- 2.39.5