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 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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::exp;
  34. inline f32_4 exp(f32_4 x) {
  35. return f32_4(sse_mathfun_exp_ps(x.v));
  36. }
  37. using std::sin;
  38. inline f32_4 sin(f32_4 x) {
  39. return f32_4(sse_mathfun_sin_ps(x.v));
  40. }
  41. using std::cos;
  42. inline f32_4 cos(f32_4 x) {
  43. return f32_4(sse_mathfun_cos_ps(x.v));
  44. }
  45. using std::floor;
  46. inline f32_4 floor(f32_4 a) {
  47. return f32_4(sse_mathfun_floor_ps(a.v));
  48. }
  49. using std::ceil;
  50. inline f32_4 ceil(f32_4 a) {
  51. return f32_4(sse_mathfun_ceil_ps(a.v));
  52. }
  53. using std::round;
  54. inline f32_4 round(f32_4 a) {
  55. return f32_4(sse_mathfun_round_ps(a.v));
  56. }
  57. using std::fmod;
  58. inline f32_4 fmod(f32_4 a, f32_4 b) {
  59. return f32_4(sse_mathfun_fmod_ps(a.v, b.v));
  60. }
  61. using std::fabs;
  62. inline f32_4 fabs(f32_4 a) {
  63. return f32_4(sse_mathfun_fabs_ps(a.v));
  64. }
  65. using std::trunc;
  66. inline f32_4 trunc(f32_4 a) {
  67. return f32_4(sse_mathfun_trunc_ps(a.v));
  68. }
  69. using std::pow;
  70. inline f32_4 pow(f32_4 a, f32_4 b) {
  71. return exp(b * log(a));
  72. }
  73. inline f32_4 pow(float a, f32_4 b) {
  74. return exp(b * std::log(a));
  75. }
  76. // Nonstandard functions
  77. /** Returns the approximate reciprocal square root.
  78. Much faster than `1/sqrt(x)`.
  79. */
  80. inline f32_4 rsqrt(f32_4 x) {
  81. return f32_4(_mm_rsqrt_ps(x.v));
  82. }
  83. /** Returns the approximate reciprocal.
  84. Much faster than `1/x`.
  85. */
  86. inline f32_4 rcp(f32_4 x) {
  87. return f32_4(_mm_rcp_ps(x.v));
  88. }
  89. // From math.hpp
  90. using math::clamp;
  91. inline f32_4 clamp(f32_4 x, f32_4 a, f32_4 b) {
  92. return fmin(fmax(x, a), b);
  93. }
  94. using math::rescale;
  95. inline f32_4 rescale(f32_4 x, f32_4 xMin, f32_4 xMax, f32_4 yMin, f32_4 yMax) {
  96. return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
  97. }
  98. } // namespace simd
  99. } // namespace rack