archmage 0.9.14

Safely invoke your intrinsic power, using the tokens granted to you by the CPU. Cast primitive magics faster than any mage alive.
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
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
//! Brute force accuracy tests for transcendental functions
//!
//! Tests all transcendental functions against std:: implementations
//! across their valid input ranges, including edge cases.

#![cfg(target_arch = "x86_64")]

use archmage::{SimdToken, X64V3Token, arcane};
use magetypes::simd::f32x8;

// ============================================================================
// Test wrappers using #[arcane]
// ============================================================================

#[arcane]
fn simd_cbrt_lowp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).cbrt_lowp().to_array()
}

#[arcane]
fn simd_cbrt_midp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).cbrt_midp().to_array()
}

#[arcane]
fn simd_pow_lowp(token: X64V3Token, input: &[f32; 8], n: f32) -> [f32; 8] {
    f32x8::load(token, input).pow_lowp(n).to_array()
}

#[arcane]
fn simd_pow_midp(token: X64V3Token, input: &[f32; 8], n: f32) -> [f32; 8] {
    f32x8::load(token, input).pow_midp(n).to_array()
}

#[arcane]
fn simd_exp2_lowp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).exp2_lowp().to_array()
}

#[arcane]
fn simd_exp2_midp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).exp2_midp().to_array()
}

#[arcane]
fn simd_log2_lowp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).log2_lowp().to_array()
}

#[arcane]
fn simd_log2_midp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).log2_midp().to_array()
}

#[arcane]
fn simd_ln_lowp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).ln_lowp().to_array()
}

#[arcane]
fn simd_ln_midp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).ln_midp().to_array()
}

#[arcane]
fn simd_exp_lowp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).exp_lowp().to_array()
}

#[arcane]
fn simd_exp_midp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).exp_midp().to_array()
}

#[arcane]
fn simd_log10_lowp(token: X64V3Token, input: &[f32; 8]) -> [f32; 8] {
    f32x8::load(token, input).log10_lowp().to_array()
}

// ============================================================================
// Test infrastructure
// ============================================================================

struct AccuracyStats {
    name: &'static str,
    max_rel_err: f32,
    max_abs_err: f32,
    avg_rel_err: f64,
    worst_input: f32,
    worst_expected: f32,
    worst_got: f32,
    total_tested: usize,
    nan_count: usize,
    inf_count: usize,
}

impl AccuracyStats {
    fn new(name: &'static str) -> Self {
        Self {
            name,
            max_rel_err: 0.0,
            max_abs_err: 0.0,
            avg_rel_err: 0.0,
            worst_input: 0.0,
            worst_expected: 0.0,
            worst_got: 0.0,
            total_tested: 0,
            nan_count: 0,
            inf_count: 0,
        }
    }

    fn update(&mut self, input: f32, expected: f32, got: f32) {
        if !expected.is_finite() || !got.is_finite() {
            if got.is_nan() && !expected.is_nan() {
                self.nan_count += 1;
            }
            if got.is_infinite() && !expected.is_infinite() {
                self.inf_count += 1;
            }
            return;
        }

        self.total_tested += 1;
        let abs_err = (got - expected).abs();
        let rel_err = if expected.abs() > 1e-10 {
            abs_err / expected.abs()
        } else {
            abs_err
        };

        self.avg_rel_err += rel_err as f64;

        if rel_err > self.max_rel_err {
            self.max_rel_err = rel_err;
            self.max_abs_err = abs_err;
            self.worst_input = input;
            self.worst_expected = expected;
            self.worst_got = got;
        }
    }

    fn finalize(&mut self) {
        if self.total_tested > 0 {
            self.avg_rel_err /= self.total_tested as f64;
        }
    }

    fn print(&self) {
        println!(
            "{:20} max_rel_err: {:.2e}  avg_rel_err: {:.2e}  tested: {}",
            self.name, self.max_rel_err, self.avg_rel_err, self.total_tested
        );
        if self.max_rel_err > 1e-5 {
            println!(
                "    worst: input={:.8e} expected={:.8e} got={:.8e}",
                self.worst_input, self.worst_expected, self.worst_got
            );
        }
        if self.nan_count > 0 || self.inf_count > 0 {
            println!(
                "    ERRORS: {} unexpected NaN, {} unexpected Inf",
                self.nan_count, self.inf_count
            );
        }
    }

    fn assert_max_rel_err(&self, max_allowed: f32) {
        assert!(
            self.max_rel_err <= max_allowed,
            "{}: max_rel_err {:.2e} exceeds limit {:.2e} at input={:.8e}",
            self.name,
            self.max_rel_err,
            max_allowed,
            self.worst_input
        );
        assert!(
            self.nan_count == 0,
            "{}: {} unexpected NaN values",
            self.name,
            self.nan_count
        );
    }
}

// ============================================================================
// ULP helpers
// ============================================================================

/// Compute ULP distance between two f32 values.
/// Returns None if either is NaN.
fn ulp_distance(a: f32, b: f32) -> Option<u32> {
    if a.is_nan() || b.is_nan() {
        return None;
    }
    if a == b {
        return Some(0);
    }
    // Convert sign-magnitude to two's complement-like ordering for ULP distance
    let ai = a.to_bits() as i32;
    let bi = b.to_bits() as i32;
    let ai = if ai < 0 { i32::MIN - ai } else { ai };
    let bi = if bi < 0 { i32::MIN - bi } else { bi };
    Some((ai - bi).unsigned_abs())
}

struct UlpStats {
    name: &'static str,
    max_ulp: u32,
    max_ulp_input: f32,
    max_ulp_expected: f32,
    max_ulp_got: f32,
    total_tested: usize,
    nan_count: usize,
    inf_count: usize,
    ulp_histogram: [usize; 8], // [0 ulp, 1 ulp, 2 ulp, 3 ulp, 4-7, 8-15, 16-63, 64+]
    max_rel_err: f64,
    avg_rel_err: f64,
}

impl UlpStats {
    fn new(name: &'static str) -> Self {
        Self {
            name,
            max_ulp: 0,
            max_ulp_input: 0.0,
            max_ulp_expected: 0.0,
            max_ulp_got: 0.0,
            total_tested: 0,
            nan_count: 0,
            inf_count: 0,
            ulp_histogram: [0; 8],
            max_rel_err: 0.0,
            avg_rel_err: 0.0,
        }
    }

    fn update(&mut self, input: f32, expected: f32, got: f32) {
        if got.is_nan() && !expected.is_nan() {
            self.nan_count += 1;
            return;
        }
        if got.is_infinite() && !expected.is_infinite() {
            self.inf_count += 1;
            return;
        }
        if !expected.is_finite() || !got.is_finite() {
            return;
        }

        self.total_tested += 1;

        // Relative error
        let abs_err = (got - expected).abs() as f64;
        let rel_err = if expected.abs() > 1e-38 {
            abs_err / expected.abs() as f64
        } else {
            abs_err
        };
        self.avg_rel_err += rel_err;
        if rel_err > self.max_rel_err {
            self.max_rel_err = rel_err;
        }

        // ULP distance
        if let Some(ulps) = ulp_distance(expected, got) {
            let bucket = match ulps {
                0 => 0,
                1 => 1,
                2 => 2,
                3 => 3,
                4..=7 => 4,
                8..=15 => 5,
                16..=63 => 6,
                _ => 7,
            };
            self.ulp_histogram[bucket] += 1;

            if ulps > self.max_ulp {
                self.max_ulp = ulps;
                self.max_ulp_input = input;
                self.max_ulp_expected = expected;
                self.max_ulp_got = got;
            }
        }
    }

    fn finalize(&mut self) {
        if self.total_tested > 0 {
            self.avg_rel_err /= self.total_tested as f64;
        }
    }

    fn print(&self) {
        println!(
            "{:20} max_ulp: {:4}  max_rel: {:.2e}  avg_rel: {:.2e}  tested: {}",
            self.name, self.max_ulp, self.max_rel_err, self.avg_rel_err, self.total_tested
        );
        println!(
            "    ULP histogram: 0:{} 1:{} 2:{} 3:{} 4-7:{} 8-15:{} 16-63:{} 64+:{}",
            self.ulp_histogram[0],
            self.ulp_histogram[1],
            self.ulp_histogram[2],
            self.ulp_histogram[3],
            self.ulp_histogram[4],
            self.ulp_histogram[5],
            self.ulp_histogram[6],
            self.ulp_histogram[7],
        );
        if self.max_ulp > 3 {
            println!(
                "    worst: input={:e} ({:#010x}) expected={:e} ({:#010x}) got={:e} ({:#010x})",
                self.max_ulp_input,
                self.max_ulp_input.to_bits(),
                self.max_ulp_expected,
                self.max_ulp_expected.to_bits(),
                self.max_ulp_got,
                self.max_ulp_got.to_bits(),
            );
        }
        if self.nan_count > 0 || self.inf_count > 0 {
            println!(
                "    ERRORS: {} unexpected NaN, {} unexpected Inf",
                self.nan_count, self.inf_count
            );
        }
    }

    fn assert_max_ulp(&self, max_allowed: u32) {
        assert!(
            self.max_ulp <= max_allowed,
            "{}: max_ulp {} exceeds limit {} at input={:e} ({:#010x})",
            self.name,
            self.max_ulp,
            max_allowed,
            self.max_ulp_input,
            self.max_ulp_input.to_bits(),
        );
        assert!(
            self.nan_count == 0,
            "{}: {} unexpected NaN values",
            self.name,
            self.nan_count,
        );
    }
}

// ============================================================================
// cbrt tests — comprehensive with ULP measurement
// ============================================================================

/// Generate test vectors covering normal, denormal, edge, and special values.
fn cbrt_test_vectors() -> Vec<f32> {
    let mut vals = Vec::with_capacity(5_000_000);

    // 1. Normal range: logarithmic sweep 1e-37 to 1e37
    for i in 0..1_000_000 {
        let t = i as f32 / 1_000_000.0;
        vals.push(10.0f32.powf(-37.0 + t * 74.0));
    }

    // 2. Negative normal range
    for i in 0..1_000_000 {
        let t = i as f32 / 1_000_000.0;
        vals.push(-10.0f32.powf(-37.0 + t * 74.0));
    }

    // 3. Near-zero normal values
    for i in -100_000..100_000i32 {
        let v = i as f32 * 1e-10;
        if v != 0.0 {
            vals.push(v);
        }
    }

    // 4. Denormals (smallest normal is 1.17549435e-38)
    // Positive denormals
    for i in 1..100_000u32 {
        vals.push(f32::from_bits(i)); // smallest denormals
    }
    for i in 1..100_000u32 {
        vals.push(f32::from_bits(0x0080_0000 - i)); // largest denormals (just below normal)
    }
    // Negative denormals
    for i in 1..100_000u32 {
        vals.push(f32::from_bits(0x8000_0000 | i));
    }
    for i in 1..100_000u32 {
        vals.push(f32::from_bits(0x8000_0000 | (0x0080_0000 - i)));
    }

    // 5. Special values
    vals.push(0.0);
    vals.push(-0.0);
    vals.push(f32::INFINITY);
    vals.push(f32::NEG_INFINITY);
    vals.push(f32::NAN);
    vals.push(f32::MIN_POSITIVE); // smallest normal
    vals.push(-f32::MIN_POSITIVE);
    vals.push(f32::MAX);
    vals.push(f32::MIN);
    vals.push(1.0);
    vals.push(-1.0);
    vals.push(8.0);
    vals.push(27.0);
    vals.push(0.125); // 1/8 = 0.5^3

    // 6. Perfect cubes
    for i in -100..=100i32 {
        if i != 0 {
            vals.push((i as f32).powi(3));
        }
    }

    vals
}

/// Run cbrt variant over test vectors, comparing against std::f32::cbrt().
fn run_cbrt_test(
    name: &'static str,
    token: X64V3Token,
    func: fn(X64V3Token, &[f32; 8]) -> [f32; 8],
    vals: &[f32],
) -> UlpStats {
    let mut stats = UlpStats::new(name);

    for chunk in vals.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = func(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.cbrt();
            stats.update(x, expected, result[i]);
        }
    }

    stats.finalize();
    stats
}

/// Compare cbrt_lowp against cbrt_midp (not just std::cbrt).
fn run_cbrt_vs_midp(
    name: &'static str,
    token: X64V3Token,
    func: fn(X64V3Token, &[f32; 8]) -> [f32; 8],
    vals: &[f32],
) -> UlpStats {
    let mut stats = UlpStats::new(name);

    for chunk in vals.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = func(token, arr);
        let midp_result = simd_cbrt_midp(token, arr);
        for i in 0..8 {
            stats.update(arr[i], midp_result[i], result[i]);
        }
    }

    stats.finalize();
    stats
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_cbrt_all_variants_comprehensive() {
    let Some(token) = X64V3Token::summon() else {
        eprintln!("AVX2+FMA not available, skipping test");
        return;
    };

    let vals = cbrt_test_vectors();

    // Working range: normal nonzero finite values within 1e-37..1e37.
    // This is where image/color processing operates. Excludes:
    // - Zero (bit hack on 0 bits produces garbage; use _precise variant)
    // - Denormals (< MIN_POSITIVE; use _precise variant)
    // - Inf/NaN (not meaningful for cbrt)
    // - Extreme values near f32::MAX (Newton formulation in midp overflows
    //   because 3*y³ > f32::MAX; Halley formulation avoids this)
    let working_range: Vec<f32> = vals
        .iter()
        .copied()
        .filter(|x| x.is_finite() && x.abs() >= f32::MIN_POSITIVE && x.abs() <= 1e37)
        .collect();

    // Extreme values (>1e37): Newton formulation in midp overflows because
    // 3*y³ > f32::MAX. Halley formulation avoids this since ratio stays ~1.0.
    // Pad to 8 values so chunks work.
    let mut extremes: Vec<f32> = vals
        .iter()
        .copied()
        .filter(|x| x.is_finite() && x.abs() > 1e37)
        .collect();
    while extremes.len() % 8 != 0 {
        extremes.push(1e38); // pad
    }

    println!(
        "\n=== cbrt vs std::f32::cbrt() — working range ({} values, 1e-37..1e37) ===\n",
        working_range.len()
    );

    let midp_stats = run_cbrt_test("cbrt_midp", token, simd_cbrt_midp, &working_range);
    midp_stats.print();
    midp_stats.assert_max_ulp(4);

    let lowp_stats = run_cbrt_test("cbrt_lowp", token, simd_cbrt_lowp, &working_range);
    lowp_stats.print();
    // lowp has ~15 bits precision, so expect up to ~256 ULP
    lowp_stats.assert_max_ulp(512);

    println!("\n=== cbrt_lowp vs cbrt_midp (parity check, working range) ===\n");

    let lowp_vs_midp = run_cbrt_vs_midp("lowp vs midp", token, simd_cbrt_lowp, &working_range);
    lowp_vs_midp.print();

    // Full range including extremes — informational (Newton overflow at f32::MAX)
    println!("\n=== cbrt on extreme values near f32::MAX (informational) ===\n");
    let extremes: Vec<f32> = vals
        .iter()
        .copied()
        .filter(|x| x.is_finite() && x.abs() > 1e37)
        .collect();
    if !extremes.is_empty() {
        let midp_ext = run_cbrt_test("midp (extreme)", token, simd_cbrt_midp, &extremes);
        midp_ext.print();
        let lowp_ext = run_cbrt_test("lowp (extreme)", token, simd_cbrt_lowp, &extremes);
        lowp_ext.print();
    }

    // Denormals — informational (none of the base variants handle these)
    println!("\n=== cbrt on denormals (informational, no assertion) ===\n");
    let denormals_only: Vec<f32> = vals
        .iter()
        .copied()
        .filter(|x| x.is_finite() && *x != 0.0 && x.abs() < f32::MIN_POSITIVE)
        .collect();
    if !denormals_only.is_empty() {
        let midp_denorm = run_cbrt_test("midp (denorm)", token, simd_cbrt_midp, &denormals_only);
        midp_denorm.print();
        let lowp_denorm = run_cbrt_test("lowp (denorm)", token, simd_cbrt_lowp, &denormals_only);
        lowp_denorm.print();
    }

    println!("\n=== Edge case spot checks ===\n");

    // Zero handling
    let zeros = [0.0f32, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0];
    let lowp_z = simd_cbrt_lowp(token, &zeros);
    let midp_z = simd_cbrt_midp(token, &zeros);
    println!(
        "cbrt(0.0):  lowp={:e}  midp={:e}  std={:e}",
        lowp_z[0],
        midp_z[0],
        0.0f32.cbrt()
    );
    println!(
        "cbrt(-0.0): lowp={:e}  midp={:e}  std={:e}",
        lowp_z[1],
        midp_z[1],
        (-0.0f32).cbrt()
    );

    // NaN handling
    let nans = [f32::NAN; 8];
    let lowp_n = simd_cbrt_lowp(token, &nans);
    let midp_n = simd_cbrt_midp(token, &nans);
    println!(
        "cbrt(NaN):  lowp={}  midp={}  (should be NaN)",
        lowp_n[0].is_nan(),
        midp_n[0].is_nan()
    );

    // Inf handling
    let infs = [
        f32::INFINITY,
        f32::NEG_INFINITY,
        f32::INFINITY,
        f32::NEG_INFINITY,
        f32::INFINITY,
        f32::NEG_INFINITY,
        f32::INFINITY,
        f32::NEG_INFINITY,
    ];
    let lowp_i = simd_cbrt_lowp(token, &infs);
    let midp_i = simd_cbrt_midp(token, &infs);
    println!(
        "cbrt(+inf): lowp={:e}  midp={:e}  std={:e}",
        lowp_i[0],
        midp_i[0],
        f32::INFINITY.cbrt()
    );
    println!(
        "cbrt(-inf): lowp={:e}  midp={:e}  std={:e}",
        lowp_i[1],
        midp_i[1],
        f32::NEG_INFINITY.cbrt()
    );

    // Denormal spot check
    let denorms = [
        1e-40f32,
        1e-42,
        1e-44,
        f32::from_bits(1),
        f32::from_bits(100),
        f32::from_bits(10000),
        1e-39,
        1e-41,
    ];
    let midp_d = simd_cbrt_midp(token, &denorms);
    let lowp_d = simd_cbrt_lowp(token, &denorms);
    println!("\nDenormal inputs (no denormal handling in any base variant):");
    for i in 0..4 {
        let expected = denorms[i].cbrt();
        println!(
            "  cbrt({:e}): std={:e}  midp={:e}({}ulp)  lowp={:e}({}ulp)",
            denorms[i],
            expected,
            midp_d[i],
            ulp_distance(expected, midp_d[i]).map_or("NaN".to_string(), |u| u.to_string()),
            lowp_d[i],
            ulp_distance(expected, lowp_d[i]).map_or("NaN".to_string(), |u| u.to_string()),
        );
    }
}

// ============================================================================
// pow tests
// ============================================================================

#[test]
#[cfg(target_arch = "x86_64")]
fn test_pow_midp_brute_force() {
    let Some(token) = X64V3Token::summon() else {
        eprintln!("AVX2+FMA not available, skipping test");
        return;
    };

    // Test pow(x, 2.4) - sRGB gamma
    let mut stats = AccuracyStats::new("pow_midp(x, 2.4)");

    let test_values: Vec<f32> = (1..1_000_000).map(|i| i as f32 / 1_000_000.0).collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_pow_midp(token, arr, 2.4);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.powf(2.4);
            stats.update(x, expected, result[i]);
        }
    }

    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(1e-5);

    // Test pow(x, 1/2.4) - inverse sRGB gamma
    let mut stats = AccuracyStats::new("pow_midp(x, 1/2.4)");

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_pow_midp(token, arr, 1.0 / 2.4);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.powf(1.0 / 2.4);
            stats.update(x, expected, result[i]);
        }
    }

    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(1e-5);

    // Test pow(x, 0.5) - should match sqrt
    let mut stats = AccuracyStats::new("pow_midp(x, 0.5)");

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_pow_midp(token, arr, 0.5);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.sqrt();
            stats.update(x, expected, result[i]);
        }
    }

    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(1e-5);
}

// ============================================================================
// exp2/log2 tests
// ============================================================================

#[test]
#[cfg(target_arch = "x86_64")]
fn test_exp2_midp_brute_force() {
    let Some(token) = X64V3Token::summon() else {
        eprintln!("AVX2+FMA not available, skipping test");
        return;
    };

    let mut stats = AccuracyStats::new("exp2_midp");

    // Test range: -126 to 126 (full f32 exponent range)
    let test_values: Vec<f32> = (0..1_000_000)
        .map(|i| -126.0 + (i as f32 / 1_000_000.0) * 252.0)
        .collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_exp2_midp(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.exp2();
            stats.update(x, expected, result[i]);
        }
    }

    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(1e-5);
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_log2_midp_brute_force() {
    let Some(token) = X64V3Token::summon() else {
        eprintln!("AVX2+FMA not available, skipping test");
        return;
    };

    let mut stats = AccuracyStats::new("log2_midp");

    // Test range: 1e-37 to 1e37 (avoiding denormals near 1e-38)
    // f32 denormal range is below ~1.2e-38
    let test_values: Vec<f32> = (0..1_000_000)
        .map(|i| {
            let t = i as f32 / 1_000_000.0;
            10.0f32.powf(-37.0 + t * 74.0)
        })
        .collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_log2_midp(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.log2();
            stats.update(x, expected, result[i]);
        }
    }

    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(1e-5);
}

// ============================================================================
// ln/exp tests
// ============================================================================

#[test]
#[cfg(target_arch = "x86_64")]
fn test_ln_midp_brute_force() {
    let Some(token) = X64V3Token::summon() else {
        eprintln!("AVX2+FMA not available, skipping test");
        return;
    };

    let mut stats = AccuracyStats::new("ln_midp");

    // Test range: 1e-37 to 1e37 (avoiding denormals near 1e-38)
    let test_values: Vec<f32> = (0..1_000_000)
        .map(|i| {
            let t = i as f32 / 1_000_000.0;
            10.0f32.powf(-37.0 + t * 74.0)
        })
        .collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_ln_midp(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.ln();
            stats.update(x, expected, result[i]);
        }
    }

    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(1e-5);
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_exp_midp_brute_force() {
    let Some(token) = X64V3Token::summon() else {
        eprintln!("AVX2+FMA not available, skipping test");
        return;
    };

    let mut stats = AccuracyStats::new("exp_midp");

    // Test range: -80 to 80 (well within safe range)
    // exp(80) ≈ 5.5e34, giving good margin before overflow at exp(88.7) ≈ 3.4e38
    let test_values: Vec<f32> = (0..1_000_000)
        .map(|i| -80.0 + (i as f32 / 1_000_000.0) * 160.0)
        .collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_exp_midp(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.exp();
            stats.update(x, expected, result[i]);
        }
    }

    stats.finalize();
    stats.print();
    // exp_midp uses exp2_midp(x * LOG2_E), compound error is ~1.5 ULP
    stats.assert_max_rel_err(2e-5);
}

// ============================================================================
// lowp tests (should have higher error bounds)
// ============================================================================

#[test]
#[cfg(target_arch = "x86_64")]
fn test_lowp_functions_brute_force() {
    let Some(token) = X64V3Token::summon() else {
        eprintln!("AVX2+FMA not available, skipping test");
        return;
    };

    println!("\n=== Low-precision function accuracy ===\n");

    // exp2_lowp
    let mut stats = AccuracyStats::new("exp2_lowp");
    let test_values: Vec<f32> = (0..100_000)
        .map(|i| -20.0 + (i as f32 / 100_000.0) * 40.0)
        .collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_exp2_lowp(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.exp2();
            stats.update(x, expected, result[i]);
        }
    }
    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(0.01); // 1% max error for lowp

    // log2_lowp
    let mut stats = AccuracyStats::new("log2_lowp");
    let test_values: Vec<f32> = (1..100_000)
        .map(|i| {
            let t = i as f32 / 100_000.0;
            10.0f32.powf(-10.0 + t * 20.0)
        })
        .collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_log2_lowp(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.log2();
            stats.update(x, expected, result[i]);
        }
    }
    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(0.01);

    // pow_lowp
    let mut stats = AccuracyStats::new("pow_lowp(x, 2.4)");
    let test_values: Vec<f32> = (1..100_000).map(|i| i as f32 / 100_000.0).collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_pow_lowp(token, arr, 2.4);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.powf(2.4);
            stats.update(x, expected, result[i]);
        }
    }
    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(0.01);

    // ln_lowp
    let mut stats = AccuracyStats::new("ln_lowp");
    let test_values: Vec<f32> = (1..100_000)
        .map(|i| {
            let t = i as f32 / 100_000.0;
            10.0f32.powf(-10.0 + t * 20.0)
        })
        .collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_ln_lowp(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.ln();
            stats.update(x, expected, result[i]);
        }
    }
    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(0.01);

    // exp_lowp
    let mut stats = AccuracyStats::new("exp_lowp");
    let test_values: Vec<f32> = (0..100_000)
        .map(|i| -10.0 + (i as f32 / 100_000.0) * 20.0)
        .collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_exp_lowp(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.exp();
            stats.update(x, expected, result[i]);
        }
    }
    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(0.01);

    // log10_lowp
    let mut stats = AccuracyStats::new("log10_lowp");
    let test_values: Vec<f32> = (1..100_000)
        .map(|i| {
            let t = i as f32 / 100_000.0;
            10.0f32.powf(-10.0 + t * 20.0)
        })
        .collect();

    for chunk in test_values.chunks(8) {
        if chunk.len() < 8 {
            continue;
        }
        let arr: &[f32; 8] = chunk.try_into().unwrap();
        let result = simd_log10_lowp(token, arr);
        for (i, &x) in arr.iter().enumerate() {
            let expected = x.log10();
            stats.update(x, expected, result[i]);
        }
    }
    stats.finalize();
    stats.print();
    stats.assert_max_rel_err(0.01);
}