16#pragma intrinsic(_BitScanForward)
21inline constexpr double e = std::numbers::e_v <double>;
22inline constexpr double ln2 = std::numbers::ln2_v <double>;
23inline constexpr double ln10 = std::numbers::ln10_v<double>;
24inline constexpr double pi = std::numbers::pi_v <double>;
32[[nodiscard]]
constexpr auto floodRight(std::unsigned_integral
auto x)
noexcept
37 x |= x >> ((
sizeof(x) >= 2) ? 8 : 0);
38 x |= x >> ((
sizeof(x) >= 4) ? 16 : 0);
39 x |= x >> ((
sizeof(x) >= 8) ? 32 : 0);
46template<std::
signed_
integral T>
49 static_assert((T(-1) >> 1) == T(-1),
"right-shift must preserve sign");
50 if (int16_t(x) == x) [[likely]] {
51 return narrow_cast<int16_t>(x);
53 constexpr int SHIFT = (
sizeof(T) * CHAR_BIT) - 1;
54 return narrow_cast<int16_t>(0x7FFF - (x >> SHIFT));
63 static_assert((-1 >> 1) == -1,
"right-shift must preserve sign");
64 if (uint8_t(x) == x) [[likely]] {
65 return narrow_cast<uint8_t>(x);
67 return narrow_cast<uint8_t>(~(x >> 31));
75[[nodiscard]]
constexpr unsigned reverseNBits(
unsigned x,
unsigned bits)
79 ret = (ret << 1) | (x & 1);
138 return narrow_cast<uint8_t>((((a * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL) >> 32);
141 return narrow_cast<uint8_t>((((a * 0x0802 & 0x22110) | (a * 0x8020 & 0x88440)) * 0x10101) >> 16);
151 return x ? std::countr_zero(x) + 1 : 0;
167[[nodiscard]]
constexpr float cubicHermite(std::span<const float, 4> y,
float x)
169 assert(0.0f <= x); assert(x <= 1.0f);
170 float a = -0.5f*y[0] + 1.5f*y[1] - 1.5f*y[2] + 0.5f*y[3];
171 float b = y[0] - 2.5f*y[1] + 2.0f*y[2] - 0.5f*y[3];
172 float c = -0.5f*y[0] + 0.5f*y[2];
176 return a*x3 + b*x2 + c*x + d;
189 int q = dividend / divisor;
190 int r = dividend % divisor;
191 if ((r != 0) && ((r < 0) != (divisor < 0))) {
constexpr QuotientRemainder div_mod_floor(int dividend, int divisor)
constexpr unsigned reverseNBits(unsigned x, unsigned bits)
Reverse the lower N bits of a given value.
unsigned findFirstSet(uint32_t x)
Find the least significant bit that is set.
constexpr auto floodRight(std::unsigned_integral auto x) noexcept
Returns the smallest number of the form 2^n-1 that is greater or equal to the given number.
uint8_t clipIntToByte(int x)
Clip x to range [0,255].
constexpr int div_floor(int dividend, int divisor)
constexpr float cubicHermite(std::span< const float, 4 > y, float x)
constexpr int mod_floor(int dividend, int divisor)
int16_t clipToInt16(T x)
Clip x to range [-32768,32767].
constexpr uint8_t reverseByte(uint8_t a)
Reverse the bits in a byte.
Divide one integer by another, rounding towards minus infinity.