Skip to content
Navigation Menu
{{ message }}
forked from microsoft/DirectXMath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXDSP.h
More file actions
811 lines (729 loc) · 36.5 KB
/
Copy pathXDSP.h
File metadata and controls
811 lines (729 loc) · 36.5 KB
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
//--------------------------------------------------------------------------------------
// File: XDSP.h
//
// DirectXMath based Digital Signal Processing (DSP) functions for audio,
// primarily Fast Fourier Transform (FFT)
//
// All buffer parameters must be 16-byte aligned
//
// All FFT functions support only single-precision floating-point audio
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// http://go.microsoft.com/fwlink/?LinkID=615557
//--------------------------------------------------------------------------------------
#pragma once
#include <assert.h>
#include <directxmath.h>
#pragma warning(push)
#pragma warning(disable : 4005 4668)
#include <stdint.h>
#pragma warning(pop)
#pragma warning(push)
#pragma warning(disable: 4328 4640 6001 6262)
namespace XDSP
{
#if (DIRECTXMATH_VERSION < 305) && !defined(XM_CALLCONV)
#define XM_CALLCONV __fastcall
typedef const DirectX::XMVECTOR& HXMVECTOR;
typedef const DirectX::XMMATRIX& FXMMATRIX;
#endif
typedef DirectX::XMVECTOR XMVECTOR;
typedef DirectX::FXMVECTOR FXMVECTOR;
typedef DirectX::GXMVECTOR GXMVECTOR;
typedef DirectX::CXMVECTOR CXMVECTOR;
inline bool ISPOWEROF2(size_t n) { return ( ((n)&((n)-1)) == 0 && (n) != 0 ); }
// Parallel multiplication of four complex numbers, assuming real and imaginary values are stored in separate vectors.
__forceinline void XM_CALLCONV vmulComplex (_Out_ XMVECTOR& rResult, _Out_ XMVECTOR& iResult,
_In_ FXMVECTOR r1, _In_ FXMVECTOR i1, _In_ FXMVECTOR r2, _In_ GXMVECTOR i2)
{
using namespace DirectX;
// (r1, i1) * (r2, i2) = (r1r2 - i1i2, r1i2 + r2i1)
XMVECTOR vi1i2 = XMVectorMultiply(i1, i2);
XMVECTOR vr1r2 = XMVectorMultiply(r1, r2);
XMVECTOR vr1i2 = XMVectorMultiply(r1, i2);
XMVECTOR vr2i1 = XMVectorMultiply(r2, i1);
rResult = XMVectorSubtract(vr1r2, vi1i2); // real: (r1*r2 - i1*i2)
iResult = XMVectorAdd(vr1i2, vr2i1); // imaginary: (r1*i2 + r2*i1)
}
__forceinline void XM_CALLCONV vmulComplex (_Inout_ XMVECTOR& r1, _Inout_ XMVECTOR& i1, _In_ FXMVECTOR r2, _In_ FXMVECTOR i2)
{
using namespace DirectX;
// (r1, i1) * (r2, i2) = (r1r2 - i1i2, r1i2 + r2i1)
XMVECTOR vi1i2 = XMVectorMultiply(i1, i2);
XMVECTOR vr1r2 = XMVectorMultiply(r1, r2);
XMVECTOR vr1i2 = XMVectorMultiply(r1, i2);
XMVECTOR vr2i1 = XMVectorMultiply(r2, i1);
r1 = XMVectorSubtract(vr1r2, vi1i2); // real: (r1*r2 - i1*i2)
i1 = XMVectorAdd(vr1i2, vr2i1); // imaginary: (r1*i2 + r2*i1)
}
//----------------------------------------------------------------------------------
// Radix-4 decimation-in-time FFT butterfly.
// This version assumes that all four elements of the butterfly are
// adjacent in a single vector.
//
// Compute the product of the complex input vector and the
// 4-element DFT matrix:
// | 1 1 1 1 | | (r1X,i1X) |
// | 1 -j -1 j | | (r1Y,i1Y) |
// | 1 -1 1 -1 | | (r1Z,i1Z) |
// | 1 j -1 -j | | (r1W,i1W) |
//
// This matrix can be decomposed into two simpler ones to reduce the
// number of additions needed. The decomposed matrices look like this:
// | 1 0 1 0 | | 1 0 1 0 |
// | 0 1 0 -j | | 1 0 -1 0 |
// | 1 0 -1 0 | | 0 1 0 1 |
// | 0 1 0 j | | 0 1 0 -1 |
//
// Combine as follows:
// | 1 0 1 0 | | (r1X,i1X) | | (r1X + r1Z, i1X + i1Z) |
// Temp = | 1 0 -1 0 | * | (r1Y,i1Y) | = | (r1X - r1Z, i1X - i1Z) |
// | 0 1 0 1 | | (r1Z,i1Z) | | (r1Y + r1W, i1Y + i1W) |
// | 0 1 0 -1 | | (r1W,i1W) | | (r1Y - r1W, i1Y - i1W) |
//
// | 1 0 1 0 | | (rTempX,iTempX) | | (rTempX + rTempZ, iTempX + iTempZ) |
// Result = | 0 1 0 -j | * | (rTempY,iTempY) | = | (rTempY + iTempW, iTempY - rTempW) |
// | 1 0 -1 0 | | (rTempZ,iTempZ) | | (rTempX - rTempZ, iTempX - iTempZ) |
// | 0 1 0 j | | (rTempW,iTempW) | | (rTempY - iTempW, iTempY + rTempW) |
//----------------------------------------------------------------------------------
__forceinline void ButterflyDIT4_1 (_Inout_ XMVECTOR& r1, _Inout_ XMVECTOR& i1)
{
using namespace DirectX;
// sign constants for radix-4 butterflies
const static XMVECTORF32 vDFT4SignBits1 = { 1.0f, -1.0f, 1.0f, -1.0f };
const static XMVECTORF32 vDFT4SignBits2 = { 1.0f, 1.0f, -1.0f, -1.0f };
const static XMVECTORF32 vDFT4SignBits3 = { 1.0f, -1.0f, -1.0f, 1.0f };
// calculating Temp
// [r1X| r1X|r1Y| r1Y] + [r1Z|-r1Z|r1W|-r1W]
// [i1X| i1X|i1Y| i1Y] + [i1Z|-i1Z|i1W|-i1W]
XMVECTOR r1L = XMVectorSwizzle<0,0,1,1>( r1 );
XMVECTOR r1H = XMVectorSwizzle<2,2,3,3>( r1 );
XMVECTOR i1L = XMVectorSwizzle<0,0,1,1>( i1 );
XMVECTOR i1H = XMVectorSwizzle<2,2,3,3>( i1 );
XMVECTOR rTemp = XMVectorMultiplyAdd( r1H, vDFT4SignBits1, r1L );
XMVECTOR iTemp = XMVectorMultiplyAdd( i1H, vDFT4SignBits1, i1L );
// calculating Result
XMVECTOR rZrWiZiW = XMVectorPermute<2,3,6,7>(rTemp,iTemp); // [rTempZ|rTempW|iTempZ|iTempW]
XMVECTOR rZiWrZiW = XMVectorSwizzle<0,3,0,3>(rZrWiZiW); // [rTempZ|iTempW|rTempZ|iTempW]
XMVECTOR iZrWiZrW = XMVectorSwizzle<2,1,2,1>(rZrWiZiW); // [rTempZ|iTempW|rTempZ|iTempW]
// [rTempX| rTempY| rTempX| rTempY] + [rTempZ| iTempW|-rTempZ|-iTempW]
// [iTempX| iTempY| iTempX| iTempY] + // [iTempZ|-rTempW|-iTempZ| rTempW]
XMVECTOR rTempL = XMVectorSwizzle<0,1,0,1>(rTemp);
XMVECTOR iTempL = XMVectorSwizzle<0,1,0,1>(iTemp);
r1 = XMVectorMultiplyAdd( rZiWrZiW, vDFT4SignBits2, rTempL );
i1 = XMVectorMultiplyAdd( iZrWiZrW, vDFT4SignBits3, iTempL );
}
//----------------------------------------------------------------------------------
// Radix-4 decimation-in-time FFT butterfly.
// This version assumes that elements of the butterfly are
// in different vectors, so that each vector in the input
// contains elements from four different butterflies.
// The four separate butterflies are processed in parallel.
//
// The calculations here are the same as the ones in the single-vector
// radix-4 DFT, but instead of being done on a single vector (X,Y,Z,W)
// they are done in parallel on sixteen independent complex values.
// There is no interdependence between the vector elements:
// | 1 0 1 0 | | (rIn0,iIn0) | | (rIn0 + rIn2, iIn0 + iIn2) |
// | 1 0 -1 0 | * | (rIn1,iIn1) | = Temp = | (rIn0 - rIn2, iIn0 - iIn2) |
// | 0 1 0 1 | | (rIn2,iIn2) | | (rIn1 + rIn3, iIn1 + iIn3) |
// | 0 1 0 -1 | | (rIn3,iIn3) | | (rIn1 - rIn3, iIn1 - iIn3) |
//
// | 1 0 1 0 | | (rTemp0,iTemp0) | | (rTemp0 + rTemp2, iTemp0 + iTemp2) |
// Result = | 0 1 0 -j | * | (rTemp1,iTemp1) | = | (rTemp1 + iTemp3, iTemp1 - rTemp3) |
// | 1 0 -1 0 | | (rTemp2,iTemp2) | | (rTemp0 - rTemp2, iTemp0 - iTemp2) |
// | 0 1 0 j | | (rTemp3,iTemp3) | | (rTemp1 - iTemp3, iTemp1 + rTemp3) |
//----------------------------------------------------------------------------------
__forceinline void ButterflyDIT4_4 (_Inout_ XMVECTOR& r0,
_Inout_ XMVECTOR& r1,
_Inout_ XMVECTOR& r2,
_Inout_ XMVECTOR& r3,
_Inout_ XMVECTOR& i0,
_Inout_ XMVECTOR& i1,
_Inout_ XMVECTOR& i2,
_Inout_ XMVECTOR& i3,
_In_reads_(uStride*4) const XMVECTOR* __restrict pUnityTableReal,
_In_reads_(uStride*4) const XMVECTOR* __restrict pUnityTableImaginary,
_In_ size_t uStride,
_In_ const bool fLast)
{
using namespace DirectX;
assert(pUnityTableReal);
assert(pUnityTableImaginary);
assert((uintptr_t)pUnityTableReal % 16 == 0);
assert((uintptr_t)pUnityTableImaginary % 16 == 0);
assert(ISPOWEROF2(uStride));
// calculating Temp
XMVECTOR rTemp0 = XMVectorAdd(r0, r2);
XMVECTOR iTemp0 = XMVectorAdd(i0, i2);
XMVECTOR rTemp2 = XMVectorAdd(r1, r3);
XMVECTOR iTemp2 = XMVectorAdd(i1, i3);
XMVECTOR rTemp1 = XMVectorSubtract(r0, r2);
XMVECTOR iTemp1 = XMVectorSubtract(i0, i2);
XMVECTOR rTemp3 = XMVectorSubtract(r1, r3);
XMVECTOR iTemp3 = XMVectorSubtract(i1, i3);
XMVECTOR rTemp4 = XMVectorAdd(rTemp0, rTemp2);
XMVECTOR iTemp4 = XMVectorAdd(iTemp0, iTemp2);
XMVECTOR rTemp5 = XMVectorAdd(rTemp1, iTemp3);
XMVECTOR iTemp5 = XMVectorSubtract(iTemp1, rTemp3);
XMVECTOR rTemp6 = XMVectorSubtract(rTemp0, rTemp2);
XMVECTOR iTemp6 = XMVectorSubtract(iTemp0, iTemp2);
XMVECTOR rTemp7 = XMVectorSubtract(rTemp1, iTemp3);
XMVECTOR iTemp7 = XMVectorAdd(iTemp1, rTemp3);
// calculating Result
// vmulComplex(rTemp0, iTemp0, rTemp0, iTemp0, pUnityTableReal[0], pUnityTableImaginary[0]); // first one is always trivial
vmulComplex(rTemp5, iTemp5, pUnityTableReal[uStride], pUnityTableImaginary[uStride]);
vmulComplex(rTemp6, iTemp6, pUnityTableReal[uStride*2], pUnityTableImaginary[uStride*2]);
vmulComplex(rTemp7, iTemp7, pUnityTableReal[uStride*3], pUnityTableImaginary[uStride*3]);
if (fLast)
{
ButterflyDIT4_1(rTemp4, iTemp4);
ButterflyDIT4_1(rTemp5, iTemp5);
ButterflyDIT4_1(rTemp6, iTemp6);
ButterflyDIT4_1(rTemp7, iTemp7);
}
r0 = rTemp4; i0 = iTemp4;
r1 = rTemp5; i1 = iTemp5;
r2 = rTemp6; i2 = iTemp6;
r3 = rTemp7; i3 = iTemp7;
}
//==================================================================================
// F-U-N-C-T-I-O-N-S
//==================================================================================
//----------------------------------------------------------------------------------
// DESCRIPTION:
// 4-sample FFT.
//
// PARAMETERS:
// pReal - [inout] real components, must have at least uCount elements
// pImaginary - [inout] imaginary components, must have at least uCount elements
// uCount - [in] number of FFT iterations
//----------------------------------------------------------------------------------
__forceinline void FFT4(_Inout_updates_(uCount) XMVECTOR* __restrict pReal,
_Inout_updates_(uCount) XMVECTOR* __restrict pImaginary,
_In_ const size_t uCount=1)
{
assert(pReal);
assert(pImaginary);
assert((uintptr_t)pReal % 16 == 0);
assert((uintptr_t)pImaginary % 16 == 0);
assert(ISPOWEROF2(uCount));
for (size_t uIndex=0; uIndex < uCount; ++uIndex)
{
ButterflyDIT4_1(pReal[uIndex], pImaginary[uIndex]);
}
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// 8-sample FFT.
//
// PARAMETERS:
// pReal - [inout] real components, must have at least uCount*2 elements
// pImaginary - [inout] imaginary components, must have at least uCount*2 elements
// uCount - [in] number of FFT iterations
//----------------------------------------------------------------------------------
__forceinline void FFT8 (_Inout_updates_(uCount*2) XMVECTOR* __restrict pReal,
_Inout_updates_(uCount*2) XMVECTOR* __restrict pImaginary,
_In_ const size_t uCount=1)
{
using namespace DirectX;
assert(pReal);
assert(pImaginary);
assert((uintptr_t)pReal % 16 == 0);
assert((uintptr_t)pImaginary % 16 == 0);
assert(ISPOWEROF2(uCount));
static const XMVECTORF32 wr1 = { 1.0f, 0.70710677f, 0.0f, -0.70710677f };
static const XMVECTORF32 wi1 = { 0.0f, -0.70710677f, -1.0f, -0.70710677f };
static const XMVECTORF32 wr2 = { -1.0f, -0.70710677f, 0.0f, 0.70710677f };
static const XMVECTORF32 wi2 = { 0.0f, 0.70710677f, 1.0f, 0.70710677f };
for (size_t uIndex=0; uIndex < uCount; ++uIndex)
{
XMVECTOR* __restrict pR = pReal + uIndex*2;
XMVECTOR* __restrict pI = pImaginary + uIndex*2;
XMVECTOR oddsR = XMVectorPermute<1,3,5,7>(pR[0], pR[1]);
XMVECTOR evensR = XMVectorPermute<0,2,4,6>(pR[0], pR[1]);
XMVECTOR oddsI = XMVectorPermute<1,3,5,7>(pI[0], pI[1]);
XMVECTOR evensI = XMVectorPermute<0,2,4,6>(pI[0], pI[1]);
ButterflyDIT4_1(oddsR, oddsI);
ButterflyDIT4_1(evensR, evensI);
XMVECTOR r, i;
vmulComplex(r, i, oddsR, oddsI, wr1, wi1);
pR[0] = XMVectorAdd(evensR, r);
pI[0] = XMVectorAdd(evensI, i);
vmulComplex(r, i, oddsR, oddsI, wr2, wi2);
pR[1] = XMVectorAdd(evensR, r);
pI[1] = XMVectorAdd(evensI, i);
}
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// 16-sample FFT.
//
// PARAMETERS:
// pReal - [inout] real components, must have at least uCount*4 elements
// pImaginary - [inout] imaginary components, must have at least uCount*4 elements
// uCount - [in] number of FFT iterations
//----------------------------------------------------------------------------------
__forceinline void FFT16 (_Inout_updates_(uCount*4) XMVECTOR* __restrict pReal,
_Inout_updates_(uCount*4) XMVECTOR* __restrict pImaginary,
_In_ const size_t uCount=1)
{
using namespace DirectX;
assert(pReal);
assert(pImaginary);
assert((uintptr_t)pReal % 16 == 0);
assert((uintptr_t)pImaginary % 16 == 0);
assert(ISPOWEROF2(uCount));
static const XMVECTORF32 aUnityTableReal[4] = { { 1.0f, 1.0f, 1.0f, 1.0f },
{ 1.0f, 0.92387950f, 0.70710677f, 0.38268343f },
{ 1.0f, 0.70710677f, -4.3711388e-008f, -0.70710677f },
{ 1.0f, 0.38268343f, -0.70710677f, -0.92387950f } };
static const XMVECTORF32 aUnityTableImaginary[4] = { { -0.0f, -0.0f, -0.0f, -0.0f },
{ -0.0f, -0.38268343f, -0.70710677f, -0.92387950f },
{ -0.0f, -0.70710677f, -1.0f, -0.70710677f },
{ -0.0f, -0.92387950f, -0.70710677f, 0.38268343f } };
for (size_t uIndex=0; uIndex < uCount; ++uIndex)
{
ButterflyDIT4_4(pReal[uIndex*4],
pReal[uIndex*4 + 1],
pReal[uIndex*4 + 2],
pReal[uIndex*4 + 3],
pImaginary[uIndex*4],
pImaginary[uIndex*4 + 1],
pImaginary[uIndex*4 + 2],
pImaginary[uIndex*4 + 3],
reinterpret_cast<const XMVECTOR*>(aUnityTableReal),
reinterpret_cast<const XMVECTOR*>(aUnityTableImaginary),
1, true);
}
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// 2^N-sample FFT.
//
// REMARKS:
// For FFTs length 16 and below, call FFT16(), FFT8(), or FFT4().
//
// PARAMETERS:
// pReal - [inout] real components, must have at least (uLength*uCount)/4 elements
// pImaginary - [inout] imaginary components, must have at least (uLength*uCount)/4 elements
// pUnityTable - [in] unity table, must have at least uLength*uCount elements, see FFTInitializeUnityTable()
// uLength - [in] FFT length in samples, must be a power of 2 > 16
// uCount - [in] number of FFT iterations
//----------------------------------------------------------------------------------
inline void FFT (_Inout_updates_((uLength*uCount)/4) XMVECTOR* __restrict pReal,
_Inout_updates_((uLength*uCount)/4) XMVECTOR* __restrict pImaginary,
_In_reads_(uLength*uCount) const XMVECTOR* __restrict pUnityTable,
_In_ const size_t uLength,
_In_ const size_t uCount=1)
{
assert(pReal);
assert(pImaginary);
assert(pUnityTable);
assert((uintptr_t)pReal % 16 == 0);
assert((uintptr_t)pImaginary % 16 == 0);
assert((uintptr_t)pUnityTable % 16 == 0);
assert(uLength > 16);
_Analysis_assume_(uLength > 16);
assert(ISPOWEROF2(uLength));
assert(ISPOWEROF2(uCount));
const XMVECTOR* __restrict pUnityTableReal = pUnityTable;
const XMVECTOR* __restrict pUnityTableImaginary = pUnityTable + (uLength>>2);
const size_t uTotal = uCount * uLength;
const size_t uTotal_vectors = uTotal >> 2;
const size_t uStage_vectors = uLength >> 2;
const size_t uStage_vectors_mask = uStage_vectors - 1;
const size_t uStride = uLength >> 4; // stride between butterfly elements
const size_t uStrideMask = uStride - 1;
const size_t uStride2 = uStride * 2;
const size_t uStride3 = uStride * 3;
const size_t uStrideInvMask = ~uStrideMask;
for (size_t uIndex=0; uIndex < (uTotal_vectors>>2); ++uIndex)
{
const size_t n = ((uIndex & uStrideInvMask) << 2) + (uIndex & uStrideMask);
ButterflyDIT4_4(pReal[n],
pReal[n + uStride],
pReal[n + uStride2],
pReal[n + uStride3],
pImaginary[n ],
pImaginary[n + uStride],
pImaginary[n + uStride2],
pImaginary[n + uStride3],
pUnityTableReal + (n & uStage_vectors_mask),
pUnityTableImaginary + (n & uStage_vectors_mask),
uStride, false);
}
if (uLength > 16*4)
{
FFT(pReal, pImaginary, pUnityTable+(uLength>>1), uLength>>2, uCount*4);
}
else if (uLength == 16*4)
{
FFT16(pReal, pImaginary, uCount*4);
}
else if (uLength == 8*4)
{
FFT8(pReal, pImaginary, uCount*4);
}
else if (uLength == 4*4)
{
FFT4(pReal, pImaginary, uCount*4);
}
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// Initializes unity roots lookup table used by FFT functions.
// Once initialized, the table need not be initialized again unless a
// different FFT length is desired.
//
// REMARKS:
// The unity tables of FFT length 16 and below are hard coded into the
// respective FFT functions and so need not be initialized.
//
// PARAMETERS:
// pUnityTable - [out] unity table, receives unity roots lookup table, must have at least uLength elements
// uLength - [in] FFT length in frames, must be a power of 2 > 16
//----------------------------------------------------------------------------------
inline void FFTInitializeUnityTable (_Out_writes_(uLength) XMVECTOR* __restrict pUnityTable, _In_ size_t uLength)
{
assert(pUnityTable);
assert(uLength > 16);
_Analysis_assume_(uLength > 16);
assert(ISPOWEROF2(uLength));
float* __restrict pfUnityTable = reinterpret_cast<float* __restrict>(pUnityTable);
// initialize unity table for recursive FFT lengths: uLength, uLength/4, uLength/16... > 16
do
{
float flStep = 6.283185307f / uLength; // 2PI / FFT length
uLength >>= 2;
// pUnityTable[0 to uLength*4-1] contains real components for current FFT length
// pUnityTable[uLength*4 to uLength*8-1] contains imaginary components for current FFT length
for (size_t i=0; i<4; ++i)
{
for (size_t j=0; j<uLength; ++j)
{
size_t uIndex = (i*uLength) + j;
pfUnityTable[uIndex] = cosf(float(i)*float(j)*flStep); // real component
#pragma warning(suppress: 6386)
pfUnityTable[uIndex + uLength*4] = -sinf(float(i)*float(j)*flStep); // imaginary component
}
}
pfUnityTable += uLength*8;
}
while (uLength > 16);
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// The FFT functions generate output in bit reversed order.
// Use this function to re-arrange them into order of increasing frequency.
//
// REMARKS:
//
// PARAMETERS:
// pOutput - [out] output buffer, receives samples in order of increasing frequency, cannot overlap pInput, must have at least (1<<uLog2Length)/4 elements
// pInput - [in] input buffer, samples in bit reversed order as generated by FFT functions, cannot overlap pOutput, must have at least (1<<uLog2Length)/4 elements
// uLog2Length - [in] LOG (base 2) of FFT length in samples, must be >= 2
//----------------------------------------------------------------------------------
inline void FFTUnswizzle (_Out_writes_((1<<uLog2Length)/4) XMVECTOR* __restrict pOutput,
_In_reads_((1<<uLog2Length)/4) const XMVECTOR* __restrict pInput,
_In_ const size_t uLog2Length)
{
assert(pOutput);
assert(pInput);
assert(uLog2Length >= 2);
_Analysis_assume_(uLog2Length >= 2);
float* __restrict pfOutput = (float* __restrict)pOutput;
const float* __restrict pfInput = (const float* __restrict)pInput;
const size_t uLength = size_t(1) << uLog2Length;
if ((uLog2Length & 0x1) == 0)
{
// even powers of two
for (size_t uIndex=0; uIndex < uLength; ++uIndex)
{
size_t n = uIndex;
n = ( (n & 0xcccccccc) >> 2 ) | ( (n & 0x33333333) << 2 );
n = ( (n & 0xf0f0f0f0) >> 4 ) | ( (n & 0x0f0f0f0f) << 4 );
n = ( (n & 0xff00ff00) >> 8 ) | ( (n & 0x00ff00ff) << 8 );
n = ( (n & 0xffff0000) >> 16 ) | ( (n & 0x0000ffff) << 16 );
n >>= (32 - uLog2Length);
pfOutput[n] = pfInput[uIndex];
}
}
else
{
// odd powers of two
for (size_t uIndex=0; uIndex < uLength; ++uIndex)
{
size_t n = (uIndex>>3);
n = ( (n & 0xcccccccc) >> 2 ) | ( (n & 0x33333333) << 2 );
n = ( (n & 0xf0f0f0f0) >> 4 ) | ( (n & 0x0f0f0f0f) << 4 );
n = ( (n & 0xff00ff00) >> 8 ) | ( (n & 0x00ff00ff) << 8 );
n = ( (n & 0xffff0000) >> 16 ) | ( (n & 0x0000ffff) << 16 );
n >>= (32 - (uLog2Length-3));
n |= ((uIndex & 0x7) << (uLog2Length - 3));
pfOutput[n] = pfInput[uIndex];
}
}
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// Convert complex components to polar form.
//
// PARAMETERS:
// pOutput - [out] output buffer, receives samples in polar form, must have at least uLength/4 elements
// pInputReal - [in] input buffer (real components), must have at least uLength/4 elements
// pInputImaginary - [in] input buffer (imaginary components), must have at least uLength/4 elements
// uLength - [in] FFT length in samples, must be a power of 2 >= 4
//----------------------------------------------------------------------------------
#pragma warning(suppress: 6101)
inline void FFTPolar (_Out_writes_(uLength/4) XMVECTOR* __restrict pOutput,
_In_reads_(uLength/4) const XMVECTOR* __restrict pInputReal,
_In_reads_(uLength/4) const XMVECTOR* __restrict pInputImaginary,
_In_ const size_t uLength)
{
using namespace DirectX;
assert(pOutput);
assert(pInputReal);
assert(pInputImaginary);
assert(uLength >= 4);
_Analysis_assume_(uLength >= 4);
assert(ISPOWEROF2(uLength));
float flOneOverLength = 1.0f / uLength;
// result = sqrtf((real/uLength)^2 + (imaginary/uLength)^2) * 2
XMVECTOR vOneOverLength = XMVectorReplicate( flOneOverLength );
for (size_t uIndex=0; uIndex < (uLength>>2); ++uIndex)
{
XMVECTOR vReal = XMVectorMultiply(pInputReal[uIndex], vOneOverLength);
XMVECTOR vImaginary = XMVectorMultiply(pInputImaginary[uIndex], vOneOverLength);
XMVECTOR vRR = XMVectorMultiply(vReal, vReal);
XMVECTOR vII = XMVectorMultiply(vImaginary, vImaginary);
XMVECTOR vRRplusII = XMVectorAdd(vRR, vII);
XMVECTOR vTotal = XMVectorSqrt(vRRplusII);
pOutput[uIndex] = XMVectorAdd(vTotal, vTotal);
}
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// Deinterleaves audio samples
//
// REMARKS:
// For example, audio of the form [LRLRLR] becomes [LLLRRR].
//
// PARAMETERS:
// pOutput - [out] output buffer, receives samples in deinterleaved form, cannot overlap pInput, must have at least (uChannelCount*uFrameCount)/4 elements
// pInput - [in] input buffer, cannot overlap pOutput, must have at least (uChannelCount*uFrameCount)/4 elements
// uChannelCount - [in] number of channels, must be > 1
// uFrameCount - [in] number of frames of valid data, must be > 0
//----------------------------------------------------------------------------------
inline void Deinterleave (_Out_writes_((uChannelCount*uFrameCount)/4) XMVECTOR* __restrict pOutput,
_In_reads_((uChannelCount*uFrameCount)/4) const XMVECTOR* __restrict pInput,
_In_ const size_t uChannelCount,
_In_ const size_t uFrameCount)
{
assert(pOutput);
assert(pInput);
assert(uChannelCount > 1);
assert(uFrameCount > 0);
float* __restrict pfOutput = reinterpret_cast<float* __restrict>(pOutput);
const float* __restrict pfInput = reinterpret_cast<const float* __restrict>(pInput);
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
for (size_t uFrame=0; uFrame < uFrameCount; ++uFrame)
{
pfOutput[uChannel * uFrameCount + uFrame] = pfInput[uFrame * uChannelCount + uChannel];
}
}
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// Interleaves audio samples
//
// REMARKS:
// For example, audio of the form [LLLRRR] becomes [LRLRLR].
//
// PARAMETERS:
// pOutput - [out] output buffer, receives samples in interleaved form, cannot overlap pInput, must have at least (uChannelCount*uFrameCount)/4 elements
// pInput - [in] input buffer, cannot overlap pOutput, must have at least (uChannelCount*uFrameCount)/4 elements
// uChannelCount - [in] number of channels, must be > 1
// uFrameCount - [in] number of frames of valid data, must be > 0
//----------------------------------------------------------------------------------
inline void Interleave (_Out_writes_((uChannelCount*uFrameCount)/4) XMVECTOR* __restrict pOutput,
_In_reads_((uChannelCount*uFrameCount)/4) const XMVECTOR* __restrict pInput,
_In_ const size_t uChannelCount,
_In_ const size_t uFrameCount)
{
assert(pOutput);
assert(pInput);
assert(uChannelCount > 1);
assert(uFrameCount > 0);
float* __restrict pfOutput = reinterpret_cast<float* __restrict>(pOutput);
const float* __restrict pfInput = reinterpret_cast<const float* __restrict>(pInput);
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
for (size_t uFrame=0; uFrame < uFrameCount; ++uFrame)
{
pfOutput[uFrame * uChannelCount + uChannel] = pfInput[uChannel * uFrameCount + uFrame];
}
}
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// This function applies a 2^N-sample FFT and unswizzles the result such
// that the samples are in order of increasing frequency.
// Audio is first deinterleaved if multichannel.
//
// PARAMETERS:
// pReal - [inout] real components, must have at least (1<<uLog2Length*uChannelCount)/4 elements
// pImaginary - [out] imaginary components, must have at least (1<<uLog2Length*uChannelCount)/4 elements
// pUnityTable - [in] unity table, must have at least (1<<uLog2Length) elements, see FFTInitializeUnityTable()
// uChannelCount - [in] number of channels, must be within [1, 6]
// uLog2Length - [in] LOG (base 2) of FFT length in frames, must within [2, 9]
//----------------------------------------------------------------------------------
inline void FFTInterleaved (_Inout_updates_(((1<<uLog2Length)*uChannelCount)/4) XMVECTOR* __restrict pReal,
_Out_writes_(((1<<uLog2Length)*uChannelCount)/4) XMVECTOR* __restrict pImaginary,
_In_reads_(1<<uLog2Length) const XMVECTOR* __restrict pUnityTable,
_In_ const size_t uChannelCount,
_In_ const size_t uLog2Length)
{
assert(pReal);
assert(pImaginary);
assert(pUnityTable);
assert((uintptr_t)pReal % 16 == 0);
assert((uintptr_t)pImaginary % 16 == 0);
assert((uintptr_t)pUnityTable % 16 == 0);
assert(uChannelCount > 0 && uChannelCount <= 6);
assert(uLog2Length >= 2 && uLog2Length <= 9);
XMVECTOR vRealTemp[768];
XMVECTOR vImaginaryTemp[768];
const size_t uLength = size_t(1) << uLog2Length;
if (uChannelCount > 1)
{
Deinterleave(vRealTemp, pReal, uChannelCount, uLength);
}
else
{
memcpy_s(vRealTemp, sizeof(vRealTemp), pReal, (uLength>>2)*sizeof(XMVECTOR));
}
memset( vImaginaryTemp, 0, (uChannelCount*(uLength>>2)) * sizeof(XMVECTOR) );
if (uLength > 16)
{
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFT(&vRealTemp[uChannel*(uLength>>2)], &vImaginaryTemp[uChannel*(uLength>>2)], pUnityTable, uLength);
}
}
else if (uLength == 16)
{
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFT16(&vRealTemp[uChannel*(uLength>>2)], &vImaginaryTemp[uChannel*(uLength>>2)]);
}
}
else if (uLength == 8)
{
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFT8(&vRealTemp[uChannel*(uLength>>2)], &vImaginaryTemp[uChannel*(uLength>>2)]);
}
}
else if (uLength == 4)
{
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFT4(&vRealTemp[uChannel*(uLength>>2)], &vImaginaryTemp[uChannel*(uLength>>2)]);
}
}
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFTUnswizzle(&pReal[uChannel*(uLength>>2)], &vRealTemp[uChannel*(uLength>>2)], uLog2Length);
FFTUnswizzle(&pImaginary[uChannel*(uLength>>2)], &vImaginaryTemp[uChannel*(uLength>>2)], uLog2Length);
}
}
//----------------------------------------------------------------------------------
// DESCRIPTION:
// This function applies a 2^N-sample inverse FFT.
// Audio is interleaved if multichannel.
//
// PARAMETERS:
// pReal - [inout] real components, must have at least (1<<uLog2Length*uChannelCount)/4 elements
// pImaginary - [in] imaginary components, must have at least (1<<uLog2Length*uChannelCount)/4 elements
// pUnityTable - [in] unity table, must have at least (1<<uLog2Length) elements, see FFTInitializeUnityTable()
// uChannelCount - [in] number of channels, must be > 0
// uLog2Length - [in] LOG (base 2) of FFT length in frames, must within [2, 9]
//----------------------------------------------------------------------------------
inline void IFFTDeinterleaved (_Inout_updates_(((1<<uLog2Length)*uChannelCount)/4) XMVECTOR* __restrict pReal,
_In_reads_(((1<<uLog2Length)*uChannelCount)/4) const XMVECTOR* __restrict pImaginary,
_In_reads_(1<<uLog2Length) const XMVECTOR* __restrict pUnityTable,
_In_ const size_t uChannelCount,
_In_ const size_t uLog2Length)
{
using namespace DirectX;
assert(pReal);
assert(pImaginary);
assert(pUnityTable);
assert((uintptr_t)pReal % 16 == 0);
assert((uintptr_t)pImaginary % 16 == 0);
assert((uintptr_t)pUnityTable % 16 == 0);
assert(uChannelCount > 0 && uChannelCount <= 6);
_Analysis_assume_(uChannelCount > 0 && uChannelCount <= 6);
assert(uLog2Length >= 2 && uLog2Length <= 9);
_Analysis_assume_(uLog2Length >= 2 && uLog2Length <= 9);
XMVECTOR vRealTemp[768] = { 0 };
XMVECTOR vImaginaryTemp[768] = { 0 };
const size_t uLength = size_t(1) << uLog2Length;
const XMVECTOR vRnp = XMVectorReplicate(1.0f/uLength);
const XMVECTOR vRnm = XMVectorReplicate(-1.0f/uLength);
for (size_t u=0; u < uChannelCount*(uLength>>2); u++)
{
vRealTemp[u] = XMVectorMultiply(pReal[u], vRnp);
vImaginaryTemp[u] = XMVectorMultiply(pImaginary[u], vRnm);
}
if (uLength > 16)
{
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFT(&vRealTemp[uChannel*(uLength>>2)], &vImaginaryTemp[uChannel*(uLength>>2)], pUnityTable, uLength);
}
}
else if (uLength == 16)
{
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFT16(&vRealTemp[uChannel*(uLength>>2)], &vImaginaryTemp[uChannel*(uLength>>2)]);
}
}
else if (uLength == 8)
{
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFT8(&vRealTemp[uChannel*(uLength>>2)], &vImaginaryTemp[uChannel*(uLength>>2)]);
}
}
else if (uLength == 4)
{
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFT4(&vRealTemp[uChannel*(uLength>>2)], &vImaginaryTemp[uChannel*(uLength>>2)]);
}
}
for (size_t uChannel=0; uChannel < uChannelCount; ++uChannel)
{
FFTUnswizzle(&vImaginaryTemp[uChannel*(uLength>>2)], &vRealTemp[uChannel*(uLength>>2)], uLog2Length);
}
if (uChannelCount > 1)
{
Interleave(pReal, vImaginaryTemp, uChannelCount, uLength);
}
else
{
memcpy_s(pReal, uLength*uChannelCount*sizeof(float), vImaginaryTemp, (uLength>>2)*sizeof(XMVECTOR));
}
}
}; // namespace XDSP
#pragma warning(pop)
You can’t perform that action at this time.
