multidouble 5.2.0

Double-double, quad-double and octo-double arithmetic, scalar and 8-lane SIMD (AVX-512, AVX2, SSE2, NEON, portable) with bitwise identical results, dual numbers and LU solves
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
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
//! Series products (`series`): the exact product `mullow` against the naive sums of the same terms formed in
//! octo-double, on double-, quad- and octo-double lanes, and the relaxed products (`Relaxed`) of online recurrences
//! against their planted solutions and their naive online sums.
//!
//! Pass, fixed before the first run. `mullow` on every planted case (below) at n = 128, 251 and 512 is within
//! 1e-30 (DV), 1e-60 (QV) and 1e-120 (OV) of the octo-double reference relative to sum |a_i| |b_(k-i)|, with every
//! inverse transform's value within 1/4 of its integer and no fallback; on small integers it is exact. Controls, which
//! must fail: the slope and the blocks removed on the cases spanning 10^+-60; the last block pair dropped; the output
//! unscaled with a wrong slope; a NaN in an output, and in an input (which also makes `max_frac` infinite).
//!
//! The relaxed products: on recurrences whose solutions are dyadic integers (exact in every precision), the online
//! solution equals the planted one bit for bit, for one factor known (a series quotient) and both online (a square
//! root), with squares from side 4 and with the lanes' own `BLOCK_MIN`; on full-precision coefficients the online
//! solutions with relaxed and with naive sums agree to 1e-28 (DV), 1e-58 (QV), 1e-118 (OV) relative to each
//! coefficient. Controls: the first square dropped (`RELAXED_MUT_DROP`) must break both. Without squares (`with_min`
//! 0) `sum` is bitwise `cs_range`.
//!
//! Under emulation (`common::emulated`) the planted cases stop at n = 251, the lanes' own squares are QV's alone,
//! and the relaxed accuracy runs to n = 300 rather than 700.
//!
//! Planted cases (8 lanes, random signs, all parts random, each lane its own offsets): `flat`, |a_i| ~ 2^(-0.7 i) and
//! |b_i| ~ 2^(-0.4 i), each times 2^(+-4) at random; `ramp`, log10 |a_i| from -60 to +60 and log10 |b_i| from +60 to
//! -60, each +-1 at random; `bumpy`, log10 |a_i| = 60 sin(2 pi 1.3 i / n + phase), log10 |b_i| = 60 cos(...);
//! `holes`, `flat` with a third of the coefficients and one whole lane zero; `ints`, integers in [-1000, 1000].
#![allow(clippy::needless_range_loop)]
mod common;
use common::Rng;
use multidouble::dd::DD;
use multidouble::dual::Lane;
use multidouble::field::{cs_range, Fl};
use multidouble::native::{Du, DV, OV, QV, V8};
use multidouble::od::OD;
use multidouble::series::{
    mullow, mullow_naive, mullow_pow2, Cfg, Expansion, Relaxed, Stats, MUT_CHUNK_TOP, MUT_DROP_PAIR, MUT_DROP_TILE,
    MUT_E2_PLAIN, MUT_E2_PRESCALE, MUT_E2_SPLIT_ALWAYS, MUT_NO_TILE, MUT_SLOPE, MUT_ZERO_LANE, RELAXED_MUT_DROP,
};
use multidouble::simd::Native;

const L10: f64 = std::f64::consts::LOG2_10;

/// a random octo-double of magnitude 2^e (within a factor 2), random sign, all eight parts in use
fn rand_od(r: &mut Rng, e: f64) -> OD {
    let sign = if r.next_u64() & 1 == 0 { 1.0 } else { -1.0 };
    let e0 = e.floor();
    let mut x = [0.0f64; 8];
    x[0] = sign * (1.0 + r.unit()) * (e0 - 1.0).exp2();
    for j in 1..8 {
        x[j] = (r.unit() - 0.5) * x[j - 1].abs() * (-52.0f64).exp2();
    }
    OD::new(x)
}

/// an expansion with lane l the first NP parts of lanes\[l\]
fn make<E: Expansion<Native>>(lanes: &[OD; 8]) -> E {
    let p: Vec<V8> = (0..E::NP)
        .map(|q| V8::from_array(std::array::from_fn(|l| lanes[l].x[q])))
        .collect();
    E::from_parts(&p)
}

/// lane l of x as an octo-double, exactly
fn od_lane<E: Expansion<Native>>(x: &E, l: usize) -> OD {
    OD::new(std::array::from_fn(|q| {
        if q < E::NP {
            x.part(q).to_array()[l]
        } else {
            0.0
        }
    }))
}

/// x as octo-double lanes, exactly
fn promote<E: Expansion<Native>>(x: &E) -> OV {
    OV::from_lanes(&std::array::from_fn(|l| od_lane(x, l)))
}

/// a series of n coefficients, log2 |coefficient i| on lane l given by lg(i, l), zero where zero(i, l)
fn series<E: Expansion<Native>>(
    r: &mut Rng,
    n: usize,
    lg: impl Fn(usize, usize, &mut Rng) -> f64,
    zero: impl Fn(usize, usize) -> bool,
) -> Vec<E> {
    (0..n)
        .map(|i| {
            let lanes: [OD; 8] = std::array::from_fn(|l| {
                if zero(i, l) {
                    OD::ZERO
                } else {
                    let e = lg(i, l, r);
                    rand_od(r, e)
                }
            });
            make(&lanes)
        })
        .collect()
}

/// the planted pair (a, b) of a case
fn case<E: Expansion<Native>>(name: &str, n: usize, seed: u64) -> (Vec<E>, Vec<E>) {
    let mut r = Rng(seed);
    let nf = n as f64;
    let no = |_: usize, _: usize| false;
    match name {
        "flat" | "holes" => {
            let holes = name == "holes";
            let z = move |i: usize, l: usize| holes && (l == 5 || (i * 7 + l).is_multiple_of(3));
            let a = series(
                &mut r,
                n,
                |i, l, r| -0.7 * i as f64 + l as f64 + 8.0 * (r.unit() - 0.5),
                z,
            );
            let b = series(
                &mut r,
                n,
                |i, l, r| -0.4 * i as f64 - l as f64 + 8.0 * (r.unit() - 0.5),
                z,
            );
            (a, b)
        }
        "ramp" => {
            let ramp = |i: usize| 120.0 * i as f64 / (nf - 1.0);
            let a = series(
                &mut r,
                n,
                |i, l, r| L10 * (-60.0 + ramp(i) + 2.0 * (r.unit() - 0.5)) + l as f64,
                no,
            );
            let b = series(
                &mut r,
                n,
                |i, l, r| L10 * (60.0 - ramp(i) + 2.0 * (r.unit() - 0.5)) - l as f64,
                no,
            );
            (a, b)
        }
        "bumpy" => {
            let tp = 2.0 * std::f64::consts::PI;
            let a = series(
                &mut r,
                n,
                |i, l, _| L10 * 60.0 * (tp * 1.3 * i as f64 / nf + 0.3 * l as f64).sin(),
                no,
            );
            let b = series(
                &mut r,
                n,
                |i, l, _| L10 * 60.0 * (tp * 0.8 * i as f64 / nf + 0.2 * l as f64).cos(),
                no,
            );
            (a, b)
        }
        "ints" => {
            let int = |r: &mut Rng| -> Vec<E> {
                (0..n)
                    .map(|_| {
                        make(&std::array::from_fn(|_| {
                            OD::from_f64((r.next_u64() % 2001) as f64 - 1000.0)
                        }))
                    })
                    .collect()
            };
            let a = int(&mut r);
            let b = int(&mut r);
            (a, b)
        }
        _ => panic!("case {name}"),
    }
}

/// the fast product into fresh accumulators
fn fast<E: Expansion<Native>>(a: &[E], b: &[E], n: usize, cfg: &Cfg) -> (Vec<E>, Stats) {
    let mut acc = vec![E::acc_zero(); n];
    let st = mullow(a, b, n, cfg, &mut acc);
    (acc.iter().map(E::acc_fin).collect(), st)
}

/// the reference: the naive sums of the terms promoted to octo-double
fn reference<E: Expansion<Native>>(a: &[E], b: &[E], n: usize) -> Vec<OV> {
    let (ao, bo): (Vec<OV>, Vec<OV>) = (a.iter().map(promote).collect(), b.iter().map(promote).collect());
    mullow_naive(&ao, &bo, n)
}

/// max over k and lanes of |x - y| / sum_i |a_i| |b_(k-i)| and of |x - y| / |y| (y nonzero), the difference in
/// octo-double, both infinite where a part of x or y is not finite; and the number of coefficients that differ from y
/// (y rounded to x's parts) in any bit
fn compare<E: Expansion<Native>>(x: &[E], y: &[OV], a: &[E], b: &[E]) -> (f64, f64, usize) {
    let ah: Vec<[f64; 8]> = a.iter().map(|v| v.part(0).to_array().map(f64::abs)).collect();
    let bh: Vec<[f64; 8]> = b.iter().map(|v| v.part(0).to_array().map(f64::abs)).collect();
    let (mut ra, mut rc, mut differ) = (0.0f64, 0.0f64, 0);
    for k in 0..y.len() {
        let yl = y[k].lanes();
        for l in 0..8 {
            let xl = od_lane(&x[k], l);
            let d = (xl - yl[l]).to_f64().abs();
            if xl.x.iter().chain(yl[l].x.iter()).any(|p| !p.is_finite()) || !d.is_finite() {
                (ra, rc, differ) = (f64::INFINITY, f64::INFINITY, differ + 1);
                continue;
            }
            let ab: f64 = (0..=k)
                .filter(|&i| i < ah.len() && k - i < bh.len())
                .map(|i| ah[i][l] * bh[k - i][l])
                .sum();
            if ab > 0.0 {
                ra = ra.max(d / ab);
            } else if d > 0.0 {
                ra = f64::INFINITY;
            }
            let yv = yl[l].to_f64().abs();
            if yv > 0.0 {
                rc = rc.max(d / yv);
            }
            if d != 0.0 {
                differ += 1;
            }
        }
    }
    (ra, rc, differ)
}

/// the bound of each expansion's checks, relative to sum |a_i| |b_(k-i)|
fn bound<E: Expansion<Native>>() -> f64 {
    match E::NP {
        2 => 1e-30,
        4 => 1e-60,
        _ => 1e-120,
    }
}

/// every planted case at n = 128, 251, 512 (128 and 251 under emulation) within the bound with max_frac < 1/4 and no
/// fallback, the integers exact, and the controls failing; the report of each case on stdout
fn planted<E: Expansion<Native>>(name: &str) -> bool {
    let cfg = Cfg::default();
    let tol = bound::<E>();
    let mut ok = true;
    let ns: &[usize] = if common::emulated() {
        &[128, 251]
    } else {
        &[128, 251, 512]
    };
    for &n in ns {
        for (ci, case_name) in ["flat", "holes", "ramp", "bumpy", "ints"].iter().enumerate() {
            let (a, b) = case::<E>(case_name, n, 1000 + 17 * n as u64 + ci as u64);
            let rf = reference(&a, &b, n);
            let (fx, st) = fast(&a, &b, n, &cfg);
            let (ra, rc, differ) = compare(&fx, &rf, &a, &b);
            let (na, _, _) = compare(&mullow_naive(&a, &b, n), &rf, &a, &b);
            let pass = ra <= tol && st.max_frac < 0.25 && st.fallback == 0 && (*case_name != "ints" || differ == 0);
            // the controls: the slope and blocks off on the wide cases; the last convolved pair dropped where there
            // are two or more; a wrong slope where the slope is not zero
            let wide = *case_name == "ramp" || *case_name == "bumpy";
            let (c0, _, _) = compare(
                &fast(
                    &a,
                    &b,
                    n,
                    &Cfg {
                        scale: false,
                        blocks: false,
                        ..cfg
                    },
                )
                .0,
                &rf,
                &a,
                &b,
            );
            let (c1, _, _) = compare(
                &fast(
                    &a,
                    &b,
                    n,
                    &Cfg {
                        mutate: MUT_DROP_PAIR,
                        ..cfg
                    },
                )
                .0,
                &rf,
                &a,
                &b,
            );
            let (c2, _, _) = compare(
                &fast(
                    &a,
                    &b,
                    n,
                    &Cfg {
                        mutate: MUT_SLOPE,
                        ..cfg
                    },
                )
                .0,
                &rf,
                &a,
                &b,
            );
            let ctrl = (!wide || c0 > tol) && c1 > tol && (*case_name == "ints" || c2 > tol);
            ok &= pass && ctrl;
            println!(
                "{name} {case_name:5} {n:3}: blocks {:2}x{:<2} pairs {:3} digits {:2} max_frac {:.1e} rel|a||b| \
                 {ra:.2e} (naive {na:.2e}) rel|c| {rc:.2e} differ {differ:4} | controls {c0:.1e} {c1:.1e} {c2:.1e} \
                 {}",
                st.blocks.0,
                st.blocks.1,
                st.pairs,
                st.digits,
                st.max_frac,
                if pass && ctrl { "PASS" } else { "FAIL" }
            );
        }
    }
    // NaN controls: one in an output coefficient (compare fails), one in an input (max_frac infinite, and the NaN
    // reaches the sums)
    let (a, b) = case::<E>("flat", 128, 77);
    let rf = reference(&a, &b, 128);
    let (mut fx, _) = fast(&a, &b, 128, &cfg);
    let mut lanes: [OD; 8] = std::array::from_fn(|l| od_lane(&fx[40], l));
    lanes[3].x[1] = f64::NAN;
    fx[40] = make(&lanes);
    let (r1, _, _) = compare(&fx, &rf, &a, &b);
    let mut a2 = a.clone();
    let mut lanes: [OD; 8] = std::array::from_fn(|l| od_lane(&a2[17], l));
    lanes[5].x[0] = f64::NAN;
    a2[17] = make(&lanes);
    let (fx2, st2) = fast(&a2, &b, 128, &cfg);
    let (r2, _, _) = compare(&fx2, &reference(&a2, &b, 128), &a2, &b);
    // the NaN of a_17 lane 5 reaches c_17 lane 5 itself, and lane 4 stays finite
    let reached = od_lane(&fx2[17], 5).x[0].is_nan() && od_lane(&fx2[17], 4).x.iter().all(|p| p.is_finite());
    // a NaN compares false, so each must not pass its bound
    let passes = |v: f64, b: f64| v <= b;
    let fired = !passes(r1, tol) && !passes(st2.max_frac, 0.2499) && !passes(r2, tol) && reached;
    println!(
        "{name} NaN controls: output {r1:.1e}, input max_frac {:.1e} rel {r2:.1e}: fired {fired}",
        st2.max_frac
    );
    ok && fired
}

#[test]
fn mullow_planted_dv() {
    assert!(planted::<DV>("DV"));
}

#[test]
fn mullow_planted_qv() {
    assert!(planted::<QV>("QV"));
}

#[test]
fn mullow_planted_ov() {
    assert!(planted::<OV>("OV"));
}

/// the digits of a coefficient against its value, sum_r d_r 2^(16 r) - x 2^k per lane in octo-double, within 8; the
/// control, one digit one off, must miss it
fn digit_check<E: Expansion<Native>>(mutate: bool) -> f64 {
    let mut r = Rng(5);
    let mut worst = 0.0f64;
    for it in 0..2000 {
        let e = (r.unit() * 40.0) as i32 - 20;
        let lanes: [OD; 8] = std::array::from_fn(|_| rand_od(&mut r, e as f64));
        let x: E = make(&lanes);
        let t = 28;
        let w = 16 * t as i32 - 2;
        let k: [i32; 8] = std::array::from_fn(|_| w - e - 1 - (it % 7));
        let mut dv = vec![V8::zero(); t];
        multidouble::series::digits(&x, &k, t, &mut dv);
        let mut d: Vec<[f64; 8]> = dv.iter().map(|v| v.to_array()).collect();
        if mutate && it == 0 {
            d[3][2] += 1.0;
        }
        for l in 0..8 {
            let mut acc = OD::ZERO;
            for (q, dq) in d.iter().enumerate() {
                acc = acc + OD::from_f64(dq[l]).mul_pwr2((16.0 * q as f64).exp2());
            }
            let xv = od_lane(&x, l).mul_pwr2((k[l] as f64).exp2());
            let diff = (acc - xv).to_f64().abs();
            worst = if diff.is_finite() {
                worst.max(diff)
            } else {
                f64::INFINITY
            };
        }
    }
    worst
}

#[test]
fn digits_exact() {
    for (name, w, wm) in [
        ("DV", digit_check::<DV>(false), digit_check::<DV>(true)),
        ("QV", digit_check::<QV>(false), digit_check::<QV>(true)),
        ("OV", digit_check::<OV>(false), digit_check::<OV>(true)),
    ] {
        println!("digits {name}: worst {w:.2e}, control {wm:.2e}");
        assert!(w <= 8.0, "{name} digits");
        assert!(wm > 8.0, "{name} digit control did not fail");
    }
}

/// splat of an f64 as an expansion
fn splat<E: Expansion<Native>>(x: f64) -> E {
    make(&[OD::from_f64(x); 8])
}

/// exactly v on every lane, its lower parts zero
fn exact<E: Expansion<Native>>(c: &[E], v: f64) -> bool {
    c.iter()
        .all(|x| (0..8).all(|l| od_lane(x, l).x[0] == v && od_lane(x, l).x[1..].iter().all(|&p| p == 0.0)))
}

/// The edge cases under a control (`Cfg::mutate`, 0 off), each against the reference or its exact value: a product
/// past the FFT tables (8193 ones times one); a lane whose planned scale lies outside f64's range (lane 0 of a =
/// 2^800, 2^-802, 0 and b zero; MUT_ZERO_LANE fails it); 2^e2 on the long route at the ends of the range, 8193
/// copies of 2^-600 times 2^-600 at e2 1200 and of 2^1023 times 2^-1000 at e2 1, exactly 1 and 2^24 (MUT_E2_PLAIN
/// fails it); 2^-1074 and 2^-537 squared; a block too wide for the digits (1 and 2^-1024 squared, spread 1024, no
/// slope); the direct route at e2 = 0 on 2^1023 and 2^-900 (MUT_E2_SPLIT_ALWAYS fails it); and at e2 != 0 on 2^900,
/// 2^-900 at e2 1 and 2^1000, 2^-900 times 2^-100 at e2 100 (MUT_E2_PRESCALE fails it)
fn edges<E: Expansion<Native>>(mutate: u8) -> bool {
    let cfg = Cfg {
        mutate,
        ..Cfg::default()
    };
    let tol = bound::<E>();
    let one: E = splat(1.0);
    let long: Vec<E> = vec![one; 8193];
    let (fx, st) = fast(&long, &[one], 8193, &cfg);
    let (r1, _, _) = compare(&fx, &reference(&long, &[one], 8193), &long, &[one]);
    let lane0 = |x: [f64; 3]| -> Vec<E> {
        x.iter()
            .map(|&v| {
                let mut l = [OD::from_f64(1.0); 8];
                l[0] = OD::from_f64(v);
                make(&l)
            })
            .collect()
    };
    let a = lane0([800f64.exp2(), (-802f64).exp2(), 0.0]);
    let b = lane0([0.0, 0.0, 0.0]);
    let (fx2, st2) = fast(&a, &b, 3, &cfg);
    let (r2, _, _) = compare(&fx2, &reference(&a, &b, 3), &a, &b);
    let e2case = |x: f64, y: f64, e2: i32, want: f64| -> bool {
        let xs: Vec<E> = vec![splat(x); 8193];
        let mut acc = vec![E::acc_zero(); 8193];
        let st = mullow_pow2(&xs, &[splat::<E>(y)], 8193, &cfg, e2, &mut acc);
        let c: Vec<E> = acc.iter().map(E::acc_fin).collect();
        st.max_frac < 0.25 && exact(&c, want)
    };
    let e2a = e2case((-600f64).exp2(), (-600f64).exp2(), 1200, 1.0);
    let e2b = e2case(1023f64.exp2(), (-1000f64).exp2(), 1, 24f64.exp2());
    let tiny = |x: f64| -> f64 {
        let v: Vec<E> = vec![splat(x)];
        let (c, st) = fast(&v, &v, 1, &cfg);
        let (r, _, _) = compare(&c, &reference(&v, &v, 1), &v, &v);
        if st.max_frac < 0.25 {
            r
        } else {
            f64::INFINITY
        }
    };
    let (u1, u2) = (tiny((-1074f64).exp2()), tiny((-537f64).exp2()));
    let wide = Cfg {
        scale: false,
        spread: 1024,
        ..cfg
    };
    let w2: Vec<E> = vec![one, splat((-1024f64).exp2())];
    let mut accw = vec![E::acc_zero(); 2];
    let stw = mullow(&w2, &w2, 2, &wide, &mut accw);
    let cw: Vec<E> = accw.iter().map(E::acc_fin).collect();
    let (rw, _, _) = compare(&cw, &reference(&w2, &w2, 2), &w2, &w2);
    let wide_ok = exact(&cw[..1], 1.0) && rw <= tol && stw.direct > 0;
    let flat = Cfg {
        scale: false,
        blocks: false,
        spread: 2048,
        ..cfg
    };
    let w3: Vec<E> = vec![splat(1023f64.exp2()), splat((-900f64).exp2())];
    let mut acc3 = vec![E::acc_zero(); 2];
    let st3 = mullow(&w3, &[one], 2, &flat, &mut acc3);
    let c3: Vec<E> = acc3.iter().map(E::acc_fin).collect();
    let wide0_ok = exact(&c3[..1], 1023f64.exp2()) && exact(&c3[1..], (-900f64).exp2()) && st3.direct > 0;
    let wide_e2 = |a: [f64; 2], b: f64, e2: i32, want: [f64; 2]| -> bool {
        let x: Vec<E> = vec![splat(a[0].exp2()), splat(a[1].exp2())];
        let mut acc = vec![E::acc_zero(); 2];
        let st = mullow_pow2(&x, &[splat::<E>(b.exp2())], 2, &flat, e2, &mut acc);
        let c: Vec<E> = acc.iter().map(E::acc_fin).collect();
        st.direct > 0 && exact(&c[..1], want[0].exp2()) && exact(&c[1..], want[1].exp2())
    };
    let we1 = wide_e2([900.0, -900.0], 0.0, 1, [901.0, -899.0]);
    let we2 = wide_e2([1000.0, -900.0], -100.0, 100, [1000.0, -900.0]);
    let ok = r1 <= tol
        && r2 <= tol
        && st.max_frac < 0.25
        && st2.max_frac < 0.25
        && e2a
        && e2b
        && u1 <= tol
        && u2 <= tol
        && wide_ok
        && wide0_ok
        && we1
        && we2;
    println!(
        "edges {} (mutate {mutate}): {r1:.1e} {r2:.1e} {e2a} {e2b} {u1:.1e} {u2:.1e} {wide_ok} {wide0_ok} {we1} {we2}",
        E::NP
    );
    ok
}

/// the edge cases and their controls; the zero-lane control only where the case goes by convolution (`zero_lane`):
/// the double- and quad-double cost models form that 3-term pair term by term, where no lane is scaled
fn edges_all<E: Expansion<Native>>(zero_lane: bool) {
    assert!(edges::<E>(0), "edge cases");
    if zero_lane {
        assert!(!edges::<E>(MUT_ZERO_LANE), "the zero-lane control did not fail");
    }
    assert!(!edges::<E>(MUT_E2_PLAIN), "the e2 control did not fail");
    assert!(
        !edges::<E>(MUT_E2_SPLIT_ALWAYS),
        "the e2 = 0 split control did not fail"
    );
    assert!(!edges::<E>(MUT_E2_PRESCALE), "the e2 prescale control did not fail");
}

#[test]
fn mullow_edges() {
    edges_all::<DV>(false);
    edges_all::<QV>(false);
    edges_all::<OV>(true);
}

/// Products at the top of f64's range: f64::MAX times 1 at c_0, the other terms small, by the convolution route
/// (n = 64, so that the pair is convolved): c_0 exactly f64::MAX and the rest within the bound. Control: with
/// MUT_CHUNK_TOP the double- and quad-double outputs are read in chunks there too, and c_0 must come out wrong
fn top_of_range<E: Expansion<Native>>(mutate: u8) -> bool {
    let n = 64;
    let a: Vec<E> = (0..n)
        .map(|i| {
            if i == 0 {
                splat(f64::MAX)
            } else {
                splat((-(i as f64)).exp2())
            }
        })
        .collect();
    let b: Vec<E> = (0..n)
        .map(|i| {
            if i == 0 {
                splat(1.0)
            } else {
                splat((-(2.0 * i as f64)).exp2())
            }
        })
        .collect();
    let (fx, st) = fast(
        &a,
        &b,
        n,
        &Cfg {
            mutate,
            ..Cfg::default()
        },
    );
    let (ra, _, _) = compare(&fx, &reference(&a, &b, n), &a, &b);
    let top = exact(&fx[..1], f64::MAX);
    println!(
        "top of range {}: c_0 exactly f64::MAX {top}, rel {ra:.1e}, pairs {} direct {}",
        E::NP,
        st.pairs,
        st.direct
    );
    top && ra <= bound::<E>() && st.pairs > st.direct
}

#[test]
fn mullow_top_of_range() {
    assert!(top_of_range::<DV>(0), "DV");
    assert!(top_of_range::<QV>(0), "QV");
    assert!(top_of_range::<OV>(0), "OV");
    assert!(!top_of_range::<DV>(MUT_CHUNK_TOP), "the DV chunk control did not fail");
    assert!(!top_of_range::<QV>(MUT_CHUNK_TOP), "the QV chunk control did not fail");
}

/// A product of fields with no lane vectors steps and sums without squares or panics
#[test]
fn relaxed_zero_width() {
    let x: Vec<Fl<DV, 0>> = vec![Fl([]); 8];
    let mut p = Relaxed::<DV, 0>::with_min(8, 1);
    for k in 0..8 {
        let _ = p.sum(&x, &x, k, 0, k);
        p.step(&x, &x, k);
    }
    assert_eq!(p.squares, 0);
}

#[test]
fn mullow_empty() {
    let (a, _) = case::<QV>("flat", 16, 3);
    let none: Vec<QV> = Vec::new();
    for (x, y) in [(&none, &a), (&a, &none), (&none, &none)] {
        let (fx, _) = fast(x, y, 16, &Cfg::default());
        assert!(fx.len() == 16 && exact(&fx, 0.0), "empty factors give zeros");
    }
}

// ---- relaxed products

const B: usize = 2;

/// a field series from per-index point values and tangents: coefficient i, point p = 8 b + l
fn field_series<T: Lane<S = Native>>(
    n: usize,
    v: impl Fn(usize, usize) -> DD,
    t: impl Fn(usize, usize) -> DD,
) -> Vec<Fl<T, B>> {
    let load = |f: &dyn Fn(usize) -> DD| -> Fl<DV, B> {
        let (h, l): (Vec<f64>, Vec<f64>) = (0..8 * B).map(|p| (f(p).hi, f(p).lo)).unzip();
        Fl::load(&h, &l)
    };
    (0..n)
        .map(|i| {
            let (fv, ft) = (load(&|p| v(i, p)), load(&|p| t(i, p)));
            Fl(std::array::from_fn(|b| T::from_vt(fv.0[b], ft.0[b])))
        })
        .collect()
}

/// u and v bit for bit: every lane vector's value and tangent in octo-double
fn same<T: Lane<S = Native>>(u: &[Fl<T, B>], v: &[Fl<T, B>]) -> bool {
    let bits = |x: OV| x.c.map(|p| p.to_array().map(f64::to_bits));
    u.len() == v.len()
        && u.iter()
            .zip(v)
            .all(|(p, q)| (0..B).all(|b| (0..2).all(|c| bits(p.0[b].coef_od(c)) == bits(q.0[b].coef_od(c)))))
}

/// The planted quotient y = r / a solved online, y_k = r_k - sum_(i=1..=k) a_i y_(k-i) (a_0 = 1), by the relaxed
/// product of a and y with squares from side s0 (0: none, the naive sums), and the planted square root x of r = x^2,
/// x_k = r_k - sum_(i=1..k-1) x_i x_(k-i) (x_0 = 1/2). Their coefficients and tangents are integers in [-1000, 1000]
/// times 2^-floor(i/4) (a_0 and x_0 without tangents), so every sum is exact and the solutions are the planted ones bit
/// for bit. Returns whether both are.
fn relaxed_exact<T: Lane<S = Native>>(n: usize, s0: usize, mutate: u8) -> bool {
    let mut r = Rng(11);
    let tab: Vec<Vec<f64>> = (0..6)
        .map(|_| (0..n * 8 * B).map(|_| (r.next_u64() % 2001) as f64 - 1000.0).collect())
        .collect();
    let g = |t: usize, i: usize, p: usize| DD::from_f64(tab[t][i * 8 * B + p] * (-((i / 4) as f64)).exp2());
    let a = field_series::<T>(
        n,
        |i, p| if i == 0 { DD::ONE } else { g(0, i, p) },
        |i, p| if i == 0 { DD::ZERO } else { g(1, i, p) },
    );
    let y = field_series::<T>(n, |i, p| g(2, i, p), |i, p| g(3, i, p));
    let x = field_series::<T>(
        n,
        |i, p| if i == 0 { DD::from_f64(0.5) } else { g(4, i, p) },
        |i, p| if i == 0 { DD::ZERO } else { g(5, i, p) },
    );
    let ry: Vec<Fl<T, B>> = (0..n).map(|k| cs_range(&a, &y, k, 0, k)).collect();
    let rx: Vec<Fl<T, B>> = (0..n).map(|k| cs_range(&x, &x, k, 0, k)).collect();
    let mut pa = Relaxed::<T, B>::with_min(n, s0);
    let mut px = Relaxed::<T, B>::with_min(n, s0);
    pa.mutate = mutate;
    px.mutate = mutate;
    let mut ys = vec![Fl::<T, B>::zero(); n];
    let mut xs = vec![Fl::<T, B>::zero(); n];
    for k in 0..n {
        ys[k] = if k == 0 {
            ry[0]
        } else {
            ry[k] - pa.sum(&a, &ys, k, 1, k)
        };
        xs[k] = match k {
            0 => x[0],
            1 => rx[1],
            _ => rx[k] - px.sum(&xs, &xs, k, 1, k - 1),
        };
        pa.step(&a, &ys, k);
        px.step(&xs, &xs, k);
    }
    same(&ys, &y) && same(&xs, &x)
}

#[test]
fn relaxed_planted_exact() {
    let run = |n: usize, s: [usize; 4], m: u8| -> [bool; 4] {
        [
            relaxed_exact::<DV>(n, s[0], m),
            relaxed_exact::<QV>(n, s[1], m),
            relaxed_exact::<OV>(n, s[2], m),
            relaxed_exact::<Du>(n, s[3], m),
        ]
    };
    for (n, s) in [(300usize, [4usize; 4]), (300, [16; 4])] {
        let ok = run(n, s, 0);
        println!("relaxed exact n {n}, squares from {s:?}: DV QV OV Du {ok:?}");
        assert!(ok.iter().all(|&o| o), "planted solutions not recovered");
        let ctl = run(n, s, RELAXED_MUT_DROP);
        println!("  control, the first square dropped: {ctl:?} (must be all false)");
        assert!(ctl.iter().all(|&o| !o), "the dropped-square control did not fail");
    }
    // the lanes' own smallest squares, at n with two sides or more of them: 2100 for DV and Du (512), 600 for QV (64)
    // and OV (16); under emulation QV's alone, OV's side being that of the second run above
    let full = !common::emulated();
    let own = [
        full.then(|| {
            (
                relaxed_exact::<DV>(2100, DV::BLOCK_MIN, 0),
                relaxed_exact::<DV>(2100, DV::BLOCK_MIN, RELAXED_MUT_DROP),
            )
        }),
        Some((
            relaxed_exact::<QV>(600, QV::BLOCK_MIN, 0),
            relaxed_exact::<QV>(600, QV::BLOCK_MIN, RELAXED_MUT_DROP),
        )),
        full.then(|| {
            (
                relaxed_exact::<OV>(600, OV::BLOCK_MIN, 0),
                relaxed_exact::<OV>(600, OV::BLOCK_MIN, RELAXED_MUT_DROP),
            )
        }),
        full.then(|| {
            (
                relaxed_exact::<Du>(2100, Du::BLOCK_MIN, 0),
                relaxed_exact::<Du>(2100, Du::BLOCK_MIN, RELAXED_MUT_DROP),
            )
        }),
    ];
    println!("relaxed exact, the lanes' own squares: DV QV OV Du (recovered, control recovered) {own:?}");
    assert!(own.iter().flatten().all(|&(o, c)| o && !c), "the lanes' own squares");
}

/// Without squares `sum` is bitwise `cs_range`, on a lane with squares (DV) and on one without (F8)
#[test]
fn relaxed_without_squares_is_cs_range() {
    use multidouble::native::F8;
    let mut r = Rng(3);
    let tab: Vec<DD> = (0..40 * 8 * B).map(|_| DD::new(r.sym(), r.sym() * 1e-17)).collect();
    let x = field_series::<DV>(40, |i, p| tab[i * 8 * B + p], |_, _| DD::ZERO);
    let p = Relaxed::<DV, B>::with_min(40, 0);
    let pf = Relaxed::<F8, B>::new(40);
    let xf: Vec<Fl<F8, B>> = x.iter().map(|f| Fl(f.0.map(F8::cst))).collect();
    for k in 0..40 {
        assert!(
            same(&[p.sum(&x, &x, k, 1, k)], &[cs_range(&x, &x, k, 1, k)]),
            "DV order {k}"
        );
        let (u, w) = (pf.sum(&xf, &xf, k, 0, k), cs_range(&xf, &xf, k, 0, k));
        assert!(
            (0..B).all(|b| u.0[b].val().hi.to_array() == w.0[b].val().hi.to_array()),
            "F8 order {k}"
        );
    }
}

/// The planted quotient on full-precision coefficients: a_0 = 1, |a_i| ~ 2^(-2i - 3), |y_i| ~ 2^-0.5i, r = a y in
/// octo-double rounded to the lane; y solved online with relaxed sums (squares from s0) and with naive sums. Returns
/// max_k max_lanes |y_relaxed - y_naive| / sum_i |a_i| |y_(k-i)|.
fn relaxed_accuracy<E: Expansion<Native> + Lane<S = Native>>(n: usize, s0: usize, mutate: u8) -> f64 {
    let mut r = Rng(29);
    let sr = |r: &mut Rng, g: f64, c: f64| -> Vec<Fl<E, B>> {
        (0..n)
            .map(|i| {
                Fl(std::array::from_fn(|_| {
                    make::<E>(&std::array::from_fn(|_| {
                        let e = -g * i as f64 - c + 2.0 * (r.unit() - 0.5);
                        rand_od(r, e)
                    }))
                }))
            })
            .collect()
    };
    let mut a = sr(&mut r, 2.0, 3.0);
    a[0] = Fl::splat(DD::ONE);
    let y = sr(&mut r, 0.5, 0.0);
    let ab = |b: usize| -> (Vec<OV>, Vec<OV>) {
        (
            a.iter().map(|f| promote(&f.0[b])).collect(),
            y.iter().map(|f| promote(&f.0[b])).collect(),
        )
    };
    // r in octo-double, rounded to E
    let rr: Vec<Fl<E, B>> = {
        let per: Vec<Vec<OV>> = (0..B)
            .map(|b| {
                let (x, z) = ab(b);
                mullow_naive(&x, &z, n)
            })
            .collect();
        (0..n)
            .map(|k| Fl(std::array::from_fn(|b| make::<E>(&per[b][k].lanes()))))
            .collect()
    };
    let solve = |s: Option<usize>| -> Vec<Fl<E, B>> {
        let mut p = Relaxed::<E, B>::with_min(n, s.unwrap_or(0));
        p.mutate = mutate;
        let mut ys = vec![Fl::<E, B>::zero(); n];
        for k in 0..n {
            ys[k] = if k == 0 { rr[0] } else { rr[k] - p.sum(&a, &ys, k, 1, k) };
            p.step(&a, &ys, k);
        }
        ys
    };
    let (yr, yn) = (solve(Some(s0)), solve(None));
    let mut worst = 0.0f64;
    for b in 0..B {
        let ah: Vec<[f64; 8]> = a.iter().map(|f| f.0[b].part(0).to_array().map(f64::abs)).collect();
        let yh: Vec<[f64; 8]> = yn.iter().map(|f| f.0[b].part(0).to_array().map(f64::abs)).collect();
        for k in 0..n {
            for l in 0..8 {
                let d = (od_lane(&yr[k].0[b], l) - od_lane(&yn[k].0[b], l)).to_f64().abs();
                let sc: f64 = (0..=k).map(|i| ah[i][l] * yh[k - i][l]).sum();
                worst = if d.is_finite() {
                    worst.max(d / sc)
                } else {
                    f64::INFINITY
                };
            }
        }
    }
    worst
}

#[test]
fn relaxed_accuracy_against_naive() {
    let t = |np: usize| match np {
        2 => 1e-28,
        4 => 1e-58,
        _ => 1e-118,
    };
    let n = if common::emulated() { 300 } else { 700 };
    for (name, s0, w, wc, tol) in [
        (
            "DV",
            16,
            relaxed_accuracy::<DV>(n, 16, 0),
            relaxed_accuracy::<DV>(n, 16, RELAXED_MUT_DROP),
            t(2),
        ),
        (
            "QV",
            16,
            relaxed_accuracy::<QV>(n, 16, 0),
            relaxed_accuracy::<QV>(n, 16, RELAXED_MUT_DROP),
            t(4),
        ),
        (
            "OV",
            16,
            relaxed_accuracy::<OV>(n, 16, 0),
            relaxed_accuracy::<OV>(n, 16, RELAXED_MUT_DROP),
            t(8),
        ),
    ] {
        println!("relaxed accuracy {name} (squares from {s0}, n {n}): {w:.2e}, control {wc:.2e} (bound {tol:.0e})");
        assert!(w <= tol, "{name} relaxed against naive");
        assert!(wc > tol, "{name} dropped-square control did not fail");
    }
}

/// log2 |coefficient i| on lane l: -rate (1 + 0.05 l) i, plus noise of half-width 1 (the shape of the decay of the
/// D 50000 series)
fn decay<E: Expansion<Native>>(r: &mut Rng, rate: f64, len: usize) -> Vec<E> {
    series(
        r,
        len,
        |i, l, r| -rate * (1.0 + 0.05 * l as f64) * i as f64 + 2.0 * (r.unit() - 0.5),
        |_, _| false,
    )
}

/// c_k of the factors promoted to octo-double, summed in octo-double
fn sum_at_od(ao: &[OV], bo: &[OV], k: usize) -> OV {
    let mut acc = <OV as Expansion<Native>>::acc_zero();
    for i in k.saturating_sub(bo.len() - 1)..ao.len().min(k + 1) {
        <OV as Expansion<Native>>::acc_prod(&mut acc, &ao[i], &bo[k - i]);
    }
    <OV as Expansion<Native>>::acc_fin(&acc)
}

/// c_k summed in the expansion itself, as the term-by-term route does
fn sum_at<E: Expansion<Native>>(a: &[E], b: &[E], k: usize) -> E {
    let mut acc = E::acc_zero();
    for i in k.saturating_sub(b.len() - 1)..a.len().min(k + 1) {
        E::acc_prod(&mut acc, &a[i], &b[k - i]);
    }
    E::acc_fin(&acc)
}

/// the outputs to check of a la x lb product to n: around the multiples of 4096 (where the tiles meet), the last, and
/// six more at random
fn sample_ks(la: usize, lb: usize, n: usize, seed: u64) -> Vec<usize> {
    let top = n.min(la + lb - 1);
    let mut ks: Vec<usize> = vec![0, 1, top - 1];
    for m in 1..=top / 4096 + 1 {
        ks.extend([4096 * m - 1, 4096 * m, 4096 * m + 1]);
    }
    let mut r = Rng(seed);
    ks.extend((0..6).map(|_| (r.next_u64() % top as u64) as usize));
    ks.retain(|&k| k < top);
    ks.sort_unstable();
    ks.dedup();
    ks
}

/// What `sampled` found
struct Sampled {
    /// the largest |x_k - c_k| / sum_i |a_i| |b_(k-i)| over the lanes, c_k the sum in octo-double
    fast: f64,
    /// the same for the sum formed in the expansion itself (the term-by-term route's)
    naive: f64,
    /// the same for |x_k - the sum in the expansion|
    apart: f64,
    /// the lanes whose parts differ in any bit from the sum in the expansion, of all lanes sampled
    differ: usize,
    lanes: usize,
}

/// `Sampled` over the sampled outputs ks
fn sampled<E: Expansion<Native>>(x: &[E], ks: &[usize], a: &[E], b: &[E]) -> Sampled {
    let (ao, bo): (Vec<OV>, Vec<OV>) = (a.iter().map(promote).collect(), b.iter().map(promote).collect());
    let ah: Vec<[f64; 8]> = a.iter().map(|v| v.part(0).to_array().map(f64::abs)).collect();
    let bh: Vec<[f64; 8]> = b.iter().map(|v| v.part(0).to_array().map(f64::abs)).collect();
    let mut out = Sampled {
        fast: 0.0,
        naive: 0.0,
        apart: 0.0,
        differ: 0,
        lanes: 8 * ks.len(),
    };
    let rel = |d: f64, ab: f64, w: f64| {
        if d.is_finite() && ab > 0.0 {
            w.max(d / ab)
        } else {
            f64::INFINITY
        }
    };
    for &k in ks {
        let (yo, ye) = (sum_at_od(&ao, &bo, k).lanes(), sum_at::<E>(a, b, k));
        for l in 0..8 {
            let xl = od_lane(&x[k], l);
            let ab: f64 = (k.saturating_sub(b.len() - 1)..a.len().min(k + 1))
                .map(|i| ah[i][l] * bh[k - i][l])
                .sum();
            let yl = od_lane(&ye, l);
            out.fast = rel((xl - yo[l]).to_f64().abs(), ab, out.fast);
            out.naive = rel((yl - yo[l]).to_f64().abs(), ab, out.naive);
            out.apart = rel((xl - yl).to_f64().abs(), ab, out.apart);
            if xl.x != yl.x {
                out.differ += 1;
            }
        }
    }
    out
}

/// One planted pair of factors la x lb terms with decay rates (ra, rb), to n outputs, by `mullow` under a control
/// (0 none): the stats, and `sampled`
fn tiled_case<E: Expansion<Native>>(
    (ra, rb): (f64, f64),
    (la, lb): (usize, usize),
    n: usize,
    mutate: u8,
) -> (Stats, Sampled) {
    let mut r = Rng(31 + la as u64 + 7 * lb as u64);
    let a: Vec<E> = decay(&mut r, ra, la);
    let b: Vec<E> = decay(&mut r, rb, lb);
    let cfg = Cfg {
        mutate,
        ..Cfg::default()
    };
    let (x, st) = fast(&a, &b, n, &cfg);
    let ks = sample_ks(la, lb, n, 5);
    (st, sampled(&x, &ks, &a, &b))
}

/// A tiled case: decay rates of a and b, their lengths, and the outputs
type Tcase = ((f64, f64), (usize, usize), usize);

/// The cases: the D 50000 square of side 8192 at ks 27918 (decay 0.02, 11535 outputs of the 16383); a flat series;
/// mismatched rates, both ways (blocks of unequal lengths); unequal lengths cut short by n
const TILED: [Tcase; 5] = [
    ((0.02, 0.02), (8192, 8192), 11535),
    ((0.0, 0.0), (8192, 8192), 16383),
    ((0.02, 0.007), (8192, 8192), 16383),
    ((0.007, 0.02), (8193, 8192), 16384),
    ((0.01, 0.01), (9000, 5000), 12000),
];

/// the smallest case, a pair of 8193 terms (4097 x 4097, 2 tiles of each factor), for the controls that are slow by
/// the old route
const TILED_SMALL: Tcase = ((0.0, 0.0), (4097, 4097), 8193);

/// Pairs of more than 2^13 terms (past the FFT tables) by tiles: at the sampled outputs within the bound of the
/// octo-double sums relative to sum |a_i| |b_(k-i)|, every pair convolved (tiles formed, none term by term, none
/// missing the 1/4 check). Reported: the error of the sums formed in the expansion itself (the old route's), and how
/// far the product lies from them
fn tiled_ok<E: Expansion<Native>>(cases: &[Tcase]) -> bool {
    let tol = bound::<E>();
    let mut ok = true;
    for &(rates, len, n) in cases {
        let (st, sm) = tiled_case::<E>(rates, len, n, 0);
        println!(
            "tiled {} rates {rates:?} {len:?} n {n}: error {:.1e} (bound {tol:.0e}), of the sums in the expansion \
             {:.1e}, apart {:.1e} with {} of {} lanes differing in a bit; pairs {} tiles {} direct {} fallback {} \
             blocks {:?} max_frac {:.3}",
            E::NP,
            sm.fast,
            sm.naive,
            sm.apart,
            sm.differ,
            sm.lanes,
            st.pairs,
            st.tiles,
            st.direct,
            st.fallback,
            st.blocks,
            st.max_frac
        );
        ok &= sm.fast <= tol && st.tiles > 0 && st.direct == 0 && st.fallback == 0 && st.max_frac < 0.25;
    }
    ok
}

/// The pair just short of the tables, 4096 x 4096 (8191 terms, one convolution), the route the tiles' pairs take: its
/// error is the one the tiles' should match, and no tile is formed
fn untiled_ok<E: Expansion<Native>>() -> bool {
    let (rates, len, n) = ((0.0, 0.0), (4096, 4096), 8191);
    let (st, sm) = tiled_case::<E>(rates, len, n, 0);
    println!(
        "untiled {} {len:?} n {n}: error {:.1e}, of the sums in the expansion {:.1e}; pairs {} tiles {} direct {}",
        E::NP,
        sm.fast,
        sm.naive,
        st.pairs,
        st.tiles,
        st.direct
    );
    sm.fast <= bound::<E>() && st.tiles == 0 && st.direct == 0 && st.fallback == 0
}

/// the controls: the first tile dropped puts the result past the bound; the old route (MUT_NO_TILE) forms the pair
/// term by term, so `direct` and no tiles, which `tiled_ok` fails (not for octo-double, where it takes half a minute)
fn tiled_controls<E: Expansion<Native>>(old_route: bool) {
    let tol = bound::<E>();
    let (rates, len, n) = TILED_SMALL;
    let (st, sm) = tiled_case::<E>(rates, len, n, MUT_DROP_TILE);
    println!("tiled {} dropped tile: error {:.1e} tiles {}", E::NP, sm.fast, st.tiles);
    assert!(sm.fast > tol, "{} the dropped-tile control did not fail", E::NP);
    if old_route {
        let (st, sm) = tiled_case::<E>(rates, len, n, MUT_NO_TILE);
        println!(
            "tiled {} old route: error {:.1e} direct {} tiles {}",
            E::NP,
            sm.fast,
            st.direct,
            st.tiles
        );
        assert!(sm.fast <= tol, "{} the old route is within the bound", E::NP);
        assert!(
            st.direct > 0 && st.tiles == 0,
            "{} the old-route control did not fail",
            E::NP
        );
    }
}

#[test]
fn mullow_tiled() {
    // under emulation only the smallest case, and not octo-double
    let cases: &[Tcase] = if common::emulated() { &[TILED_SMALL] } else { &TILED };
    assert!(
        untiled_ok::<DV>() && untiled_ok::<QV>() && untiled_ok::<OV>(),
        "the untiled pair"
    );
    assert!(tiled_ok::<DV>(cases), "DV");
    assert!(tiled_ok::<QV>(cases), "QV");
    if !common::emulated() {
        assert!(tiled_ok::<OV>(cases), "OV");
    }
    tiled_controls::<DV>(true);
    tiled_controls::<QV>(true);
    if !common::emulated() {
        tiled_controls::<OV>(false);
    }
}