Skip to content

Commit

Permalink
Minor FFT speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Sep 16, 2023
1 parent 96189be commit 716cd84
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions Cavern/Utilities/Measurements.FFT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ static unsafe void ProcessFFT(Complex[] samples, FFTCache cache, int depth) {
fixed (Complex* pOdd = odd)
fixed (float* pCosCache = FFTCache.cos[depth + 1])
fixed (float* pSinCache = FFTCache.sin[depth + 1]) {
Complex* result = pSamples,
int step = Vector<float>.Count >> 1;
Vector2* result = (Vector2*)pSamples,
mirror = result + (samples.Length >> 1),
end = mirror,
endSimd = end - (Vector<float>.Count >> 1),
evenRef = pEven,
oddRef = pOdd;
endSimd = end - step,
evenRef = (Vector2*)pEven,
oddRef = (Vector2*)pOdd;
float* cosCache = pCosCache,
sinCache = pSinCache;

Expand All @@ -64,7 +65,6 @@ static unsafe void ProcessFFT(Complex[] samples, FFTCache cache, int depth) {
*(Vector<float>*)result = evenSource + newOdd;
*(Vector<float>*)mirror = evenSource - newOdd;

int step = Vector<float>.Count >> 1;
result += step;
mirror += step;
evenRef += step;
Expand All @@ -75,19 +75,15 @@ static unsafe void ProcessFFT(Complex[] samples, FFTCache cache, int depth) {

// Slow pass
while (result != end) {
float evenReal = evenRef->Real,
evenImag = evenRef->Imaginary,
oddRealSource = oddRef->Real,
oddImagSource = oddRef->Imaginary,
float oddRealSource = oddRef->X,
oddImagSource = oddRef->Y,
cachedCos = *cosCache,
cachedSin = *sinCache,
oddReal = oddRealSource * cachedCos - oddImagSource * cachedSin,
oddImag = oddRealSource * cachedSin + oddImagSource * cachedCos;
cachedSin = *sinCache;
Vector2 newOdd = new Vector2(oddRealSource * cachedCos - oddImagSource * cachedSin,
oddRealSource * cachedSin + oddImagSource * cachedCos);

result->Real = evenReal + oddReal;
result->Imaginary = evenImag + oddImag;
mirror->Real = evenReal - oddReal;
mirror->Imaginary = evenImag - oddImag;
*result = *evenRef + newOdd;
*mirror = *evenRef - newOdd;

result++;
mirror++;
Expand Down

0 comments on commit 716cd84

Please sign in to comment.