From 730b9c5afb03bd054adbac03a00b8946bf2e814f Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Wed, 27 Dec 2023 16:18:50 +0100 Subject: [PATCH] include/intarith: introduce round_down_to() Same as with round_up_to(), d isn't required to be a power of two. Signed-off-by: Ilya Dryomov (cherry picked from commit 94bf3a5d74cfd61e0bd881ca2863fe2205f27767) --- src/include/intarith.h | 4 ++++ src/librbd/api/DiffIterate.cc | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/include/intarith.h b/src/include/intarith.h index d3eaff15635f7..f43981a6530f4 100644 --- a/src/include/intarith.h +++ b/src/include/intarith.h @@ -22,6 +22,10 @@ constexpr inline std::make_unsigned_t> div_round_up(T n return (n + d - 1) / d; } +template +constexpr inline std::make_unsigned_t> round_down_to(T n, U d) { + return n - n % d; +} template constexpr inline std::make_unsigned_t> round_up_to(T n, U d) { diff --git a/src/librbd/api/DiffIterate.cc b/src/librbd/api/DiffIterate.cc index 6c86f56b3eea3..d2003101f28b2 100644 --- a/src/librbd/api/DiffIterate.cc +++ b/src/librbd/api/DiffIterate.cc @@ -291,7 +291,7 @@ int DiffIterate::execute() { uint64_t left = m_length; while (left > 0) { - uint64_t period_off = off - (off % period); + uint64_t period_off = round_down_to(off, period); uint64_t read_len = std::min(period_off + period - off, left); if (fast_diff_enabled) { -- 2.39.5