multidouble 6.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
//! Forward-mode dual lanes: one generic function evaluated with value lanes, with a double-double tangent (`Du`), with
//! N f64 tangents (`Dtn<N>`) and in plain f64 (`Fu`), against derivatives written out by hand in scalar double-double.
//! A second function goes through every method of `Lane` on all eight lane types, and `mul_pwr2` is checked bit for bit.
//! `Dc` (a complex tangent) is `Du` on the real and on the imaginary part separately, bit for bit.
//! `Qu` (quad-double value and tangent) is checked against derivatives by hand in scalar quad-double.
#![allow(clippy::needless_range_loop)]
mod common;
use common::Rng;
use multidouble::dd::DD;
use multidouble::dual::{take_counts, Counted, Lane, COUNTED_OPS};
use multidouble::dv::DV;
use multidouble::native::{Dc, Dtn, Du, Fu, Fun, Jet, Qu, F8, QV};
use multidouble::qd::QD;
use multidouble::simd::{Native, Simd};
use multidouble::v8::V8;

/// f(x, y) = (x^3 + 2 x y) / (x + 3), written once over the `Lane` trait
fn f<T: Lane>(x: T, y: T) -> T {
    let two = T::cst(DV::splat_f(2.0));
    let three = T::cst(DV::splat_f(3.0));
    (x * x * x + two * x * y) / (x + three)
}

/// f and its partial derivatives in scalar double-double, by hand
fn f_dd(x: DD, y: DD) -> (DD, DD, DD) {
    let two = DD::from_f64(2.0);
    let three = DD::from_f64(3.0);
    let num = x * x * x + two * x * y;
    let den = x + three;
    let fx = ((DD::from_f64(3.0) * x * x + two * y) * den - num) / (den * den);
    let fy = (two * x) / den;
    (num / den, fx, fy)
}

fn inputs(r: &mut Rng) -> ([DD; 8], [DD; 8]) {
    // x away from -3, the pole
    let x = std::array::from_fn(|_| DD::new(1.0 + 4.0 * r.unit(), r.sym() * 1e-17));
    let y = std::array::from_fn(|_| DD::new(r.sym() * 5.0, r.sym() * 1e-17));
    (x, y)
}

fn dv<S: Simd>(v: &[DD; 8]) -> DV<S> {
    let hi: Vec<f64> = v.iter().map(|d| d.hi).collect();
    let lo: Vec<f64> = v.iter().map(|d| d.lo).collect();
    DV::load(&hi, &lo)
}

fn rel(a: DD, b: DD) -> f64 {
    ((a - b).to_f64() / b.to_f64()).abs()
}

#[test]
fn values_match_the_scalar_formula() {
    let mut r = Rng(21);
    for _ in 0..500 {
        let (x, y) = inputs(&mut r);
        let v = f(dv::<Native>(&x), dv(&y));
        for j in 0..8 {
            assert!(rel(v.lane(j), f_dd(x[j], y[j]).0) < 1e-30);
        }
    }
}

#[test]
fn du_gives_exact_partial_derivatives() {
    let mut r = Rng(22);
    let mut worst: f64 = 0.0;
    for _ in 0..500 {
        let (x, y) = inputs(&mut r);
        let one = DV::splat_f(1.0);
        // d/dx: tangent 1 on x; d/dy: tangent 1 on y
        let gx = f(Du::from_vt(dv(&x), one), Du::from_vt(dv(&y), DV::zero()));
        let gy = f(Du::from_vt(dv(&x), DV::zero()), Du::from_vt(dv(&y), one));
        for j in 0..8 {
            let (val, fx, fy) = f_dd(x[j], y[j]);
            for (got, want) in [(gx.val().lane(j), val), (gx.tan().lane(j), fx), (gy.tan().lane(j), fy)] {
                let e = rel(got, want);
                worst = worst.max(e);
                assert!(e < 1e-29, "lane {j}: {got:?} vs {want:?}");
            }
        }
    }
    println!("Du: worst relative error {worst:.1e}");
}

#[test]
fn dtn_gives_both_partials_in_one_pass() {
    let mut r = Rng(23);
    for _ in 0..500 {
        let (x, y) = inputs(&mut r);
        let (one, zero) = (V8::splat(1.0), V8::zero());
        let xs = Dtn::<2> {
            v: dv(&x),
            d: [one, zero],
        };
        let ys = Dtn::<2> {
            v: dv(&y),
            d: [zero, one],
        };
        let g = f(xs, ys);
        let (dx, dy) = (g.d[0].to_array(), g.d[1].to_array());
        for j in 0..8 {
            let (val, fx, fy) = f_dd(x[j], y[j]);
            // value in double-double, tangents in f64
            assert!(rel(g.v.lane(j), val) < 1e-30);
            assert!(
                ((dx[j] - fx.to_f64()) / fx.to_f64()).abs() < 1e-13,
                "lane {j}: {} vs {:?}",
                dx[j],
                fx
            );
            assert!(
                ((dy[j] - fy.to_f64()) / fy.to_f64()).abs() < 1e-13,
                "lane {j}: {} vs {:?}",
                dy[j],
                fy
            );
        }
    }
}

#[test]
fn fu_is_plain_f64_forward_mode() {
    let mut r = Rng(24);
    for _ in 0..500 {
        let (x, y) = inputs(&mut r);
        let hx = V8::from_array(std::array::from_fn(|j| x[j].hi));
        let hy = V8::from_array(std::array::from_fn(|j| y[j].hi));
        let g = f(
            Fu {
                v: hx,
                d: V8::splat(1.0),
            },
            Fu { v: hy, d: V8::zero() },
        );
        let (v, d) = (g.v.to_array(), g.d.to_array());
        for j in 0..8 {
            let (xh, yh) = (x[j].hi, y[j].hi);
            let (val, fx, _) = f_dd(DD::from_f64(xh), DD::from_f64(yh));
            // f64 errors relative to the size of the terms (the numerator can cancel)
            let scale = (xh.powi(3).abs() + (2.0 * xh * yh).abs()) / (xh + 3.0);
            let dscale = (3.0 * xh * xh + (2.0 * yh).abs()) / (xh + 3.0) + scale / (xh + 3.0);
            assert!(
                (v[j] - val.to_f64()).abs() < 1e-14 * scale,
                "lane {j}: {} vs {:?}",
                v[j],
                val
            );
            assert!(
                (d[j] - fx.to_f64()).abs() < 1e-14 * dscale,
                "lane {j}: {} vs {:?}",
                d[j],
                fx
            );
        }
    }
}

/// Control: a wrong hand derivative (the quotient rule's sign flipped) is caught by the same comparison.
#[test]
fn control_wrong_derivative_is_caught() {
    let mut r = Rng(25);
    let (x, y) = inputs(&mut r);
    let one = DV::splat_f(1.0);
    let gx = f(Du::from_vt(dv(&x), one), Du::from_vt(dv(&y), DV::zero()));
    let caught = (0..8).any(|j| {
        let (val, fx, _) = f_dd(x[j], y[j]);
        let wrong = fx + DD::from_f64(2.0) * val / (x[j] + DD::from_f64(3.0));
        rel(gx.tan().lane(j), wrong) > 1e-29
    });
    assert!(caught);
}

/// g(x, y) = ((c x - s y)(x/2) + x y + c x - (y + c)) / (x_0 + 3), written once over `Lane` through every one of its
/// methods: the product sum by the accumulator, and x_0, lane 0's x, by `bcast`
fn g<T: Lane>(x: T, y: T, c: DV<T::S>, s: DD) -> T {
    let a = x.mulc(c) - y.muls(s);
    let p = T::fin(T::acc1(T::accc(T::acc(T::acc0(), x, y), x, c), -y.addc(c)));
    T::zero() + (a * x.mulf(0.5) + p) / (x.bcast(0) + T::splat(DD::from_f64(3.0)))
}

type Reference = fn(DD, DD, DD, DD, DD) -> (DD, DD, DD);

/// g and its partials in scalar double-double, by hand, at one lane. The tangent seeds every lane, so x_0 moves with x.
fn g_dd(x: DD, y: DD, c: DD, s: DD, x0: DD) -> (DD, DD, DD) {
    let half = DD::from_f64(0.5);
    let den = x0 + DD::from_f64(3.0);
    let q = (c * x - s * y) * (x * half) + x * y + c * x - (y + c);
    let qx = c * x - s * y * half + y + c;
    let qy = x - s * x * half - DD::ONE;
    (q / den, (qx * den - q) / (den * den), qy / den)
}

/// worst relative errors of g on lane type T against `reference`: value, d/dx, d/dy. The inputs keep every term of
/// g, its partials and their numerators positive, so the errors are relative to well-conditioned values.
fn errors<T: Lane>(reference: Reference) -> [f64; 3] {
    let mut r = Rng(26);
    let s = DD::ONE / DD::from_f64(3.0);
    let (one, zero) = (DV::splat_f(1.0), DV::zero());
    let mut worst = [0.0f64; 3];
    for _ in 0..200 {
        let x: [DD; 8] = std::array::from_fn(|_| DD::new(2.0 + r.unit(), r.sym() * 1e-16));
        let y: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
        let c: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
        let gx = g(T::from_vt(dv(&x), one), T::from_vt(dv(&y), zero), dv(&c), s);
        // d/dy through the last f64 tangent where the lane has them, through `from_vt` otherwise
        let k = T::NT.saturating_sub(1);
        let mut ys = T::from_vt(dv(&y), if T::TAN_F64 { zero } else { one });
        if T::TAN_F64 {
            ys.set_tan64(k, V8::splat(1.0));
        }
        let gy = g(T::from_vt(dv(&x), zero), ys, dv(&c), s);
        let gy_tan = if T::TAN_F64 {
            DV {
                hi: gy.tan64(k),
                lo: V8::zero(),
            }
        } else {
            gy.tan()
        };
        for j in 0..8 {
            // f64-valued lanes see the hi parts of the inputs and constants
            let h = |d: DD| if T::VAL_F64 { DD::from_f64(d.hi) } else { d };
            let (val, fx, fy) = reference(h(x[j]), h(y[j]), h(c[j]), h(s), h(x[0]));
            worst[0] = worst[0].max(rel(gx.val().lane(j), val));
            if T::TANGENT {
                worst[1] = worst[1].max(rel(gx.tan().lane(j), fx));
                worst[2] = worst[2].max(rel(gy_tan.lane(j), fy));
            }
        }
    }
    worst
}

/// double-double accuracy for double-double parts, f64 accuracy for f64 parts
fn within<T: Lane>(e: [f64; 3]) -> bool {
    let tv = if T::VAL_F64 { 1e-13 } else { 1e-29 };
    let tt = if T::TAN_F64 { 1e-13 } else { 1e-29 };
    e[0] < tv && e[1] < tt && e[2] < tt
}

#[test]
fn every_lane_method_on_every_lane_type() {
    fn run<T: Lane>(name: &str) {
        let e = errors::<T>(g_dd);
        println!(
            "{name}: worst relative errors {:.1e} value, {:.1e} d/dx, {:.1e} d/dy",
            e[0], e[1], e[2]
        );
        assert!(within::<T>(e), "{name}: {e:?}");
    }
    run::<DV<Native>>("DV");
    run::<F8>("F8");
    run::<Du>("Du");
    run::<Fu>("Fu");
    run::<Dtn<2>>("Dtn<2>");
    run::<Fun<2>>("Fun<2>");
    run::<Qu>("Qu");
    run::<Dc>("Dc");
    run::<Jet<2>>("Jet<2>");
    run::<Jet<5>>("Jet<5>");
}

/// Controls: hand references that get `bcast` wrong are caught. A value with each lane's own x in place of lane 0's,
/// on the value lanes, and a d/dx that misses the tangent `bcast` carries from lane 0, on every tangent lane type.
#[test]
fn controls_on_bcast_are_caught() {
    fn own_x(x: DD, y: DD, c: DD, s: DD, _x0: DD) -> (DD, DD, DD) {
        g_dd(x, y, c, s, x)
    }
    assert!(!within::<DV<Native>>(errors::<DV<Native>>(own_x)));
    assert!(!within::<F8>(errors::<F8>(own_x)));
    fn no_bcast_tangent(x: DD, y: DD, c: DD, s: DD, x0: DD) -> (DD, DD, DD) {
        let (val, _, fy) = g_dd(x, y, c, s, x0);
        let qx = c * x - s * y * DD::from_f64(0.5) + y + c;
        (val, qx / (x0 + DD::from_f64(3.0)), fy)
    }
    assert!(!within::<Du>(errors::<Du>(no_bcast_tangent)));
    assert!(!within::<Fu>(errors::<Fu>(no_bcast_tangent)));
    assert!(!within::<Dtn<2>>(errors::<Dtn<2>>(no_bcast_tangent)));
    assert!(!within::<Fun<2>>(errors::<Fun<2>>(no_bcast_tangent)));
    assert!(!within::<Qu>(errors::<Qu>(no_bcast_tangent)));
    assert!(!within::<Jet<2>>(errors::<Jet<2>>(no_bcast_tangent)));
}

/// every part of a lane, value and tangents, as bits
fn parts<T: Lane>(x: T) -> Vec<u64> {
    let mut out = Vec::new();
    let mut push = |v: V8<T::S>| {
        let mut a = [0.0; 8];
        v.store(&mut a);
        out.extend(a.iter().map(|f| f.to_bits()));
    };
    let v = x.val();
    push(v.hi);
    push(v.lo);
    if T::TAN_F64 {
        for k in 0..T::NT {
            push(x.tan64(k));
        }
    } else if T::TANGENT {
        let t = x.tan();
        push(t.hi);
        push(t.lo);
    }
    out
}

/// a lane with random normalized parts of either sign: value, and every tangent
fn random_lane<T: Lane>(r: &mut Rng) -> T {
    nudged_lane(r, false)
}

/// `random_lane`, with the last bit of every lane's leading value part flipped when nudge
fn nudged_lane<T: Lane>(r: &mut Rng, nudge: bool) -> T {
    let mut v: [DD; 8] = std::array::from_fn(|_| r.dd(8));
    if nudge {
        for d in &mut v {
            d.hi = f64::from_bits(d.hi.to_bits() ^ 1);
        }
    }
    let t: [DD; 8] = std::array::from_fn(|_| r.dd(8));
    let mut x = T::from_vt(dv(&v), dv(&t));
    if T::TAN_F64 {
        for k in 0..T::NT {
            let a: [f64; 8] = std::array::from_fn(|_| r.dd(8).hi);
            x.set_tan64(k, V8::load(&a));
        }
    }
    x
}

/// `scale` (the method under test, or a wrong one in the controls) against `mulf`, bit for bit, at random powers of
/// two 2^-200..2^200 (every part stays normal), and the round trip: scaling by 2^k and then by 2^-k gives every part
/// back bit for bit
fn scales_exactly<T: Lane>(scale: impl Fn(T, f64) -> T) -> bool {
    let mut r = Rng(53);
    for _ in 0..2000 {
        let x = random_lane::<T>(&mut r);
        let s = 2f64.powi((r.next_u64() % 401) as i32 - 200);
        let y = scale(x, s);
        if parts(y) != parts(x.mulf(s)) || parts(scale(y, 1.0 / s)) != parts(x) {
            return false;
        }
    }
    true
}

#[test]
fn mul_pwr2_is_exact_on_every_lane_type() {
    assert!(scales_exactly::<DV<Native>>(|x, s| x.mul_pwr2(s)), "DV");
    assert!(scales_exactly::<F8>(|x, s| x.mul_pwr2(s)), "F8");
    assert!(scales_exactly::<Du>(|x, s| x.mul_pwr2(s)), "Du");
    assert!(scales_exactly::<Fu>(|x, s| x.mul_pwr2(s)), "Fu");
    assert!(scales_exactly::<Dtn<2>>(|x, s| x.mul_pwr2(s)), "Dtn<2>");
    assert!(scales_exactly::<Fun<2>>(|x, s| x.mul_pwr2(s)), "Fun<2>");
    assert!(scales_exactly::<Qu>(|x, s| x.mul_pwr2(s)), "Qu");
    assert!(scales_exactly::<Dc>(|x, s| x.mul_pwr2(s)), "Dc");
    assert!(scales_exactly::<Jet<3>>(|x, s| x.mul_pwr2(s)), "Jet<3>");
    // the scalar double-double, through lane 0 of DV
    let mut r = Rng(54);
    for _ in 0..2000 {
        let x = r.dd(30);
        let s = 2f64.powi((r.next_u64() % 401) as i32 - 200);
        let y = x.mul_pwr2(s);
        assert_eq!((y.hi.to_bits(), y.lo.to_bits()), {
            let m = x.mul_f(s);
            (m.hi.to_bits(), m.lo.to_bits())
        });
        assert_eq!(y.mul_pwr2(1.0 / s), x);
    }
}

/// Controls: wrong scalings are caught. One power of two too many, on every lane type; the low part left unscaled, on
/// the double-double value lanes; the (first) tangent left unscaled, on every tangent lane type.
#[test]
fn controls_on_mul_pwr2_are_caught() {
    fn wrong_power<T: Lane>(x: T, s: f64) -> T {
        x.mul_pwr2(2.0 * s)
    }
    assert!(!scales_exactly::<DV<Native>>(wrong_power));
    assert!(!scales_exactly::<F8>(wrong_power));
    assert!(!scales_exactly::<Du>(wrong_power));
    assert!(!scales_exactly::<Fu>(wrong_power));
    assert!(!scales_exactly::<Dtn<2>>(wrong_power));
    assert!(!scales_exactly::<Fun<2>>(wrong_power));
    assert!(!scales_exactly::<Qu>(wrong_power));
    assert!(!scales_exactly::<Dc>(wrong_power));
    assert!(!scales_exactly::<Jet<3>>(wrong_power));
    fn lo_unscaled<T: Lane>(x: T, s: f64) -> T {
        let y = x.mul_pwr2(s);
        let v = DV {
            hi: y.val().hi,
            lo: x.val().lo,
        };
        let mut z = T::from_vt(v, y.tan());
        if T::TAN_F64 {
            for k in 0..T::NT {
                z.set_tan64(k, y.tan64(k));
            }
        }
        z
    }
    assert!(!scales_exactly::<DV<Native>>(lo_unscaled));
    assert!(!scales_exactly::<Du>(lo_unscaled));
    assert!(!scales_exactly::<Dtn<2>>(lo_unscaled));
    assert!(!scales_exactly::<Qu>(lo_unscaled));
    assert!(!scales_exactly::<Dc>(lo_unscaled));
    assert!(!scales_exactly::<Jet<3>>(lo_unscaled));
    fn tangent_unscaled<T: Lane>(x: T, s: f64) -> T {
        let mut y = x.mul_pwr2(s);
        if T::TAN_F64 {
            y.set_tan64(0, x.tan64(0));
        } else {
            y = T::from_vt(y.val(), x.tan());
        }
        y
    }
    assert!(!scales_exactly::<Du>(tangent_unscaled));
    assert!(!scales_exactly::<Fu>(tangent_unscaled));
    assert!(!scales_exactly::<Dtn<2>>(tangent_unscaled));
    assert!(!scales_exactly::<Fun<2>>(tangent_unscaled));
    assert!(!scales_exactly::<Qu>(tangent_unscaled));
    assert!(!scales_exactly::<Dc>(tangent_unscaled));
    assert!(!scales_exactly::<Jet<3>>(tangent_unscaled));
    // the imaginary part left unscaled is caught through the parts (`parts` reads the real tangent only)
    let mut r = Rng(57);
    for _ in 0..200 {
        let x: Dc = random_lane(&mut r);
        let x = Dc {
            i: dv(&std::array::from_fn(|_| r.dd(8))),
            ..x
        };
        let s = 2f64.powi((r.next_u64() % 401) as i32 - 200);
        let y = x.mul_pwr2(s);
        let wrong = Dc { i: x.i, ..y };
        assert!(bits(y.i) == bits(x.i.mulf(s)) && (s == 1.0 || bits(wrong.i) != bits(y.i)));
    }
}

fn bits(v: DV<Native>) -> [u64; 16] {
    let (h, l) = (v.hi.to_array(), v.lo.to_array());
    std::array::from_fn(|k| if k < 8 { h[k].to_bits() } else { l[k - 8].to_bits() })
}

/// `Dc` on g: with a real tangent its value and real part are `Du`'s bit for bit and its imaginary part is zero; with
/// the d/dx seed on the imaginary part and the d/dy seed on the real part, the two parts are the two `Du` tangents
/// bit for bit. Controls: the parts swapped, and a seed of 2 on the imaginary part, must differ.
#[test]
fn dc_is_du_on_the_real_and_the_imaginary_part() {
    let mut r = Rng(58);
    let s = DD::ONE / DD::from_f64(3.0);
    let (one, zero) = (DV::splat_f(1.0), DV::zero());
    for _ in 0..200 {
        let x: [DD; 8] = std::array::from_fn(|_| DD::new(2.0 + r.unit(), r.sym() * 1e-16));
        let y: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
        let c: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
        let gx = g(Du::from_vt(dv(&x), one), Du::from_vt(dv(&y), zero), dv(&c), s);
        let gy = g(Du::from_vt(dv(&x), zero), Du::from_vt(dv(&y), one), dv(&c), s);
        let real = g(Dc::from_vt(dv(&x), one), Dc::from_vt(dv(&y), zero), dv(&c), s);
        assert!(bits(real.v) == bits(gx.v) && bits(real.r) == bits(gx.d) && bits(real.i) == bits(zero));
        let both = g(
            Dc {
                v: dv(&x),
                r: zero,
                i: one,
            },
            Dc {
                v: dv(&y),
                r: one,
                i: zero,
            },
            dv(&c),
            s,
        );
        assert!(bits(both.v) == bits(gx.v) && bits(both.i) == bits(gx.d) && bits(both.r) == bits(gy.d));
        assert!(bits(both.r) != bits(gx.d), "control: the parts swapped agree");
        let two = g(
            Dc {
                v: dv(&x),
                r: zero,
                i: DV::splat_f(2.0),
            },
            Dc::from_vt(dv(&y), zero),
            dv(&c),
            s,
        );
        assert!(
            bits(two.i) != bits(gx.d),
            "control: a seed of 2 agrees with a seed of 1"
        );
    }
}

/// `VAL_QD` on every lane type: the quad-double lanes only, forwarded by `Counted`
#[test]
fn val_qd_marks_the_quad_double_lanes() {
    fn val_qd<T: Lane>() -> bool {
        T::VAL_QD
    }
    let quad = [
        val_qd::<QV>(),
        val_qd::<Qu>(),
        val_qd::<Counted<Qu>>(),
        val_qd::<Counted<QV>>(),
    ];
    let other = [
        val_qd::<DV<Native>>(),
        val_qd::<Du>(),
        val_qd::<Dc>(),
        val_qd::<F8>(),
        val_qd::<Fu>(),
        val_qd::<Dtn<2>>(),
        val_qd::<Fun<2>>(),
        val_qd::<Counted<Du>>(),
        val_qd::<Jet<4>>(),
    ];
    assert!(quad.iter().all(|&q| q), "{quad:?}");
    assert!(other.iter().all(|&q| !q), "{other:?}");
}

/// the scales `mul_pwr2` accepts: normal powers of two of either sign, nothing else
#[test]
fn is_pwr2_accepts_normal_powers_of_two_only() {
    use multidouble::dd::is_pwr2;
    for k in -1022..=1023 {
        assert!(is_pwr2(2f64.powi(k)) && is_pwr2(-(2f64.powi(k))), "2^{k}");
    }
    for b in [
        0.0,
        -0.0,
        3.0,
        0.75,
        1.0 + f64::EPSILON,
        f64::MIN_POSITIVE / 2.0,
        f64::INFINITY,
        f64::NAN,
    ] {
        assert!(!is_pwr2(b), "{b:e}");
    }
}

/// in debug builds, a scale that is not a power of two is refused (release builds do not check)
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "not a power of two")]
fn mul_pwr2_refuses_other_scales_in_debug() {
    let _ = Du::from_vt(DV::splat_f(1.0), DV::splat_f(1.0)).mul_pwr2(3.0);
}

/// f plus a sum of products through the accumulator, written once over `Lane`
fn f_acc<T: Lane>(x: T, y: T) -> T {
    let c = DV::splat_f(0.5);
    let a = T::acc1(T::accc(T::acc(T::acc(T::acc0(), x, y), y, y), x, c), x);
    -(T::fin(a) - f(x, y)).mulf(3.0).addc(c).mul_pwr2(0.25).mulc(c)
}

/// `Counted` gives its lane type's results bit for bit and counts every operation by kind: f has 2 adds, 4 multiplies
/// and a divide; g adds 4 accumulations and a finish, a subtraction, a negation and one each of x f64 (twice:
/// `mulf`, `mul_pwr2`), + const and x const. Control: the counts of g are not those of f (an operation is seen).
#[test]
fn counted_lanes_count_and_change_nothing() {
    fn run<T: Lane>(x: T, y: T) -> (T, [u64; 11]) {
        take_counts();
        let v = f_acc(x, y);
        (v, take_counts())
    }
    let mut r = Rng(31);
    let (x, y) = inputs(&mut r);
    let (xd, yd) = (dv::<Native>(&x), dv::<Native>(&y));
    let want = [3, 4, 1, 1, 1, 2, 1, 2, 1, 1, 1];
    let (plain, n0) = run(xd, yd);
    let (cv, n1) = run(Counted(xd), Counted(yd));
    assert_eq!(n0, [0; 11], "plain lanes are not counted");
    assert_eq!(n1, want, "counts of g on Counted<DV> ({COUNTED_OPS:?})");
    for j in 0..8 {
        let (a, b) = (plain.lane(j), cv.0.lane(j));
        assert!(
            a.hi.to_bits() == b.hi.to_bits() && a.lo.to_bits() == b.lo.to_bits(),
            "lane {j}"
        );
    }
    let (xu, yu) = (Du::from_vt(xd, DV::splat_f(1.0)), Du::from_vt(yd, DV::zero()));
    let (pu, _) = run(xu, yu);
    let (cu, nu) = run(Counted(xu), Counted(yu));
    assert_eq!(nu, want, "counts of g on Counted<Du>");
    for (a, b) in [(pu.val(), cu.val()), (pu.tan(), cu.tan())] {
        for j in 0..8 {
            assert!(
                a.lane(j).hi.to_bits() == b.lane(j).hi.to_bits() && a.lane(j).lo.to_bits() == b.lane(j).lo.to_bits()
            );
        }
    }
    take_counts();
    let _ = f(Counted(xd), Counted(yd));
    let nf = take_counts();
    assert_eq!(nf, [2, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0], "counts of f");
    assert_ne!(nf, want, "control: f and g counted alike");
}

// ---------------------------------------------------------------------------------------------------------------
// Qu: quad-double value and tangent
// ---------------------------------------------------------------------------------------------------------------

/// f and its partial derivatives in scalar quad-double, by hand
fn f_qd(x: QD, y: QD) -> (QD, QD, QD) {
    let two = QD::from_f64(2.0);
    let three = QD::from_f64(3.0);
    let num = x * x * x + two * x * y;
    let den = x + three;
    let fx = ((three * x * x + two * y) * den - num) / (den * den);
    let fy = (two * x) / den;
    (num / den, fx, fy)
}

/// |a - b| / max(|b|, 1)
fn err_qd(a: QD, b: QD) -> f64 {
    (a - b).to_f64().abs() / b.to_f64().abs().max(1.0)
}

/// worst error of f's value and partials on Qu lanes with quad-double inputs, against f_qd; with dd_tangent the
/// tangents come from Du lanes instead (inputs rounded to double-double, and the reference at those inputs)
fn qu_errors(dd_tangent: bool) -> f64 {
    let mut r = Rng(27);
    let mut worst = 0.0f64;
    for _ in 0..300 {
        let mut x: [QD; 8] =
            std::array::from_fn(|_| QD::new(1.0 + 4.0 * r.unit(), r.sym() * 1e-17, r.sym() * 1e-34, r.sym() * 1e-51));
        let mut y: [QD; 8] =
            std::array::from_fn(|_| QD::new(r.sym() * 5.0, r.sym() * 1e-16, r.sym() * 1e-33, r.sym() * 1e-50));
        let got: [Vec<QD>; 3] = if dd_tangent {
            x = x.map(|v| QD::from_dd(v.to_dd()));
            y = y.map(|v| QD::from_dd(v.to_dd()));
            let d = |v: &[QD; 8]| dv::<Native>(&v.map(|q| q.to_dd()));
            let one = DV::splat_f(1.0);
            let gx = f(Du::from_vt(d(&x), one), Du::from_vt(d(&y), DV::zero()));
            let gy = f(Du::from_vt(d(&x), DV::zero()), Du::from_vt(d(&y), one));
            let l = |v: DV<Native>| (0..8).map(|j| QD::from_dd(v.lane(j))).collect::<Vec<_>>();
            [l(gx.v), l(gx.d), l(gy.d)]
        } else {
            let (one, zero) = (QV::splat(QD::ONE), QV::zero());
            let gx = f(
                Qu {
                    v: QV::from_lanes(&x),
                    d: one,
                },
                Qu {
                    v: QV::from_lanes(&y),
                    d: zero,
                },
            );
            let gy = f(
                Qu {
                    v: QV::from_lanes(&x),
                    d: zero,
                },
                Qu {
                    v: QV::from_lanes(&y),
                    d: one,
                },
            );
            [gx.v.lanes().to_vec(), gx.d.lanes().to_vec(), gy.d.lanes().to_vec()]
        };
        for j in 0..8 {
            let (val, fx, fy) = f_qd(x[j], y[j]);
            for (g, want) in [(got[0][j], val), (got[1][j], fx), (got[2][j], fy)] {
                worst = worst.max(err_qd(g, want));
            }
        }
    }
    worst
}

#[test]
fn qu_gives_quad_double_partial_derivatives() {
    let e = qu_errors(false);
    println!("Qu: worst error {e:.1e}");
    assert!(e < 1e-58, "{e:e}");
}

/// Control: the same check on Du's double-double tangents fails, so it measures quad-double accuracy.
#[test]
fn control_du_tangents_fail_the_quad_double_check() {
    let e = qu_errors(true);
    println!("Du against the quad-double reference: worst error {e:.1e}");
    assert!(e > 1e-40, "{e:e}");
}

/// Qu's value is bitwise QV's: g through every Lane method on both, with the same inputs and constants
#[test]
fn qu_value_is_qv_bitwise() {
    let mut r = Rng(28);
    let s = DD::ONE / DD::from_f64(3.0);
    for _ in 0..200 {
        let x: [DD; 8] = std::array::from_fn(|_| DD::new(2.0 + r.unit(), r.sym() * 1e-16));
        let y: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
        let c: [DD; 8] = std::array::from_fn(|_| DD::new(1.0 + r.unit(), r.sym() * 1e-16));
        let gq = g(QV::from_dv(dv(&x)), QV::from_dv(dv(&y)), dv(&c), s);
        let gu = g(
            Qu::from_vt(dv(&x), DV::splat_f(1.0)),
            Qu::from_vt(dv(&y), DV::zero()),
            dv(&c),
            s,
        );
        for q in 0..4 {
            let (a, b) = (gq.c[q].to_array(), gu.v.c[q].to_array());
            assert!(
                a.iter().zip(&b).all(|(u, v)| u.to_bits() == v.to_bits()),
                "part {q}: {a:?} vs {b:?}"
            );
        }
    }
}

// ---------------------------------------------------------------------------------------------------------------
// pinned outputs
// ---------------------------------------------------------------------------------------------------------------

fn v8_bits(v: V8<Native>) -> Vec<u64> {
    let mut a = [0.0; 8];
    v.store(&mut a);
    a.map(f64::to_bits).to_vec()
}

fn dv_bits(v: DV<Native>) -> Vec<u64> {
    [v8_bits(v.hi), v8_bits(v.lo)].concat()
}

fn qv_bits(v: QV) -> Vec<u64> {
    v.c.iter().flat_map(|&c| v8_bits(c)).collect()
}

/// the bits of the pinned outputs of lane type T, by group, every part of each read by bits. nudge
/// flips the last bit of the first x's leading value part in every lane.
fn dual_outputs<T: Lane<S = Native>>(bits: fn(T) -> Vec<u64>, nudge: bool) -> Vec<Vec<u64>> {
    let mut r = Rng(39);
    let s = DD::ONE / DD::from_f64(7.0);
    let mut w = vec![Vec::new(); 3];
    for i in 0..20 {
        let x: T = nudged_lane(&mut r, nudge && i == 0);
        let y: T = random_lane(&mut r);
        let c = dv(&std::array::from_fn(|_| r.dd(2)));
        w[0].extend([bits(x * y), bits(x / y)].concat());
        w[1].extend(bits(T::fin(T::acc1(T::accc(T::acc(T::acc0(), x, y), y, c), x))));
        w[2].extend(bits(g(x, y, c, s)));
    }
    w
}

/// the groups of lane type T, named, that differ from their pins
fn dual_mismatches<T: Lane<S = Native>>(name: &str, bits: fn(T) -> Vec<u64>, want: [u64; 3]) -> Vec<String> {
    let names = ["product and quotient", "accumulator", "every Lane method"].map(|g| format!("{name} {g}"));
    let names: Vec<&str> = names.iter().map(|n| n.as_str()).collect();
    common::pin_mismatches(&names, &dual_outputs(bits, false), &dual_outputs(bits, true), &want)
}

/// The dual lanes' outputs pinned bit for bit on fixed inputs, against hashes recorded with 4.1.1:
/// the product and quotient, the accumulator, and every `Lane` method (`g`), on every part of `Du`,
/// `Dc`, `Qu`, `F8`, `Fu`, `Dtn<3>` and `Fun<3>`. Every backend gives these bits (the lanes are the
/// scalar formulas', tests/lanes.rs). Control: the first input's last bit flipped changes every
/// group's hash.
#[test]
fn dual_outputs_are_pinned() {
    let mut bad = Vec::new();
    bad.extend(dual_mismatches::<Du>(
        "Du",
        |x| [dv_bits(x.v), dv_bits(x.d)].concat(),
        [0x45f4_cdb7_5aab_2b9d, 0x95b1_ede5_43ef_f9a8, 0x4cf7_0a93_51e6_fe44],
    ));
    bad.extend(dual_mismatches::<Dc>(
        "Dc",
        |x| [dv_bits(x.v), dv_bits(x.r), dv_bits(x.i)].concat(),
        [0x0679_8a53_2b10_989d, 0xe25f_5c9f_100a_8228, 0x81ad_a1cb_48f2_dd44],
    ));
    bad.extend(dual_mismatches::<Qu>(
        "Qu",
        |x| [qv_bits(x.v), qv_bits(x.d)].concat(),
        [0x1c5f_166f_1ce5_153e, 0x81d2_45b1_a3d5_dd28, 0xe28f_da15_f617_9db8],
    ));
    bad.extend(dual_mismatches::<F8>(
        "F8",
        |x| v8_bits(x.0),
        [0x3ca4_595b_a224_e9dc, 0x261b_b538_7d87_4e60, 0x7528_5721_cae9_fae0],
    ));
    bad.extend(dual_mismatches::<Fu>(
        "Fu",
        |x| [v8_bits(x.v), v8_bits(x.d)].concat(),
        [0xad11_9f0e_d667_a92f, 0x6d85_92b9_6c62_0454, 0x09bb_5f64_1438_ffef],
    ));
    bad.extend(dual_mismatches::<Dtn<3>>(
        "Dtn<3>",
        |x| [dv_bits(x.v), x.d.iter().flat_map(|&d| v8_bits(d)).collect()].concat(),
        [0x3433_5025_b06d_fb92, 0x0de6_7438_5c5b_8d96, 0x31fa_4905_ffd7_fe56],
    ));
    bad.extend(dual_mismatches::<Fun<3>>(
        "Fun<3>",
        |x| [v8_bits(x.v), x.d.iter().flat_map(|&d| v8_bits(d)).collect()].concat(),
        [0xe100_30d7_742a_f188, 0x30bc_1854_d2a0_4e26, 0x097f_ce43_f176_0c58],
    ));
    assert!(bad.is_empty(), "{bad:#?}");
}