| e8555c89 Andy McCall |
1 |
#ifndef MATH_HH |
| 2 |
#define MATH_HH |
| 3 |
|
| 4 |
#include "narrow.hh" |
| 5 |
#include <bit> |
| 6 |
#include <cassert> |
| 7 |
#include <climits> |
| 8 |
#include <cmath> |
| 9 |
#include <concepts> |
| 10 |
#include <cstdint> |
| 11 |
#include <numbers> |
| 12 |
#include <span> |
| 13 |
|
| 14 |
#ifdef _MSC_VER |
| 15 |
#include <intrin.h> |
| 16 |
#pragma intrinsic(_BitScanForward) |
| 17 |
#endif |
| 18 |
|
| 19 |
namespace Math { |
| 20 |
|
| 21 |
inline constexpr double e = std::numbers::e_v <double>; |
| 22 |
inline constexpr double ln2 = std::numbers::ln2_v <double>; |
| 23 |
inline constexpr double ln10 = std::numbers::ln10_v<double>; |
| 24 |
inline constexpr double pi = std::numbers::pi_v <double>; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
template <std::floating_point T> |
| 30 |
[[nodiscard]] constexpr T fract(T x) noexcept |
| 31 |
{ |
| 32 |
return x - std::floor(x); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
[[nodiscard]] constexpr auto floodRight(std::unsigned_integral auto x) noexcept |
| 42 |
{ |
| 43 |
x |= x >> 1; |
| 44 |
x |= x >> 2; |
| 45 |
x |= x >> 4; |
| 46 |
x |= x >> ((sizeof(x) >= 2) ? 8 : 0); |
| 47 |
x |= x >> ((sizeof(x) >= 4) ? 16 : 0); |
| 48 |
x |= x >> ((sizeof(x) >= 8) ? 32 : 0); |
| 49 |
return x; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
template<std::signed_integral T> |
| 56 |
[[nodiscard]] inline int16_t clipToInt16(T x) |
| 57 |
{ |
| 58 |
static_assert((T(-1) >> 1) == T(-1), "right-shift must preserve sign"); |
| 59 |
if (int16_t(x) == x) [[likely]] { |
| 60 |
return narrow_cast<int16_t>(x); |
| 61 |
} else { |
| 62 |
constexpr int SHIFT = (sizeof(T) * CHAR_BIT) - 1; |
| 63 |
return narrow_cast<int16_t>(0x7FFF - (x >> SHIFT)); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
[[nodiscard]] inline uint8_t clipIntToByte(int x) |
| 71 |
{ |
| 72 |
static_assert((-1 >> 1) == -1, "right-shift must preserve sign"); |
| 73 |
if (uint8_t(x) == x) [[likely]] { |
| 74 |
return narrow_cast<uint8_t>(x); |
| 75 |
} else { |
| 76 |
return narrow_cast<uint8_t>(~(x >> 31)); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
[[nodiscard]] constexpr unsigned reverseNBits(unsigned x, unsigned bits) |
| 85 |
{ |
| 86 |
unsigned ret = 0; |
| 87 |
while (bits--) { |
| 88 |
ret = (ret << 1) | (x & 1); |
| 89 |
x >>= 1; |
| 90 |
} |
| 91 |
return ret; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
[[nodiscard]] constexpr uint8_t reverseByte(uint8_t a) |
| 135 |
{ |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#ifdef __x86_64 |
| 146 |
|
| 147 |
return narrow_cast<uint8_t>((((a * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL) >> 32); |
| 148 |
#else |
| 149 |
|
| 150 |
return narrow_cast<uint8_t>((((a * 0x0802 & 0x22110) | (a * 0x8020 & 0x88440)) * 0x10101) >> 16); |
| 151 |
#endif |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
[[nodiscard]] constexpr unsigned findFirstSet(uint32_t x) |
| 159 |
{ |
| 160 |
return x ? std::countr_zero(x) + 1 : 0; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
[[nodiscard]] constexpr float cubicHermite(std::span<const float, 4> y, float x) |
| 177 |
{ |
| 178 |
assert(0.0f <= x); assert(x <= 1.0f); |
| 179 |
float a = -0.5f*y[0] + 1.5f*y[1] - 1.5f*y[2] + 0.5f*y[3]; |
| 180 |
float b = y[0] - 2.5f*y[1] + 2.0f*y[2] - 0.5f*y[3]; |
| 181 |
float c = -0.5f*y[0] + 0.5f*y[2]; |
| 182 |
float d = y[1]; |
| 183 |
float x2 = x * x; |
| 184 |
float x3 = x * x2; |
| 185 |
return a*x3 + b*x2 + c*x + d; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
struct QuotientRemainder { |
| 194 |
int quotient; |
| 195 |
int remainder; |
| 196 |
}; |
| 197 |
constexpr QuotientRemainder div_mod_floor(int dividend, int divisor) { |
| 198 |
int q = dividend / divisor; |
| 199 |
int r = dividend % divisor; |
| 200 |
if ((r != 0) && ((r < 0) != (divisor < 0))) { |
| 201 |
--q; |
| 202 |
r += divisor; |
| 203 |
} |
| 204 |
return {.quotient = q, .remainder = r}; |
| 205 |
} |
| 206 |
constexpr int div_floor(int dividend, int divisor) { |
| 207 |
return div_mod_floor(dividend, divisor).quotient; |
| 208 |
} |
| 209 |
constexpr int mod_floor(int dividend, int divisor) { |
| 210 |
return div_mod_floor(dividend, divisor).remainder; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
class DenormalGuard { |
| 226 |
private: |
| 227 |
#if (defined(__SSE__) && (defined(__GNUC__) || defined(__clang__))) |
| 228 |
|
| 229 |
static constexpr int DAZ_BIT = 6; |
| 230 |
static constexpr int FTZ_BIT = 15; |
| 231 |
|
| 232 |
uint32_t saved_mxcsr; |
| 233 |
#endif |
| 234 |
|
| 235 |
public: |
| 236 |
[[nodiscard]] DenormalGuard() { |
| 237 |
#if (defined(__SSE__) && (defined(__GNUC__) || defined(__clang__))) |
| 238 |
|
| 239 |
asm volatile("stmxcsr %0" : "=m"(saved_mxcsr)); |
| 240 |
|
| 241 |
|
| 242 |
auto new_mxcsr = saved_mxcsr | (1u << DAZ_BIT) | (1u << FTZ_BIT); |
| 243 |
asm volatile("ldmxcsr %0" : : "m"(new_mxcsr)); |
| 244 |
#endif |
| 245 |
} |
| 246 |
|
| 247 |
~DenormalGuard() { |
| 248 |
#if (defined(__SSE__) && (defined(__GNUC__) || defined(__clang__))) |
| 249 |
asm volatile("ldmxcsr %0" : : "m"(saved_mxcsr)); |
| 250 |
#endif |
| 251 |
} |
| 252 |
|
| 253 |
DenormalGuard(const DenormalGuard&) = delete; |
| 254 |
DenormalGuard& operator=(const DenormalGuard&) = delete; |
| 255 |
DenormalGuard(DenormalGuard&&) = delete; |
| 256 |
DenormalGuard& operator=(DenormalGuard&&) = delete; |
| 257 |
}; |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
#endif |