26 inline size_t mul(
size_t a,
size_t b)
30 if (SUCCEEDED(SIZETMult(a, b, &result)))
32#elif defined(_MSC_VER)
35 if (b <= SIZE_MAX / a)
39 if (!__builtin_mul_overflow(a, b, &result))
42 throw std::invalid_argument(
"multiply overflow");
53 inline size_t add(
size_t a,
size_t b)
57 if (SUCCEEDED(SIZETAdd(a, b, &result)))
59#elif defined(_MSC_VER)
60 if (a <= SIZE_MAX - b)
64 if (!__builtin_add_overflow(a, b, &result))
67 throw std::invalid_argument(
"add overflow");
78 inline uint32_t rol(_In_ uint32_t value, _In_
int bits)
81 return _rotl(value, bits);
83 return (value << bits) | (value >> (32 - bits));
96 inline constexpr int32_t muldiv(int32_t n, int32_t k, int32_t q)
98 return static_cast<int32_t
>(
static_cast<int64_t
>(n) * k / q);
110 inline constexpr uint32_t muldiv(uint32_t n, uint32_t k, uint32_t q)
112 return static_cast<uint32_t
>(
static_cast<uint64_t
>(n) * k / q);
124 inline constexpr int64_t muldiv(int64_t n, int64_t k, int64_t q)
138 inline constexpr uint64_t muldiv(uint64_t n, uint64_t k, uint64_t q)