alnumeric.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #ifndef AL_NUMERIC_H
  2. #define AL_NUMERIC_H
  3. #include <algorithm>
  4. #include <array>
  5. #include <cmath>
  6. #include <cstddef>
  7. #include <cstdint>
  8. #include <iterator>
  9. #include <type_traits>
  10. #ifdef HAVE_INTRIN_H
  11. #include <intrin.h>
  12. #endif
  13. #ifdef HAVE_SSE_INTRINSICS
  14. #include <xmmintrin.h>
  15. #endif
  16. #include "albit.h"
  17. #include "altraits.h"
  18. #include "opthelpers.h"
  19. constexpr auto operator "" _i64(unsigned long long n) noexcept { return static_cast<std::int64_t>(n); }
  20. constexpr auto operator "" _u64(unsigned long long n) noexcept { return static_cast<std::uint64_t>(n); }
  21. constexpr auto operator "" _z(unsigned long long n) noexcept
  22. { return static_cast<std::make_signed_t<std::size_t>>(n); }
  23. constexpr auto operator "" _uz(unsigned long long n) noexcept { return static_cast<std::size_t>(n); }
  24. constexpr auto operator "" _zu(unsigned long long n) noexcept { return static_cast<std::size_t>(n); }
  25. constexpr auto GetCounterSuffix(size_t count) noexcept -> const char*
  26. {
  27. auto &suffix = (((count%100)/10) == 1) ? "th" :
  28. ((count%10) == 1) ? "st" :
  29. ((count%10) == 2) ? "nd" :
  30. ((count%10) == 3) ? "rd" : "th";
  31. return std::data(suffix);
  32. }
  33. constexpr inline float lerpf(float val1, float val2, float mu) noexcept
  34. { return val1 + (val2-val1)*mu; }
  35. constexpr inline double lerpd(double val1, double val2, double mu) noexcept
  36. { return val1 + (val2-val1)*mu; }
  37. /** Find the next power-of-2 for non-power-of-2 numbers. */
  38. inline uint32_t NextPowerOf2(uint32_t value) noexcept
  39. {
  40. if(value > 0)
  41. {
  42. value--;
  43. value |= value>>1;
  44. value |= value>>2;
  45. value |= value>>4;
  46. value |= value>>8;
  47. value |= value>>16;
  48. }
  49. return value+1;
  50. }
  51. /**
  52. * If the value is not already a multiple of r, round down to the next
  53. * multiple.
  54. */
  55. template<typename T>
  56. constexpr T RoundDown(T value, al::type_identity_t<T> r) noexcept
  57. { return value - (value%r); }
  58. /**
  59. * If the value is not already a multiple of r, round up to the next multiple.
  60. */
  61. template<typename T>
  62. constexpr T RoundUp(T value, al::type_identity_t<T> r) noexcept
  63. { return RoundDown(value + r-1, r); }
  64. /**
  65. * Fast float-to-int conversion. No particular rounding mode is assumed; the
  66. * IEEE-754 default is round-to-nearest with ties-to-even, though an app could
  67. * change it on its own threads. On some systems, a truncating conversion may
  68. * always be the fastest method.
  69. */
  70. inline int fastf2i(float f) noexcept
  71. {
  72. #if defined(HAVE_SSE_INTRINSICS)
  73. return _mm_cvt_ss2si(_mm_set_ss(f));
  74. #elif defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 0
  75. int i;
  76. __asm fld f
  77. __asm fistp i
  78. return i;
  79. #elif (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
  80. && !defined(__SSE_MATH__)
  81. int i;
  82. __asm__ __volatile__("fistpl %0" : "=m"(i) : "t"(f) : "st");
  83. return i;
  84. #else
  85. return static_cast<int>(f);
  86. #endif
  87. }
  88. inline unsigned int fastf2u(float f) noexcept
  89. { return static_cast<unsigned int>(fastf2i(f)); }
  90. /** Converts float-to-int using standard behavior (truncation). */
  91. inline int float2int(float f) noexcept
  92. {
  93. #if defined(HAVE_SSE_INTRINSICS)
  94. return _mm_cvtt_ss2si(_mm_set_ss(f));
  95. #elif (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 0) \
  96. || ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
  97. && !defined(__SSE_MATH__))
  98. const int conv_i{al::bit_cast<int>(f)};
  99. const int sign{(conv_i>>31) | 1};
  100. const int shift{((conv_i>>23)&0xff) - (127+23)};
  101. /* Over/underflow */
  102. if(shift >= 31 || shift < -23) UNLIKELY
  103. return 0;
  104. const int mant{(conv_i&0x7fffff) | 0x800000};
  105. if(shift < 0) LIKELY
  106. return (mant >> -shift) * sign;
  107. return (mant << shift) * sign;
  108. #else
  109. return static_cast<int>(f);
  110. #endif
  111. }
  112. inline unsigned int float2uint(float f) noexcept
  113. { return static_cast<unsigned int>(float2int(f)); }
  114. /** Converts double-to-int using standard behavior (truncation). */
  115. inline int double2int(double d) noexcept
  116. {
  117. #if defined(HAVE_SSE_INTRINSICS)
  118. return _mm_cvttsd_si32(_mm_set_sd(d));
  119. #elif (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP < 2) \
  120. || ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
  121. && !defined(__SSE2_MATH__))
  122. const int64_t conv_i64{al::bit_cast<int64_t>(d)};
  123. const int sign{static_cast<int>(conv_i64 >> 63) | 1};
  124. const int shift{(static_cast<int>(conv_i64 >> 52) & 0x7ff) - (1023 + 52)};
  125. /* Over/underflow */
  126. if(shift >= 63 || shift < -52) UNLIKELY
  127. return 0;
  128. const int64_t mant{(conv_i64 & 0xfffffffffffff_i64) | 0x10000000000000_i64};
  129. if(shift < 0) LIKELY
  130. return static_cast<int>(mant >> -shift) * sign;
  131. return static_cast<int>(mant << shift) * sign;
  132. #else
  133. return static_cast<int>(d);
  134. #endif
  135. }
  136. /**
  137. * Rounds a float to the nearest integral value, according to the current
  138. * rounding mode. This is essentially an inlined version of rintf, although
  139. * makes fewer promises (e.g. -0 or -0.25 rounded to 0 may result in +0).
  140. */
  141. inline float fast_roundf(float f) noexcept
  142. {
  143. #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) \
  144. && !defined(__SSE_MATH__)
  145. float out;
  146. __asm__ __volatile__("frndint" : "=t"(out) : "0"(f));
  147. return out;
  148. #elif (defined(__GNUC__) || defined(__clang__)) && defined(__aarch64__)
  149. float out;
  150. __asm__ volatile("frintx %s0, %s1" : "=w"(out) : "w"(f));
  151. return out;
  152. #else
  153. /* Integral limit, where sub-integral precision is not available for
  154. * floats.
  155. */
  156. static constexpr std::array ilim{
  157. 8388608.0f /* 0x1.0p+23 */,
  158. -8388608.0f /* -0x1.0p+23 */
  159. };
  160. const unsigned int conv_i{al::bit_cast<unsigned int>(f)};
  161. const unsigned int sign{(conv_i>>31)&0x01};
  162. const unsigned int expo{(conv_i>>23)&0xff};
  163. if(expo >= 150/*+23*/) UNLIKELY
  164. {
  165. /* An exponent (base-2) of 23 or higher is incapable of sub-integral
  166. * precision, so it's already an integral value. We don't need to worry
  167. * about infinity or NaN here.
  168. */
  169. return f;
  170. }
  171. /* Adding the integral limit to the value (with a matching sign) forces a
  172. * result that has no sub-integral precision, and is consequently forced to
  173. * round to an integral value. Removing the integral limit then restores
  174. * the initial value rounded to the integral. The compiler should not
  175. * optimize this out because of non-associative rules on floating-point
  176. * math (as long as you don't use -fassociative-math,
  177. * -funsafe-math-optimizations, -ffast-math, or -Ofast, in which case this
  178. * may break without __builtin_assoc_barrier support).
  179. */
  180. #if HAS_BUILTIN(__builtin_assoc_barrier)
  181. return __builtin_assoc_barrier(f + ilim[sign]) - ilim[sign];
  182. #else
  183. f += ilim[sign];
  184. return f - ilim[sign];
  185. #endif
  186. #endif
  187. }
  188. // Converts level (mB) to gain.
  189. inline float level_mb_to_gain(float x)
  190. {
  191. if(x <= -10'000.0f)
  192. return 0.0f;
  193. return std::pow(10.0f, x / 2'000.0f);
  194. }
  195. // Converts gain to level (mB).
  196. inline float gain_to_level_mb(float x)
  197. {
  198. if (x <= 0.0f)
  199. return -10'000.0f;
  200. return std::max(std::log10(x) * 2'000.0f, -10'000.0f);
  201. }
  202. #endif /* AL_NUMERIC_H */