49constexpr T
div_ceil(T numerator, T denominator) {
50 static_assert(std::is_integral_v<T>,
"div_ceil<T> needs integral T");
59 const bool quotient_not_negative{(numerator < 0) == (denominator < 0)};
60 return numerator / denominator +
61 (quotient_not_negative && numerator % denominator != 0);
72[[nodiscard]]
static inline uint64_t
multiply_uint64(uint64_t x, uint64_t y,
82[[nodiscard]]
static inline uint64_t
divide_128(uint64_t high, uint64_t low,
103 uint32_t x_hi =
static_cast<uint32_t
>(x >> 32);
104 uint32_t x_lo =
static_cast<uint32_t
>(x);
105 uint32_t y_hi =
static_cast<uint32_t
>(y >> 32);
106 uint32_t y_lo =
static_cast<uint32_t
>(y);
108 uint64_t hi_lo =
static_cast<uint64_t
>(x_hi) * y_lo;
110 uint64_t low =
static_cast<uint64_t
>(x_lo) * y_lo;
113 uint64_t mid = (low >> 32) +
static_cast<uint64_t
>(x_lo) * y_hi +
114 static_cast<uint32_t
>(hi_lo);
115 hi = (mid >> 32) +
static_cast<uint64_t
>(x_hi) * y_hi + (hi_lo >> 32);
116 return static_cast<uint32_t
>(low) + (mid << 32);
120#if defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC)
123#pragma intrinsic(_umul128)
124[[nodiscard]]
static inline uint64_t
multiply_uint64(uint64_t x, uint64_t y,
126 return _umul128(x, y, &hi);
128#elif defined(__SIZEOF_INT128__)
131[[nodiscard]]
static inline uint64_t
multiply_uint64(uint64_t x, uint64_t y,
133 unsigned __int128 res = (
unsigned __int128)x * y;
134 hi =
static_cast<uint64_t
>(res >> 64);
135 return static_cast<uint64_t
>(res);
144[[nodiscard]]
static inline uint64_t
divide_128(uint64_t high, uint64_t low,
147 for (
auto current_bit = 63; current_bit >= 0; current_bit--) {
148 const auto div_hi = current_bit ? (div >> (64 - current_bit)) : 0;
149 const auto div_lo = div << current_bit;
150 if (div_hi < high || (div_hi == high && div_lo <= low)) {
156 res += 1ULL << current_bit;
222 const uint64_t guess = hi *
m_mod;
223 const uint64_t rest = x - guess;
268 stored_data.
m_inv.load(std::memory_order_relaxed)};
276 data.
m_mod.store(new_mod, std::memory_order_relaxed);
277 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:165
uint64_t get_inverse() const
Gets the precomputed value of inverse.
Definition: ut0math.h:229
uint64_t m_inv
Definition: ut0math.h:252
uint64_t compute(uint64_t x) const
Computes the value of x % mod.
Definition: ut0math.h:218
fast_modulo_t(uint64_t mod)
Definition: ut0math.h:213
static uint64_t precompute_inv(uint64_t mod)
Precomputes the inverse needed for fast modulo operations.
Definition: ut0math.h:235
uint64_t m_mod
Definition: ut0math.h:251
fast_modulo_t(uint64_t mod, uint64_t inv)
Definition: ut0math.h:215
uint64_t get_mod() const
Gets the modulo value.
Definition: ut0math.h:232
A class that allows to atomically set new modulo value for fast modulo computations.
Definition: ut0math.h:257
mt_fast_modulo_t()
Definition: ut0math.h:259
mt_fast_modulo_t(uint64_t mod)
Definition: ut0math.h:260
void store(uint64_t new_mod)
Definition: ut0math.h:272
Seq_lock< data_t > m_data
Definition: ut0math.h:287
fast_modulo_t load() const
Definition: ut0math.h:265
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:100
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:49
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:138
static uint64_t divide_128(uint64_t high, uint64_t low, uint64_t div)
Definition: ut0math.h:144
Definition: ut0math.h:282
std::atomic< uint64_t > m_mod
Definition: ut0math.h:283
std::atomic< uint64_t > m_inv
Definition: ut0math.h:284
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:292
Implements a sequential lock structure for non-locking atomic read/write operations on a complex stru...
int n
Definition: xcom_base.cc:509