mzstd 0.2.6

Zstandard (RFC 8878) compression and decompression in safe Rust; single-threaded encoding is byte-identical to zstd 1.5.7
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
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
//! The optimal parser: `btopt`, `btultra` and `btultra2` (`zstd_opt.c`).
//!
//! Prices are fixed-point bit costs (1/256 bit) derived from running symbol
//! statistics, exactly as the reference computes them, so the chosen parse
//! — and therefore every emitted byte — matches it.

use alloc::vec::Vec;

use fearless_simd::{Simd, dispatch};
use fearless_simd_macros::simd;

use super::entropy::{Tables, fse, hist, huf};
use super::ldm::RawSeqStore;
use super::match_state::{BlockInput, DictMatchState, MatchState};
use super::mem;
use super::opt_count;
use super::params::ParamSwitch;
use super::seq_store::{
    SeqStore, ll_code, ml_code, new_rep, offset_to_offbase, repcode_to_offbase,
};
use crate::shared::format::{
    LL_BITS, MAX_LIT, MAX_LL, MAX_ML, MAX_OFF, MIN_MATCH, ML_BITS, REP_NUM, highbit32,
};

/// `ZSTD_OPT_NUM`.
pub(crate) const OPT_NUM: usize = 1 << 12;
/// `ZSTD_OPT_SIZE`.
pub(crate) const OPT_SIZE: usize = OPT_NUM + 3;

const LITFREQ_ADD: u32 = 2;
/// Literal runs from this length on refresh the whole literal weight table
/// ([`weights`]) rather than each literal's entry as it is counted.
const BULK_WEIGHT_LITERALS: usize = 64;
const MAX_PRICE: i32 = 1 << 30;
const PREDEF_THRESHOLD: usize = 8;
const BITCOST_ACCURACY: u32 = 8;
const BITCOST_MULTIPLIER: u32 = 1 << BITCOST_ACCURACY;
/// `prime4bytes`, `prime5bytes` and `prime6bytes` of `ZSTD_hashPtr`.
const PRIME_4_BYTES: u32 = 2_654_435_761;
const PRIME_5_BYTES: u64 = 889_523_592_379;
const PRIME_6_BYTES: u64 = 227_718_039_650_203;
/// `prime7bytes`: dictionary loads hash with an unclamped minimum match.
const PRIME_7_BYTES: u64 = 58_295_818_150_454_627;

/// How prices are derived (`ZSTD_OptPrice_e`).
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub(crate) enum PriceType {
    #[default]
    Dynamic,
    Predef,
}

/// One position of the forward pass (`ZSTD_optimal_t`).
#[derive(Copy, Clone, Debug, Default)]
struct Optimal {
    price: i32,
    off: u32,
    mlen: u32,
    litlen: u32,
    rep: [u32; REP_NUM],
}

/// One candidate match (`ZSTD_match_t`).
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct Match {
    pub(crate) off: u32,
    pub(crate) len: u32,
}

/// Pricing statistics of the optimal parser (`optState_t`).
#[derive(Clone, Debug, Default)]
pub(crate) struct OptState {
    pub(crate) lit_freq: Vec<u32>,
    pub(crate) lit_length_freq: Vec<u32>,
    pub(crate) match_length_freq: Vec<u32>,
    pub(crate) off_code_freq: Vec<u32>,
    match_table: Vec<Match>,
    price_table: Vec<Optimal>,
    pub(crate) lit_sum: u32,
    pub(crate) lit_length_sum: u32,
    pub(crate) match_length_sum: u32,
    pub(crate) off_code_sum: u32,
    pub(crate) lit_sum_base_price: u32,
    pub(crate) lit_length_sum_base_price: u32,
    pub(crate) match_length_sum_base_price: u32,
    pub(crate) off_code_sum_base_price: u32,
    pub(crate) price_type: PriceType,
    /// The frame's literal compression switch.
    pub(crate) literal_compression: ParamSwitch,
    /// The frequencies' weights, kept current with them.
    tables: PriceTables,
}

/// The symbol statistics' `WEIGHT`s, folded with each symbol's extra bits,
/// so that a dynamic price is one table read plus the sum's base price.
///
/// [`OptState::rescale_freqs`] rebuilds them for the block's optimization
/// level and [`OptState::update_stats`] patches the entries it changes,
/// so every price equals the reference's `ZSTD_litLengthPrice`,
/// `ZSTD_getMatchPrice` and `ZSTD_rawLiteralsCost`, computed from the
/// frequencies on each call. Sums wrap as the reference's `U32` sums do.
/// Each table is a power of two longer than any code its index function
/// returns, so a masked index needs no bounds check.
#[derive(Clone, Debug)]
struct PriceTables {
    /// `WEIGHT(litFreq[byte])`.
    literal: [u32; 256],
    /// `LL_bits[code] * BITCOST_MULTIPLIER - WEIGHT(litLengthFreq[code])`.
    lit_length: [u32; 64],
    /// `ML_bits[code] * BITCOST_MULTIPLIER - WEIGHT(matchLengthFreq[code])`
    /// plus the per-sequence surcharge.
    match_length: [u32; 128],
    /// `code * BITCOST_MULTIPLIER - WEIGHT(offCodeFreq[code])` plus the
    /// long-distance handicap below level 2.
    off_code: [u32; 32],
    /// The optimization level the weights were taken at.
    opt_level: u32,
}

impl Default for PriceTables {
    fn default() -> Self {
        Self {
            literal: [0; 256],
            lit_length: [0; 64],
            match_length: [0; 128],
            off_code: [0; 32],
            opt_level: 0,
        }
    }
}

impl PriceTables {
    /// The literal length entry for `code`.
    #[inline(always)]
    fn set_lit_length(&mut self, code: usize, freq: u32) {
        self.lit_length[code & 63] = (u32::from(LL_BITS[code]) * BITCOST_MULTIPLIER)
            .wrapping_sub(weight(freq, self.opt_level));
    }

    /// The match length entry for `code`.
    #[inline(always)]
    fn set_match_length(&mut self, code: usize, freq: u32) {
        self.match_length[code & 127] = (u32::from(ML_BITS[code]) * BITCOST_MULTIPLIER)
            .wrapping_sub(weight(freq, self.opt_level))
            .wrapping_add(BITCOST_MULTIPLIER / 5);
    }

    /// The offset code entry for `code`.
    #[inline(always)]
    fn set_off_code(&mut self, code: usize, freq: u32) {
        let code32 = code as u32;
        let mut price = (code32 * BITCOST_MULTIPLIER).wrapping_sub(weight(freq, self.opt_level));
        if self.opt_level < 2 && code32 >= 20 {
            price = price.wrapping_add((code32 - 19) * 2 * BITCOST_MULTIPLIER);
        }
        self.off_code[code & 31] = price;
    }
}

impl OptState {
    /// Sizes the frequency tables (`ZSTD_reset_matchState`'s opt space).
    ///
    /// The position and match tables are left to [`Self::reserve_matches`]
    /// and [`grow_positions`]: the reference carves them uninitialized
    /// from its workspace, and filling all [`OPT_SIZE`] entries here
    /// dominated frames that never reach the parser (empty input) or use a
    /// few of them (tiny or incompressible input).
    pub(crate) fn allocate(&mut self) {
        self.lit_freq.resize(MAX_LIT as usize + 1, 0);
        self.lit_length_freq.resize(MAX_LL as usize + 1, 0);
        self.match_length_freq.resize(MAX_ML as usize + 1, 0);
        self.off_code_freq.resize(MAX_OFF as usize + 1, 0);
    }

    /// Grows the match table to hold every match one search can report in
    /// a block of `block_len` bytes with `nb_compares` tree comparisons.
    ///
    /// A search reports matches of strictly increasing lengths: up to
    /// three repeat offsets or one 3-byte match, one per tree comparison,
    /// and one LDM candidate. Lengths lie within the block and the tree
    /// walk stops past [`OPT_NUM`], so the list is also shorter than
    /// `block_len + 3` and than [`OPT_SIZE`]. The table only grows, so each
    /// entry is written once per engine.
    fn reserve_matches(&mut self, block_len: usize, nb_compares: u32) {
        let len = OPT_SIZE.min(block_len + 3).min(nb_compares as usize + 5);
        if self.match_table.len() < len {
            self.match_table.resize(len, Match::default());
        }
    }

    /// Bytes held by the parser's position and match tables.
    pub(crate) fn table_bytes(&self) -> usize {
        self.price_table.capacity() * core::mem::size_of::<Optimal>()
            + self.match_table.capacity() * core::mem::size_of::<Match>()
    }

    fn compressed_literals(&self) -> bool {
        self.literal_compression != ParamSwitch::Disable
    }

    fn set_base_prices(&mut self, opt_level: u32) {
        if self.compressed_literals() {
            self.lit_sum_base_price = weight(self.lit_sum, opt_level);
        }
        self.lit_length_sum_base_price = weight(self.lit_length_sum, opt_level);
        self.match_length_sum_base_price = weight(self.match_length_sum, opt_level);
        self.off_code_sum_base_price = weight(self.off_code_sum, opt_level);
    }

    /// `ZSTD_rescaleFreqs`: the first block of a frame starts from the
    /// inherited entropy tables when a dictionary validated them
    /// (`symbol_costs`, `ms->opt.symbolCosts`), else from `src`'s literals
    /// and fixed sequence statistics; later blocks scale the running
    /// statistics down.
    fn rescale_freqs(&mut self, src: &[u8], opt_level: u32, symbol_costs: Option<&Tables>) {
        let compressed_literals = self.compressed_literals();
        self.price_type = PriceType::Dynamic;
        if self.lit_length_sum == 0 {
            if src.len() <= PREDEF_THRESHOLD {
                self.price_type = PriceType::Predef;
            }
            if let Some(tables) = symbol_costs
                && tables.huf.repeat == huf::Repeat::Valid
            {
                // Huffman statistics over every byte: tables presumed
                // generated by a dictionary.
                self.price_type = PriceType::Dynamic;
                self.seed_from_dictionary(tables);
            } else {
                self.seed_without_dictionary(src);
            }
        } else {
            if compressed_literals {
                self.lit_sum = scale_stats(&mut self.lit_freq, MAX_LIT, 12);
            }
            self.lit_length_sum = scale_stats(&mut self.lit_length_freq, MAX_LL, 11);
            self.match_length_sum = scale_stats(&mut self.match_length_freq, MAX_ML, 11);
            self.off_code_sum = scale_stats(&mut self.off_code_freq, MAX_OFF, 11);
        }
        self.set_base_prices(opt_level);
        self.rebuild_tables(opt_level);
    }

    /// `ZSTD_rescaleFreqs`'s first-block statistics from a dictionary's
    /// entropy tables: each symbol's frequency is `2^(scale - bits)` for
    /// the bits its code costs (literals scaled to 2K, sequence symbols to
    /// 1K), and `1` for a symbol without a code.
    ///
    /// The reference asserts that no code is longer than the scale; a
    /// malformed table past that shifts as its unchecked `1 << n` does on
    /// common targets (the count taken modulo 32) instead of failing.
    fn seed_from_dictionary(&mut self, tables: &Tables) {
        let frequency = |bit_cost: u32, scale_log: u32| {
            if bit_cost != 0 {
                1u32.wrapping_shl(scale_log.wrapping_sub(bit_cost))
            } else {
                1
            }
        };
        if self.compressed_literals() {
            let mut sum = 0u32;
            for (literal, freq) in (0..=MAX_LIT).zip(self.lit_freq.iter_mut()) {
                *freq = frequency(tables.huf.table.symbol_nb_bits(literal), 11);
                sum = sum.wrapping_add(*freq);
            }
            self.lit_sum = sum;
        }
        let seed = |freqs: &mut [u32], table: &fse::CTable, last: u32| {
            let mut sum = 0u32;
            for (symbol, freq) in (0..=last).zip(freqs.iter_mut()) {
                *freq = frequency(table.max_nb_bits(symbol), 10);
                sum = sum.wrapping_add(*freq);
            }
            sum
        };
        self.lit_length_sum = seed(&mut self.lit_length_freq, &tables.fse.litlength, MAX_LL);
        self.match_length_sum = seed(&mut self.match_length_freq, &tables.fse.matchlength, MAX_ML);
        self.off_code_sum = seed(&mut self.off_code_freq, &tables.fse.offcode, MAX_OFF);
    }

    /// `ZSTD_rescaleFreqs`'s first-block statistics without a dictionary:
    /// literal frequencies from `src`, fixed ones for the sequence symbols.
    fn seed_without_dictionary(&mut self, src: &[u8]) {
        const BASE_LL_FREQS: [u32; MAX_LL as usize + 1] = [
            4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1, 1, 1, 1, 1,
        ];
        const BASE_OFC_FREQS: [u32; MAX_OFF as usize + 1] = [
            6, 2, 1, 1, 2, 3, 4, 4, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 1,
        ];
        if self.compressed_literals() {
            let mut lit = MAX_LIT;
            let _ = hist::count(&mut self.lit_freq, &mut lit, src, false);
            self.lit_sum = downscale_stats(&mut self.lit_freq, MAX_LIT, 8, false);
        }
        self.lit_length_freq.copy_from_slice(&BASE_LL_FREQS);
        self.lit_length_sum = BASE_LL_FREQS.iter().sum();
        self.match_length_freq.fill(1);
        self.match_length_sum = MAX_ML + 1;
        self.off_code_freq.copy_from_slice(&BASE_OFC_FREQS);
        self.off_code_sum = BASE_OFC_FREQS.iter().sum();
    }

    /// Recomputes every [`PriceTables`] entry from the frequencies.
    ///
    /// Equal to calling each `set_*` in turn, but the weights of a whole
    /// table are taken at once ([`weights`]) and folded with straight-line
    /// loops the compiler vectorizes: a block of a few bytes pays for this
    /// rebuild on every call.
    fn rebuild_tables(&mut self, opt_level: u32) {
        let tables = &mut self.tables;
        tables.opt_level = opt_level;
        if self.literal_compression != ParamSwitch::Disable {
            weights(&self.lit_freq, opt_level, &mut tables.literal);
        }
        let mut weight_of = [0u32; 64];
        weights(&self.lit_length_freq, opt_level, &mut weight_of);
        for ((entry, &bits), &weight) in tables.lit_length.iter_mut().zip(&LL_BITS).zip(&weight_of)
        {
            *entry = (u32::from(bits) * BITCOST_MULTIPLIER).wrapping_sub(weight);
        }
        weights(&self.match_length_freq, opt_level, &mut weight_of);
        for ((entry, &bits), &weight) in
            tables.match_length.iter_mut().zip(&ML_BITS).zip(&weight_of)
        {
            *entry = (u32::from(bits) * BITCOST_MULTIPLIER)
                .wrapping_sub(weight)
                .wrapping_add(BITCOST_MULTIPLIER / 5);
        }
        weights(&self.off_code_freq, opt_level, &mut weight_of);
        for (code, (entry, &weight)) in tables.off_code.iter_mut().zip(&weight_of).enumerate() {
            let code = code as u32;
            let handicap = if opt_level < 2 && code >= 20 {
                (code - 19) * 2 * BITCOST_MULTIPLIER
            } else {
                0
            };
            *entry = (code * BITCOST_MULTIPLIER)
                .wrapping_sub(weight)
                .wrapping_add(handicap);
        }
    }

    /// `ZSTD_rawLiteralsCost` for one literal.
    #[inline(always)]
    fn literal_price(&self, literal: u8) -> i32 {
        if !self.compressed_literals() {
            return ((1 << 3) * BITCOST_MULTIPLIER) as i32;
        }
        if self.price_type == PriceType::Predef {
            return (6 * BITCOST_MULTIPLIER) as i32;
        }
        let lit_price_max = self.lit_sum_base_price - BITCOST_MULTIPLIER;
        let lit_price = self.tables.literal[usize::from(literal)].min(lit_price_max);
        self.lit_sum_base_price.wrapping_sub(lit_price) as i32
    }

    /// `ZSTD_litLengthPrice`.
    #[inline(always)]
    fn lit_length_price(&self, lit_length: u32) -> i32 {
        if self.price_type == PriceType::Predef {
            return weight(lit_length, self.tables.opt_level) as i32;
        }
        if lit_length as usize == crate::shared::format::BLOCK_SIZE_MAX {
            return BITCOST_MULTIPLIER as i32 + self.lit_length_price(lit_length - 1);
        }
        let code = usize::from(ll_code(lit_length));
        self.tables.lit_length[code & 63].wrapping_add(self.lit_length_sum_base_price) as i32
    }

    /// The offset part of `ZSTD_getMatchPrice`; [`Self::match_length_price`]
    /// adds the rest. The sum wraps as the reference's `U32` sum does.
    #[inline(always)]
    fn offset_price(&self, off_base: u32) -> u32 {
        let off_code = highbit32(off_base);
        if self.price_type == PriceType::Predef {
            return (16 + off_code) * BITCOST_MULTIPLIER;
        }
        self.tables.off_code[off_code as usize & 31].wrapping_add(self.off_code_sum_base_price)
    }

    /// The match length part of `ZSTD_getMatchPrice`, including its
    /// heuristic surcharge per sequence.
    #[inline(always)]
    fn match_length_price(&self, match_length: u32) -> u32 {
        let ml_base = match_length - MIN_MATCH as u32;
        if self.price_type == PriceType::Predef {
            return weight(ml_base, self.tables.opt_level);
        }
        let code = usize::from(ml_code(ml_base));
        self.tables.match_length[code & 127].wrapping_add(self.match_length_sum_base_price)
    }

    /// `ZSTD_getMatchPrice`.
    #[cfg(test)]
    fn match_price(&self, off_base: u32, match_length: u32) -> u32 {
        self.offset_price(off_base)
            .wrapping_add(self.match_length_price(match_length))
    }

    /// `ZSTD_updateStats`.
    fn update_stats(&mut self, literals: &[u8], off_base: u32, match_length: u32) {
        if self.compressed_literals() {
            // A byte indexes the 256 frequencies without a bounds check, and
            // the level's weight form is chosen once for the run.
            let level = self.tables.opt_level;
            let literal_weights = &mut self.tables.literal;
            if let Some(freqs) = self.lit_freq.first_chunk_mut::<256>() {
                if literals.len() >= BULK_WEIGHT_LITERALS {
                    // A long run touches most of the table: count it as
                    // the reference does, then take every weight at once.
                    for &lit in literals {
                        freqs[usize::from(lit)] += LITFREQ_ADD;
                    }
                    weights(freqs, level, literal_weights);
                } else if level != 0 {
                    for &lit in literals {
                        let freq = &mut freqs[usize::from(lit)];
                        *freq += LITFREQ_ADD;
                        literal_weights[usize::from(lit)] = frac_weight(*freq);
                    }
                } else {
                    for &lit in literals {
                        let freq = &mut freqs[usize::from(lit)];
                        *freq += LITFREQ_ADD;
                        literal_weights[usize::from(lit)] = bit_weight(*freq);
                    }
                }
            } else {
                for &lit in literals {
                    let freq = &mut self.lit_freq[usize::from(lit)];
                    *freq += LITFREQ_ADD;
                    literal_weights[usize::from(lit)] = weight(*freq, level);
                }
            }
            self.lit_sum += literals.len() as u32 * LITFREQ_ADD;
        }
        let ll = usize::from(ll_code(literals.len() as u32));
        self.lit_length_freq[ll] += 1;
        self.tables.set_lit_length(ll, self.lit_length_freq[ll]);
        self.lit_length_sum += 1;
        let off_code = highbit32(off_base) as usize;
        self.off_code_freq[off_code] += 1;
        self.tables
            .set_off_code(off_code, self.off_code_freq[off_code]);
        self.off_code_sum += 1;
        let ml = usize::from(ml_code(match_length - MIN_MATCH as u32));
        self.match_length_freq[ml] += 1;
        self.tables.set_match_length(ml, self.match_length_freq[ml]);
        self.match_length_sum += 1;
    }
}

/// Grows the parser's position table to `end` positions and returns it.
///
/// The parser reads and writes positions up to two past the furthest
/// match end it prices, which lies below [`OPT_NUM`] and before the end
/// of the block. It grows the table when it first finds a match short of
/// that, instead of covering [`OPT_SIZE`] positions up front, which an
/// incompressible block never reaches. A table only grows, so each entry
/// is written once per engine.
///
/// The parser indexes the returned slice, and calls this only when that
/// slice is too short: the table itself is touched only when it grows, so
/// the slice stays in registers across the parser's calls.
#[cold]
#[inline(never)]
fn grow_positions(table: &mut Vec<Optimal>, end: usize) -> &mut [Optimal] {
    if table.len() < end {
        table.resize(end, Optimal::default());
    }
    table
}

/// `ZSTD_bitWeight`.
const fn bit_weight(stat: u32) -> u32 {
    highbit32(stat + 1) * BITCOST_MULTIPLIER
}

/// `ZSTD_fracWeight`.
const fn frac_weight(raw_stat: u32) -> u32 {
    let stat = raw_stat + 1;
    let hb = highbit32(stat);
    let b_weight = hb * BITCOST_MULTIPLIER;
    let f_weight = (stat << BITCOST_ACCURACY) >> hb;
    b_weight + f_weight
}

/// `WEIGHT(stat, optLevel)`.
#[inline(always)]
const fn weight(stat: u32, opt_level: u32) -> u32 {
    if opt_level != 0 {
        frac_weight(stat)
    } else {
        bit_weight(stat)
    }
}

/// `WEIGHT(stats[i], opt_level)` into `out[i]`, for as many entries as both
/// hold.
///
/// For `stat + 1 < 2^24`, `stat + 1` converts to `f32` exactly, its
/// exponent is `highbit32(stat + 1) + 127`, and its top eight mantissa bits
/// are `((stat + 1) << 8 >> highbit) - 256`. So `bits >> 23` gives
/// `ZSTD_bitWeight` and `bits >> 15` gives `ZSTD_fracWeight`, each up to a
/// constant, in a loop the compiler vectorizes. From `2^24` on, the
/// reference's `stat << 8` wraps, so a table holding such a statistic is
/// redone with the integer form, which wraps the same way.
fn weights(stats: &[u32], opt_level: u32, out: &mut [u32]) {
    let bits = |stat: u32| ((stat + 1) as i32 as f32).to_bits();
    let mut seen = 0u32;
    if opt_level != 0 {
        for (weight_out, &stat) in out.iter_mut().zip(stats) {
            seen |= stat + 1;
            *weight_out = (bits(stat) >> 15).wrapping_sub(126 * BITCOST_MULTIPLIER);
        }
    } else {
        for (weight_out, &stat) in out.iter_mut().zip(stats) {
            seen |= stat + 1;
            *weight_out = ((bits(stat) >> 23).wrapping_sub(127)).wrapping_mul(BITCOST_MULTIPLIER);
        }
    }
    if seen >> 24 != 0 {
        for (weight_out, &stat) in out.iter_mut().zip(stats) {
            *weight_out = weight(stat, opt_level);
        }
    }
}

/// `ZSTD_downscaleStats`.
fn downscale_stats(table: &mut [u32], last: u32, shift: u32, base1: bool) -> u32 {
    let mut sum = 0u32;
    for value in &mut table[..=last as usize] {
        let base = if base1 { 1 } else { u32::from(*value > 0) };
        let new_stat = base + (*value >> shift);
        sum += new_stat;
        *value = new_stat;
    }
    sum
}

/// `ZSTD_scaleStats`.
fn scale_stats(table: &mut [u32], last: u32, log_target: u32) -> u32 {
    let prev_sum: u32 = table[..=last as usize].iter().sum();
    let factor = prev_sum >> log_target;
    if factor <= 1 {
        return prev_sum;
    }
    downscale_stats(table, last, highbit32(factor), true)
}

// ---------------------------------------------------------------------------
// Binary tree.

/// Per-block constants of the tree searches.
///
/// The reference walks raw pointers: `base + index` in the prefix and
/// `dictBase + index` in the external dictionary. Here both segments are
/// plain slices, so an index below `dict_limit` is an offset from the end of
/// `ext` and one at or above it an offset from the start of `prefix`.
#[derive(Copy, Clone, Debug)]
struct Ctx<'a, S> {
    simd: S,
    window: mem::Window<'a>,
    /// Bytes from index `dict_limit` to the end of the block.
    prefix: &'a [u8],
    /// Bytes from index `low_limit` to `dict_limit`.
    ext: &'a [u8],
    dict_limit: u32,
    iend: u32,
    mls: u32,
    /// The attached dictionary; [`DictSearch::EMPTY`], and never read,
    /// outside the dictMatchState variants. Behind a reference, so that
    /// the other variants' context keeps its size and field offsets.
    dms: &'a DictSearch<'a>,
}

/// An attached dictionary's tables and bytes for one block: the `dms*`
/// constants of `ZSTD_insertBtAndGetAllMatches`.
///
/// Indices are the dictionary's own. `bytes` runs from index `start` to
/// `high_limit` (`dmsEnd`); a run reaching its end continues at the start
/// of the block's prefix, as the reference's `ZSTD_count_2segments` does.
#[derive(Copy, Clone, Debug)]
struct DictSearch<'a> {
    hash_table: &'a [u32],
    /// The dictionary's tree, cut to its `2 << dmsBtLog` cells.
    bt: &'a [u32],
    bytes: &'a [u8],
    /// Index of `bytes[0]`.
    start: u32,
    /// `dmsLowLimit`: candidates at or below it end the walk.
    low_limit: u32,
    /// `dmsHighLimit`: the index one past the dictionary's content.
    high_limit: u32,
    /// `dmsHashLog`.
    hash_log: u32,
    /// `dmsBtMask`.
    bt_mask: u32,
    /// `dmsBtLow`: the walk stops after a candidate at or below it.
    bt_low: u32,
}

impl DictSearch<'static> {
    /// No dictionary: empty tables and limits that end every walk.
    const EMPTY: Self = Self {
        hash_table: &[],
        bt: &[],
        bytes: &[],
        start: 0,
        low_limit: 0,
        high_limit: 0,
        hash_log: 0,
        bt_mask: 0,
        bt_low: 0,
    };
}

impl<'a> DictSearch<'a> {
    /// The constants of `dms` (`ms->dictMatchState`).
    fn new(dms: &DictMatchState<'a>) -> Self {
        let cparams = dms.ms.cparams;
        let bt_mask = (1u32 << (cparams.chain_log - 1)) - 1;
        let prefix = dms.window.prefix;
        let high_limit = prefix.end();
        let low_limit = dms.ms.window.low_limit;
        let bt_low = if bt_mask < high_limit.wrapping_sub(low_limit) {
            high_limit - bt_mask
        } else {
            low_limit
        };
        Self {
            hash_table: &dms.ms.hash_table,
            bt: &dms.ms.chain_table[..2 * (bt_mask as usize + 1)],
            bytes: prefix.slice(prefix.start(), high_limit),
            start: prefix.start(),
            low_limit,
            high_limit,
            hash_log: cparams.hash_log,
            bt_mask,
            bt_low,
        }
    }

    /// `ZSTD_readMINMATCH`'s word at dictionary index `index`.
    fn read32(&self, index: u32) -> u32 {
        let at = index.wrapping_sub(self.start) as usize;
        let mut word = [0u8; 4];
        word.copy_from_slice(&self.bytes[at..at + 4]);
        u32::from_le_bytes(word)
    }
}

impl<'a, S: Simd> Ctx<'a, S> {
    fn new(simd: S, block: &BlockInput<'a>, mls: u32) -> Self {
        let window = block.window;
        let prefix = window.prefix;
        let ext = window.ext;
        Self {
            simd,
            window,
            // Cut at the block's end, so that the end is the slice's: a
            // run then needs no limit of its own.
            prefix: prefix.slice(prefix.start(), block.end),
            ext: ext.slice(ext.start(), ext.end()),
            dict_limit: prefix.start(),
            iend: block.end,
            mls,
            dms: &DictSearch::EMPTY,
        }
    }

    /// [`Self::new`] with the attached dictionary `dms` searched too.
    fn with_dictionary(simd: S, block: &BlockInput<'a>, mls: u32, dms: &'a DictSearch<'a>) -> Self {
        Self {
            dms,
            ..Self::new(simd, block, mls)
        }
    }

    /// `ZSTD_count_2segments(ip, dmsBase + candidate, iend, dmsEnd,
    /// prefixStart)` for dictionary index `candidate`. A candidate past the
    /// dictionary's end counts nothing, as the reference's does (its
    /// segment end then lies before `ip`).
    #[inline(always)]
    fn count_dictionary(&self, ip: u32, candidate: u32) -> usize {
        let earlier = candidate.wrapping_sub(self.dms.start) as usize;
        if earlier > self.dms.bytes.len() {
            return 0;
        }
        opt_count::count_2segments(
            self.simd,
            self.prefix,
            self.offset(ip),
            self.dms.bytes,
            earlier,
            self.prefix.len(),
        )
    }

    /// Offset of prefix index `index` in [`Self::prefix`].
    #[inline(always)]
    const fn offset(&self, index: u32) -> usize {
        (index - self.dict_limit) as usize
    }

    /// Offset of external index `index` in [`Self::ext`].
    #[inline(always)]
    const fn ext_offset(&self, index: u32) -> usize {
        self.ext.len() - (self.dict_limit - index) as usize
    }

    /// The byte at window index `index`.
    #[inline(always)]
    fn byte<const EXT: bool>(&self, index: usize) -> u8 {
        let dict_limit = self.dict_limit as usize;
        if !EXT || index >= dict_limit {
            self.prefix[index - dict_limit]
        } else {
            self.ext[self.ext.len() - (dict_limit - index)]
        }
    }

    /// Little-endian `u32` at prefix index `index`.
    #[inline(always)]
    fn read32(&self, index: u32) -> u32 {
        let at = self.offset(index);
        let mut word = [0u8; 4];
        word.copy_from_slice(&self.prefix[at..at + 4]);
        u32::from_le_bytes(word)
    }

    /// `ZSTD_hashPtr(base + index, hash_log, MLS)`: the 4-byte hash for
    /// `MLS` 3 and 4, the 5-, 6- and 7-byte ones above (7 only in
    /// dictionary loads, which do not clamp the minimum match).
    #[inline(always)]
    fn hash<const MLS: u32>(&self, index: u32, hash_log: u32) -> usize {
        if MLS <= 4 {
            return (self.read32(index).wrapping_mul(PRIME_4_BYTES) >> (32 - hash_log)) as usize;
        }
        let at = self.offset(index);
        let mut word = [0u8; 8];
        word.copy_from_slice(&self.prefix[at..at + 8]);
        let (shift, prime) = match MLS {
            5 => (24, PRIME_5_BYTES),
            6 => (16, PRIME_6_BYTES),
            _ => (8, PRIME_7_BYTES),
        };
        ((u64::from_le_bytes(word) << shift).wrapping_mul(prime) >> (64 - hash_log)) as usize
    }

    /// Hints that the hash cells of `ip + 4` will be read soon: the parser
    /// and the tree update mostly go position after position, and a search
    /// is much shorter than a load from memory, so the hint runs a few
    /// searches ahead. With `HASH3` and `MLS` 3 the 3-byte table's cell is
    /// hinted too. A position whose bytes do not lie in the block is
    /// skipped.
    #[inline(always)]
    fn prefetch_ahead<const MLS: u32, const HASH3: bool>(&self, finder: &Finder<'_>, ip: u32) {
        let ahead = ip + 4;
        if MLS <= 4 {
            // The 4 bytes at `ip + 4` end the word at `ip`, which the
            // search reads too.
            let at = self.offset(ip);
            let Some(bytes) = self.prefix.get(at..at + 8) else {
                return;
            };
            let mut word = [0u8; 8];
            word.copy_from_slice(bytes);
            let word = (u64::from_le_bytes(word) >> 32) as u32;
            let h = word.wrapping_mul(PRIME_4_BYTES) >> (32 - finder.hash_log);
            prefetch_l1(finder.hash_table, h as usize);
            if HASH3 && MLS == 3 {
                let h3 = mem::hash3(word, finder.hash_log3, 0);
                prefetch_l1(finder.hash_table3, h3);
            }
        } else if self.offset(ahead) + 8 <= self.prefix.len() {
            prefetch_l1(finder.hash_table, self.hash::<MLS>(ahead, finder.hash_log));
        }
    }

    /// `ZSTD_count(ip, match, iend)` inside the prefix.
    #[inline(always)]
    fn count(&self, ip: u32, candidate: u32) -> usize {
        opt_count::count(
            self.simd,
            self.prefix,
            self.offset(ip),
            self.offset(candidate),
            self.prefix.len(),
        )
    }

    /// `ZSTD_count_2segments(ip, dictBase + candidate, iend, dictEnd,
    /// prefixStart)` for a candidate in the external dictionary.
    #[inline(always)]
    fn count_2segments(&self, ip: u32, candidate: u32) -> usize {
        opt_count::count_2segments(
            self.simd,
            self.prefix,
            self.offset(ip),
            self.ext,
            self.ext_offset(candidate),
            self.prefix.len(),
        )
    }

    /// The bytes from prefix index `curr` to the end of the block.
    #[inline(always)]
    fn current(&self, curr: u32) -> &'a [u8] {
        &self.prefix[self.offset(curr)..]
    }

    /// The tree walk's comparison: `known` bytes are already equal; returns
    /// the full common length of `curr` (whose bytes to the end of the
    /// block are `current`) and `candidate`, the latter read through both
    /// segments with `EXT`, and whether the candidate's first differing
    /// byte is the smaller (`false` when the run reaches the end of the
    /// block, where the walk stops without looking).
    #[inline(always)]
    fn extend<const EXT: bool>(
        &self,
        current: &[u8],
        curr: u32,
        candidate: u32,
        known: usize,
    ) -> (usize, bool) {
        let dict_limit = self.dict_limit as usize;
        if !EXT || candidate as usize + known >= dict_limit {
            let (length, smaller) = opt_count::count_ordered(
                self.simd,
                current,
                self.prefix,
                // The candidate may start in the external segment with its
                // known prefix reaching into this one.
                candidate + known as u32 - self.dict_limit,
                known as u32,
            );
            (known + length, smaller)
        } else {
            let known32 = known as u32;
            let length = known + self.count_2segments(curr + known32, candidate + known32);
            let smaller = length < current.len()
                && self.byte::<EXT>(candidate as usize + length)
                    < self.byte::<EXT>(curr as usize + length);
            (length, smaller)
        }
    }
}

/// Hints that `table[index]` will be read soon (`PREFETCH_L1`). The pointer
/// is only handed to the prefetch instruction, never dereferenced, so a
/// hint past the table is harmless; targets without one ignore it.
#[inline(always)]
fn prefetch_l1<T>(table: &[T], index: usize) {
    branches::prefetch_read_data::<T, 0>(table.as_ptr().wrapping_add(index));
}

/// `ZSTD_readMINMATCH` comparison of `length` (3 or 4) bytes.
#[inline(always)]
fn min_match_equal(x: u32, y: u32, length: u32) -> bool {
    if length == 3 {
        (x << 8) == (y << 8)
    } else {
        x == y
    }
}

/// The binary-tree match finder's tables and its parameters for one block.
///
/// The reference reads `ms->cParams` and the window on every search; here
/// whatever is fixed for the block is derived once, so a search loads it
/// rather than recomputing it from the parameters.
struct Finder<'t> {
    hash_table: &'t mut [u32],
    /// The tree: two cells per position, cut to its `2 << btLog` cells.
    chain_table: &'t mut [u32],
    hash_table3: &'t mut [u32],
    /// `ms->nextToUpdate`, stored back after the block.
    next_to_update: u32,
    /// Index from which the 3-byte table is brought up to date.
    next_to_update3: u32,
    hash_log: u32,
    hash_log3: u32,
    /// `(1 << btLog) - 1`.
    bt_mask: u32,
    /// `1 << searchLog`, at least one.
    nb_compares: u32,
    /// `MIN(targetLength, ZSTD_OPT_NUM - 1)`.
    sufficient_len: u32,
    /// The window's reach `1 << windowLog`; with a loaded dictionary every
    /// index from `low_limit` on stays valid, which no distance exceeds.
    max_distance: u32,
    low_limit: u32,
}

impl<'t> Finder<'t> {
    /// The finder over `ms`'s tables for a block whose updates resume at
    /// `ms.next_to_update`, and the pricing state, which the parser keeps
    /// using beside it.
    fn split(ms: &'t mut MatchState) -> (Self, &'t mut OptState) {
        let cparams = ms.cparams;
        let bt_mask = (1u32 << (cparams.chain_log - 1)) - 1;
        let finder = Self {
            hash_table: &mut ms.hash_table,
            chain_table: &mut ms.chain_table[..2 * (bt_mask as usize + 1)],
            hash_table3: &mut ms.hash_table3,
            next_to_update: ms.next_to_update,
            next_to_update3: ms.next_to_update,
            hash_log: cparams.hash_log,
            hash_log3: ms.hash_log3,
            bt_mask,
            nb_compares: 1u32 << cparams.search_log,
            sufficient_len: cparams.target_length.min(OPT_NUM as u32 - 1),
            max_distance: if ms.loaded_dict_end != 0 {
                u32::MAX
            } else {
                1u32 << cparams.window_log
            },
            low_limit: ms.window.low_limit,
        };
        (finder, &mut ms.opt)
    }

    /// `ZSTD_getLowestMatchIndex`.
    #[inline(always)]
    const fn lowest_match_index(&self, curr: u32) -> u32 {
        if curr.wrapping_sub(self.low_limit) > self.max_distance {
            curr - self.max_distance
        } else {
            self.low_limit
        }
    }
}

/// `ZSTD_insertAndFindFirstIndexHash3`.
#[inline(always)]
fn insert_and_find_first_index_hash3<S: Simd>(
    finder: &mut Finder<'_>,
    ctx: &Ctx<'_, S>,
    ip: u32,
) -> u32 {
    let hash_log3 = finder.hash_log3;
    let table = &mut *finder.hash_table3;
    let hash3 = |index: u32| mem::hash3(ctx.read32(index), hash_log3, 0);
    let mut idx = finder.next_to_update3;
    while idx < ip {
        table[hash3(idx)] = idx;
        idx += 1;
    }
    finder.next_to_update3 = ip;
    table[hash3(ip)]
}

/// `ZSTD_insertBt1`: returns how many positions it accounted for.
#[inline(always)]
fn insert_bt1<S: Simd, const EXT: bool, const MLS: u32>(
    finder: &mut Finder<'_>,
    ctx: &Ctx<'_, S>,
    curr: u32,
    target: u32,
) -> u32 {
    let h = ctx.hash::<MLS>(curr, finder.hash_log);
    let match_index = finder.hash_table[h];
    finder.hash_table[h] = curr;
    ctx.prefetch_ahead::<MLS, false>(finder, curr);
    let walk = TreeWalk {
        curr,
        match_index,
        window_low: finder.lowest_match_index(target),
        nb_compares: finder.nb_compares,
        best_length: 8,
    };
    let (best_length, match_end_idx, _) =
        walk.run::<S, EXT>(ctx, finder.chain_table, finder.bt_mask, |_, _| false);
    let positions = if best_length > 384 {
        192.min((best_length - 384) as u32)
    } else {
        0
    };
    positions.max(match_end_idx - (curr + 8))
}

/// One insertion of `curr` into the binary tree: the walk shared by
/// `ZSTD_insertBt1` and `ZSTD_insertBtAndGetAllMatches`.
#[derive(Copy, Clone, Debug)]
struct TreeWalk {
    curr: u32,
    /// The first candidate, from the hash table.
    match_index: u32,
    /// Candidates below this index are out of the window.
    window_low: u32,
    nb_compares: u32,
    /// Length a candidate must exceed to be reported.
    best_length: usize,
}

impl TreeWalk {
    /// Walks the tree from `match_index`, re-linking it so that `curr` is
    /// the new root, and calls `on_longer(index, length)` for each candidate
    /// longer than every earlier one; it returns `true` to stop the walk.
    /// Returns the longest length, the farthest match end (`matchEndIdx`)
    /// and the comparisons left for an attached dictionary's tree: none
    /// after a stop, as the reference's dictMatchState walk sets them.
    ///
    /// `bt` holds the tree's `2 * (bt_mask + 1)` cells. The reference parks
    /// a finished side on a dummy cell; here a side whose walk ended below
    /// `bt_low` is simply not cleared at the end.
    #[inline(always)]
    fn run<S: Simd, const EXT: bool>(
        self,
        ctx: &Ctx<'_, S>,
        bt: &mut [u32],
        bt_mask: u32,
        mut on_longer: impl FnMut(u32, usize) -> bool,
    ) -> (usize, u32, u32) {
        let Self {
            curr,
            mut match_index,
            window_low,
            mut nb_compares,
            mut best_length,
        } = self;
        // The cells the next smaller / larger candidate is linked from.
        let mut smaller = 2 * (curr & bt_mask) as usize;
        let mut match_end_idx = curr + 8 + 1;
        // `nb_compares` is at least one, so only the window can rule out
        // the first candidate: then `curr` becomes a leaf straight away,
        // which incompressible input does at nearly every position.
        if match_index < window_low {
            bt[smaller..smaller + 2].fill(0);
            return (best_length, match_end_idx, nb_compares);
        }
        // Cut to the tree's size and indexed through `cells`: every cell
        // index is below that size already, so the mask changes nothing
        // but lets the compiler drop the bounds checks.
        let cells = (2 * bt_mask + 1) as usize;
        let bt = &mut bt[..=cells];
        let mut larger = smaller + 1;
        let bt_low = curr.saturating_sub(bt_mask);
        let current = ctx.current(curr);
        let mut common_smaller = 0usize;
        let mut common_larger = 0usize;
        let (clear_smaller, clear_larger) = loop {
            let (match_length, match_smaller) = ctx.extend::<EXT>(
                current,
                curr,
                match_index,
                common_smaller.min(common_larger),
            );
            if match_length > best_length {
                best_length = match_length;
                if match_length as u32 > match_end_idx - match_index {
                    match_end_idx = match_index + match_length as u32;
                }
                if on_longer(match_index, match_length) {
                    nb_compares = 0;
                    break (true, true);
                }
            }
            if match_length == current.len() {
                // The run reaches the end of the block: no way to order it.
                nb_compares = 0;
                break (true, true);
            }
            // `cells` is `2 * bt_mask + 1` and an even number has no low
            // bit: this is `2 * (match_index & bt_mask)`.
            let next = ((match_index as usize) << 1) & cells;
            if match_smaller {
                bt[smaller & cells] = match_index;
                common_smaller = match_length;
                if match_index <= bt_low {
                    break (false, true);
                }
                smaller = next + 1;
                match_index = bt[(next + 1) & cells];
            } else {
                bt[larger & cells] = match_index;
                common_larger = match_length;
                if match_index <= bt_low {
                    break (true, false);
                }
                larger = next;
                match_index = bt[next & cells];
            }
            nb_compares -= 1;
            if nb_compares == 0 || match_index < window_low {
                break (true, true);
            }
        };
        if clear_smaller {
            bt[smaller & cells] = 0;
        }
        if clear_larger {
            bt[larger & cells] = 0;
        }
        (best_length, match_end_idx, nb_compares)
    }
}

/// Where and what `get_all_matches` searches: the position, its repeat
/// offsets, whether the literal length is zero, and the length to beat.
#[derive(Copy, Clone, Debug)]
struct MatchQuery<'r> {
    ip: u32,
    rep: &'r [u32; REP_NUM],
    ll0: bool,
    length_to_beat: u32,
}

/// `ZSTD_insertBtAndGetAllMatches`; `DMS` searches the attached
/// dictionary's tree after the block's own (the reference's
/// `dictMode == ZSTD_dictMatchState`).
#[inline(always)]
fn insert_bt_and_get_all_matches<S: Simd, const EXT: bool, const DMS: bool, const MLS: u32>(
    finder: &mut Finder<'_>,
    ctx: &Ctx<'_, S>,
    matches: &mut [Match],
    query: MatchQuery<'_>,
) -> u32 {
    let MatchQuery {
        ip,
        rep,
        ll0,
        length_to_beat,
    } = query;
    let sufficient_len = finder.sufficient_len;
    let curr = ip;
    let mls = MLS;
    let min_match = if mls == 3 { 3 } else { 4 };
    let h = ctx.hash::<MLS>(ip, finder.hash_log);
    let match_index = finder.hash_table[h];
    // Large tables miss the caches: a later search's cell loads while this
    // one runs.
    ctx.prefetch_ahead::<MLS, true>(finder, ip);
    let dict_limit = ctx.dict_limit;
    let window_low = finder.lowest_match_index(curr);
    let match_low = if window_low != 0 { window_low } else { 1 };
    let mut mnum = 0usize;
    let mut best_length = (length_to_beat - 1) as usize;
    let i_limit = ctx.iend;

    // Repeat offsets.
    {
        // Every repeat candidate in the prefix lies before `ip`: its word
        // lies in the bytes up to `ip`'s, so one length test covers all.
        let ip_off = curr - dict_limit;
        let head = &ctx.prefix[..ip_off as usize + 4];
        let word_at = |at: u32| {
            let mut word = [0u8; 4];
            word.copy_from_slice(&head[at as usize..at as usize + 4]);
            u32::from_le_bytes(word)
        };
        let ip_word = word_at(ip_off);
        let ll0 = u32::from(ll0);
        let last_r = REP_NUM as u32 + ll0;
        for rep_code in ll0..last_r {
            let rep_offset = if rep_code == REP_NUM as u32 {
                rep[0].wrapping_sub(1)
            } else {
                rep[rep_code as usize]
            };
            let rep_index = curr.wrapping_sub(rep_offset);
            let mut rep_len = 0u32;
            // `rep_offset - 1 < curr - dict_limit`, spelled so that the
            // candidate's offset is seen to stay at or before `ip_off`.
            if rep_offset != 0 && rep_offset <= ip_off {
                // The candidate lies in the prefix, so its word reads before
                // the window test, as the reference's `&` lets it: most
                // positions settle on the word alone.
                if min_match_equal(ip_word, word_at(ip_off - rep_offset), min_match)
                    && rep_index >= window_low
                {
                    rep_len =
                        ctx.count(ip + min_match, ip + min_match - rep_offset) as u32 + min_match;
                }
            } else if EXT
                && rep_offset.wrapping_sub(1) < curr - window_low
                && dict_limit.wrapping_sub(1).wrapping_sub(rep_index) >= 3
                && min_match_equal(ip_word, ctx.window.ext.read32(rep_index), min_match)
            {
                rep_len =
                    ctx.count_2segments(ip + min_match, rep_index + min_match) as u32 + min_match;
            } else if DMS {
                rep_len =
                    dictionary_repeat_length(ctx, ip, ip_word, rep_offset, window_low, min_match);
            }
            if rep_len as usize > best_length {
                best_length = rep_len as usize;
                matches[mnum] = Match {
                    off: repcode_to_offbase(rep_code - ll0 + 1),
                    len: rep_len,
                };
                mnum += 1;
                if rep_len > sufficient_len || ip + rep_len == i_limit {
                    return mnum as u32;
                }
            }
        }
    }

    // HC3 match finder.
    if mls == 3 && best_length < mls as usize {
        let match_index3 = insert_and_find_first_index_hash3(finder, ctx, ip);
        if match_index3 >= match_low && curr - match_index3 < (1 << 18) {
            let mlen = if !EXT || match_index3 >= dict_limit {
                ctx.count(ip, match_index3)
            } else {
                ctx.count_2segments(ip, match_index3)
            };
            if mlen >= mls as usize {
                best_length = mlen;
                matches[0] = Match {
                    off: offset_to_offbase(curr - match_index3),
                    len: mlen as u32,
                };
                mnum = 1;
                if mlen > sufficient_len as usize || ip as usize + mlen == i_limit as usize {
                    finder.next_to_update = curr + 1;
                    return 1;
                }
            }
        }
    }

    finder.hash_table[h] = curr;
    let walk = TreeWalk {
        curr,
        match_index,
        window_low: match_low,
        nb_compares: finder.nb_compares,
        best_length,
    };
    let (best_length, mut match_end_idx, nb_compares) = walk.run::<S, EXT>(
        ctx,
        finder.chain_table,
        finder.bt_mask,
        |match_index, match_length| {
            matches[mnum] = Match {
                off: offset_to_offbase(curr - match_index),
                len: match_length as u32,
            };
            mnum += 1;
            // A run reaching the block's end stops the walk after this.
            match_length > OPT_NUM
        },
    );
    if DMS && nb_compares != 0 {
        let found = DictionaryFound {
            best_length,
            match_end_idx,
            mnum,
        };
        let found = search_dictionary::<S, MLS>(ctx, matches, curr, window_low, nb_compares, found);
        match_end_idx = found.match_end_idx;
        mnum = found.mnum;
    }
    finder.next_to_update = match_end_idx - 8;
    mnum as u32
}

/// The dictMatchState repeat-offset check of
/// `ZSTD_insertBtAndGetAllMatches` for a repeat offset that reaches before
/// the prefix: the length of the match `rep_offset` back from `ip`, read in
/// the attached dictionary and continued at the prefix start, or `0`.
///
/// `ip_word` holds the four bytes at `ip`; `window_low` is the window's
/// lowest index at `ip`, which places the dictionary's end just below it
/// (`dmsIndexDelta`).
#[inline(always)]
fn dictionary_repeat_length<S: Simd>(
    ctx: &Ctx<'_, S>,
    ip: u32,
    ip_word: u32,
    rep_offset: u32,
    window_low: u32,
    min_match: u32,
) -> u32 {
    let dms = &ctx.dms;
    let curr = ip;
    let rep_index = curr.wrapping_sub(rep_offset);
    let index_delta = window_low.wrapping_sub(dms.high_limit);
    // `curr > repIndex >= dmsLowLimit` in the dictionary's indices, and the
    // word does not straddle the prefix start (`ZSTD_index_overlap_check`).
    if rep_offset.wrapping_sub(1) < curr.wrapping_sub(dms.low_limit.wrapping_add(index_delta))
        && ctx.dict_limit.wrapping_sub(1).wrapping_sub(rep_index) >= 3
    {
        let dict_index = rep_index.wrapping_sub(index_delta);
        if min_match_equal(ip_word, dms.read32(dict_index), min_match) {
            return ctx.count_dictionary(ip + min_match, dict_index + min_match) as u32 + min_match;
        }
    }
    0
}

/// What a search found before the attached dictionary's tree: the length
/// to beat, the farthest match end and the number of matches stored.
#[derive(Copy, Clone, Debug)]
struct DictionaryFound {
    best_length: usize,
    match_end_idx: u32,
    mnum: usize,
}

/// The dictMatchState tail of `ZSTD_insertBtAndGetAllMatches`: walks the
/// attached dictionary's tree with the `nb_compares` comparisons the
/// block's own tree left, storing each candidate longer than every
/// earlier match. The dictionary's tree is only read.
///
/// A dictionary index `i` is window index `i + dmsIndexDelta`, so the
/// dictionary ends just below `window_low`.
#[inline(always)]
fn search_dictionary<S: Simd, const MLS: u32>(
    ctx: &Ctx<'_, S>,
    matches: &mut [Match],
    curr: u32,
    window_low: u32,
    mut nb_compares: u32,
    found: DictionaryFound,
) -> DictionaryFound {
    let DictionaryFound {
        mut best_length,
        mut match_end_idx,
        mut mnum,
    } = found;
    let dms = &ctx.dms;
    let index_delta = window_low.wrapping_sub(dms.high_limit);
    let cells = (2 * dms.bt_mask + 1) as usize;
    let bt = &dms.bt[..=cells];
    let current = ctx.current(curr);
    let mut dict_match_index = dms.hash_table[ctx.hash::<MLS>(curr, dms.hash_log)];
    let mut common_smaller = 0usize;
    let mut common_larger = 0usize;
    while nb_compares != 0 && dict_match_index > dms.low_limit {
        let next = ((dict_match_index as usize) << 1) & cells;
        let known = common_smaller.min(common_larger);
        let match_length =
            known + ctx.count_dictionary(curr + known as u32, dict_match_index + known as u32);
        if match_length > best_length {
            let match_index = dict_match_index.wrapping_add(index_delta);
            if match_length as u32 > match_end_idx.wrapping_sub(match_index) {
                match_end_idx = match_index.wrapping_add(match_length as u32);
            }
            best_length = match_length;
            matches[mnum] = Match {
                off: offset_to_offbase(curr.wrapping_sub(match_index)),
                len: match_length as u32,
            };
            mnum += 1;
            if match_length > OPT_NUM || match_length == current.len() {
                break;
            }
        }
        if dict_match_index <= dms.bt_low {
            break;
        }
        // The candidate's next byte: in the dictionary, or past its end in
        // the prefix, at the window index the reference moves its `match`
        // pointer to.
        let at = dict_match_index.wrapping_add(match_length as u32);
        let candidate_byte = if at < dms.high_limit {
            dms.bytes[at.wrapping_sub(dms.start) as usize]
        } else {
            ctx.prefix[at.wrapping_add(index_delta).wrapping_sub(ctx.dict_limit) as usize]
        };
        let Some(&current_byte) = current.get(match_length) else {
            // A run to the end of the block is longer than any before it,
            // which ended the walk above.
            break;
        };
        if candidate_byte < current_byte {
            common_smaller = match_length;
            dict_match_index = bt[(next + 1) & cells];
        } else {
            common_larger = match_length;
            dict_match_index = bt[next & cells];
        }
        nb_compares -= 1;
    }
    DictionaryFound {
        best_length,
        match_end_idx,
        mnum,
    }
}

/// `ZSTD_btGetAllMatches_internal`: brings the tree up to `query.ip`, then
/// collects the matches there.
///
/// The wrapper inlines into the parser, which calls the level's
/// feature-enabled entry directly; the tree update and the search inline
/// into that entry.
#[simd]
#[inline(always)]
fn get_all_matches<S: Simd, const EXT: bool, const DMS: bool, const MLS: u32>(
    simd: S,
    finder: &mut Finder<'_>,
    ctx: &Ctx<'_, S>,
    matches: &mut [Match],
    query: MatchQuery<'_>,
) -> u32 {
    let _ = simd;
    if query.ip < finder.next_to_update {
        return 0;
    }
    if finder.next_to_update < query.ip {
        update_tree::<S, EXT, MLS>(finder, ctx, query.ip);
    }
    finder.next_to_update = query.ip;
    insert_bt_and_get_all_matches::<S, EXT, DMS, MLS>(finder, ctx, matches, query)
}

/// `ZSTD_updateTree_internal`: inserts the positions a long match skipped.
///
/// Out of line, so that its walk does not crowd the search's registers;
/// it is plain scalar code, and the scan of a long run calls into the
/// backend's feature context as the search's does.
#[inline(never)]
fn update_tree<S: Simd, const EXT: bool, const MLS: u32>(
    finder: &mut Finder<'_>,
    ctx: &Ctx<'_, S>,
    target: u32,
) {
    let mut idx = finder.next_to_update;
    while idx < target {
        idx += insert_bt1::<S, EXT, MLS>(finder, ctx, idx, target);
    }
}

/// [`get_all_matches`] for the block's minimum match length, the
/// reference's `ZSTD_selectBtGetAllMatches`; the branch resolves the same
/// way for the whole block.
#[inline(always)]
fn find_matches<S: Simd, const EXT: bool, const DMS: bool>(
    simd: S,
    finder: &mut Finder<'_>,
    ctx: &Ctx<'_, S>,
    matches: &mut [Match],
    query: MatchQuery<'_>,
) -> u32 {
    match ctx.mls {
        3 => get_all_matches::<S, EXT, DMS, 3>(simd, finder, ctx, matches, query),
        4 => get_all_matches::<S, EXT, DMS, 4>(simd, finder, ctx, matches, query),
        5 => get_all_matches::<S, EXT, DMS, 5>(simd, finder, ctx, matches, query),
        _ => get_all_matches::<S, EXT, DMS, 6>(simd, finder, ctx, matches, query),
    }
}

// ---------------------------------------------------------------------------
// LDM candidates.

/// LDM candidate tracking (`ZSTD_optLdm_t`).
#[derive(Debug)]
struct OptLdm {
    seq_store: RawSeqStore,
    start_pos_in_block: u32,
    end_pos_in_block: u32,
    offset: u32,
}

impl OptLdm {
    /// `ZSTD_opt_getNextMatchAndUpdateSeqStore`.
    fn next_match(&mut self, curr_pos_in_block: u32, block_bytes_remaining: u32) {
        if self.seq_store.size() == 0 || self.seq_store.pos >= self.seq_store.size() {
            self.start_pos_in_block = u32::MAX;
            self.end_pos_in_block = u32::MAX;
            return;
        }
        let curr = self.seq_store.seq[self.seq_store.pos];
        let curr_block_end_pos = curr_pos_in_block + block_bytes_remaining;
        let pos_in_seq = self.seq_store.pos_in_sequence as u32;
        let literals_remaining = curr.lit_length.saturating_sub(pos_in_seq);
        let match_remaining = if literals_remaining == 0 {
            curr.match_length - (pos_in_seq - curr.lit_length)
        } else {
            curr.match_length
        };
        if literals_remaining >= block_bytes_remaining {
            self.start_pos_in_block = u32::MAX;
            self.end_pos_in_block = u32::MAX;
            self.seq_store
                .skip_bytes_opt(block_bytes_remaining as usize);
            return;
        }
        self.start_pos_in_block = curr_pos_in_block + literals_remaining;
        self.end_pos_in_block = self.start_pos_in_block + match_remaining;
        self.offset = curr.offset;
        if self.end_pos_in_block > curr_block_end_pos {
            self.end_pos_in_block = curr_block_end_pos;
            self.seq_store
                .skip_bytes_opt((curr_block_end_pos - curr_pos_in_block) as usize);
        } else {
            self.seq_store
                .skip_bytes_opt((literals_remaining + match_remaining) as usize);
        }
    }

    /// `ZSTD_optLdm_maybeAddMatch`.
    fn maybe_add_match(
        &self,
        matches: &mut [Match],
        nb_matches: &mut u32,
        curr_pos_in_block: u32,
        min_match: u32,
    ) {
        let pos_diff = curr_pos_in_block.wrapping_sub(self.start_pos_in_block);
        let candidate_len = self
            .end_pos_in_block
            .wrapping_sub(self.start_pos_in_block)
            .wrapping_sub(pos_diff);
        if curr_pos_in_block < self.start_pos_in_block
            || curr_pos_in_block >= self.end_pos_in_block
            || candidate_len < min_match
        {
            return;
        }
        if *nb_matches == 0
            || (candidate_len > matches[*nb_matches as usize - 1].len
                && (*nb_matches as usize) < OPT_NUM)
        {
            matches[*nb_matches as usize] = Match {
                off: offset_to_offbase(self.offset),
                len: candidate_len,
            };
            *nb_matches += 1;
        }
    }

    /// `ZSTD_optLdm_processMatchCandidate`.
    ///
    /// Without LDM candidates this is one inline test per position; the
    /// candidate bookkeeping stays out of line.
    #[inline(always)]
    fn process(
        &mut self,
        matches: &mut [Match],
        nb_matches: &mut u32,
        curr_pos_in_block: u32,
        remaining: u32,
        min_match: u32,
    ) {
        if self.seq_store.pos >= self.seq_store.size() {
            return;
        }
        self.process_candidate(matches, nb_matches, curr_pos_in_block, remaining, min_match);
    }

    /// [`Self::process`] with candidates left.
    #[inline(never)]
    fn process_candidate(
        &mut self,
        matches: &mut [Match],
        nb_matches: &mut u32,
        curr_pos_in_block: u32,
        remaining: u32,
        min_match: u32,
    ) {
        if curr_pos_in_block >= self.end_pos_in_block {
            if curr_pos_in_block > self.end_pos_in_block {
                let overshoot = curr_pos_in_block - self.end_pos_in_block;
                self.seq_store.skip_bytes_opt(overshoot as usize);
            }
            self.next_match(curr_pos_in_block, remaining);
        }
        self.maybe_add_match(matches, nb_matches, curr_pos_in_block, min_match);
    }
}

// ---------------------------------------------------------------------------
// The parser.

/// `ZSTD_compressBlock_opt_generic`, in `simd`'s feature context.
///
/// `OPT_LEVEL` (0 for btopt, 2 for the ultra strategies), `EXT` (an
/// external dictionary is in the window) and `DMS` (the attached
/// dictionary `block.dms` is searched too) are the reference's template
/// parameters; all are fixed per block, and at most one of `EXT` and
/// `DMS` is set.
#[simd]
fn opt_generic<S: Simd, const OPT_LEVEL: u32, const EXT: bool, const DMS: bool>(
    simd: S,
    ms: &mut MatchState,
    seq_store: &mut SeqStore,
    rep: &mut [u32; REP_NUM],
    block: &BlockInput<'_>,
) -> usize {
    let opt_level = OPT_LEVEL;
    let prefix = block.window.prefix;
    let istart = block.start;
    let iend = block.end;
    let ilimit = i64::from(iend) - 8;
    let prefix_start = ms.window.dict_limit;
    let cparams = ms.cparams;
    let sufficient_len = cparams.target_length.min(OPT_NUM as u32 - 1);
    let min_match = if cparams.min_match == 3 { 3 } else { 4 };
    let mls = cparams.min_match.clamp(3, 6);
    let dictionary = match block.dms {
        Some(dms) if DMS => Some(DictSearch::new(&dms)),
        _ => None,
    };
    let ctx = match &dictionary {
        Some(dictionary) => Ctx::with_dictionary(simd, block, mls, dictionary),
        None => Ctx::new(simd, block, mls),
    };

    ms.opt
        .reserve_matches((iend - istart) as usize, 1u32 << cparams.search_log);
    let mut opt_table = core::mem::take(&mut ms.opt.price_table);
    let mut opt: &mut [Optimal] = &mut opt_table;
    // Every match the parser prices ends inside the block, and below
    // `OPT_NUM` positions from where the parse started.
    let reach = OPT_SIZE.min((iend - istart) as usize + 3);
    let mut matches = core::mem::take(&mut ms.opt.match_table);
    let mut last_stretch = Optimal::default();
    // The reference copies the store's header; here the store is moved out
    // for the block and returned with its cursor as it was.
    let lent = ms.ldm_seq_store.take();
    let lent_cursor = lent
        .as_ref()
        .map(|store| (store.pos, store.pos_in_sequence));
    let mut opt_ldm = OptLdm {
        seq_store: lent.unwrap_or_default(),
        start_pos_in_block: 0,
        end_pos_in_block: 0,
        offset: 0,
    };
    let mut ip = istart;
    let mut anchor = istart;
    opt_ldm.next_match(ip - istart, iend - ip);

    ms.opt
        .rescale_freqs(prefix.slice(istart, iend), opt_level, block.symbol_costs);
    ip += u32::from(ip == prefix_start);
    let (mut finder, stats) = Finder::split(ms);

    while i64::from(ip) < ilimit {
        let mut cur: u32;
        let mut last_pos: u32;

        // Find the first match.
        let litlen = ip - anchor;
        let ll0 = litlen == 0;
        let mut nb_matches = find_matches::<S, EXT, DMS>(
            simd,
            &mut finder,
            &ctx,
            &mut matches,
            MatchQuery {
                ip,
                rep,
                ll0,
                length_to_beat: min_match,
            },
        );
        opt_ldm.process(
            &mut matches,
            &mut nb_matches,
            ip - istart,
            iend - ip,
            min_match,
        );
        if nb_matches == 0 {
            ip += 1;
            continue;
        }

        if opt.len() < reach {
            opt = grow_positions(&mut opt_table, reach);
        }
        let max_ml = matches[nb_matches as usize - 1].len;
        let max_off_base = matches[nb_matches as usize - 1].off;

        let os = &*stats;
        opt[0].mlen = 0;
        opt[0].litlen = litlen;
        opt[0].price = os.lit_length_price(litlen);
        opt[0].rep = *rep;
        let shortest_path_ready;
        if max_ml > sufficient_len {
            last_stretch.litlen = 0;
            last_stretch.mlen = max_ml;
            last_stretch.off = max_off_base;
            cur = 0;
            last_pos = max_ml;
            shortest_path_ready = true;
        } else {
            let mut pos = 1u32;
            while pos < min_match {
                opt[pos as usize].price = MAX_PRICE;
                opt[pos as usize].mlen = 0;
                opt[pos as usize].litlen = litlen + pos;
                pos += 1;
            }
            let ll0_price = os.lit_length_price(0);
            for m in &matches[..nb_matches as usize] {
                let off_base = m.off;
                let end = m.len;
                let off_price = os.offset_price(off_base);
                while pos <= end {
                    let match_price = off_price.wrapping_add(os.match_length_price(pos)) as i32;
                    let sequence_price = opt[0].price + match_price;
                    let slot = &mut opt[pos as usize];
                    slot.mlen = pos;
                    slot.off = off_base;
                    slot.litlen = 0;
                    slot.price = sequence_price + ll0_price;
                    pos += 1;
                }
            }
            last_pos = pos - 1;
            opt[pos as usize].price = MAX_PRICE;
            shortest_path_ready = false;
            cur = 0;
        }

        if !shortest_path_ready {
            let mut jumped = false;
            cur = 1;
            while cur <= last_pos {
                let inr = ip + cur;
                {
                    let os = &*stats;
                    let ll_price = |l: u32| os.lit_length_price(l);
                    let ll_inc_price = |l: u32| ll_price(l) - ll_price(l - 1);
                    let litlen = opt[cur as usize - 1].litlen + 1;
                    let price = opt[cur as usize - 1].price
                        + os.literal_price(prefix.byte(ip + cur - 1))
                        + ll_inc_price(litlen);
                    if price <= opt[cur as usize].price {
                        let prev_match = opt[cur as usize];
                        opt[cur as usize] = opt[cur as usize - 1];
                        opt[cur as usize].litlen = litlen;
                        opt[cur as usize].price = price;
                        if opt_level >= 1
                            && prev_match.litlen == 0
                            && ll_inc_price(1) < 0
                            && ip + cur < iend
                        {
                            let next_lit = os.literal_price(prefix.byte(ip + cur));
                            let with1literal = prev_match.price + next_lit + ll_inc_price(1);
                            let with_more_literals = price + next_lit + ll_inc_price(litlen + 1);
                            if with1literal < with_more_literals
                                && with1literal < opt[cur as usize + 1].price
                            {
                                let prev = cur - prev_match.mlen;
                                let new_reps = new_rep(
                                    &opt[prev as usize].rep,
                                    prev_match.off,
                                    opt[prev as usize].litlen == 0,
                                );
                                opt[cur as usize + 1] = prev_match;
                                opt[cur as usize + 1].rep = new_reps;
                                opt[cur as usize + 1].litlen = 1;
                                opt[cur as usize + 1].price = with1literal;
                                if last_pos < cur + 1 {
                                    last_pos = cur + 1;
                                }
                            }
                        }
                    }
                }

                if opt[cur as usize].litlen == 0 {
                    let prev = cur - opt[cur as usize].mlen;
                    let new_reps = new_rep(
                        &opt[prev as usize].rep,
                        opt[cur as usize].off,
                        opt[prev as usize].litlen == 0,
                    );
                    opt[cur as usize].rep = new_reps;
                }

                if i64::from(inr) > ilimit {
                    cur += 1;
                    continue;
                }
                if cur == last_pos {
                    break;
                }
                if opt_level == 0
                    && opt[cur as usize + 1].price
                        <= opt[cur as usize].price + (BITCOST_MULTIPLIER / 2) as i32
                {
                    cur += 1;
                    continue;
                }

                let ll0 = opt[cur as usize].litlen == 0;
                let previous_price = opt[cur as usize].price;
                let base_price = previous_price + stats.lit_length_price(0);
                let cur_rep = opt[cur as usize].rep;
                let mut nb_matches = find_matches::<S, EXT, DMS>(
                    simd,
                    &mut finder,
                    &ctx,
                    &mut matches,
                    MatchQuery {
                        ip: inr,
                        rep: &cur_rep,
                        ll0,
                        length_to_beat: min_match,
                    },
                );
                opt_ldm.process(
                    &mut matches,
                    &mut nb_matches,
                    inr - istart,
                    iend - inr,
                    min_match,
                );
                if nb_matches == 0 {
                    cur += 1;
                    continue;
                }
                let longest_ml = matches[nb_matches as usize - 1].len;
                if longest_ml > sufficient_len
                    || cur + longest_ml >= OPT_NUM as u32
                    || ip + cur + longest_ml >= iend
                {
                    last_stretch.mlen = longest_ml;
                    last_stretch.off = matches[nb_matches as usize - 1].off;
                    last_stretch.litlen = 0;
                    last_pos = cur + longest_ml;
                    jumped = true;
                    break;
                }

                let os = &*stats;
                for match_nb in 0..nb_matches as usize {
                    let offset = matches[match_nb].off;
                    let last_ml = matches[match_nb].len;
                    let start_ml = if match_nb > 0 {
                        matches[match_nb - 1].len + 1
                    } else {
                        min_match
                    };
                    let off_price = os.offset_price(offset);
                    let mut mlen = last_ml;
                    while mlen >= start_ml {
                        let pos = cur + mlen;
                        let price =
                            base_price + off_price.wrapping_add(os.match_length_price(mlen)) as i32;
                        if pos > last_pos || price < opt[pos as usize].price {
                            while last_pos < pos {
                                last_pos += 1;
                                opt[last_pos as usize].price = MAX_PRICE;
                                opt[last_pos as usize].litlen = 1;
                            }
                            let slot = &mut opt[pos as usize];
                            slot.mlen = mlen;
                            slot.off = offset;
                            slot.litlen = 0;
                            slot.price = price;
                        } else if opt_level == 0 {
                            break;
                        }
                        mlen -= 1;
                    }
                }
                opt[last_pos as usize + 1].price = MAX_PRICE;
                cur += 1;
            }

            if !jumped {
                last_stretch = opt[last_pos as usize];
                cur = last_pos - last_stretch.mlen;
            }
        }

        // _shortestPath:
        if last_stretch.mlen == 0 {
            ip += last_pos;
            continue;
        }
        if last_stretch.litlen == 0 {
            *rep = new_rep(
                &opt[cur as usize].rep,
                last_stretch.off,
                opt[cur as usize].litlen == 0,
            );
        } else {
            *rep = last_stretch.rep;
            cur -= last_stretch.litlen;
        }

        let store_end = cur + 2;
        let mut store_start;
        let mut stretch_pos = cur;
        if last_stretch.litlen > 0 {
            opt[store_end as usize].litlen = last_stretch.litlen;
            opt[store_end as usize].mlen = 0;
            store_start = store_end - 1;
            opt[store_start as usize] = last_stretch;
        }
        opt[store_end as usize] = last_stretch;
        store_start = store_end;
        loop {
            let next_stretch = opt[stretch_pos as usize];
            opt[store_start as usize].litlen = next_stretch.litlen;
            if next_stretch.mlen == 0 {
                break;
            }
            store_start -= 1;
            opt[store_start as usize] = next_stretch;
            stretch_pos -= next_stretch.litlen + next_stretch.mlen;
        }

        for store_pos in store_start..=store_end {
            let llen = opt[store_pos as usize].litlen;
            let mlen = opt[store_pos as usize].mlen;
            let off_base = opt[store_pos as usize].off;
            let advance = llen + mlen;
            if mlen == 0 {
                ip = anchor + llen;
                continue;
            }
            let literals = prefix.slice(anchor, anchor + llen);
            stats.update_stats(literals, off_base, mlen);
            seq_store.store_seq(literals, off_base, mlen as usize);
            anchor += advance;
            ip = anchor;
        }
        stats.set_base_prices(opt_level);
    }

    ms.next_to_update = finder.next_to_update;
    ms.opt.price_table = opt_table;
    ms.opt.match_table = matches;
    if let Some((pos, pos_in_sequence)) = lent_cursor {
        let mut store = opt_ldm.seq_store;
        store.pos = pos;
        store.pos_in_sequence = pos_in_sequence;
        ms.ldm_seq_store = Some(store);
    }
    (iend - anchor) as usize
}

/// `ZSTD_compressBlock_btopt`.
pub(crate) fn btopt(
    ms: &mut MatchState,
    seq_store: &mut SeqStore,
    rep: &mut [u32; 3],
    block: &BlockInput<'_>,
) -> usize {
    dispatch!(block.backend.0, simd => opt_generic::<_, 0, false, false>(simd, ms, seq_store, rep, block))
}

/// `ZSTD_compressBlock_btopt_extDict`.
pub(crate) fn btopt_ext(
    ms: &mut MatchState,
    seq_store: &mut SeqStore,
    rep: &mut [u32; 3],
    block: &BlockInput<'_>,
) -> usize {
    dispatch!(block.backend.0, simd => opt_generic::<_, 0, true, false>(simd, ms, seq_store, rep, block))
}

/// `ZSTD_compressBlock_btultra`.
pub(crate) fn btultra(
    ms: &mut MatchState,
    seq_store: &mut SeqStore,
    rep: &mut [u32; 3],
    block: &BlockInput<'_>,
) -> usize {
    dispatch!(block.backend.0, simd => opt_generic::<_, 2, false, false>(simd, ms, seq_store, rep, block))
}

/// `ZSTD_compressBlock_btultra_extDict` (also btultra2 over an external
/// dictionary).
pub(crate) fn btultra_ext(
    ms: &mut MatchState,
    seq_store: &mut SeqStore,
    rep: &mut [u32; 3],
    block: &BlockInput<'_>,
) -> usize {
    dispatch!(block.backend.0, simd => opt_generic::<_, 2, true, false>(simd, ms, seq_store, rep, block))
}

/// `ZSTD_compressBlock_btultra2`: a first pass over the frame's first block
/// seeds the statistics, then the block is compressed for real.
pub(crate) fn btultra2(
    ms: &mut MatchState,
    seq_store: &mut SeqStore,
    rep: &mut [u32; 3],
    block: &BlockInput<'_>,
) -> usize {
    let curr = block.start;
    let src_size = block.end - block.start;
    // The reference's "no LDM" test only checks that no sequence was stored
    // yet; long matches offered as candidates do not disable the first pass.
    if ms.opt.lit_length_sum == 0
        && seq_store.nb_seq() == 0
        && ms.window.dict_limit == ms.window.low_limit
        && curr == ms.window.dict_limit
        && src_size as usize > PREDEF_THRESHOLD
    {
        // ZSTD_initStats_ultra.
        let mut tmp_rep = *rep;
        btultra(ms, seq_store, &mut tmp_rep, block);
        seq_store.reset();
        ms.window.base = ms.window.base.add(-i64::from(src_size));
        ms.window.dict_limit += src_size;
        ms.window.low_limit = ms.window.dict_limit;
        ms.next_to_update = ms.window.dict_limit;
        let shifted = BlockInput {
            window: mem::Window {
                prefix: block
                    .window
                    .prefix
                    .rebased(block.window.prefix.start() + src_size),
                ext: mem::Mem::new(&[], ms.window.dict_limit),
            },
            start: block.start + src_size,
            end: block.end + src_size,
            backend: block.backend,
            dms: block.dms,
            symbol_costs: block.symbol_costs,
        };
        return btultra(ms, seq_store, rep, &shifted);
    }
    btultra(ms, seq_store, rep, block)
}

/// Indexes dictionary content up to `target` into the binary tree
/// (`ZSTD_updateTree(ms, iend - HASH_READ_SIZE, iend)`).
///
/// `ZSTD_updateTree` hashes with the unclamped minimum match, so a minimum
/// match of 7 indexes 7-byte hashes, unlike the searches (at most 6).
pub(crate) fn load_dictionary(
    ms: &mut MatchState,
    window: &mem::Window<'_>,
    target: u32,
    end: u32,
) {
    let block = BlockInput {
        window: *window,
        start: target,
        end,
        backend: ms.backend,
        dms: None,
        symbol_costs: None,
    };
    let mls = ms.cparams.min_match.clamp(3, 7);
    dispatch!(block.backend.0, simd => {
        let ctx = Ctx::new(simd, &block, mls);
        let (mut finder, _) = Finder::split(ms);
        match mls {
            3 => update_tree::<_, false, 3>(&mut finder, &ctx, target),
            4 => update_tree::<_, false, 4>(&mut finder, &ctx, target),
            5 => update_tree::<_, false, 5>(&mut finder, &ctx, target),
            6 => update_tree::<_, false, 6>(&mut finder, &ctx, target),
            _ => update_tree::<_, false, 7>(&mut finder, &ctx, target),
        }
    });
    ms.next_to_update = target;
}

/// `ZSTD_compressBlock_btopt_dictMatchState`.
///
/// Without an attached dictionary in `block` this is [`btopt`], the
/// variant `ZSTD_matchState_dictMode` selects then.
pub(crate) fn btopt_dms(
    ms: &mut MatchState,
    seq_store: &mut SeqStore,
    rep: &mut [u32; 3],
    block: &BlockInput<'_>,
) -> usize {
    if block.dms.is_none() {
        return btopt(ms, seq_store, rep, block);
    }
    dispatch!(block.backend.0, simd => opt_generic::<_, 0, false, true>(simd, ms, seq_store, rep, block))
}

/// `ZSTD_compressBlock_btultra_dictMatchState` (also btultra2's, which has
/// no variant of its own for an attached dictionary).
///
/// Without an attached dictionary in `block` this is [`btultra`].
pub(crate) fn btultra_dms(
    ms: &mut MatchState,
    seq_store: &mut SeqStore,
    rep: &mut [u32; 3],
    block: &BlockInput<'_>,
) -> usize {
    if block.dms.is_none() {
        return btultra(ms, seq_store, rep, block);
    }
    dispatch!(block.backend.0, simd => opt_generic::<_, 2, false, true>(simd, ms, seq_store, rep, block))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Backend;
    use crate::shared::format::BLOCK_SIZE_MAX;
    use alloc::vec;

    /// The reference's `ZSTD_litLengthPrice`, from the frequencies.
    fn reference_lit_length_price(os: &OptState, lit_length: u32, opt_level: u32) -> i32 {
        if os.price_type == PriceType::Predef {
            return weight(lit_length, opt_level) as i32;
        }
        if lit_length as usize == BLOCK_SIZE_MAX {
            return BITCOST_MULTIPLIER as i32
                + reference_lit_length_price(os, lit_length - 1, opt_level);
        }
        let code = usize::from(ll_code(lit_length));
        (u32::from(LL_BITS[code]) * BITCOST_MULTIPLIER + os.lit_length_sum_base_price)
            .wrapping_sub(weight(os.lit_length_freq[code], opt_level)) as i32
    }

    /// The reference's `ZSTD_getMatchPrice`, from the frequencies.
    fn reference_match_price(os: &OptState, off_base: u32, length: u32, opt_level: u32) -> u32 {
        let off_code = highbit32(off_base);
        let ml_base = length - MIN_MATCH as u32;
        if os.price_type == PriceType::Predef {
            return weight(ml_base, opt_level) + (16 + off_code) * BITCOST_MULTIPLIER;
        }
        let mut price = off_code * BITCOST_MULTIPLIER
            + os.off_code_sum_base_price
                .wrapping_sub(weight(os.off_code_freq[off_code as usize], opt_level));
        if opt_level < 2 && off_code >= 20 {
            price += (off_code - 19) * 2 * BITCOST_MULTIPLIER;
        }
        let code = usize::from(ml_code(ml_base));
        price = price.wrapping_add(
            u32::from(ML_BITS[code]) * BITCOST_MULTIPLIER
                + os.match_length_sum_base_price
                    .wrapping_sub(weight(os.match_length_freq[code], opt_level)),
        );
        price + BITCOST_MULTIPLIER / 5
    }

    /// The reference's `ZSTD_rawLiteralsCost` for one literal.
    fn reference_literal_price(os: &OptState, literal: u8, opt_level: u32) -> i32 {
        if !os.compressed_literals() {
            return (8 * BITCOST_MULTIPLIER) as i32;
        }
        if os.price_type == PriceType::Predef {
            return (6 * BITCOST_MULTIPLIER) as i32;
        }
        let max = os.lit_sum_base_price - BITCOST_MULTIPLIER;
        let price = weight(os.lit_freq[usize::from(literal)], opt_level).min(max);
        os.lit_sum_base_price.wrapping_sub(price) as i32
    }

    fn assert_prices_match(os: &OptState, opt_level: u32) {
        for literal in 0..=u8::MAX {
            assert_eq!(
                os.literal_price(literal),
                reference_literal_price(os, literal, opt_level),
                "literal {literal}"
            );
        }
        let lengths = (0..300u32).chain([1000, 65_535, 65_536, 131_071, BLOCK_SIZE_MAX as u32]);
        for lit_length in lengths {
            assert_eq!(
                os.lit_length_price(lit_length),
                reference_lit_length_price(os, lit_length, opt_level),
                "literal length {lit_length}"
            );
        }
        let offsets = (1..70u32).chain((4..32).map(|bit| (1u32 << bit) + 5));
        for off_base in offsets {
            for length in (3..200u32).chain([1000, 65_538, 131_074]) {
                assert_eq!(
                    os.match_price(off_base, length),
                    reference_match_price(os, off_base, length, opt_level),
                    "offset {off_base} length {length}"
                );
            }
        }
    }

    fn sample(len: usize, seed: u32) -> Vec<u8> {
        let mut state = seed;
        (0..len)
            .map(|_| {
                state = state.wrapping_mul(1_103_515_245).wrapping_add(12_345);
                (state >> 24) as u8 & 0x3f
            })
            .collect()
    }

    #[test]
    fn price_tables_match_the_reference_formulas() {
        for opt_level in [0, 2] {
            for literal_compression in [ParamSwitch::Auto, ParamSwitch::Disable] {
                let mut os = OptState {
                    literal_compression,
                    ..OptState::default()
                };
                os.allocate();
                let src = sample(4096, opt_level + 1);
                os.rescale_freqs(&src, opt_level, None);
                assert_eq!(os.price_type, PriceType::Dynamic);
                assert_prices_match(&os, opt_level);
                // Statistics updated sequence by sequence, then rescaled.
                for (round, chunk) in src.chunks(37).enumerate() {
                    let round = round as u32;
                    let off_base = if round.is_multiple_of(3) {
                        2
                    } else {
                        4 + (round << (round % 23))
                    };
                    let literals = &chunk[..(round as usize % 37).min(chunk.len())];
                    os.update_stats(literals, off_base, 3 + round * 7);
                    os.set_base_prices(opt_level);
                }
                assert_prices_match(&os, opt_level);
                // Runs long enough to take every weight at once, around
                // the threshold, then short ones again.
                for (round, len) in [64usize, 63, 200, 1000, 65, 3].into_iter().enumerate() {
                    let literals = &src[round * 7..round * 7 + len];
                    os.update_stats(literals, 4 + round as u32, 3 + len as u32);
                    os.set_base_prices(opt_level);
                    assert_prices_match(&os, opt_level);
                }
                os.rescale_freqs(&src, opt_level, None);
                assert_prices_match(&os, opt_level);
            }
        }
    }

    #[test]
    fn finder_bounds_match_the_match_state() {
        use super::super::params::{ParamSwitch as Switch, level_cparams};
        for (level, loaded_dict_end) in [(16, 0), (19, 0), (22, 0), (19, 7)] {
            let mut ms = MatchState::new(level_cparams(level, 1 << 20), Switch::Disable);
            ms.window.low_limit = 5;
            ms.window.dict_limit = 9;
            ms.next_to_update = 40;
            ms.loaded_dict_end = loaded_dict_end;
            let cparams = ms.cparams;
            let currs = [
                5u32,
                9,
                100,
                1 << 20,
                (1 << 20) + 6,
                (1 << 21) + 3,
                u32::MAX - 9,
            ];
            let expected = currs.map(|curr| ms.lowest_match_index(curr, cparams.window_log));
            let (finder, _) = Finder::split(&mut ms);
            assert_eq!(currs.map(|curr| finder.lowest_match_index(curr)), expected);
            assert_eq!(finder.chain_table.len(), 1 << cparams.chain_log);
            assert_eq!(finder.bt_mask, (1 << (cparams.chain_log - 1)) - 1);
            assert_eq!(finder.nb_compares, 1 << cparams.search_log);
            assert_eq!(
                finder.sufficient_len,
                cparams.target_length.min(OPT_NUM as u32 - 1)
            );
            assert_eq!((finder.next_to_update, finder.next_to_update3), (40, 40));
        }
    }

    #[test]
    fn prefetching_ahead_skips_positions_past_the_block() {
        use super::super::params::{ParamSwitch as Switch, level_cparams};
        let bytes = sample(64, 3);
        let mut ms = MatchState::new(level_cparams(19, 64), Switch::Disable);
        let (finder, _) = Finder::split(&mut ms);
        for backend in Backend::available() {
            let block = BlockInput {
                window: mem::Window {
                    prefix: mem::Mem::new(&bytes, 100),
                    ext: mem::Mem::new(&[], 100),
                },
                start: 100,
                end: 164,
                backend,
                dms: None,
                symbol_costs: None,
            };
            dispatch!(backend.0, simd => {
                let ctx = Ctx::new(simd, &block, 3);
                // Hints only: every position up to the block's end, words
                // whole or cut, leaves the tables as they were.
                for ip in 100..164 {
                    ctx.prefetch_ahead::<3, true>(&finder, ip);
                    ctx.prefetch_ahead::<4, false>(&finder, ip);
                    ctx.prefetch_ahead::<5, false>(&finder, ip);
                    ctx.prefetch_ahead::<6, false>(&finder, ip);
                }
            });
        }
        assert!(finder.hash_table.iter().all(|&cell| cell == 0));
        assert!(finder.hash_table3.iter().all(|&cell| cell == 0));
    }

    #[test]
    fn table_weights_match_the_integer_weights() {
        // Every power of two and its neighbours up to 2^24, then values the
        // float form cannot hold, which take the integer fallback.
        let mut stats: Vec<u32> = (0..32)
            .flat_map(|bit| {
                let p = 1u32 << bit;
                [p.wrapping_sub(2), p.wrapping_sub(1), p, p.wrapping_add(1)]
            })
            .filter(|&stat| stat < (1 << 24))
            .collect();
        stats.extend(0..5000);
        for opt_level in [0, 2] {
            let mut out = vec![0u32; stats.len()];
            weights(&stats, opt_level, &mut out);
            for (&stat, &got) in stats.iter().zip(&out) {
                assert_eq!(
                    got,
                    weight(stat, opt_level),
                    "stat {stat} level {opt_level}"
                );
            }
            let large = [1u32 << 24, (1 << 24) + 1, u32::MAX - 1, 7];
            let mut out = [0u32; 4];
            weights(&large, opt_level, &mut out);
            for (&stat, &got) in large.iter().zip(&out) {
                assert_eq!(
                    got,
                    weight(stat, opt_level),
                    "stat {stat} level {opt_level}"
                );
            }
        }
    }

    #[test]
    fn position_tables_grow_with_the_block_up_to_the_parser_window() {
        let mut os = OptState::default();
        os.allocate();
        assert_eq!(os.table_bytes(), 0);
        os.reserve_matches(44, 1 << 12);
        assert_eq!(os.match_table.len(), 47);
        // A smaller block keeps the larger table.
        os.reserve_matches(5, 1 << 12);
        assert_eq!(os.match_table.len(), 47);
        // Few comparisons bound the list below the block.
        os.reserve_matches(BLOCK_SIZE_MAX, 1 << 4);
        assert_eq!(os.match_table.len(), 47);
        os.reserve_matches(BLOCK_SIZE_MAX, 1 << 6);
        assert_eq!(os.match_table.len(), 69);
        os.reserve_matches(BLOCK_SIZE_MAX, 1 << 13);
        assert_eq!(os.match_table.len(), OPT_SIZE);
        // Positions follow the block's remaining length.
        let mut table = core::mem::take(&mut os.price_table);
        assert_eq!(grow_positions(&mut table, 3).len(), 3);
        grow_positions(&mut table, 40);
        assert_eq!(grow_positions(&mut table, 12).len(), 40);
        os.price_table = table;
        assert!(os.table_bytes() >= 40 * core::mem::size_of::<Optimal>());
    }

    #[test]
    fn tiny_blocks_use_predefined_prices() {
        for opt_level in [0, 2] {
            let mut os = OptState::default();
            os.allocate();
            os.rescale_freqs(b"tiny", opt_level, None);
            assert_eq!(os.price_type, PriceType::Predef);
            assert_prices_match(&os, opt_level);
        }
    }

    #[test]
    fn tree_comparisons_read_both_segments_like_the_reference() {
        // Repetitive bytes so that runs cross the segment boundary.
        let ext: Vec<u8> = (0..64u32).map(|i| (i % 5) as u8).collect();
        let mut prefix: Vec<u8> = (0..96u32).map(|i| (i % 5) as u8).collect();
        prefix[70] = 9;
        let dict_limit = 1000u32;
        let window = mem::Window {
            prefix: mem::Mem::new(&prefix, dict_limit),
            ext: mem::Mem::new(&ext, dict_limit - 64),
        };
        let end = dict_limit + prefix.len() as u32;
        for backend in Backend::available() {
            let block = BlockInput {
                window,
                start: dict_limit,
                end,
                backend,
                dms: None,
                symbol_costs: None,
            };
            dispatch!(backend.0, simd => {
                let ctx = Ctx::new(simd, &block, 4);
                for curr in dict_limit + 10..dict_limit + 40 {
                    let current = ctx.current(curr);
                    for candidate in dict_limit - 64..curr {
                        let full = window.count_virtual(curr, candidate, end);
                        for known in [0, 1, 7, 8, 9, 30].map(|k: usize| k.min(full)) {
                            let (length, smaller) =
                                ctx.extend::<true>(current, curr, candidate, known);
                            assert_eq!(length, full, "{backend} {curr} {candidate} {known}");
                            let expected = curr + (full as u32) < end
                                && window.byte_at(candidate + full as u32)
                                    < window.byte_at(curr + full as u32);
                            assert_eq!(smaller, expected, "{backend} {curr} {candidate}");
                            if candidate >= dict_limit {
                                let plain = ctx.extend::<false>(current, curr, candidate, known);
                                assert_eq!(plain, (length, smaller));
                            }
                        }
                    }
                }
            });
        }
    }

    #[test]
    fn specialized_hashes_match_the_shared_ones() {
        let bytes = sample(64, 7);
        for backend in Backend::available() {
            let block = BlockInput {
                window: mem::Window {
                    prefix: mem::Mem::new(&bytes, 100),
                    ext: mem::Mem::new(&[], 100),
                },
                start: 100,
                end: 164,
                backend,
                dms: None,
                symbol_costs: None,
            };
            let prefix = &block.window.prefix;
            dispatch!(backend.0, simd => {
                let ctx = Ctx::new(simd, &block, 4);
                for index in 100..156 {
                    for hash_log in [10, 17, 24] {
                        let expected = |mls| mem::hash_ptr(prefix, index, hash_log, mls);
                        assert_eq!(ctx.hash::<3>(index, hash_log), expected(3));
                        assert_eq!(ctx.hash::<4>(index, hash_log), expected(4));
                        assert_eq!(ctx.hash::<5>(index, hash_log), expected(5));
                        assert_eq!(ctx.hash::<6>(index, hash_log), expected(6));
                        assert_eq!(ctx.hash::<7>(index, hash_log), expected(7));
                    }
                }
            });
        }
    }

    // -----------------------------------------------------------------------
    // Dictionaries.

    use super::super::cctx::{Engine, RequestedParams, compress_bound};
    use super::super::cdict::{AttachPref, CDict};
    use super::super::compress_once_into;
    use super::super::dict_tests::{check, dict_params, inputs, request, trained};
    use super::super::params::Strategy;
    use super::super::stream::{EndOp, Stream};
    use crate::dictionary::DictionaryContent;
    use alloc::boxed::Box;
    use zstd_ffi::corpus;
    use zstd_ffi::oracle::{self, Directive};

    /// A raw-content dictionary: text and some of the records.
    fn raw_dictionary(records: &[Vec<u8>]) -> Vec<u8> {
        [corpus::text(12_000, 9), records[..100].concat()].concat()
    }

    /// Prefixes of the last record, from empty to a few dozen bytes.
    fn tiny_inputs(records: &[Vec<u8>]) -> Vec<Vec<u8>> {
        let record = records[records.len() - 1].repeat(2);
        [0usize, 1, 3, 5, 8, 9, 12, 16, 20, 33, 47, 64]
            .into_iter()
            .map(|len| record[..len.min(record.len())].to_vec())
            .collect()
    }

    /// A handful of record inputs and tiny ones, for wide parameter sweeps.
    fn few_inputs(records: &[Vec<u8>]) -> Vec<Vec<u8>> {
        let mut out: Vec<Vec<u8>> = [1usize, 3, 40, 300]
            .into_iter()
            .map(|count| records[records.len() - count..].concat())
            .collect();
        out.push(corpus::text(9_000, 5));
        out.extend(tiny_inputs(records).into_iter().step_by(3));
        out
    }

    /// [`check`] on every backend the host offers, each with its own
    /// engine, with the requested maximum block size.
    /// `ZSTD_c_maxBlockSize`, which the shared oracle parameters omit.
    const ZSTD_C_MAX_BLOCK_SIZE: zstd_ffi::ZSTD_cParameter = 1015;

    fn check_backends(requested: &RequestedParams, dict: &[u8], inputs: &[Vec<u8>]) {
        let content = DictionaryContent::Auto;
        let cdict = CDict::new(Box::from(dict), content, requested).expect("prepares");
        let mut params = dict_params(requested, dict, content);
        if requested.max_block_size != 0 {
            params = params.with(ZSTD_C_MAX_BLOCK_SIZE, requested.max_block_size as i32);
        }
        for backend in Backend::available() {
            let mut engine = Engine::with_backend(backend);
            for (index, input) in inputs.iter().enumerate() {
                let expected = oracle::compress2(input, &params).expect("oracle");
                let mut dst = vec![0u8; compress_bound(input.len()).expect("bound")];
                let size =
                    compress_once_into(&mut engine, requested, Some(&cdict), input, 3, &mut dst)
                        .expect("compresses");
                assert!(
                    dst[..size] == expected[..],
                    "{backend} input {index}, {requested:?}"
                );
            }
        }
    }

    /// Streams `input` in chunks with a flush in the middle, with `cdict`.
    fn stream_with_dictionary(
        engine: &mut Engine,
        requested: &RequestedParams,
        cdict: &CDict,
        steps: &[(&[u8], Directive)],
    ) -> Vec<u8> {
        let mut state = Stream::new();
        let mut out = Vec::new();
        let mut buffer = vec![0u8; 1 << 17];
        for &(data, directive) in steps {
            let op = match directive {
                Directive::Continue => EndOp::Continue,
                Directive::Flush => EndOp::Flush,
                Directive::End => EndOp::End,
            };
            let mut in_pos = 0;
            loop {
                let step = state
                    .compress(
                        engine,
                        requested,
                        Some(cdict),
                        &mut buffer[..],
                        &data[in_pos..],
                        op,
                    )
                    .expect("stream");
                in_pos += step.consumed;
                out.extend_from_slice(&buffer[..step.produced]);
                if in_pos == data.len() && (op == EndOp::Continue || step.remaining == 0) {
                    break;
                }
            }
        }
        out
    }

    #[test]
    fn attached_trained_dictionaries_match_the_reference_at_optimal_levels() {
        let (dict, records) = trained();
        let inputs = [inputs(&records), tiny_inputs(&records)].concat();
        // From level 13 on, a dictionary of this size resolves to the
        // optimal parser.
        for level in 13..=22 {
            let requested = request(level, None, AttachPref::ForceAttach);
            check(&requested, &dict, DictionaryContent::Auto, &inputs);
        }
    }

    #[test]
    fn attached_raw_dictionaries_match_the_reference_at_optimal_levels() {
        let (trained, records) = trained();
        let raw = raw_dictionary(&records);
        let inputs = inputs(&records);
        for level in 13..=22 {
            let requested = request(level, None, AttachPref::ForceAttach);
            check(&requested, &raw, DictionaryContent::Auto, &inputs);
            // A trained dictionary read as raw content: no entropy tables.
            check(&requested, &trained, DictionaryContent::Raw, &inputs);
        }
    }

    #[test]
    fn default_attachment_matches_the_reference_at_optimal_levels() {
        let (dict, records) = trained();
        let raw = raw_dictionary(&records);
        let inputs = [inputs(&records), tiny_inputs(&records)].concat();
        for level in 13..=22 {
            let requested = request(level, None, AttachPref::Default);
            check(&requested, &dict, DictionaryContent::Auto, &inputs);
            check(&requested, &raw, DictionaryContent::Auto, &inputs);
        }
    }

    #[test]
    fn copied_and_loaded_trained_dictionaries_seed_statistics_like_the_reference() {
        let (dict, records) = trained();
        let inputs = [inputs(&records), tiny_inputs(&records)].concat();
        for level in 11..=22 {
            for pref in [AttachPref::ForceCopy, AttachPref::ForceLoad] {
                let requested = request(level, None, pref);
                check(&requested, &dict, DictionaryContent::Auto, &inputs);
            }
        }
    }

    #[test]
    fn explicit_optimal_strategies_match_the_reference_with_dictionaries() {
        let (dict, records) = trained();
        let raw = raw_dictionary(&records);
        let inputs = few_inputs(&records);
        for strategy in [Strategy::BtOpt, Strategy::BtUltra, Strategy::BtUltra2] {
            for level in [3, 11, 12, 19] {
                for min_match in [3, 4, 5] {
                    for pref in [AttachPref::ForceAttach, AttachPref::ForceCopy] {
                        let mut requested = request(level, Some(strategy), pref);
                        requested.cparams.min_match = min_match;
                        check(&requested, &dict, DictionaryContent::Auto, &inputs);
                    }
                    let mut requested = request(level, Some(strategy), AttachPref::ForceAttach);
                    requested.cparams.min_match = min_match;
                    check(&requested, &raw, DictionaryContent::Auto, &inputs);
                }
            }
        }
    }

    /// Dictionary loads hash 7 bytes for a minimum match of 7, which the
    /// searches clamp to 6.
    #[test]
    fn dictionaries_load_with_a_minimum_match_of_seven_like_the_reference() {
        let (dict, records) = trained();
        let raw = raw_dictionary(&records);
        let inputs = few_inputs(&records);
        for (strategy, prefs) in [
            (
                Strategy::BtOpt,
                &[
                    AttachPref::ForceAttach,
                    AttachPref::ForceCopy,
                    AttachPref::ForceLoad,
                ][..],
            ),
            (
                Strategy::BtUltra2,
                &[AttachPref::ForceAttach, AttachPref::ForceCopy][..],
            ),
            (
                Strategy::BtLazy2,
                &[
                    AttachPref::ForceAttach,
                    AttachPref::ForceCopy,
                    AttachPref::ForceLoad,
                ][..],
            ),
        ] {
            for &pref in prefs {
                for level in [12, 19] {
                    let mut requested = request(level, Some(strategy), pref);
                    requested.cparams.min_match = 7;
                    check(&requested, &dict, DictionaryContent::Auto, &inputs);
                    check(&requested, &raw, DictionaryContent::Auto, &inputs);
                }
            }
        }
    }

    #[test]
    fn small_blocks_carry_dictionary_statistics_like_the_reference() {
        let (dict, records) = trained();
        let inputs = [
            records[records.len() - 300..].concat(),
            corpus::mixed(40_000, 3),
        ];
        for level in [13, 16, 19, 22] {
            for pref in [
                AttachPref::ForceAttach,
                AttachPref::ForceCopy,
                AttachPref::ForceLoad,
            ] {
                let mut requested = request(level, None, pref);
                requested.max_block_size = 1 << 12;
                check_backends(&requested, &dict, &inputs);
            }
        }
    }

    #[test]
    fn long_distance_matching_with_dictionaries_matches_the_reference() {
        let (dict, records) = trained();
        let base = records[records.len() - 600..].concat();
        let input = [base.clone(), corpus::binary(150_000, 4), base].concat();
        let inputs = [input, records[records.len() - 40..].concat()];
        for level in [16, 19, 22] {
            for pref in [AttachPref::ForceAttach, AttachPref::ForceCopy] {
                let mut requested = request(level, None, pref);
                requested.ldm.enable = ParamSwitch::Enable;
                requested.cparams.window_log = 24;
                check(&requested, &dict, DictionaryContent::Auto, &inputs);
            }
        }
    }

    #[test]
    fn every_backend_matches_the_reference_with_attached_dictionaries() {
        let (dict, records) = trained();
        let raw = raw_dictionary(&records);
        let inputs = few_inputs(&records);
        for (level, strategy) in [
            (13, None),
            (16, None),
            (19, None),
            (22, None),
            (12, Some(Strategy::BtUltra2)),
        ] {
            let requested = request(level, strategy, AttachPref::ForceAttach);
            check_backends(&requested, &dict, &inputs);
            check_backends(&requested, &raw, &inputs);
            let requested = request(level, strategy, AttachPref::ForceCopy);
            check_backends(&requested, &dict, &inputs);
        }
    }

    #[test]
    fn streaming_with_an_attached_dictionary_matches_the_reference() {
        let (dict, records) = trained();
        let input = records[records.len() - 1200..].concat();
        let chunks: Vec<&[u8]> = input.chunks(7_001).collect();
        let steps: Vec<(&[u8], Directive)> = chunks
            .iter()
            .enumerate()
            .map(|(n, &data)| {
                let directive = if n + 1 == chunks.len() {
                    Directive::End
                } else if n == chunks.len() / 2 {
                    Directive::Flush
                } else {
                    Directive::Continue
                };
                (data, directive)
            })
            .collect();
        for level in [13, 16, 19, 22] {
            for pref in [AttachPref::Default, AttachPref::ForceAttach] {
                let requested = request(level, None, pref);
                let content = DictionaryContent::Auto;
                let cdict =
                    CDict::new(Box::from(&dict[..]), content, &requested).expect("prepares");
                let expected =
                    oracle::compress_stream(&dict_params(&requested, &dict, content), &steps)
                        .expect("oracle");
                for backend in Backend::available() {
                    let mut engine = Engine::with_backend(backend);
                    let ours = stream_with_dictionary(&mut engine, &requested, &cdict, &steps);
                    assert!(ours == expected, "{backend} level {level} {pref:?}");
                }
            }
        }
    }

    #[test]
    fn dictionary_variants_without_a_dictionary_parse_like_the_plain_ones() {
        use super::super::params::{ParamSwitch as Switch, level_cparams};
        let bytes = corpus::text(20_000, 2);
        let block = BlockInput {
            window: mem::Window {
                prefix: mem::Mem::new(&bytes, 2),
                ext: mem::Mem::new(&[], 2),
            },
            start: 2,
            end: 2 + bytes.len() as u32,
            backend: Backend::default(),
            dms: None,
            symbol_costs: None,
        };
        use super::super::match_state::BlockCompressor;
        let pairs: [(BlockCompressor, BlockCompressor, i32); 2] =
            [(btopt, btopt_dms, 16), (btultra, btultra_dms, 18)];
        for (plain, dms, level) in pairs {
            let cparams = level_cparams(level, bytes.len() as u64);
            let mut outputs = Vec::new();
            for compressor in [plain, dms] {
                let mut ms = MatchState::new(cparams, Switch::Disable);
                let mut seq_store = SeqStore::default();
                seq_store.reserve(bytes.len(), cparams.min_match);
                let mut rep = [1, 4, 8];
                let last = compressor(&mut ms, &mut seq_store, &mut rep, &block);
                outputs.push((last, rep, seq_store.sequences, seq_store.literals));
            }
            assert_eq!(outputs[0], outputs[1], "level {level}");
        }
    }

    #[test]
    fn dictionary_runs_continue_at_the_prefix_like_the_reference() {
        let dict_bytes: Vec<u8> = (0..300u32).map(|i| (i % 7) as u8).collect();
        let requested = request(19, Some(Strategy::BtUltra), AttachPref::ForceAttach);
        let cdict = CDict::new(
            Box::from(&dict_bytes[..]),
            DictionaryContent::Raw,
            &requested,
        )
        .expect("prepares");
        let dms = cdict.match_state();
        let dict = dms.window.prefix;
        let mut prefix: Vec<u8> = (0..120u32).map(|i| (i % 7) as u8).collect();
        prefix[90] = 50;
        let dict_limit = 1000u32;
        let block = BlockInput {
            window: mem::Window {
                prefix: mem::Mem::new(&prefix, dict_limit),
                ext: mem::Mem::new(&[], dict_limit),
            },
            start: dict_limit,
            end: dict_limit + prefix.len() as u32,
            backend: Backend::default(),
            dms: Some(dms),
            symbol_costs: None,
        };
        // The virtual run: the dictionary from `candidate`, then the prefix
        // from its start.
        let expected = |ip: u32, candidate: u32| {
            let virtual_bytes: Vec<u8> = dict
                .slice(candidate.min(dict.end()), dict.end())
                .iter()
                .chain(prefix.iter())
                .copied()
                .collect();
            if candidate > dict.end() {
                return 0;
            }
            prefix[(ip - dict_limit) as usize..]
                .iter()
                .zip(&virtual_bytes)
                .take_while(|(a, b)| a == b)
                .count()
        };
        for backend in Backend::available() {
            dispatch!(backend.0, simd => {
                let search = DictSearch::new(&dms);
                let ctx = Ctx::with_dictionary(simd, &block, 4, &search);
                assert_eq!(ctx.dms.high_limit, dict.end());
                assert_eq!(ctx.dms.bytes.len(), dict_bytes.len());
                for ip in dict_limit..dict_limit + 100 {
                    for candidate in (dict.start()..dict.end() + 3).step_by(5) {
                        assert_eq!(
                            ctx.count_dictionary(ip, candidate),
                            expected(ip, candidate),
                            "{backend} {ip} {candidate}"
                        );
                    }
                }
            });
        }
    }

    #[test]
    fn dictionary_tables_seed_the_first_block_statistics() {
        let mut tables = Tables::default();
        tables.huf.repeat = huf::Repeat::Valid;
        tables.huf.table.max_symbol_value = 255;
        for (symbol, bits) in tables.huf.table.nb_bits.iter_mut().enumerate() {
            // Absent symbols and every length up to the scale.
            *bits = (symbol % 12) as u8;
        }
        // Low-probability, absent and larger symbols.
        let normalized = |last: u32| {
            let mut norm: Vec<i16> = (0..=last as usize)
                .map(|s| [1, -1, 0, 3, 9][s % 5])
                .collect();
            let total: i16 = norm.iter().map(|&n| n.abs()).sum();
            norm[0] += 256 - total;
            norm
        };
        tables.fse.matchlength.build(&normalized(MAX_ML), MAX_ML, 8);
        tables.fse.litlength.build(&normalized(MAX_LL), MAX_LL, 8);
        tables.fse.offcode.build(&normalized(MAX_OFF), MAX_OFF, 8);
        let frequency = |bits: u32, scale: u32| {
            if bits == 0 {
                1
            } else {
                1u32.wrapping_shl(scale.wrapping_sub(bits))
            }
        };
        for literal_compression in [ParamSwitch::Auto, ParamSwitch::Disable] {
            for (src, opt_level) in [(&b"tiny"[..], 0), (&[7u8; 300][..], 2)] {
                let mut os = OptState {
                    literal_compression,
                    ..OptState::default()
                };
                os.allocate();
                os.rescale_freqs(src, opt_level, Some(&tables));
                // Dynamic prices even for a tiny block.
                assert_eq!(os.price_type, PriceType::Dynamic);
                if literal_compression == ParamSwitch::Auto {
                    for literal in 0..=MAX_LIT {
                        let bits = u32::from(tables.huf.table.nb_bits[literal as usize]);
                        assert_eq!(os.lit_freq[literal as usize], frequency(bits, 11));
                    }
                    let sum = os.lit_freq.iter().fold(0u32, |sum, &f| sum.wrapping_add(f));
                    assert_eq!(os.lit_sum, sum);
                }
                for (freqs, table, sum) in [
                    (
                        &os.lit_length_freq,
                        &tables.fse.litlength,
                        os.lit_length_sum,
                    ),
                    (
                        &os.match_length_freq,
                        &tables.fse.matchlength,
                        os.match_length_sum,
                    ),
                    (&os.off_code_freq, &tables.fse.offcode, os.off_code_sum),
                ] {
                    for (symbol, &freq) in freqs.iter().enumerate() {
                        let tt = table.symbol_tt[symbol];
                        let bits = (tt.delta_nb_bits + 0xFFFF) >> 16;
                        assert_eq!(freq, frequency(bits, 10), "symbol {symbol}");
                    }
                    assert_eq!(sum, freqs.iter().sum::<u32>());
                }
                assert_prices_match(&os, opt_level);
                // Later blocks scale the statistics down as without one.
                os.rescale_freqs(src, opt_level, Some(&tables));
                assert_prices_match(&os, opt_level);
            }
        }
        // Tables a dictionary did not validate leave the plain seeding.
        tables.huf.repeat = huf::Repeat::Check;
        let mut os = OptState::default();
        os.allocate();
        os.rescale_freqs(b"tiny", 0, Some(&tables));
        assert_eq!(os.price_type, PriceType::Predef);
        assert_eq!(os.match_length_sum, MAX_ML + 1);
    }
}