🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
8.6 KiB

  1. #ifndef AL_NUMERIC_H
  2. #define AL_NUMERIC_H
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #ifdef HAVE_INTRIN_H
  8. #include <intrin.h>
  9. #endif
  10. #ifdef HAVE_SSE_INTRINSICS
  11. #include <xmmintrin.h>
  12. #endif
  13. #include "opthelpers.h"
  14. inline constexpr int64_t operator "" _i64(unsigned long long int n) noexcept { return static_cast<int64_t>(n); }
  15. inline constexpr uint64_t operator "" _u64(unsigned long long int n) noexcept { return static_cast<uint64_t>(n); }
  16. constexpr inline float minf(float a, float b) noexcept
  17. { return ((a > b) ? b : a); }
  18. constexpr inline float maxf(float a, float b) noexcept
  19. { return ((a > b) ? a : b); }
  20. constexpr inline float clampf(float val, float min, float max) noexcept
  21. { return minf(max, maxf(min, val)); }
  22. constexpr inline double mind(double a, double b) noexcept
  23. { return ((a > b) ? b : a); }
  24. constexpr inline double maxd(double a, double b) noexcept
  25. { return ((a > b) ? a : b); }
  26. constexpr inline double clampd(double val, double min, double max) noexcept
  27. { return mind(max, maxd(min, val)); }
  28. constexpr inline unsigned int minu(unsigned int a, unsigned int b) noexcept
  29. { return ((a > b) ? b : a); }
  30. constexpr inline unsigned int maxu(unsigned int a, unsigned int b) noexcept
  31. { return ((a > b) ? a : b); }
  32. constexpr inline unsigned int clampu(unsigned int val, unsigned int min, unsigned int max) noexcept
  33. { return minu(max, maxu(min, val)); }
  34. constexpr inline int mini(int a, int b) noexcept
  35. { return ((a > b) ? b : a); }
  36. constexpr inline int maxi(int a, int b) noexcept
  37. { return ((a > b) ? a : b); }
  38. constexpr inline int clampi(int val, int min, int max) noexcept
  39. { return mini(max, maxi(min, val)); }
  40. constexpr inline int64_t mini64(int64_t a, int64_t b) noexcept
  41. { return ((a > b) ? b : a); }
  42. constexpr inline int64_t maxi64(int64_t a, int64_t b) noexcept
  43. { return ((a > b) ? a : b); }
  44. constexpr inline int64_t clampi64(int64_t val, int64_t min, int64_t max) noexcept
  45. { return mini64(max, maxi64(min, val)); }
  46. constexpr inline uint64_t minu64(uint64_t a, uint64_t b) noexcept
  47. { return ((a > b) ? b : a); }
  48. constexpr inline uint64_t maxu64(uint64_t a, uint64_t b) noexcept
  49. { return ((a > b) ? a : b); }
  50. constexpr inline uint64_t clampu64(uint64_t val, uint64_t min, uint64_t max) noexcept
  51. { return minu64(max, maxu64(min, val)); }
  52. constexpr inline size_t minz(size_t a, size_t b) noexcept
  53. { return ((a > b) ? b : a); }
  54. constexpr inline size_t maxz(size_t a, size_t b) noexcept
  55. { return ((a > b) ? a : b); }
  56. constexpr inline size_t clampz(size_t val, size_t min, size_t max) noexcept
  57. { return minz(max, maxz(min, val)); }
  58. constexpr inline float lerpf(float val1, float val2, float mu) noexcept
  59. { return val1 + (val2-val1)*mu; }
  60. constexpr inline float cubic(float val1, float val2, float val3, float val4, float mu) noexcept
  61. {
  62. const float mu2{mu*mu}, mu3{mu2*mu};
  63. const float a0{-0.5f*mu3 + mu2 + -0.5f*mu};
  64. const float a1{ 1.5f*mu3 + -2.5f*mu2 + 1.0f};
  65. const float a2{-1.5f*mu3 + 2.0f*mu2 + 0.5f*mu};
  66. const float a3{ 0.5f*mu3 + -0.5f*mu2};
  67. return val1*a0 + val2*a1 + val3*a2 + val4*a3;
  68. }
  69. /** Find the next power-of-2 for non-power-of-2 numbers. */
  70. inline uint32_t NextPowerOf2(uint32_t value) noexcept
  71. {
  72. if(value > 0)
  73. {
  74. value--;
  75. value |= value>>1;
  76. value |= value>>2;
  77. value |= value>>4;
  78. value |= value>>8;
  79. value |= value>>16;
  80. }
  81. return value+1;
  82. }
  83. /** Round up a value to the next multiple. */
  84. inline size_t RoundUp(size_t value, size_t r) noexcept
  85. {
  86. value += r-1;
  87. return value - (value%r);
  88. }
  89. /**
  90. * Fast float-to-int conversion. No particular rounding mode is assumed; the
  91. * IEEE-754 default is round-to-nearest with ties-to-even, though an app could
  92. * change it on its own threads. On some systems, a truncating conversion may
  93. * always be the fastest method.
  94. */
  95. inline int fastf2i(float f) noexcept
  96. {
  97. #if defined(HAVE_SSE_INTRINSICS)
  98. return _mm_cvt_ss2si(_mm_set_ss(f));
  99. #elif defined(_MSC_VER) && defined(_M_IX86_FP)
  100. int i;
  101. __asm fld f
  102. __asm fistp i
  103. return i;
  104. #elif (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
  105. int i;
  106. #ifdef __SSE_MATH__
  107. __asm__("cvtss2si %1, %0" : "=r"(i) : "x"(f));
  108. #else
  109. __asm__ __volatile__("fistpl %0" : "=m"(i) : "t"(f) : "st");
  110. #endif
  111. return i;
  112. #else
  113. return static_cast<int>(f);
  114. #endif
  115. }
  116. inline unsigned int fastf2u(float f) noexcept
  117. { return static_cast<unsigned int>(fastf2i(f)); }
  118. /** Converts float-to-int using standard behavior (truncation). */
  119. inline int float2int(float f) noexcept
  120. {
  121. #if defined(HAVE_SSE_INTRINSICS)
  122. return _mm_cvtt_ss2si(_mm_set_ss(f));
  123. #elif (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 0) \
  124. || ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
  125. && !defined(__SSE_MATH__))
  126. int sign, shift, mant;
  127. union {
  128. float f;
  129. int i;
  130. } conv;
  131. conv.f = f;
  132. sign = (conv.i>>31) | 1;
  133. shift = ((conv.i>>23)&0xff) - (127+23);
  134. /* Over/underflow */
  135. if UNLIKELY(shift >= 31 || shift < -23)
  136. return 0;
  137. mant = (conv.i&0x7fffff) | 0x800000;
  138. if LIKELY(shift < 0)
  139. return (mant >> -shift) * sign;
  140. return (mant << shift) * sign;
  141. #else
  142. return static_cast<int>(f);
  143. #endif
  144. }
  145. inline unsigned int float2uint(float f) noexcept
  146. { return static_cast<unsigned int>(float2int(f)); }
  147. /** Converts double-to-int using standard behavior (truncation). */
  148. inline int double2int(double d) noexcept
  149. {
  150. #if defined(HAVE_SSE_INTRINSICS)
  151. return _mm_cvttsd_si32(_mm_set_sd(d));
  152. #elif (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP < 2) \
  153. || ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
  154. && !defined(__SSE2_MATH__))
  155. int sign, shift;
  156. int64_t mant;
  157. union {
  158. double d;
  159. int64_t i64;
  160. } conv;
  161. conv.d = d;
  162. sign = (conv.i64 >> 63) | 1;
  163. shift = ((conv.i64 >> 52) & 0x7ff) - (1023 + 52);
  164. /* Over/underflow */
  165. if UNLIKELY(shift >= 63 || shift < -52)
  166. return 0;
  167. mant = (conv.i64 & 0xfffffffffffff_i64) | 0x10000000000000_i64;
  168. if LIKELY(shift < 0)
  169. return (int)(mant >> -shift) * sign;
  170. return (int)(mant << shift) * sign;
  171. #else
  172. return static_cast<int>(d);
  173. #endif
  174. }
  175. /**
  176. * Rounds a float to the nearest integral value, according to the current
  177. * rounding mode. This is essentially an inlined version of rintf, although
  178. * makes fewer promises (e.g. -0 or -0.25 rounded to 0 may result in +0).
  179. */
  180. inline float fast_roundf(float f) noexcept
  181. {
  182. #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
  183. && !defined(__SSE_MATH__)
  184. float out;
  185. __asm__ __volatile__("frndint" : "=t"(out) : "0"(f));
  186. return out;
  187. #elif (defined(__GNUC__) || defined(__clang__)) && defined(__aarch64__)
  188. float out;
  189. __asm__ volatile("frintx %s0, %s1" : "=w"(out) : "w"(f));
  190. return out;
  191. #else
  192. /* Integral limit, where sub-integral precision is not available for
  193. * floats.
  194. */
  195. static const float ilim[2]{
  196. 8388608.0f /* 0x1.0p+23 */,
  197. -8388608.0f /* -0x1.0p+23 */
  198. };
  199. unsigned int sign, expo;
  200. union {
  201. float f;
  202. unsigned int i;
  203. } conv;
  204. conv.f = f;
  205. sign = (conv.i>>31)&0x01;
  206. expo = (conv.i>>23)&0xff;
  207. if UNLIKELY(expo >= 150/*+23*/)
  208. {
  209. /* An exponent (base-2) of 23 or higher is incapable of sub-integral
  210. * precision, so it's already an integral value. We don't need to worry
  211. * about infinity or NaN here.
  212. */
  213. return f;
  214. }
  215. /* Adding the integral limit to the value (with a matching sign) forces a
  216. * result that has no sub-integral precision, and is consequently forced to
  217. * round to an integral value. Removing the integral limit then restores
  218. * the initial value rounded to the integral. The compiler should not
  219. * optimize this out because of non-associative rules on floating-point
  220. * math (as long as you don't use -fassociative-math,
  221. * -funsafe-math-optimizations, -ffast-math, or -Ofast, in which case this
  222. * may break).
  223. */
  224. f += ilim[sign];
  225. return f - ilim[sign];
  226. #endif
  227. }
  228. template<typename T>
  229. constexpr const T& clamp(const T& value, const T& min_value, const T& max_value) noexcept
  230. {
  231. return std::min(std::max(value, min_value), max_value);
  232. }
  233. // Converts level (mB) to gain.
  234. inline float level_mb_to_gain(float x)
  235. {
  236. if(x <= -10'000.0f)
  237. return 0.0f;
  238. return std::pow(10.0f, x / 2'000.0f);
  239. }
  240. // Converts gain to level (mB).
  241. inline float gain_to_level_mb(float x)
  242. {
  243. if (x <= 0.0f)
  244. return -10'000.0f;
  245. return maxf(std::log10(x) * 2'000.0f, -10'000.0f);
  246. }
  247. #endif /* AL_NUMERIC_H */