multidouble 5.0.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
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
//! `exp` and `ln` of the double-double, quad-double and octo-double types against a reference far more accurate than
//! 2^-424 that shares no code with them (`exp_ref`): e^x for an exact dyadic x, by its Taylor series at x 2^-12 in
//! fixed point with P = 1700 fractional bits, then twelve squarings in floating point with P + 64-bit significands,
//! with a rigorous bound on its error, below 2^-1600 relative. ln is measured through it: for the computed y and
//! d = y - ln x, |e^y - x| / x = |e^d - 1| stands for |d|, within a factor in [1 - |d|/2, 1 + |d|] for |d| <= 1. Within
//! the bounds checked here |d| < 2^-90 (2^-40 for `ln_f64`), so the factor is that close to 1, and a result with a
//! larger d is far off and fails.
//!
//! The reference is checked against planted truth (`reference_is_planted_truth`): e = sum 1/k! in exact integer
//! arithmetic, within 2^-1550, and e^x to 470 of the 700 digits that Python's decimal module gives
//! (scripts/explog_vectors.py, tests/data/explog_vectors.txt); a reference cut to 60 Taylor terms must fail both.
//!
//! The bounds, with u = 2^-53, are checked on every input that meets their hypotheses on x (`covered`; for exp, on
//! every input): the proven ones of the paper's Theorems 40 and 42 and Proposition 44 (paper/explog_consts.py),
//! `OD::exp` 3.661 u^8 e^x + 8 2^-1074 and `OD::ln` 9.926 u^8 |ln x| + 2^-509, `DD::exp` 5.465 u^2 e^x + 2 2^-1074 and
//! `DD::ln` 15.351 u^2 |ln x| + 2^-1071, for x on the ladder |x_i| <= (2u)^i |x_0| (for the double-double a
//! double-word) with normal parts; the theorems also ask that no product inside the run underflow, apart from those
//! that only choose k, m, the degree or a branch; an emulator scanning every binade of x finds none
//! (paper/explog_bits.py). Where `OD::ln`'s absolute term dominates, near x = 1, it is held to its relative constant as
//! well, a tested threshold. The quad-double, which has no proven bounds, is held to 64 u^4 relative, fixed before the
//! first run (exp with the absolute term 4 2^-1074 as well). The start of ln, `explog::ln_f64`, is checked against its
//! proven 5.915 u. Inputs, n per category for the double-double, n/2 for the quad-double and n/4 for the octo-double:
//! random over the range, near 0, near the reduction's boundaries (k + 1/2) ln 2 and its multiples k ln 2, near
//! overflow (with tails past it) and underflow, near 1 and within 3 ulps of f64::MAX (with tails near half an ulp) for
//! ln, subnormal, with gaps and zero parts, at the top of the ladder (overlapping by a bit, not greedy, with random
//! signs; not for the double-double, which must be a double-word), and the special values.
//!
//! ```text
//! cargo test --release --test explog                        the checks (EXPLOG_REPORT=1 prints the worst cases)
//! cargo test --release --test explog --features mutate      and their controls (`explog::mutate`)
//! cargo test --release --test explog timing -- --ignored --nocapture
//! ```
mod common;
use common::Rng;
use multidouble::dd::DD;
use multidouble::exact::{BigU, Dy};
use multidouble::native;
use multidouble::od::OD;
use multidouble::qd::QD;

/// the reference's fixed-point fractional bits
const P: u64 = 1700;
/// the reference's squarings: |x| 2^-12 <= 0.182 for |x| <= 745.2
const SQ: u32 = 12;

/// the proven bounds (paper/explog_consts.py), in units of u^n; exp's add n 2^-1074, ln's 2^FLOOR
const OD_EXP: f64 = 3.661;
const OD_LN: f64 = 9.926;
const OD_LN_FLOOR: i32 = -509;
const DD_EXP: f64 = 5.465;
const DD_LN: f64 = 15.351;
const DD_LN_FLOOR: i32 = -1071;
/// tested, not proven: `OD::ln` relative to |ln x| where the proven bound's absolute term dominates
const OD_LN_TESTED: f64 = OD_LN;
/// the start `ln_f64`'s proven bound, in units of u
const LN_F64: f64 = 5.915;
/// arguments of `ln_f64` checked besides the random ones: 0x1.22e14015c154dp+0, near its worst found by a search
const START_PINNED: [u64; 1] = [0x3ff2_2e14_015c_154d];
/// the quad-double thresholds, fixed before the first run
const QD_EXP: f64 = 64.0;
const QD_LN: f64 = 64.0;

// -------------------------------------------------------------------------------------------------------------
// the reference
// -------------------------------------------------------------------------------------------------------------

/// e^x = m 2^e within 2^err2 relative (err2 a log2 bound)
struct Ref {
    m: BigU,
    e: i64,
    err2: f64,
}

impl Ref {
    fn dy(&self) -> Dy {
        Dy {
            neg: false,
            m: self.m.clone(),
            e: self.e,
        }
    }
}

/// the reference's domain: |x| 2^-12 <= 0.182, where its Taylor series converges as its error bound assumes
const REF_MAX: f64 = 745.2;

/// e^x for an exact dyadic x with |x| <= REF_MAX, or None outside it, so that a result far off fails at once instead of
/// running the series where its terms grow. y = x 2^-12, Y = floor(|y| 2^P); the terms T_k = floor(T_(k-1) Y /
/// (2^P k)) of e^(+-Y 2^-P) are each within 2 units below the true ones (T_(k-1) is within 2 units, and Y 2^-P / k <
/// 1), so the K terms' sum S is within 2K + 4 units of e^(Y 2^-P) 2^P counting the tail after the first zero term,
/// and |y - Y 2^-P| < 2^-P adds 2 units. S >= 0.83 2^P. Each squaring doubles the relative error and its truncation
/// to P + 64 bits adds 2^-(P + 63). cut: keep only that many terms (the control); a series cut before its first zero
/// term has no certified bound, err2 = +inf.
fn exp_ref(x: &Dy, cut: Option<usize>) -> Option<Ref> {
    if x.to_f64().abs() > REF_MAX {
        return None;
    }
    let sh = x.e + P as i64 - SQ as i64;
    let y = if sh >= 0 {
        x.m.shl(sh as u64)
    } else {
        x.m.shr((-sh) as u64)
    };
    let one = BigU::from_u64(1).shl(P);
    let (mut pos, mut neg) = (one.clone(), BigU::zero());
    let mut t = one;
    let mut k = 0u64;
    let mut early = false;
    loop {
        k += 1;
        if cut.is_some_and(|c| k as usize >= c) {
            early = true;
            break;
        }
        t = t.mul(&y).shr(P).divrem_small(k).0;
        if t.is_zero() {
            break;
        }
        if x.neg && k % 2 == 1 {
            neg = neg.add(&t);
        } else {
            pos = pos.add(&t);
        }
    }
    let mut m = pos.sub(&neg);
    let mut e = -(P as i64);
    let mut err2 = ((2 * k + 6) as f64 / 0.83).log2() - P as f64;
    for _ in 0..SQ {
        m = m.mul(&m);
        e *= 2;
        let b = m.bits();
        if b > P + 64 {
            m = m.shr(b - P - 64);
            e += (b - P - 64) as i64;
        }
        // 2 eps + eps^2 + 2^-(P + 63) <= 2^(err2 + 1.001) while err2 < -100
        err2 = (err2 + 1.0).max(-(P as f64) - 63.0) + 0.001;
    }
    if early {
        err2 = f64::INFINITY;
    }
    Some(Ref { m, e, err2 })
}

/// the decimal d.ddd...e+NNN as (digits, power of ten)
fn decimal(s: &str) -> (BigU, i64) {
    let (mant, ex) = s.split_once('e').unwrap();
    let digits: String = mant.chars().filter(|c| c.is_ascii_digit()).collect();
    let frac = mant.split_once('.').map_or(0, |(_, f)| f.len()) as i64;
    (BigU::from_dec(&digits), ex.parse::<i64>().unwrap() - frac)
}

/// |m 2^e - d 10^p| / (d 10^p) <= 10^-digits, in integers
fn agrees(r: &Ref, d: &BigU, p: i64, digits: u64) -> bool {
    // compare m 2^e 10^-p with d: scale both to integers
    let (mut a, mut b) = (r.m.clone(), d.clone());
    if r.e >= 0 {
        a = a.shl(r.e as u64);
    } else {
        b = b.shl((-r.e) as u64);
    }
    if p >= 0 {
        b = b.mul(&BigU::pow_small(10, p as u64));
    } else {
        a = a.mul(&BigU::pow_small(10, (-p) as u64));
    }
    let diff = if a.cmp(&b) == std::cmp::Ordering::Less {
        b.sub(&a)
    } else {
        a.sub(&b)
    };
    diff.mul(&BigU::pow_small(10, digits)).cmp(&b) != std::cmp::Ordering::Greater
}

/// e as sum_(k <= 320) 1/k! in integers: e 320! = sum 320!/k! + (a tail below 1), within 2^-1550 relative
fn e_check(r: &Ref) -> bool {
    let n = 320u64;
    let mut f = BigU::from_u64(1);
    for k in 2..=n {
        f = f.mul_small(k);
    }
    let mut s = BigU::zero();
    let mut q = f.clone();
    for k in 0..=n {
        if k > 0 {
            q = q.divrem_small(k).0;
        }
        s = s.add(&q);
    }
    // m 2^e n! against s
    let (mut a, mut b) = (r.m.mul(&f), s);
    if r.e >= 0 {
        a = a.shl(r.e as u64);
    } else {
        b = b.shl((-r.e) as u64);
    }
    let diff = if a.cmp(&b) == std::cmp::Ordering::Less {
        b.sub(&a)
    } else {
        a.sub(&b)
    };
    diff.shl(1550).cmp(&b) != std::cmp::Ordering::Greater
}

fn vectors() -> Vec<(Dy, BigU, i64)> {
    include_str!("data/explog_vectors.txt")
        .lines()
        .map(|l| {
            let (xs, ds) = l.split_once(' ').unwrap();
            let x = xs
                .split(',')
                .map(|h| Dy::from_f64(f64::from_bits(u64::from_str_radix(h, 16).unwrap())))
                .fold(Dy::zero(), |a, b| a.add(&b));
            let (d, p) = decimal(ds);
            (x, d, p)
        })
        .collect()
}

/// whether the reference agrees with e = sum 1/k!, and with each 700-digit decimal value, and |x| of each. The uncut
/// reference must certify its bound below 2^-1600, and no reference may certify a bound that its value misses: a
/// series cut before its first zero term certifies none.
fn planted(cut: Option<usize>) -> (bool, Vec<(bool, f64)>) {
    let e = exp_ref(&Dy::from_f64(1.0), cut).unwrap();
    let e_ok = e_check(&e);
    assert!(e_ok || e.err2 > -1550.0, "e certified within 2^{} and missed", e.err2);
    let vs = vectors()
        .iter()
        .map(|(x, d, p)| {
            let r = exp_ref(x, cut).unwrap();
            if cut.is_none() {
                assert!(r.err2 < -1600.0, "reference error bound 2^{}", r.err2);
            }
            // 10^-470 = 2^-1561, above the reference's bound
            let ok = agrees(&r, d, *p, 470);
            assert!(
                ok || r.err2 > -1561.0,
                "x = {x:?} certified within 2^{} and missed",
                r.err2
            );
            (ok, x.to_f64().abs())
        })
        .collect();
    (e_ok, vs)
}

/// The checks reject malformed results: ln(4) given as (0, 1), a nonzero result with a zero leading part, whose
/// absolute term's share would otherwise be infinite; exp(710) given as -inf or NaN, where only the canonical +inf is
/// right; and +inf for exp(709), below the overflow threshold.
#[test]
fn checks_reject_malformed_results() {
    let four = DD::from_f64(4.0);
    let (e, fl) = ln_err_of(four, DD { hi: 0.0, lo: 1.0 }, DD_LN_FLOOR);
    assert!(e > DD_LN + fl, "ln(4) = (0, 1) passed");
    assert!(
        ln_err_of(four, four.ln(), DD_LN_FLOOR).0 <= DD_LN,
        "the true ln(4) failed"
    );
    let fails = |x: DD, y: DD| exp_fails(x, y, exp_err_of(x, y), DD_EXP);
    let (over, below) = (DD::from_f64(710.0), DD::from_f64(709.0));
    let inf = DD {
        hi: f64::INFINITY,
        lo: 0.0,
    };
    assert!(!fails(over, inf), "+inf for exp(710) failed");
    assert!(fails(over, -inf), "-inf for exp(710) passed");
    assert!(fails(over, DD { hi: f64::NAN, lo: 0.0 }), "NaN for exp(710) passed");
    assert!(
        fails(
            over,
            DD {
                hi: f64::INFINITY,
                lo: 1.0
            }
        ),
        "(+inf, 1) for exp(710) passed"
    );
    assert!(fails(below, inf), "+inf for exp(709) passed");
    assert!(!fails(below, below.exp()), "the true exp(709) failed");
}

/// x on H(2) whose f = x - 1, exact, is led by a part below half its value: e_f of the leading part does not bound |f|,
/// while `bound_expo`'s U does, sum |f_i| (1 + 2^-48) < 2^(e + 1); and ln x is within its bound
#[test]
fn leading_part_does_not_bound_the_value() {
    let x = OD {
        x: [
            1.0,
            1.5050180741806962e-71,
            -1.0947644252537633e-47,
            1.0947644252537632e-47,
            -1.6444249949300811e-63,
            -4.130301482114151e-79,
            -1.1985091468012028e-94,
            -1.9639758264985916e-110,
        ],
    };
    assert!(ladder(x));
    let f = x.add_f(-1.0);
    let fd = exact(f);
    assert!(fd.sub(&exact(x).sub(&Dy::from_f64(1.0))).is_zero(), "x - 1 is exact");
    let expo = |h: f64| ((h.to_bits() >> 52) & 0x7ff) as i32 - 1023;
    let lead = Dy::from_f64(pow2i(expo(f.x[0]) + 1));
    assert_eq!(
        fd.cmp_abs(&lead),
        std::cmp::Ordering::Greater,
        "|f| above 2^(e+1) of its leading part"
    );
    let u = f.x.iter().fold(0.0, |a, v| a + v.abs()) * (1.0 + 2f64.powi(-48));
    assert_eq!(
        fd.cmp_abs(&Dy::from_f64(pow2i(expo(u) + 1))),
        std::cmp::Ordering::Less,
        "|f| below U's bound"
    );
    let (e, fl) = ln_err(x, OD_LN_FLOOR);
    assert!(e <= OD_LN + fl && e <= OD_LN_TESTED, "ln x errs by {e} u^8");
}

#[test]
fn reference_is_planted_truth() {
    let (e, vs) = planted(None);
    assert!(e && vs.iter().all(|v| v.0), "e {e}, decimal values {vs:?}");
    // the domain ends at REF_MAX: a wrong ln result such as 800 or 2^600 is refused at once
    assert!(exp_ref(&Dy::from_f64(REF_MAX), None).is_some() && exp_ref(&Dy::from_f64(-REF_MAX), None).is_some());
    assert!(exp_ref(&Dy::from_f64(800.0), None).is_none() && exp_ref(&Dy::from_f64(2f64.powi(600)), None).is_none());
    // control: 60 Taylor terms of e^(x/4096) err by about 2^-990 for |x| near 1, which fails both checks wherever
    // |x| >= 1/4 (smaller x need fewer terms)
    let (e, vs) = planted(Some(60));
    let big: Vec<&(bool, f64)> = vs.iter().filter(|v| v.1 >= 0.25).collect();
    assert!(
        !e && big.len() >= 10 && big.iter().all(|v| !v.0),
        "cut reference: e {e}, {vs:?}"
    );
}

// -------------------------------------------------------------------------------------------------------------
// the types, generically
// -------------------------------------------------------------------------------------------------------------

/// what the checks need of each type
trait Ty: Copy + std::fmt::Debug {
    const N: usize;
    const NAME: &'static str;
    fn parts(self) -> Vec<f64>;
    fn from_parts(p: &[f64]) -> Self;
    fn exp(self) -> Self;
    fn ln(self) -> Self;
}

impl Ty for DD {
    const N: usize = 2;
    const NAME: &'static str = "dd";
    fn parts(self) -> Vec<f64> {
        vec![self.hi, self.lo]
    }
    fn from_parts(p: &[f64]) -> DD {
        DD { hi: p[0], lo: p[1] }
    }
    fn exp(self) -> DD {
        DD::exp(self)
    }
    fn ln(self) -> DD {
        DD::ln(self)
    }
}

impl Ty for QD {
    const N: usize = 4;
    const NAME: &'static str = "qd";
    fn parts(self) -> Vec<f64> {
        self.x.to_vec()
    }
    fn from_parts(p: &[f64]) -> QD {
        QD {
            x: [p[0], p[1], p[2], p[3]],
        }
    }
    fn exp(self) -> QD {
        QD::exp(self)
    }
    fn ln(self) -> QD {
        QD::ln(self)
    }
}

impl Ty for OD {
    const N: usize = 8;
    const NAME: &'static str = "od";
    fn parts(self) -> Vec<f64> {
        self.x.to_vec()
    }
    fn from_parts(p: &[f64]) -> OD {
        OD {
            x: std::array::from_fn(|i| p[i]),
        }
    }
    fn exp(self) -> OD {
        OD::exp(self)
    }
    fn ln(self) -> OD {
        OD::ln(self)
    }
}

fn exact<T: Ty>(x: T) -> Dy {
    Dy::sum(&x.parts())
}

/// the greedy n-part expansion of v: x_0 = RN(v), x_1 = RN(v - x_0), ...
fn greedy<T: Ty>(v: &Dy) -> T {
    let mut r = v.clone();
    let mut p = vec![0.0; T::N];
    for x in p.iter_mut() {
        let c = r.to_f64();
        *x = c;
        if c == 0.0 || !c.is_finite() {
            break;
        }
        r = r.sub(&Dy::from_f64(c));
    }
    T::from_parts(&p)
}

/// 2^e for -1074 <= e <= 1023, from its bits (in a debug build 2f64.powi(-1070) is 0, as 2^1070 overflows first)
fn pow2i(e: i32) -> f64 {
    if e >= -1022 {
        f64::from_bits(((e + 1023) as u64) << 52)
    } else {
        f64::from_bits(1 << (e + 1074))
    }
}

/// ulp(h): the gap below |h| to the next f64, for an |h| that is not a power of two (finite at f64::MAX)
fn ulp(h: f64) -> f64 {
    h.abs() - f64::from_bits(h.abs().to_bits() - 1)
}

/// whether e^x is at least f64::MAX + ulp(f64::MAX)/2, where a double rounds to +inf
fn overflows<T: Ty>(x: T) -> bool {
    let lim = Dy::from_f64(f64::MAX).add(&Dy::from_f64(pow2i(970)));
    let r = exp_ref(&exact(x), None).expect("exp's inputs lie in the reference's domain");
    r.dy().cmp_abs(&lim) != std::cmp::Ordering::Less
}

/// whether y fails as exp(x), with err = exp_err_of(x, y): a finite y must be within C u^n e^x + n 2^-1074 (`bound`
/// C), and a non-finite one must be the canonical +inf, (+inf, 0, ...), and only where e^x is at least f64::MAX + ulp/2
fn exp_fails<T: Ty>(x: T, y: T, err: (f64, f64, bool, bool), bound: f64) -> bool {
    let (e, fl, finite, _) = err;
    if finite {
        return e > bound + fl;
    }
    let p = y.parts();
    let canonical = p[0] == f64::INFINITY && p[1..].iter().all(|v| *v == 0.0);
    !(canonical && overflows(x))
}

/// the unit u^n
fn un<T: Ty>() -> f64 {
    2f64.powi(-53 * T::N as i32)
}

/// the proven bounds' hypotheses on x: on the ladder |x_i| <= (2u)^i |x_0| (for the double-double, a double-word, x_h
/// = RN(x_h + x_l), as the published bounds ask), with every nonzero part normal, so that no product of a subnormal
/// part leaves (A2)
fn ladder<T: Ty>(x: T) -> bool {
    let p = x.parts();
    let a0 = p[0].abs();
    let shape = if T::N == 2 {
        p[0] + p[1] == p[0]
    } else {
        (1..p.len()).all(|i| p[i].abs() <= a0 * 2f64.powi(-52 * i as i32))
    };
    p.iter().all(|v| *v == 0.0 || v.is_normal()) && shape
}

/// ln 2 to 1700 bits from sum 1/(j 2^j), a value within (P + 1) 2^-P below ln 2: independent of the crate's constants
fn ln2() -> Dy {
    let mut s = BigU::zero();
    for j in 1..=P {
        s = s.add(&BigU::from_u64(1).shl(P - j).divrem_small(j).0);
    }
    Dy {
        neg: false,
        m: s,
        e: -(P as i64),
    }
}

/// a random exact value of 300 random bits times 2^e (sign included)
fn rand_dy(r: &mut Rng, e: i64, neg: bool) -> Dy {
    let mut m = BigU::from_u64(r.next_u64() | 1 << 63);
    for _ in 0..4 {
        m = m.shl(64).add(&BigU::from_u64(r.next_u64()));
    }
    Dy { neg, m, e: e - 319 }
}

/// rand_dy with an exponent uniform in [lo, hi) and, if signed, a random sign
fn rand_in(r: &mut Rng, lo: i64, hi: i64, signed: bool) -> Dy {
    let e = lo + (r.next_u64() % (hi - lo) as u64) as i64;
    let neg = signed && r.next_u64() & 1 == 0;
    rand_dy(r, e, neg)
}

/// an input with gaps: random parts at random depths, some zero, on the ladder
fn gapped<T: Ty>(r: &mut Rng, x0: f64) -> T {
    let mut p = vec![0.0; T::N];
    p[0] = x0;
    for (i, x) in p.iter_mut().enumerate().skip(1) {
        if r.next_u64().is_multiple_of(3) {
            continue;
        }
        let depth = 53 * i as i32 + (r.next_u64() % 120) as i32;
        *x = r.sym() * x0.abs() * 2f64.powi(-depth);
    }
    T::from_parts(&p)
}

/// parts at the top of the ladder, |x_i| in ((2u)^i / 2, (2u)^i] |x_0|, with random signs: overlapping by a bit and
/// not greedy, which H(2) allows and `greedy` and `gapped` never produce (not a double-word: for QD and OD only)
fn top_ladder<T: Ty>(r: &mut Rng, x0: f64) -> T {
    let mut p = vec![0.0; T::N];
    p[0] = x0;
    for (i, x) in p.iter_mut().enumerate().skip(1) {
        let s = if r.next_u64() & 1 == 0 { 1.0 } else { -1.0 };
        *x = s * (0.5 + 0.5 * r.unit()) * x0.abs() * 2f64.powi(-52 * i as i32);
    }
    T::from_parts(&p)
}

// -------------------------------------------------------------------------------------------------------------
// the checks
// -------------------------------------------------------------------------------------------------------------

/// the result of one category's checks
#[derive(Debug, Default)]
struct Row {
    what: String,
    n: usize,
    /// inputs within the bound's hypotheses
    covered: usize,
    fails: usize,
    /// the largest error in units of u^n (relative; for ln relative to |ln x|)
    max: f64,
    worst: String,
}

/// for a result y of exp(x), |y - e^x| / e^x in units of u^n, the share of the absolute term n 2^-1074 (the final
/// scaling's loss to the subnormals) in the same units, whether y is finite, and whether its leading part leaves room
/// for n normal parts, |y_0| > 2^(-1022 + 53 (n - 1)), where the worst relative error is recorded
fn exp_err_of<T: Ty>(x: T, y: T) -> (f64, f64, bool, bool) {
    if !y.parts().iter().all(|v| v.is_finite()) {
        return (f64::INFINITY, 0.0, false, false);
    }
    let r = exp_ref(&exact(x), None).expect("exp's inputs lie in the reference's domain");
    let yd = exact(y);
    let e = yd.sub(&r.dy()).ratio(&r.dy()) / un::<T>();
    // n 2^-1074 / e^x / u^n, from its bits: a debug build gives 2f64.powi(-1074) = 0, as 2^1074 overflows first
    let fl = Dy::from_f64(f64::from_bits(T::N as u64)).ratio(&r.dy()) / un::<T>();
    let room = y.parts()[0].abs() > pow2i(-1022 + 53 * (T::N as i32 - 1));
    (e, fl, true, room)
}

/// |ln(x) - ln x| / |ln x| in units of u^n, and the share of an absolute term 2^fl, 2^fl / |ln x| / u^n
fn ln_err<T: Ty>(x: T, fl: i32) -> (f64, f64) {
    ln_err_of(x, x.ln(), fl)
}

/// ln_err for a given result y of ln(x)
fn ln_err_of<T: Ty>(x: T, y: T, fl: i32) -> (f64, f64) {
    if !y.parts().iter().all(|v| v.is_finite()) {
        return (f64::INFINITY, 0.0);
    }
    let yd = exact(y);
    let xd = exact(x);
    if yd.is_zero() {
        // only ln 1 may be zero
        let one = xd.sub(&Dy::from_f64(1.0)).is_zero();
        return (if one { 0.0 } else { f64::INFINITY }, 0.0);
    }
    // a nonzero result led by a zero part is malformed (and would make the absolute term's share infinite)
    if y.parts()[0] == 0.0 {
        return (f64::INFINITY, 0.0);
    }
    // a result outside the reference's domain is far from ln x, |ln x| < 745
    let Some(r) = exp_ref(&yd, None) else {
        return (f64::INFINITY, 0.0);
    };
    let e = r.dy().sub(&xd).ratio(&xd.mul(&yd)) / un::<T>();
    let floor = pow2i(fl) / y.parts()[0].abs() / un::<T>();
    (e, floor)
}

/// exp on n inputs of each category
fn run_exp<T: Ty>(n: usize, seed: u64, bound: f64, proven: bool) -> Vec<Row> {
    let mut r = Rng(seed);
    let l2 = ln2();
    let mut rows = Vec::new();
    let mut cats = vec![
        "random",
        "near 0",
        "(k+1/2) ln 2",
        "k ln 2",
        "overflow",
        "overflow edge",
        "underflow",
        "gaps",
    ];
    if T::N > 2 {
        cats.push("top ladder");
    }
    for cat in cats {
        let mut row = Row {
            what: format!("{} exp {cat}", T::NAME),
            ..Row::default()
        };
        for _ in 0..n {
            let x: T = match cat {
                "random" => {
                    let v = -740.0 + 1445.0 * r.unit();
                    greedy(&Dy::from_f64(v).add(&rand_in(&mut r, -52, -51, true)))
                }
                "near 0" => greedy(&rand_in(&mut r, -1000, 0, true)),
                "(k+1/2) ln 2" | "k ln 2" => {
                    let k = (r.next_u64() % 2048) as i64 - 1074;
                    let h = if cat == "k ln 2" { 0 } else { 1 };
                    let c = l2.mul(&Dy::from_f64((2 * k + h) as f64)).ldexp(-1);
                    let d = rand_in(&mut r, -420, -20, true);
                    greedy(&c.add(&d))
                }
                "overflow" => greedy(&Dy::from_f64(
                    709.782712893384 - 2f64.powi(-((r.next_u64() % 40) as i32)),
                )),
                "overflow edge" => {
                    // x_0 within 3 ulps of ln f64::MAX, with a tail near half its ulp either way: some results
                    // overflow, as e^x exceeds f64::MAX + ulp/2, and the others must be finite and within the bound
                    let x0 = f64::from_bits(709.782712893384f64.to_bits() - 3 + r.next_u64() % 6);
                    let mut p = vec![0.0; T::N];
                    p[0] = x0;
                    p[1] = r.sym().signum() * ulp(x0) / 2.0 * (1.0 - r.unit() * pow2i(-40));
                    T::from_parts(&p)
                }
                "underflow" => greedy(&Dy::from_f64(-708.0 - 37.0 * r.unit())),
                "gaps" => {
                    let x0 = -745.0 + 1454.0 * r.unit();
                    gapped(&mut r, x0)
                }
                _ => {
                    // over the range, or near 0
                    let x0 = if r.next_u64() & 1 == 0 {
                        -740.0 + 1445.0 * r.unit()
                    } else {
                        r.sym() * pow2i(-((r.next_u64() % 1000) as i32))
                    };
                    top_ladder(&mut r, x0)
                }
            };
            let y = x.exp();
            let err = exp_err_of(x, y);
            let (e, _, finite, room) = err;
            row.n += 1;
            // `covered` counts the results within the proven bound's hypotheses
            let hyp = finite && (!proven || ladder(x));
            row.covered += hyp as usize;
            if exp_fails(x, y, err, bound) {
                row.fails += 1;
            }
            // the worst relative error where the result has room for n normal parts
            if hyp && room && e > row.max {
                row.max = e;
                row.worst = format!("{:?}", x.parts());
            }
        }
        rows.push(row);
    }
    rows
}

/// ln on n inputs of each category
fn run_ln<T: Ty>(n: usize, seed: u64, bound: f64, floor: Option<i32>, proven: bool) -> Vec<Row> {
    let mut r = Rng(seed);
    let mut rows = Vec::new();
    let mut cats = vec![
        "random",
        "near 1",
        "sqrt2 2^k",
        "subnormal",
        "near max",
        "gaps",
        "power of 2",
        "top of range",
    ];
    if T::N > 2 {
        cats.push("top ladder");
    }
    for cat in cats {
        let mut row = Row {
            what: format!("{} ln {cat}", T::NAME),
            ..Row::default()
        };
        for i in 0..n {
            let x: T = match cat {
                "random" => greedy(&rand_in(&mut r, -1021, 1024, false)),
                "near 1" => {
                    let d = rand_in(&mut r, -600, 0, true);
                    greedy(&Dy::from_f64(1.0).add(&d))
                }
                "sqrt2 2^k" => {
                    // x near 2^(k + 1/2), where the reduction of exp(-y0) changes k
                    let k = (r.next_u64() % 2000) as i64 - 1000;
                    let s = Dy::from_f64(std::f64::consts::SQRT_2);
                    let d = rand_in(&mut r, -150, -50, true);
                    greedy(&s.add(&d).ldexp(k))
                }
                "subnormal" => greedy(&Dy::from_f64(f64::from_bits(1 + r.next_u64() % (1 << 52)))),
                "near max" => greedy(&Dy::from_f64(f64::MAX * (1.0 - r.unit() * 2f64.powi(-20)))),
                "gaps" => {
                    let x0 = (1.0 + r.unit()) * 2f64.powi((r.next_u64() % 1200) as i32 - 600);
                    gapped(&mut r, x0)
                }
                "top of range" => {
                    // x_0 within 3 ulps of f64::MAX and a tail near half its ulp, where a double-double of x itself
                    // could round past f64::MAX; the first is the input that found it, and a subnormal last part
                    // takes some off the ladder
                    let mut p = vec![0.0; T::N];
                    p[0] = f64::from_bits(f64::MAX.to_bits() - r.next_u64() % 4);
                    p[1] = ulp(p[0]) / 2.0
                        * match r.next_u64() % 4 {
                            0 => -1.0,
                            1 => 1.0 - r.unit() * pow2i(-40),
                            _ => 1.0,
                        };
                    if T::N > 2 {
                        p[2] = r.sym() * pow2i(915 - (r.next_u64() % 40) as i32);
                    }
                    if r.next_u64().is_multiple_of(4) {
                        p[T::N - 1] = pow2i(-1074);
                    }
                    if i == 0 && T::N > 2 {
                        p = vec![0.0; T::N];
                        (p[0], p[1], p[2]) = (f64::MAX, pow2i(970), -pow2i(915));
                    }
                    T::from_parts(&p)
                }
                "top ladder" => {
                    // over the range, near 1, or near sqrt 2
                    let x0 = match r.next_u64() % 3 {
                        0 => (1.0 + r.unit()) * 2f64.powi((r.next_u64() % 1200) as i32 - 600),
                        1 => 1.0 + r.sym() * pow2i(-((r.next_u64() % 52) as i32) - 1),
                        _ => std::f64::consts::SQRT_2 * (1.0 + r.sym() * 2f64.powi(-40)),
                    };
                    top_ladder(&mut r, x0)
                }
                _ => {
                    // 2^k for -1074 <= k <= 1023, subnormal powers included
                    let k = (r.next_u64() % 2098) as i64 - 1074;
                    let b = if k >= -1022 {
                        ((k + 1023) as u64) << 52
                    } else {
                        1u64 << (k + 1074)
                    };
                    greedy(&Dy::from_f64(f64::from_bits(b)))
                }
            };
            let (e, fl) = ln_err(x, floor.unwrap_or(-1074));
            row.n += 1;
            // a single subnormal part is scaled exactly before any operation
            let hyp = !proven || ladder(x) || x.parts()[1..].iter().all(|v| *v == 0.0);
            let lim = bound + if floor.is_some() { fl } else { 0.0 };
            if hyp {
                row.covered += 1;
                // where the absolute term dominates, the octo-double is held to a tested relative threshold too
                if e > lim || (floor == Some(OD_LN_FLOOR) && e > OD_LN_TESTED) {
                    row.fails += 1;
                }
                if e > row.max {
                    row.max = e;
                    row.worst = format!("{:?}", x.parts());
                }
            } else if !e.is_finite() {
                // outside the hypotheses a result may be inaccurate, but never infinite or NaN
                row.fails += 1;
            }
        }
        rows.push(row);
    }
    rows
}

/// the start's bound (paper, Lemma 43): `ln_f64` within `LN_F64` u of ln h relative (ln_f64(1) = 0 exactly), on
/// random normal and subnormal arguments, near 1, near the boundaries sqrt(2) 2^k of its reduction, at powers of 2 and
/// at `START_PINNED`; the count of misses and the worst error in units of u, with its result times 1 + nudge (the
/// control)
fn start_bound(n: usize, seed: u64, nudge: f64) -> (usize, f64) {
    let mut r = Rng(seed);
    let (mut bad, mut worst) = (0, 0.0f64);
    for i in 0..n + START_PINNED.len() {
        let k = (r.next_u64() % 2046) as i32 - 1022;
        let h = match i % 5 {
            _ if i >= n => f64::from_bits(START_PINNED[i - n]),
            0 => (1.0 + r.unit()) * 2f64.powi(k),
            1 => 1.0 + r.sym() * pow2i(-((r.next_u64() % 53) as i32)),
            2 => f64::from_bits(1 + r.next_u64() % (1 << 52)),
            3 => std::f64::consts::SQRT_2 * (1.0 + r.sym() * pow2i(-((r.next_u64() % 53) as i32))) * 2f64.powi(k),
            _ => pow2i((r.next_u64() % 2098) as i32 - 1074),
        };
        if !(h > 0.0 && h.is_finite()) {
            continue;
        }
        let l = multidouble::explog::ln_f64(h) * (1.0 + nudge);
        if l == 0.0 {
            bad += (h != 1.0) as usize;
            continue;
        }
        // |e^l - h| / (h |l|) = |e^(l - ln h) - 1| / |l| ~ |l - ln h| / |l|
        let Some(rf) = exp_ref(&Dy::from_f64(l), None) else {
            bad += 1;
            continue;
        };
        let e = rf
            .dy()
            .sub(&Dy::from_f64(h))
            .ratio(&Dy::from_f64(h).mul(&Dy::from_f64(l)))
            / 2f64.powi(-53);
        worst = worst.max(e);
        bad += (e > LN_F64) as usize;
    }
    (bad, worst)
}

fn report() -> bool {
    std::env::var("EXPLOG_REPORT").map(|v| v == "1").unwrap_or(false)
}

fn show(rows: &[Row]) {
    for r in rows {
        println!(
            "{:<28} n {:>5} covered {:>5} fails {:>4} worst {:.3e} u^n {}",
            r.what,
            r.n,
            r.covered,
            r.fails,
            r.max,
            if report() { &r.worst } else { "" }
        );
    }
}

fn all_rows(n: usize, seed: u64) -> Vec<Row> {
    let mut rows = run_exp::<DD>(n, seed, DD_EXP, true);
    rows.extend(run_ln::<DD>(n, seed, DD_LN, Some(DD_LN_FLOOR), true));
    rows.extend(run_exp::<QD>(n / 2, seed, QD_EXP, false));
    rows.extend(run_ln::<QD>(n / 2, seed, QD_LN, None, false));
    rows.extend(run_exp::<OD>(n / 4, seed, OD_EXP, true));
    rows.extend(run_ln::<OD>(n / 4, seed, OD_LN, Some(OD_LN_FLOOR), true));
    rows
}

/// The inputs of the paper's Table V for the octo-double exp and ln (paper/table1_inputs.py), with the floors their
/// errors reach in units of u^8: x chosen so that f(x) lies just past 2^E (1 + u + ... + u^7) and the result is 2^E (1,
/// u, ..., u^7) up to sign, the ninth part that the renormalization drops near u^8 2^E.
const OD_TABLE1: [(bool, [u64; 8], f64); 2] = [
    (
        true,
        [
            0x3cd1fffffffffffe,
            0xb97dffffffffffe3,
            0x3617fffffffffe78,
            0x3290000000005734,
            0xaf399999999ea6ee,
            0xabd1111110c3fec7,
            0x286c09c092a9409b,
            0x25442702796c90a9,
        ],
        0.999999999999,
    ),
    (
        false,
        [
            0x3fef80feabfeefa4,
            0x3c7804fa722c50e6,
            0xb90d33e543ae606f,
            0xb5a9c51096bddb1f,
            0xb220568c7ee0a0ac,
            0xaeb561138ac7c121,
            0x2b51118a13c0f359,
            0x27f77c36d87db0bc,
        ],
        0.99998,
    ),
];

/// `OD::exp` and `OD::ln` on the inputs of the paper's Table V. Pass: each input on the ladder, each result 2^E (1, u,
/// ..., u^7) up to sign, scalar and lanes alike, and the check, the error between its floor and the proven bound.
/// Controls: the check rejects a wrong result past the bound (the last part 16 2^E u^8 further out) and one far more
/// accurate (the greedy eight parts of the sum of the result's parts and 2^E u^8), and each input with its leading part
/// one ulp up errs by less than its floor.
#[test]
fn od_table1_inputs() {
    let u = 2f64.powi(-53);
    // the error of y as f(x) in units of u^8
    let err = |exp: bool, x: OD, y: OD| -> f64 {
        if exp {
            exp_err_of(x, y).0
        } else {
            ln_err_of(x, y, OD_LN_FLOOR).0
        }
    };
    for (exp, b, floor) in OD_TABLE1 {
        let name = if exp { "exp" } else { "ln" };
        let x = OD {
            x: b.map(f64::from_bits),
        };
        assert!(ladder(x), "{name}: input off the ladder");
        let bound = if exp { OD_EXP } else { OD_LN };
        let check = |y: OD| -> (f64, bool) {
            let e = err(exp, x, y);
            (e, e >= floor && e <= bound)
        };
        let (y, lane) = if exp {
            (x.exp(), native::OV::splat(x).exp().lane(0))
        } else {
            (x.ln(), native::OV::splat(x).ln().lane(0))
        };
        let top = y.x[0];
        let pow = f64::from_bits(top.abs().to_bits() & 0x7ff0_0000_0000_0000).copysign(top);
        let shape = (1..8).all(|k| y.x[k] == pow * u.powi(k as i32));
        assert!(shape, "{name}: the result is not 2^E (1, u, ..., u^7)");
        assert_eq!(y.x, lane.x, "{name}: lanes differ from the scalar result");
        let (e, ok) = check(y);
        println!("{name}: error {e:.9} u^8, floor {floor}");
        assert!(ok, "{name}: error {e} u^8 outside [{floor}, {bound}]");
        let mut hi = y;
        hi.x[7] += 16.0 * pow * u.powi(8);
        let lo: OD = greedy(&exact(y).add(&Dy::from_f64(pow * u.powi(8))));
        let ((eh, okh), (el, okl)) = (check(hi), check(lo));
        let mut z = x;
        z.x[0] = z.x[0].next_up();
        let ez = err(exp, z, if exp { z.exp() } else { z.ln() });
        println!("  controls: {eh:.3e} and {el:.3e} u^8 for the wrong results, {ez:.3e} u^8 for x one ulp up");
        assert!(!okh && !okl, "{name} control: the check passes a wrong result");
        assert!(
            ez < floor,
            "{name} control: {ez} u^8, the input one ulp up still reaches the floor"
        );
    }
}

#[test]
fn exp_and_ln_within_bounds() {
    let n = std::env::var("EXPLOG_N")
        .ok()
        .and_then(|v| v.parse().ok())
        .unwrap_or(400);
    // n / 4 octo-double inputs per category: fewer than 4 would check none
    assert!(
        n >= 4,
        "EXPLOG_N = {n} leaves the octo-double without inputs; use 4 or more"
    );
    let rows = all_rows(n, 1);
    show(&rows);
    let bad: Vec<&Row> = rows.iter().filter(|r| r.fails > 0).collect();
    assert!(bad.is_empty(), "{bad:#?}");
    // the proven bounds are checked on most inputs, not a few
    // (the top of the range is half off the ladder by design)
    for r in rows
        .iter()
        .filter(|r| !r.what.ends_with("underflow") && !r.what.ends_with("top of range"))
    {
        assert!(
            r.covered * 2 >= r.n,
            "{}: {} of {} inputs within the hypotheses",
            r.what,
            r.covered,
            r.n
        );
    }
    let (bad, worst) = start_bound(4 * n, 2, 0.0);
    println!("ln_f64: {} arguments, worst {worst:.3} u", 4 * n + START_PINNED.len());
    assert_eq!(bad, 0, "ln_f64 beyond {LN_F64} u");
    // control: off by 2^-48 = 32 u relative, it misses wherever ln h is not 0
    assert!(start_bound(64, 2, 2f64.powi(-48)).0 > 48);
}

/// special values, exact cases and the range ends, as f64's exp and ln give them
#[test]
fn special_values() {
    fn check<T: Ty>() {
        let f = |v: f64| T::from_parts(&[v, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0][..T::N]);
        let hi = |x: T| x.parts()[0];
        assert!(hi(f(f64::NAN).exp()).is_nan());
        assert_eq!(hi(f(f64::INFINITY).exp()), f64::INFINITY);
        assert_eq!(hi(f(f64::NEG_INFINITY).exp()).to_bits(), 0);
        assert_eq!(f(0.0).exp().parts(), f(1.0).parts());
        assert_eq!(f(-0.0).exp().parts(), f(1.0).parts());
        assert_eq!(hi(f(709.79).exp()), f64::INFINITY);
        assert_eq!(f(709.8).exp().parts(), f(f64::INFINITY).parts());
        assert!(hi(f(709.78).exp()).is_finite());
        assert_eq!(hi(f(-745.21).exp()).to_bits(), 0);
        assert!(hi(f(-745.0).exp()) > 0.0, "e^-745 rounds to the least subnormal");
        assert!(hi(f(f64::NAN).ln()).is_nan());
        assert!(hi(f(-1.0).ln()).is_nan());
        assert!(hi(f(-f64::MIN_POSITIVE).ln()).is_nan());
        assert_eq!(hi(f(0.0).ln()), f64::NEG_INFINITY);
        assert_eq!(hi(f(-0.0).ln()), f64::NEG_INFINITY);
        assert_eq!(hi(f(f64::INFINITY).ln()), f64::INFINITY);
        assert!(f(1.0).ln().parts().iter().all(|v| *v == 0.0), "ln 1 = 0 exactly");
        // e^(ln 2 + ln 3) = 6, ln(e^x) = x, to the precision
        let six = (f(2.0).ln().parts(), f(3.0).ln().parts());
        let s = exact(T::from_parts(&six.0)).add(&exact(T::from_parts(&six.1)));
        let back = exact(greedy::<T>(&s).exp());
        assert!(back.sub(&Dy::from_f64(6.0)).ratio(&Dy::from_f64(6.0)) < 100.0 * un::<T>());
    }
    check::<DD>();
    check::<QD>();
    check::<OD>();
}

/// The controls (feature `mutate`): each breakage of `explog::mutate` makes the checks of every type fail.
#[cfg(feature = "mutate")]
#[test]
fn controls_fire() {
    use multidouble::explog::mutate;
    let mut silent = Vec::new();
    for id in 1..=mutate::N {
        mutate::set(id);
        let rows = all_rows(64, 3);
        mutate::set(0);
        // exp's mutations must fail an exp row, ln's an ln row, for each type
        let f = if id == mutate::RED_TAIL || id == mutate::TAYLOR_HALF {
            "exp"
        } else {
            "ln"
        };
        for t in ["dd", "qd", "od"] {
            let failed: Vec<&str> = rows
                .iter()
                .filter(|r| r.what.starts_with(&format!("{t} {f} ")) && r.fails > 0)
                .map(|r| r.what.as_str())
                .collect();
            println!(
                "control {id} {:<40} {t}: {} rows fail{}",
                mutate::NAMES[id as usize],
                failed.len(),
                if report() {
                    format!(" {failed:?}")
                } else {
                    String::new()
                }
            );
            let failed = failed.len();
            if failed == 0 {
                silent.push((id, t));
            }
        }
    }
    assert!(silent.is_empty(), "controls that did not fire: {silent:?}");
}

/// time per call of each function, on arguments spread over the range, and per value of the lane functions on the
/// compile-time backend, which take 8 values a call
#[test]
#[ignore]
fn timing() {
    fn time<T: Ty>(xs: &[T], f: fn(T) -> T) -> f64 {
        let reps = 3;
        let mut best = f64::INFINITY;
        for _ in 0..reps {
            let t = std::time::Instant::now();
            let mut acc = 0.0;
            for &x in xs {
                acc += f(std::hint::black_box(x)).parts()[0];
            }
            std::hint::black_box(acc);
            best = best.min(t.elapsed().as_secs_f64() / xs.len() as f64);
        }
        best * 1e9
    }
    fn time_lanes<T: Ty, V: Copy>(xs: &[T], load: fn(&[T; 8]) -> V, f: fn(V) -> V, hi: fn(V) -> f64) -> f64 {
        let vs: Vec<V> = xs.as_chunks::<8>().0.iter().map(load).collect();
        let mut best = f64::INFINITY;
        for _ in 0..3 {
            let t = std::time::Instant::now();
            let mut acc = 0.0;
            for &v in &vs {
                acc += hi(f(std::hint::black_box(v)));
            }
            std::hint::black_box(acc);
            best = best.min(t.elapsed().as_secs_f64() / (8 * vs.len()) as f64);
        }
        best * 1e9
    }
    fn args<T: Ty>(lo: f64, hi: f64) -> Vec<T> {
        let mut r = Rng(5);
        (0..2000)
            .map(|_| {
                greedy(
                    &rand_dy(&mut r, 0, false)
                        .ldexp(-1)
                        .add(&Dy::from_f64(lo + (hi - lo) * r.unit())),
                )
            })
            .collect()
    }
    /// the scalar functions of T and the lane functions of V, whose lane 0's leading part is hi
    fn row<T: Ty, V: Copy>(load: fn(&[T; 8]) -> V, exp: fn(V) -> V, ln: fn(V) -> V, hi: fn(V) -> f64) {
        let e = args::<T>(-700.0, 700.0);
        let l = args::<T>(1e-3, 1e3);
        println!(
            "{}: exp {:.0} ns, ln {:.0} ns; lanes on {}: exp {:.0} ns, ln {:.0} ns per value",
            T::NAME,
            time(&e, T::exp),
            time(&l, T::ln),
            native::V8::ISA,
            time_lanes(&e, load, exp, hi),
            time_lanes(&l, load, ln, hi)
        );
    }
    row::<DD, native::DV>(
        |x| {
            let (hi, lo) = multidouble::dd::split(x);
            native::DV::load(&hi, &lo)
        },
        native::DV::exp,
        native::DV::ln,
        |v| v.hi.to_array()[0],
    );
    row::<QD, native::QV>(native::QV::from_lanes, native::QV::exp, native::QV::ln, |v| {
        v.c[0].to_array()[0]
    });
    row::<OD, native::OV>(native::OV::from_lanes, native::OV::exp, native::OV::ln, |v| {
        v.c[0].to_array()[0]
    });
}