Algorithms
This page goes from the mathematics of the DFT to what FFTL runs. Each transform in the library is built from a few pieces, and the same pieces appear in the complex, real and real-to-real paths.
Stockham chains
A transform of length
no bit reversal or digit reversal at the start or the end,
each stage reads and writes with regular strides,
the radix sequence
is a single object that the plan carries.
The radices are chosen by a fixed rule (src/factor.jl). The rule is the result of exhaustive searches over the decompositions of many lengths on the development machines, not of a runtime search. It prefers radix 4 and radix 8 for powers of two. On aarch64 it merges a factor 3 with a 4 into a 12; the x86 rules differ and are in the same file. Every other prime up to 73 is a radix of its own. Larger primes are not radices; the part of the length they make up is resampled onto a smooth grid (below).
Codelets
A stage of radix src/codegen.jl). The generator writes the DFT as a sum of terms and simplifies it:
multiplications by
are rotations of the term, not multiplications; two terms with rotations that differ by an odd power of combine in one complex add-and-rotate; a composite radix
is split in registers with the usual index mapping;an odd prime uses the symmetric form:
with the cosine sumsand the sine sums , which pairs the outputs and .
The generated code runs on complex SIMD vectors (src/complexvec.jl), with the vector width of the element type. On aarch64 the complex add-and-rotate and the complex multiply are single instructions (FCADD, FCMLA).
The prime radices from 17 to 73 use the same symmetric form, but as loops: one kernel (src/primestage.jl) takes the radix and a table of the cosines and sines as arguments. Straight-line code for such a radix has about
Lengths with large prime factors: resampling
Lengths with a prime factor above 73 are transformed by oversampled interpolation onto a smooth grid (a type-2 nonuniform FFT evaluated at the points src/resample.jl):
the input, divided by the Fourier transform of an interpolation kernel
, is zero-padded to a smooth length ,one transform of length
, output
is the sum of the grid values around , weighted by (the exponential of a semicircle).
The result is not exact: the truncation error of the kernel is about Float64, 8 in Float32. In floating point a second term dominates in Float64: step 1 multiplies the input by up to Float64 (Bluestein's algorithm 2–4 Float32, where resampling is more accurate than Bluestein's algorithm (2–4 float64_large_primes = "bluestein" (FFTL.FLOAT64_LARGE_PRIMES) selects Bluestein's algorithm for every Float64 transform of such a length.
The weights of every output are tabulated up to a table size (1 MiB in Float64, 4 MiB in Float32, where the two measured about even), above it they are evaluated from piecewise polynomials. Generic number types (BigFloat and others, src/generic.jl) keep Bluestein's algorithm, which is exact: there the width grows with the precision and the interpolation would cost more than the transform it saves. Float64 on aarch64 keeps it too for single lines (see the design decisions): with two values per NEON register the 16-tap interpolation costs more than the second transform.
Compared with Bluestein's algorithm, which needs two transforms of a length
The grid length is a smooth number. On aarch64 it is the smallest 5-smooth grid_length in src/plan.jl).
A large grid on one thread is transformed by a four-step: with 64-byte vectors (AVX-512) from the length a single line would take one (serial_fourstep), on other x86 machines from 8 MiB (Float64), on aarch64 never (grid_fourstep in src/plan.jl). The four-step then runs on the calling thread instead of a chain.
Lengths with a smooth part
A length
A chain of length
over the interleaved lines (the inner sum), multiplication by the twiddle factors
, folded into the deconvolution of the next step, resampled transforms of length (the outer sum), each written with stride .
From 32 lanes on, the
With fewer than 32 lanes each lane is one resampled transform of its own, vectorised along the line. A batched pass vectorises across the lanes, so few lanes, or a lane count that is not a multiple of the vector width, leave most of the vector unused: on an EPYC 9354 (AVX-512, one thread, Float64 (2.1–7.3 in Float32), 1.1–1.6 (1.6–2.9) at RS_BATCH_LANES in src/resample.jl). The rule depends on the length only, not on the number of threads, so a threaded plan computes the same values as a serial one.
When composite_split in src/plan.jl; the threshold was measured with Bluestein's algorithm in place of resampling).
Multidimensional transforms without transposition
A transform along dimension
Long one-dimensional transforms: four-step
A single long line with a smooth length Float32 for powers of two only):
transforms of length over the columns, multiplication by the twiddle factors
, transforms of length over the rows.
Each of the two transform steps is split over the threads, with a barrier in between. The factorisation fourstep_split).
Both steps work on blocks of columns, and each block passes through a buffer in cache. The twiddle multiplication and the transposition into the intermediate array happen in registers, a square of vectors at a time. A block's rows are at least 128 bytes (256 on one thread), so Float32 takes twice the columns of Float64. With 64-byte vectors (AVX-512), where it was measured, the first step also copies the block's rows into the buffer before transforming it, so no butterfly stage reads the input with its row stride (for a power of two a large power of two, whose rows share cache sets); the intermediate array is written with non-temporal stores (the second step reads it on other threads, and ordinary stores would first have to take ownership of lines that another core still holds), and the second step writes a large output through the buffer with non-temporal stores as well. On an Apple M1 Max the copy cost 5–15 %.
On one thread the four-step pays only well beyond the cache: a chain makes one pass over memory per stage, the four-step two, but its work in cache costs about as much as a dozen passes of a chain at full bandwidth. A chain of a large power of two loses bandwidth to its strides, which is where the four-step gains most.
Real transforms
For even
The pairs c2r) runs the same steps backwards.
Real-to-real transforms
Each kind is computed from a real or complex DFT of a rearranged input. Examples:
DCT-II. The even-indexed inputs go forward and the odd-indexed ones backward:
. A real DFT of , multiplied by , gives the DCT-II. This is the reordering that makes the DCT cost one real DFT of length .DST-II and DST-IV are DCTs of the input with alternating signs, read in reverse order.
DCT-I and DST-I extend the input to an even or odd sequence of length
or and take one real DFT of that length.DHT is the real DFT with the real and imaginary parts combined:
.
The formulas are in the header comment of src/r2r.jl. Their inverses and scale factors are listed on the cosine, sine and Hartley page.
Passes
plan_pass applies one 1D transform to all lines of a strided array with the same kernel. It lets a library assemble its own multidimensional transform one dimension at a time. NFFT.jl transforms its oversampled grid this way; it skips the boxes of the grid that it does not need itself, so the passes themselves carry no pruning.