/
FAudio_internal_simd.c
1616 lines (1438 loc) · 46.1 KB
/
FAudio_internal_simd.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
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
/* FAudio - XAudio Reimplementation for FNA
*
* Copyright (c) 2011-2024 Ethan Lee, Luigi Auriemma, and the MonoGame Team
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in a
* product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
* Ethan "flibitijibibo" Lee <flibitijibibo@flibitijibibo.com>
*
*/
#include "FAudio_internal.h"
/* SECTION 0: SSE/NEON Detection */
/* The SSE/NEON detection comes from MojoAL:
* https://hg.icculus.org/icculus/mojoAL/file/default/mojoal.c
*/
#if defined(__aarch64__) || defined(_M_ARM64) || defined(__arm64ec__) || defined(_M_ARM64EC)
/* Some platforms fail to define this... */
#ifndef __ARM_NEON__
#define __ARM_NEON__ 1
#endif
/* AArch64 guarantees NEON. */
#define NEED_SCALAR_CONVERTER_FALLBACKS 0
#elif defined(__x86_64__) || defined(_M_X64)
/* Some platforms fail to define this... */
#ifndef __SSE2__
#define __SSE2__ 1
#endif
/* x86_64 guarantees SSE2. */
#define NEED_SCALAR_CONVERTER_FALLBACKS 0
#elif __MACOSX__ && !defined(__POWERPC__)
/* Some build systems may need to specify this. */
#if !defined(__SSE2__) && !defined(__ARM_NEON__)
#error macOS does not have SSE2/NEON? Bad compiler?
#endif
/* Mac OS X/Intel guarantees SSE2. */
#define NEED_SCALAR_CONVERTER_FALLBACKS 0
#else
/* Need plain C implementations to support all other hardware */
#define NEED_SCALAR_CONVERTER_FALLBACKS 1
#endif
/* Our NEON paths require AArch64, don't check __ARM_NEON__ here */
#if defined(__aarch64__) || defined(_M_ARM64) || defined(__arm64ec__) || defined(_M_ARM64EC)
#include <arm_neon.h>
#define HAVE_NEON_INTRINSICS 1
#endif
#ifdef __SSE2__
#include <emmintrin.h>
#define HAVE_SSE2_INTRINSICS 1
#endif
/* SECTION 1: Type Converters */
/* The SSE/NEON converters are based on SDL_audiotypecvt:
* https://hg.libsdl.org/SDL/file/default/src/audio/SDL_audiotypecvt.c
*/
#define DIVBY128 0.0078125f
#define DIVBY32768 0.000030517578125f
#define DIVBY8388607 0.00000011920930376163766f
#if NEED_SCALAR_CONVERTER_FALLBACKS
void FAudio_INTERNAL_Convert_U8_To_F32_Scalar(
const uint8_t *restrict src,
float *restrict dst,
uint32_t len
) {
uint32_t i;
for (i = 0; i < len; i += 1)
{
*dst++ = (*src++ * DIVBY128) - 1.0f;
}
}
void FAudio_INTERNAL_Convert_S16_To_F32_Scalar(
const int16_t *restrict src,
float *restrict dst,
uint32_t len
) {
uint32_t i;
for (i = 0; i < len; i += 1)
{
*dst++ = *src++ * DIVBY32768;
}
}
void FAudio_INTERNAL_Convert_S32_To_F32_Scalar(
const int32_t *restrict src,
float *restrict dst,
uint32_t len
) {
uint32_t i;
for (i = 0; i < len; i += 1)
{
*dst++ = (*src++ >> 8) * DIVBY8388607;
}
}
#endif /* NEED_SCALAR_CONVERTER_FALLBACKS */
#if HAVE_SSE2_INTRINSICS
void FAudio_INTERNAL_Convert_U8_To_F32_SSE2(
const uint8_t *restrict src,
float *restrict dst,
uint32_t len
) {
int i;
src += len - 1;
dst += len - 1;
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = len; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
*dst = (((float) *src) * DIVBY128) - 1.0f;
}
src -= 15; dst -= 15; /* adjust to read SSE blocks from the start. */
FAudio_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
const __m128i *mmsrc = (const __m128i *) src;
const __m128i zero = _mm_setzero_si128();
const __m128 divby128 = _mm_set1_ps(DIVBY128);
const __m128 minus1 = _mm_set1_ps(-1.0f);
while (i >= 16) { /* 16 * 8-bit */
const __m128i bytes = _mm_load_si128(mmsrc); /* get 16 uint8 into an XMM register. */
/* treat as int16, shift left to clear every other sint16, then back right with zero-extend. Now uint16. */
const __m128i shorts1 = _mm_srli_epi16(_mm_slli_epi16(bytes, 8), 8);
/* right-shift-zero-extend gets us uint16 with the other set of values. */
const __m128i shorts2 = _mm_srli_epi16(bytes, 8);
/* unpack against zero to make these int32, convert to float, multiply, add. Whew! */
/* Note that AVX2 can do floating point multiply+add in one instruction, fwiw. SSE2 cannot. */
const __m128 floats1 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(shorts1, zero)), divby128), minus1);
const __m128 floats2 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(shorts2, zero)), divby128), minus1);
const __m128 floats3 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(shorts1, zero)), divby128), minus1);
const __m128 floats4 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(shorts2, zero)), divby128), minus1);
/* Interleave back into correct order, store. */
_mm_store_ps(dst, _mm_unpacklo_ps(floats1, floats2));
_mm_store_ps(dst+4, _mm_unpackhi_ps(floats1, floats2));
_mm_store_ps(dst+8, _mm_unpacklo_ps(floats3, floats4));
_mm_store_ps(dst+12, _mm_unpackhi_ps(floats3, floats4));
i -= 16; mmsrc--; dst -= 16;
}
src = (const uint8_t *) mmsrc;
}
src += 15; dst += 15; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
*dst = (((float) *src) * DIVBY128) - 1.0f;
i--; src--; dst--;
}
}
void FAudio_INTERNAL_Convert_S16_To_F32_SSE2(
const int16_t *restrict src,
float *restrict dst,
uint32_t len
) {
int i;
src += len - 1;
dst += len - 1;
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = len; i && (((size_t) (dst-7)) & 15); --i, --src, --dst) {
*dst = ((float) *src) * DIVBY32768;
}
src -= 7; dst -= 7; /* adjust to read SSE blocks from the start. */
FAudio_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
const __m128 divby32768 = _mm_set1_ps(DIVBY32768);
while (i >= 8) { /* 8 * 16-bit */
const __m128i ints = _mm_load_si128((__m128i const *) src); /* get 8 sint16 into an XMM register. */
/* treat as int32, shift left to clear every other sint16, then back right with sign-extend. Now sint32. */
const __m128i a = _mm_srai_epi32(_mm_slli_epi32(ints, 16), 16);
/* right-shift-sign-extend gets us sint32 with the other set of values. */
const __m128i b = _mm_srai_epi32(ints, 16);
/* Interleave these back into the right order, convert to float, multiply, store. */
_mm_store_ps(dst, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi32(a, b)), divby32768));
_mm_store_ps(dst+4, _mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi32(a, b)), divby32768));
i -= 8; src -= 8; dst -= 8;
}
}
src += 7; dst += 7; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
*dst = ((float) *src) * DIVBY32768;
i--; src--; dst--;
}
}
void FAudio_INTERNAL_Convert_S32_To_F32_SSE2(
const int32_t *restrict src,
float *restrict dst,
uint32_t len
) {
int i;
/* Get dst aligned to 16 bytes */
for (i = len; i && (((size_t) dst) & 15); --i, ++src, ++dst) {
*dst = ((float) (*src>>8)) * DIVBY8388607;
}
FAudio_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do SSE blocks as long as we have 16 bytes available. */
const __m128 divby8388607 = _mm_set1_ps(DIVBY8388607);
const __m128i *mmsrc = (const __m128i *) src;
while (i >= 4) { /* 4 * sint32 */
/* shift out lowest bits so int fits in a float32. Small precision loss, but much faster. */
_mm_store_ps(dst, _mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_load_si128(mmsrc), 8)), divby8388607));
i -= 4; mmsrc++; dst += 4;
}
src = (const int32_t *) mmsrc;
}
/* Finish off any leftovers with scalar operations. */
while (i) {
*dst = ((float) (*src>>8)) * DIVBY8388607;
i--; src++; dst++;
}
}
#endif /* HAVE_SSE2_INTRINSICS */
#if HAVE_NEON_INTRINSICS
void FAudio_INTERNAL_Convert_U8_To_F32_NEON(
const uint8_t *restrict src,
float *restrict dst,
uint32_t len
) {
int i;
src += len - 1;
dst += len - 1;
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = len; i && (((size_t) (dst-15)) & 15); --i, --src, --dst) {
*dst = (((float) *src) * DIVBY128) - 1.0f;
}
src -= 15; dst -= 15; /* adjust to read NEON blocks from the start. */
FAudio_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do NEON blocks as long as we have 16 bytes available. */
const uint8_t *mmsrc = (const uint8_t *) src;
const float32x4_t divby128 = vdupq_n_f32(DIVBY128);
const float32x4_t negone = vdupq_n_f32(-1.0f);
while (i >= 16) { /* 16 * 8-bit */
const uint8x16_t bytes = vld1q_u8(mmsrc); /* get 16 uint8 into a NEON register. */
const uint16x8_t uint16hi = vmovl_u8(vget_high_u8(bytes)); /* convert top 8 bytes to 8 uint16 */
const uint16x8_t uint16lo = vmovl_u8(vget_low_u8(bytes)); /* convert bottom 8 bytes to 8 uint16 */
/* split uint16 to two uint32, then convert to float, then multiply to normalize, subtract to adjust for sign, store. */
vst1q_f32(dst, vmlaq_f32(negone, vcvtq_f32_u32(vmovl_u16(vget_low_u16(uint16lo))), divby128));
vst1q_f32(dst+4, vmlaq_f32(negone, vcvtq_f32_u32(vmovl_u16(vget_high_u16(uint16lo))), divby128));
vst1q_f32(dst+8, vmlaq_f32(negone, vcvtq_f32_u32(vmovl_u16(vget_low_u16(uint16hi))), divby128));
vst1q_f32(dst+12, vmlaq_f32(negone, vcvtq_f32_u32(vmovl_u16(vget_high_u16(uint16hi))), divby128));
i -= 16; mmsrc -= 16; dst -= 16;
}
src = (const uint8_t *) mmsrc;
}
src += 15; dst += 15; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
*dst = (((float) *src) * DIVBY128) - 1.0f;
i--; src--; dst--;
}
}
void FAudio_INTERNAL_Convert_S16_To_F32_NEON(
const int16_t *restrict src,
float *restrict dst,
uint32_t len
) {
int i;
src += len - 1;
dst += len - 1;
/* Get dst aligned to 16 bytes (since buffer is growing, we don't have to worry about overreading from src) */
for (i = len; i && (((size_t) (dst-7)) & 15); --i, --src, --dst) {
*dst = ((float) *src) * DIVBY32768;
}
src -= 7; dst -= 7; /* adjust to read NEON blocks from the start. */
FAudio_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do NEON blocks as long as we have 16 bytes available. */
const float32x4_t divby32768 = vdupq_n_f32(DIVBY32768);
while (i >= 8) { /* 8 * 16-bit */
const int16x8_t ints = vld1q_s16((int16_t const *) src); /* get 8 sint16 into a NEON register. */
/* split int16 to two int32, then convert to float, then multiply to normalize, store. */
vst1q_f32(dst, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(ints))), divby32768));
vst1q_f32(dst+4, vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(ints))), divby32768));
i -= 8; src -= 8; dst -= 8;
}
}
src += 7; dst += 7; /* adjust for any scalar finishing. */
/* Finish off any leftovers with scalar operations. */
while (i) {
*dst = ((float) *src) * DIVBY32768;
i--; src--; dst--;
}
}
void FAudio_INTERNAL_Convert_S32_To_F32_NEON(
const int32_t *restrict src,
float *restrict dst,
uint32_t len
) {
int i;
/* Get dst aligned to 16 bytes */
for (i = len; i && (((size_t) dst) & 15); --i, ++src, ++dst) {
*dst = ((float) (*src>>8)) * DIVBY8388607;
}
FAudio_assert(!i || ((((size_t) dst) & 15) == 0));
/* Make sure src is aligned too. */
if ((((size_t) src) & 15) == 0) {
/* Aligned! Do NEON blocks as long as we have 16 bytes available. */
const float32x4_t divby8388607 = vdupq_n_f32(DIVBY8388607);
const int32_t *mmsrc = (const int32_t *) src;
while (i >= 4) { /* 4 * sint32 */
/* shift out lowest bits so int fits in a float32. Small precision loss, but much faster. */
vst1q_f32(dst, vmulq_f32(vcvtq_f32_s32(vshrq_n_s32(vld1q_s32(mmsrc), 8)), divby8388607));
i -= 4; mmsrc += 4; dst += 4;
}
src = (const int32_t *) mmsrc;
}
/* Finish off any leftovers with scalar operations. */
while (i) {
*dst = ((float) (*src>>8)) * DIVBY8388607;
i--; src++; dst++;
}
}
#endif /* HAVE_NEON_INTRINSICS */
/* SECTION 2: Linear Resamplers */
void FAudio_INTERNAL_ResampleGeneric(
float *restrict dCache,
float *restrict resampleCache,
uint64_t *resampleOffset,
uint64_t resampleStep,
uint64_t toResample,
uint8_t channels
) {
uint32_t i, j;
uint64_t cur = *resampleOffset & FIXED_FRACTION_MASK;
for (i = 0; i < toResample; i += 1)
{
for (j = 0; j < channels; j += 1)
{
/* lerp, then convert to float value */
*resampleCache++ = (float) (
dCache[j] +
(dCache[j + channels] - dCache[j]) *
FIXED_TO_DOUBLE(cur)
);
}
/* Increment fraction offset by the stepping value */
*resampleOffset += resampleStep;
cur += resampleStep;
/* Only increment the sample offset by integer values.
* Sometimes this will be 0 until cur accumulates
* enough steps, especially for "slow" rates.
*/
dCache += (cur >> FIXED_PRECISION) * channels;
/* Now that any integer has been added, drop it.
* The offset pointer will preserve the total.
*/
cur &= FIXED_FRACTION_MASK;
}
}
#if NEED_SCALAR_CONVERTER_FALLBACKS
void FAudio_INTERNAL_ResampleMono_Scalar(
float *restrict dCache,
float *restrict resampleCache,
uint64_t *resampleOffset,
uint64_t resampleStep,
uint64_t toResample,
uint8_t UNUSED
) {
uint32_t i;
uint64_t cur = *resampleOffset & FIXED_FRACTION_MASK;
for (i = 0; i < toResample; i += 1)
{
/* lerp, then convert to float value */
*resampleCache++ = (float) (
dCache[0] +
(dCache[1] - dCache[0]) *
FIXED_TO_DOUBLE(cur)
);
/* Increment fraction offset by the stepping value */
*resampleOffset += resampleStep;
cur += resampleStep;
/* Only increment the sample offset by integer values.
* Sometimes this will be 0 until cur accumulates
* enough steps, especially for "slow" rates.
*/
dCache += (cur >> FIXED_PRECISION);
/* Now that any integer has been added, drop it.
* The offset pointer will preserve the total.
*/
cur &= FIXED_FRACTION_MASK;
}
}
void FAudio_INTERNAL_ResampleStereo_Scalar(
float *restrict dCache,
float *restrict resampleCache,
uint64_t *resampleOffset,
uint64_t resampleStep,
uint64_t toResample,
uint8_t UNUSED
) {
uint32_t i;
uint64_t cur = *resampleOffset & FIXED_FRACTION_MASK;
for (i = 0; i < toResample; i += 1)
{
/* lerp, then convert to float value */
*resampleCache++ = (float) (
dCache[0] +
(dCache[2] - dCache[0]) *
FIXED_TO_DOUBLE(cur)
);
*resampleCache++ = (float) (
dCache[1] +
(dCache[3] - dCache[1]) *
FIXED_TO_DOUBLE(cur)
);
/* Increment fraction offset by the stepping value */
*resampleOffset += resampleStep;
cur += resampleStep;
/* Only increment the sample offset by integer values.
* Sometimes this will be 0 until cur accumulates
* enough steps, especially for "slow" rates.
*/
dCache += (cur >> FIXED_PRECISION) * 2;
/* Now that any integer has been added, drop it.
* The offset pointer will preserve the total.
*/
cur &= FIXED_FRACTION_MASK;
}
}
#endif /* NEED_SCALAR_CONVERTER_FALLBACKS */
/* The SSE2 versions of the resamplers come from @8thMage! */
#if HAVE_SSE2_INTRINSICS
void FAudio_INTERNAL_ResampleMono_SSE2(
float *restrict dCache,
float *restrict resampleCache,
uint64_t *resampleOffset,
uint64_t resampleStep,
uint64_t toResample,
uint8_t UNUSED
) {
uint32_t i, header, tail;
uint64_t cur_scalar_1, cur_scalar_2, cur_scalar_3;
float *dCache_1, *dCache_2, *dCache_3;
uint64_t cur_scalar = *resampleOffset & FIXED_FRACTION_MASK;
__m128 one_over_fixed_one, half, current_next_0_1, current_next_2_3,
current, next, sub, cur_fixed, mul, res;
__m128i cur_frac, adder_frac, adder_frac_loop;
/* This is the header, the Dest needs to be aligned to 16B */
header = (16 - ((size_t) resampleCache) % 16) / 4;
if (header == 4)
{
header = 0;
}
for (i = 0; i < header; i += 1)
{
/* lerp, then convert to float value */
*resampleCache++ = (float) (
dCache[0] +
(dCache[1] - dCache[0]) *
FIXED_TO_FLOAT(cur_scalar)
);
/* Increment fraction offset by the stepping value */
*resampleOffset += resampleStep;
cur_scalar += resampleStep;
/* Only increment the sample offset by integer values.
* Sometimes this will be 0 until cur accumulates
* enough steps, especially for "slow" rates.
*/
dCache += (cur_scalar >> FIXED_PRECISION);
/* Now that any integer has been added, drop it.
* The offset pointer will preserve the total.
*/
cur_scalar &= FIXED_FRACTION_MASK;
}
toResample -= header;
/* initialising the varius cur
* cur_frac is the fractional part of cur with 4 samples. as the
* fractional part is 32 bit unsigned value, it can be just added
* and the modulu operation for keeping the fractional part will be implicit.
* the 0.5 is for converting signed values to float (no unsigned convert),
* the 0.5 is added later.
*/
cur_frac = _mm_set1_epi32(
(uint32_t) (cur_scalar & FIXED_FRACTION_MASK) - DOUBLE_TO_FIXED(0.5)
);
adder_frac = _mm_setr_epi32(
0,
(uint32_t) (resampleStep & FIXED_FRACTION_MASK),
(uint32_t) ((resampleStep * 2) & FIXED_FRACTION_MASK),
(uint32_t) ((resampleStep * 3) & FIXED_FRACTION_MASK)
);
cur_frac = _mm_add_epi32(cur_frac, adder_frac);
/* The various cur_scalar is for the different samples
* (1, 2, 3 compared to original cur_scalar = 0)
*/
cur_scalar_1 = cur_scalar + resampleStep;
cur_scalar_2 = cur_scalar + resampleStep * 2;
cur_scalar_3 = cur_scalar + resampleStep * 3;
dCache_1 = dCache + (cur_scalar_1 >> FIXED_PRECISION);
dCache_2 = dCache + (cur_scalar_2 >> FIXED_PRECISION);
dCache_3 = dCache + (cur_scalar_3 >> FIXED_PRECISION);
cur_scalar &= FIXED_FRACTION_MASK;
cur_scalar_1 &= FIXED_FRACTION_MASK;
cur_scalar_2 &= FIXED_FRACTION_MASK;
cur_scalar_3 &= FIXED_FRACTION_MASK;
/* FIXME: These should be _mm_undefined_ps! */
current_next_0_1 = _mm_setzero_ps();
current_next_2_3 = _mm_setzero_ps();
/* Constants */
one_over_fixed_one = _mm_set1_ps(1.0f / FIXED_ONE);
half = _mm_set1_ps(0.5f);
adder_frac_loop = _mm_set1_epi32(
(uint32_t) ((resampleStep * 4) & FIXED_FRACTION_MASK)
);
tail = toResample % 4;
for (i = 0; i < toResample - tail; i += 4, resampleCache += 4)
{
/* current next holds 2 pairs of the sample and the sample + 1
* after that need to seperate them.
*/
current_next_0_1 = _mm_loadl_pi(current_next_0_1, (__m64*) dCache);
current_next_0_1 = _mm_loadh_pi(current_next_0_1, (__m64*) dCache_1);
current_next_2_3 = _mm_loadl_pi(current_next_2_3, (__m64*) dCache_2);
current_next_2_3 = _mm_loadh_pi(current_next_2_3, (__m64*) dCache_3);
/* Unpack them to have seperate current and next in 2 vectors. */
current = _mm_shuffle_ps(current_next_0_1, current_next_2_3, 0x88); /* 0b1000 */
next = _mm_shuffle_ps(current_next_0_1, current_next_2_3, 0xdd); /* 0b1101 */
sub = _mm_sub_ps(next, current);
/* Convert the fractional part to float and then mul to get the fractions out.
* then add back the 0.5 we subtracted before.
*/
cur_fixed = _mm_add_ps(
_mm_mul_ps(
_mm_cvtepi32_ps(cur_frac),
one_over_fixed_one
),
half
);
mul = _mm_mul_ps(sub, cur_fixed);
res = _mm_add_ps(current, mul);
/* Store back */
_mm_store_ps(resampleCache, res);
/* Update dCaches for next iteration */
cur_scalar += resampleStep * 4;
cur_scalar_1 += resampleStep * 4;
cur_scalar_2 += resampleStep * 4;
cur_scalar_3 += resampleStep * 4;
dCache = dCache + (cur_scalar >> FIXED_PRECISION);
dCache_1 = dCache_1 + (cur_scalar_1 >> FIXED_PRECISION);
dCache_2 = dCache_2 + (cur_scalar_2 >> FIXED_PRECISION);
dCache_3 = dCache_3 + (cur_scalar_3 >> FIXED_PRECISION);
cur_scalar &= FIXED_FRACTION_MASK;
cur_scalar_1 &= FIXED_FRACTION_MASK;
cur_scalar_2 &= FIXED_FRACTION_MASK;
cur_scalar_3 &= FIXED_FRACTION_MASK;
cur_frac = _mm_add_epi32(cur_frac, adder_frac_loop);
}
*resampleOffset += resampleStep * (toResample - tail);
/* This is the tail. */
for (i = 0; i < tail; i += 1)
{
/* lerp, then convert to float value */
*resampleCache++ = (float) (
dCache[0] +
(dCache[1] - dCache[0]) *
FIXED_TO_FLOAT(cur_scalar)
);
/* Increment fraction offset by the stepping value */
*resampleOffset += resampleStep;
cur_scalar += resampleStep;
/* Only increment the sample offset by integer values.
* Sometimes this will be 0 until cur accumulates
* enough steps, especially for "slow" rates.
*/
dCache += (cur_scalar >> FIXED_PRECISION);
/* Now that any integer has been added, drop it.
* The offset pointer will preserve the total.
*/
cur_scalar &= FIXED_FRACTION_MASK;
}
}
void FAudio_INTERNAL_ResampleStereo_SSE2(
float *restrict dCache,
float *restrict resampleCache,
uint64_t *resampleOffset,
uint64_t resampleStep,
uint64_t toResample,
uint8_t UNUSED
) {
uint32_t i, header, tail;
uint64_t cur_scalar, cur_scalar_1;
float *dCache_1;
__m128 one_over_fixed_one, half, current_next_1, current_next_2,
current, next, sub, cur_fixed, mul, res;
__m128i cur_frac, adder_frac, adder_frac_loop;
/* This is the header, the Dest needs to be aligned to 16B */
header = (16 - ((size_t) resampleCache) % 16) / 8;
if (header == 2)
{
header = 0;
}
cur_scalar = *resampleOffset & FIXED_FRACTION_MASK;
for (i = 0; i < header; i += 2)
{
/* lerp, then convert to float value */
*resampleCache++ = (float) (
dCache[0] +
(dCache[2] - dCache[0]) *
FIXED_TO_FLOAT(cur_scalar)
);
*resampleCache++ = (float) (
dCache[1] +
(dCache[3] - dCache[1]) *
FIXED_TO_FLOAT(cur_scalar)
);
/* Increment fraction offset by the stepping value */
*resampleOffset += resampleStep;
cur_scalar += resampleStep;
/* Only increment the sample offset by integer values.
* Sometimes this will be 0 until cur accumulates
* enough steps, especially for "slow" rates.
*/
dCache += (cur_scalar >> FIXED_PRECISION) * 2;
/* Now that any integer has been added, drop it.
* The offset pointer will preserve the total.
*/
cur_scalar &= FIXED_FRACTION_MASK;
}
toResample -= header;
/* initialising the varius cur.
* cur_frac holds the fractional part of cur.
* to avoid duplication please see the mono part for a thorough
* explanation.
*/
cur_frac = _mm_set1_epi32(
(uint32_t) (cur_scalar & FIXED_FRACTION_MASK) - DOUBLE_TO_FIXED(0.5)
);
adder_frac = _mm_setr_epi32(
0,
0,
(uint32_t) (resampleStep & FIXED_FRACTION_MASK),
(uint32_t) (resampleStep & FIXED_FRACTION_MASK)
);
cur_frac = _mm_add_epi32(cur_frac, adder_frac);
/* dCache_1 is the pointer for dcache in the next resample pos. */
cur_scalar_1 = cur_scalar + resampleStep;
dCache_1 = dCache + (cur_scalar_1 >> FIXED_PRECISION) * 2;
cur_scalar_1 &= FIXED_FRACTION_MASK;
one_over_fixed_one = _mm_set1_ps(1.0f / FIXED_ONE);
half = _mm_set1_ps(0.5f);
adder_frac_loop = _mm_set1_epi32(
(uint32_t) ((resampleStep * 2) & FIXED_FRACTION_MASK)
);
tail = toResample % 2;
for (i = 0; i < toResample - tail; i += 2, resampleCache += 4)
{
/* Current_next_1 and current_next_2 each holds 4 src
* sample points for getting 4 dest resample point at the end.
* current_next_1 holds:
* (current_ch_1, current_ch_2, next_ch_1, next_ch_2)
* for the first resample position, while current_next_2 holds
* the same for the 2nd resample position
*/
current_next_1 = _mm_loadu_ps(dCache); /* A1B1A2B2 */
current_next_2 = _mm_loadu_ps(dCache_1); /* A3B3A4B4 */
/* Unpack them to get the current and the next in seperate vectors. */
current = _mm_castpd_ps(
_mm_unpacklo_pd(
_mm_castps_pd(current_next_1),
_mm_castps_pd(current_next_2)
)
);
next = _mm_castpd_ps(
_mm_unpackhi_pd(
_mm_castps_pd(current_next_1),
_mm_castps_pd(current_next_2)
)
);
sub = _mm_sub_ps(next, current);
/* Adding the 0.5 back.
* See mono explanation for more elaborate explanation.
*/
cur_fixed = _mm_add_ps(
_mm_mul_ps(
_mm_cvtepi32_ps(cur_frac),
one_over_fixed_one
),
half
);
mul = _mm_mul_ps(sub, cur_fixed);
res = _mm_add_ps(current, mul);
/* Store the results */
_mm_store_ps(resampleCache, res);
/* Update dCaches for next iteration */
cur_scalar += resampleStep * 2;
cur_scalar_1 += resampleStep * 2;
dCache = dCache + (cur_scalar >> FIXED_PRECISION) * 2;
dCache_1 = dCache_1 + (cur_scalar_1 >> FIXED_PRECISION) * 2;
cur_scalar &= FIXED_FRACTION_MASK;
cur_scalar_1 &= FIXED_FRACTION_MASK;
cur_frac = _mm_add_epi32(cur_frac, adder_frac_loop);
}
*resampleOffset += resampleStep * (toResample - tail);
/* This is the tail. */
for (i = 0; i < tail; i += 1)
{
/* lerp, then convert to float value */
*resampleCache++ = (float) (
dCache[0] +
(dCache[2] - dCache[0]) *
FIXED_TO_FLOAT(cur_scalar)
);
*resampleCache++ = (float) (
dCache[1] +
(dCache[3] - dCache[1]) *
FIXED_TO_FLOAT(cur_scalar)
);
/* Increment fraction offset by the stepping value */
*resampleOffset += resampleStep;
cur_scalar += resampleStep;
/* Only increment the sample offset by integer values.
* Sometimes this will be 0 until cur accumulates
* enough steps, especially for "slow" rates.
*/
dCache += (cur_scalar >> FIXED_PRECISION) * 2;
/* Now that any integer has been added, drop it.
* The offset pointer will preserve the total.
*/
cur_scalar &= FIXED_FRACTION_MASK;
}
}
#endif /* HAVE_SSE2_INTRINSICS */
#if HAVE_NEON_INTRINSICS
void FAudio_INTERNAL_ResampleMono_NEON(
float *restrict dCache,
float *restrict resampleCache,
uint64_t *resampleOffset,
uint64_t resampleStep,
uint64_t toResample,
uint8_t UNUSED
) {
uint32_t i, header, tail;
uint64_t cur_scalar_1, cur_scalar_2, cur_scalar_3;
float *dCache_1, *dCache_2, *dCache_3;
uint64_t cur_scalar = *resampleOffset & FIXED_FRACTION_MASK;
float32x4_t one_over_fixed_one, half, current_next_0_1, current_next_2_3,
current, next, sub, cur_fixed, mul, res;
int32x4_t cur_frac, adder_frac, adder_frac_loop;
/* This is the header, the Dest needs to be aligned to 16B */
header = (16 - ((size_t) resampleCache) % 16) / 4;
if (header == 4)
{
header = 0;
}
for (i = 0; i < header; i += 1)
{
/* lerp, then convert to float value */
*resampleCache++ = (float) (
dCache[0] +
(dCache[1] - dCache[0]) *
FIXED_TO_FLOAT(cur_scalar)
);
/* Increment fraction offset by the stepping value */
*resampleOffset += resampleStep;
cur_scalar += resampleStep;
/* Only increment the sample offset by integer values.
* Sometimes this will be 0 until cur accumulates
* enough steps, especially for "slow" rates.
*/
dCache += (cur_scalar >> FIXED_PRECISION);
/* Now that any integer has been added, drop it.
* The offset pointer will preserve the total.
*/
cur_scalar &= FIXED_FRACTION_MASK;
}
toResample -= header;
/* initialising the varius cur
* cur_frac is the fractional part of cur with 4 samples. as the
* fractional part is 32 bit unsigned value, it can be just added
* and the modulu operation for keeping the fractional part will be implicit.
* the 0.5 is for converting signed values to float (no unsigned convert),
* the 0.5 is added later.
*/
cur_frac = vdupq_n_s32(
(uint32_t) (cur_scalar & FIXED_FRACTION_MASK) - DOUBLE_TO_FIXED(0.5)
);
ALIGN(int32_t, 16) data[4] =
{
0,
(uint32_t) (resampleStep & FIXED_FRACTION_MASK),
(uint32_t) ((resampleStep * 2) & FIXED_FRACTION_MASK),
(uint32_t) ((resampleStep * 3) & FIXED_FRACTION_MASK)
};
adder_frac = vld1q_s32(data);
cur_frac = vaddq_s32(cur_frac, adder_frac);
/* The various cur_scalar is for the different samples
* (1, 2, 3 compared to original cur_scalar = 0)
*/
cur_scalar_1 = cur_scalar + resampleStep;
cur_scalar_2 = cur_scalar + resampleStep * 2;
cur_scalar_3 = cur_scalar + resampleStep * 3;
dCache_1 = dCache + (cur_scalar_1 >> FIXED_PRECISION);
dCache_2 = dCache + (cur_scalar_2 >> FIXED_PRECISION);
dCache_3 = dCache + (cur_scalar_3 >> FIXED_PRECISION);
cur_scalar &= FIXED_FRACTION_MASK;
cur_scalar_1 &= FIXED_FRACTION_MASK;
cur_scalar_2 &= FIXED_FRACTION_MASK;
cur_scalar_3 &= FIXED_FRACTION_MASK;
/* Constants */
one_over_fixed_one = vdupq_n_f32(1.0f / FIXED_ONE);
half = vdupq_n_f32(0.5f);
adder_frac_loop = vdupq_n_s32(
(uint32_t) ((resampleStep * 4) & FIXED_FRACTION_MASK)
);
tail = toResample % 4;
for (i = 0; i < toResample - tail; i += 4, resampleCache += 4)
{
/* current next holds 2 pairs of the sample and the sample + 1
* after that need to separate them.
*/
current_next_0_1 = vcombine_f32(
vld1_f32(dCache),
vld1_f32(dCache_1)
);
current_next_2_3 = vcombine_f32(
vld1_f32(dCache_2),
vld1_f32(dCache_3)
);
/* Unpack them to have seperate current and next in 2 vectors. */
current = vuzp1q_f32(current_next_0_1, current_next_2_3);
next = vuzp2q_f32(current_next_0_1, current_next_2_3);
sub = vsubq_f32(next, current);
/* Convert the fractional part to float and then mul to get the fractions out.
* then add back the 0.5 we subtracted before.
*/
cur_fixed = vaddq_f32(
vmulq_f32(
vcvtq_f32_s32(cur_frac),
one_over_fixed_one
),
half
);
mul = vmulq_f32(sub, cur_fixed);
res = vaddq_f32(current, mul);
/* Store back */
vst1q_f32(resampleCache, res);
/* Update dCaches for next iteration */
cur_scalar += resampleStep * 4;
cur_scalar_1 += resampleStep * 4;
cur_scalar_2 += resampleStep * 4;
cur_scalar_3 += resampleStep * 4;
dCache = dCache + (cur_scalar >> FIXED_PRECISION);
dCache_1 = dCache_1 + (cur_scalar_1 >> FIXED_PRECISION);
dCache_2 = dCache_2 + (cur_scalar_2 >> FIXED_PRECISION);
dCache_3 = dCache_3 + (cur_scalar_3 >> FIXED_PRECISION);
cur_scalar &= FIXED_FRACTION_MASK;
cur_scalar_1 &= FIXED_FRACTION_MASK;
cur_scalar_2 &= FIXED_FRACTION_MASK;
cur_scalar_3 &= FIXED_FRACTION_MASK;
cur_frac = vaddq_s32(cur_frac, adder_frac_loop);
}
*resampleOffset += resampleStep * (toResample - tail);
/* This is the tail. */
for (i = 0; i < tail; i += 1)
{
/* lerp, then convert to float value */
*resampleCache++ = (float) (
dCache[0] +
(dCache[1] - dCache[0]) *
FIXED_TO_FLOAT(cur_scalar)
);