DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
FFT.h
1// DSPark -- Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi -- MIT License
3
4#pragma once
5
96#include "SimdOps.h"
97
98#include <algorithm>
99#include <array>
100#include <bit>
101#include <cassert>
102#include <cmath>
103#include <cstddef>
104#include <cstdint>
105#include <cstring>
106#include <functional>
107#include <numbers>
108#include <type_traits>
109#include <utility>
110#include <vector>
111
112// --- Exception policy --------------------------------------------------------
113// By default invalid FFT sizes throw std::invalid_argument. For embedded and
114// plugin builds compiled without exception support, define DSPARK_NO_EXCEPTIONS
115// (auto-detected from -fno-exceptions / /EHs-c-) -- invalid sizes then assert in
116// debug and degrade to a safe minimal size in release instead of throwing.
117#if !defined(DSPARK_NO_EXCEPTIONS)
118 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EXCEPTIONS)
119 #define DSPARK_NO_EXCEPTIONS 1
120 #elif defined(_MSC_VER) && !defined(_CPPUNWIND)
121 #define DSPARK_NO_EXCEPTIONS 1
122 #endif
123#endif
124
125#if !defined(DSPARK_NO_EXCEPTIONS)
126 #include <stdexcept>
127#endif
128
129namespace dspark {
130namespace detail {
131namespace fft {
132
133// ============================================================================
134// Kernels
135// ============================================================================
136
137// The plan types live in a namespace with no functions, so argument-dependent
138// lookup on them cannot reach the baseline passes from the AVX2 copy.
139namespace plan {
140
146template <typename T>
148{
149 const T* w;
150
153 [[nodiscard]] static constexpr size_t firstBlock(size_t n1, size_t block) noexcept
154 {
155 return n1 < block ? n1 : block;
156 }
157
162 [[nodiscard]] static constexpr size_t index(size_t stride, size_t block, size_t row, size_t p) noexcept
163 {
164 if (stride != 1) return 6 * p + row;
165 const size_t l = p & (block - 1);
166 return 6 * (p - l) + row * block + l;
167 }
168};
169
174struct Pass { size_t len; size_t stride; size_t twOffset; bool radix2; bool radix8; bool twiddled8; };
175
183#if defined(_M_AMD64) || defined(_M_X64) || defined(__x86_64__) || defined(__amd64__) \
184 || defined(_M_IX86) || defined(__i386__) || defined(__EMSCRIPTEN__)
185inline constexpr size_t kClosingRowReach = 4096;
186#else
187inline constexpr size_t kClosingRowReach = 8192;
188#endif
189
199#if defined(DSPARK_SIMD_NEON) && !defined(__APPLE__)
200inline constexpr bool kTwiddledRadix8 = true;
201#else
202inline constexpr bool kTwiddledRadix8 = false;
203#endif
204
210inline constexpr size_t kTwiddled8RowReach = 16384;
211
219#if (defined(DSPARK_SIMD_SSE2) && !defined(DSPARK_SIMD_AVX)) || defined(DSPARK_SIMD_NEON)
220inline constexpr size_t kRealWholeTwiddleReach = 256;
221#else
222inline constexpr size_t kRealWholeTwiddleReach = 0;
223#endif
224
233template <typename T>
234inline constexpr size_t kRealHalvesReach = sizeof(T) == sizeof(float) ? ~size_t{ 0 } : size_t{ 2048 };
235
237template <typename T>
238struct Buffers { T* xr; T* xi; T* yr; T* yi; };
239
242template <typename T>
243struct Plan
244{
245 const Pass* passes;
246 size_t passCount;
247 size_t n;
248 const T* twiddles;
250};
251
253template <typename T>
254struct Closing { T* out; T scaleRe; T scaleIm; };
255
258template <typename T>
260{
261 size_t n;
262 size_t lanes;
263 bool first8;
264 bool halves;
265 size_t wholeReach;
266 const T* twiddles;
267 const T* turn;
268 const T* edge;
270
272 [[nodiscard]] constexpr size_t firstLength() const noexcept { return first8 ? 8 : 4; }
273
277 [[nodiscard]] static constexpr size_t passValues(size_t m, size_t repeat) noexcept
278 {
279 return 6 * repeat * (m / 2 - 1);
280 }
281
284 [[nodiscard]] static constexpr size_t repeatFor(size_t m, size_t lanes, size_t wholeReach) noexcept
285 {
286 return m <= wholeReach ? lanes : 1;
287 }
288
292 [[nodiscard]] static constexpr bool halvesFor(size_t size, size_t lanes, size_t reach) noexcept
293 {
294 return lanes * sizeof(T) == 32 && size <= reach;
295 }
296
302 [[nodiscard]] static constexpr size_t laneBin(size_t lanes, size_t p, bool byHalves) noexcept
303 {
304 if (!byHalves) return p;
305 const size_t run = lanes / 4;
306 const size_t order[4] = { 0, 2, 1, 3 };
307 return run * order[p / run] + p % run;
308 }
309
312 [[nodiscard]] static constexpr size_t passCount(size_t n) noexcept
313 {
314 return static_cast<size_t>(std::countr_zero(n)) / 2;
315 }
316};
317
318} // namespace plan
319
320using plan::TwiddleSpan;
321using plan::Pass;
322using plan::Buffers;
323using plan::Plan;
324using plan::Closing;
325using plan::RealPlan;
326
329template <typename T> inline constexpr size_t kBlock = std::is_same_v<T, double> ? 4 : 8;
330
331// The passes, compiled for the build's baseline (detail/FftKernels.inl).
332template <typename T, int W> using Vec = simd::Vec<T, W>;
333template <typename T> inline constexpr int kWide = simd::kVecWidth<T>;
334template <typename T> inline constexpr int kNarrow = simd::kVecNarrowWidth<T>;
335#include "detail/FftKernels.inl"
336
337} // namespace fft
338} // namespace detail
339} // namespace dspark
340
341#if defined(DSPARK_SIMD_DISPATCH_AVX2)
342// The same passes compiled for AVX2+FMA, run when isRuntimeDispatchActive().
343DSPARK_SIMD_AVX2_BEGIN
344namespace dspark {
345namespace detail {
346namespace fft {
347namespace avx2 {
348template <typename T, int W> using Vec = simd::avx2::Vec<T, W>;
349template <typename T> inline constexpr int kWide = simd::avx2::kVecWidth<T>;
350template <typename T> inline constexpr int kNarrow = simd::avx2::kVecNarrowWidth<T>;
351#include "detail/FftKernels.inl"
352} // namespace avx2
353} // namespace fft
354} // namespace detail
355} // namespace dspark
356DSPARK_SIMD_AVX2_END
357#endif
358
359namespace dspark {
360namespace detail {
361namespace fft {
362
380template <typename T>
382{
383public:
384 WorkBuffers() = default;
385
387 explicit WorkBuffers(size_t count)
388 : count_(count), storage_(values(count), T(0)), lead_(leadOf(storage_.data()))
389 {
390 }
391
393 : count_(other.count_), storage_(other.storage_.size(), T(0)), lead_(leadOf(storage_.data()))
394 {
395 if (count_ > 0)
396 std::copy_n(other.first(), stride(count_) + count_, first());
397 }
398
399 WorkBuffers(WorkBuffers&& other) noexcept
400 : count_(std::exchange(other.count_, size_t{ 0 })), storage_(std::move(other.storage_)),
401 lead_(std::exchange(other.lead_, size_t{ 0 }))
402 {
403 }
404
406 {
407 std::swap(count_, other.count_);
408 storage_.swap(other.storage_);
409 std::swap(lead_, other.lead_);
410 return *this;
411 }
412
413 [[nodiscard]] T* first() noexcept { return storage_.data() + lead_; }
414 [[nodiscard]] const T* first() const noexcept { return storage_.data() + lead_; }
415 [[nodiscard]] T* second() noexcept { return first() + stride(count_); }
416 [[nodiscard]] const T* second() const noexcept { return first() + stride(count_); }
417
421 [[nodiscard]] static constexpr size_t values(size_t count) noexcept
422 {
423 return (kLine - 1) + stride(count) + count;
424 }
425
426private:
427 static constexpr size_t kLine = 64 / sizeof(T);
428
429 static constexpr size_t stride(size_t count) noexcept
430 {
431 return (count + kLine - 1) / kLine * kLine;
432 }
433
435 static size_t leadOf(const T* data) noexcept
436 {
437 const auto address = reinterpret_cast<std::uintptr_t>(data);
438 return static_cast<size_t>((64 - address % 64) % 64) / sizeof(T);
439 }
440
441 size_t count_ = 0;
442 std::vector<T> storage_;
443 size_t lead_ = 0;
444};
445
451template <typename T>
453{
454public:
457 SplitFFT() = default;
458
459 explicit SplitFFT(size_t n, bool interleavedInput = false) : n_(n), work_(2 * n)
460 {
461 PlanPasses steps;
462 size_t offset = 0;
463 const size_t passCount = buildPlan(n_, steps, offset);
464 passes_.assign(steps.begin(), steps.begin() + static_cast<std::ptrdiff_t>(passCount));
465
466 twiddles_.assign(offset, T(0));
467 for (const Pass& pass : passes_)
468 {
469 if (pass.radix2 || pass.radix8) continue;
470 if (pass.twiddled8)
471 {
472 // w^(kp), k = 1 .. 7: the fourteen values of a group together,
473 // each one a whole vector of the width the pass runs at (it
474 // loads them: a broadcast would take a vector operation).
475 constexpr size_t lanes = static_cast<size_t>(kWide<T>);
476 T* const w = twiddles_.data() + pass.twOffset;
477 for (size_t p = 0; p < pass.len / 8; ++p)
478 {
479 const double a = -2.0 * std::numbers::pi_v<double> * static_cast<double>(p)
480 / static_cast<double>(pass.len);
481 for (size_t k = 1; k <= 7; ++k)
482 {
483 T* const at = w + lanes * (14 * p + 2 * (k - 1));
484 std::fill_n(at, lanes, static_cast<T>(std::cos(static_cast<double>(k) * a)));
485 std::fill_n(at + lanes, lanes, static_cast<T>(std::sin(static_cast<double>(k) * a)));
486 }
487 }
488 continue;
489 }
490 const size_t n1 = pass.len / 4;
491 const size_t block = TwiddleSpan<T>::firstBlock(n1, kBlock<T>);
492 T* w = twiddles_.data() + pass.twOffset;
493 for (size_t p = 0; p < n1; ++p)
494 {
495 const double a = -2.0 * std::numbers::pi_v<double> * static_cast<double>(p)
496 / static_cast<double>(pass.len);
497 const double factors[6] = { std::cos(a), std::sin(a), std::cos(2.0 * a), std::sin(2.0 * a),
498 std::cos(3.0 * a), std::sin(3.0 * a) };
499 for (size_t row = 0; row < 6; ++row)
500 w[TwiddleSpan<T>::index(pass.stride, block, row, p)] = static_cast<T>(factors[row]);
501 }
502 }
503
504 // The first pass's twiddles once more, for interleaved input read in
505 // lane (see FftKernels.inl, kInLaneDeinterleave): within each block of
506 // butterflies, its four runs of kBlock / 4 in the order (0, 2, 1, 3).
507 // Only builds with a kernel set that reads that way keep them.
508 if (const size_t inLane = inLaneTwiddleCount(steps, passCount, interleavedInput); inLane > 0)
509 {
510 const Pass& first = passes_.front();
511 constexpr size_t B = kBlock<T>, q = B / 4;
512 const size_t n1 = first.len / 4;
513 inLaneTwiddles_.assign(inLane, T(0));
514 const T* w = twiddles_.data() + first.twOffset;
515 for (size_t row = 0; row < 6; ++row)
516 for (size_t p = 0; p < n1; ++p)
517 {
518 const size_t l = p % B, j = l / q;
519 const size_t from = p - l + q * ((j & 1) * 2 + (j >> 1)) + l % q;
520 inLaneTwiddles_[TwiddleSpan<T>::index(1, B, row, p)] = w[TwiddleSpan<T>::index(1, B, row, from)];
521 }
522 }
523 }
524
528 static constexpr size_t kAllocationOverhead = 64;
529
536 [[nodiscard]] static size_t allocationBytes(size_t n, bool interleavedInput) noexcept
537 {
538 PlanPasses steps;
539 size_t twiddles = 0;
540 const size_t passCount = buildPlan(n, steps, twiddles);
541 const size_t inLane = inLaneTwiddleCount(steps, passCount, interleavedInput);
542 const size_t allocations = 2 + (twiddles > 0 ? 1 : 0) + (inLane > 0 ? 1 : 0);
543 return (WorkBuffers<T>::values(2 * n) + twiddles + inLane) * sizeof(T)
544 + passCount * sizeof(Pass) + allocations * kAllocationOverhead;
545 }
546
547 [[nodiscard]] size_t size() const noexcept { return n_; }
548
550 [[nodiscard]] T* inRe() noexcept { return work_.first(); }
551 [[nodiscard]] T* inIm() noexcept { return work_.first() + n_; }
552
558 void run(const T*& outRe, const T*& outIm) noexcept
559 {
560 Buffers<T> b = buffers();
561 const Plan<T> p = plan();
562#if defined(DSPARK_SIMD_DISPATCH_AVX2)
564 avx2::transformRun(p, b);
565 else
566#endif
567 fft::transformRun(p, b);
568 outRe = b.xr;
569 outIm = b.xi;
570 }
571
580 void runFrom(const T* data, bool conjugate, const T*& outRe, const T*& outIm) noexcept
581 {
582 if (passes_.size() == 1)
583 {
584 T* const re = work_.second();
585 T* const im = re + n_;
586 onePass([&](size_t i, T& r, T& m) { readPair(data, conjugate, i, r, m); },
587 [&](size_t i, T r, T m) { re[i] = r; im[i] = m; });
588 outRe = re;
589 outIm = im;
590 return;
591 }
592 Buffers<T> b = buffers();
593 const Plan<T> p = plan();
594#if defined(DSPARK_SIMD_DISPATCH_AVX2)
596 avx2::transformFrom(p, b, data, conjugate);
597 else
598#endif
599 fft::transformFrom(p, b, data, conjugate);
600 outRe = b.xr;
601 outIm = b.xi;
602 }
603
612 void runFromSplit(const T* re, const T* im, const T*& outRe, const T*& outIm) noexcept
613 {
614 Buffers<T> b = buffers();
615 const Plan<T> p = plan();
616#if defined(DSPARK_SIMD_DISPATCH_AVX2)
618 avx2::transformFromSplit(p, b, re, im);
619 else
620#endif
621 fft::transformFromSplit(p, b, re, im);
622 outRe = b.xr;
623 outIm = b.xi;
624 }
625
643 void runScaledSwap(const T* re, const T* im, const T* gain, size_t mask,
644 const T*& outRe, const T*& outIm) noexcept
645 {
646 Buffers<T> b = buffers();
647 // The first pass must not write over its own input: when that is the
648 // second work buffer, the passes start from it.
649 const std::less<const T*> before;
650 const T* const second = work_.second();
651 if (!before(re, second) && before(re, second + 2 * n_))
652 {
653 std::swap(b.xr, b.yr);
654 std::swap(b.xi, b.yi);
655 }
656 const Plan<T> p = plan();
657#if defined(DSPARK_SIMD_DISPATCH_AVX2)
659 avx2::transformScaledSwap(p, b, re, im, gain, mask);
660 else
661#endif
662 fft::transformScaledSwap(p, b, re, im, gain, mask);
663 outRe = b.xr;
664 outIm = b.xi;
665 }
666
680 void runScaledSwapFolded(const T* re, const T* im, const T* gain, size_t fold,
681 const T*& outRe, const T*& outIm) noexcept
682 {
683 Buffers<T> b = buffers();
684 const Plan<T> p = plan();
685#if defined(DSPARK_SIMD_DISPATCH_AVX2)
687 avx2::transformScaledSwapFolded(p, b, re, im, gain, fold);
688 else
689#endif
690 fft::transformScaledSwapFolded(p, b, re, im, gain, fold);
691 outRe = b.xr;
692 outIm = b.xi;
693 }
694
701 void runTo(T* out, T scaleRe, T scaleIm) noexcept
702 {
703 if (passes_.size() == 1)
704 {
705 const T* const re = work_.first();
706 const T* const im = re + n_;
707 onePass([&](size_t i, T& r, T& m) { r = re[i]; m = im[i]; },
708 [&](size_t i, T r, T m) { out[2 * i] = r * scaleRe; out[2 * i + 1] = m * scaleIm; });
709 return;
710 }
711 Buffers<T> b = buffers();
712 const Plan<T> p = plan();
713 const Closing<T> c { out, scaleRe, scaleIm };
714#if defined(DSPARK_SIMD_DISPATCH_AVX2)
716 avx2::transformTo(p, b, c);
717 else
718#endif
719 fft::transformTo(p, b, c);
720 }
721
724 void runFromTo(const T* data, bool conjugate, T* out, T scaleRe, T scaleIm) noexcept
725 {
726 if (passes_.size() == 1)
727 {
728 onePass([&](size_t i, T& r, T& m) { readPair(data, conjugate, i, r, m); },
729 [&](size_t i, T r, T m) { out[2 * i] = r * scaleRe; out[2 * i + 1] = m * scaleIm; });
730 return;
731 }
732 Buffers<T> b = buffers();
733 const Plan<T> p = plan();
734 const Closing<T> c { out, scaleRe, scaleIm };
735#if defined(DSPARK_SIMD_DISPATCH_AVX2)
737 avx2::transformFromTo(p, b, data, conjugate, c);
738 else
739#endif
740 fft::transformFromTo(p, b, data, conjugate, c);
741 }
742
745 [[nodiscard]] size_t runToStepCount() const noexcept { return passes_.size(); }
746
759 void runToStep(size_t step, T* out, T scaleRe, T scaleIm) noexcept
760 {
761 Buffers<T> b = buffers();
762 if ((step & 1u) != 0)
763 {
764 std::swap(b.xr, b.yr);
765 std::swap(b.xi, b.yi);
766 }
767 const Plan<T> p = plan();
768 const Closing<T> c { out, scaleRe, scaleIm };
769 if (step == 0) stepAvx2_ = simd::isRuntimeDispatchActive();
770#if defined(DSPARK_SIMD_DISPATCH_AVX2)
771 if (stepAvx2_)
772 avx2::transformToStep(p, b, step, c);
773 else
774#endif
775 fft::transformToStep(p, b, step, c);
776 }
777
778private:
779 [[nodiscard]] Buffers<T> buffers() noexcept
780 {
781 return { work_.first(), work_.first() + n_, work_.second(), work_.second() + n_ };
782 }
783
785 static void readPair(const T* data, bool conjugate, size_t i, T& r, T& m) noexcept
786 {
787 r = data[2 * i];
788 m = conjugate ? -data[2 * i + 1] : data[2 * i + 1];
789 }
790
791 // A transform of one pass (2 or 4 points) runs here: its few additions
792 // cost less than reaching the kernels. The arithmetic is the closing
793 // pass's (FftKernels.inl), so the result is the same to the bit. Every
794 // input is read before the first output is written (in place is safe).
795 template <typename Load, typename Store>
796 void onePass(Load load, Store store) const noexcept
797 {
798 if (n_ == 2)
799 {
800 T ar, ai, br, bi;
801 load(0, ar, ai);
802 load(1, br, bi);
803 store(0, ar + br, ai + bi);
804 store(1, ar - br, ai - bi);
805 return;
806 }
807 T ar, ai, br, bi, cr, ci, dr, di;
808 load(0, ar, ai);
809 load(1, br, bi);
810 load(2, cr, ci);
811 load(3, dr, di);
812 const T apcR = ar + cr, apcI = ai + ci, amcR = ar - cr, amcI = ai - ci;
813 const T bpdR = br + dr, bpdI = bi + di, bmdR = br - dr, bmdI = bi - di;
814 store(0, apcR + bpdR, apcI + bpdI);
815 store(1, amcR + bmdI, amcI - bmdR);
816 store(2, apcR - bpdR, apcI - bpdI);
817 store(3, amcR - bmdI, amcI + bmdR);
818 }
819
820 // Each transform makes one call into one kernel set (the AVX2+FMA copy
821 // when the dispatch is active), so all of its passes agree on the work
822 // layout even if the dispatch flag changes meanwhile.
823 [[nodiscard]] Plan<T> plan() const noexcept
824 {
825 return { passes_.data(), passes_.size(), n_, twiddles_.data(),
826 inLaneTwiddles_.empty() ? nullptr : inLaneTwiddles_.data() };
827 }
828
830 using PlanPasses = std::array<Pass, 40>;
831
832 // Plan: a first radix-4 pass, twiddled passes and a closing pass without
833 // twiddles. With radix-4 passes alone, an odd log2(N) closes with a
834 // radix-8 pass in place of a length-8 radix-4 pass and a radix-2 one, or
835 // with that pair where the radix-8 pass's eight rows (n/8 elements of the
836 // blocked layout each, re and im) would sit kClosingRowReach apart or
837 // more, where its rows would not fill a block of the work layout, and
838 // where it would be the first pass. Where kTwiddledRadix8, and up to the
839 // size whose eighths sit kTwiddled8RowReach apart, the bits between the
840 // first pass and the closing one go three at a time (a twiddled radix-8
841 // pass), after the one or two radix-4 passes that the count leaves over,
842 // and the closing pass is the radix-8 one whenever it may be (per bit it
843 // is the cheapest pass) and the radix-4 one otherwise.
844 // The constructor and allocationBytes() both build it here; twiddleValues
845 // receives the size of the twiddle table.
846 static size_t buildPlan(size_t n, PlanPasses& out, size_t& twiddleValues) noexcept
847 {
848 const size_t bits = static_cast<size_t>(std::countr_zero(n));
849 size_t len = n, stride = 1, offset = 0, count = 0;
850 const auto radix4 = [&]() noexcept {
851 out[count++] = { len, stride, offset, false, false, false };
852 offset += 6 * (len / 4);
853 len /= 4;
854 stride *= 4;
855 };
856 const auto twiddled8 = [&]() noexcept {
857 out[count++] = { len, stride, offset, false, false, true };
858 offset += 14 * static_cast<size_t>(kWide<T>) * (len / 8);
859 len /= 8;
860 stride *= 8;
861 };
862 if (bits >= 2)
863 {
864 const size_t rest = bits - 2; // bits after the first pass
865 const bool closing8 = n >= 32 && n / 8 >= kBlock<T>
866 && 2 * (n / 8) * sizeof(T) < plan::kClosingRowReach;
867 // Bits of the closing pass (0: the first pass is the only one),
868 // and the radix-4 and twiddled radix-8 passes before it.
869 size_t closing = 0, fours = 0, eights = 0;
870 if (rest > 0)
871 {
872 closing = (rest & 1) == 0 ? 2 : closing8 ? 3 : 1;
873 fours = (rest - closing) / 2;
874 if constexpr (plan::kTwiddledRadix8)
875 {
876 const size_t c = closing8 && rest >= 3 && rest != 4 ? 3 : 2;
877 if (rest >= c + 3 && 2 * (n / 8) * sizeof(T) < plan::kTwiddled8RowReach)
878 {
879 const size_t between = rest - c;
880 // Remainders 0, 1 and 2 of three leave 0, 2 and 1 radix-4 passes.
881 const size_t f = (3 - between % 3) % 3;
882 const size_t e = (between - 2 * f) / 3;
883 // A twiddled radix-8 pass needs whole blocks of stride.
884 if (between >= 2 * f && e > 0 && (size_t{ 4 } << (2 * f)) >= kBlock<T>)
885 {
886 closing = c;
887 fours = f;
888 eights = e;
889 }
890 }
891 }
892 }
893 radix4();
894 for (size_t i = 0; i < fours; ++i) radix4();
895 for (size_t i = 0; i < eights; ++i) twiddled8();
896 if (closing == 2)
897 radix4();
898 else if (closing == 3)
899 out[count++] = { 8, stride, offset, false, true, false };
900 else if (closing == 1)
901 out[count++] = { 2, stride, offset, true, false, false };
902 }
903 else
904 out[count++] = { 2, stride, offset, true, false, false };
905 twiddleValues = offset;
906 return count;
907 }
908
910 static size_t inLaneTwiddleCount(const PlanPasses& steps, size_t count,
911 bool interleavedInput) noexcept
912 {
913#if defined(DSPARK_SIMD_DISPATCH_AVX2)
914 constexpr bool inLaneReader = true;
915#else
916 constexpr bool inLaneReader = kInLaneDeinterleave<T, kWide<T>>;
917#endif
918 const Pass& first = steps[0];
919 if (inLaneReader && interleavedInput && count > 1 && !first.radix2 && !first.radix8
920 && (first.len / 4) % kBlock<T> == 0)
921 return 6 * (first.len / 4);
922 return 0;
923 }
924
925 size_t n_ = 0;
926 WorkBuffers<T> work_;
927 std::vector<T> twiddles_;
928 std::vector<T> inLaneTwiddles_;
929 std::vector<Pass> passes_;
930 bool stepAvx2_ = false;
931};
932
940template <typename T>
942{
943public:
944 RealLaneTables() = default;
945
958 RealLaneTables(size_t size, size_t lanes, size_t wholeReach, size_t halvesReach)
959 : n_(size / lanes), lanes_(lanes), wholeReach_(wholeReach),
960 halves_(RealPlan<T>::halvesFor(size, lanes, halvesReach)), values_(valueCount(size, lanes, wholeReach))
961 {
962 T* w = values_.data();
963 double c = 0.0, s = 0.0;
964 // Passes: w^(ji), j = 1 .. 3, w = exp(-2 pi i / 4m), for the bins i
965 // of the spectra of m points that the pass joins.
966 for (size_t m = firstLength(n_); m < n_; m *= 4)
967 {
968 const size_t repeat = RealPlan<T>::repeatFor(m, lanes_, wholeReach_);
969 for (size_t i = 1; i < m / 2; ++i)
970 for (size_t j = 1; j <= 3; ++j)
971 {
972 unit(j * i, 4 * m, c, s);
973 w = std::fill_n(w, repeat, static_cast<T>(c));
974 w = std::fill_n(w, repeat, static_cast<T>(0.0 - s));
975 }
976 }
977 // The joining step: exp(-2 pi i j k / size) for the lanes j >= 1,
978 // the bins k of a group along a vector, in the order the kernels
979 // hold them (RealPlan::laneBin()).
980 turn_ = static_cast<size_t>(w - values_.data());
981 for (size_t first = 0; first < n_ / 2; first += lanes_)
982 for (size_t j = 1; j < lanes_; ++j)
983 {
984 for (size_t t = 0; t < lanes_; ++t)
985 {
986 unit(j * (first + RealPlan<T>::laneBin(lanes_, t, halves_)), size, c, s);
987 w[t] = static_cast<T>(c);
988 w[lanes_ + t] = static_cast<T>(0.0 - s);
989 }
990 w += 2 * lanes_;
991 }
992 // The middle bin: cos and sin of pi j (2r + 1) / lanes.
993 edge_ = static_cast<size_t>(w - values_.data());
994 const size_t half = lanes_ / 2;
995 for (size_t j = 0; j < lanes_; ++j)
996 for (size_t r = 0; r < half; ++r)
997 {
998 unit(j * (2 * r + 1), 2 * lanes_, c, s);
999 w[j * half + r] = static_cast<T>(c);
1000 w[lanes_ * half + j * half + r] = static_cast<T>(s);
1001 }
1002 }
1003
1005 [[nodiscard]] static constexpr size_t valueCount(size_t size, size_t lanes, size_t wholeReach) noexcept
1006 {
1007 const size_t n = size / lanes;
1008 size_t count = (lanes - 1) * n + lanes * lanes;
1009 for (size_t m = firstLength(n); m < n; m *= 4)
1010 count += RealPlan<T>::passValues(m, RealPlan<T>::repeatFor(m, lanes, wholeReach));
1011 return count;
1012 }
1013
1014 [[nodiscard]] bool empty() const noexcept { return lanes_ == 0; }
1015
1017 [[nodiscard]] size_t passes() const noexcept { return empty() ? 0 : RealPlan<T>::passCount(n_); }
1018
1019 [[nodiscard]] RealPlan<T> plan() const noexcept
1020 {
1021 return { n_, lanes_, firstLength(n_) == 8, halves_, wholeReach_, values_.data(), values_.data() + turn_,
1022 values_.data() + edge_, T(1) / static_cast<T>(n_ * lanes_) };
1023 }
1024
1025private:
1027 [[nodiscard]] static constexpr size_t firstLength(size_t n) noexcept
1028 {
1029 return (std::countr_zero(n) & 1) != 0 ? 8 : 4;
1030 }
1031
1036 static void unit(size_t k, size_t count, double& c, double& s) noexcept
1037 {
1038 const size_t quarter = count / 4;
1039 const size_t rest = k % quarter;
1040 const double step = 2.0 * std::numbers::pi_v<double> / static_cast<double>(count);
1041 double x = 1.0, y = 0.0;
1042 if (rest != 0)
1043 {
1044 if (2 * rest == quarter)
1045 x = y = 0.70710678118654752440;
1046 else if (2 * rest < quarter)
1047 {
1048 x = std::cos(step * static_cast<double>(rest));
1049 y = std::sin(step * static_cast<double>(rest));
1050 }
1051 else
1052 {
1053 x = std::sin(step * static_cast<double>(quarter - rest));
1054 y = std::cos(step * static_cast<double>(quarter - rest));
1055 }
1056 }
1057 switch ((k / quarter) & 3) // quarter turns
1058 {
1059 case 0: c = x; s = y; break;
1060 case 1: c = 0.0 - y; s = x; break; // zeros stay positive
1061 case 2: c = 0.0 - x; s = 0.0 - y; break;
1062 default: c = y; s = 0.0 - x; break;
1063 }
1064 }
1065
1066 size_t n_ = 0;
1067 size_t lanes_ = 0;
1068 size_t wholeReach_ = 0;
1069 bool halves_ = false;
1070 size_t turn_ = 0;
1071 size_t edge_ = 0;
1072 std::vector<T> values_;
1073};
1074
1075} // namespace fft
1076} // namespace detail
1077
1078// ============================================================================
1079// FFTComplex
1080// ============================================================================
1081
1092template <typename T>
1094{
1095public:
1102 explicit FFTComplex(size_t size)
1103 : engine_(validateSize(size), true)
1104 {
1105 }
1106
1111 [[nodiscard]] size_t getSize() const noexcept { return engine_.size(); }
1112
1123 [[nodiscard]] static size_t allocationBytes(size_t size) noexcept
1124 {
1125 if (size < 2 || (size & (size - 1)) != 0)
1126 return 0;
1128 }
1129
1135 void forward(T* data) noexcept { transform(data, false); }
1136
1142 void inverse(T* data) noexcept { transform(data, true); }
1143
1144private:
1145 static size_t validateSize(size_t size)
1146 {
1147 if (size < 2 || (size & (size - 1)) != 0)
1148 {
1149#if defined(DSPARK_NO_EXCEPTIONS)
1150 assert(false && "FFTComplex size must be a power of two >= 2");
1151 return 2; // degrade to the minimal valid size
1152#else
1153 throw std::invalid_argument("FFTComplex size must be a power of two >= 2");
1154#endif
1155 }
1156 return size;
1157 }
1158
1159 void transform(T* data, bool inverse) noexcept
1160 {
1161 // The inverse conjugates on the way in, and on the way out scales by 1/N
1162 // and conjugates again.
1163 const T scale = inverse ? T(1) / static_cast<T>(engine_.size()) : T(1);
1164 engine_.runFromTo(data, inverse, data, scale, inverse ? -scale : scale);
1165 }
1166
1167 detail::fft::SplitFFT<T> engine_;
1168};
1169
1170// ============================================================================
1171// FFTReal
1172// ============================================================================
1173
1195template <typename T>
1197{
1198public:
1205 explicit FFTReal(size_t size)
1206 : realSize_(validateSize(size)) // validates BEFORE anything is built
1207 , halfSize_(realSize_ / 2) // derived from the VALIDATED size, so
1208 , engine_(makeEngine(realSize_)) // everything stays coherent when size degrades
1209 {
1210 const size_t lanes = baseLanes(realSize_);
1211 if (lanes == 0)
1212 {
1213 computeSplitFactors();
1214 return;
1215 }
1216 laneWork_ = detail::fft::WorkBuffers<T>(realSize_);
1217 constexpr size_t halvesReach = detail::fft::plan::kRealHalvesReach<T>;
1219 halvesReach);
1220 if (const size_t other = dispatchedLanes(realSize_); other != lanes)
1221 wideLanes_ = detail::fft::RealLaneTables<T>(realSize_, other, 0, halvesReach);
1222 }
1223
1225 [[nodiscard]] size_t getSize() const noexcept { return realSize_; }
1226
1228 [[nodiscard]] size_t getFrequencyDomainSize() const noexcept { return realSize_ + 2; }
1229
1231 [[nodiscard]] size_t getNumBins() const noexcept { return halfSize_ + 1; }
1232
1243 [[nodiscard]] static size_t allocationBytes(size_t size) noexcept
1244 {
1245 if (size < 4 || (size & (size - 1)) != 0)
1246 return 0;
1247 constexpr size_t overhead = detail::fft::SplitFFT<T>::kAllocationOverhead;
1248 const size_t lanes = baseLanes(size);
1249 if (lanes == 0)
1250 return detail::fft::SplitFFT<T>::allocationBytes(size / 2, true)
1251 + 2 * (splitFactorCount(size / 2) * sizeof(T) + overhead);
1252 using Tables = detail::fft::RealLaneTables<T>;
1253 size_t values = detail::fft::WorkBuffers<T>::values(size)
1254 + Tables::valueCount(size, lanes, detail::fft::plan::kRealWholeTwiddleReach);
1255 size_t allocations = 2;
1256 if (const size_t other = dispatchedLanes(size); other != lanes)
1257 {
1258 values += Tables::valueCount(size, other, 0);
1259 ++allocations;
1260 }
1261 return values * sizeof(T) + allocations * overhead;
1262 }
1263
1269 void forward(const T* timeData, T* freqData) noexcept
1270 {
1271 if (!lanes_.empty())
1272 {
1273 T* const a = laneWork_.first();
1274 T* const b = laneWork_.second();
1275#if defined(DSPARK_SIMD_DISPATCH_AVX2)
1277 {
1278 detail::fft::avx2::realForward(dispatchedTables().plan(), timeData, freqData, a, b);
1279 return;
1280 }
1281#endif
1282 detail::fft::realForward(lanes_.plan(), timeData, freqData, a, b);
1283 return;
1284 }
1285 // (even, odd) sample pairs are the complex input.
1286 const T* zr = nullptr;
1287 const T* zi = nullptr;
1288 engine_.runFrom(timeData, false, zr, zi);
1289 unpackForward(zr, zi, freqData);
1290 }
1291
1297 void inverse(const T* freqData, T* timeData) noexcept
1298 {
1299 if (!lanes_.empty())
1300 {
1301 T* const a = laneWork_.first();
1302 T* const b = laneWork_.second();
1303#if defined(DSPARK_SIMD_DISPATCH_AVX2)
1305 {
1306 detail::fft::avx2::realInverse(dispatchedTables().plan(), freqData, timeData, a, b);
1307 return;
1308 }
1309#endif
1310 detail::fft::realInverse(lanes_.plan(), freqData, timeData, a, b);
1311 return;
1312 }
1313 // Pack into the conjugated half-size spectrum, run the forward engine,
1314 // conjugate back and scale by 1/(N/2): ifft = conj(fft(conj)) / M.
1315 packInverseConj(freqData, engine_.inRe(), engine_.inIm());
1316 const T scale = inverseScale();
1317 engine_.runTo(timeData, scale, -scale);
1318 }
1319
1322 [[nodiscard]] int getInverseStepCount() const noexcept
1323 {
1324 if (!lanes_.empty())
1325 return static_cast<int>(std::max(lanes_.passes(), wideLanes_.passes())) + 1;
1326 return static_cast<int>(engine_.runToStepCount()) + 1;
1327 }
1328
1345 void inverseStep(int step, const T* freqData, T* timeData) noexcept
1346 {
1347 if (!lanes_.empty())
1348 {
1349 // Step 0 chooses the kernel set and the later steps keep it. A
1350 // set whose transform has fewer passes than the count leaves its
1351 // spare steps right after the first, so the last step is always
1352 // the one that writes the samples.
1353 if (step <= 0) stepDispatched_ = simd::isRuntimeDispatchActive();
1354 const detail::fft::RealLaneTables<T>& tables = stepDispatched_ ? dispatchedTables() : lanes_;
1355 const int spare = getInverseStepCount() - 1 - static_cast<int>(tables.passes());
1356 if (step > 0 && step <= spare) return;
1357 const size_t own = step <= 0 ? size_t{ 0 } : static_cast<size_t>(step - spare);
1358 T* const a = laneWork_.first();
1359 T* const b = laneWork_.second();
1360#if defined(DSPARK_SIMD_DISPATCH_AVX2)
1361 if (stepDispatched_)
1362 {
1363 detail::fft::avx2::realInverseStep(tables.plan(), own, freqData, timeData, a, b);
1364 return;
1365 }
1366#endif
1367 detail::fft::realInverseStep(tables.plan(), own, freqData, timeData, a, b);
1368 return;
1369 }
1370 if (step <= 0)
1371 {
1372 packInverseConj(freqData, engine_.inRe(), engine_.inIm());
1373 return;
1374 }
1375 const T scale = inverseScale();
1376 engine_.runToStep(static_cast<size_t>(step - 1), timeData, scale, -scale);
1377 }
1378
1384 void computeMagnitudes(const T* freqData, T* magnitudes) const noexcept
1385 {
1386 for (size_t k = 0; k <= halfSize_; ++k)
1387 {
1388 T re = freqData[2 * k];
1389 T im = freqData[2 * k + 1];
1390 magnitudes[k] = std::sqrt(re * re + im * im);
1391 }
1392 }
1393
1399 void computePhases(const T* freqData, T* phases) const noexcept
1400 {
1401 for (size_t k = 0; k <= halfSize_; ++k)
1402 {
1403 T re = freqData[2 * k];
1404 T im = freqData[2 * k + 1];
1405 phases[k] = std::atan2(im, re);
1406 }
1407 }
1408
1414 void computePowerSpectrum(const T* freqData, T* power) const noexcept
1415 {
1416 for (size_t k = 0; k <= halfSize_; ++k)
1417 {
1418 T re = freqData[2 * k];
1419 T im = freqData[2 * k + 1];
1420 power[k] = re * re + im * im;
1421 }
1422 }
1423
1431 [[nodiscard]] static T binToFrequency(size_t binIndex, double sampleRate, size_t fftSize) noexcept
1432 {
1433 return static_cast<T>(static_cast<double>(binIndex) * sampleRate / static_cast<double>(fftSize));
1434 }
1435
1443 [[nodiscard]] static size_t frequencyToBin(double frequency, double sampleRate, size_t fftSize) noexcept
1444 {
1445 return static_cast<size_t>(std::round(frequency * static_cast<double>(fftSize) / sampleRate));
1446 }
1447
1448private:
1453 static size_t validateSize(size_t size)
1454 {
1455 if (size < 4 || (size & (size - 1)) != 0)
1456 {
1457#if defined(DSPARK_NO_EXCEPTIONS)
1458 assert(false && "FFTReal size must be a power of two >= 4");
1459 return 4;
1460#else
1461 throw std::invalid_argument("FFTReal size must be a power of two >= 4");
1462#endif
1463 }
1464 return size;
1465 }
1466
1468 [[nodiscard]] static size_t splitFactorCount(size_t m) noexcept
1469 {
1470 return std::min(m, m / 2 + detail::fft::kBlock<T>);
1471 }
1472
1473 void computeSplitFactors()
1474 {
1475 // The pre and post passes pair bin k <= M/2 with M - k through the
1476 // split factor c_k = (1 - j w^k) / 2, w = exp(-2 pi i / N): with t =
1477 // 2 pi k / N, c_k = ((1 - sin t) / 2, -cos(t) / 2), its real part
1478 // taken as sin^2(pi / 4 - t / 2), which keeps its relative accuracy
1479 // where 1 - sin t cancels. c_(M-k) = conj(c_k), so the table is built
1480 // to satisfy it exactly: computed up to M/2 (where c is exactly 0)
1481 // and mirrored past it, as far as a vector starting at or below M/2
1482 // reads.
1483 const size_t m = halfSize_;
1484 const size_t used = splitFactorCount(m);
1485 splitRe_.resize(used);
1486 splitIm_.resize(used);
1487 for (size_t k = 0; k <= m / 2; ++k)
1488 {
1489 const double half = std::numbers::pi_v<double> * static_cast<double>(k)
1490 / static_cast<double>(realSize_);
1491 const double s = std::sin(0.25 * std::numbers::pi_v<double> - half);
1492 splitRe_[k] = static_cast<T>(s * s);
1493 splitIm_[k] = static_cast<T>(-0.5 * std::cos(2.0 * half));
1494 }
1495 splitRe_[m / 2] = T(0);
1496 splitIm_[m / 2] = T(0);
1497 for (size_t k = m / 2 + 1; k < used; ++k)
1498 {
1499 splitRe_[k] = splitRe_[m - k];
1500 splitIm_[k] = -splitIm_[m - k];
1501 }
1502 }
1503
1505 void unpackForward(const T* zr, const T* zi, T* out) const noexcept
1506 {
1507#if defined(DSPARK_SIMD_DISPATCH_AVX2)
1509 {
1510 detail::fft::avx2::unpackRealForward(zr, zi, out, halfSize_, splitRe_.data(), splitIm_.data());
1511 return;
1512 }
1513#endif
1514 detail::fft::unpackRealForward(zr, zi, out, halfSize_, splitRe_.data(), splitIm_.data());
1515 }
1516
1519 void packInverseConj(const T* in, T* zr, T* zi) const noexcept
1520 {
1521#if defined(DSPARK_SIMD_DISPATCH_AVX2)
1523 {
1524 detail::fft::avx2::packRealSpectrum(in, zr, zi, halfSize_, splitRe_.data(), splitIm_.data());
1525 return;
1526 }
1527#endif
1528 detail::fft::packRealSpectrum(in, zr, zi, halfSize_, splitRe_.data(), splitIm_.data());
1529 }
1530
1532 [[nodiscard]] T inverseScale() const noexcept { return T(1) / static_cast<T>(halfSize_); }
1533
1543 [[nodiscard]] static constexpr size_t lanesFor(size_t size, int wide, int narrow) noexcept
1544 {
1545 const auto fits = [size](int width) noexcept {
1546 const size_t lanes = static_cast<size_t>(width);
1547 return width > 1 && size >= 8 * lanes && 2 * size >= lanes * lanes * lanes;
1548 };
1549 return fits(wide) ? static_cast<size_t>(wide) : fits(narrow) ? static_cast<size_t>(narrow) : size_t{ 0 };
1550 }
1551
1553 [[nodiscard]] static constexpr size_t baseLanes(size_t size) noexcept
1554 {
1555 return lanesFor(size, detail::fft::kWide<T>, detail::fft::kNarrow<T>);
1556 }
1557
1560 [[nodiscard]] static constexpr size_t dispatchedLanes(size_t size) noexcept
1561 {
1562#if defined(DSPARK_SIMD_DISPATCH_AVX2)
1563 return lanesFor(size, detail::fft::avx2::kWide<T>, detail::fft::avx2::kNarrow<T>);
1564#else
1565 return baseLanes(size);
1566#endif
1567 }
1568
1570 [[nodiscard]] static detail::fft::SplitFFT<T> makeEngine(size_t size)
1571 {
1572 if (baseLanes(size) != 0)
1573 return detail::fft::SplitFFT<T>();
1574 return detail::fft::SplitFFT<T>(size / 2, true);
1575 }
1576
1579 [[nodiscard]] const detail::fft::RealLaneTables<T>& dispatchedTables() const noexcept
1580 {
1581 return wideLanes_.empty() ? lanes_ : wideLanes_;
1582 }
1583
1584 size_t realSize_;
1585 size_t halfSize_;
1586 detail::fft::SplitFFT<T> engine_;
1587 std::vector<T> splitRe_, splitIm_;
1588 detail::fft::WorkBuffers<T> laneWork_;
1589 detail::fft::RealLaneTables<T> lanes_;
1590 detail::fft::RealLaneTables<T> wideLanes_;
1591 bool stepDispatched_ = false;
1592};
1593
1594} // namespace dspark
Complex FFT on interleaved data (Stockham radix-4 engine, SIMD).
Definition FFT.h:1094
FFTComplex(size_t size)
Constructs an FFT processor for the given size.
Definition FFT.h:1102
size_t getSize() const noexcept
Returns the FFT size (number of complex points).
Definition FFT.h:1111
static size_t allocationBytes(size_t size) noexcept
Upper bound of the heap bytes a constructor of this size requests, excluding the object itself,...
Definition FFT.h:1123
void forward(T *data) noexcept
Performs a forward (time->frequency) FFT in-place.
Definition FFT.h:1135
void inverse(T *data) noexcept
Performs an inverse (frequency->time) FFT in-place.
Definition FFT.h:1142
FFT optimised for real-valued input signals (the common audio case).
Definition FFT.h:1197
static size_t frequencyToBin(double frequency, double sampleRate, size_t fftSize) noexcept
Returns the bin index closest to a given frequency.
Definition FFT.h:1443
static size_t allocationBytes(size_t size) noexcept
Upper bound of the heap bytes a constructor of this size requests, excluding the object itself,...
Definition FFT.h:1243
void computePhases(const T *freqData, T *phases) const noexcept
Computes the phase angle of each frequency bin.
Definition FFT.h:1399
size_t getNumBins() const noexcept
Returns the number of frequency bins (N/2 + 1) including DC and Nyquist.
Definition FFT.h:1231
void computePowerSpectrum(const T *freqData, T *power) const noexcept
Computes the power spectrum (magnitude squared) of each bin.
Definition FFT.h:1414
int getInverseStepCount() const noexcept
Returns the number of steps inverseStep() splits inverse() into (the same for the life of the object)...
Definition FFT.h:1322
void forward(const T *timeData, T *freqData) noexcept
Forward transform: real time-domain -> complex frequency-domain.
Definition FFT.h:1269
FFTReal(size_t size)
Constructs a real FFT processor.
Definition FFT.h:1205
void inverse(const T *freqData, T *timeData) noexcept
Inverse transform: complex frequency-domain -> real time-domain.
Definition FFT.h:1297
size_t getFrequencyDomainSize() const noexcept
Returns the frequency-domain buffer size in elements (N + 2).
Definition FFT.h:1228
static T binToFrequency(size_t binIndex, double sampleRate, size_t fftSize) noexcept
Returns the frequency in Hz corresponding to a given bin index.
Definition FFT.h:1431
size_t getSize() const noexcept
Returns the number of real input samples (N).
Definition FFT.h:1225
void inverseStep(int step, const T *freqData, T *timeData) noexcept
Runs one step of inverse(), for callers that spread a large transform over several calls (a real-time...
Definition FFT.h:1345
void computeMagnitudes(const T *freqData, T *magnitudes) const noexcept
Computes the magnitude of each frequency bin.
Definition FFT.h:1384
The tables of a real transform by lanes (detail/FftKernels.inl) for one lane count,...
Definition FFT.h:942
RealPlan< T > plan() const noexcept
Definition FFT.h:1019
bool empty() const noexcept
Definition FFT.h:1014
RealLaneTables(size_t size, size_t lanes, size_t wholeReach, size_t halvesReach)
Builds the tables of a transform of size real samples.
Definition FFT.h:958
size_t passes() const noexcept
Passes of each lane's transform (0 when empty).
Definition FFT.h:1017
static constexpr size_t valueCount(size_t size, size_t lanes, size_t wholeReach) noexcept
Values the constructor requests (one allocation).
Definition FFT.h:1005
The shared Stockham engine: a forward complex FFT of size N on split (re, im) arrays,...
Definition FFT.h:453
static size_t allocationBytes(size_t n, bool interleavedInput) noexcept
Upper bound of the bytes the constructor requests for a power-of-two size n: the two work buffers (on...
Definition FFT.h:536
void runScaledSwap(const T *re, const T *im, const T *gain, size_t mask, const T *&outRe, const T *&outIm) noexcept
Transforms the spectrum product whose bin k is (im[k & mask] gain[k], re[k & mask] gain[k]),...
Definition FFT.h:643
T * inRe() noexcept
Input buffers: fill inRe() / inIm() with N values each before run().
Definition FFT.h:550
void runFrom(const T *data, bool conjugate, const T *&outRe, const T *&outIm) noexcept
Transforms interleaved (re, im) pairs, conjugated if asked, without first copying them into the input...
Definition FFT.h:580
void runScaledSwapFolded(const T *re, const T *im, const T *gain, size_t fold, const T *&outRe, const T *&outIm) noexcept
runScaledSwap() of a spectrum fold times this engine's size, folded: bin k sums the products of bins ...
Definition FFT.h:680
size_t runToStepCount() const noexcept
Definition FFT.h:745
SplitFFT(size_t n, bool interleavedInput=false)
Definition FFT.h:459
void run(const T *&outRe, const T *&outIm) noexcept
Runs the forward transform of the input buffers.
Definition FFT.h:558
T * inIm() noexcept
Definition FFT.h:551
static constexpr size_t kAllocationOverhead
Definition FFT.h:528
void runFromSplit(const T *re, const T *im, const T *&outRe, const T *&outIm) noexcept
Transforms N values read from two arrays of real and imaginary parts, without first copying them into...
Definition FFT.h:612
void runToStep(size_t step, T *out, T scaleRe, T scaleIm) noexcept
Runs one step of runTo(): step s below runToStepCount() - 1 is pass s, the last step the closing pass...
Definition FFT.h:759
size_t size() const noexcept
Definition FFT.h:547
void runFromTo(const T *data, bool conjugate, T *out, T scaleRe, T scaleIm) noexcept
Definition FFT.h:724
void runTo(T *out, T scaleRe, T scaleIm) noexcept
Transforms the input buffers and writes interleaved output directly.
Definition FFT.h:701
Two buffers of the same length in one allocation, each starting on a 64-byte boundary,...
Definition FFT.h:382
WorkBuffers(WorkBuffers &&other) noexcept
Definition FFT.h:399
const T * first() const noexcept
Definition FFT.h:414
WorkBuffers(const WorkBuffers &other)
Definition FFT.h:392
WorkBuffers & operator=(WorkBuffers other) noexcept
Definition FFT.h:405
WorkBuffers(size_t count)
Two zeroed buffers of count values each.
Definition FFT.h:387
static constexpr size_t values(size_t count) noexcept
Definition FFT.h:421
const T * second() const noexcept
Definition FFT.h:416
constexpr bool kTwiddledRadix8
Definition FFT.h:202
constexpr size_t kRealWholeTwiddleReach
Definition FFT.h:222
constexpr size_t kRealHalvesReach
Definition FFT.h:234
constexpr size_t kTwiddled8RowReach
Definition FFT.h:210
constexpr size_t kClosingRowReach
Definition FFT.h:187
constexpr int kNarrow
Definition FFT.h:334
simd::Vec< T, W > Vec
Definition FFT.h:332
constexpr int kWide
Definition FFT.h:333
constexpr size_t kBlock
Definition FFT.h:329
bool isRuntimeDispatchActive() noexcept
Whether the AVX2+FMA kernels are selected now (see setRuntimeDispatch()).
Definition SimdOps.h:262
Main namespace for the DSPark framework.
The two ping-pong buffers, [re | im] each: x is read, y is written.
Definition FFT.h:238
Where a closing pass into interleaved output writes, and its scales.
Definition FFT.h:254
static constexpr size_t repeatFor(size_t m, size_t lanes, size_t wholeReach) noexcept
Definition FFT.h:284
bool halves
The joining step works by halves, its tables in the order of laneBin().
Definition FFT.h:264
bool first8
The first pass takes eight samples (log2 n odd), not four.
Definition FFT.h:263
const T * turn
The factors of the step that joins the lanes.
Definition FFT.h:267
const T * edge
The factors of the lanes' middle bin.
Definition FFT.h:268
size_t wholeReach
Passes joining spectra of up to this many points hold each twiddle lanes times.
Definition FFT.h:265
static constexpr size_t passCount(size_t n) noexcept
Definition FFT.h:312
constexpr size_t firstLength() const noexcept
Points of the spectra the first pass leaves.
Definition FFT.h:272
size_t lanes
Lanes: the transform has n * lanes samples.
Definition FFT.h:262
static constexpr size_t passValues(size_t m, size_t repeat) noexcept
Definition FFT.h:277
const T * twiddles
The pass tables, by rising length of the spectra they join.
Definition FFT.h:266
T scale
1 / (n * lanes), for the inverse.
Definition FFT.h:269
static constexpr bool halvesFor(size_t size, size_t lanes, size_t reach) noexcept
Definition FFT.h:292
size_t n
Points of each lane's transform.
Definition FFT.h:261
static constexpr size_t laneBin(size_t lanes, size_t p, bool byHalves) noexcept
Definition FFT.h:302
static constexpr size_t index(size_t stride, size_t block, size_t row, size_t p) noexcept
Definition FFT.h:162
static constexpr size_t firstBlock(size_t n1, size_t block) noexcept
Definition FFT.h:153