alnumeric.h 8.1 KB

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