67 constexpr uint64_t de_brujin_sequence = 151050438420815295u;
69 constexpr static uint8_t ans[64] = {
70 0, 1, 2, 7, 3, 13, 8, 19, 4, 25, 14, 28, 9, 34, 20, 40,
71 5, 17, 26, 38, 15, 46, 29, 48, 10, 31, 35, 54, 21, 50, 41, 57,
72 63, 6, 12, 18, 24, 27, 33, 39, 16, 37, 45, 47, 30, 53, 49, 56,
73 62, 11, 23, 32, 36, 44, 52, 55, 61, 22, 43, 51, 60, 42, 59, 58,
75 return x == 0 ? 64 : ans[(x * de_brujin_sequence >> (64 - 6)) & 63];
83constexpr T
div_ceil(T numerator, T denominator) {
84 static_assert(std::is_integral_v<T>,
"div_ceil<T> needs integral T");
93 const bool quotient_not_negative{(numerator < 0) == (denominator < 0)};
94 return numerator / denominator +
95 (quotient_not_negative && numerator % denominator != 0);
106[[nodiscard]]
static inline uint64_t
multiply_uint64(uint64_t x, uint64_t y,
116[[nodiscard]]
static inline uint64_t
divide_128(uint64_t high, uint64_t low,
137 uint32_t x_hi =
static_cast<uint32_t
>(x >> 32);
138 uint32_t x_lo =
static_cast<uint32_t
>(x);
139 uint32_t y_hi =
static_cast<uint32_t
>(y >> 32);
140 uint32_t y_lo =
static_cast<uint32_t
>(y);
142 uint64_t hi_lo =
static_cast<uint64_t
>(x_hi) * y_lo;
144 uint64_t low =
static_cast<uint64_t
>(x_lo) * y_lo;
147 uint64_t mid = (low >> 32) +
static_cast<uint64_t
>(x_lo) * y_hi +
148 static_cast<uint32_t
>(hi_lo);
149 hi = (mid >> 32) +
static_cast<uint64_t
>(x_hi) * y_hi + (hi_lo >> 32);
150 return static_cast<uint32_t
>(low) + (mid << 32);
154#if defined(_MSC_VER) && defined(_M_X64) && !defined(_M_ARM64EC)
157#pragma intrinsic(_umul128)
158[[nodiscard]]
static inline uint64_t
multiply_uint64(uint64_t x, uint64_t y,
160 return _umul128(x, y, &hi);
162#elif defined(__SIZEOF_INT128__)
165[[nodiscard]]
static inline uint64_t
multiply_uint64(uint64_t x, uint64_t y,
167 unsigned __int128 res = (
unsigned __int128)x * y;
168 hi =
static_cast<uint64_t
>(res >> 64);
169 return static_cast<uint64_t
>(res);
178[[nodiscard]]
static inline uint64_t
divide_128(uint64_t high, uint64_t low,
181 for (
auto current_bit = 63; current_bit >= 0; current_bit--) {
182 const auto div_hi = current_bit ? (div >> (64 - current_bit)) : 0;
183 const auto div_lo = div << current_bit;
184 if (div_hi < high || (div_hi == high && div_lo <= low)) {
190 res += 1ULL << current_bit;
256 const uint64_t guess = hi *
m_mod;
257 const uint64_t rest = x - guess;
302 stored_data.
m_inv.load(std::memory_order_relaxed)};
310 data.
m_mod.store(new_mod, std::memory_order_relaxed);
311 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:199
uint64_t get_inverse() const
Gets the precomputed value of inverse.
Definition: ut0math.h:263
uint64_t m_inv
Definition: ut0math.h:286
uint64_t compute(uint64_t x) const
Computes the value of x % mod.
Definition: ut0math.h:252
fast_modulo_t(uint64_t mod)
Definition: ut0math.h:247
static uint64_t precompute_inv(uint64_t mod)
Precomputes the inverse needed for fast modulo operations.
Definition: ut0math.h:269
uint64_t m_mod
Definition: ut0math.h:285
fast_modulo_t(uint64_t mod, uint64_t inv)
Definition: ut0math.h:249
uint64_t get_mod() const
Gets the modulo value.
Definition: ut0math.h:266
A class that allows to atomically set new modulo value for fast modulo computations.
Definition: ut0math.h:291
mt_fast_modulo_t()
Definition: ut0math.h:293
mt_fast_modulo_t(uint64_t mod)
Definition: ut0math.h:294
void store(uint64_t new_mod)
Definition: ut0math.h:306
Seq_lock< data_t > m_data
Definition: ut0math.h:321
fast_modulo_t load() const
Definition: ut0math.h:299
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:134
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:83
static int countr_zero(uint64_t x)
Portable replacement for std::countr_zero(uint64_t) from C++20.
Definition: ut0math.h:47
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:172
static uint64_t divide_128(uint64_t high, uint64_t low, uint64_t div)
Definition: ut0math.h:178
Definition: ut0math.h:316
std::atomic< uint64_t > m_mod
Definition: ut0math.h:317
std::atomic< uint64_t > m_inv
Definition: ut0math.h:318
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:326
Implements a sequential lock structure for non-locking atomic read/write operations on a complex stru...
int n
Definition: xcom_base.cc:509