48constexpr T
div_ceil(T numerator, T denominator) {
49 static_assert(std::is_integral_v<T>,
"div_ceil<T> needs integral T");
58 const bool quotient_not_negative{(numerator < 0) == (denominator < 0)};
59 return numerator / denominator +
60 (quotient_not_negative && numerator % denominator != 0);
71[[nodiscard]]
static inline uint64_t
multiply_uint64(uint64_t x, uint64_t y,
81[[nodiscard]]
static inline uint64_t
divide_128(uint64_t high, uint64_t low,
102 uint32_t x_hi =
static_cast<uint32_t
>(x >> 32);
103 uint32_t x_lo =
static_cast<uint32_t
>(x);
104 uint32_t y_hi =
static_cast<uint32_t
>(y >> 32);
105 uint32_t y_lo =
static_cast<uint32_t
>(y);
107 uint64_t hi_lo =
static_cast<uint64_t
>(x_hi) * y_lo;
109 uint64_t low =
static_cast<uint64_t
>(x_lo) * y_lo;
112 uint64_t mid = (low >> 32) +
static_cast<uint64_t
>(x_lo) * y_hi +
113 static_cast<uint32_t
>(hi_lo);
114 hi = (mid >> 32) +
static_cast<uint64_t
>(x_hi) * y_hi + (hi_lo >> 32);
115 return static_cast<uint32_t
>(low) + (mid << 32);
119#if defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC)
122#pragma intrinsic(_umul128)
123[[nodiscard]]
static inline uint64_t
multiply_uint64(uint64_t x, uint64_t y,
125 return _umul128(x, y, &hi);
127#elif defined(__SIZEOF_INT128__)
130[[nodiscard]]
static inline uint64_t
multiply_uint64(uint64_t x, uint64_t y,
132 unsigned __int128 res = (
unsigned __int128)x * y;
133 hi =
static_cast<uint64_t
>(res >> 64);
134 return static_cast<uint64_t
>(res);
143[[nodiscard]]
static inline uint64_t
divide_128(uint64_t high, uint64_t low,
146 for (
auto current_bit = 63; current_bit >= 0; current_bit--) {
147 const auto div_hi = current_bit ? (div >> (64 - current_bit)) : 0;
148 const auto div_lo = div << current_bit;
149 if (div_hi < high || (div_hi == high && div_lo <= low)) {
155 res += 1ULL << current_bit;
221 const uint64_t guess = hi *
m_mod;
222 const uint64_t rest = x - guess;
267 stored_data.
m_inv.load(std::memory_order_relaxed)};
275 data.
m_mod.store(new_mod, std::memory_order_relaxed);
276 data.
m_inv.store(inv, std::memory_order_relaxed);
A utility class which, if inherited from, prevents the descendant class from being copied,...
Definition: ut0class_life_cycle.h:41
A class that allows to read value of variable of some type T atomically and allows the value to be ch...
Definition: ut0seq_lock.h:49
Allows to execute x % mod for a specified mod in a fast way, without using a slow operation of divisi...
Definition: ut0math.h:164
uint64_t get_inverse() const
Gets the precomputed value of inverse.
Definition: ut0math.h:228
uint64_t m_inv
Definition: ut0math.h:251
uint64_t compute(uint64_t x) const
Computes the value of x % mod.
Definition: ut0math.h:217
fast_modulo_t(uint64_t mod)
Definition: ut0math.h:212
static uint64_t precompute_inv(uint64_t mod)
Precomputes the inverse needed for fast modulo operations.
Definition: ut0math.h:234
uint64_t m_mod
Definition: ut0math.h:250
fast_modulo_t(uint64_t mod, uint64_t inv)
Definition: ut0math.h:214
uint64_t get_mod() const
Gets the modulo value.
Definition: ut0math.h:231
A class that allows to atomically set new modulo value for fast modulo computations.
Definition: ut0math.h:256
mt_fast_modulo_t()
Definition: ut0math.h:258
mt_fast_modulo_t(uint64_t mod)
Definition: ut0math.h:259
void store(uint64_t new_mod)
Definition: ut0math.h:271
Seq_lock< data_t > m_data
Definition: ut0math.h:286
fast_modulo_t load() const
Definition: ut0math.h:264
Definition: ut0tuple.h:57
constexpr uint64_t multiply_uint64_portable(uint64_t x, uint64_t y, uint64_t &hi)
Calculates the 128bit result of multiplication of the two specified 64bit integers.
Definition: ut0math.h:99
This file contains a set of libraries providing overloads for regular dynamic allocation routines whi...
Definition: aligned_alloc.h:48
constexpr T div_ceil(T numerator, T denominator)
Computes the result of division rounded towards positive infinity.
Definition: ut0math.h:48
uint64_t find_prime(uint64_t n)
Looks for a prime number slightly greater than the given argument.
Definition: ut0math.cc:36
static uint64_t multiply_uint64(uint64_t x, uint64_t y, uint64_t &hi)
Calculates the 128bit result of multiplication of the two specified 64bit integers.
Definition: ut0math.h:137
static uint64_t divide_128(uint64_t high, uint64_t low, uint64_t div)
Definition: ut0math.h:143
Definition: ut0math.h:281
std::atomic< uint64_t > m_mod
Definition: ut0math.h:282
std::atomic< uint64_t > m_inv
Definition: ut0math.h:283
Utilities related to class lifecycle.
Debug utilities for Innobase.
static uint64_t operator%(uint64_t x, const ut::fast_modulo_t &fm)
Definition: ut0math.h:291
Implements a sequential lock structure for non-locking atomic read/write operations on a complex stru...
int n
Definition: xcom_base.cc:509