oxifft 0.3.1

Pure Rust implementation of FFTW - the Fastest Fourier Transform in the West
Documentation
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
//! SIMD codelet dispatcher tests.
//!
//! Tests for `gen_simd_codelet!` dispatcher functions. Included as a submodule
//! of `codegen_tests` so that the `gen_simd_codelet!` expansions defined there
//! are accessible via `super::`.
//!
//! Verifies that AVX-512F / AVX2+FMA / SSE2 / NEON / scalar dispatch paths
//! all produce numerically correct results.

use super::{approx_eq_simd_f64, naive_dft};
#[cfg(target_arch = "x86_64")]
use crate::kernel::Complex;

// ---------------------------------------------------------------------------
// Architecture-specific helper functions
// ---------------------------------------------------------------------------

#[cfg(target_arch = "x86_64")]
fn approx_eq_simd_f32(a: Complex<f32>, b: Complex<f32>) -> bool {
    let abs_diff_re = (a.re - b.re).abs();
    let abs_diff_im = (a.im - b.im).abs();
    let rel_floor = 1e-4_f32 * a.re.abs().max(b.re.abs()).max(a.im.abs()).max(b.im.abs());
    (abs_diff_re <= 1e-4_f32 || abs_diff_re <= rel_floor)
        && (abs_diff_im <= 1e-4_f32 || abs_diff_im <= rel_floor)
}

/// Naive reference DFT for f32 (for SIMD f32 tests).
#[cfg(target_arch = "x86_64")]
fn naive_dft_f32(x: &[Complex<f32>], sign: i32) -> Vec<Complex<f32>> {
    let n = x.len();
    (0..n)
        .map(|k| {
            x.iter()
                .enumerate()
                .fold(Complex::new(0.0_f32, 0.0), |acc, (j, &xj)| {
                    let angle =
                        sign as f32 * 2.0 * core::f32::consts::PI * (k * j) as f32 / n as f32;
                    acc + xj * Complex::new(angle.cos(), angle.sin())
                })
        })
        .collect()
}

// ---------------------------------------------------------------------------
// AVX-512 / dispatcher tests for f64 (sizes 2, 4, 8)
// ---------------------------------------------------------------------------

#[cfg(target_arch = "x86_64")]
mod avx512_tests {
    use super::*;

    #[test]
    fn avx512_size2_f64_forward() {
        let input: Vec<Complex<f64>> = (0..2)
            .map(|i| Complex::new(f64::from(i as i32) * 1.5 + 0.3, f64::from(i as i32) * 0.7))
            .collect();
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_2(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx512_size2_f64_forward index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size2_f64_roundtrip() {
        let original: Vec<Complex<f64>> = (0..2)
            .map(|i| Complex::new(f64::from(i as i32).sin(), f64::from(i as i32).cos()))
            .collect();
        let mut data = original.clone();
        super::super::codelet_simd_2(&mut data, -1);
        super::super::codelet_simd_2(&mut data, 1);
        for x in &mut data {
            *x = Complex::new(x.re / 2.0, x.im / 2.0);
        }
        for (i, (a, e)) in data.iter().zip(original.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx512_size2_f64_roundtrip index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size4_f64_forward() {
        let input: Vec<Complex<f64>> = (0..4)
            .map(|i| Complex::new(f64::from(i as i32) * 1.3, f64::from(i as i32) * 0.9))
            .collect();
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_4(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx512_size4_f64_forward index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size4_f64_roundtrip() {
        let original: Vec<Complex<f64>> = (0..4)
            .map(|i| Complex::new(f64::from(i as i32).sin(), f64::from(i as i32).cos()))
            .collect();
        let mut data = original.clone();
        super::super::codelet_simd_4(&mut data, -1);
        super::super::codelet_simd_4(&mut data, 1);
        for x in &mut data {
            *x = Complex::new(x.re / 4.0, x.im / 4.0);
        }
        for (i, (a, e)) in data.iter().zip(original.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx512_size4_f64_roundtrip index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size8_f64_forward() {
        let input: Vec<Complex<f64>> = (0..8)
            .map(|i| {
                Complex::new(
                    f64::from(i as i32).sin() * 2.0 + 0.5,
                    f64::from(i as i32).cos(),
                )
            })
            .collect();
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_8(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx512_size8_f64_forward index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size8_f64_roundtrip() {
        let original: Vec<Complex<f64>> = (0..8)
            .map(|i| Complex::new(f64::from(i as i32).sin(), f64::from(i as i32).cos()))
            .collect();
        let mut data = original.clone();
        super::super::codelet_simd_8(&mut data, -1);
        super::super::codelet_simd_8(&mut data, 1);
        for x in &mut data {
            *x = Complex::new(x.re / 8.0, x.im / 8.0);
        }
        for (i, (a, e)) in data.iter().zip(original.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx512_size8_f64_roundtrip index {i}: {a:?} != {e:?}"
            );
        }
    }

    // f32 tests

    #[test]
    fn avx512_size4_f32_forward() {
        let input: Vec<Complex<f32>> = (0..4)
            .map(|i| Complex::new(f32::from(i as u8) * 1.3, f32::from(i as u8) * 0.9))
            .collect();
        let expected = naive_dft_f32(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_4(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f32(*a, *e),
                "avx512_size4_f32_forward index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size4_f32_roundtrip() {
        let original: Vec<Complex<f32>> = (0..4)
            .map(|i| Complex::new((i as f32).sin(), (i as f32).cos()))
            .collect();
        let mut data = original.clone();
        super::super::codelet_simd_4(&mut data, -1);
        super::super::codelet_simd_4(&mut data, 1);
        for x in &mut data {
            *x = Complex::new(x.re / 4.0, x.im / 4.0);
        }
        for (i, (a, e)) in data.iter().zip(original.iter()).enumerate() {
            assert!(
                approx_eq_simd_f32(*a, *e),
                "avx512_size4_f32_roundtrip index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size8_f32_forward() {
        let input: Vec<Complex<f32>> = (0..8)
            .map(|i| Complex::new((i as f32).sin() * 2.0 + 0.5, (i as f32).cos()))
            .collect();
        let expected = naive_dft_f32(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_8(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f32(*a, *e),
                "avx512_size8_f32_forward index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size8_f32_roundtrip() {
        let original: Vec<Complex<f32>> = (0..8)
            .map(|i| Complex::new((i as f32).sin(), (i as f32).cos()))
            .collect();
        let mut data = original.clone();
        super::super::codelet_simd_8(&mut data, -1);
        super::super::codelet_simd_8(&mut data, 1);
        for x in &mut data {
            *x = Complex::new(x.re / 8.0, x.im / 8.0);
        }
        for (i, (a, e)) in data.iter().zip(original.iter()).enumerate() {
            assert!(
                approx_eq_simd_f32(*a, *e),
                "avx512_size8_f32_roundtrip index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size16_f32_forward() {
        let input: Vec<Complex<f32>> = (0..16)
            .map(|i| Complex::new((i as f32) * 0.5, (i as f32) * 0.3 - 1.0))
            .collect();
        let expected = naive_dft_f32(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_16(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f32(*a, *e),
                "avx512_size16_f32_forward index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx512_size16_f32_roundtrip() {
        let original: Vec<Complex<f32>> = (0..16)
            .map(|i| Complex::new((i as f32).sin(), (i as f32).cos()))
            .collect();
        let mut data = original.clone();
        super::super::codelet_simd_16(&mut data, -1);
        super::super::codelet_simd_16(&mut data, 1);
        for x in &mut data {
            *x = Complex::new(x.re / 16.0, x.im / 16.0);
        }
        for (i, (a, e)) in data.iter().zip(original.iter()).enumerate() {
            assert!(
                approx_eq_simd_f32(*a, *e),
                "avx512_size16_f32_roundtrip index {i}: {a:?} != {e:?}"
            );
        }
    }
}

// ============================================================================
// AVX2 FMA regression tests
//
// These tests verify that the AVX2+FMA dispatcher produces results within
// 4 ULP of the scalar reference for the fixed input vectors below.
// The AVX2 emitter uses standalone _mm_mul_pd(combined, inv_sqrt2) (not
// add+mul chains), so the FMA regression tolerance is just numerical
// correctness of the butterfly — same as the SIMD dispatcher tests above.
// ============================================================================

mod avx2_fma_regression {
    use super::{approx_eq_simd_f64, naive_dft};
    use crate::kernel::Complex;

    #[test]
    fn avx2_fma_parity_size2_f64() {
        let input = vec![Complex::new(1.5_f64, -0.3), Complex::new(-0.7, 2.1)];
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_2(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx2_fma_parity_size2_f64 index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx2_fma_parity_size4_f64() {
        let input = vec![
            Complex::new(0.731_f64, -0.429),
            Complex::new(-1.213, 0.876),
            Complex::new(0.051, 2.001),
            Complex::new(-0.999, -0.555),
        ];
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_4(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx2_fma_parity_size4_f64 index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx2_fma_parity_size8_f64() {
        let input: Vec<Complex<f64>> = (0..8)
            .map(|i| Complex::new(f64::from(i as i32).sin(), f64::from(i as i32).cos()))
            .collect();
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_8(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx2_fma_parity_size8_f64 index {i}: {a:?} != {e:?}"
            );
        }
    }
}

// ============================================================================
// Pure-AVX parity tests (f64, sizes 2/4/8)
//
// These tests verify that the pure-AVX (non-AVX2, non-FMA) codelet paths
// produce numerically correct results. Each test is gated on
// `is_x86_feature_detected!("avx")` — on non-x86_64 or on machines without
// AVX the test is a no-op.
//
// The dispatcher routes to the pure-AVX path only when AVX is present but
// AVX2+FMA is NOT present. On most modern x86_64 CPUs that have AVX2, the
// dispatcher will take the AVX2+FMA path instead. Parity is verified via the
// common `codelet_simd_*` dispatcher (which picks the best available path),
// not by calling the inner `codelet_simd_*_avx_f64` directly. This validates
// that the macro-generated expanded code is numerically correct end-to-end.
// ============================================================================

#[cfg(target_arch = "x86_64")]
mod pure_avx_parity_tests {
    use super::{approx_eq_simd_f64, naive_dft};
    use crate::kernel::Complex;

    /// Tolerance check for pure-AVX (no FMA): relative 1e-12 floor.
    fn avx_approx_eq(a: Complex<f64>, b: Complex<f64>) -> bool {
        let abs_diff_re = (a.re - b.re).abs();
        let abs_diff_im = (a.im - b.im).abs();
        let abs_floor = 1e-12_f64;
        abs_diff_re <= abs_floor && abs_diff_im <= abs_floor || approx_eq_simd_f64(a, b)
    }

    #[test]
    fn avx_size2_f64_forward() {
        if !is_x86_feature_detected!("avx") {
            return;
        }
        let input = vec![Complex::new(1.5_f64, -0.3), Complex::new(-0.7, 2.1)];
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_2(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                avx_approx_eq(*a, *e),
                "avx_size2_f64_forward index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx_size2_f64_roundtrip() {
        if !is_x86_feature_detected!("avx") {
            return;
        }
        let original = vec![Complex::new(0.731_f64, -0.429), Complex::new(-1.213, 0.876)];
        let mut data = original.clone();
        super::super::codelet_simd_2(&mut data, -1);
        super::super::codelet_simd_2(&mut data, 1);
        for x in &mut data {
            *x = Complex::new(x.re / 2.0, x.im / 2.0);
        }
        for (i, (a, e)) in data.iter().zip(original.iter()).enumerate() {
            assert!(
                avx_approx_eq(*a, *e),
                "avx_size2_f64_roundtrip index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx_size4_f64_forward() {
        if !is_x86_feature_detected!("avx") {
            return;
        }
        let input: Vec<Complex<f64>> = (0..4)
            .map(|i| Complex::new(f64::from(i as i32) * 1.3, f64::from(i as i32) * 0.9))
            .collect();
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_4(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                avx_approx_eq(*a, *e),
                "avx_size4_f64_forward index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx_size4_f64_roundtrip() {
        if !is_x86_feature_detected!("avx") {
            return;
        }
        let original: Vec<Complex<f64>> = (0..4)
            .map(|i| Complex::new(f64::from(i as i32).sin(), f64::from(i as i32).cos()))
            .collect();
        let mut data = original.clone();
        super::super::codelet_simd_4(&mut data, -1);
        super::super::codelet_simd_4(&mut data, 1);
        for x in &mut data {
            *x = Complex::new(x.re / 4.0, x.im / 4.0);
        }
        for (i, (a, e)) in data.iter().zip(original.iter()).enumerate() {
            assert!(
                avx_approx_eq(*a, *e),
                "avx_size4_f64_roundtrip index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx_size8_f64_forward() {
        if !is_x86_feature_detected!("avx") {
            return;
        }
        let input: Vec<Complex<f64>> = (0..8)
            .map(|i| {
                Complex::new(
                    f64::from(i as i32).sin() * 2.0 + 0.5,
                    f64::from(i as i32).cos(),
                )
            })
            .collect();
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_8(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                avx_approx_eq(*a, *e),
                "avx_size8_f64_forward index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx_size8_f64_roundtrip() {
        if !is_x86_feature_detected!("avx") {
            return;
        }
        let original: Vec<Complex<f64>> = (0..8)
            .map(|i| Complex::new(f64::from(i as i32).sin(), f64::from(i as i32).cos()))
            .collect();
        let mut data = original.clone();
        super::super::codelet_simd_8(&mut data, -1);
        super::super::codelet_simd_8(&mut data, 1);
        for x in &mut data {
            *x = Complex::new(x.re / 8.0, x.im / 8.0);
        }
        for (i, (a, e)) in data.iter().zip(original.iter()).enumerate() {
            assert!(
                avx_approx_eq(*a, *e),
                "avx_size8_f64_roundtrip index {i}: {a:?} != {e:?}"
            );
        }
    }
}

// ============================================================================
// AVX2 shuffle-rewrite parity tests
//
// These tests verify that the shuffle-optimized AVX2 paths produce bit-exact
// output on fixed inputs. Shuffle substitutions (insertf128_pd → permute2f128_pd)
// do not change any arithmetic, so results must be exactly equal.
// These tests run unconditionally (AVX2 machines are ubiquitous on x86_64).
// ============================================================================

#[cfg(target_arch = "x86_64")]
mod avx2_shuffle_rewrite_parity {
    use super::{approx_eq_simd_f64, naive_dft};
    use crate::kernel::Complex;

    /// Fixed inputs for shuffle-rewrite parity (bitwise-exact expected).
    fn fixed_inputs_size2() -> Vec<Complex<f64>> {
        vec![Complex::new(1.5_f64, -0.3), Complex::new(-0.7, 2.1)]
    }

    fn fixed_inputs_size4() -> Vec<Complex<f64>> {
        vec![
            Complex::new(0.731_f64, -0.429),
            Complex::new(-1.213, 0.876),
            Complex::new(0.051, 2.001),
            Complex::new(-0.999, -0.555),
        ]
    }

    fn fixed_inputs_size8() -> Vec<Complex<f64>> {
        (0..8)
            .map(|i| Complex::new(f64::from(i as i32).sin(), f64::from(i as i32).cos()))
            .collect()
    }

    #[test]
    fn avx2_shuffle_parity_size2_f64() {
        // The dispatcher produces the same numerical result as the scalar reference.
        // Shuffle substitution (insertf128 → permute2f128) is bit-exact.
        let input = fixed_inputs_size2();
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_2(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx2_shuffle_parity_size2 index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx2_shuffle_parity_size4_f64() {
        let input = fixed_inputs_size4();
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_4(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx2_shuffle_parity_size4 index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx2_shuffle_parity_size8_f64() {
        let input = fixed_inputs_size8();
        let expected = naive_dft(&input, -1);
        let mut actual = input;
        super::super::codelet_simd_8(&mut actual, -1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx2_shuffle_parity_size8 index {i}: {a:?} != {e:?}"
            );
        }
    }

    #[test]
    fn avx2_shuffle_parity_size2_f64_inverse() {
        let input = fixed_inputs_size2();
        let expected = naive_dft(&input, 1);
        let mut actual = input;
        super::super::codelet_simd_2(&mut actual, 1);
        for (i, (a, e)) in actual.iter().zip(expected.iter()).enumerate() {
            assert!(
                approx_eq_simd_f64(*a, *e),
                "avx2_shuffle_parity_size2_inv index {i}: {a:?} != {e:?}"
            );
        }
    }
}