-
Notifications
You must be signed in to change notification settings - Fork 2
/
mfcc_impl01.cpp
1572 lines (1179 loc) · 54.6 KB
/
mfcc_impl01.cpp
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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <jni.h>
#include <string>
#include <atomic>
#include <istream>
#include <ostream>
#include <math.h>
#include <sys/time.h>
#include <cpu-features.h>
#include <complex>
#include "logging_macros.h"
#if defined(HAVE_NEON) && defined(HAVE_NEON_X86)
/*
* The latest version and instruction for NEON_2_SSE.h is at:
* https://github.com/intel/ARM_NEON_2_x86_SSE
*/
#include "NEON_2_SSE.h"
#elif defined(HAVE_NEON)
#include <arm_neon.h>
#endif
//#define EXPERIMENT_NEON
#define EXPERIMENT_NEON
///////////////////////////////////////
/////// DEBUGGING TOOLS BEGIN /////////
///////////////////////////////////////
static int log_counter = -1;
static char log_samples[ 4096 ];
static void reset_log_samples()
{
log_samples[ 0 ] = '\0';
}
static void add_log_sample( const float& v )
{
char cur_str[ 256 ];
sprintf( cur_str, " %.2lf", v );
strcat( log_samples, cur_str );
}
static void print_part(
const int part_num,
const float* const arr,
const int len
) {
reset_log_samples();
for ( int i = 0; i < len / 4; i++ ) {
add_log_sample( arr[ i ] );
}
LOGI( "Part %d: [%s]", part_num, log_samples );
}
static void print_points( const char* message, const float* arr, int len )
{
if ( len > 64 ) {
const int part_len = len / 4;
LOGI( "%s", message );
print_part( 1, arr, part_len );
print_part( 2, &(arr[part_len ]), part_len );
print_part( 3, &(arr[part_len*2]), part_len );
print_part( 4, &(arr[part_len*3]), part_len );
}
else {
reset_log_samples();
for (int i = 0; i < len; i++) {
add_log_sample(arr[i]);
}
LOGI("%s [%s]", message, log_samples);
}
}
static double getTimeStampInSeconds() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double) tv.tv_sec + ((double) tv.tv_usec / 1000000);
}
///////////////////////////////////////
/////// DEBUGGING TOOLS END /////////
///////////////////////////////////////
static void check_cpu_feature() {
AndroidCpuFamily family;
uint64_t features;
char buffer[512];
strlcpy(buffer, "", sizeof buffer);
family = android_getCpuFamily();
if ((family != ANDROID_CPU_FAMILY_ARM) &&
(family != ANDROID_CPU_FAMILY_X86)) {
strlcat(buffer, "Not an ARM and not an X86 CPU !\n", sizeof buffer);
}
features = android_getCpuFeatures();
if (((features & ANDROID_CPU_ARM_FEATURE_ARMv7) == 0) &&
((features & ANDROID_CPU_X86_FEATURE_SSSE3) == 0)) {
strlcat(buffer, "Not an ARMv7 and not an X86 SSSE3 CPU !\n", sizeof buffer);
}
if (((features & ANDROID_CPU_ARM_FEATURE_NEON) == 0) &&
((features & ANDROID_CPU_X86_FEATURE_SSSE3) == 0))
{
strlcat(buffer, "CPU doesn't support NEON !\n", sizeof buffer);
}
LOGI("CPUINFO:[%s]", buffer);
}
class HammingWindow {
public:
/** @brief constructor
*
* @param windowSizeSamples : number of samples in one input frame (usually 400)
* @param preEmphTap0 : pre-emphasis coefficient (usually around 0.95)
*/
HammingWindow( const int windowSizeSamples, const float preEmphTap0 )
:mWindowSizeSamples( windowSizeSamples )
,mPreEmphTap0 ( preEmphTap0 )
{
makeHammingWindow();
}
~HammingWindow() {
delete[] mHammingWindow;
}
/** @brief performs conversion on one real-valued frame, and generates windowed frame
*
* @param array_in : input samples (frame) whose length is windowSizeSamples
* @param array_out : output samples (frame) whose length is windowSizeSamples
*/
void preEmphasisHammingAndMakeComplexForFFT_cpp( float* array_in, float* array_out ) {
array_out[0] = 0.0;
for ( int i = 1; i < mWindowSizeSamples; i++ ) {
array_out[ i ] = mHammingWindow[ i ] * ( array_in[ i ] - mPreEmphTap0 * array_in[ i - 1 ] );
}
}
#ifdef HAVE_NEON
void preEmphasisHammingAndMakeComplexForFFT_neon( float* array_in, float* array_out ) {
array_out[0] = 0.0;
const float coeff1 = -1.0*mPreEmphTap0;
for ( int i = 1; i < mWindowSizeSamples; i += 4 ) {
const float32x4_t tap0 = vld1q_f32( &array_in[i ] );
float32x4_t tap1 = vld1q_f32( &array_in[i-1] );
tap1 = vmulq_n_f32( tap1, coeff1 ); // tap1 = tap1 * coeff1
const float32x4_t outvec = vaddq_f32(tap0, tap1); // outvec = tap0 + tap1
vst1q_f32( &(array_out[ i ]), outvec );
}
}
#endif
private:
void makeHammingWindow() {
mHammingWindow = new float[mWindowSizeSamples];
for ( int i = 0; i < mWindowSizeSamples; i++ ) {
mHammingWindow[i] = 0.54 - 0.46 * cos( 2.0 * M_PI * (float)i / (float)(mWindowSizeSamples - 1) );
}
}
const int mWindowSizeSamples;
const float mPreEmphTap0;
float* mHammingWindow;
};
class FFT512 {
public:
FFT512 () {
makeTwiddles();
}
/** @brief main function
*
* @param samples_re : (in) samples real
* @param samples_im : (in) samples imaginary
* @param points_re : (out) points real
* @param points_im : (out) points imaginary
*
* @return : frequency domain points in (re,im) pairs
*/
void transform_cpp( float* samples_re, float* samples_im, float* points_re, float* points_im ) {
memcpy( mArrayIn512re, samples_re, sizeof(float)* 512 );
memcpy( mArrayIn512im, samples_im, sizeof(float)* 512 );
cooley_tukey_fft_512_cpp( 0 );
memcpy( points_re, mArrayOut512re, sizeof(float)* 512 );
memcpy( points_im, mArrayOut512im, sizeof(float)* 512 );
}
#ifdef HAVE_NEON
void transform_neon( float* samples_re, float* samples_im, float* points_re, float* points_im ) {
memcpy( mArrayIn512re, samples_re, sizeof(float)* 512 );
memcpy( mArrayIn512im, samples_im, sizeof(float)* 512 );
cooley_tukey_fft_512_neon( 0 );
memcpy( points_re, mArrayOut512re, sizeof(float)* 512 );
memcpy( points_im, mArrayOut512im, sizeof(float)* 512 );
}
#endif
private:
void makeTwiddles() {
makeTwiddle( mTwiddle512re, mTwiddle512im, 512 );
makeTwiddle( mTwiddle256re, mTwiddle256im, 256 );
makeTwiddle( mTwiddle128re, mTwiddle128im, 128 );
makeTwiddle( mTwiddle64re, mTwiddle64im, 64 );
makeTwiddle( mTwiddle32re, mTwiddle32im, 32 );
makeTwiddle( mTwiddle16re, mTwiddle16im, 16 );
makeTwiddle( mTwiddle8re, mTwiddle8im, 8 );
makeTwiddle( mTwiddle4re, mTwiddle4im, 4 );
makeTwiddle( mTwiddle2re, mTwiddle2im, 2 );
}
void makeTwiddle( float re[], float im[], const int N ) {
const double dN = (double)N;
for ( int k = 0; k < N / 2 ; k++ ) {
const double theta = -2.0 * M_PI * (double)k / dN;
re[ k ] = (float) cos( theta ); // re
im[ k ] = (float) sin( theta ); // im
}
}
float mTwiddle512re[256];
float mTwiddle512im[256];
float mTwiddle256re[128];
float mTwiddle256im[128];
float mTwiddle128re[ 64];
float mTwiddle128im[ 64];
float mTwiddle64re [ 32];
float mTwiddle64im [ 32];
float mTwiddle32re [ 16];
float mTwiddle32im [ 16];
float mTwiddle16re [ 8];
float mTwiddle16im [ 8];
float mTwiddle8re [ 4];
float mTwiddle8im [ 4];
float mTwiddle4re [ 2];
float mTwiddle4im [ 2];
float mTwiddle2re [ 1];
float mTwiddle2im [ 1];
float mArrayIn512re [512];// Input to 512 FFT
float mArrayIn512im [512];
float mArrayIn256re [512];
float mArrayIn256im [512];
float mArrayIn128re [512];
float mArrayIn128im [512];
float mArrayIn64re [512];
float mArrayIn64im [512];
float mArrayIn32re [512];
float mArrayIn32im [512];
float mArrayIn16re [512];
float mArrayIn16im [512];
float mArrayIn8re [512];
float mArrayIn8im [512];
float mArrayIn4re [512];
float mArrayIn4im [512];
float mArrayIn2re [512];
float mArrayIn2im [512];
float mArrayOut512re [512];// Output to 512 FFT
float mArrayOut512im [512];
float mArrayOut256re [512];
float mArrayOut256im [512];
float mArrayOut128re [512];
float mArrayOut128im [512];
float mArrayOut64re [512];
float mArrayOut64im [512];
float mArrayOut32re [512];
float mArrayOut32im [512];
float mArrayOut16re [512];
float mArrayOut16im [512];
float mArrayOut8re [512];
float mArrayOut8im [512];
float mArrayOut4re [512];
float mArrayOut4im [512];
float mArrayOut2re [512];
float mArrayOut2im [512];
inline void cooley_tukey_fft_2( const size_t pos_base ) {
float *const array_in_re = mArrayIn2re;
float *const array_in_im = mArrayIn2im;
float *const array_out_re = mArrayOut2re;
float *const array_out_im = mArrayOut2im;
const float * const twiddle_re = mTwiddle2re;
const float * const twiddle_im = mTwiddle2im;
// Butterfly
const float tw_re = twiddle_re[ 0 ];
const float tw_im = twiddle_im[ 0 ];
const float v1_re = array_in_re [ pos_base ];
const float v1_im = array_in_im [ pos_base ];
const float v2_re = array_in_re [ pos_base + 1 ];
const float v2_im = array_in_im [ pos_base + 1 ];
array_out_re[ pos_base ] = v1_re + v2_re;
array_out_im[ pos_base ] = v1_im + v2_im;
array_out_re[ pos_base + 1 ] = v1_re - v2_re;
array_out_im[ pos_base + 1 ] = v1_im - v2_im;
}
inline void cooley_tukey_fft_4( const size_t pos_base ) {
const size_t half_width = 2;
float *const array_in_re = mArrayIn4re;
float *const array_in_im = mArrayIn4im;
float *const array_in_child_re = mArrayIn2re;
float *const array_in_child_im = mArrayIn2im;
float *const array_out_re = mArrayOut4re;
float *const array_out_im = mArrayOut4im;
float *const array_out_child_re = mArrayOut2re;
float *const array_out_child_im = mArrayOut2im;
const float * const twiddle_re = mTwiddle4re;
const float * const twiddle_im = mTwiddle4im;
// Splitting into even and odd.
array_in_child_re[ pos_base ] = array_in_re[ pos_base ]; // even re
array_in_child_im[ pos_base ] = array_in_im[ pos_base ]; // even re
array_in_child_re[ pos_base + half_width ] = array_in_re[ pos_base + 1 ]; // odd re
array_in_child_im[ pos_base + half_width ] = array_in_im[ pos_base + 1 ]; // odd re
array_in_child_re[ pos_base + 1 ] = array_in_re[ pos_base + 2 ]; // even re
array_in_child_im[ pos_base + 1 ] = array_in_im[ pos_base + 2 ]; // even re
array_in_child_re[ pos_base + half_width + 1 ] = array_in_re[ pos_base + 3 ]; // odd re
array_in_child_im[ pos_base + half_width + 1 ] = array_in_im[ pos_base + 3 ]; // odd re
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_2( pos_base );
cooley_tukey_fft_2( pos_base + half_width );
// Butterfly
{
const float v1_re = array_out_child_re[ pos_base ];
const float v1_im = array_out_child_im[ pos_base ];
const float v2_re = array_out_child_re[ pos_base + half_width ];
const float v2_im = array_out_child_im[ pos_base + half_width ];
array_out_re[ pos_base ] = v1_re + v2_re;
array_out_im[ pos_base ] = v1_im + v2_im;
array_out_re[ pos_base + half_width ] = v1_re - v2_re;
array_out_im[ pos_base + half_width ] = v1_im - v2_im;
}
{
const float v1_re = array_out_child_re[ pos_base + 1 ];
const float v1_im = array_out_child_im[ pos_base + 1 ];
const float v2_re = array_out_child_re[ pos_base + half_width + 1 ];
const float v2_im = array_out_child_im[ pos_base + half_width + 1 ];
array_out_re[ pos_base + 1 ] = v1_re + v2_im;
array_out_im[ pos_base + 1 ] = v1_im - v2_re;
array_out_re[ pos_base + half_width + 1 ] = v1_re - v2_im;
array_out_im[ pos_base + half_width + 1 ] = v1_im + v2_re;
}
}
inline void cooley_tukey_fft_8_cpp( const size_t pos_base ) {
const size_t half_width = 4;
float *const array_in_re = mArrayIn8re;
float *const array_in_im = mArrayIn8im;
float *const array_in_child_re = mArrayIn4re;
float *const array_in_child_im = mArrayIn4im;
float *const array_out_re = mArrayOut8re;
float *const array_out_im = mArrayOut8im;
float *const array_out_child_re = mArrayOut4re;
float *const array_out_child_im = mArrayOut4im;
const float *const twiddle_re = mTwiddle8re;
const float *const twiddle_im = mTwiddle8im;
// Splitting into even and odd.
for (size_t i = 0; i < half_width; i++) {
array_in_child_re[ pos_base + i ] = array_in_re[ pos_base + 2 * i ]; // even re
array_in_child_im[ pos_base + i ] = array_in_im[ pos_base + 2 * i ]; // even re
array_in_child_re[ pos_base + half_width + i ] = array_in_re[ pos_base + 2 * i + 1 ]; // odd re
array_in_child_im[ pos_base + half_width + i ] = array_in_im[ pos_base + 2 * i + 1 ]; // odd re
}
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_4( pos_base );
cooley_tukey_fft_4( pos_base + half_width );
// Butterfly
butterfly_cpp( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#ifdef HAVE_NEON
inline void cooley_tukey_fft_8_neon( const size_t pos_base ) {
const size_t half_width = 4;
float *const array_in_re = mArrayIn8re;
float *const array_in_im = mArrayIn8im;
float *const array_in_child_re = mArrayIn4re;
float *const array_in_child_im = mArrayIn4im;
float *const array_out_re = mArrayOut8re;
float *const array_out_im = mArrayOut8im;
float *const array_out_child_re = mArrayOut4re;
float *const array_out_child_im = mArrayOut4im;
const float *const twiddle_re = mTwiddle8re;
const float *const twiddle_im = mTwiddle8im;
// Splitting into even and odd.
float32x4x2_t interleaved_chunk = vld2q_f32( &(array_in_re[ pos_base ]) );
vst1q_f32( (float32_t *)( &(array_in_child_re[pos_base ]) ), interleaved_chunk.val[0] );
vst1q_f32( (float32_t *)( &(array_in_child_re[pos_base + 4]) ), interleaved_chunk.val[1] );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_4( pos_base );
cooley_tukey_fft_4( pos_base + half_width );
// Butterfly
butterfly_neon( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#endif
inline void cooley_tukey_fft_16_cpp( const size_t pos_base ) {
const size_t half_width = 8;
float *const array_in_re = mArrayIn16re;
float *const array_in_im = mArrayIn16im;
float *const array_in_child_re = mArrayIn8re;
float *const array_in_child_im = mArrayIn8im;
float *const array_out_re = mArrayOut16re;
float *const array_out_im = mArrayOut16im;
float *const array_out_child_re = mArrayOut8re;
float *const array_out_child_im = mArrayOut8im;
const float * const twiddle_re = mTwiddle16re;
const float * const twiddle_im = mTwiddle16im;
// Splitting into even and odd.
deinterleave_cpp(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_cpp(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_8_cpp( pos_base );
cooley_tukey_fft_8_cpp( pos_base + half_width );
// Butterfly
butterfly_cpp( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#ifdef HAVE_NEON
inline void cooley_tukey_fft_16_neon( const size_t pos_base ) {
const size_t half_width = 8;
float *const array_in_re = mArrayIn16re;
float *const array_in_im = mArrayIn16im;
float *const array_in_child_re = mArrayIn8re;
float *const array_in_child_im = mArrayIn8im;
float *const array_out_re = mArrayOut16re;
float *const array_out_im = mArrayOut16im;
float *const array_out_child_re = mArrayOut8re;
float *const array_out_child_im = mArrayOut8im;
const float * const twiddle_re = mTwiddle16re;
const float * const twiddle_im = mTwiddle16im;
// Splitting into even and odd.
deinterleave_neon(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_neon(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_8_neon( pos_base );
cooley_tukey_fft_8_neon( pos_base + half_width );
// Butterfly
butterfly_neon( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#endif
inline void cooley_tukey_fft_32_cpp( const size_t pos_base ) {
const size_t half_width = 16;
float *const array_in_re = mArrayIn32re;
float *const array_in_im = mArrayIn32im;
float *const array_in_child_re = mArrayIn16re;
float *const array_in_child_im = mArrayIn16im;
float *const array_out_re = mArrayOut32re;
float *const array_out_im = mArrayOut32im;
float *const array_out_child_re = mArrayOut16re;
float *const array_out_child_im = mArrayOut16im;
const float * const twiddle_re = mTwiddle32re;
const float * const twiddle_im = mTwiddle32im;
// Splitting into even and odd.
deinterleave_cpp(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_cpp(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_16_cpp( pos_base );
cooley_tukey_fft_16_cpp( pos_base + half_width );
// Butterfly
butterfly_cpp( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#ifdef HAVE_NEON
inline void cooley_tukey_fft_32_neon( const size_t pos_base ) {
const size_t half_width = 16;
float *const array_in_re = mArrayIn32re;
float *const array_in_im = mArrayIn32im;
float *const array_in_child_re = mArrayIn16re;
float *const array_in_child_im = mArrayIn16im;
float *const array_out_re = mArrayOut32re;
float *const array_out_im = mArrayOut32im;
float *const array_out_child_re = mArrayOut16re;
float *const array_out_child_im = mArrayOut16im;
const float * const twiddle_re = mTwiddle32re;
const float * const twiddle_im = mTwiddle32im;
// Splitting into even and odd.
deinterleave_neon(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_neon(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_16_neon( pos_base );
cooley_tukey_fft_16_neon( pos_base + half_width );
// Butterfly
butterfly_neon( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#endif
inline void cooley_tukey_fft_64_cpp( const size_t pos_base ) {
const size_t half_width = 32;
float *const array_in_re = mArrayIn64re;
float *const array_in_im = mArrayIn64im;
float *const array_in_child_re = mArrayIn32re;
float *const array_in_child_im = mArrayIn32im;
float *const array_out_re = mArrayOut64re;
float *const array_out_im = mArrayOut64im;
float *const array_out_child_re = mArrayOut32re;
float *const array_out_child_im = mArrayOut32im;
const float * const twiddle_re = mTwiddle64re;
const float * const twiddle_im = mTwiddle64im;
// Splitting into even and odd.
deinterleave_cpp(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_cpp(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_32_cpp( pos_base );
cooley_tukey_fft_32_cpp( pos_base + half_width );
// Butterfly
butterfly_cpp( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#ifdef HAVE_NEON
inline void cooley_tukey_fft_64_neon( const size_t pos_base ) {
const size_t half_width = 32;
float *const array_in_re = mArrayIn64re;
float *const array_in_im = mArrayIn64im;
float *const array_in_child_re = mArrayIn32re;
float *const array_in_child_im = mArrayIn32im;
float *const array_out_re = mArrayOut64re;
float *const array_out_im = mArrayOut64im;
float *const array_out_child_re = mArrayOut32re;
float *const array_out_child_im = mArrayOut32im;
const float * const twiddle_re = mTwiddle64re;
const float * const twiddle_im = mTwiddle64im;
// Splitting into even and odd.
deinterleave_neon(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_neon(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_32_neon( pos_base );
cooley_tukey_fft_32_neon( pos_base + half_width );
// Butterfly
butterfly_neon( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#endif
inline void cooley_tukey_fft_128_cpp( const size_t pos_base ) {
const size_t half_width = 64;
float *const array_in_re = mArrayIn128re;
float *const array_in_im = mArrayIn128im;
float *const array_in_child_re = mArrayIn64re;
float *const array_in_child_im = mArrayIn64im;
float *const array_out_re = mArrayOut128re;
float *const array_out_im = mArrayOut128im;
float *const array_out_child_re = mArrayOut64re;
float *const array_out_child_im = mArrayOut64im;
const float * const twiddle_re = mTwiddle128re;
const float * const twiddle_im = mTwiddle128im;
// Splitting into even and odd.
deinterleave_cpp(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_cpp(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_64_cpp( pos_base );
cooley_tukey_fft_64_cpp( pos_base + half_width );
// Butterfly
butterfly_cpp( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#ifdef HAVE_NEON
inline void cooley_tukey_fft_128_neon( const size_t pos_base ) {
const size_t half_width = 64;
float *const array_in_re = mArrayIn128re;
float *const array_in_im = mArrayIn128im;
float *const array_in_child_re = mArrayIn64re;
float *const array_in_child_im = mArrayIn64im;
float *const array_out_re = mArrayOut128re;
float *const array_out_im = mArrayOut128im;
float *const array_out_child_re = mArrayOut64re;
float *const array_out_child_im = mArrayOut64im;
const float * const twiddle_re = mTwiddle128re;
const float * const twiddle_im = mTwiddle128im;
// Splitting into even and odd.
deinterleave_neon(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_neon(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_64_neon(pos_base);
cooley_tukey_fft_64_neon(pos_base + half_width);
// Butterfly
butterfly_neon( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#endif
inline void cooley_tukey_fft_256_cpp( const size_t pos_base ) {
const size_t half_width = 128;
float *const array_in_re = mArrayIn256re;
float *const array_in_im = mArrayIn256im;
float *const array_in_child_re = mArrayIn128re;
float *const array_in_child_im = mArrayIn128im;
float *const array_out_re = mArrayOut256re;
float *const array_out_im = mArrayOut256im;
float *const array_out_child_re = mArrayOut128re;
float *const array_out_child_im = mArrayOut128im;
const float * const twiddle_re = mTwiddle256re;
const float * const twiddle_im = mTwiddle256im;
// Splitting into even and odd.
deinterleave_cpp(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_cpp(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_128_cpp( pos_base );
cooley_tukey_fft_128_cpp( pos_base + half_width );
// Butterfly
butterfly_cpp( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#ifdef HAVE_NEON
inline void cooley_tukey_fft_256_neon( const size_t pos_base ) {
const size_t half_width = 128;
float *const array_in_re = mArrayIn256re;
float *const array_in_im = mArrayIn256im;
float *const array_in_child_re = mArrayIn128re;
float *const array_in_child_im = mArrayIn128im;
float *const array_out_re = mArrayOut256re;
float *const array_out_im = mArrayOut256im;
float *const array_out_child_re = mArrayOut128re;
float *const array_out_child_im = mArrayOut128im;
const float * const twiddle_re = mTwiddle256re;
const float * const twiddle_im = mTwiddle256im;
// Splitting into even and odd.
deinterleave_neon(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_neon(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_128_neon( pos_base );
cooley_tukey_fft_128_neon( pos_base + half_width );
// Butterfly
butterfly_neon( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#endif
inline void cooley_tukey_fft_512_cpp( const size_t pos_base ) {
const size_t half_width = 256;
float * const array_in_re = mArrayIn512re;
float * const array_in_im = mArrayIn512im;
float * const array_in_child_re = mArrayIn256re;
float * const array_in_child_im = mArrayIn256im;
float * const array_out_re = mArrayOut512re;
float * const array_out_im = mArrayOut512im;
float * const array_out_child_re = mArrayOut256re;
float * const array_out_child_im = mArrayOut256im;
const float * const twiddle_re = mTwiddle512re;
const float * const twiddle_im = mTwiddle512im;
// Splitting into even and odd.
deinterleave_cpp(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_cpp(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_256_cpp( pos_base );
cooley_tukey_fft_256_cpp( pos_base + half_width );
// Butterfly
butterfly_cpp( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#ifdef HAVE_NEON
inline void cooley_tukey_fft_512_neon( const size_t pos_base ) {
const size_t half_width = 256;
float * const array_in_re = mArrayIn512re;
float * const array_in_im = mArrayIn512im;
float * const array_in_child_re = mArrayIn256re;
float * const array_in_child_im = mArrayIn256im;
float * const array_out_re = mArrayOut512re;
float * const array_out_im = mArrayOut512im;
float * const array_out_child_re = mArrayOut256re;
float * const array_out_child_im = mArrayOut256im;
const float * const twiddle_re = mTwiddle512re;
const float * const twiddle_im = mTwiddle512im;
// Splitting into even and odd.
deinterleave_neon(
&( array_in_re [ pos_base ] ),
half_width * 2,
&( array_in_child_re[ pos_base ] ),
&( array_in_child_re[ pos_base + half_width ] ) );
deinterleave_neon(
&( array_in_im [ pos_base ] ),
half_width * 2,
&( array_in_child_im[ pos_base ] ),
&( array_in_child_im[ pos_base + half_width ] ) );
// Recursive FFT expected to be expanded here.
cooley_tukey_fft_256_neon( pos_base );
cooley_tukey_fft_256_neon( pos_base + half_width );
// Butterfly
butterfly_neon( twiddle_re, twiddle_im, array_out_child_re, array_out_child_im, array_out_re, array_out_im, pos_base, half_width );
}
#endif
inline void butterfly_cpp(
const float * const twiddle_re,
const float * const twiddle_im,
float * const array_in_re,
float * const array_in_im,
float * const array_out_re,
float * const array_out_im,
const int pos_base,
const int half_width
) {
for ( size_t i = 0; i < half_width; i++ ) {
const float tw_re = twiddle_re[ i ];
const float tw_im = twiddle_im[ i ];
const float v1_re = array_in_re[ pos_base + i ];
const float v1_im = array_in_im[ pos_base + i ];
const float v2_re = array_in_re[ pos_base + half_width + i ];
const float v2_im = array_in_im[ pos_base + half_width + i ];
const float offset_re = tw_re * v2_re - tw_im * v2_im;
const float offset_im = tw_re * v2_im + tw_im * v2_re;
array_out_re[ pos_base + i ] = v1_re + offset_re;
array_out_im[ pos_base + i ] = v1_im + offset_im;
array_out_re[ pos_base + half_width + i ] = v1_re - offset_re;
array_out_im[ pos_base + half_width + i ] = v1_im - offset_im;
}
}
#ifdef HAVE_NEON
inline void butterfly_neon(
const float * const twiddle_re,
const float * const twiddle_im,
float * const array_in_re,
float * const array_in_im,
float * const array_out_re,
float * const array_out_im,
const int pos_base,
const int half_width
) {
for ( size_t i = 0; i < half_width; i+=4 ) {
const float32x4_t tw_re = vld1q_f32( &( twiddle_re[i] ) );
const float32x4_t tw_im = vld1q_f32( &( twiddle_im[i] ) );
const float32x4_t v1_re_pre = vld1q_f32( &( array_in_re[pos_base + i] ) );
const float32x4_t v1_im_pre = vld1q_f32( &( array_in_im[pos_base + i] ) );
const float32x4_t v2_re_pre = vld1q_f32( &( array_in_re[pos_base + half_width + i] ) );
const float32x4_t v2_im_pre = vld1q_f32( &( array_in_im[pos_base + half_width + i] ) );