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 float_4(_mm_blendv_ps(b.v, a.v, mask.v));
56 // Equivalent but usually slower:
57 // return (a & mask) | andnot(mask, b);
58}
59
62template <typename T>
64
65template <>
67 // Pick out N'th bit of `a` and check if it's 1.
68 int32_4 mask1234 = int32_4(1, 2, 4, 8);
69 return (mask1234 & int32_4(a)) == mask1234;
70}
71
72template <>
76
77
78// Standard math functions from std::
79
80/* 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.
81
82Example:
83
84 template <typename T>
85 T sin_plus_cos(T x) {
86 return simd::sin(x) + simd::cos(x);
87 }
88*/
89
90using std::fmax;
91
93 return float_4(_mm_max_ps(x.v, b.v));
94}
95
96using std::fmin;
97
99 return float_4(_mm_min_ps(x.v, b.v));
100}
101
102using std::sqrt;
103
105 return float_4(_mm_sqrt_ps(x.v));
106}
107
108using std::log;
109
110inline float_4 log(float_4 x) {
111 return float_4(sse_mathfun_log_ps(x.v));
112}
113
114using std::log10;
115
117 return float_4(sse_mathfun_log_ps(x.v)) / std::log(10.f);
118}
119
120using std::log2;
121
123 return float_4(sse_mathfun_log_ps(x.v)) / std::log(2.f);
124}
125
126using std::exp;
127
128inline float_4 exp(float_4 x) {
129 return float_4(sse_mathfun_exp_ps(x.v));
130}
131
132using std::sin;
133
134inline float_4 sin(float_4 x) {
135 return float_4(sse_mathfun_sin_ps(x.v));
136}
137
138using std::cos;
139
140inline float_4 cos(float_4 x) {
141 return float_4(sse_mathfun_cos_ps(x.v));
142}
143
144using std::tan;
145
146inline float_4 tan(float_4 x) {
147 return float_4(sse_mathfun_tan_ps(x.v));
148}
149
150using std::atan;
151
153 return float_4(sse_mathfun_atan_ps(x.v));
154}
155
156using std::atan2;
157
159 return float_4(sse_mathfun_atan2_ps(x.v, y.v));
160}
161
162using std::trunc;
163
164// SIMDe defines _MM_FROUND_NO_EXC with a prefix
165#ifndef _MM_FROUND_NO_EXC
166 #define _MM_FROUND_NO_EXC SIMDE_MM_FROUND_NO_EXC
167#endif
168
170 return float_4(_mm_round_ps(a.v, _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC));
171}
172
173using std::floor;
174
176 return float_4(_mm_round_ps(a.v, _MM_FROUND_TO_NEG_INF | _MM_FROUND_NO_EXC));
177}
178
179using std::ceil;
180
182 return float_4(_mm_round_ps(a.v, _MM_FROUND_TO_POS_INF | _MM_FROUND_NO_EXC));
183}
184
185using std::round;
186
188 return float_4(_mm_round_ps(a.v, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
189}
190
191using std::fmod;
192
194 return a - floor(a / b) * b;
195}
196
197using std::hypot;
198
200 return sqrt(a * a + b * b);
201}
202
203using std::fabs;
204
206 // Sign bit
207 int32_4 mask = ~0x80000000;
208 return a & float_4::cast(mask);
209}
210
211using std::abs;
212
213inline float_4 abs(float_4 a) {
214 return fabs(a);
215}
216
217inline float_4 abs(std::complex<float_4> a) {
218 return hypot(a.real(), a.imag());
219}
220
221using std::arg;
222
223inline float_4 arg(std::complex<float_4> a) {
224 return atan2(a.imag(), a.real());
225}
226
227using std::pow;
228
230 return exp(b * log(a));
231}
232
233inline float_4 pow(float a, float_4 b) {
234 return exp(b * std::log(a));
235}
236
237template <typename T>
238T pow(T a, int b) {
239 // Optimal with `-O3 -funsafe-math-optimizations` when b is known at compile-time
240 T p = 1;
241 for (int i = 1; i <= b; i *= 2) {
242 if (i & b)
243 p *= a;
244 a *= a;
245 }
246 return p;
247}
248
249// From math.hpp
250
251using math::clamp;
252
253inline float_4 clamp(float_4 x, float_4 a = 0.f, float_4 b = 1.f) {
254 return fmin(fmax(x, a), b);
255}
256
257using math::rescale;
258
259inline float_4 rescale(float_4 x, float_4 xMin, float_4 xMax, float_4 yMin, float_4 yMax) {
260 return yMin + (x - xMin) / (xMax - xMin) * (yMax - yMin);
261}
262
263using math::crossfade;
264
266 return a + (b - a) * p;
267}
268
269using math::sgn;
270
271inline float_4 sgn(float_4 x) {
272 float_4 signbit = x & -0.f;
273 float_4 nonzero = (x != 0.f);
274 return signbit | (nonzero & 1.f);
275}
276
277
278} // namespace simd
279} // namespace rack
#define _MM_FROUND_NO_EXC
Definition functions.hpp:166
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
Abstraction of aligned types for SIMD computation.
Definition functions.hpp:8
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:223
float_4 abs(float_4 a)
Definition functions.hpp:213
Vector< int32_t, 4 > int32_4
Definition Vector.hpp:341
float_4 fmax(float_4 x, float_4 b)
Definition functions.hpp:92
Vector< float, 4 > float_4
Definition Vector.hpp:340
float_4 atan2(float_4 x, float_4 y)
Definition functions.hpp:158
float_4 fmin(float_4 x, float_4 b)
Definition functions.hpp:98
float_4 hypot(float_4 a, float_4 b)
Definition functions.hpp:199
float_4 cos(float_4 x)
Definition functions.hpp:140
float_4 pow(float_4 a, float_4 b)
Definition functions.hpp:229
int32_4 movemaskInverse< int32_4 >(int a)
Definition functions.hpp:66
float_4 crossfade(float_4 a, float_4 b, float_4 p)
Definition functions.hpp:265
float_4 sqrt(float_4 x)
Definition functions.hpp:104
float_4 tan(float_4 x)
Definition functions.hpp:146
float_4 movemaskInverse< float_4 >(int a)
Definition functions.hpp:73
float_4 ceil(float_4 a)
Definition functions.hpp:181
float_4 floor(float_4 a)
Definition functions.hpp:175
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:259
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:205
float_4 rcp(float_4 x)
Returns the approximate reciprocal.
Definition functions.hpp:42
float_4 log10(float_4 x)
Definition functions.hpp:116
float_4 clamp(float_4 x, float_4 a=0.f, float_4 b=1.f)
Definition functions.hpp:253
float_4 sgn(float_4 x)
Definition functions.hpp:271
float_4 log2(float_4 x)
Definition functions.hpp:122
float_4 trunc(float_4 a)
Definition functions.hpp:169
float_4 exp(float_4 x)
Definition functions.hpp:128
float_4 atan(float_4 x)
Definition functions.hpp:152
float_4 round(float_4 a)
Definition functions.hpp:187
float ifelse(bool cond, float a, float b)
Definition functions.hpp:49
float_4 log(float_4 x)
Definition functions.hpp:110
float_4 sin(float_4 x)
Definition functions.hpp:134
float_4 fmod(float_4 a, float_4 b)
Definition functions.hpp:193
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
static Vector cast(Vector< int32_t, 4 > a)
Definition Vector.hpp:163
__m128 v
Definition Vector.hpp:39
__m128i v
Definition Vector.hpp:113