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
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
//! Tests of the octo-double arithmetic (`od`, `ov`) against exact dyadic arithmetic (`exact`), which shares no code
//! with the algorithms under test: the inputs are built from exact random values by greedy rounding to eight parts,
//! not by octo-double arithmetic, and every reference result is exact.
//!
//! ```text
//! cargo test --release --lib od_                                  accuracy, scalar/8-lane agreement, decimal I/O
//! cargo test --release --lib --features mutate od_controls        every deliberate breakage must fail a check
//! ```
//! OD_N sets the samples per operation and input category (default 2000), OD_SEED the seed (default 1),
//! OD_REPORT=1 prints the tables. The thresholds, in units of 2^-418 (`OD::EPS`), were fixed before the first run:
//! 1 for the adds (relative to |a| + |b|, Cray-style, as QD's `+`), the multiply and od x f64, 2 for the divide and
//! the square root: the quad-double tests' (`qd_tests`) thresholds of the accurate operations. Dot products of n
//! terms are checked against the accumulator's proven bound, (|Z| + c_n W) u^8 with W = sum |a_i b_i| (`acc_thr`).
//!
//! The adds, the multiply and od x f64 have a proven bound, u^8 (1 + 2^-14) with u = 2^-53, which is 1/64 unit and
//! holds for operands on the ladder |a_i| <= 2^(-52 i) |a_0| without underflow (`proven`), and the square root one of
//! 3.0000319 u^8 (`SQRT_PROVEN_THR`). Every input within those hypotheses is checked against its bound too
//! (`PROVEN_THR`), and `od_worst_case_inputs` reaches the first. Random inputs stay
//! near 10^-4 units, the ladder category's inputs included, because the error is set by the result's parts.
//!
//! The opt-in sloppy multiply (`OD::sloppy_mul`) and its dot products are checked against 4 units (`SLOPPY_THR`, fixed
//! before the first run) and their proven bounds, 2^31.93 u^8 |a_0 b_0| per product (`SLOPPY_C`, `SLOPPY_DOT_C`;
//! Theorem 30 of paper/expansion-error-bounds.pdf).
use crate::dd::DD;
use crate::exact::{BigU, Dy};
use crate::native::{ODotAcc, OV, V8};
#[cfg(feature = "mutate")]
use crate::od::mutate;
use crate::od::{OdAcc, OD};
use crate::qd::QD;
use crate::qd_tests::Rng;
use std::cmp::Ordering;

/// 2^-418, the unit of the error tables
fn unit() -> f64 {
    OD::EPS
}

/// the environment variable k as a number, or d
fn env_usize(k: &str, d: usize) -> usize {
    std::env::var(k).ok().and_then(|v| v.parse().ok()).unwrap_or(d)
}

/// OD_REPORT=1: print the tables
fn report() -> bool {
    std::env::var("OD_REPORT").map(|v| v == "1").unwrap_or(false)
}

/// select mutation id (0 = none) for the current thread
fn set_mutation(id: u32) {
    #[cfg(feature = "mutate")]
    mutate::set(id);
    #[cfg(not(feature = "mutate"))]
    assert_eq!(id, 0, "mutations need the mutate feature");
}

/// a random bit
fn coin(r: &mut Rng) -> bool {
    r.next() & 1 == 1
}

// -------------------------------------------------------------------------------------------------------------
// random exact inputs
// -------------------------------------------------------------------------------------------------------------

/// exact random value ±(1 + f)·2^e, f a random 511-bit fraction (more bits than eight parts hold)
fn rand_dy(r: &mut Rng, e: i64) -> Dy {
    let mut m: Vec<u64> = (0..8).map(|_| r.next()).collect();
    m[7] |= 1u64 << 63;
    Dy {
        neg: coin(r),
        m: BigU(m),
        e: e - 511,
    }
}

/// rand_dy with the binade drawn from lo..=hi
fn rde(r: &mut Rng, lo: i64, hi: i64) -> Dy {
    let e = r.int(lo, hi);
    rand_dy(r, e)
}

/// sum of 1-8 small integers k_i 2^(e - g_i) with random gaps (exact sums and zero error terms are common)
fn sparse_dy(r: &mut Rng, e: i64) -> Dy {
    let n = r.int(1, 8);
    let mut v = Dy::zero();
    let mut g = 0;
    for i in 0..n {
        let k = r.int(1, 2000) * if coin(r) { 1 } else { -1 };
        if i > 0 {
            g += r.int(0, 120);
        }
        v = v.add(&Dy::from_f64(k as f64).ldexp(e - g));
    }
    if v.is_zero() {
        Dy::from_f64(1.0).ldexp(e)
    } else {
        v
    }
}

/// 2^k (1 + s 2^-j f), j in a set around the part boundaries (j = 0: exactly 2^k)
fn near_pow2(r: &mut Rng, k: i64) -> Dy {
    const J: [i64; 16] = [0, 1, 52, 53, 54, 105, 106, 159, 212, 265, 318, 371, 423, 424, 425, 430];
    let j = if coin(r) {
        J[r.int(0, 15) as usize]
    } else {
        r.int(1, 440)
    };
    let p = Dy::from_f64(1.0).ldexp(k);
    if j == 0 {
        return if coin(r) { p } else { p.neg() };
    }
    let mut d = rand_dy(r, k - j);
    if coin(r) {
        d = d.ldexp(-1);
    }
    let v = p.add(&d);
    if coin(r) {
        v
    } else {
        v.neg()
    }
}

/// eight parts by greedy round-to-nearest (x0 = RN(v), x1 = RN(v - x0), ...): a normalized octo-double built
/// without any octo-double arithmetic (a lower part that rounds to zero is +0 and ends the parts)
fn greedy8(d: &Dy) -> [f64; 8] {
    let mut r = d.clone();
    let mut x = [0.0; 8];
    for (i, xi) in x.iter_mut().enumerate() {
        let c = r.to_f64();
        if c == 0.0 || !c.is_finite() {
            if i == 0 {
                *xi = c;
            }
            break;
        }
        *xi = c;
        r = r.sub(&Dy::from_f64(c));
    }
    x
}

/// a dyadic as eight greedy parts
fn od_of(d: &Dy) -> OD {
    OD { x: greedy8(d) }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Kind {
    Add,
    Mul,
    Div,
    Sqrt,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Cat {
    Wide,
    Cancel,
    Overflow,
    Underflow,
    Pow2,
    Sparse,
    Computed,
    Gap,
    Ladder,
}

/// the categories whose results' bits `OD_DIGEST` pins and whose results' parts do not overlap
const CATS: [Cat; 7] = [
    Cat::Wide,
    Cat::Cancel,
    Cat::Overflow,
    Cat::Underflow,
    Cat::Pow2,
    Cat::Sparse,
    Cat::Computed,
];

/// every category of the accuracy checks: `CATS`, and the gap and ladder categories, whose results' bits `GAP_DIGEST`
/// and `LADDER_DIGEST` pin
const ALL_CATS: [Cat; 9] = [
    Cat::Wide,
    Cat::Cancel,
    Cat::Overflow,
    Cat::Underflow,
    Cat::Pow2,
    Cat::Sparse,
    Cat::Computed,
    Cat::Gap,
    Cat::Ladder,
];

/// the category's name in the tables
fn cat_name(c: Cat) -> &'static str {
    match c {
        Cat::Wide => "wide",
        Cat::Cancel => "cancel",
        Cat::Overflow => "overflow",
        Cat::Underflow => "underflow",
        Cat::Pow2 => "pow2",
        Cat::Sparse => "sparse",
        Cat::Computed => "computed",
        Cat::Gap => "gap",
        Cat::Ladder => "ladder",
    }
}

/// a value with a part far below the one above it: x0 + x1 with x1 2^-60 to 2^-400 below x0, or greedy parts with
/// those below the first zeroed at random (zero parts between nonzero ones)
fn gapped(r: &mut Rng) -> OD {
    let e = r.int(-20, 20);
    let mut x = greedy8(&rand_dy(r, e));
    if coin(r) {
        let g = r.int(60, 400);
        x = [x[0], rand_dy(r, e - g).to_f64(), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
    } else {
        for xi in x.iter_mut().skip(1) {
            if coin(r) {
                *xi = 0.0;
            }
        }
    }
    OD { x }
}

/// A value whose parts sit near the top of the ladder that the proven bounds allow, |x_i| from 1/2 to 1 ulp of the
/// part above, signs at random. Greedy parts sit about 2^-8 lower over eight parts, so their products' order-8
/// terms are far below u^8 |ab|, where this value's are a few u^8 |ab|, as in the worst case.
fn topped(r: &mut Rng) -> OD {
    let mut x = [0.0; 8];
    let e = r.int(-20, 20);
    x[0] = rand_dy(r, e).to_f64();
    for i in 1..8 {
        // m in [1/2, 1) with 53 bits, or 1 (a part of a whole ulp) one time in eight
        let m = if r.int(0, 7) == 0 {
            1.0
        } else {
            0.5 + (r.next() >> 12) as f64 * 2f64.powi(-53)
        };
        let s = if coin(r) { -m } else { m };
        x[i] = s * ulp(x[i - 1]).to_f64();
    }
    OD { x }
}

/// one input pair (a, b) for an operation of the given kind (b unused by sqrt)
fn gen(r: &mut Rng, kind: Kind, cat: Cat) -> (OD, OD) {
    use Cat::*;
    use Kind::*;
    let one = Dy::from_f64(1.0);
    if cat == Ladder {
        let a = topped(r);
        return match kind {
            Sqrt => (a.abs(), OD::ONE),
            _ => (a, topped(r)),
        };
    }
    if cat == Gap {
        // a product of a gapped value sends the levels to the renormalization out of magnitude order; a (1/a)
        // nearly cancels them (the square root's last Newton step forms such a product)
        let a = gapped(r);
        return match kind {
            Add => (a, gapped(r)),
            Sqrt => (a.abs(), OD::ONE),
            Mul => {
                if coin(r) {
                    (a, OD::ONE / a)
                } else {
                    (a, gapped(r))
                }
            }
            Div => (gapped(r), a),
        };
    }
    if cat == Computed {
        // operands produced by octo-double arithmetic (renormalized parts, not greedy ones): x y (1/y); for the adds
        // b = -(x y (1/y)) or b = -(a + d) (|d| = 2^-k |a|), so a + b is small with misaligned parts, as in a
        // residual's subtraction of two nearly equal values computed along different routes
        let route = |r: &mut Rng, x: OD| -> OD {
            let y = od_of(&rde(r, -2, 2));
            (x * y) * (OD::ONE / y)
        };
        let a = od_of(&rde(r, -20, 20));
        return match kind {
            Add => {
                if coin(r) {
                    (a, -route(r, a))
                } else {
                    let k = r.int(1, 440);
                    let d = od_of(&rand_dy(r, -k)) * a;
                    (a, -(a + d))
                }
            }
            Sqrt => (route(r, a).abs(), OD::ONE),
            _ => {
                let b = od_of(&rde(r, -20, 20));
                (route(r, a), route(r, b))
            }
        };
    }
    let (a, b): (Dy, Dy) = match (cat, kind) {
        (Wide, Sqrt) => (rde(r, -800, 800).abs(), one),
        (Wide, Add) => (rde(r, -400, 400), rde(r, -400, 400)),
        // products and quotients within 2^-600..2^600, where all eight parts of the result are normal
        (Wide, _) => (rde(r, -300, 300), rde(r, -300, 300)),
        // a + b small: b = -a + d with |d| = 2^-k |a|
        (Cancel, Add) => {
            let e = r.int(-50, 50);
            let a = rand_dy(r, e);
            let d = rde(r, e - 460, e - 1);
            let b = a.neg().add(&d);
            (a, b)
        }
        // products near 1: (1 + d1)(1 - d2)
        (Cancel, Mul) => {
            let d1 = rde(r, -440, -1).abs();
            let d2 = rde(r, -440, -1).abs();
            (one.add(&d1), one.sub(&d2))
        }
        // quotients near 1: b = a (1 + d)
        (Cancel, Div) => {
            let a = rde(r, -50, 50);
            let d = rde(r, -440, -1);
            let b = a.add(&a.mul(&d));
            (a, b)
        }
        (Cancel, Sqrt) => (one.add(&rde(r, -440, -1)), one),
        (Overflow, Add) => (rde(r, 1000, 1021), rde(r, 900, 1021)),
        (Overflow, Mul) => {
            let t = r.int(990, 1020);
            let ea = r.int(0, 1020);
            (rand_dy(r, ea), rand_dy(r, t - ea))
        }
        (Overflow, Div) => {
            if coin(r) {
                (rde(r, 990, 1020), rde(r, -1, 0))
            } else {
                (rde(r, 990, 1021), rde(r, 990, 1021))
            }
        }
        (Overflow, Sqrt) => (rde(r, 990, 1022).abs(), one),
        // the parts reach the subnormals: the eighth part is near 2^-1074 well before the first is
        (Underflow, Add) => (rde(r, -1020, -620), rde(r, -1020, -620)),
        (Underflow, Mul) => {
            let t = r.int(-1020, -620);
            let ea = r.int(-500, -300);
            (rand_dy(r, ea), rand_dy(r, t - ea))
        }
        (Underflow, Div) => {
            let t = r.int(-1020, -620);
            let eb = r.int(-50, 100);
            (rand_dy(r, t + eb), rand_dy(r, eb))
        }
        (Underflow, Sqrt) => (rde(r, -1070, -620).abs(), one),
        (Pow2, Sqrt) => {
            let k = r.int(-40, 40);
            (near_pow2(r, k).abs(), one)
        }
        (Pow2, _) => {
            let k = r.int(-20, 20);
            (rde(r, -20, 20), near_pow2(r, k))
        }
        (Sparse, Sqrt) => {
            let e = r.int(-30, 30);
            (sparse_dy(r, e).abs(), one)
        }
        (Sparse, Add) => {
            let e = r.int(-30, 30);
            let f = e + r.int(-60, 60);
            (sparse_dy(r, e), sparse_dy(r, f))
        }
        (Sparse, _) => {
            let (e, f) = (r.int(-30, 30), r.int(-30, 30));
            (sparse_dy(r, e), sparse_dy(r, f))
        }
        (Computed, _) | (Gap, _) | (Ladder, _) => unreachable!(),
    };
    (od_of(&a), od_of(&b))
}

// -------------------------------------------------------------------------------------------------------------
// operations, their references and thresholds
// -------------------------------------------------------------------------------------------------------------

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Op {
    Add,
    AddF,
    Mul,
    MulF,
    Div,
    Sqrt,
}

const OPS: [Op; 6] = [Op::Add, Op::AddF, Op::Mul, Op::MulF, Op::Div, Op::Sqrt];

/// the operation's name in the tables
fn op_name(op: Op) -> &'static str {
    match op {
        Op::Add => "add",
        Op::AddF => "od + f64",
        Op::Mul => "mul",
        Op::MulF => "od x f64",
        Op::Div => "div",
        Op::Sqrt => "sqrt",
    }
}

/// the kind of exact reference the operation is checked against
fn op_kind(op: Op) -> Kind {
    match op {
        Op::Add | Op::AddF => Kind::Add,
        Op::Mul | Op::MulF => Kind::Mul,
        Op::Div => Kind::Div,
        Op::Sqrt => Kind::Sqrt,
    }
}

/// Cray-style measure |error| / (|a| + |b|) instead of the relative error
fn op_cray(op: Op) -> bool {
    matches!(op, Op::Add | Op::AddF)
}

/// pass threshold in units of 2^-418 (fixed before the runs)
fn op_thr(op: Op) -> f64 {
    match op {
        Op::Add | Op::AddF | Op::Mul | Op::MulF => 1.0,
        Op::Div | Op::Sqrt => 2.0,
    }
}

/// The proven bound of add, od + f64, mul and od x f64, in units of 2^-418: u^8 (1 + 2^-14) with u = 2^-53, relative
/// to |ab| for the products and to |a| + |b| for the adds. It holds for operands within `proven`'s hypotheses.
const PROVEN_THR: f64 = (1.0 + 1.0 / 16384.0) / 64.0;

/// The square root's proven bound, 3.0000319 u^8 (Theorem 22 of paper/expansion-error-bounds.pdf, theta = 2), rounded
/// up, in units of 2^-418. The test's measure |r^2 - x| / (2x) exceeds the relative error by at most its square.
const SQRT_PROVEN_THR: f64 = 3.000_032 / 64.0;

/// the proven bound of an operation, for the inputs `proven` admits
fn proven_thr(op: Op) -> f64 {
    match op {
        Op::Sqrt => SQRT_PROVEN_THR,
        _ => PROVEN_THR,
    }
}

/// exponent of the last bit of a finite nonzero x
fn lsb(x: f64) -> i64 {
    let e = ((x.to_bits() >> 52) & 0x7ff) as i64;
    if e == 0 {
        -1074
    } else {
        e - 1075
    }
}

/// |a_i| <= 2^(-52 i) |a_0| for every part, exactly: the ladder that every ulp-nonoverlapping value has
fn ladder(a: &OD) -> bool {
    let a0 = Dy::from_f64(a.x[0]);
    (1..8).all(|i| Dy::from_f64(a.x[i]).cmp_abs(&a0.ldexp(-52 * i as i64)) != Ordering::Greater)
}

/// every product of parts that the operation forms is a multiple of 2^-1074, so that a rounding below 2^-1022 is
/// exact and two-product is error-free (e_a + e_b >= -970 in exponents)
fn products_fit(a: &[f64], b: &[f64], max_order: usize) -> bool {
    a.iter().enumerate().all(|(i, &x)| {
        b.iter()
            .enumerate()
            .all(|(j, &y)| i + j > max_order || x == 0.0 || y == 0.0 || lsb(x) + lsb(y) >= -1074)
    })
}

/// The operands are within the hypotheses of the proven bound: finite, both octo-double operands on the ladder, every
/// rounding either normal or exact, and the leading parts well below overflow. Only the operation's inputs are
/// read, so division, whose intermediate quotients these do not bound, is left out. The square root works on its
/// argument scaled into [1, 4), so a positive argument whose nonzero parts are within 2^-500 of the leading one keeps
/// every intermediate far from underflow.
fn proven(op: Op, a: OD, b: OD) -> bool {
    let fin = |x: &OD| x.x.iter().all(|v| v.is_finite());
    let big = |x: f64| x.abs() >= 2f64.powi(500);
    if !fin(&a) || !fin(&b) || !ladder(&a) {
        return false;
    }
    match op {
        Op::Add => ladder(&b) && !big(a.x[0]) && !big(b.x[0]),
        Op::AddF => !big(a.x[0]) && !big(b.x[0]),
        Op::Mul => ladder(&b) && products_fit(&a.x, &b.x, 8) && !big(a.x[0]) && !big(b.x[0]),
        Op::MulF => products_fit(&a.x, &b.x[..1], 7) && !big(a.x[0]) && !big(b.x[0]),
        Op::Sqrt => {
            a.x[0] > 0.0
                && !big(a.x[0])
                && a.x[0] >= 2f64.powi(-500)
                && a.x[1..]
                    .iter()
                    .all(|&v| v == 0.0 || v.abs() >= a.x[0] * 2f64.powi(-500))
        }
        Op::Div => false,
    }
}

/// the scalar operation
fn apply_s(op: Op, a: OD, b: OD) -> OD {
    match op {
        Op::Add => a + b,
        Op::AddF => a.add_f(b.x[0]),
        Op::Mul => a * b,
        Op::MulF => a.mul_f(b.x[0]),
        Op::Div => a / b,
        Op::Sqrt => a.sqrt(),
    }
}

/// the 8-lane operation
fn apply_v(op: Op, a: OV, b: OV) -> OV {
    match op {
        Op::Add => a + b,
        Op::AddF => a.add_v(b.c[0]),
        Op::Mul => a * b,
        Op::MulF => a.mul_v(b.c[0]),
        Op::Div => a / b,
        Op::Sqrt => a.sqrt(),
    }
}

/// the second operand as the operation sees it
fn b_exact(op: Op, b: OD) -> Dy {
    match op {
        Op::AddF | Op::MulF => Dy::from_f64(b.x[0]),
        _ => b.exact(),
    }
}

/// (err, scale, rel, allow): the check is err <= thr 2^-418 scale + allow, the reported value err / scale; rel is the
/// relative error of the result (= err / scale except for the Cray-style measure). In the near-underflow inputs
/// allow is 16·2^-1074 on the result (its parts cannot go below 2^-1074), carried to err as in `qd_tests`.
fn exact_err(op: Op, a: OD, b: OD, r: OD, under: bool) -> (Dy, Dy, f64, Dy) {
    let (ae, be, re) = (a.exact(), b_exact(op, b), r.exact());
    let tiny = Dy::from_f64(16.0).ldexp(-1074);
    let allow = if !under {
        Dy::zero()
    } else {
        match op_kind(op) {
            Kind::Div => tiny.add(&tiny.mul(&be.abs())),
            Kind::Sqrt => tiny.add(&tiny.mul(&re.abs()).ldexp(1)),
            _ => tiny,
        }
    };
    match op_kind(op) {
        Kind::Add => {
            let x = ae.add(&be);
            let err = re.sub(&x).abs();
            let rel = err.ratio(&x);
            let scale = if op_cray(op) { ae.abs().add(&be.abs()) } else { x.abs() };
            (err, scale, rel, allow)
        }
        Kind::Mul => {
            let x = ae.mul(&be);
            let err = re.sub(&x).abs();
            let rel = err.ratio(&x);
            (err, x.abs(), rel, allow)
        }
        Kind::Div => {
            // |r - a/b| / |a/b| = |r b - a| / |a|
            let err = re.mul(&be).sub(&ae).abs();
            let rel = err.ratio(&ae);
            (err, ae.abs(), rel, allow)
        }
        Kind::Sqrt => {
            // r = sqrt(a)(1 + d): r^2 - a = a (2d + d^2)
            let err = re.mul(&re).sub(&ae).abs();
            let scale = ae.abs().ldexp(1);
            let rel = err.ratio(&scale);
            (err, scale, rel, allow)
        }
    }
}

/// err <= thr 2^-418 scale + allow, exactly
fn within(err: &Dy, scale: &Dy, thr: f64, allow: &Dy) -> bool {
    let lim = scale.mul(&Dy::from_f64(thr * unit())).add(allow);
    err.cmp_abs(&lim) != Ordering::Greater
}

/// every part the same bits (any NaN equal to any NaN)
fn same(a: OD, b: OD) -> bool {
    a.x.iter()
        .zip(b.x.iter())
        .all(|(x, y)| x.to_bits() == y.to_bits() || (x.is_nan() && y.is_nan()))
}

/// FNV-1a over the bits of the parts
fn fnv(h: &mut u64, x: OD) {
    for v in x.x {
        let v = if v.is_nan() { f64::NAN } else { v };
        for byte in v.to_bits().to_le_bytes() {
            *h ^= byte as u64;
            *h = h.wrapping_mul(0x100000001b3);
        }
    }
}

/// One line of the results.
#[derive(Clone, Debug)]
struct Res {
    what: String,
    cat: &'static str,
    n: usize,
    /// max and 99.9th percentile of the check's measure, in units of 2^-418
    max: f64,
    p999: f64,
    /// max of the relative error (= the measure except for the Cray-style adds), in units of 2^-418
    rel_max: f64,
    thr: f64,
    /// samples outside the bound
    fails: usize,
    /// lanes whose 8-lane result differs from the scalar one in any bit
    mism: usize,
    digest: u64,
    /// the samples within the proven bound's hypotheses, checked against `PROVEN_THR`
    proven: Proven,
}

/// samples within the proven bound's hypotheses: their number, the worst measure in units of 2^-418, those outside
#[derive(Clone, Debug, Default)]
struct Proven {
    n: usize,
    max: f64,
    fails: usize,
}

impl Res {
    /// no sample outside the bounds and no lane different from the scalar result
    fn ok(&self) -> bool {
        self.fails == 0 && self.proven.fails == 0 && self.mism == 0
    }
}

/// the q-quantile of v (sorted in place)
fn pct(v: &mut [f64], q: f64) -> f64 {
    if v.is_empty() {
        return 0.0;
    }
    v.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
    v[((v.len() - 1) as f64 * q).floor() as usize]
}

/// a number for the tables
fn fmt_u(x: f64) -> String {
    if x == 0.0 {
        "0".into()
    } else if x.is_infinite() {
        "inf".into()
    } else if (0.01..1000.0).contains(&x) {
        format!("{x:.3}")
    } else {
        format!("{x:.2e}")
    }
}

/// print the rows of an accuracy table
fn print_table(title: &str, rs: &[Res]) {
    println!("{title}");
    println!(
        "  {:<14} {:<10} {:>6} {:>10} {:>10} {:>10} {:>6} {:>6} {:>6} {:>7} {:>10} {:>7}",
        "operation", "inputs", "n", "max", "99.9%", "max rel", "thr", "fails", "lanes", "proven", "max", "fails"
    );
    for r in rs {
        println!(
            "  {:<14} {:<10} {:>6} {:>10} {:>10} {:>10} {:>6} {:>6} {:>6} {:>7} {:>10} {:>7}",
            r.what,
            r.cat,
            r.n,
            fmt_u(r.max),
            fmt_u(r.p999),
            fmt_u(r.rel_max),
            fmt_u(r.thr),
            r.fails,
            r.mism,
            r.proven.n,
            fmt_u(r.proven.max),
            r.proven.fails
        );
    }
}

/// inputs of one operation and category (generated with no mutation active)
fn inputs(op: Op, cat: Cat, n: usize, seed: u64) -> Vec<(OD, OD)> {
    let mut r = Rng::new(seed ^ ((op as u64) << 32) ^ ((cat as u64) << 40) ^ 0x0c70);
    (0..n).map(|_| gen(&mut r, op_kind(op), cat)).collect()
}

/// scalar and 8-lane results of one operation on the inputs
fn results(op: Op, inp: &[(OD, OD)]) -> (Vec<OD>, Vec<OD>) {
    let rs: Vec<OD> = inp.iter().map(|&(a, b)| apply_s(op, a, b)).collect();
    let mut rv = Vec::with_capacity(inp.len());
    for c in inp.chunks(8) {
        let mut la = [OD::ZERO; 8];
        let mut lb = [OD::ONE; 8];
        for (j, &(a, b)) in c.iter().enumerate() {
            la[j] = a;
            lb[j] = b;
        }
        let out = apply_v(op, OV::from_lanes(&la), OV::from_lanes(&lb)).lanes();
        rv.extend_from_slice(&out[..c.len()]);
    }
    (rs, rv)
}

/// accuracy and agreement of every operation in every category, with mutation `id` active during the arithmetic
fn run_ops(n: usize, seed: u64, id: u32) -> Vec<Res> {
    let mut out = Vec::new();
    for op in OPS {
        for cat in ALL_CATS {
            let inp = inputs(op, cat, n, seed);
            set_mutation(id);
            let (rs, rv) = results(op, &inp);
            set_mutation(0);
            let under = cat == Cat::Underflow;
            let (mut ms, mut fails, mut mism, mut rel_max) = (Vec::with_capacity(n), 0, 0, 0f64);
            let mut digest = 0xcbf29ce484222325u64;
            let mut pr = Proven::default();
            for (k, &(a, b)) in inp.iter().enumerate() {
                let r = rs[k];
                fnv(&mut digest, r);
                if !same(r, rv[k]) {
                    mism += 1;
                }
                if !r.x.iter().all(|v| v.is_finite()) {
                    fails += 1;
                    ms.push(f64::INFINITY);
                    continue;
                }
                let (err, scale, rel, allow) = exact_err(op, a, b, r, under);
                if !within(&err, &scale, op_thr(op), &allow) {
                    fails += 1;
                }
                if proven(op, a, b) {
                    pr.n += 1;
                    pr.max = pr.max.max(err.ratio(&scale) / unit());
                    if !within(&err, &scale, proven_thr(op), &Dy::zero()) {
                        pr.fails += 1;
                    }
                }
                ms.push(err.ratio(&scale) / unit());
                rel_max = rel_max.max(rel / unit());
            }
            let max = ms.iter().cloned().fold(0.0, f64::max);
            let p999 = pct(&mut ms, 0.999);
            out.push(Res {
                what: op_name(op).into(),
                cat: cat_name(cat),
                n,
                max,
                p999,
                rel_max,
                thr: op_thr(op),
                fails,
                mism,
                digest,
                proven: pr,
            });
        }
    }
    out
}

// -------------------------------------------------------------------------------------------------------------
// the dot accumulator
// -------------------------------------------------------------------------------------------------------------

const ACC_CATS: [&str; 4] = ["random", "cancel", "wide", "mixed"];

/// The accumulator's proven bound for len products of operands on the ladder |a_i| <= 2^(-52 i) |a_0|, in units of
/// 2^-418 relative to W = sum |a_i b_i|: |Z - x| <= (|Z| + c W) u^8 <= (1 + c) W / 64 units. c is the second term's
/// constant, rounded up, for len = 1, 4, 16 and 64 at n = 8, p = 53, theta = 2 (2^-14.92, 2^-14.28, 2^-10.55,
/// 2^2.68). The paper's W sums the leading parts' |a_0 b_0|, within a factor 1 + 5u of the W here, which the rounding
/// up absorbs. c grows like len^9 u, so the bound stays near u^8 W only up to about 50 products, and a sum of 2048
/// constructed terms reaches 2^23.8 u^8 |Z|. The proof is the paper's section on sums of products
/// (paper/expansion-error-bounds-accumulator.tex), and paper/accumulator/ computes the constants.
fn acc_thr(len: usize) -> f64 {
    let c = match len {
        1 => 3.3e-5,
        4 => 5.1e-5,
        16 => 6.7e-4,
        64 => 6.43,
        _ => panic!("acc_thr: no constant for {len} terms"),
    };
    (1.0 + c) / 64.0
}

/// accuracy of ODotAcc / OdAcc on dot products of length len (8 per sample, one per lane): |error| / sum |a_i b_i|;
/// with sloppy, of their sloppy products (`add_prod_sloppy`), against `SLOPPY_THR` and their proven bound
fn run_acc(n: usize, seed: u64, id: u32, sloppy: bool) -> Vec<Res> {
    let mut out = Vec::new();
    for len in [1usize, 4, 16, 64] {
        for (ci, cat) in ACC_CATS.iter().enumerate() {
            let mut r = Rng::new(seed ^ ((len as u64) << 20) ^ ((ci as u64) << 30) ^ 0x0acc);
            let thr = if sloppy { SLOPPY_THR } else { acc_thr(len) };
            let mut pr = Proven::default();
            let (mut ms, mut fails, mut mism, mut rel_max) = (Vec::new(), 0, 0, 0f64);
            let mut digest = 0xcbf29ce484222325u64;
            let nsamp = ((n / len.clamp(1, 16)).max(8)) / 8 * 8;
            for _ in 0..nsamp / 8 {
                let mut a = vec![[OD::ZERO; 8]; len];
                let mut b = vec![[OD::ZERO; 8]; len];
                for lane in 0..8 {
                    for i in 0..len {
                        let (x, y) = match *cat {
                            "wide" => (rde(&mut r, -100, 100), rde(&mut r, -100, 100)),
                            "cancel" if i >= len / 2 && len > 1 => {
                                // a_i b_i = -(a_k b_k)(1 + d): pairs that nearly cancel
                                let k = i - len / 2;
                                let ak = a[k][lane].exact();
                                let d = rde(&mut r, -400, -1);
                                (ak.neg().add(&ak.mul(&d)), b[k][lane].exact())
                            }
                            _ => (rde(&mut r, -3, 3), rde(&mut r, -3, 3)),
                        };
                        a[i][lane] = od_of(&x);
                        b[i][lane] = od_of(&y);
                    }
                }
                let plain = |i: usize| *cat == "mixed" && i % 4 == 3;
                set_mutation(id);
                let mut acc = ODotAcc::zero();
                for i in 0..len {
                    acc = if plain(i) {
                        acc.add(OV::from_lanes(&a[i]))
                    } else if sloppy {
                        let (x, y) = (OV::from_lanes(&a[i]), OV::from_lanes(&b[i]));
                        let mut m = acc;
                        m.add_prod_sloppy_mut(&x, &y);
                        let v = acc.add_prod_sloppy(x, y);
                        // the by-value and in-place forms are one operation
                        assert!(
                            (0..9).all(|l| v.s[l].to_array().map(f64::to_bits) == m.s[l].to_array().map(f64::to_bits)),
                            "add_prod_sloppy_mut vs add_prod_sloppy"
                        );
                        v
                    } else {
                        acc.add_prod(OV::from_lanes(&a[i]), OV::from_lanes(&b[i]))
                    };
                }
                let rv = acc.finish().lanes();
                let rs: Vec<OD> = (0..8)
                    .map(|lane| {
                        let mut s = OdAcc::zero();
                        for i in 0..len {
                            s = if plain(i) {
                                s.add(a[i][lane])
                            } else if sloppy {
                                s.add_prod_sloppy(a[i][lane], b[i][lane])
                            } else {
                                s.add_prod(a[i][lane], b[i][lane])
                            };
                        }
                        s.finish()
                    })
                    .collect();
                set_mutation(0);
                for lane in 0..8 {
                    fnv(&mut digest, rs[lane]);
                    if !same(rs[lane], rv[lane]) {
                        mism += 1;
                    }
                    let (mut x, mut s) = (Dy::zero(), Dy::zero());
                    for i in 0..len {
                        let t = if plain(i) {
                            a[i][lane].exact()
                        } else {
                            a[i][lane].exact().mul(&b[i][lane].exact())
                        };
                        s = s.add(&t.abs());
                        x = x.add(&t);
                    }
                    let err = rs[lane].exact().sub(&x).abs();
                    if !within(&err, &s, thr, &Dy::zero()) {
                        fails += 1;
                    }
                    if sloppy {
                        // (|Z| + c_K W) u^8, W = sum |a_i b_i| here (the bound's W, sum |a_i0 b_i0|, is within 2^-50 of
                        // it on these greedy operands, which the constant's last digits cover)
                        let lim = x
                            .abs()
                            .add(&s.mul(&Dy::from_f64(SLOPPY_DOT_C)))
                            .mul(&Dy::from_f64(u8()));
                        pr.n += 1;
                        pr.max = pr.max.max(err.ratio(&s) / unit());
                        if err.cmp_abs(&lim) == Ordering::Greater {
                            pr.fails += 1;
                        }
                    }
                    ms.push(err.ratio(&s) / unit());
                    rel_max = rel_max.max(err.ratio(&x) / unit());
                }
            }
            let max = ms.iter().cloned().fold(0.0, f64::max);
            let p999 = pct(&mut ms, 0.999);
            out.push(Res {
                what: format!("{}dot n={len}", if sloppy { "sloppy " } else { "" }),
                cat,
                n: ms.len(),
                max,
                p999,
                rel_max,
                thr,
                fails,
                mism,
                digest,
                proven: pr,
            });
        }
    }
    out
}

// -------------------------------------------------------------------------------------------------------------
// a double-double operand: od x dd, od + dd and the accumulator's od x dd against the promoted operations
// -------------------------------------------------------------------------------------------------------------

/// the operations with a double-double operand, each checked bit for bit against `*`, `+` or `add_prod` with the
/// operand promoted (`OD::from_dd`, `OV::from_dv`)
const DD_OPS: [&str; 3] = ["od x dd", "od + dd", "dot x dd"];

/// every bit the same, a NaN equal to any NaN: Rust leaves the sign and payload of a NaN result unspecified, and the
/// lanes and the scalar type already give NaNs of different signs on one input (as `same`)
fn bits_eq(a: &[f64], b: &[f64]) -> bool {
    a.len() == b.len()
        && a.iter()
            .zip(b)
            .all(|(x, y)| x.to_bits() == y.to_bits() || (x.is_nan() && y.is_nan()))
}

/// a double-double operand from b's two leading parts, a quarter of them with the low part +0 or -0 (an f64 constant)
fn dd_from(r: &mut Rng, b: OD) -> DD {
    match r.int(0, 7) {
        0 => DD { hi: b.x[0], lo: 0.0 },
        1 => DD { hi: b.x[0], lo: -0.0 },
        _ => DD { hi: b.x[0], lo: b.x[1] },
    }
}

/// the scalar result of a double-double operation (`DD_OPS[op]`) and its promoted reference, as their parts or the
/// accumulator's nine levels then its sum; the accumulator starts from start
fn dd_scalar(op: usize, a: OD, c: DD, start: OdAcc) -> (Vec<f64>, Vec<f64>) {
    let p = OD::from_dd(c);
    match op {
        0 => (a.mul_dd(c).x.to_vec(), (a * p).x.to_vec()),
        1 => (a.add_dd(c).x.to_vec(), (a + p).x.to_vec()),
        _ => {
            let (n, r) = (start.add_prod_dd(a, c), start.add_prod(a, p));
            let cat = |s: OdAcc| [&s.s[..], &s.finish().x[..]].concat();
            (cat(n), cat(r))
        }
    }
}

/// `dd_scalar` on 8 lanes, by lane
fn dd_lanes(op: usize, a: &[OD; 8], c: &[DD; 8], start: &[OdAcc; 8]) -> (Vec<Vec<f64>>, Vec<Vec<f64>>) {
    let av = OV::from_lanes(a);
    let cv = crate::dv::DV::<crate::simd::Native>::load(&c.map(|d| d.hi), &c.map(|d| d.lo));
    let p = OV::from_dv(cv);
    let lanes = |v: OV| -> Vec<Vec<f64>> { v.lanes().iter().map(|x| x.x.to_vec()).collect() };
    match op {
        0 => (lanes(av.mul_dv(cv)), lanes(av * p)),
        1 => (lanes(av.add_dv(cv)), lanes(av + p)),
        _ => {
            let mut s0 = ODotAcc::zero();
            for l in 0..9 {
                s0.s[l] = V8::load(&start.map(|a| a.s[l]));
            }
            let mut n = s0;
            n.add_prod_dv_mut(&av, &cv);
            let n2 = s0.add_prod_dv(av, cv);
            let r = s0.add_prod(av, p);
            let cat = |s: ODotAcc| -> Vec<Vec<f64>> {
                let f = s.finish().lanes();
                (0..8)
                    .map(|j| {
                        let mut v: Vec<f64> = s.s.iter().map(|l| l.to_array()[j]).collect();
                        v.extend_from_slice(&f[j].x);
                        v
                    })
                    .collect()
            };
            // the by-value and in-place forms are one operation
            let (nv, n2v) = (cat(n), cat(n2));
            assert!(
                nv.iter().zip(&n2v).all(|(x, y)| bits_eq(x, y)),
                "add_prod_dv_mut vs add_prod_dv"
            );
            (nv, cat(r))
        }
    }
}

/// Every operation of `DD_OPS` in every input category, with mutation id active in the new operations only: per row,
/// fails counts the results (scalar or lanes, levels and sums for the accumulator) that differ in any bit from the
/// promoted operation run with no mutation, and mism the lanes that differ from the scalar result. The accumulator
/// starts empty on even samples and from the previous sample's product on odd ones.
fn run_dd(n: usize, seed: u64, id: u32) -> Vec<Res> {
    let mut out = Vec::new();
    for (op, name) in DD_OPS.iter().enumerate() {
        for cat in ALL_CATS {
            let base = if op == 1 { Op::Add } else { Op::Mul };
            let inp = inputs(base, cat, n, seed);
            let mut r = Rng::new(seed ^ ((op as u64) << 24) ^ ((cat as u64) << 40) ^ 0x0ddc);
            let cs: Vec<DD> = inp.iter().map(|&(_, b)| dd_from(&mut r, b)).collect();
            let starts: Vec<OdAcc> = (0..inp.len())
                .map(|k| {
                    if k.is_multiple_of(2) {
                        OdAcc::zero()
                    } else {
                        OdAcc::zero().add_prod(inp[k - 1].0, inp[k - 1].1)
                    }
                })
                .collect();
            let (mut fails, mut mism) = (0, 0);
            for (c8, k0) in inp.chunks(8).zip((0..).step_by(8)) {
                let m = c8.len();
                let a: [OD; 8] = std::array::from_fn(|j| if j < m { c8[j].0 } else { OD::ONE });
                let c: [DD; 8] = std::array::from_fn(|j| if j < m { cs[k0 + j] } else { DD::ONE });
                let st: [OdAcc; 8] = std::array::from_fn(|j| if j < m { starts[k0 + j] } else { OdAcc::zero() });
                set_mutation(id);
                let news: Vec<Vec<f64>> = (0..m).map(|j| dd_scalar(op, a[j], c[j], st[j]).0).collect();
                let (newv, _) = dd_lanes(op, &a, &c, &st);
                set_mutation(0);
                let (_, refv) = dd_lanes(op, &a, &c, &st);
                for j in 0..m {
                    let refs = dd_scalar(op, a[j], c[j], st[j]).1;
                    fails += !bits_eq(&news[j], &refs) as usize + !bits_eq(&newv[j], &refv[j]) as usize;
                    mism += !bits_eq(&news[j], &newv[j]) as usize;
                }
            }
            out.push(row(name, cat_name(cat), n, fails, mism));
        }
    }
    out
}

// -------------------------------------------------------------------------------------------------------------
// the sloppy multiply
// -------------------------------------------------------------------------------------------------------------

/// u^8 = 2^-424
fn u8() -> f64 {
    2f64.powi(-424)
}

/// The sloppy multiply's proven bound for operands within `proven`'s hypotheses for the multiply: |ab - x| <= C u^8
/// |a_0 b_0|, C = R_s + Delta u = 2^31.9242 = 4.075102e9 (Theorem 30(b) of paper/expansion-error-bounds.pdf, theta =
/// 2, computed by `paper/sloppy_consts.py`), rounded up.
const SLOPPY_C: f64 = 4.0752e9;

/// The sloppy dot's proven bound for up to 64 terms (Theorem 30(c) of paper/expansion-error-bounds.pdf): |Z - x| <=
/// (|Z| + c W) u^8, W = sum |a_i0 b_i0|, c the largest of `paper/sloppy_consts.py`'s c_K for K = 1, 4, 16 and 64
/// (4.075102e9 + 6.4), rounded up.
const SLOPPY_DOT_C: f64 = 4.0752e9;

/// Pass threshold of the sloppy multiply's and the sloppy dot's measured error, in units of 2^-418 relative to |ab|
/// (to W for the dot), fixed before the first run: 4 units, 256 u^8. Expected from the level-7 content of the ladder
/// category, whose parts sit at the top of the ladder: 64 roundings of a level of about 10 u^7 |ab|.
const SLOPPY_THR: f64 = 4.0;

/// accuracy of the sloppy multiply (scalar and lanes) on the multiply's inputs of every category, against
/// `SLOPPY_THR` and, within `proven`'s hypotheses, the proven bound; with mutation id active
fn run_sloppy(n: usize, seed: u64, id: u32) -> Vec<Res> {
    let mut out = Vec::new();
    for cat in ALL_CATS {
        let inp = inputs(Op::Mul, cat, n, seed);
        set_mutation(id);
        let rs: Vec<OD> = inp.iter().map(|&(a, b)| a.sloppy_mul(b)).collect();
        let rv = sloppy_lanes(&inp);
        set_mutation(0);
        let under = cat == Cat::Underflow;
        let (mut ms, mut fails, mut mism, mut rel_max) = (Vec::with_capacity(n), 0, 0, 0f64);
        let mut digest = 0xcbf29ce484222325u64;
        let mut pr = Proven::default();
        for (k, &(a, b)) in inp.iter().enumerate() {
            let r = rs[k];
            fnv(&mut digest, r);
            if !same(r, rv[k]) {
                mism += 1;
            }
            if !r.x.iter().all(|v| v.is_finite()) {
                fails += 1;
                ms.push(f64::INFINITY);
                continue;
            }
            let (err, scale, rel, allow) = exact_err(Op::Mul, a, b, r, under);
            if !within(&err, &scale, SLOPPY_THR, &allow) {
                fails += 1;
            }
            if proven(Op::Mul, a, b) {
                let m = Dy::from_f64(a.x[0]).mul(&Dy::from_f64(b.x[0])).abs();
                pr.n += 1;
                pr.max = pr.max.max(err.ratio(&m) / u8());
                if !within(&err, &m, SLOPPY_C / 64.0, &Dy::zero()) {
                    pr.fails += 1;
                }
            }
            ms.push(err.ratio(&scale) / unit());
            rel_max = rel_max.max(rel / unit());
        }
        let max = ms.iter().cloned().fold(0.0, f64::max);
        let p999 = pct(&mut ms, 0.999);
        out.push(Res {
            what: "sloppy mul".into(),
            cat: cat_name(cat),
            n,
            max,
            p999,
            rel_max,
            thr: SLOPPY_THR,
            fails,
            mism,
            digest,
            proven: pr,
        });
    }
    out
}

/// Edge operands: zeros of either sign in any part, zero parts between nonzero ones, exact integers, values whose
/// lower parts underflow or are subnormal, values near overflow, infinities and NaNs (one with a payload).
fn od_edges() -> Vec<OD> {
    let u = 2f64.powi(-53);
    let nan_p = f64::from_bits(0x7ff8_0000_0000_1234);
    let mut v = vec![
        OD::ZERO,
        OD::from_f64(-0.0),
        OD { x: [-0.0; 8] },
        OD {
            x: [0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0],
        },
        OD::ONE,
        OD::from_f64(-1.0),
        OD::from_f64(3.0),
        OD::from_f64(-7.0),
        OD::from_f64(4503599627370497.0),
        OD {
            x: [1.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0],
        },
        OD {
            x: [-1.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0],
        },
        OD {
            x: [1.0, 0.0, u.powi(3), 0.0, 0.0, -u.powi(6), 0.0, 0.0],
        },
        OD {
            x: [-1.5, u, 0.0, 0.0, 0.0, 0.0, 0.0, u.powi(8)],
        },
        OD {
            x: [1.0, u, u * u, u.powi(3), u.powi(4), u.powi(5), u.powi(6), u.powi(7)],
        },
        OD {
            x: [1e-300, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        },
        OD {
            x: [-1e-300, 1e-317, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        },
        OD {
            x: [2f64.powi(-1022), -5e-324, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        },
        OD::from_f64(5e-324),
        OD::from_f64(-1e-310),
        OD {
            x: [1e250, 1e233, -1e216, 0.0, 0.0, 0.0, 0.0, 0.0],
        },
        OD::from_f64(1e300),
        OD::from_f64(-f64::MAX),
        OD::from_f64(f64::INFINITY),
        OD::from_f64(f64::NEG_INFINITY),
        OD::from_f64(f64::NAN),
        OD::from_f64(nan_p),
        OD {
            x: [1.0, nan_p, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
        },
        OD {
            x: [1.0, 0.0, 0.0, f64::INFINITY, 0.0, 0.0, 0.0, 0.0],
        },
    ];
    // a non-finite value or a -0 in each part: the skipped products are then not all zeros
    let full = [
        -1.25,
        u,
        -u * u,
        u.powi(3),
        -u.powi(4),
        u.powi(5),
        -u.powi(6),
        u.powi(7),
    ];
    for i in 0..8 {
        for s in [f64::INFINITY, f64::NEG_INFINITY, f64::NAN, -0.0] {
            let mut x = full;
            x[i] = s;
            v.push(OD { x });
        }
    }
    let mut r = Rng::new(0xed9e);
    for _ in 0..12 {
        let e = r.int(-30, 30);
        v.push(od_of(&rand_dy(&mut r, e)));
    }
    for _ in 0..4 {
        v.push(od_of(&rde(&mut r, -1060, -900)));
        v.push(od_of(&rde(&mut r, 900, 1020)));
    }
    v
}

/// edge double-double operands, the same kinds as `od_edges`, the low part of either sign or zero
fn dd_edges() -> Vec<DD> {
    let d = |hi: f64, lo: f64| DD { hi, lo };
    let u = 2f64.powi(-53);
    vec![
        d(0.0, 0.0),
        d(0.0, 1.0),
        d(-0.0, -u),
        d(u, 1.0),
        d(1.0, f64::INFINITY),
        d(-0.0, 0.0),
        d(0.0, -0.0),
        d(-0.0, -0.0),
        d(1.0, 0.0),
        d(1.0, -0.0),
        d(-1.0, 0.0),
        d(-1.0, -0.0),
        d(3.0, -u * 1.5),
        d(-7.0, u),
        d(0.1, -5.551115123125783e-18),
        d(1.0, u),
        d(1.0, -u / 2.0),
        d(2f64.powi(-1022), 0.0),
        d(5e-324, 0.0),
        d(-1e-300, 1e-317),
        d(1e300, -1e283),
        d(f64::MAX, 0.0),
        d(f64::INFINITY, 0.0),
        d(f64::NEG_INFINITY, -0.0),
        d(f64::NAN, 0.0),
        d(1.0, f64::NAN),
    ]
}

/// The edge pairs (`od_edges` x `dd_edges`): the new operations' levels (`alg::expand_dd` against `alg::expand`) and
/// results against the promoted ones, scalar and lanes, every bit. Returns the mismatches as (operation, a, c) and
/// the number of comparisons.
fn dd_edge_mismatches() -> (Vec<String>, usize) {
    let (ods, dds) = (od_edges(), dd_edges());
    let pairs: Vec<(OD, DD, OdAcc)> = ods
        .iter()
        .flat_map(|&a| dds.iter().map(move |&c| (a, c)))
        .enumerate()
        .map(|(k, (a, c))| {
            let start = if k.is_multiple_of(3) {
                OdAcc::zero()
            } else {
                OdAcc::zero().add_prod(ods[k % ods.len()], ods[(k / 7) % ods.len()])
            };
            (a, c, start)
        })
        .collect();
    let (mut bad, mut n) = (Vec::new(), 0);
    for &(a, c, start) in &pairs {
        let lv = crate::od::alg::expand_dd(&a.x, c.hi, c.lo);
        let lr = crate::od::alg::expand(&a.x, &OD::from_dd(c).x);
        n += 1;
        if !bits_eq(&lv, &lr) {
            bad.push(format!("levels {:?} x {:?}: {lv:?} vs {lr:?}", a.x, c));
        }
        for op in 0..3 {
            let (x, y) = dd_scalar(op, a, c, start);
            n += 1;
            if !bits_eq(&x, &y) {
                bad.push(format!("{} {:?} {:?}: {x:?} vs {y:?}", DD_OPS[op], a.x, c));
            }
        }
    }
    for ch in pairs.chunks(8) {
        let m = ch.len();
        let a: [OD; 8] = std::array::from_fn(|j| ch[j.min(m - 1)].0);
        let c: [DD; 8] = std::array::from_fn(|j| ch[j.min(m - 1)].1);
        let st: [OdAcc; 8] = std::array::from_fn(|j| ch[j.min(m - 1)].2);
        let cv = crate::dv::DV::<crate::simd::Native>::load(&c.map(|d| d.hi), &c.map(|d| d.lo));
        let av = OV::from_lanes(&a);
        let lv = crate::od::alg::expand_dd(&av.c, cv.hi, cv.lo);
        let lr = crate::od::alg::expand(&av.c, &OV::from_dv(cv).c);
        for j in 0..m {
            n += 1;
            let (x, y): (Vec<f64>, Vec<f64>) = (
                lv.iter().map(|l| l.to_array()[j]).collect(),
                lr.iter().map(|l| l.to_array()[j]).collect(),
            );
            if !bits_eq(&x, &y) {
                bad.push(format!("lanes: levels {:?} x {:?}", a[j].x, c[j]));
            }
        }
        for op in 0..3 {
            let (x, y) = dd_lanes(op, &a, &c, &st);
            for j in 0..m {
                n += 2;
                if !bits_eq(&x[j], &y[j]) {
                    bad.push(format!(
                        "lanes: {} {:?} {:?}: {:?} vs {:?}",
                        DD_OPS[op], a[j].x, c[j], x[j], y[j]
                    ));
                }
                // the lanes are the scalar operation
                if !bits_eq(&x[j], &dd_scalar(op, a[j], c[j], st[j]).0) {
                    bad.push(format!("lanes vs scalar: {} {:?} {:?}", DD_OPS[op], a[j].x, c[j]));
                }
            }
        }
    }
    (bad, n)
}

/// the sloppy multiply of the pairs on the lanes, 8 at a time
fn sloppy_lanes(inp: &[(OD, OD)]) -> Vec<OD> {
    let mut rv = Vec::with_capacity(inp.len());
    for c in inp.chunks(8) {
        let mut la = [OD::ZERO; 8];
        let mut lb = [OD::ONE; 8];
        for (j, &(a, b)) in c.iter().enumerate() {
            la[j] = a;
            lb[j] = b;
        }
        let out = OV::from_lanes(&la).sloppy_mul(OV::from_lanes(&lb)).lanes();
        rv.extend_from_slice(&out[..c.len()]);
    }
    rv
}

/// |ab - x| / (u^8 |a_0 b_0|) of the sloppy multiply (scalar), exactly, for the hill climb
fn sloppy_err(a: OD, b: OD) -> f64 {
    let x = a.exact().mul(&b.exact());
    let m = Dy::from_f64(a.x[0]).mul(&Dy::from_f64(b.x[0])).abs();
    a.sloppy_mul(b).exact().sub(&x).abs().ratio(&m) / u8()
}

/// one part of a value at the top of the ladder: s m ulp(prev), m in [1/2, 1] with 53 bits
fn top_part(r: &mut Rng, prev: f64) -> f64 {
    let m = if r.int(0, 7) == 0 {
        1.0
    } else {
        0.5 + (r.next() >> 12) as f64 * 2f64.powi(-53)
    };
    let s = if coin(r) { -1.0 } else { 1.0 };
    s * m * ulp(prev).to_f64()
}

/// A hill climb on the sloppy multiply's error relative to u^8 |a_0 b_0|: from `topped` pairs, change one part at a
/// time (a new mantissa and sign at the top of the ladder, its sign alone, or a zero), and keep the change when the
/// error grows. Returns the worst pair and its error.
fn sloppy_climb(seed: u64, starts: usize, steps: usize) -> (OD, OD, f64) {
    let mut r = Rng::new(seed);
    let mut best = (OD::ONE, OD::ONE, 0.0);
    for _ in 0..starts {
        let (mut a, mut b) = (topped(&mut r), topped(&mut r));
        let mut e = sloppy_err(a, b);
        for _ in 0..steps {
            let (mut a2, mut b2) = (a, b);
            let w = if coin(&mut r) { &mut a2 } else { &mut b2 };
            let i = r.int(1, 7) as usize;
            match r.int(0, 5) {
                0 => w.x[i] = -w.x[i],
                1 => w.x[i] = 0.0,
                _ => {
                    w.x[i] = top_part(
                        &mut r,
                        if w.x[i - 1] != 0.0 {
                            w.x[i - 1]
                        } else {
                            w.x[0] * 2f64.powi(-52 * (i as i32 - 1))
                        },
                    )
                }
            }
            // the parts below a changed one stay on the ladder only if the ladder holds: keep the pair if it does
            if !ladder(&a2) || !ladder(&b2) {
                continue;
            }
            let e2 = sloppy_err(a2, b2);
            if e2 > e {
                (a, b, e) = (a2, b2, e2);
            }
        }
        if e > best.2 {
            best = (a, b, e);
        }
    }
    best
}

// -------------------------------------------------------------------------------------------------------------
// conversions, comparisons and decimal input/output
// -------------------------------------------------------------------------------------------------------------

/// ulp of a finite nonzero f64
fn ulp(x: f64) -> Dy {
    let e = ((x.to_bits() >> 52) & 0x7ff) as i64;
    Dy::from_f64(1.0).ldexp(if e == 0 { -1074 } else { e - 1075 })
}

/// a decimal digit string's value times 10^(e10 + sh) as an exact integer Dy (e10 + sh >= 0)
fn dec_scaled(neg: bool, digits: &str, e10: i64, sh: i64) -> Dy {
    assert!(e10 + sh >= 0);
    let m = BigU::from_dec(digits).mul(&BigU::pow_small(10, (e10 + sh) as u64));
    Dy {
        neg: neg && !m.is_zero(),
        m,
        e: 0,
    }
}

/// 10^k, exactly
fn pow10(k: i64) -> Dy {
    Dy {
        neg: false,
        m: BigU::pow_small(10, k as u64),
        e: 0,
    }
}

/// a one-row result
fn row(what: &str, cat: &'static str, n: usize, fails: usize, mism: usize) -> Res {
    Res {
        what: what.into(),
        cat,
        n,
        max: fails as f64,
        p999: 0.0,
        rel_max: 0.0,
        thr: 0.0,
        fails,
        mism,
        digest: 0,
        proven: Proven::default(),
    }
}

/// decimal parsing and printing, round trips, the conversions to DD, QD and f64, and the order, against exact arithmetic, with mutation id active
fn run_io(n: usize, seed: u64, id: u32) -> Vec<Res> {
    let mut out = Vec::new();
    let mut r = Rng::new(seed ^ 0x0d10);
    // parsing: random decimals of 1-160 digits, exponents -250..300, greedy round-to-nearest checked exactly
    let mut greedy_fails = 0;
    for _ in 0..n {
        let nd = r.int(1, 160) as usize;
        let mut d = String::new();
        d.push((b'1' + (r.next() % 9) as u8) as char);
        for _ in 1..nd {
            d.push((b'0' + (r.next() % 10) as u8) as char);
        }
        let e10 = r.int(-250 - nd as i64, 300 - nd as i64);
        let neg = coin(&mut r);
        let s = format!("{}{}e{}", if neg { "-" } else { "" }, d, e10);
        set_mutation(id);
        let q = OD::parse(&s).unwrap();
        set_mutation(0);
        let sh = (-e10).max(0);
        let v = dec_scaled(neg, &d, e10, sh);
        let err = q.exact().mul(&pow10(sh)).sub(&v).abs();
        // |q - v| <= ulp(x7)/2 when all eight parts are used, else at most half the least subnormal (a remainder
        // that rounds to zero ends the parts); a zero lower part is +0
        let last = q.x.iter().rposition(|c| *c != 0.0).unwrap();
        let sub = Dy::from_f64(1.0).ldexp(-1075);
        let bound = if last == 7 { ulp(q.x[7]).ldexp(-1) } else { sub }.mul(&pow10(sh));
        if err.cmp_abs(&bound) == Ordering::Greater || q.x[1..].iter().any(|v| v.to_bits() == (-0.0f64).to_bits()) {
            greedy_fails += 1;
        }
    }
    out.push(row("parse", "greedy RN", n, greedy_fails, 0));

    // printing: correctly rounded, exactly checked; round trips through all the exact digits and through 140
    // (424 bits and the gaps between the parts' bits, about 7 on average), where every part is normal
    let (mut print_fails, mut np, mut rt_exact, mut rt140) = (0, 0, 0, 0);
    for _ in 0..n / 4 {
        let x = od_of(&rde(&mut r, -1000, 1000));
        for digits in [1usize, 17, 64, 126, 128, 130, 150] {
            np += 1;
            let s = x.to_sci(digits);
            let (neg, rest) = match s.strip_prefix('-') {
                Some(t) => (true, t),
                None => (false, s.as_str()),
            };
            let p = rest.find('e').unwrap();
            let e: i64 = rest[p + 1..].parse().unwrap();
            let m: String = rest[..p].chars().filter(|c| *c != '.').collect();
            let e = e - (m.len() as i64 - 1);
            let sh = (1 - e).max(0);
            let xs = x.exact().mul(&pow10(sh));
            let ds = dec_scaled(neg, &m, e, sh);
            let half = dec_scaled(false, "5", e - 1, sh);
            let c = xs.sub(&ds).abs().cmp_abs(&half);
            let even = BigU::from_dec(&m).divrem_small(2).1 == 0;
            if c == Ordering::Greater || (c == Ordering::Equal && !even) || m.len() != digits || neg != x.exact().neg {
                print_fails += 1;
            }
        }
        let x = od_of(&rde(&mut r, -500, 500));
        let (_, dm, _) = x.exact().to_decimal();
        let all = dm.to_dec().trim_end_matches('0').len().max(1);
        set_mutation(id);
        let y = OD::parse(&x.to_sci(all)).unwrap();
        let y140 = OD::parse(&x.to_sci(140)).unwrap();
        set_mutation(0);
        rt_exact += !same(x, y) as usize;
        rt140 += !same(x, y140) as usize;
    }
    out.push(row("print", "rounding", np, print_fails, 0));
    out.push(row("round trip", "exact", n / 4, rt_exact, 0));
    out.push(row("round trip", "140 digits", n / 4, rt140, 0));

    // conversions: to_dd within 2^-105 relative, to_qd within 2^-210, to_f64 within one ulp; the lanes bit for bit
    let (mut fails, mut conv_mism) = (0, 0);
    let mut lanes = Vec::new();
    for _ in 0..n {
        let x = od_of(&rde(&mut r, -900, 900));
        let xv = x.exact();
        let (d, q, f) = (x.to_dd(), x.to_qd(), x.to_f64());
        let tol = |k| xv.abs().ldexp(k);
        if Dy::sum(&[d.hi, d.lo]).sub(&xv).abs().cmp_abs(&tol(-105)) == Ordering::Greater {
            fails += 1;
        }
        if q.exact().sub(&xv).abs().cmp_abs(&tol(-210)) == Ordering::Greater {
            fails += 1;
        }
        if Dy::from_f64(f).sub(&xv).abs().cmp_abs(&ulp(f)) == Ordering::Greater {
            fails += 1;
        }
        if OD::from_dd(d).x[..2] != [d.hi, d.lo] || OD::from_qd(q).x[..4] != q.x || OD::from_qd(q).x[4..] != [0.0; 4] {
            fails += 1;
        }
        lanes.push((x, d, q, f));
    }
    for c in lanes.as_chunks::<8>().0 {
        let v = OV::from_lanes(&std::array::from_fn(|j| c[j].0));
        let (dv, qv, fv) = (v.to_dv(), v.to_qv(), v.to_v8().to_array());
        for j in 0..8 {
            let (dj, qj) = (dv.lane(j), qv.lane(j));
            if dj.hi.to_bits() != c[j].1.hi.to_bits()
                || dj.lo.to_bits() != c[j].1.lo.to_bits()
                || qj.x.map(f64::to_bits) != c[j].2.x.map(f64::to_bits)
                || fv[j].to_bits() != c[j].3.to_bits()
            {
                conv_mism += 1;
            }
        }
    }
    out.push(row("to_dd, to_qd, to_f64", "random", n, fails, conv_mism));

    // comparisons: the lexicographic order against the exact one, on pairs that differ in one part (often a low
    // one), scalar and lanes
    let (mut cmp_fails, mut cmp_mism) = (0, 0);
    let mut pairs = Vec::new();
    for _ in 0..n {
        let a = od_of(&rde(&mut r, -50, 50));
        let b = match r.int(0, 3) {
            0 => od_of(&rde(&mut r, -50, 50)),
            1 => a,
            _ => {
                let k = r.int(0, 7) as usize;
                let mut b = a;
                b.x[k] = f64::from_bits(if coin(&mut r) {
                    b.x[k].to_bits() + 1
                } else {
                    b.x[k].to_bits() - 1
                });
                OD::new(b.x)
            }
        };
        let want = a.exact().sub(&b.exact());
        let ord = if want.is_zero() {
            Ordering::Equal
        } else if want.neg {
            Ordering::Less
        } else {
            Ordering::Greater
        };
        if a.partial_cmp(&b) != Some(ord) {
            cmp_fails += 1;
        }
        pairs.push((a, b));
    }
    for c in pairs.as_chunks::<8>().0 {
        let va = OV::from_lanes(&std::array::from_fn(|j| c[j].0));
        let vb = OV::from_lanes(&std::array::from_fn(|j| c[j].1));
        let m = [va.lt(vb), va.le(vb), va.gt(vb), va.ge(vb), va.eq(vb)].map(|m| m.bits());
        for j in 0..8 {
            let (a, b) = c[j];
            let want = [a < b, a <= b, a > b, a >= b, a == b];
            for (mk, w) in m.iter().zip(want) {
                if (mk >> j & 1 == 1) != w {
                    cmp_mism += 1;
                }
            }
        }
    }
    out.push(row("compare", "exact order", n, cmp_fails, cmp_mism));
    out
}

/// print the rows of the decimal, conversion and order checks
fn print_rows(title: &str, rs: &[Res]) {
    println!("{title}");
    for r in rs {
        println!(
            "  {} ({}): {} of {} fail, {} lane mismatches",
            r.what, r.cat, r.fails, r.n, r.mism
        );
    }
}

// -------------------------------------------------------------------------------------------------------------
// the tests
// -------------------------------------------------------------------------------------------------------------

/// constants, decimal digits of pi, powers, special values, the power-of-two scaling and the conversions
#[test]
fn od_constants_and_special_values() {
    assert_eq!(OD::EPS, 2f64.powi(-418));
    assert_eq!(
        format!("{:.124}", OD::ONE / OD::from_f64(3.0)),
        format!("3.{}e-01", "3".repeat(124))
    );
    // 140 digits of pi parse to parts whose first four are QD's pi (both greedy), and print back
    let pi = "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798\
              21480865132823066470938446095505822317253594";
    let p = OD::parse(pi).unwrap();
    assert_eq!(&p.x[..4], &QD::PI.x);
    assert_eq!(p.to_sci(126), format!("{}e+00", &pi[..127]));
    assert_eq!(OD::from_f64(-0.0).to_sci(3), "-0.00e+00");
    assert_eq!(OD::parse("1e400").unwrap().x[0], f64::INFINITY);
    assert!(same(OD::parse("-0").unwrap(), OD::from_f64(-0.0)));
    // powi and recip against repeated products, sqrt against its square
    let x = OD::parse("1.0000001234567890123456789").unwrap();
    let q = x * x * x * x * x;
    assert!((x.powi(5) - q).abs().to_f64() < 1e-123, "{}", (x.powi(5) - q).to_f64());
    assert!(((x.powi(-3) * x.powi(3)) - OD::ONE).abs().to_f64() < 1e-123);
    let two = OD::from_f64(2.0);
    assert!((two.sqrt() * two.sqrt() - two).abs().to_f64() < 1e-124);
    assert_eq!(two.recip().x, OD::from_f64(0.5).x);
    // zeros, negatives, infinities
    assert!(same(OD::ZERO.sqrt(), OD::ZERO));
    assert!((-two).sqrt().x.iter().any(|v| v.is_nan()));
    assert!((OD::from_f64(f64::INFINITY) + two).x[0].is_infinite());
    assert!(same(-OD::ZERO, OD::from_f64(-0.0)));
    assert!((-OD::ONE).x[1..].iter().all(|v| v.to_bits() == 0));
    // mul_pwr2 is exact and bitwise mul_f by the power of two
    let y = od_of(
        &Dy::from_f64(1.0)
            .add(&Dy::from_f64(1.0).ldexp(-300))
            .add(&Dy::from_f64(3.0).ldexp(-400)),
    );
    assert!(same(y.mul_pwr2(0.25), y.mul_f(0.25)));
    assert!(same(y.mul_pwr2(2f64.powi(-600)).mul_pwr2(2f64.powi(600)), y));
    // conversions back and forth
    let d = DD::new(1.0, 2f64.powi(-60));
    assert_eq!(OD::from_dd(d).to_dd(), d);
    assert_eq!(OD::from_qd(QD::PI).to_qd().x, QD::PI.x);
    assert_eq!(p.to_f64(), std::f64::consts::PI);
    // the parts of slices round trip
    let v = vec![p, x, y];
    let parts = crate::od::to_parts(&v);
    let back = crate::od::from_parts(std::array::from_fn(|k| &parts[k][..]));
    assert!(v.iter().zip(&back).all(|(a, b)| same(*a, *b)));
}

/// every operation in every input category against exact arithmetic, and the lanes against the scalar type
#[test]
fn od_accuracy() {
    let n = env_usize("OD_N", 2000) / 8 * 8;
    let seed = env_usize("OD_SEED", 1) as u64;
    let rs = run_ops(n, seed, 0);
    let digest = digest(&rs);
    if report() {
        print_table(
            &format!("octo-double accuracy (units of 2^-418; n per row; isa {})", V8::ISA),
            &rs,
        );
    }
    println!("od digest {digest:016x} (n {n}, seed {seed}, isa {})", V8::ISA);
    let bad: Vec<&Res> = rs.iter().filter(|r| !r.ok()).collect();
    assert!(bad.is_empty(), "{} rows fail: {:?}", bad.len(), bad);
    // the proven bound is checked on most inputs of the four operations it covers, not on a few
    for op in [Op::Add, Op::AddF, Op::Mul, Op::MulF] {
        let covered: usize = rs.iter().filter(|r| r.what == op_name(op)).map(|r| r.proven.n).sum();
        assert!(
            covered >= 5 * n,
            "{}: {covered} inputs within the proven bound's hypotheses",
            op_name(op)
        );
    }
    if (n, seed) == (2000, 1) {
        let gap = digest_cat(&rs, "gap");
        let ladder = digest_cat(&rs, "ladder");
        println!("od gap digest {gap:016x}, ladder digest {ladder:016x}");
        assert!(
            digest == OD_DIGEST && gap == GAP_DIGEST && ladder == LADDER_DIGEST,
            "the results' bits changed: digests {digest:#018x}, {gap:#018x}, {ladder:#018x}, pinned {:#018x}, {:#018x}, \
             {:#018x}",
            OD_DIGEST,
            GAP_DIGEST,
            LADDER_DIGEST
        );
    }
}

/// the digest of the results (`Res::digest`) of every row in `CATS`, in order
fn digest(rs: &[Res]) -> u64 {
    fold_digest(rs.iter().filter(|r| CATS.iter().any(|&c| cat_name(c) == r.cat)))
}

/// the digest of the results of one category's rows, in order
fn digest_cat(rs: &[Res], cat: &str) -> u64 {
    fold_digest(rs.iter().filter(|r| r.cat == cat))
}

fn fold_digest<'a>(rs: impl Iterator<Item = &'a Res>) -> u64 {
    rs.fold(0xcbf29ce484222325u64, |h, r| (h ^ r.digest).wrapping_mul(0x100000001b3))
}

/// The digest of `od_accuracy`'s results at its default size and seed (2000, 1), recorded with
/// 4.1.1 and again when `sqrt` began from the double-double square root: every operation's bits in
/// every category, pinned, and the lanes' with them, which must be the scalar ones. Every backend
/// gives these bits.
const OD_DIGEST: u64 = 0xfb89_5d1d_a057_c861;

/// The digest of the gap category's results at the same size and seed, recorded with 4.8.1, whose renormalization's
/// second pass by two-sums made them correct, and again when `sqrt` began from the double-double square root.
const GAP_DIGEST: u64 = 0xc9db_20d6_a82f_28af;

/// The digest of the ladder category's results at the same size and seed, recorded with 4.8.2 and again when `sqrt`
/// began from the double-double square root.
const LADDER_DIGEST: u64 = 0xb02b_b2f9_037d_22c4;

/// Control for the pinned digest: with the order-6 term a2 b3 of the product dropped
/// (`mutate::MUL_E23`), the results' digest is not the pinned one.
#[cfg(feature = "mutate")]
#[test]
fn od_digest_control() {
    assert!(digest(&run_ops(2000, 1, mutate::MUL_E23)) != OD_DIGEST);
}

/// the dot accumulators against exact sums, 1 to 64 terms
#[test]
fn od_accumulator() {
    let n = env_usize("OD_N", 2000) / 8 * 8;
    let seed = env_usize("OD_SEED", 1) as u64;
    let rs = run_acc(n / 4, seed, 0, false);
    if report() {
        print_table(
            "dot accumulator: |error| / sum |a_i b_i| (units of 2^-418); threshold the proven bound (acc_thr)",
            &rs,
        );
    }
    let bad: Vec<&Res> = rs.iter().filter(|r| !r.ok()).collect();
    assert!(bad.is_empty(), "{bad:?}");
}

/// Pass: od x dd (`OD::mul_dd`, `OV::mul_dv`), od + dd (`add_dd`, `add_dv`) and the accumulators' od x dd
/// (`OdAcc::add_prod_dd`, `ODotAcc::add_prod_dv` and `add_prod_dv_mut`) are bit for bit the operations with the
/// operand promoted to octo-double, levels included, on every input category (OD_N per row) and on the edge pairs,
/// whose zeros' signs, infinities and NaNs must match too (`bits_eq`). Control: `od_dd_control`.
#[test]
fn od_dd_operand_is_the_promoted_operation() {
    let n = env_usize("OD_N", 2000) / 8 * 8;
    let seed = env_usize("OD_SEED", 1) as u64;
    let rs = run_dd(n, seed, 0);
    if report() {
        print_rows(
            "a double-double operand: results differing from the promoted operation (fails)",
            &rs,
        );
    }
    let bad: Vec<&Res> = rs.iter().filter(|r| !r.ok()).collect();
    assert!(bad.is_empty(), "{bad:?}");
    let (bad, count) = dd_edge_mismatches();
    println!("edge pairs: {count} comparisons, {} mismatches", bad.len());
    assert!(
        bad.is_empty(),
        "{} of {count}: {:?}",
        bad.len(),
        &bad[..bad.len().min(8)]
    );
}

/// Control for `od_dd_operand_is_the_promoted_operation`: with the second level of od x dd and od + dd one ulp up
/// (`mutate::DD_ULP`; a subnormal or zero level stays), every operation fails in every input category, and the edge
/// pairs fail.
#[cfg(feature = "mutate")]
#[test]
fn od_dd_control() {
    let rs = run_dd(400, 3, mutate::DD_ULP);
    for r in &rs {
        println!("{:<9} {:<10} {} of {} results differ", r.what, r.cat, r.fails, 2 * r.n);
    }
    let quiet: Vec<String> = rs
        .iter()
        .filter(|r| r.fails == 0)
        .map(|r| format!("{}/{}", r.what, r.cat))
        .collect();
    assert!(quiet.is_empty(), "rows where no result changed: {quiet:?}");
    set_mutation(mutate::DD_ULP);
    let (bad, count) = dd_edge_mismatches();
    set_mutation(0);
    println!("edge pairs under the control: {} of {count} differ", bad.len());
    assert!(!bad.is_empty());
}

/// The sloppy multiply (`OD::sloppy_mul`, `OV::sloppy_mul`) on the multiply's inputs of every category, scalar and
/// lanes: pass when every result is within `SLOPPY_THR` (4 units, fixed before the first run), every input within
/// `proven`'s hypotheses within the proven bound `SLOPPY_C` u^8 |a_0 b_0|, the bound checked on most inputs, and the
/// lanes bitwise the scalar results. OD_REPORT=1 prints the table and the default `*`'s worst error beside it.
#[test]
fn od_sloppy_mul() {
    let n = env_usize("OD_N", 2000) / 8 * 8;
    let seed = env_usize("OD_SEED", 1) as u64;
    let rs = run_sloppy(n, seed, 0);
    if report() {
        print_table(
            &format!(
                "sloppy multiply (units of 2^-418 relative to |ab|; proven max in u^8 |a_0 b_0|; isa {})",
                V8::ISA
            ),
            &rs,
        );
        let def = run_ops(n, seed, 0);
        for r in &rs {
            let d = def
                .iter()
                .find(|d| d.what == op_name(Op::Mul) && d.cat == r.cat)
                .unwrap();
            println!(
                "  {:<10} sloppy max {:>10} p99.9 {:>10}   default max {:>10} p99.9 {:>10}",
                r.cat,
                fmt_u(r.max),
                fmt_u(r.p999),
                fmt_u(d.max),
                fmt_u(d.p999)
            );
        }
    }
    let bad: Vec<&Res> = rs.iter().filter(|r| !r.ok()).collect();
    assert!(bad.is_empty(), "{} rows fail: {:?}", bad.len(), bad);
    let covered: usize = rs.iter().map(|r| r.proven.n).sum();
    assert!(
        covered >= 5 * n,
        "{covered} inputs within the proven bound's hypotheses"
    );
}

/// Dot products of sloppy products (`ODotAcc::add_prod_sloppy`, `OdAcc::add_prod_sloppy`), 1 to 64 terms, against
/// exact sums: pass when every sum is within `SLOPPY_THR` of W = sum |a_i b_i| and within the proven bound (|Z| +
/// `SLOPPY_DOT_C` W) u^8, and the lanes bitwise the scalar sums.
#[test]
fn od_sloppy_dot() {
    let n = env_usize("OD_N", 2000) / 8 * 8;
    let seed = env_usize("OD_SEED", 1) as u64;
    let rs = run_acc(n / 4, seed, 0, true);
    if report() {
        print_table(
            "sloppy dot: |error| / sum |a_i b_i| (units of 2^-418); proven max in units of 2^-418 of W",
            &rs,
        );
        let def = run_acc(n / 4, seed, 0, false);
        for (r, d) in rs.iter().zip(&def) {
            println!(
                "  {:<14} {:<7} sloppy max {:>10}   default max {:>10}",
                d.what,
                r.cat,
                fmt_u(r.max),
                fmt_u(d.max)
            );
        }
    }
    let bad: Vec<&Res> = rs.iter().filter(|r| !r.ok()).collect();
    assert!(bad.is_empty(), "{bad:?}");
}

/// The sloppy multiply's worst cases: the default's worst-case product (`od_worst_case_inputs`), the product of the
/// 4.8.1 gap case with its reciprocal (`gap_cases`), the all-positive top of the ladder (every part a whole ulp of the
/// one above), and a hill climb from the ladder category's inputs (OD_CLIMB starts of 300 steps, default 8). Pass:
/// every error within the proven bound, scalar and lanes alike. The worst is printed, in u^8 |a_0 b_0|.
#[test]
fn od_sloppy_worst_case() {
    let u = 2f64.powi(-53);
    let mut cases: Vec<(String, OD, OD)> = Vec::new();
    let mut a7 = [0.0; 8];
    for (i, p) in a7.iter_mut().enumerate().take(7) {
        *p = u.powi(i as i32);
    }
    let mut b7 = [0.0; 8];
    b7[0] = 1.0;
    b7[7] = u.powi(7);
    cases.push(("default's worst case".into(), OD { x: a7 }, OD { x: b7 }));
    let (_, _, g, _) = gap_cases()[1];
    cases.push(("gap a (1/a)".into(), g, OD::ONE / g));
    let mut top = [0.0; 8];
    top[0] = 1.0;
    for i in 1..8 {
        top[i] = ulp(top[i - 1]).to_f64();
    }
    cases.push(("top of the ladder squared".into(), OD { x: top }, OD { x: top }));
    let mut top2 = top;
    top2[0] = 1.0 + 2.0 * f64::EPSILON;
    cases.push(("top of the ladder, a0 = 1 + 2ulp".into(), OD { x: top2 }, OD { x: top }));
    let starts = env_usize("OD_CLIMB", 8);
    let (ca, cb, ce) = sloppy_climb(0x5109, starts, 300);
    cases.push((format!("hill climb ({starts} x 300 steps)"), ca, cb));
    let mut worst = 0.0f64;
    for (name, a, b) in &cases {
        assert!(proven(Op::Mul, *a, *b), "{name}: operands outside the hypotheses");
        let rv = sloppy_lanes(&[(*a, *b)]);
        assert!(
            same(a.sloppy_mul(*b), rv[0]),
            "{name}: lanes differ from the scalar result"
        );
        let e = sloppy_err(*a, *b);
        let d = {
            let x = a.exact().mul(&b.exact());
            let m = Dy::from_f64(a.x[0]).mul(&Dy::from_f64(b.x[0])).abs();
            (*a * *b).exact().sub(&x).abs().ratio(&m) / u8()
        };
        println!("{name:<34} sloppy {e:>12.4} u^8 |a0 b0|   default {d:.4}");
        worst = worst.max(e);
        assert!(
            e <= SLOPPY_C,
            "{name}: {e} u^8 |a0 b0|, past the proven bound {SLOPPY_C}"
        );
    }
    println!(
        "worst {worst:.4} u^8 |a0 b0| = 2^{:.2}, bound {SLOPPY_C:.4e} = 2^{:.2}; climb's {ce:.4}",
        worst.log2(),
        SLOPPY_C.log2()
    );
    println!(
        "climb's pair: a {:?}\n              b {:?}\n  sloppy_mul    {:?}",
        ca.x.map(f64::to_bits),
        cb.x.map(f64::to_bits),
        ca.sloppy_mul(cb).x.map(f64::to_bits)
    );
}

/// Control for the sloppy multiply's checks: with the carries out of level 6 dropped (`mutate::SLOPPY_C6`, the
/// truncation one level too high, an error of about u^7 |ab| whenever one of them is nonzero), the proven bound and
/// the threshold each fail more than half of the ladder row's samples and of the sloppy dot's random row of length 4,
/// and the hill climb's worst exceeds the bound. A single carry out of level 6 is zero on most ladder inputs (their
/// parts lie on one grid of bits, so most additions at level 6 are exact), so one carry dropped is not a reliable
/// control: in the paper's emulator the carry of a1 b5's deposit fired on 18 of 60 such inputs, all of them on 51.
#[cfg(feature = "mutate")]
#[test]
fn od_sloppy_control() {
    let n = env_usize("OD_N", 400) / 8 * 8;
    let rs = run_sloppy(n, 7, mutate::SLOPPY_C6);
    let row = rs.iter().find(|r| r.cat == "ladder").unwrap();
    println!(
        "sloppy mul with the carries out of level 6 dropped, ladder inputs: {} of {n} past {SLOPPY_THR} units, \
         {} of {} past the proven bound, worst {:.3e} u^8 |a0 b0|",
        row.fails, row.proven.fails, row.proven.n, row.proven.max
    );
    assert!(row.fails * 2 > n && row.proven.fails * 2 > row.proven.n, "{row:?}");
    let ds = run_acc(n / 4, 7, mutate::SLOPPY_C6, true);
    let d = ds
        .iter()
        .find(|r| r.what == "sloppy dot n=4" && r.cat == "random")
        .unwrap();
    println!(
        "sloppy dot n=4 with those carries dropped: {} of {} past the threshold, {} past the bound",
        d.fails, d.n, d.proven.fails
    );
    assert!(d.fails * 2 > d.n && d.proven.fails * 2 > d.proven.n, "{d:?}");
    set_mutation(mutate::SLOPPY_C6);
    let (_, _, e) = sloppy_climb(0x5109, 1, 50);
    set_mutation(0);
    println!("hill climb with those carries dropped: {e:.3e} u^8 |a0 b0|");
    assert!(e > SLOPPY_C, "{e}");
}

/// Timing of the sloppy multiply and dot term against the default ones on the native lanes, alternating in one run:
/// OD_REPS repetitions (default 15) of 4096 dependent products and of a 4096-term dot product each, with eight-part
/// operands. Prints the time per lane operation (8 products) of each, min, median and max, and the ratio's.
/// `cargo test --release --lib od_sloppy_timing -- --ignored --nocapture`
#[test]
#[ignore]
fn od_sloppy_timing() {
    use std::hint::black_box;
    use std::time::Instant;
    let reps = env_usize("OD_REPS", 15);
    let mut r = Rng::new(0x71e);
    let len = 4096;
    let v: Vec<OV> = (0..len)
        .map(|_| OV::from_lanes(&std::array::from_fn(|_| od_of(&rde(&mut r, -2, 2)))))
        .collect();
    let w: Vec<OV> = (0..len)
        .map(|_| {
            OV::from_lanes(&std::array::from_fn(|_| {
                od_of(&Dy::from_f64(1.0).add(&rand_dy(&mut r, -20)))
            }))
        })
        .collect();
    let time = |f: &dyn Fn() -> OV| {
        let t = Instant::now();
        black_box(f());
        t.elapsed().as_secs_f64() * 1e9 / len as f64
    };
    let mul_d = || {
        let mut x = v[0];
        for y in &w {
            x = black_box(x * *y);
        }
        x
    };
    let mul_s = || {
        let mut x = v[0];
        for y in &w {
            x = black_box(x.sloppy_mul(*y));
        }
        x
    };
    let dot_d = || {
        let mut a = ODotAcc::zero();
        for (x, y) in v.iter().zip(&w) {
            a.add_prod_mut(x, y);
        }
        a.finish()
    };
    let dot_s = || {
        let mut a = ODotAcc::zero();
        for (x, y) in v.iter().zip(&w) {
            a.add_prod_sloppy_mut(x, y);
        }
        a.finish()
    };
    let mut t = vec![Vec::new(); 4];
    for _ in 0..reps {
        t[0].push(time(&mul_d));
        t[1].push(time(&mul_s));
        t[2].push(time(&dot_d));
        t[3].push(time(&dot_s));
    }
    let stats = |x: &[f64]| {
        let mut y = x.to_vec();
        y.sort_by(|a, b| a.partial_cmp(b).unwrap());
        (y[0], y[y.len() / 2], y[y.len() - 1])
    };
    println!(
        "ns per lane operation (8 products), {reps} repetitions, isa {}: min / median / max",
        V8::ISA
    );
    for (k, name) in ["mul", "sloppy mul", "dot term", "sloppy dot term"].iter().enumerate() {
        let (a, m, b) = stats(&t[k]);
        println!("  {name:<16} {a:8.1} {m:8.1} {b:8.1}");
    }
    for (k, name) in [(0, "mul"), (2, "dot term")] {
        let q: Vec<f64> = t[k].iter().zip(&t[k + 1]).map(|(d, s)| d / s).collect();
        let (a, m, b) = stats(&q);
        println!("  speedup {name:<8} {a:.3} {m:.3} {b:.3} (per repetition, default / sloppy)");
    }
}

/// the largest |x_(i+1)| / ulp(x_i) over x's nonzero parts and their successors, infinite when a nonzero part follows a
/// zero one, zero for a non-finite value: at most 1/2 for parts that do not overlap, at most 1 for ulp-nonoverlapping
/// ones
fn overlap(x: OD) -> f64 {
    if x.x.iter().any(|p| !p.is_finite()) {
        return 0.0;
    }
    let mut w = 0.0f64;
    for i in 0..7 {
        if x.x[i] == 0.0 {
            return if x.x[i + 1..].iter().all(|&p| p == 0.0) {
                w
            } else {
                f64::INFINITY
            };
        }
        w = w.max(Dy::from_f64(x.x[i + 1]).abs().ratio(&ulp(x.x[i])));
    }
    w
}

/// Pass, set after a first run against `od`'s earlier statement (every part at most half the ulp of the one above it
/// for every operation), which the adds miss after cancellation: on `od_accuracy`'s inputs of the categories in `CATS`
/// (OD_N per operation and category, scalar and lanes), every finite result of the multiply, od x f64, the divide and
/// the square root has |x_(i+1)| <= ulp(x_i)/2, and of the adds (od + od, od + f64) |x_(i+1)| <= ulp(x_i), with no
/// nonzero part after a zero one. Controls: a value whose second part is twice the first's ulp fails both, one whose
/// second part is a whole ulp fails the half-ulp bound only, and one with a nonzero part after a zero one fails both.
#[test]
fn od_results_do_not_overlap() {
    let n = env_usize("OD_N", 2000) / 8 * 8;
    let seed = env_usize("OD_SEED", 1) as u64;
    let mut bad = Vec::new();
    for op in OPS {
        let bound = if op_cray(op) { 1.0 } else { 0.5 };
        let (mut worst, mut past_half, mut count) = (0.0f64, 0usize, 0usize);
        for cat in CATS {
            let (rs, rv) = results(op, &inputs(op, cat, n, seed));
            for x in rs.iter().chain(&rv) {
                let w = overlap(*x);
                count += 1;
                past_half += (w > 0.5) as usize;
                worst = worst.max(w);
            }
        }
        println!(
            "{}: worst |x_(i+1)| / ulp(x_i) {worst:.4}, {past_half} of {count} past 1/2 (bound {bound})",
            op_name(op)
        );
        if worst > bound {
            bad.push((op_name(op), worst));
        }
    }
    let part = |x1: f64, x2: f64| {
        let mut x = [0.0; 8];
        (x[0], x[1], x[2]) = (1.0, x1, x2);
        overlap(OD { x })
    };
    let controls = [part(2.0 * f64::EPSILON, 0.0), part(f64::EPSILON, 0.0), part(0.0, 1e-40)];
    println!("controls (twice the ulp, a whole ulp, a gap): {controls:?}");
    assert!(bad.is_empty(), "results past their bound: {bad:?}");
    assert!(
        controls[0] > 1.0 && controls[1] > 0.5 && controls[1] <= 1.0 && controls[2] > 1.0,
        "{controls:?}"
    );
}

/// Inputs on which the add, od + f64, mul, od x f64 and the divide reach their proven bounds, u^8 times the measure to
/// within a factor 1 - O(u). The products and quotients come out as (1, u, ..., u^7) up to sign, and the order-8 term
/// that the renormalization drops is the error. For od x f64, 3 g = 2^53 + 1 makes a_0 b = 1 + u. Pass: each
/// measure between 0.99 u^8 and its bound, scalar and lanes alike, and the four operations' operands within
/// `proven`'s hypotheses.
#[test]
fn od_worst_case_inputs() {
    let u = 2f64.powi(-53);
    let ladder_to = |k: usize| -> OD {
        let mut x = [0.0; 8];
        for (i, p) in x.iter_mut().enumerate().take(k) {
            *p = u.powi(i as i32);
        }
        OD { x }
    };
    let mut b_mul = [0.0; 8];
    b_mul[0] = 1.0;
    b_mul[7] = u.powi(7);
    let mut c = [0.0; 8];
    c[0] = u.powi(8);
    let mut a_mulf = [0.0; 8];
    for i in [0, 2, 4, 6] {
        a_mulf[i] = 1.5 * u.powi(i as i32);
    }
    a_mulf[7] = 1.5 * u.powi(8);
    let g = 3002399751580331.0 * 2f64.powi(-52);
    let mut d = [0.0; 8];
    d[0] = -1.0;
    d[1] = u;
    // the divide's runs are within the hypotheses too: its remainders are (u^k, 0, ...) and its quotient terms -u^k,
    // so every product is exact; its bound is u^8 (1 + 2^-25) relative
    let div_thr = (1.0 + 2f64.powi(-25)) / 64.0;
    let cases = [
        (Op::Mul, ladder_to(7), OD { x: b_mul }, PROVEN_THR),
        (Op::Add, ladder_to(8), OD { x: c }, PROVEN_THR),
        (Op::AddF, ladder_to(8), OD::from_f64(u.powi(8)), PROVEN_THR),
        (Op::MulF, OD { x: a_mulf }, OD::from_f64(g), PROVEN_THR),
        (Op::Div, OD::ONE, OD { x: d }, div_thr),
    ];
    assert_eq!(3 * 3002399751580331u64, (1u64 << 53) + 1);
    for (op, a, b, thr) in cases {
        assert!(
            op == Op::Div || proven(op, a, b),
            "{}: operands outside the hypotheses",
            op_name(op)
        );
        let (rs, rv) = results(op, &[(a, b)]);
        assert!(
            same(rs[0], rv[0]),
            "{}: lanes differ from the scalar result",
            op_name(op)
        );
        let (err, scale, _, _) = exact_err(op, a, b, rs[0], false);
        let m = err.ratio(&scale) / unit();
        println!("{:<9} error {m:.10} units of 2^-418, bound {thr:.10}", op_name(op));
        assert!(
            within(&err, &scale, thr, &Dy::zero()),
            "{}: {m} units, past the proven bound",
            op_name(op)
        );
        assert!(m >= 0.99 / 64.0, "{}: {m} units, below 0.99 u^8", op_name(op));
    }
}

/// The parts of a finite OD from their bits
fn od_bits(b: [u64; 8]) -> OD {
    OD {
        x: b.map(f64::from_bits),
    }
}

/// An input on which the square root errs by u^8 |sqrt x| to within a factor 1 - 5u (the paper's Table V,
/// paper/table1_inputs.py): x = (1 + 2u, x_1, ..., x_7), its parts chosen one at a time so that the levels of the last
/// product, r x, are 1, u, ..., u^7 with a ninth just below u^8, which the renormalization drops, so that the result is
/// (1, u, ..., u^7). Pass: that result, scalar and lanes alike, x within `proven`'s hypotheses, and the check, the
/// measure |z^2 - x| / (2x) between 0.99 u^8 and the proven bound. Controls: the check rejects a wrong result past the
/// bound (the last part 16 u^8 up) and one far more accurate (the greedy eight parts of the sum of the result's parts
/// and u^8), and x with its leading part one ulp up measures below 0.99 u^8.
#[test]
fn od_sqrt_worst_case_input() {
    let u = 2f64.powi(-53);
    let x = od_bits([
        0x3ff0000000000001,
        0x3967fdf3b645a1cb,
        0x38b0624dd2f1af00,
        0x35f0000000000004,
        0x32b0000000000003,
        0x2c3bff7ced916873,
        0x2b60624dd2f1be00,
        0x28b0000000000009,
    ]);
    // the measure of z as sqrt(x) in units of 2^-418, and whether the check passes it
    let check = |x: OD, z: OD| -> (f64, bool) {
        let (err, scale, _, _) = exact_err(Op::Sqrt, x, OD::ONE, z, false);
        let m = err.ratio(&scale) / unit();
        (
            m,
            m >= 0.99 / 64.0 && within(&err, &scale, SQRT_PROVEN_THR, &Dy::zero()),
        )
    };
    assert!(proven(Op::Sqrt, x, OD::ONE), "sqrt: input outside the hypotheses");
    let (rs, rv) = results(Op::Sqrt, &[(x, OD::ONE)]);
    let z = rs[0];
    let ladder: [f64; 8] = std::array::from_fn(|k| u.powi(k as i32));
    assert_eq!(z.x, ladder, "sqrt: the result is not (1, u, ..., u^7)");
    assert!(same(z, rv[0]), "sqrt: lanes differ from the scalar result");
    let (m, ok) = check(x, z);
    println!(
        "sqrt error {m:.10} units of 2^-418, (m - 1/64) / (u/64) = {:.3}",
        (m * 64.0 - 1.0) / u
    );
    assert!(ok, "sqrt: {m} units, outside [0.99 u^8, the proven bound]");
    let mut hi = z;
    hi.x[7] += 16.0 * u.powi(8);
    let lo = od_of(&z.exact().add(&Dy::from_f64(u.powi(8))));
    let (mh, okh) = check(x, hi);
    let (ml, okl) = check(x, lo);
    let mut y = x;
    y.x[0] = y.x[0].next_up();
    let my = check(y, y.sqrt()).0;
    println!("controls: {mh:.3e} and {ml:.3e} units for the wrong results, {my:.3e} for x one ulp up");
    assert!(!okh && !okl, "control: the check passes a wrong result");
    assert!(
        my < 0.99 / 64.0,
        "control: {my} units, the input one ulp up still reaches 0.99 u^8"
    );
}

/// The proven bound catches what the 1-unit thresholds miss: with the products of order 8 dropped, an error of a few
/// u^8 |ab| on the ladder category's inputs. Pass, set after a first run (2 of 400 past 1 unit, 348 past the
/// proven bound): the proven bound fails more than half of the ladder row's samples and more than ten times as many as
/// the thresholds.
#[cfg(feature = "mutate")]
#[test]
fn od_proven_bound_control() {
    let n = env_usize("OD_N", 400) / 8 * 8;
    let rs = run_ops(n, 7, mutate::MUL_L8);
    let row = rs
        .iter()
        .find(|r| r.what == op_name(Op::Mul) && r.cat == "ladder")
        .unwrap();
    println!(
        "mul without order 8, ladder inputs: {} of {n} past 1 unit, {} past the proven bound, worst {:.4} units",
        row.fails, row.proven.fails, row.proven.max
    );
    assert!(row.proven.fails * 2 > n && row.proven.fails > 10 * row.fails, "{row:?}");
}

/// The square root's proven bound can fail: with its second Newton step skipped (`mutate::SQRT_ITER`), more than half
/// of the ladder row's samples, all within its hypotheses, miss it.
#[cfg(feature = "mutate")]
#[test]
fn od_sqrt_proven_bound_control() {
    let n = env_usize("OD_N", 400) / 8 * 8;
    let rs = run_ops(n, 7, mutate::SQRT_ITER);
    let row = rs
        .iter()
        .find(|r| r.what == op_name(Op::Sqrt) && r.cat == "ladder")
        .unwrap();
    println!(
        "sqrt without its second step, ladder inputs: {} of {} within the hypotheses past the proven bound",
        row.proven.fails, row.proven.n
    );
    assert!(row.proven.n * 2 > n && row.proven.fails * 2 > row.proven.n, "{row:?}");
}

/// Inputs whose results miss their bounds by 1e10 to 1e28 units when the renormalization's second pass is by
/// quick-two-sums: the square root of an eight-part value with gaps and zero parts, the product of a two-part value
/// with a gap and its reciprocal, and two quotients of values with zero parts. Pass: each within its operation's
/// threshold, scalar and lanes alike.
#[test]
fn od_gap_regressions() {
    let bad: Vec<(&str, f64)> = gap_cases()
        .iter()
        .filter_map(|&(name, op, a, b)| {
            let (r, rv) = results(op, &[(a, b)]);
            let (err, scale, _, allow) = exact_err(op, a, b, r[0], false);
            let ok = within(&err, &scale, op_thr(op), &allow) && same(r[0], rv[0]);
            (!ok).then(|| (name, err.ratio(&scale) / unit()))
        })
        .collect();
    assert!(bad.is_empty(), "{bad:?}");
}

/// Control: with the second pass by quick-two-sums (`mutate::RENORM_FAST`), every case misses its bound.
#[cfg(feature = "mutate")]
#[test]
fn od_gap_regressions_control() {
    for (name, op, a, b) in gap_cases() {
        set_mutation(mutate::RENORM_FAST);
        let r = apply_s(op, a, b);
        set_mutation(0);
        let (err, scale, _, allow) = exact_err(op, a, b, r, false);
        assert!(!within(&err, &scale, op_thr(op), &allow), "{name} within its bound");
    }
}

/// eight to nine values ±(1 + f)·2^e, e in -300..=0, in random magnitude order; half the time one value nearly cancels
/// another, as the levels of a·(1/a) do
fn unordered(r: &mut Rng) -> [f64; 9] {
    let mut c: [f64; 9] = std::array::from_fn(|_| rde(r, -300, 0).to_f64());
    if coin(r) {
        let (i, j) = (r.int(0, 8) as usize, r.int(0, 8) as usize);
        if i != j {
            c[j] = -c[i] * (1.0 + f64::EPSILON * r.int(1, 1 << 20) as f64);
        }
    }
    c
}

/// 4.8.0's renormalization (second pass by quick-two-sums), and whether its every quick-two-sum was exact
fn renorm_fast_pass(c: [f64; 9]) -> ([f64; 8], bool) {
    let c = crate::od::vecsum(c);
    let (mut out, mut k, mut acc, mut exact) = ([0.0f64; 8], 0, c[0], true);
    for &ci in &c[1..] {
        let (t, e) = crate::od::quick_two_sum(acc, ci);
        exact &= (t, e) == crate::od::two_sum(acc, ci);
        if k < 7 && e != 0.0 {
            out[k] = t;
            k += 1;
            acc = e;
        } else {
            acc = t;
        }
    }
    out[k] = acc;
    (out, exact)
}

fn exact_sum(c: &[f64]) -> Dy {
    c.iter().fold(Dy::zero(), |s, &x| s.add(&Dy::from_f64(x)))
}

/// Pass: eight values in any magnitude order renormalize with their exact sum kept (seven emissions at most, so no
/// part is truncated, and every two-sum is exact).
#[test]
fn renorm_of_eight_keeps_the_exact_sum_in_any_order() {
    let mut r = Rng::new(0x5e7e);
    let lost = (0..20000)
        .filter(|_| {
            let c: [f64; 8] = unordered(&mut r)[..8].try_into().unwrap();
            !exact_sum(&crate::od::renorm_scalar(c)).sub(&exact_sum(&c)).is_zero()
        })
        .count();
    assert_eq!(lost, 0, "{lost} of 20000 renormalizations lost part of their sum");
}

/// Control: with the second pass by quick-two-sums (`mutate::RENORM_FAST`), some of the same renormalizations lose
/// part of their sum.
#[cfg(feature = "mutate")]
#[test]
fn renorm_of_eight_keeps_the_exact_sum_control() {
    let mut r = Rng::new(0x5e7e);
    set_mutation(mutate::RENORM_FAST);
    let lost = (0..20000)
        .filter(|_| {
            let c: [f64; 8] = unordered(&mut r)[..8].try_into().unwrap();
            !exact_sum(&crate::od::renorm_scalar(c)).sub(&exact_sum(&c)).is_zero()
        })
        .count();
    set_mutation(0);
    assert!(lost > 0, "the quick-two-sum pass kept every sum");
}

/// Pass: wherever every quick-two-sum of 4.8.0's second pass was exact, the two-sum pass gives its bits; the inputs,
/// half of them in magnitude order, include both kinds (at least 1000 of each in 20000).
#[test]
fn renorm_keeps_the_fast_pass_bits_where_it_was_exact() {
    let (exact, inexact, changed) = fast_pass_comparison(0);
    println!("renorm: {exact} with every quick-two-sum exact ({changed} changed), {inexact} with one inexact");
    assert_eq!(
        changed, 0,
        "{changed} results changed where every quick-two-sum was exact"
    );
    assert!(exact >= 1000 && inexact >= 1000, "{exact} exact, {inexact} inexact");
}

/// Control: with the renormalization's eighth part dropped (`mutate::RENORM_DROP`), results change where every
/// quick-two-sum was exact.
#[cfg(feature = "mutate")]
#[test]
fn renorm_keeps_the_fast_pass_bits_control() {
    assert!(fast_pass_comparison(mutate::RENORM_DROP).2 > 0);
}

/// over 20000 renormalizations, half in magnitude order (as the levels of a product of full values) and half in random
/// order: those whose every quick-two-sum in 4.8.0's pass was exact, those with one inexact, and among the first those
/// whose bits differ from 4.8.0's, with mutation `id` active in `renorm_scalar`
fn fast_pass_comparison(id: u32) -> (usize, usize, usize) {
    let mut r = Rng::new(0xb175);
    let (mut exact, mut inexact, mut changed) = (0, 0, 0);
    for i in 0..20000 {
        let c = if i % 2 == 0 {
            std::array::from_fn(|k| {
                let e = -53 * k as i64 - r.int(0, 3);
                rand_dy(&mut r, e).to_f64()
            })
        } else {
            unordered(&mut r)
        };
        let (old, was_exact) = renorm_fast_pass(c);
        set_mutation(id);
        let new = crate::od::renorm_scalar(c);
        set_mutation(0);
        if was_exact {
            exact += 1;
            changed += (old.map(f64::to_bits) != new.map(f64::to_bits)) as usize;
        } else {
            inexact += 1;
        }
    }
    (exact, inexact, changed)
}

/// the cases of `od_gap_regressions`
fn gap_cases() -> Vec<(&'static str, Op, OD, OD)> {
    let od = |h: [u64; 8]| OD {
        x: h.map(f64::from_bits),
    };
    let a = od([0x3ff97f22dc331771, 0x320e700ef097bbfd, 0, 0, 0, 0, 0, 0]);
    // gaps after its first, second and fourth parts, and zero parts
    let sq = od([
        0x3ff93b4a66f163b7,
        0x30a0f22c361cefdc,
        0,
        0x1e0c251714b46fe7,
        0,
        0x0bc7e97fd3b29241,
        0x885f41d74c3832f1,
        0x84f56713803b93f1,
    ]);
    vec![
        ("sqrt", Op::Sqrt, sq, OD::ONE),
        ("a (1/a)", Op::Mul, a, OD::ONE / a),
        (
            "quotient 1",
            Op::Div,
            od([
                0x3fdc0b223f8ec38c,
                0xbc6e614a71d2f866,
                0xb590b5eca31e706d,
                0x2ea39cd33f0fce0b,
                0x27bc6d352bcd0000,
                0,
                0,
                0,
            ]),
            od([
                0x3ff04936766afb19,
                0xbc65eb912e2cf5f6,
                0x2b655daf2bc93326,
                0xa7f828724fe44000,
                0,
                0,
                0,
                0,
            ]),
        ),
        (
            "quotient 2",
            Op::Div,
            od([
                0x40081779531a9783,
                0xb929a00ece965c74,
                0xb26660a9a05ed265,
                0x2efe880195dd03a0,
                0x281c44f426214000,
                0,
                0,
                0,
            ]),
            od([
                0x3fb9b4b7d1740212,
                0xbc49003abd38f755,
                0xab2ec2cfd3d8683b,
                0xa78f516e79640000,
                0,
                0,
                0,
                0,
            ]),
        ),
    ]
}

/// decimal input and output, the conversions and the order, exactly
#[test]
fn od_decimal_io_conversions_and_order() {
    let n = env_usize("OD_N", 2000);
    let seed = env_usize("OD_SEED", 1) as u64;
    let rs = run_io(n, seed, 0);
    if report() {
        print_rows("decimal input/output, conversions and comparisons", &rs);
    }
    let bad: Vec<&Res> = rs.iter().filter(|r| !r.ok()).collect();
    assert!(bad.is_empty(), "{bad:?}");
}

/// Every deliberate breakage (od::mutate) must make at least one check fail.
#[cfg(feature = "mutate")]
#[test]
fn od_controls() {
    let n = env_usize("OD_N", 400) / 8 * 8;
    let mut silent = Vec::new();
    for id in 1..=mutate::N {
        let mut rs = run_ops(n, 7, id);
        rs.extend(run_acc(n / 4, 7, id, false));
        rs.extend(run_sloppy(n, 7, id));
        rs.extend(run_acc(n / 4, 7, id, true));
        rs.extend(run_io(n / 2, 7, id));
        rs.extend(run_dd(n / 4, 7, id));
        let failed: Vec<&Res> = rs.iter().filter(|r| !r.ok()).collect();
        let worst = failed.iter().map(|r| r.max).fold(0.0, f64::max);
        let lanes: usize = failed.iter().map(|r| r.mism).sum();
        let rows: Vec<String> = failed.iter().take(6).map(|r| format!("{}/{}", r.what, r.cat)).collect();
        println!(
            "control {id:2} {:<52} fires: {:>3} rows fail, worst {} u, {} lane mismatches; e.g. {}",
            mutate::NAMES[id as usize],
            failed.len(),
            fmt_u(worst),
            lanes,
            rows.join(", ")
        );
        if failed.is_empty() {
            silent.push(id);
        }
    }
    assert!(silent.is_empty(), "controls that did not fire: {silent:?}");
}