-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrMath.c
458 lines (377 loc) · 9.86 KB
/
wrMath.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include "wrMath.h"
#include <math.h>
// this function takes 0-1
// does a 1/x lookup with inverse
// particularly useful for ramp->tri->saw morphs
// where total time (pitch) remains stable
// NB: 3 divisions. use a LUT if you're out of cycles
void math_get_ramps( float skew, float* rise, float* fall )
{
*rise = 0.5f / (0.998f * skew + 0.001f);
*fall = 1.0f/ (2.0f - (1.0f / *rise));
}
float max_f(float a, float b) {
return (a > b ? a : b);
}
float min_f(float a, float b) {
return (a < b ? a : b);
}
float lim_f(float in, float min, float max)
{
return (in < min ? min : in > max ? max : in);
}
float lim_f_0_1(float in)
{
return (in < 0.0f ? 0.0f : in > 1.0f ? 1.0f : in);
}
float lim_f_n1_1(float in)
{
return (in < -1.0f ? -1.0f : in > 1.0f ? 1.0f : in);
}
float wrap_f(float in, float min, float max) {
float diff = max - min;
while(in<min) { in += diff; }
while(in>=max) { in -= diff; }
return in;
}
float dB_to_amp( float dB ){
return powf(10.0, dB/20.0);
}
float amp_to_dB( float amp ){
return 20.0 * log10f(amp);
}
/*float interp_lin_asm(float in1, float in2, float mix)
{
// accurate at min & max. arm cortex m4 instruction set
__asm__(
"vfnms.f32 s0, s0, s2 \n\t"
"vfnms.f32 s0, s1, s2 \n\t"
);
}*/
float interp_lin_f(float in1, float in2, float mix) {
return (in1 + mix * (in2 - in1));
}
float interp_lin_f_2d(float in1_x, float in2_x,
float in1_y, float in2_y,
float mix_x, float mix_y){
float tmp, tmp2;
tmp = in1_x + mix_x*(in2_x - in1_x);
tmp2 = in1_y + mix_x*(in2_y - in1_y);
return (tmp + mix_y*(tmp2 - tmp));
}
int32_t lim_i24_audio( int32_t in )
{
return ( (in < (int32_t)MIN24b)
? (int32_t)MIN24b
: (in > (int32_t)MAX24b)
? (int32_t)MAX24b
: in
);
}
int32_t lim_i32(int32_t in, int32_t min, int32_t max) {
return (in < min ? min : in > max ? max : in);
}
int32_t wrap_i32(int32_t in, int32_t min, int32_t max) {
int32_t diff = max - min;
while(in<min) { in += diff; }
while(in>max) { in -= diff; }
return in;
}
int16_t min_u16(uint16_t a, uint16_t b) {
return (a < b ? a : b);
}
uint8_t min_u8(uint8_t a, uint8_t b) {
return (a < b ? a : b);
}
uint8_t max_u8(uint8_t a, uint8_t b) {
return (a > b ? a : b);
}
/////////////////////////////////////
// block processing math functions //
/////////////////////////////////////
void lim_v32_32(int32_t* in, int32_t min, int32_t max, int32_t* out, uint16_t size) {
int32_t* in2=in;
int32_t* out2=out;
for(uint16_t i=0; i<size; i++) {
if(*in2 < min) {
*out2++ = min; // lower limit
in2++;
}
else if(*in2 > max) {
*out2++ = max; // upper limit
in2++;
}
else {
*out2++ = *in2++; // echo in range
}
}
}
void add_v32_v32_sat24(int32_t* a, int32_t* b, int32_t* out, uint16_t size){
int32_t* a2=a;
int32_t* b2=b;
int32_t* out2=out;
for(uint16_t i=0; i<size; i++) {
*out2 = (*a2++) + (*b2++);
if(*out2 < MIN24b) {
*out2++ = MIN24b;
}
else if(*out2 > MAX24b) {
*out2++ = MAX24b; // upper limit
}
else { out2++; }
}
}
void muladd_v32_f_v32_sat24(int32_t* in, float mul, int32_t* add, int32_t* out, uint16_t size) {
int32_t* in2 = in;
int32_t* add2 = add;
int32_t* out2 = out;
for(uint16_t i=0; i<size; i++) {
// multiply v32 by float (in * thrulevel)
// add above(vf) to (vi)output
*out2 = (int32_t)((float)(*in2++) * mul) + (*add2++);
// saturate to 24b
if(*out2 < MIN24b) {
*out2++ = MIN24b;
}
else if(*out2 > MAX24b) {
*out2++ = MAX24b; // upper limit
}
else { out2++; }
}
}
void lim_vf_f(float* in, float min, float max, float* out, uint16_t size) {
float* in2=in;
float* out2=out;
for(uint16_t i=0; i<size; i++) {
if(*in2 < min) { *out2++ = min; } // lower limit
else if(*in2 > max) { *out2++ = max; } // upper limit
else { *out2++ = *in2; } // echo in range
in2++;
}
}
float* lim_vf_audio( float* audio, int size )
{
float* s = audio;
for( int i=0; i<size; i++ ){
if( *s < -1.0 ) { *s++ = -1.0; } // lower limit
else if( *s > 1.0 ) { *s++ = 1.0; } // upper limit
else { s++; } // don't change
}
return audio;
}
// set an array to a single value
void set_v32_32(int32_t b, int32_t* out, uint16_t size) {
int32_t* out2=out; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*out2++ = b;
}
}
//////////////////////////
////////////////////// ADD
// add two arrays of floats sequentially
void add_vf_vf(float* a, float* b, float* sum, uint16_t size) {
float* a2=a;
float* b2=b;
float* sum2=sum; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*sum2++ = (*a2++) + (*b2++);
}
}
// increment float array by scalar
void add_vf_f(float* a, float b, float* sum, uint16_t size) {
float* a2=a;
float* sum2=sum; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*sum2++ = (*a2++) + b;
}
}
void sub_vf_f(float* a, float b, float* diff, uint16_t size) {
float* a2=a;
float* diff2=diff; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*diff2++ = (*a2++) - b;
}
}
//////////////////////////
////////////////////// MUL
// vector multiplication
void mul_vf_vf(float* a, float* b, float* product, uint16_t size) {
float* a2=a;
float* b2=b;
float* product2=product; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*product2++ = (*a2++) * (*b2++);
}
}
// vector x constant multiply
float* mul_vf_f(float* a, float b, int size){
float* a2=a;
for( int i=0; i<size; i++ ){
*a2++ *= b;
}
return a;
}
void muladd_vf_f_f(float* vin, float mul, float add, float* product, uint16_t size) {
float* vin2=vin;
float* product2=product; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*product2++ = (*vin2++) * mul + add;
}
}
void muladd_vf_f_vf(float* vin, float mul, float* vadd, float* product, uint16_t size) {
float* vin2=vin;
float* vadd2=vadd;
float* product2=product; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*product2++ = (*vin2++) * mul + (*vadd2++);
}
}
void mac_vf_f_vf(float* vmul, float mul, float* vadd, uint16_t size) {
float* vmul2=vmul;
float* vadd2=vadd; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
(*vadd2++) += (*vmul2++) * mul;
}
}
//////////////////////////
////////////////////// DIV
// vector x constant divion
void div_vf_f(float* a, float b, float* divd, uint16_t size) {
float* a2=a;
float* divd2=divd; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*divd2++ = (*a2++) / b;
}
}
//////////////////////////
/////////////////// INTERP
void interp_lin_f_v(float* a, float* b, float* c, float* out, uint16_t size) {
float* a2=a;
float* b2=b;
float* c2=c;
float* out2=out;
for(uint16_t i=0;i<size;i++) {
*out2++ = *a2 + *c2++ * (*b2++ - *a2);
a2++;
}
}
void interp_lin_f_vvf(float* a, float* b, float c, float* out, uint16_t size) {
float* a2=a;
float* b2=b;
float* out2=out;
for(uint16_t i=0;i<size;i++) {
*out2++ = *a2 + c * (*b2++ - *a2);
a2++;
}
}
//////////////////////////
//////////////// SET VALUE
// copy whole vector
void set_v8_v8(uint8_t* b, uint8_t* out, uint16_t size) {
uint8_t* b2=b;
uint8_t* out2=out; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*out2++ = (*b2++);
}
}
// set vector to a constant
void set_v8_8(uint8_t b, uint8_t* out, uint16_t size) {
uint8_t* out2=out; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*out2++ = b;
}
}
//////////////////////////
////////////////////// ADD
// add two vectors
void add_v8_v8(uint8_t* a, uint8_t* b, uint8_t* sum, uint16_t size) {
uint8_t* a2=a;
uint8_t* b2=b;
uint8_t* sum2=sum; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*sum2++ = (*a2++) + (*b2++);
}
}
// shift vector by scalar
void add_v8_8(uint8_t* a, uint8_t b, uint8_t* sum, uint16_t size) {
uint8_t* a2=a;
uint8_t* sum2=sum; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*sum2++ = (*a2++) + b;
}
}
void add_v32_32(int32_t* a, int32_t b, int32_t* sum, uint16_t size) {
int32_t* a2=a;
int32_t* sum2=sum; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*sum2++ = (*a2++) + b;
}
}
/*
void lim_vf_f(float* in, float min, float max, float* out, uint16_t size) {
float* in2=in;
float* out2=out;
for(uint16_t i=0; i<size; i++) {
if(*in2 < min) { *out2++ = min; } // lower limit
else if(*in2 > max) { *out2++ = max; } // upper limit
else { *out2++ = *in2; } // echo in range
*in2++;
}
}
*/
//////////////////////////
////////////////// BITWISE
// bitwise OR a vector with a single mask
void OR_v8_8(uint8_t* a, uint8_t mask, uint8_t* out, uint16_t size) {
uint8_t* a2=a;
uint8_t* out2=out; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*out2++ = (*a2++) | mask;
}
}
// bitwise AND a vector with a single mask
void AND_v8_8(uint8_t* a, uint8_t mask, uint8_t* out, uint16_t size) {
uint8_t* a2=a;
uint8_t* out2=out; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*out2++ = (*a2++) & mask;
}
}
// left bitshift
void SHL_v8_8(uint8_t* a, uint8_t shift, uint8_t* out, uint16_t size) {
uint8_t* a2=a;
uint8_t* out2=out; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*out2++ = (*a2++) << shift;
}
}
// right bitshift
void SHR_v8_8(uint8_t* a, uint8_t shift, uint8_t* out, uint16_t size) {
uint8_t* a2=a;
uint8_t* out2=out; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*out2++ = (*a2++) >> shift;
}
}
// right bitshift
// about 30% faster!
void SHR_v32_32(int32_t* a, uint16_t shift, int32_t* out, uint16_t size) {
int32_t* a2=a;
int32_t* out2=out; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*out2++ = (*a2++) >> shift;
}
}
void SHRadd_v32_32(int32_t* a, uint16_t shift, int32_t add, int32_t* out, uint16_t size) {
int32_t* a2=a;
int32_t* out2=out; // point to start of arrays
for(uint16_t i=0; i<size; i++) {
*out2++ = ((*a2++) >> shift) + add;
}
}
int16_t max_s16( int16_t a, int16_t b )
{
return (( a < b )
? a
: b );
}