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.

160 lines
2.6KB

  1. #pragma once
  2. #include "vector.hpp"
  3. #include "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. using namespace simd;
  14. return sin(x) + cos(x);
  15. }
  16. */
  17. using std::fmax;
  18. inline f32_4 fmax(f32_4 x, f32_4 b) {
  19. return f32_4(_mm_max_ps(x.v, b.v));
  20. }
  21. using std::fmin;
  22. inline f32_4 fmin(f32_4 x, f32_4 b) {
  23. return f32_4(_mm_min_ps(x.v, b.v));
  24. }
  25. using std::sqrt;
  26. inline f32_4 sqrt(f32_4 x) {
  27. return f32_4(_mm_sqrt_ps(x.v));
  28. }
  29. using std::log;
  30. inline f32_4 log(f32_4 x) {
  31. return f32_4(sse_mathfun_log_ps(x.v));
  32. }
  33. using std::log10;
  34. inline f32_4 log10(f32_4 x) {
  35. return f32_4(sse_mathfun_log_ps(x.v)) / std::log(10.f);
  36. }
  37. using std::exp;
  38. inline f32_4 exp(f32_4 x) {
  39. return f32_4(sse_mathfun_exp_ps(x.v));
  40. }
  41. using std::sin;
  42. inline f32_4 sin(f32_4 x) {
  43. return f32_4(sse_mathfun_sin_ps(x.v));
  44. }
  45. using std::cos;
  46. inline f32_4 cos(f32_4 x) {
  47. return f32_4(sse_mathfun_cos_ps(x.v));
  48. }
  49. using std::floor;
  50. inline f32_4 floor(f32_4 a) {
  51. return f32_4(sse_mathfun_floor_ps(a.v));
  52. }
  53. using std::ceil;
  54. inline f32_4 ceil(f32_4 a) {
  55. return f32_4(sse_mathfun_ceil_ps(a.v));
  56. }
  57. using std::round;
  58. inline f32_4 round(f32_4 a) {
  59. return f32_4(sse_mathfun_round_ps(a.v));
  60. }
  61. using std::fmod;
  62. inline f32_4 fmod(f32_4 a, f32_4 b) {
  63. return f32_4(sse_mathfun_fmod_ps(a.v, b.v));
  64. }
  65. using std::fabs;
  66. inline f32_4 fabs(f32_4 a) {
  67. return f32_4(sse_mathfun_fabs_ps(a.v));
  68. }
  69. using std::trunc;
  70. inline f32_4 trunc(f32_4 a) {
  71. return f32_4(sse_mathfun_trunc_ps(a.v));
  72. }
  73. using std::pow;
  74. inline f32_4 pow(f32_4 a, f32_4 b) {
  75. return exp(b * log(a));
  76. }
  77. inline f32_4 pow(float a, f32_4 b) {
  78. return exp(b * std::log(a));
  79. }
  80. // Nonstandard functions
  81. /** Returns the approximate reciprocal square root.
  82. Much faster than `1/sqrt(x)`.
  83. */
  84. inline f32_4 rsqrt(f32_4 x) {
  85. return f32_4(_mm_rsqrt_ps(x.v));
  86. }
  87. /** Returns the approximate reciprocal.
  88. Much faster than `1/x`.
  89. */
  90. inline f32_4 rcp(f32_4 x) {
  91. return f32_4(_mm_rcp_ps(x.v));
  92. }
  93. // From math.hpp
  94. using math::clamp;
  95. inline f32_4 clamp(f32_4 x, f32_4 a, f32_4 b) {
  96. return fmin(fmax(x, a), b);
  97. }
  98. using math::rescale;
  99. inline f32_4 rescale(f32_4 x, f32_4 xMin, f32_4 xMax, f32_4 yMin, f32_4 yMax) {
  100. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  101. }
  102. using math::sgn;
  103. inline f32_4 sgn(f32_4 x) {
  104. f32_4 signbit = x & -0.f;
  105. f32_4 nonzero = (x != 0.f);
  106. return signbit | (nonzero & 1.f);
  107. }
  108. } // namespace simd
  109. } // namespace rack