alnumeric.h 7.3 KB

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