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.

functions.hpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #pragma once
  2. #include <simd/vector.hpp>
  3. #include <simd/sse_mathfun.h>
  4. #include <math.hpp>
  5. #include <cmath>
  6. namespace rack {
  7. namespace simd {
  8. // Standard math functions from std::
  9. /* Import std:: math functions into the simd namespace so you can use `sin(T)` etc in templated functions and get both the scalar and vector versions.
  10. Example:
  11. template <typename T>
  12. T sin_plus_cos(T x) {
  13. return simd::sin(x) + simd::cos(x);
  14. }
  15. */
  16. using std::fmax;
  17. inline float_4 fmax(float_4 x, float_4 b) {
  18. return float_4(_mm_max_ps(x.v, b.v));
  19. }
  20. using std::fmin;
  21. inline float_4 fmin(float_4 x, float_4 b) {
  22. return float_4(_mm_min_ps(x.v, b.v));
  23. }
  24. using std::sqrt;
  25. inline float_4 sqrt(float_4 x) {
  26. return float_4(_mm_sqrt_ps(x.v));
  27. }
  28. using std::log;
  29. inline float_4 log(float_4 x) {
  30. return float_4(sse_mathfun_log_ps(x.v));
  31. }
  32. using std::log10;
  33. inline float_4 log10(float_4 x) {
  34. return float_4(sse_mathfun_log_ps(x.v)) / std::log(10.f);
  35. }
  36. using std::log2;
  37. inline float_4 log2(float_4 x) {
  38. return float_4(sse_mathfun_log_ps(x.v)) / std::log(2.f);
  39. }
  40. using std::exp;
  41. inline float_4 exp(float_4 x) {
  42. return float_4(sse_mathfun_exp_ps(x.v));
  43. }
  44. using std::sin;
  45. inline float_4 sin(float_4 x) {
  46. return float_4(sse_mathfun_sin_ps(x.v));
  47. }
  48. using std::cos;
  49. inline float_4 cos(float_4 x) {
  50. return float_4(sse_mathfun_cos_ps(x.v));
  51. }
  52. using std::floor;
  53. inline float_4 floor(float_4 a) {
  54. return float_4(sse_mathfun_floor_ps(a.v));
  55. }
  56. using std::ceil;
  57. inline float_4 ceil(float_4 a) {
  58. return float_4(sse_mathfun_ceil_ps(a.v));
  59. }
  60. using std::round;
  61. inline float_4 round(float_4 a) {
  62. return float_4(sse_mathfun_round_ps(a.v));
  63. }
  64. using std::fmod;
  65. inline float_4 fmod(float_4 a, float_4 b) {
  66. return float_4(sse_mathfun_fmod_ps(a.v, b.v));
  67. }
  68. using std::fabs;
  69. inline float_4 fabs(float_4 a) {
  70. return float_4(sse_mathfun_fabs_ps(a.v));
  71. }
  72. using std::trunc;
  73. inline float_4 trunc(float_4 a) {
  74. return float_4(sse_mathfun_trunc_ps(a.v));
  75. }
  76. using std::pow;
  77. inline float_4 pow(float_4 a, float_4 b) {
  78. return exp(b * log(a));
  79. }
  80. inline float_4 pow(float a, float_4 b) {
  81. return exp(b * std::log(a));
  82. }
  83. template <typename T>
  84. T pow(T a, int b) {
  85. // Optimal with `-O3 -ffast-math` when b is known at compile-time
  86. T p = 1;
  87. for (int i = 1; i <= b; i *= 2) {
  88. if (i & b)
  89. p *= a;
  90. a *= a;
  91. }
  92. return p;
  93. }
  94. // Nonstandard functions
  95. inline float ifelse(bool cond, float a, float b) {
  96. return cond ? a : b;
  97. }
  98. /** Given a mask, returns a if mask is 0xffffffff per element, b if mask is 0x00000000 */
  99. inline float_4 ifelse(float_4 mask, float_4 a, float_4 b) {
  100. return (a & mask) | andnot(mask, b);
  101. }
  102. /** Returns the approximate reciprocal square root.
  103. Much faster than `1/sqrt(x)`.
  104. */
  105. inline float_4 rsqrt(float_4 x) {
  106. return float_4(_mm_rsqrt_ps(x.v));
  107. }
  108. /** Returns the approximate reciprocal.
  109. Much faster than `1/x`.
  110. */
  111. inline float_4 rcp(float_4 x) {
  112. return float_4(_mm_rcp_ps(x.v));
  113. }
  114. // From math.hpp
  115. using math::clamp;
  116. inline float_4 clamp(float_4 x, float_4 a, float_4 b) {
  117. return fmin(fmax(x, a), b);
  118. }
  119. using math::rescale;
  120. inline float_4 rescale(float_4 x, float_4 xMin, float_4 xMax, float_4 yMin, float_4 yMax) {
  121. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  122. }
  123. using math::sgn;
  124. inline float_4 sgn(float_4 x) {
  125. float_4 signbit = x & -0.f;
  126. float_4 nonzero = (x != 0.f);
  127. return signbit | (nonzero & 1.f);
  128. }
  129. } // namespace simd
  130. } // namespace rack