29requires (std::is_unsigned_v<std::decay_t<T>> &&
sizeof(T) == 8)
60requires (std::is_unsigned_v<std::decay_t<T>> &&
sizeof(T) == 4)
91template <std::
integral T>
93 return x && (!(x&(x-1)));
142template <
typename RandItr,
typename C>
144 return cmp(*l, *m) ? (cmp(*m, *r) ? m : (cmp(*l, *r) ? r : l ))
145 : (cmp(*r, *m) ? m : (cmp(*r, *l) ? r : l ));
170template <
typename RandItr,
typename C>
172 size_t N = std::distance(beg, end);
173 size_t offset = N >> 3;
200template<
typename Iter,
typename Compare>
201void sort2(Iter a, Iter b, Compare comp) {
202 if (comp(*b, *a)) std::iter_swap(a, b);
225template<
typename Iter,
typename Compare>
226void sort3(Iter a, Iter b, Iter c, Compare comp) {
251template <std::
integral T>
253 static std::atomic<T> counter{0};
254 return counter.fetch_add(1, std::memory_order_relaxed);
278inline void atomic_max(std::atomic<T>& v,
const T& max_v)
noexcept {
279 T prev = v.load(std::memory_order_relaxed);
280 while(prev < max_v &&
281 !v.compare_exchange_weak(prev, max_v, std::memory_order_relaxed,
282 std::memory_order_relaxed)) {
307inline void atomic_min(std::atomic<T>& v,
const T& min_v)
noexcept {
308 T prev = v.load(std::memory_order_relaxed);
309 while(prev > min_v &&
310 !v.compare_exchange_weak(prev, min_v, std::memory_order_relaxed,
311 std::memory_order_relaxed)) {
332 return std::chrono::system_clock::now().time_since_epoch().count();
356 for (
size_t x = N; --x > 0;) {
357 if (std::gcd(x, N) == 1) {
380 static_assert(N>0,
"N must be greater than 0");
381 std::array<size_t, N> coprimes{};
382 for (
size_t n = 0; n < N; ++n) {
413 static_assert(std::is_unsigned<T>::value,
"Xorshift requires an unsigned integral type.");
481 if constexpr (
sizeof(T) == 8) {
483 _state ^= _state << 13;
484 _state ^= _state >> 7;
485 _state ^= _state << 17;
486 return _state * 0x2545F4914F6CDD1DULL;
488 else if constexpr (
sizeof(T) == 4) {
490 _state ^= _state << 13;
491 _state ^= _state >> 17;
492 _state ^= _state << 5;
496 static_assert(
sizeof(T) == 0,
"Unsupported bit-width for Xorshift. Use uint32_t or uint64_t.");
void seed(T value)
seeds the generator with a new value
Definition math.hpp:457
T operator()()
generates the next pseudo-random value
Definition math.hpp:480
Xorshift(T value)
constructs a xor-shift generator with the given seed
Definition math.hpp:442
Xorshift()=default
constructs an uninitialized xor-shift generator
taskflow namespace
Definition small_vector.hpp:20
RandItr median_of_three(RandItr l, RandItr m, RandItr r, C cmp)
finds the median of three numbers pointed to by iterators using the given comparator
Definition math.hpp:143
T unique_id()
generates a program-wide unique ID of the given type in a thread-safe manner
Definition math.hpp:252
constexpr size_t coprime(size_t N)
computes a coprime of a given number
Definition math.hpp:352
T seed() noexcept
generates a random seed based on the current system clock
Definition math.hpp:331
constexpr T next_pow2(T x)
rounds the given 64-bit unsigned integer to the nearest power of 2
Definition math.hpp:30
void atomic_max(std::atomic< T > &v, const T &max_v) noexcept
updates an atomic variable with the maximum value
Definition math.hpp:278
void atomic_min(std::atomic< T > &v, const T &min_v) noexcept
updates an atomic variable with the minimum value
Definition math.hpp:307
constexpr std::array< size_t, N > make_coprime_lut()
generates a compile-time array of coprimes for numbers from 0 to N-1
Definition math.hpp:379
RandItr pseudo_median_of_nine(RandItr beg, RandItr end, C cmp)
finds the pseudo median of a range of items using a spread of nine numbers
Definition math.hpp:171
void sort3(Iter a, Iter b, Iter c, Compare comp)
Sorts three elements of dereferenced iterators using the given comparison function.
Definition math.hpp:226
void sort2(Iter a, Iter b, Compare comp)
sorts two elements of dereferenced iterators using the given comparison function
Definition math.hpp:201
constexpr size_t static_floor_log2()
returns the floor of log2(N) at compile time
Definition math.hpp:112
constexpr bool is_pow2(const T &x)
checks if the given number is a power of 2
Definition math.hpp:92