-
Notifications
You must be signed in to change notification settings - Fork 0
/
vdsp.c
952 lines (855 loc) · 30 KB
/
vdsp.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
#include <string.h>
#include <math.h>
#include <assert.h>
#include "vdsp.h"
#ifdef __ARM_NEON
#include <arm_neon.h>
#else
#warning "NEON is not used"
#endif
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
#ifdef __ARM_NEON
static inline float AccumulateNeonLane(const float32x4_t lane) {
#ifdef __aarch64__
return vaddvq_f32(lane);
#else
return vgetq_lane_f32(lane, 0) + vgetq_lane_f32(lane, 1) +
vgetq_lane_f32(lane, 2) + vgetq_lane_f32(lane, 3);
#endif
}
// From:
// https://github.com/Tencent/ncnn/blob/master/src/layer/arm/neon_mathfun.h
#define c_inv_mant_mask ~0x7f800000u
#define c_cephes_SQRTHF 0.707106781186547524f
#define c_cephes_log_p0 7.0376836292E-2f
#define c_cephes_log_p1 - 1.1514610310E-1f
#define c_cephes_log_p2 1.1676998740E-1f
#define c_cephes_log_p3 - 1.2420140846E-1f
#define c_cephes_log_p4 + 1.4249322787E-1f
#define c_cephes_log_p5 - 1.6668057665E-1f
#define c_cephes_log_p6 + 2.0000714765E-1f
#define c_cephes_log_p7 - 2.4999993993E-1f
#define c_cephes_log_p8 + 3.3333331174E-1f
#define c_cephes_log_q1 -2.12194440e-4f
#define c_cephes_log_q2 0.693359375f
/* natural logarithm computed for 4 simultaneous float
return NaN for x <= 0
*/
static inline float32x4_t log_ps(float32x4_t x) {
float32x4_t one = vdupq_n_f32(1);
x = vmaxq_f32(x, vdupq_n_f32(0)); /* force flush to zero on denormal values */
uint32x4_t invalid_mask = vcleq_f32(x, vdupq_n_f32(0));
int32x4_t ux = vreinterpretq_s32_f32(x);
int32x4_t emm0 = vshrq_n_s32(ux, 23);
/* keep only the fractional part */
ux = vandq_s32(ux, vdupq_n_s32(c_inv_mant_mask));
ux = vorrq_s32(ux, vreinterpretq_s32_f32(vdupq_n_f32(0.5f)));
x = vreinterpretq_f32_s32(ux);
emm0 = vsubq_s32(emm0, vdupq_n_s32(0x7f));
float32x4_t e = vcvtq_f32_s32(emm0);
e = vaddq_f32(e, one);
/* part2:
if( x < SQRTHF ) {
e -= 1;
x = x + x - 1.0;
} else { x = x - 1.0; }
*/
uint32x4_t mask = vcltq_f32(x, vdupq_n_f32(c_cephes_SQRTHF));
float32x4_t tmp = vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(x), mask));
x = vsubq_f32(x, one);
e = vsubq_f32(e, vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(one), mask)));
x = vaddq_f32(x, tmp);
float32x4_t z = vmulq_f32(x, x);
float32x4_t y = vdupq_n_f32(c_cephes_log_p0);
y = vmulq_f32(y, x);
y = vaddq_f32(y, vdupq_n_f32(c_cephes_log_p1));
y = vmulq_f32(y, x);
y = vaddq_f32(y, vdupq_n_f32(c_cephes_log_p2));
y = vmulq_f32(y, x);
y = vaddq_f32(y, vdupq_n_f32(c_cephes_log_p3));
y = vmulq_f32(y, x);
y = vaddq_f32(y, vdupq_n_f32(c_cephes_log_p4));
y = vmulq_f32(y, x);
y = vaddq_f32(y, vdupq_n_f32(c_cephes_log_p5));
y = vmulq_f32(y, x);
y = vaddq_f32(y, vdupq_n_f32(c_cephes_log_p6));
y = vmulq_f32(y, x);
y = vaddq_f32(y, vdupq_n_f32(c_cephes_log_p7));
y = vmulq_f32(y, x);
y = vaddq_f32(y, vdupq_n_f32(c_cephes_log_p8));
y = vmulq_f32(y, x);
y = vmulq_f32(y, z);
tmp = vmulq_f32(e, vdupq_n_f32(c_cephes_log_q1));
y = vaddq_f32(y, tmp);
tmp = vmulq_f32(z, vdupq_n_f32(0.5f));
y = vsubq_f32(y, tmp);
tmp = vmulq_f32(e, vdupq_n_f32(c_cephes_log_q2));
x = vaddq_f32(x, y);
x = vaddq_f32(x, tmp);
x = vreinterpretq_f32_u32(
vorrq_u32(vreinterpretq_u32_f32(x), invalid_mask)); // negative arg will be NAN
return x;
}
#define c_exp_hi 88.3762626647949f
#define c_exp_lo -88.3762626647949f
#define c_cephes_LOG2EF 1.44269504088896341f
#define c_cephes_exp_C1 0.693359375f
#define c_cephes_exp_C2 -2.12194440e-4f
#define c_cephes_exp_p0 1.9875691500E-4f
#define c_cephes_exp_p1 1.3981999507E-3f
#define c_cephes_exp_p2 8.3334519073E-3f
#define c_cephes_exp_p3 4.1665795894E-2f
#define c_cephes_exp_p4 1.6666665459E-1f
#define c_cephes_exp_p5 5.0000001201E-1f
/* exp() computed for 4 float at once */
static inline float32x4_t exp_ps(float32x4_t x) {
float32x4_t tmp, fx;
float32x4_t one = vdupq_n_f32(1);
x = vminq_f32(x, vdupq_n_f32(c_exp_hi));
x = vmaxq_f32(x, vdupq_n_f32(c_exp_lo));
/* express exp(x) as exp(g + n*log(2)) */
fx = vmlaq_f32(vdupq_n_f32(0.5f), x, vdupq_n_f32(c_cephes_LOG2EF));
/* perform a floorf */
tmp = vcvtq_f32_s32(vcvtq_s32_f32(fx));
/* if greater, substract 1 */
uint32x4_t mask = vcgtq_f32(tmp, fx);
mask = vandq_u32(mask, vreinterpretq_u32_f32(one));
fx = vsubq_f32(tmp, vreinterpretq_f32_u32(mask));
tmp = vmulq_f32(fx, vdupq_n_f32(c_cephes_exp_C1));
float32x4_t z = vmulq_f32(fx, vdupq_n_f32(c_cephes_exp_C2));
x = vsubq_f32(x, tmp);
x = vsubq_f32(x, z);
static const float cephes_exp_p[6] = {c_cephes_exp_p0, c_cephes_exp_p1, c_cephes_exp_p2,
c_cephes_exp_p3, c_cephes_exp_p4, c_cephes_exp_p5};
float32x4_t y = vld1q_dup_f32(cephes_exp_p + 0);
float32x4_t c1 = vld1q_dup_f32(cephes_exp_p + 1);
float32x4_t c2 = vld1q_dup_f32(cephes_exp_p + 2);
float32x4_t c3 = vld1q_dup_f32(cephes_exp_p + 3);
float32x4_t c4 = vld1q_dup_f32(cephes_exp_p + 4);
float32x4_t c5 = vld1q_dup_f32(cephes_exp_p + 5);
y = vmulq_f32(y, x);
z = vmulq_f32(x, x);
y = vaddq_f32(y, c1);
y = vmulq_f32(y, x);
y = vaddq_f32(y, c2);
y = vmulq_f32(y, x);
y = vaddq_f32(y, c3);
y = vmulq_f32(y, x);
y = vaddq_f32(y, c4);
y = vmulq_f32(y, x);
y = vaddq_f32(y, c5);
y = vmulq_f32(y, z);
y = vaddq_f32(y, x);
y = vaddq_f32(y, one);
/* build 2^n */
int32x4_t mm;
mm = vcvtq_s32_f32(fx);
mm = vaddq_s32(mm, vdupq_n_s32(0x7f));
mm = vshlq_n_s32(mm, 23);
float32x4_t pow2n = vreinterpretq_f32_s32(mm);
y = vmulq_f32(y, pow2n);
return y;
}
static inline float32x4_t pow_ps(float32x4_t a, float32x4_t b) {
// pow(x, m) = exp(m * log(x))
return exp_ps(vmulq_f32(b, log_ps(a)));
}
#endif
#define HAMM_A 0.54f
#define HAMM_B 0.46f
void vDSP_hamm_window(float *__C, vDSP_Length __N, int __Flag) {
vDSP_Length n = __Flag ? (__N + 1) / 2 : __N;
for (vDSP_Length i = 0; i < n; ++i) {
__C[i] = HAMM_A - HAMM_B * cosf(2 * (float) M_PI * i / __N);
}
}
void vDSP_hann_window(float *__C, vDSP_Length __N, int __Flag) {
vDSP_Length n = (__Flag & vDSP_HALF_WINDOW) ? (__N + 1) / 2 : __N;
const float W = (__Flag & vDSP_HANN_NORM) ? 0.8165f : 0.5f;
for (vDSP_Length i = 0; i < n; ++i) {
__C[i] = W * (1.0f - cosf(2 * (float) M_PI * i / __N));
}
}
void vDSP_vfill(const float *__A, float *__C, vDSP_Stride __IC, vDSP_Length __N) {
for (vDSP_Length n = 0; n < __N; n += __IC) {
__C[n] = *__A;
}
}
void vDSP_dotpr(const float *__A, vDSP_Stride __IA, const float *__B, vDSP_Stride __IB, float *__C,
vDSP_Length __N) {
vDSP_Length n = 0;
*__C = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
float32x4_t c = vdupq_n_f32(0);
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
float32x4_t b = vld1q_f32(__B + n);
c = vmlaq_f32(c, a, b);
}
*__C = AccumulateNeonLane(c);
#endif
for (; n < __N; n++) {
*__C += __A[n] * __B[n];
}
}
void vDSP_vadd(const float *__A, vDSP_Stride __IA, const float *__B, vDSP_Stride __IB, float *__C,
vDSP_Stride __IC, vDSP_Length __N) {
vDSP_Length n = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
float32x4_t b = vld1q_f32(__B + n);
float32x4_t c = vaddq_f32(a, b);
vst1q_f32(__C + n, c);
}
#endif
for (; n < __N; ++n) {
__C[n] = __A[n] + __B[n];
}
}
void vDSP_vsub(const float *__B, vDSP_Stride __IB, const float *__A, vDSP_Stride __IA, float *__C,
vDSP_Stride __IC, vDSP_Length __N) {
for (vDSP_Length n = 0; n < __N; ++n) {
__C[n] = __A[n] - __B[n];
}
}
void vDSP_vmul(const float *__A, vDSP_Stride __IA, const float *__B, vDSP_Stride __IB, float *__C,
vDSP_Stride __IC, vDSP_Length __N) {
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
vDSP_Length n = 0;
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
float32x4_t b = vld1q_f32(__B + n);
float32x4_t c = vmulq_f32(a, b);
vst1q_f32(__C + n, c);
}
for (; n < __N; n++) {
__C[n] = __A[n] * __B[n];
}
#else
for (vDSP_Length i = 0; i < __N; ++i) {
__C[i] = __A[i] * __B[i];
}
#endif
}
void vDSP_vdiv(const float *__B, vDSP_Stride __IB, const float *__A, vDSP_Stride __IA, float *__C,
vDSP_Stride __IC, vDSP_Length __N) {
for (vDSP_Length i = 0; i < __N; ++i) {
__C[i] = __A[i] / __B[i];
}
}
void vDSP_zvmul(const DSPSplitComplex *__A, vDSP_Stride __IA, const DSPSplitComplex *__B,
vDSP_Stride __IB,
const DSPSplitComplex *__C, vDSP_Stride __IC, vDSP_Length __N, int __Conjugate) {
assert(__Conjugate == 1 || __Conjugate == -1);
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
vDSP_Length n = 0;
for (; n < postamble_start; n += 4) {
float32x4_t Ar = vld1q_f32(__A->realp + n);
float32x4_t Br = vld1q_f32(__B->realp + n);
float32x4_t Ai = vld1q_f32(__A->imagp + n);
float32x4_t Bi = vld1q_f32(__B->imagp + n);
float32x4_t Cr = vmulq_f32(Ar, Br);
float32x4_t Ci = vmulq_f32(Ar, Bi);
if (__Conjugate == 1) {
Cr = vmlsq_f32(Cr, Ai, Bi);
Ci = vmlaq_f32(Ci, Ai, Br);
} else {
Cr = vmlaq_f32(Cr, Ai, Bi);
Ci = vmlsq_f32(Ci, Ai, Br);
}
vst1q_f32(__C->realp + n, Cr);
vst1q_f32(__C->imagp + n, Ci);
}
for (; n < __N; n++) {
__C->realp[n] =
__A->realp[n] * __B->realp[n] - (float) __Conjugate * __A->imagp[n] * __B->imagp[n];
__C->imagp[n] =
__A->realp[n] * __B->imagp[n] + (float) __Conjugate * __A->imagp[n] * __B->realp[n];
}
#else
for (vDSP_Length n = 0; n < __N; ++n) {
__C->realp[n] =
__A->realp[n] * __B->realp[n] - (float) __Conjugate * __A->imagp[n] * __B->imagp[n];
__C->imagp[n] =
__A->realp[n] * __B->imagp[n] + (float) __Conjugate * __A->imagp[n] * __B->realp[n];
}
#endif
}
void vDSP_zrvmul(const DSPSplitComplex *__A, vDSP_Stride __IA, const float *__B, vDSP_Stride __IB,
const DSPSplitComplex *__C, vDSP_Stride __IC, vDSP_Length __N) {
vDSP_Length n = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
for (; n < postamble_start; n += 4) {
float32x4_t Ar = vld1q_f32(__A->realp + n);
float32x4_t Ai = vld1q_f32(__A->imagp + n);
float32x4_t B = vld1q_f32(__B + n);
float32x4_t Cr = vmulq_f32(Ar, B);
float32x4_t Ci = vmulq_f32(Ai, B);
vst1q_f32(__C->realp + n, Cr);
vst1q_f32(__C->imagp + n, Ci);
}
#endif
for (; n < __N; ++n) {
__C->realp[n] = __A->realp[n] * __B[n];
__C->imagp[n] = __A->imagp[n] * __B[n];
}
}
void vDSP_zrvdiv(const DSPSplitComplex *__A, vDSP_Stride __IA, const float *__B, vDSP_Stride __IB,
const DSPSplitComplex *__C, vDSP_Stride __IC, vDSP_Length __N) {
for (vDSP_Length n = 0; n < __N; ++n) {
__C->realp[n] = __A->realp[n] / __B[n];
__C->imagp[n] = __A->imagp[n] / __B[n];
}
}
void vDSP_zvadd(const DSPSplitComplex *__A, vDSP_Stride __IA, const DSPSplitComplex *__B,
vDSP_Stride __IB, const DSPSplitComplex *__C, vDSP_Stride __IC, vDSP_Length __N) {
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
vDSP_Length n = 0;
for (; n < postamble_start; n += 4) {
float32x4_t Ar = vld1q_f32(__A->realp + n);
float32x4_t Br = vld1q_f32(__B->realp + n);
float32x4_t Ai = vld1q_f32(__A->imagp + n);
float32x4_t Bi = vld1q_f32(__B->imagp + n);
float32x4_t Cr = vaddq_f32(Ar, Br);
float32x4_t Ci = vaddq_f32(Ai, Bi);;
vst1q_f32(__C->realp + n, Cr);
vst1q_f32(__C->imagp + n, Ci);
}
for (; n < __N; n++) {
__C->realp[n] = __A->realp[n] + __B->realp[n];
__C->imagp[n] = __A->imagp[n] + __B->imagp[n];
}
#else
for (vDSP_Length n = 0; n < __N; ++n) {
__C->realp[n] = __A->realp[n] + __B->realp[n];
__C->imagp[n] = __A->imagp[n] + __B->imagp[n];
}
#endif
}
void vDSP_zvsub(const DSPSplitComplex *__A, vDSP_Stride __IA, const DSPSplitComplex *__B,
vDSP_Stride __IB, const DSPSplitComplex *__C, vDSP_Stride __IC, vDSP_Length __N) {
vDSP_Length n = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
for (; n < postamble_start; n += 4) {
float32x4_t Ar = vld1q_f32(__A->realp + n);
float32x4_t Br = vld1q_f32(__B->realp + n);
float32x4_t Ai = vld1q_f32(__A->imagp + n);
float32x4_t Bi = vld1q_f32(__B->imagp + n);
float32x4_t Cr = vsubq_f32(Ar, Br);
float32x4_t Ci = vsubq_f32(Ai, Bi);;
vst1q_f32(__C->realp + n, Cr);
vst1q_f32(__C->imagp + n, Ci);
}
#endif
for (; n < __N; n++) {
__C->realp[n] = __A->realp[n] - __B->realp[n];
__C->imagp[n] = __A->imagp[n] - __B->imagp[n];
}
}
void vDSP_zvma(const DSPSplitComplex *__A, vDSP_Stride __IA, const DSPSplitComplex *__B,
vDSP_Stride __IB, const DSPSplitComplex *__C, vDSP_Stride __IC,
const DSPSplitComplex *__D, vDSP_Stride __ID, vDSP_Length __N) {
vDSP_Length n = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
for (; n < postamble_start; n += 4) {
float32x4_t Ar = vld1q_f32(__A->realp + n);
float32x4_t Br = vld1q_f32(__B->realp + n);
float32x4_t Cr = vld1q_f32(__C->realp + n);
float32x4_t Ai = vld1q_f32(__A->imagp + n);
float32x4_t Bi = vld1q_f32(__B->imagp + n);
float32x4_t Ci = vld1q_f32(__C->imagp + n);
float32x4_t Dr = vmlaq_f32(Cr, Ar, Br);
Dr = vmlsq_f32(Dr, Ai, Bi);
vst1q_f32(__D->realp + n, Dr);
float32x4_t Di = vmlaq_f32(Ci, Ar, Bi);
Di = vmlaq_f32(Di, Ai, Br);
vst1q_f32(__D->imagp + n, Di);
}
#endif
for (; n < __N; n++) {
__D->realp[n] =
__C->realp[n] + __A->realp[n] * __B->realp[n] - __A->imagp[n] * __B->imagp[n];
__D->imagp[n] =
__C->imagp[n] + __A->realp[n] * __B->imagp[n] + __A->imagp[n] * __B->realp[n];
}
}
void vDSP_zvabs(const DSPSplitComplex *__A, vDSP_Stride __IA, float *__C, vDSP_Stride __IC,
vDSP_Length __N) {
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
vDSP_Length n = 0;
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A->realp + n);
float32x4_t b = vld1q_f32(__A->imagp + n);
a = vmulq_f32(a, a);
a = vmlaq_f32(a, b, b);
float32x4_t a1 = vmaxq_f32(a, vdupq_n_f32(FLT_MIN));
float32x4_t e = vrsqrteq_f32(a1);
e = vmulq_f32(vrsqrtsq_f32(vmulq_f32(a1, e), e), e);
e = vmulq_f32(vrsqrtsq_f32(vmulq_f32(a1, e), e), e);
a = vmulq_f32(a, e);
vst1q_f32(__C + n, a);
}
for (; n < __N; n++) {
__C[n] = sqrtf(__A->realp[n] * __A->realp[n] + __A->imagp[n] * __A->imagp[n]);
}
#else
for (vDSP_Length n = 0; n < __N; ++n) {
__C[n] = sqrtf(__A->realp[n] * __A->realp[n] + __A->imagp[n] * __A->imagp[n]);
}
#endif
}
void vDSP_vmsa(const float *__A, vDSP_Stride __IA, const float *__B, vDSP_Stride __IB,
const float *__C, float *__D, vDSP_Stride __ID, vDSP_Length __N) {
for (vDSP_Length n = 0; n < __N; ++n) {
__D[n] = __A[n] * __B[n] + *__C;
}
}
void
vDSP_vsma(const float *__A, vDSP_Stride __IA, const float *__B, const float *__C, vDSP_Stride __IC,
float *__D, vDSP_Stride __ID, vDSP_Length __N) {
vDSP_Length n = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
const float32x4_t b = vdupq_n_f32(*__B);
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
float32x4_t c = vld1q_f32(__C + n);
float32x4_t d = vmlaq_f32(c, a, b);
vst1q_f32(__D + n, d);
}
#endif
for (; n < __N; n++) {
__D[n] = __A[n] * *__B + __C[n];
}
}
void vDSP_vsmsa(const float *__A, vDSP_Stride __IA, const float *__B, const float *__C, float *__D,
vDSP_Stride __ID, vDSP_Length __N) {
vDSP_Length n = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
const float32x4_t b = vdupq_n_f32(*__B);
const float32x4_t c = vdupq_n_f32(*__C);
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
float32x4_t d = vmlaq_f32(c, a, b);
vst1q_f32(__D + n, d);
}
#endif
for (; n < __N; n++) {
__D[n] = __A[n] * *__B + *__C;
}
}
void vDSP_vsmsma(const float *__A, vDSP_Stride __IA, const float *__B, const float *__C,
vDSP_Stride __IC, const float *__D, float *__E, vDSP_Stride __IE,
vDSP_Length __N) {
vDSP_Length n = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
float32x4_t b = vdupq_n_f32(*__B);
float32x4_t d = vdupq_n_f32(*__D);
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
float32x4_t c = vld1q_f32(__C + n);
float32x4_t e = vmlaq_f32(vmulq_f32(a, b), c, d);
vst1q_f32(__E + n, e);
}
#endif
for (; n < __N; n++) {
__E[n] = __A[n] * *__B + __C[n] * *__D;
}
}
void vDSP_maxvi(const float *__A, vDSP_Stride __IA, float *__C, vDSP_Length *__I, vDSP_Length __N) {
*__C = -INFINITY;
for (vDSP_Length n = 0; n < __N; ++n) {
if (__A[n] > *__C) {
*__C = __A[n];
*__I = n;
}
}
}
void vDSP_rmsqv(const float *__A, vDSP_Stride __IA, float *__C, vDSP_Length __N) {
float sum = 0;
for (vDSP_Length n = 0; n < __N; ++n) {
sum += __A[n] * __A[n];
}
*__C = sqrtf(sum / __N);
}
void vDSP_vdbcon(const float *__A, vDSP_Stride __IA, const float *__B, float *__C, vDSP_Stride __IC,
vDSP_Length __N, unsigned int __F) {
#ifdef __ARM_NEON
assert(__N % 4 == 0);
const float32x4_t alpha = vmulq_f32(vdupq_n_f32(__F == 1 ? 20 : 10),
vdupq_n_f32(0.43429448190325176f)); // 1/ln(10);
const float32x4_t b = vdupq_n_f32(*__B);
float32x4_t reciprocal = vrecpeq_f32(b);
// use a couple Newton-Raphson steps to refine the estimate
reciprocal = vmulq_f32(vrecpsq_f32(b, reciprocal), reciprocal);
reciprocal = vmulq_f32(vrecpsq_f32(b, reciprocal), reciprocal);
for (vDSP_Length i = 0; i < __N; i += 4) {
float32x4_t a = vld1q_f32(__A + i);
float32x4_t c = log_ps(vmulq_f32(a, reciprocal));
c = vmulq_f32(c, alpha);
vst1q_f32(__C + i, c);
}
#else
const int alpha = __F == 1 ? 20 : 10;
for (vDSP_Length n = 0; n < __N; ++n) {
__C[n] = alpha * log10f(__A[n] / *__B);
}
#endif
}
void vDSP_vclip(const float *__A, vDSP_Stride __IA, const float *__B, const float *__C, float *__D,
vDSP_Stride __ID, vDSP_Length __N) {
vDSP_Length n = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
const float32x4_t b = vdupq_n_f32(*__B);
const float32x4_t c = vdupq_n_f32(*__C);
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
a = vminq_f32(a, c);
a = vmaxq_f32(a, b);
vst1q_f32(__D + n, a);
}
#endif
for (; n < __N; n++) {
if (__A[n * __IA] < *__B) {
__D[n * __ID] = *__B;
} else if (__A[n * __IA] > *__C) {
__D[n * __ID] = *__C;
} else {
__D[n * __ID] = __A[n * __IA];
}
}
}
void vDSP_vthr(const float *__A, vDSP_Stride __IA, const float *__B, float *__C, vDSP_Stride __IC,
vDSP_Length __N) {
vDSP_Length n = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
const float32x4_t b = vdupq_n_f32(*__B);
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
a = vmaxq_f32(a, b);
vst1q_f32(__C + n, a);
}
#endif
for (; n < __N; n++) {
if (__A[n * __IA] < *__B) {
__C[n * __IC] = *__B;
} else {
__C[n * __IC] = __A[n * __IA];
}
}
}
#ifndef __ARM_NEON
// https://developer.apple.com/reference/accelerate/1449771-vdsp_vgenp?language=objc
void vDSP_vgenp(const float *__A, vDSP_Stride __IA, const float *__B, vDSP_Stride __IB, float *__C,
vDSP_Stride __IC, vDSP_Length __N, vDSP_Length __M) {
vDSP_Length n, m = 0;
vDSP_Length B[__M];
for (n = 0; n < __M; ++n) {
B[n] = (vDSP_Length) __B[n]; // truncate
}
for (n = 0; n < __N; ++n) {
if (n <= B[0]) {
__C[n] = __A[0];
} else if (n > B[__M - 1]) {
__C[n] = __A[__M - 1];
} else {
for (; m < __M && B[m] <= n; ++m);
m--;
__C[n] = __A[m] + (__A[m + 1] - __A[m]) * (n - __B[m]) / (__B[m + 1] - __B[m]);
}
}
}
#endif
void vDSP_ctoz(const DSPComplex *__C, vDSP_Stride __IC, const DSPSplitComplex *__Z,
vDSP_Stride __IZ, vDSP_Length __N) {
assert(__IC > 1);
vDSP_Length n = 0;
#ifdef __ARM_NEON
if (__IC == 2 && __IZ == 1) {
vDSP_Length postamble_start = __N & ~3;
for (; n < postamble_start; n += 4) {
float32x4x2_t C = vld2q_f32((float *) __C + 2 * n);
vst1q_f32(__Z->realp + n, C.val[0]);
vst1q_f32(__Z->imagp + n, C.val[1]);
}
}
#endif
for (; n < __N; n++) {
__Z->realp[n * __IZ] = __C[n * __IC / 2].real;
__Z->imagp[n * __IZ] = __C[n * __IC / 2].imag;
}
}
void vDSP_ztoc(const DSPSplitComplex *__Z, vDSP_Stride __IZ, DSPComplex *__C, vDSP_Stride __IC,
vDSP_Length __N) {
assert(__IC > 1);
vDSP_Length n = 0;
#ifdef __ARM_NEON
if (__IC == 2 && __IZ == 1) {
vDSP_Length postamble_start = __N & ~3;
float32x4x2_t Z;
for (; n < postamble_start; n += 4) {
Z.val[0] = vld1q_f32(__Z->realp + n);
Z.val[1] = vld1q_f32(__Z->imagp + n);
vst2q_f32((float *) __C + 2 * n, Z);
}
}
#endif
for (; n < __N; n++) {
__C[n * __IC / 2].real = __Z->realp[n * __IZ];
__C[n * __IC / 2].imag = __Z->imagp[n * __IZ];
}
}
void vDSP_sve(const float *__A, vDSP_Stride __I, float *__C, vDSP_Length __N) {
vDSP_Length n = 0;
*__C = 0;
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
float32x4_t c = vdupq_n_f32(0);
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
c = vaddq_f32(a, c);
}
*__C = AccumulateNeonLane(c);
#endif
for (; n < __N; n++) {
*__C += __A[n];
}
}
void vDSP_svesq(const float *__A, vDSP_Stride __IA, float *__C, vDSP_Length __N) {
*__C = 0;
for (vDSP_Length n = 0; n < __N; ++n) {
*__C += __A[n] * __A[n];
}
}
void vDSP_vsmul(const float *__A, vDSP_Stride __IA, const float *__B, float *__C, vDSP_Stride __IC,
vDSP_Length __N) {
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
vDSP_Length n = 0;
float32_t b = *__B;
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
float32x4_t c = vmulq_n_f32(a, b);
vst1q_f32(__C + n, c);
}
for (; n < __N; n++) {
__C[n] = __A[n] * b;
}
#else
for (vDSP_Length i = 0; i < __N; i++) {
__C[i] = __A[i] * *__B;
}
#endif
}
void vDSP_vsadd(const float *__A, vDSP_Stride __IA, const float *__B, float *__C, vDSP_Stride __IC,
vDSP_Length __N) {
#ifdef __ARM_NEON
float32x4_t b = vdupq_n_f32(*__B);
vDSP_Length postamble_start = __N & ~3;
vDSP_Length n = 0;
for (; n < postamble_start; n += 4) {
float32x4_t a = vld1q_f32(__A + n);
float32x4_t c = vaddq_f32(a, b);
vst1q_f32(__C + n, c);
}
for (; n < __N; n++) {
__C[n] = __A[n] + *__B;
}
#else
for (vDSP_Length i = 0; i < __N; i++) {
__C[i] = __A[i] + *__B;
}
#endif
}
void vDSP_vsdiv(const float *__A, vDSP_Stride __IA, const float *__B, float *__C, vDSP_Stride __IC,
vDSP_Length __N) {
const float divider = 1.0f / *__B;
vDSP_vsmul(__A, __IA, ÷r, __C, __IC, __N);
}
void
vDSP_vflt16(const short *__A, vDSP_Stride __IA, float *__C, vDSP_Stride __IC, vDSP_Length __N) {
#ifdef __ARM_NEON
for (vDSP_Length i = 0; i < __N; i += 4) {
// Load, convert to unsigned int, then to float
float32x4_t c = vcvtq_f32_s32(vmovl_s16(vld1_s16(__A + i)));
vst1q_f32(__C + i, c);
}
#else
for (vDSP_Length i = 0; i < __N; i++) {
__C[i] = (float) __A[i];
}
#endif
}
void
vDSP_vfix16(const float *__A, vDSP_Stride __IA, short *__C, vDSP_Stride __IC, vDSP_Length __N) {
// #ifdef __ARM_NEON
// for (vDSP_Length i = 0; i < __N; i+=4) {
// float32x4_t a = vld1q_f32(__A + i);
// int16x4_t c = vmovn_s32(vcvtq_s32_f32(a));
// vst1_s16(__C + i, c);
// }
// #else
for (vDSP_Length i = 0; i < __N; i++) {
__C[i] = (short) roundf(__A[i]);
}
// #endif
}
void vDSP_mmul(const float *__A, vDSP_Stride __IA, const float *__B, vDSP_Stride __IB, float *__C,
vDSP_Stride __IC, vDSP_Length __M, vDSP_Length __N, vDSP_Length __P) {
if (__N == 1) { // Matrix x Vector aka cblas_sgemv
for (unsigned m = 0; m < __M; m++) {
vDSP_dotpr(__A + m * __P, __IA, __B, __IB, __C + m, __P);
}
} else {
for (int m = 0; m < __M; m++) {
for (int n = 0; n < __N; n++) {
float cell = 0;
for (int p = 0; p < __P; p++) {
cell += __A[(m * __P + p) * __IA] * __B[(p * __N + n) * __IB];
}
__C[(m * __N + n) * __IC] = cell;
}
}
}
}
static const float minus_one = -1.0f;
void vDSP_zvconj(const DSPSplitComplex *__A, vDSP_Stride __IA, const DSPSplitComplex *__C,
vDSP_Stride __IC, vDSP_Length __N) {
/*
for (vDSP_Length n = 0; n < __N; n++) {
__C->realp[n] = __A->realp[n];
__C->imagp[n] = -1.0f * __A->imagp[n];
}
*/
if (__A->realp != __C->realp) {
memcpy(__C->realp, __A->realp, __N * sizeof(float));
}
vDSP_vsmul(__A->imagp, 1, &minus_one, __C->imagp, 1, __N);
}
void vDSP_zmmul(const DSPSplitComplex *__A, vDSP_Stride __IA, const DSPSplitComplex *__B,
vDSP_Stride __IB, const DSPSplitComplex *__C, vDSP_Stride __IC, vDSP_Length __M,
vDSP_Length __N, vDSP_Length __P) {
for (int m = 0; m < __M; m++) {
for (int n = 0; n < __N; n++) {
float realp = 0, imagp = 0;
// TODO: optimize
for (int p = 0; p < __P; p++) {
realp += __A->realp[(m * __P + p) * __IA] * __B->realp[(p * __N + n) * __IB] -
__A->imagp[(m * __P + p) * __IA] * __B->imagp[(p * __N + n) * __IB];
imagp += __A->realp[(m * __P + p) * __IA] * __B->imagp[(p * __N + n) * __IB] +
__A->imagp[(m * __P + p) * __IA] * __B->realp[(p * __N + n) * __IB];
}
__C->realp[(m * __N + n) * __IC] = realp;
__C->imagp[(m * __N + n) * __IC] = imagp;
}
}
}
void vDSP_zvmags(const DSPSplitComplex *__A, vDSP_Stride __IA, float *__C, vDSP_Stride __IC,
vDSP_Length __N) {
#ifdef __ARM_NEON
vDSP_Length postamble_start = __N & ~3;
vDSP_Length n = 0;
for (; n < postamble_start; n += 4) {
float32x4_t Ar = vld1q_f32(__A->realp + n);
float32x4_t Ai = vld1q_f32(__A->imagp + n);
float32x4_t C = vmlaq_f32(vmulq_f32(Ar, Ar), Ai, Ai);
vst1q_f32(__C + n, C);
}
for (; n < __N; n++) {
__C[n] = __A->realp[n] * __A->realp[n] + __A->imagp[n] * __A->imagp[n];
}
#else
for (int n = 0; n < __N; ++n) {
__C[n] = __A->realp[n] * __A->realp[n] + __A->imagp[n] * __A->imagp[n];
}
#endif
}
void vDSP_mtrans(const float *__A, vDSP_Stride __IA, float *__C, vDSP_Stride __IC, vDSP_Length __M,
vDSP_Length __N) {
for (int m = 0; m < __M; m++) {
for (int n = 0; n < __N; n++) {
__C[(m * __N + n) * __IC] = __A[(n * __M + m) * __IA];
}
}
}
void vDSP_deq22(const float *__A, vDSP_Stride __IA, const float *__B, float *__C, vDSP_Stride __IC,
vDSP_Length __N) {
for (int n = 2; n < __N + 2; ++n) {
__C[n] = __A[n] * __B[0] + __A[n - 1] * __B[1] + __A[n - 2] * __B[2] -
__C[n - 1] * __B[3] - __C[n - 2] * __B[4];
}
}
/* Reasonably fast for small sizes, use FFT implementation for large sizes instead */
void vDSP_conv(const float *__A, vDSP_Stride __IA, const float *__F, vDSP_Stride __IF,
float *__C, vDSP_Stride __IC, vDSP_Length __N, vDSP_Length __P) {
for (vDSP_Length n = 0; n < __N; ++n) {
float sum = 0;
for (vDSP_Length p = 0; p < __P; p++) {
sum += __A[n + p] * __F[p * __IF];
}
__C[n] = sum;
}
}
void vvcosf(float *out, const float *in, const int *size) {
for (int i = 0; i < *size; i++) {
out[i] = cosf(in[i]);
}
}
void vvsinf(float *out, const float *in, const int *size) {
for (int i = 0; i < *size; i++) {
out[i] = sinf(in[i]);
}
}
void vvlog10f(float *out, const float *in, const int *size) {
for (int i = 0; i < *size; i++) {
out[i] = log10f(in[i]);
}
}
void vvsqrtf(float *out, const float *in, const int *size) {
for (int i = 0; i < *size; i++) {
out[i] = sqrtf(in[i]);
}
}
void vvpowf(float *z, const float *y, const float *x, const int *n) {
int i = 0;
#ifdef __ARM_NEON
int postamble_start = *n & ~3;
for (; i < postamble_start; i += 4) {
float32x4_t X = vld1q_f32(x + i);
float32x4_t Y = vld1q_f32(y + i);
vst1q_f32(z + i, pow_ps(X, Y));
}
#endif
for (; i < *n; i++) {
z[i] = powf(x[i], y[i]);
}
}
void vvexpf(float *y, const float *x, const int *n) {
int i = 0;
#ifdef __ARM_NEON
int postamble_start = *n & ~3;
for (; i < postamble_start; i += 4) {
float32x4_t X = vld1q_f32(x + i);
vst1q_f32(y + i, exp_ps(X));
}
#endif
for (; i < *n; i++) {
y[i] = expf(x[i]);
}
}