VCV Rack API v2
Loading...
Searching...
No Matches
functions.hpp
Go to the documentation of this file.
1#pragma once
2#include <simd/Vector.hpp>
4#include <math.hpp>
5
6
7namespace rack {
8namespace simd {
9
10
11// Functions based on instructions
12
15 return float_4(_mm_andnot_ps(a.v, b.v));
16}
17
21inline int movemask(float_4 a) {
22 return _mm_movemask_ps(a.v);
23}
24
28inline int movemask(int32_4 a) {
29 return _mm_movemask_ps(_mm_castsi128_ps(a.v));
30}
31
36 return float_4(_mm_rsqrt_ps(x.v));
37}
38
42inline float_4 rcp(float_4 x) {
43 return float_4(_mm_rcp_ps(x.v));
44}
45
46
47// Nonstandard convenience functions
48
49inline float ifelse(bool cond, float a, float b) {
50 return cond ? a : b;
51}
52
54inline float_4 ifelse(float_4 mask, float_4 a, float_4 b) {
55 return (a & mask) | andnot(mask, b);
56}
57
60template <typename T>
62
63template <>
65 // Pick out N'th bit of `a` and check if it's 1.
66 int32_4 mask1234 = int32_4(1, 2, 4, 8);
67 return (mask1234 & int32_4(a)) == mask1234;
68}
69
70template <>
74
75
76// Standard math functions from std::
77
78/* 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.
79
80Example:
81
82 template <typename T>
83 T sin_plus_cos(T x) {
84 return simd::sin(x) + simd::cos(x);
85 }
86*/
87
88using std::fmax;
89
91 return float_4(_mm_max_ps(x.v, b.v));
92}
93
94using std::fmin;
95
97 return float_4(_mm_min_ps(x.v, b.v));
98}
99
100using std::sqrt;
101
103 return float_4(_mm_sqrt_ps(x.v));
104}
105
106using std::log;
107
108inline float_4 log(float_4 x) {
109 return float_4(sse_mathfun_log_ps(x.v));
110}
111
112using std::log10;
113
115 return float_4(sse_mathfun_log_ps(x.v)) / std::log(10.f);
116}
117
118using std::log2;
119
121 return float_4(sse_mathfun_log_ps(x.v)) / std::log(2.f);
122}
123
124using std::exp;
125
126inline float_4 exp(float_4 x) {
127 return float_4(sse_mathfun_exp_ps(x.v));
128}
129
130using std::sin;
131
132inline float_4 sin(float_4 x) {
133 return float_4(sse_mathfun_sin_ps(x.v));
134}
135
136using std::cos;
137
138inline float_4 cos(float_4 x) {
139 return float_4(sse_mathfun_cos_ps(x.v));
140}
141
142using std::tan;
143
144inline float_4 tan(float_4 x) {
145 return float_4(sse_mathfun_tan_ps(x.v));
146}
147
148using std::atan;
149
151 return float_4(sse_mathfun_atan_ps(x.v));
152}
153
154using std::atan2;
155
157 return float_4(sse_mathfun_atan2_ps(x.v, y.v));
158}
159
160using std::trunc;
161
162// SIMDe defines _MM_FROUND_NO_EXC with a prefix
163#ifndef _MM_FROUND_NO_EXC
164 #define _MM_FROUND_NO_EXC SIMDE_MM_FROUND_NO_EXC
165#endif
166
168 return float_4(_mm_round_ps(a.v, _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC));
169}
170
171using std::floor;
172
174 return float_4(_mm_round_ps(a.v, _MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC));
175}
176
177using std::ceil;
178
180 return float_4(_mm_round_ps(a.v, _MM_FROUND_TO_POS_INF | _MM_FROUND_NO_EXC));
181}
182
183using std::round;
184
186 return float_4(_mm_round_ps(a.v, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
187}
188
189using std::fmod;
190
192 return a - floor(a / b) * b;
193}
194
195using std::hypot;
196
198 return sqrt(a * a + b * b);
199}
200
201using std::fabs;
202
204 // Sign bit
205 int32_4 mask = ~0x80000000;
206 return a & float_4::cast(mask);
207}
208
209using std::abs;
210
211inline float_4 abs(float_4 a) {
212 return fabs(a);
213}
214
215inline float_4 abs(std::complex<float_4> a) {
216 return hypot(a.real(), a.imag());
217}
218
219using std::arg;
220
221inline float_4 arg(std::complex<float_4> a) {
222 return atan2(a.imag(), a.real());
223}
224
225using std::pow;
226
228 return exp(b * log(a));
229}
230
231inline float_4 pow(float a, float_4 b) {
232 return exp(b * std::log(a));
233}
234
235template <typename T>
236T pow(T a, int b) {
237 // Optimal with `-O3 -funsafe-math-optimizations` when b is known at compile-time
238 T p = 1;
239 for (int i = 1; i <= b; i *= 2) {
240 if (i & b)
241 p *= a;
242 a *= a;
243 }
244 return p;
245}
246
247// From math.hpp
248
249using math::clamp;
250
251inline float_4 clamp(float_4 x, float_4 a = 0.f, float_4 b = 1.f) {
252 return fmin(fmax(x, a), b);
253}
254
255using math::rescale;
256
257inline float_4 rescale(float_4 x, float_4 xMin, float_4 xMax, float_4 yMin, float_4 yMax) {
258 return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
259}
260
261using math::crossfade;
262
264 return a + (b - a) * p;
265}
266
267using math::sgn;
268
269inline float_4 sgn(float_4 x) {
270 float_4 signbit = x & -0.f;
271 float_4 nonzero = (x != 0.f);
272 return signbit | (nonzero & 1.f);
273}
274
275
276} // namespace simd
277} // namespace rack
#define _MM_FROUND_NO_EXC
Definition functions.hpp:164
int clamp(int x, int a, int b)
Limits x between a and b.
Definition math.hpp:32
float rescale(float x, float xMin, float xMax, float yMin, float yMax)
Rescales x from the range [xMin, xMax] to [yMin, yMax].
Definition math.hpp:151
float crossfade(float a, float b, float p)
Linearly interpolates between a and b, from p = 0 to p = 1.
Definition math.hpp:157
T sgn(T x)
Returns 1 for positive numbers, -1 for negative numbers, and 0 for zero.
Definition math.hpp:95
float_4 andnot(float_4 a, float_4 b)
~a & b
Definition functions.hpp:14
float_4 arg(std::complex< float_4 > a)
Definition functions.hpp:221
float_4 abs(float_4 a)
Definition functions.hpp:211
Vector< int32_t, 4 > int32_4
Definition Vector.hpp:341
float_4 fmax(float_4 x, float_4 b)
Definition functions.hpp:90
Vector< float, 4 > float_4
Definition Vector.hpp:340
float_4 atan2(float_4 x, float_4 y)
Definition functions.hpp:156
float_4 fmin(float_4 x, float_4 b)
Definition functions.hpp:96
float_4 hypot(float_4 a, float_4 b)
Definition functions.hpp:197
float_4 cos(float_4 x)
Definition functions.hpp:138
float_4 pow(float_4 a, float_4 b)
Definition functions.hpp:227
int32_4 movemaskInverse< int32_4 >(int a)
Definition functions.hpp:64
float_4 crossfade(float_4 a, float_4 b, float_4 p)
Definition functions.hpp:263
float_4 sqrt(float_4 x)
Definition functions.hpp:102
float_4 tan(float_4 x)
Definition functions.hpp:144
float_4 movemaskInverse< float_4 >(int a)
Definition functions.hpp:71
float_4 ceil(float_4 a)
Definition functions.hpp:179
float_4 floor(float_4 a)
Definition functions.hpp:173
T movemaskInverse(int a)
Returns a vector where element N is all 1's if the N'th bit of a is 1, or all 0's if the N'th bit of ...
float_4 rsqrt(float_4 x)
Returns the approximate reciprocal square root.
Definition functions.hpp:35
float_4 rescale(float_4 x, float_4 xMin, float_4 xMax, float_4 yMin, float_4 yMax)
Definition functions.hpp:257
int movemask(float_4 a)
Returns an integer with each bit corresponding to the most significant bit of each element.
Definition functions.hpp:21
float_4 fabs(float_4 a)
Definition functions.hpp:203
float_4 rcp(float_4 x)
Returns the approximate reciprocal.
Definition functions.hpp:42
float_4 log10(float_4 x)
Definition functions.hpp:114
float_4 clamp(float_4 x, float_4 a=0.f, float_4 b=1.f)
Definition functions.hpp:251
float_4 sgn(float_4 x)
Definition functions.hpp:269
float_4 log2(float_4 x)
Definition functions.hpp:120
float_4 trunc(float_4 a)
Definition functions.hpp:167
float_4 exp(float_4 x)
Definition functions.hpp:126
float_4 atan(float_4 x)
Definition functions.hpp:150
float_4 round(float_4 a)
Definition functions.hpp:185
float ifelse(bool cond, float a, float b)
Definition functions.hpp:49
float_4 log(float_4 x)
Definition functions.hpp:108
float_4 sin(float_4 x)
Definition functions.hpp:132
float_4 fmod(float_4 a, float_4 b)
Definition functions.hpp:191
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
__m128 sse_mathfun_cos_ps(__m128 x)
Definition sse_mathfun.h:297
__m128 sse_mathfun_log_ps(__m128 x)
Definition sse_mathfun.h:58
__m128 sse_mathfun_exp_ps(__m128 x)
Definition sse_mathfun.h:129
__m128 sse_mathfun_sin_ps(__m128 x)
Definition sse_mathfun.h:209
__m128 sse_mathfun_atan_ps(__m128 x)
Definition sse_mathfun_extension.h:176
__m128 sse_mathfun_tan_ps(__m128 x)
Definition sse_mathfun_extension.h:167
__m128 sse_mathfun_atan2_ps(__m128 y, __m128 x)
Definition sse_mathfun_extension.h:247
Wrapper for __m128 representing an aligned vector of 4 single-precision float values.
Definition Vector.hpp:34
static Vector cast(Vector< int32_t, 4 > a)
Definition Vector.hpp:163
__m128 v
Definition Vector.hpp:39
Definition Vector.hpp:108
__m128i v
Definition Vector.hpp:113