zipora 3.1.2

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
//! BMI2 Hardware Acceleration for Rank/Select Operations
//!
//! This module implements advanced BMI2 instruction optimizations for ultra-fast
//! rank/select operations. Based on research from high-performance succinct
//! data structure libraries, it leverages modern CPU instructions for
//! significant performance improvements.
//!
//! # Key Features
//!
//! - **PDEP/PEXT Instructions**: Parallel bit deposit and extract for O(1) select
//! - **BZHI Optimization**: Zero high bits for fast trailing population count
//! - **LZCNT/TZCNT**: Leading/trailing zero count for bit manipulation
//! - **POPCNT**: Hardware population count with SIMD variants
//! - **Runtime Detection**: Automatic fallback for older CPUs
//!
//! # BMI2 Instructions Used
//!
//! ```text
//! PDEP (Parallel Bits Deposit):
//!   _pdep_u64(src, mask) - Deposit bits from src into positions set in mask
//!   
//! PEXT (Parallel Bits Extract):
//!   _pext_u64(src, mask) - Extract bits from src at positions set in mask
//!   
//! BZHI (Zero High Bits):
//!   _bzhi_u64(src, index) - Zero out bits above index
//!   
//! LZCNT (Leading Zero Count):
//!   _lzcnt_u64(src) - Count leading zeros
//!   
//! TZCNT (Trailing Zero Count):
//!   _tzcnt_u64(src) - Count trailing zeros
//! ```
//!
//! # Performance Benefits
//!
//! - **Select Operations**: 5-10x faster with PDEP
//! - **Range Queries**: 3-5x faster with BZHI
//! - **Bit Manipulation**: 2-4x faster with PEXT
//! - **Population Count**: 2-3x faster with vectorized POPCNT

use crate::error::{Result, ZiporaError};
use crate::succinct::rank_select::SimdCapabilities;

/// BMI2 instruction set capabilities
#[derive(Debug, Clone)]
pub struct Bmi2Capabilities {
    /// BMI1 instructions available (LZCNT, TZCNT, POPCNT)
    pub has_bmi1: bool,
    /// BMI2 instructions available (PDEP, PEXT, BZHI, etc.)
    pub has_bmi2: bool,
    /// Advanced SIMD capabilities
    pub simd_caps: SimdCapabilities,
}

impl Bmi2Capabilities {
    /// Detect BMI2 capabilities at runtime
    pub fn detect() -> Self {
        let simd_caps = SimdCapabilities::detect();

        Self {
            has_bmi1: simd_caps.cpu_features.has_popcnt, // BMI1 includes POPCNT
            has_bmi2: simd_caps.cpu_features.has_bmi2,
            simd_caps,
        }
    }

    /// Get a cached instance of BMI2 capabilities
    #[inline]
    pub fn get() -> &'static Self {
        use std::sync::OnceLock;
        static BMI2_CAPS: OnceLock<Bmi2Capabilities> = OnceLock::new();
        BMI2_CAPS.get_or_init(Self::detect)
    }
}

/// Ultra-fast rank operations using BMI2 instructions
pub struct Bmi2RankOps;

/// Prefetch operations for cache optimization
pub struct Bmi2PrefetchOps;

impl Bmi2PrefetchOps {
    /// Prefetch bit data into L1 cache
    #[inline]
    pub fn prefetch_bit_data(bit_data: &[u64], word_index: usize) {
        #[cfg(target_arch = "x86_64")]
        {
            if word_index < bit_data.len() {
                //// SAFETY: Pointer derived from valid slice, word_index < bit_data.len() checked above
                unsafe {
                    let ptr = bit_data.as_ptr().add(word_index) as *const i8;
                    std::arch::x86_64::_mm_prefetch::<{ std::arch::x86_64::_MM_HINT_T0 }>(ptr);
                }
            }
        }
    }

    /// Prefetch rank cache data
    #[inline]
    pub fn prefetch_rank_cache(rank_cache: &[u32], block_index: usize) {
        #[cfg(target_arch = "x86_64")]
        {
            if block_index < rank_cache.len() {
                //// SAFETY: Pointer derived from valid slice, block_index < rank_cache.len() checked above
                unsafe {
                    let ptr = rank_cache.as_ptr().add(block_index) as *const i8;
                    std::arch::x86_64::_mm_prefetch::<{ std::arch::x86_64::_MM_HINT_T0 }>(ptr);
                }
            }
        }
    }

    /// Prefetch multiple cache lines ahead for sequential access
    #[inline]
    pub fn prefetch_sequential(bit_data: &[u64], start_word: usize, prefetch_distance: usize) {
        #[cfg(target_arch = "x86_64")]
        {
            let end_word = (start_word + prefetch_distance).min(bit_data.len());
            for word_idx in (start_word..end_word).step_by(8) {
                //// SAFETY: Pointer derived from valid slice, word_idx < end_word ≤ bit_data.len() guaranteed by loop bounds
                // Prefetch every 8 words (512 bytes = 8 cache lines)
                unsafe {
                    let ptr = bit_data.as_ptr().add(word_idx) as *const i8;
                    std::arch::x86_64::_mm_prefetch::<{ std::arch::x86_64::_MM_HINT_T0 }>(ptr);
                }
            }
        }
    }
}

impl Bmi2RankOps {
    /// Fast population count using hardware POPCNT
    #[inline]
    pub fn popcount_u64(x: u64) -> u32 {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi1 {
                //// SAFETY: has_bmi1 guarantees popcnt availability
                return unsafe { Self::popcount_hardware(x) };
            }
        }

        // Fallback to software implementation
        x.count_ones()
    }

    /// Hardware POPCNT implementation
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "popcnt")]
    #[inline]
    unsafe fn popcount_hardware(x: u64) -> u32 {
        std::arch::x86_64::_popcnt64(x as i64) as u32
    }

    /// Fast trailing population count using BZHI
    #[inline]
    pub fn popcount_trail(x: u64, n: u32) -> u32 {
        if n == 0 {
            return 0;
        }
        if n >= 64 {
            return Self::popcount_u64(x);
        }

        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                //// SAFETY: has_bmi2 guarantees bzhi/popcnt availability
                return unsafe { Self::popcount_trail_bzhi(x, n) };
            }
        }

        // Fallback: mask manually
        let mask = (1u64 << n) - 1;
        Self::popcount_u64(x & mask)
    }

    /// Hardware BZHI implementation for trailing popcount
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi2,popcnt")]
    #[inline]
    unsafe fn popcount_trail_bzhi(x: u64, n: u32) -> u32 {
        let masked = std::arch::x86_64::_bzhi_u64(x, n);
        //// SAFETY: BMI2 and popcnt guaranteed by #[target_feature(enable = "bmi2,popcnt")]
        unsafe { Self::popcount_hardware(masked) }
    }

    /// Fast leading zero count
    #[inline]
    pub fn leading_zeros(x: u64) -> u32 {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi1 && x != 0 {
                //// SAFETY: has_bmi1 guarantees lzcnt availability
                return unsafe { std::arch::x86_64::_lzcnt_u64(x) as u32 };
            }
        }

        x.leading_zeros()
    }

    /// Fast trailing zero count
    #[inline]
    pub fn trailing_zeros(x: u64) -> u32 {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi1 && x != 0 {
                //// SAFETY: has_bmi1 guarantees tzcnt availability
                return unsafe { std::arch::x86_64::_tzcnt_u64(x) as u32 };
            }
        }

        x.trailing_zeros()
    }

    /// Vectorized population count for multiple words
    pub fn popcount_bulk(words: &[u64]) -> Vec<u32> {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.simd_caps.cpu_features.has_avx2 && words.len() >= 4 {
                //// SAFETY: has_avx2 guarantees avx2/popcnt availability
                return unsafe { Self::popcount_bulk_avx2(words) };
            }
        }

        // Fallback: serial popcount
        words.iter().map(|&w| Self::popcount_u64(w)).collect()
    }

    /// AVX2 vectorized population count
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2,popcnt")]
    unsafe fn popcount_bulk_avx2(words: &[u64]) -> Vec<u32> {
        use std::arch::x86_64::*;

        let mut result = Vec::with_capacity(words.len());
        let chunks = words.chunks_exact(4);
        let remainder = chunks.remainder();

        for chunk in chunks {
            // Load 4 x 64-bit words into AVX2 register
            // SAFETY: AVX2 and popcnt guaranteed, chunks * 4 ≤ words.len()
            let vec = unsafe { _mm256_loadu_si256(chunk.as_ptr() as *const __m256i) };

            // Use parallel popcount (if available in future CPUs)
            // For now, extract and use scalar popcount
            let mut chunk_results = [0u32; 4];
            for i in 0..4 {
                // Extract with constant index
                // SAFETY: Hardware features verified or caller ensures safety invariants
                let word = unsafe {
                    // SAFETY: AVX2 extract with constant index, i < 4
                    match i {
                        0 => _mm256_extract_epi64::<0>(vec) as u64,
                        1 => _mm256_extract_epi64::<1>(vec) as u64,
                        2 => _mm256_extract_epi64::<2>(vec) as u64,
                        3 => _mm256_extract_epi64::<3>(vec) as u64,
                        _ => unreachable!(),
                    }
                };
                // SAFETY: Hardware features verified or caller ensures safety invariants
                chunk_results[i] = unsafe { Self::popcount_hardware(word) };
            }

            result.extend_from_slice(&chunk_results);
        }

        // Handle remaining words
        for &word in remainder {
            // SAFETY: Hardware features verified or caller ensures safety invariants
            result.push(unsafe { Self::popcount_hardware(word) });
        }

        result
    }
}

/// Ultra-fast select operations using BMI2 PDEP
pub struct Bmi2SelectOps;

impl Bmi2SelectOps {
    /// Ultra-fast select using PDEP instruction
    ///
    /// This is the fastest possible select implementation, using BMI2's PDEP
    /// instruction to directly compute the position of the k-th one bit.
    #[inline]
    pub fn select1_u64(word: u64, k: u32) -> Option<u32> {
        if word == 0 || k >= word.count_ones() {
            return None;
        }

        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                // SAFETY: has_bmi2 guarantees pdep availability
                return Some(unsafe { Self::select1_pdep_optimized(word, k) });
            }
        }

        // Fallback to binary search
        Self::select1_binary_search(word, k)
    }

    /// Enhanced select with advanced optimizations
    ///
    /// Uses specific patterns and compiler optimizations from advanced research
    /// for maximum performance on BMI2-enabled CPUs.
    #[inline]
    pub fn select1_u64_enhanced(word: u64, k: u32) -> Option<u32> {
        if word == 0 || k >= word.count_ones() {
            return None;
        }

        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                // SAFETY: has_bmi2 guarantees pdep availability
                return Some(unsafe { Self::select1_pdep_optimized(word, k) });
            }
        }

        // Fallback with optimized binary search
        Self::select1_binary_search_optimized(word, k)
    }

    /// Hardware PDEP implementation with advanced optimization
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi1,bmi2")]
    #[inline]
    unsafe fn select1_pdep(word: u64, k: u32) -> u32 {
        // Advanced optimization: use (1ull << r) pattern for better instruction scheduling
        let deposited = std::arch::x86_64::_pdep_u64(1u64 << k, word);
        std::arch::x86_64::_tzcnt_u64(deposited) as u32
    }

    /// Enhanced hardware PDEP implementation with compiler-specific optimizations
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi1,bmi2")]
    #[inline]
    unsafe fn select1_pdep_optimized(word: u64, k: u32) -> u32 {
        debug_assert!(k < word.count_ones());
        debug_assert!(word != 0);
        
        // Use the advanced pattern: _pdep_u64(1ull<<r, x) + _tzcnt
        // This pattern often generates better assembly than separate operations
        #[cfg(any(target_env = "gnu", target_env = ""))]
        {
            // GCC/Clang: __builtin_ctzll often faster than _tzcnt_u64
            let deposited = std::arch::x86_64::_pdep_u64(1u64 << k, word);
            deposited.trailing_zeros()
        }
        #[cfg(not(any(target_env = "gnu", target_env = "")))]
        {
            // MSVC: Use intrinsic directly
            let deposited = std::arch::x86_64::_pdep_u64(1u64 << k, word);
            std::arch::x86_64::_tzcnt_u64(deposited) as u32
        }
    }

    /// Binary search fallback for select
    fn select1_binary_search(word: u64, k: u32) -> Option<u32> {
        let mut ones_seen = 0;

        for bit_pos in 0..64 {
            if (word >> bit_pos) & 1 == 1 {
                if ones_seen == k {
                    return Some(bit_pos);
                }
                ones_seen += 1;
            }
        }

        None
    }

    /// Optimized binary search with advanced hybrid strategy
    ///
    /// Uses linear search for small ranges (< 8 ones) and binary search for larger ranges.
    /// This follows advanced optimization patterns where linear search is faster for small
    /// ranges due to better branch prediction and cache locality.
    fn select1_binary_search_optimized(word: u64, k: u32) -> Option<u32> {
        let ones_count = word.count_ones();
        if k >= ones_count {
            return None;
        }

        // Advanced optimization: use linear search for few ones
        if ones_count <= 8 {
            let mut ones_seen = 0;
            for bit_pos in 0..64 {
                if (word >> bit_pos) & 1 == 1 {
                    if ones_seen == k {
                        return Some(bit_pos);
                    }
                    ones_seen += 1;
                }
            }
            return None;
        }

        // Binary search for denser words
        let mut low = 0u32;
        let mut high = 64u32;

        while low < high {
            let mid = (low + high) / 2;
            let rank_at_mid = Self::popcount_range(word, 0, mid);

            if rank_at_mid <= k {
                low = mid + 1;
            } else {
                high = mid;
            }
        }

        // Verify we found the correct position
        if low > 0 && (word >> (low - 1)) & 1 == 1 {
            let rank_before = if low == 1 { 0 } else { Self::popcount_range(word, 0, low - 1) };
            if rank_before == k {
                return Some(low - 1);
            }
        }

        None
    }

    /// Count population in a bit range [start, end)
    #[inline]
    fn popcount_range(word: u64, start: u32, end: u32) -> u32 {
        if start >= end || start >= 64 {
            return 0;
        }

        let end = end.min(64);
        let mask = if end == 64 {
            u64::MAX
        } else {
            (1u64 << end) - 1
        };

        let start_mask = if start == 0 {
            0
        } else {
            (1u64 << start) - 1
        };

        let range_mask = mask & !start_mask;
        (word & range_mask).count_ones()
    }

    /// Select0 using inverted PDEP
    #[inline]
    pub fn select0_u64(word: u64, k: u32) -> Option<u32> {
        let inverted = !word;
        Self::select1_u64(inverted, k)
    }

    /// Hybrid select cache strategy based on advanced research
    ///
    /// Implements intelligent cache lookup with linear search for small ranges
    /// and binary search for larger ranges, following advanced optimizations.
    pub fn select1_hybrid_cache(
        rank_cache: &[u32],
        select_cache: &[u32],
        bit_data: &[u64],
        target_rank: u32,
        cache_density: usize,
    ) -> Result<u32> {
        if select_cache.is_empty() {
            return Err(ZiporaError::invalid_data("Empty select cache"));
        }

        // Calculate cache index
        let cache_idx = (target_rank / cache_density as u32) as usize;
        if cache_idx >= select_cache.len() {
            return Err(ZiporaError::invalid_data("Cache index out of bounds"));
        }

        // Get starting position from cache
        let start_block = if cache_idx == 0 { 0 } else { select_cache[cache_idx - 1] };
        let end_block = if cache_idx + 1 < select_cache.len() {
            select_cache[cache_idx + 1]
        } else {
            rank_cache.len() as u32
        };

        // Advanced optimization: use linear search for small ranges
        let search_range = end_block - start_block;
        
        if search_range <= 32 {
            // Linear search for small ranges - better branch prediction
            for block_idx in start_block..end_block.min(rank_cache.len() as u32) {
                if rank_cache[block_idx as usize] > target_rank {
                    return Self::find_select_in_block(bit_data, block_idx as usize, target_rank, rank_cache);
                }
            }
        } else {
            // Binary search for larger ranges
            let mut low = start_block;
            let mut high = end_block.min(rank_cache.len() as u32);

            while low < high {
                let mid = (low + high) / 2;
                if rank_cache[mid as usize] <= target_rank {
                    low = mid + 1;
                } else {
                    high = mid;
                }
            }

            if low < rank_cache.len() as u32 {
                return Self::find_select_in_block(bit_data, low as usize, target_rank, rank_cache);
            }
        }

        Err(ZiporaError::invalid_data("Select target not found"))
    }

    /// Find select position within a specific block
    fn find_select_in_block(
        bit_data: &[u64],
        block_idx: usize,
        target_rank: u32,
        rank_cache: &[u32],
    ) -> Result<u32> {
        let block_start_bit = block_idx * 256; // 256-bit blocks
        let block_start_word = block_start_bit / 64;
        
        // Prefetch the block data
        #[cfg(target_arch = "x86_64")]
        {
            if block_start_word < bit_data.len() {
                //// SAFETY: Pointer derived from valid slice, block_start_word < bit_data.len() checked above
                // SAFETY: Pointer valid from bit_data, block_start_word < bit_data.len()
                unsafe {
                    let ptr = bit_data.as_ptr().add(block_start_word) as *const i8;
                    std::arch::x86_64::_mm_prefetch::<{ std::arch::x86_64::_MM_HINT_T0 }>(ptr);
                }
            }
        }

        let rank_before_block = if block_idx == 0 { 0 } else { rank_cache[block_idx - 1] };
        let target_in_block = target_rank - rank_before_block;

        // Search within the 4 words of this block
        let mut current_rank = 0;
        for word_in_block in 0..4 {
            let word_idx = block_start_word + word_in_block;
            if word_idx >= bit_data.len() {
                break;
            }

            let word = bit_data[word_idx];
            let word_popcount = word.count_ones();

            if current_rank + word_popcount > target_in_block {
                // Target is in this word
                let target_in_word = target_in_block - current_rank;
                if let Some(bit_pos) = Self::select1_u64_enhanced(word, target_in_word) {
                    return Ok((word_idx * 64) as u32 + bit_pos);
                }
            }

            current_rank += word_popcount;
        }

        Err(ZiporaError::invalid_data("Select position not found in block"))
    }

    /// Bulk select operations with BMI2 optimization
    pub fn select1_bulk(words: &[u64], indices: &[u32]) -> Result<Vec<u32>> {
        let mut results = Vec::with_capacity(indices.len());
        let _global_ones_count = 0u32;

        for &target_k in indices {
            let mut remaining_k = target_k;
            let mut found = false;

            for (word_idx, &word) in words.iter().enumerate() {
                let word_ones = Bmi2RankOps::popcount_u64(word);

                if remaining_k < word_ones {
                    // The target one is in this word
                    if let Some(bit_pos) = Self::select1_u64(word, remaining_k) {
                        results.push((word_idx * 64) as u32 + bit_pos);
                        found = true;
                        break;
                    }
                }

                remaining_k -= word_ones;
            }

            if !found {
                return Err(ZiporaError::invalid_data(format!(
                    "Select index {} not found",
                    target_k
                )));
            }
        }

        Ok(results)
    }
}

/// Advanced bit manipulation using BMI2 PEXT
pub struct Bmi2BitOps;

impl Bmi2BitOps {
    /// Extract bits at specified positions using PEXT
    #[inline]
    pub fn extract_bits(src: u64, mask: u64) -> u64 {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                //// SAFETY: has_bmi2 guarantees pext availability
                // SAFETY: has_bmi2 guarantees pext availability
                return unsafe { std::arch::x86_64::_pext_u64(src, mask) };
            }
        }

        // Fallback: manual bit extraction
        Self::extract_bits_manual(src, mask)
    }

    /// Manual bit extraction fallback
    fn extract_bits_manual(src: u64, mask: u64) -> u64 {
        let mut result = 0u64;
        let mut result_pos = 0;

        for bit_pos in 0..64 {
            if (mask >> bit_pos) & 1 == 1 {
                if (src >> bit_pos) & 1 == 1 {
                    result |= 1u64 << result_pos;
                }
                result_pos += 1;
            }
        }

        result
    }

    /// Deposit bits at specified positions using PDEP
    #[inline]
    pub fn deposit_bits(src: u64, mask: u64) -> u64 {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                //// SAFETY: has_bmi2 guarantees pdep availability
                // SAFETY: has_bmi2 guarantees pdep availability
                return unsafe { std::arch::x86_64::_pdep_u64(src, mask) };
            }
        }

        // Fallback: manual bit deposition
        Self::deposit_bits_manual(src, mask)
    }

    /// Manual bit deposition fallback
    fn deposit_bits_manual(src: u64, mask: u64) -> u64 {
        let mut result = 0u64;
        let mut src_pos = 0;

        for bit_pos in 0..64 {
            if (mask >> bit_pos) & 1 == 1 {
                if (src >> src_pos) & 1 == 1 {
                    result |= 1u64 << bit_pos;
                }
                src_pos += 1;
            }
        }

        result
    }

    /// Reset lowest set bit (BLSR equivalent)
    #[inline]
    pub fn reset_lowest_bit(x: u64) -> u64 {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi1 {
                //// SAFETY: has_bmi1 guarantees blsr availability
                return unsafe { std::arch::x86_64::_blsr_u64(x) };
            }
        }

        x & (x.wrapping_sub(1))
    }

    /// Isolate lowest set bit (BLSI equivalent)  
    #[inline]
    pub fn isolate_lowest_bit(x: u64) -> u64 {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi1 {
                //// SAFETY: has_bmi1 guarantees blsi availability
                return unsafe { std::arch::x86_64::_blsi_u64(x) };
            }
        }

        x & x.wrapping_neg()
    }

    /// Get mask up to lowest set bit (BLSMSK equivalent)
    #[inline]
    pub fn mask_up_to_lowest_bit(x: u64) -> u64 {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi1 {
                //// SAFETY: has_bmi1 guarantees blsmsk availability
                return unsafe { std::arch::x86_64::_blsmsk_u64(x) };
            }
        }

        x ^ (x.wrapping_sub(1))
    }
}

/// BMI2 BEXTR operations for bit field extraction
pub struct Bmi2BextrOps;

impl Bmi2BextrOps {
    /// Extract bits using BMI2 BEXTR instruction
    /// 
    /// Extracts `length` bits starting from `start` position.
    /// Performance: 2-3x faster than shift+mask operations
    #[inline]
    pub fn extract_bits_bextr(src: u64, start: u32, length: u32) -> u64 {
        if length == 0 || start >= 64 {
            return 0;
        }
        
        let effective_length = length.min(64 - start);
        
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                //// SAFETY: has_bmi2 guarantees bextr availability
                return unsafe { Self::extract_bits_bextr_hardware(src, start, effective_length) };
            }
        }
        
        // Fallback: shift and mask
        let shifted = src >> start;
        if effective_length >= 64 {
            shifted
        } else {
            shifted & ((1u64 << effective_length) - 1)
        }
    }
    
    /// Hardware BEXTR implementation
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi1")]
    #[inline]
    unsafe fn extract_bits_bextr_hardware(src: u64, start: u32, length: u32) -> u64 {
        std::arch::x86_64::_bextr_u64(src, start, length)
    }
    
    /// Extract multiple bit fields in parallel
    /// 
    /// Extracts bits from multiple source words using the same field specification.
    /// Highly optimized for bulk field extraction operations.
    pub fn extract_bits_multi(sources: &[u64], start: u32, length: u32) -> Vec<u64> {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 && sources.len() >= 4 {
                //// SAFETY: has_bmi2 guarantees bmi2 availability for vectorized ops
                return unsafe { Self::extract_bits_multi_vectorized(sources, start, length) };
            }
        }
        
        // Fallback: sequential extraction
        sources.iter()
            .map(|&src| Self::extract_bits_bextr(src, start, length))
            .collect()
    }
    
    /// Vectorized multi-extraction with BEXTR
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi1")]
    unsafe fn extract_bits_multi_vectorized(sources: &[u64], start: u32, length: u32) -> Vec<u64> {
        let mut results = Vec::with_capacity(sources.len());
        
        for &src in sources {
            results.push(std::arch::x86_64::_bextr_u64(src, start, length));
        }
        
        results
    }
    
    /// Hash bucket extraction using BEXTR
    /// 
    /// Optimized for hash table implementations that need to extract
    /// bucket indices from hash values.
    #[inline]
    pub fn extract_hash_bucket(hash: u64, bucket_bits: u32) -> u32 {
        Self::extract_bits_bextr(hash, 0, bucket_bits) as u32
    }
    
    /// Extract multiple hash buckets efficiently
    pub fn extract_hash_buckets_bulk(hashes: &[u64], bucket_bits: u32) -> Vec<u32> {
        Self::extract_bits_multi(hashes, 0, bucket_bits)
            .into_iter()
            .map(|x| x as u32)
            .collect()
    }
}

/// Enhanced BMI2 BZHI operations with advanced masking patterns
pub struct Bmi2BzhiOps;

impl Bmi2BzhiOps {
    /// Enhanced BZHI with population count optimization
    /// 
    /// Combines BZHI with POPCNT for ultra-fast trailing population count.
    /// Performance: 3-5x faster than manual masking + popcount
    #[inline]
    pub fn popcount_bzhi_enhanced(word: u64, bit_count: u32) -> u32 {
        if bit_count == 0 {
            return 0;
        }
        if bit_count >= 64 {
            return Bmi2RankOps::popcount_u64(word);
        }
        
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                //// SAFETY: has_bmi2 guarantees bzhi/popcnt availability
                return unsafe { Self::popcount_bzhi_hardware(word, bit_count) };
            }
        }
        
        // Fallback: manual mask
        let mask = (1u64 << bit_count) - 1;
        Bmi2RankOps::popcount_u64(word & mask)
    }
    
    /// Hardware BZHI + POPCNT combination
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi2,popcnt")]
    #[inline]
    unsafe fn popcount_bzhi_hardware(word: u64, bit_count: u32) -> u32 {
        let masked = std::arch::x86_64::_bzhi_u64(word, bit_count);
        //// SAFETY: BMI2 and popcnt guaranteed by #[target_feature(enable = "bmi2,popcnt")]
        unsafe { Bmi2RankOps::popcount_hardware(masked) }
    }
    
    /// BZHI mask operations for advanced bit manipulation
    /// 
    /// Creates masks with BZHI for various bit manipulation patterns.
    pub fn bzhi_mask_operations(word: u64, boundaries: &[u32]) -> Vec<u64> {
        boundaries.iter()
            .map(|&boundary| {
                #[cfg(target_arch = "x86_64")]
                {
                    let caps = Bmi2Capabilities::get();
                    if caps.has_bmi2 {
                        //// SAFETY: has_bmi2 guarantees bzhi availability
                        return unsafe { std::arch::x86_64::_bzhi_u64(word, boundary) };
                    }
                }
                
                // Fallback
                if boundary >= 64 {
                    word
                } else {
                    word & ((1u64 << boundary) - 1)
                }
            })
            .collect()
    }
    
    /// Vectorized BZHI operations for bulk processing
    pub fn bzhi_bulk_process(words: &[u64], bit_count: u32) -> Vec<u64> {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 && words.len() >= 4 {
                //// SAFETY: has_bmi2 guarantees bzhi availability for vectorized ops
                return unsafe { Self::bzhi_bulk_vectorized(words, bit_count) };
            }
        }
        
        // Fallback: sequential processing
        let mask = if bit_count >= 64 {
            u64::MAX
        } else {
            (1u64 << bit_count) - 1
        };
        
        words.iter().map(|&word| word & mask).collect()
    }
    
    /// Hardware vectorized BZHI
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi2")]
    unsafe fn bzhi_bulk_vectorized(words: &[u64], bit_count: u32) -> Vec<u64> {
        words.iter()
            .map(|&word| std::arch::x86_64::_bzhi_u64(word, bit_count))
            .collect()
    }
}

/// Advanced PDEP/PEXT patterns for high-performance bit operations
pub struct Bmi2AdvancedPatterns;

impl Bmi2AdvancedPatterns {
    /// PDEP + CTZ select for high-performance operations
    /// 
    /// Ultra-fast select using the PDEP+CTZ pattern: PDEP(1<<k, word) + CTZ
    /// Performance: 5-10x faster than binary search for select operations
    #[inline]
    pub fn pdep_ctz_select(word: u64, k: u32) -> Option<u32> {
        if word == 0 || k >= word.count_ones() {
            return None;
        }
        
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                // SAFETY: has_bmi2 guarantees pdep/ctz ops
                return Some(unsafe { Self::pdep_ctz_select_hardware(word, k) });
            }
        }
        
        // Fallback to existing select implementation
        Bmi2SelectOps::select1_u64(word, k)
    }
    
    /// Hardware PDEP + CTZ implementation
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi1,bmi2")]
    #[inline]
    unsafe fn pdep_ctz_select_hardware(word: u64, k: u32) -> u32 {
        let deposited = std::arch::x86_64::_pdep_u64(1u64 << k, word);
        std::arch::x86_64::_tzcnt_u64(deposited) as u32
    }
    
    /// PEXT parallel extract for compressed data processing
    /// 
    /// Extracts bits from multiple words using the same mask, optimized
    /// for entropy coding and variable-length decoding operations.
    pub fn pext_parallel_extract(sources: &[u64], mask: u64) -> Vec<u64> {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                //// SAFETY: has_bmi2 guarantees pext availability for parallel ops
                return unsafe { Self::pext_parallel_hardware(sources, mask) };
            }
        }
        
        // Fallback: manual bit extraction
        sources.iter()
            .map(|&src| Bmi2BitOps::extract_bits(src, mask))
            .collect()
    }
    
    /// Hardware parallel PEXT
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi2")]
    unsafe fn pext_parallel_hardware(sources: &[u64], mask: u64) -> Vec<u64> {
        sources.iter()
            .map(|&src| std::arch::x86_64::_pext_u64(src, mask))
            .collect()
    }
    
    /// PDEP + BZHI composite operations for complex bit manipulation
    /// 
    /// Combines PDEP and BZHI for advanced bit field operations,
    /// particularly useful for hash table collision resolution.
    pub fn pdep_bzhi_composite(src: u64, mask: u64, bit_limit: u32) -> u64 {
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                //// SAFETY: has_bmi2 guarantees pdep/bzhi availability
                return unsafe { Self::pdep_bzhi_composite_hardware(src, mask, bit_limit) };
            }
        }
        
        // Fallback: combine operations manually
        let deposited = Bmi2BitOps::deposit_bits(src, mask);
        if bit_limit >= 64 {
            deposited
        } else {
            deposited & ((1u64 << bit_limit) - 1)
        }
    }
    
    /// Hardware PDEP + BZHI composite
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi2")]
    #[inline]
    unsafe fn pdep_bzhi_composite_hardware(src: u64, mask: u64, bit_limit: u32) -> u64 {
        let deposited = std::arch::x86_64::_pdep_u64(src, mask);
        std::arch::x86_64::_bzhi_u64(deposited, bit_limit)
    }
    
    /// Bulk PDEP + CTZ select operations
    /// 
    /// Performs multiple select operations efficiently using vectorized
    /// PDEP + CTZ pattern for maximum throughput.
    pub fn pdep_ctz_select_bulk(word: u64, indices: &[u32]) -> Result<Vec<u32>> {
        if word == 0 {
            return Err(ZiporaError::invalid_data("Cannot select from zero word"));
        }
        
        let word_popcount = word.count_ones();
        let mut results = Vec::with_capacity(indices.len());
        
        for &k in indices {
            if k >= word_popcount {
                return Err(ZiporaError::invalid_data(format!(
                    "Select index {} out of bounds (word has {} ones)", k, word_popcount
                )));
            }
            
            if let Some(pos) = Self::pdep_ctz_select(word, k) {
                results.push(pos);
            } else {
                return Err(ZiporaError::invalid_data(format!(
                    "Select failed for index {}", k
                )));
            }
        }
        
        Ok(results)
    }
}

/// BMI2-accelerated range operations
pub struct Bmi2RangeOps;

impl Bmi2RangeOps {
    /// Count ones in a bit range using BZHI optimization
    pub fn count_ones_range(word: u64, start: u32, len: u32) -> u32 {
        if len == 0 {
            return 0;
        }
        if start >= 64 {
            return 0;
        }

        let effective_len = len.min(64 - start);

        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                //// SAFETY: has_bmi2 guarantees bmi2 ops availability
                return unsafe { Self::count_ones_range_bmi2(word, start, effective_len) };
            }
        }

        // Fallback: manual masking
        let shifted = word >> start;
        let mask = if effective_len >= 64 {
            u64::MAX
        } else {
            (1u64 << effective_len) - 1
        };
        Bmi2RankOps::popcount_u64(shifted & mask)
    }

    /// BMI2 optimized range count
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "bmi2,popcnt")]
    unsafe fn count_ones_range_bmi2(word: u64, start: u32, len: u32) -> u32 {
        use std::arch::x86_64::*;

        // Shift to start position
        let shifted = word >> start;

        // Zero high bits beyond length using BZHI
        let masked = _bzhi_u64(shifted, len);

        //// SAFETY: BMI2 and popcnt guaranteed by #[target_feature(enable = "bmi2,popcnt")]
        // Count with hardware POPCNT
        unsafe { Bmi2RankOps::popcount_hardware(masked) }
    }

    /// Extract and count bits in multiple ranges efficiently
    pub fn count_ones_multi_range(word: u64, ranges: &[(u32, u32)]) -> Vec<u32> {
        ranges
            .iter()
            .map(|&(start, len)| Self::count_ones_range(word, start, len))
            .collect()
    }

    /// Parallel bit field extraction for rank computation
    pub fn extract_rank_fields(words: &[u64], field_mask: u64) -> Vec<u64> {
        words
            .iter()
            .map(|&word| Bmi2BitOps::extract_bits(word, field_mask))
            .collect()
    }
}

/// Sequence length operations (advanced enhancement)
pub struct Bmi2SequenceOps;

impl Bmi2SequenceOps {
    /// Length of ones sequence starting at bit position
    ///
    /// Counts consecutive ones starting from the given position.
    /// Based on advanced one_seq_len implementation.
    #[inline]
    pub fn one_seq_len(bit_data: &[u64], bit_pos: usize) -> usize {
        if bit_pos >= bit_data.len() * 64 {
            return 0;
        }

        let word_idx = bit_pos / 64;
        let bit_offset = bit_pos % 64;
        
        if word_idx >= bit_data.len() {
            return 0;
        }

        let word = bit_data[word_idx];
        
        // Check if starting bit is even set
        if (word >> bit_offset) & 1 == 0 {
            return 0;
        }

        // Count consecutive ones in current word
        let remaining_word = word >> bit_offset;
        let trailing_ones = Self::count_trailing_ones(remaining_word);
        
        if trailing_ones < 64 - bit_offset {
            // Sequence ends within this word
            return trailing_ones;
        }

        // Sequence continues to next words
        let mut total_ones = 64 - bit_offset;
        
        for next_word_idx in (word_idx + 1)..bit_data.len() {
            let next_word = bit_data[next_word_idx];
            let ones_in_word = Self::count_trailing_ones(next_word);
            
            total_ones += ones_in_word;
            
            if ones_in_word < 64 {
                // Sequence ends in this word
                break;
            }
        }

        total_ones
    }

    /// Length of zeros sequence starting at bit position
    ///
    /// Counts consecutive zeros starting from the given position.
    /// Based on advanced zero_seq_len implementation.
    #[inline]
    pub fn zero_seq_len(bit_data: &[u64], bit_pos: usize) -> usize {
        if bit_pos >= bit_data.len() * 64 {
            return 0;
        }

        let word_idx = bit_pos / 64;
        let bit_offset = bit_pos % 64;
        
        if word_idx >= bit_data.len() {
            return 0;
        }

        let word = bit_data[word_idx];
        
        // Check if starting bit is set (we want zeros)
        if (word >> bit_offset) & 1 == 1 {
            return 0;
        }

        // Count consecutive zeros in current word (invert and count ones)
        let remaining_word = (!word) >> bit_offset;
        let trailing_zeros = Self::count_trailing_ones(remaining_word);
        
        if trailing_zeros < 64 - bit_offset {
            // Sequence ends within this word
            return trailing_zeros;
        }

        // Sequence continues to next words
        let mut total_zeros = 64 - bit_offset;
        
        for next_word_idx in (word_idx + 1)..bit_data.len() {
            let next_word = bit_data[next_word_idx];
            let zeros_in_word = Self::count_trailing_ones(!next_word);
            
            total_zeros += zeros_in_word;
            
            if zeros_in_word < 64 {
                // Sequence ends in this word
                break;
            }
        }

        total_zeros
    }

    /// Reverse length of ones sequence ending at bit position
    ///
    /// Counts consecutive ones ending at the given position.
    /// Based on advanced one_seq_revlen implementation.
    #[inline]
    pub fn one_seq_revlen(bit_data: &[u64], end_pos: usize) -> usize {
        if end_pos == 0 || end_pos > bit_data.len() * 64 {
            return 0;
        }

        let bit_pos = end_pos - 1;
        let word_idx = bit_pos / 64;
        let bit_offset = bit_pos % 64;
        
        if word_idx >= bit_data.len() {
            return 0;
        }

        let word = bit_data[word_idx];
        
        // Check if ending bit is set
        if (word >> bit_offset) & 1 == 0 {
            return 0;
        }

        // Count leading ones in the masked word
        let mask = (1u64 << (bit_offset + 1)) - 1;
        let masked_word = word & mask;
        let leading_ones = Self::count_leading_ones_in_mask(masked_word, bit_offset + 1);
        
        if leading_ones < bit_offset + 1 {
            // Sequence starts within this word
            return leading_ones;
        }

        // Sequence continues to previous words
        let mut total_ones = bit_offset + 1;
        
        if word_idx > 0 {
            for prev_word_idx in (0..word_idx).rev() {
                let prev_word = bit_data[prev_word_idx];
                let ones_in_word = Self::count_leading_ones_in_mask(prev_word, 64);
                
                total_ones += ones_in_word;
                
                if ones_in_word < 64 {
                    // Sequence starts in this word
                    break;
                }
            }
        }

        total_ones
    }

    /// Count trailing ones in a word (consecutive ones from LSB)
    #[inline]
    fn count_trailing_ones(word: u64) -> usize {
        if word == u64::MAX {
            return 64;
        }
        
        // Find first zero bit
        let inverted = !word;
        if inverted == 0 {
            64
        } else {
            inverted.trailing_zeros() as usize
        }
    }

    /// Count leading ones in a masked word
    #[inline]
    fn count_leading_ones_in_mask(word: u64, bit_count: usize) -> usize {
        if bit_count == 0 {
            return 0;
        }
        
        if word == (1u64 << bit_count) - 1 {
            return bit_count;
        }
        
        // Invert and count leading zeros in the masked area
        let inverted = !word;
        let shift_amount = 64 - bit_count;
        let shifted = inverted << shift_amount;
        
        if shifted == 0 {
            bit_count
        } else {
            (shifted.leading_zeros() as usize).min(bit_count)
        }
    }
}

/// BMI2-accelerated block operations
pub struct Bmi2BlockOps;

impl Bmi2BlockOps {
    /// Process multiple 64-bit blocks with BMI2 optimization
    pub fn rank_bulk(blocks: &[u64], positions: &[usize]) -> Vec<usize> {
        let mut results = Vec::with_capacity(positions.len());

        for &pos in positions {
            let mut rank = 0;
            let block_idx = pos / 64;
            let bit_offset = pos % 64;

            // Count full blocks
            for i in 0..block_idx.min(blocks.len()) {
                rank += Bmi2RankOps::popcount_u64(blocks[i]) as usize;
            }

            // Count partial block
            if block_idx < blocks.len() {
                rank += Bmi2RankOps::popcount_trail(blocks[block_idx], bit_offset as u32) as usize;
            }

            results.push(rank);
        }

        results
    }

    /// Parallel select across multiple blocks
    pub fn select_bulk(blocks: &[u64], indices: &[usize]) -> Result<Vec<usize>> {
        let mut results = Vec::with_capacity(indices.len());

        // Pre-compute cumulative popcount for blocks
        let mut cumulative_counts = Vec::with_capacity(blocks.len() + 1);
        cumulative_counts.push(0);

        for &block in blocks {
            //// SAFETY: cumulative_counts has at least one element (pushed 0 at line 1318)
            let last_count = cumulative_counts.last().expect("cumulative_counts non-empty");
            cumulative_counts.push(last_count + Bmi2RankOps::popcount_u64(block) as usize);
        }

        for &k in indices {
            // Find the first block where cumulative count > k
            // This gives us the block containing the k-th one (0-indexed)
            let block_idx = match cumulative_counts.binary_search(&(k + 1)) {
                Ok(idx) => {
                    // Exact match means k+1 ones are before this block
                    // So the k-th one is in the previous block
                    if idx == 0 {
                        return Err(ZiporaError::invalid_data(format!(
                            "Select index {} out of bounds - no ones before first block",
                            k
                        )));
                    }
                    idx - 1
                }
                Err(idx) => {
                    // idx is where k+1 would be inserted
                    // So block idx-1 is where the k-th one is located
                    if idx == 0 {
                        return Err(ZiporaError::invalid_data(format!(
                            "Select index {} out of bounds - before first block",
                            k
                        )));
                    }
                    idx - 1
                }
            };

            if block_idx >= blocks.len() {
                return Err(ZiporaError::invalid_data(format!(
                    "Select index {} out of bounds",
                    k
                )));
            }

            let ones_before_block = cumulative_counts[block_idx];
            let k_in_block = k - ones_before_block;

            if let Some(bit_pos) = Bmi2SelectOps::select1_u64(blocks[block_idx], k_in_block as u32)
            {
                results.push(block_idx * 64 + bit_pos as usize);
            } else {
                return Err(ZiporaError::invalid_data(format!(
                    "Select failed for index {}",
                    k
                )));
            }
        }

        Ok(results)
    }

    /// Advanced SIMD + BMI2 block processing
    #[cfg(target_arch = "x86_64")]
    pub fn process_blocks_simd(blocks: &[u64]) -> Vec<(u32, u32)> {
        let caps = Bmi2Capabilities::get();

        if caps.simd_caps.cpu_features.has_avx2 && blocks.len() >= 4 {
            //// SAFETY: AVX2 availability verified by has_avx2 check above
            unsafe { Self::process_blocks_avx2_bmi2(blocks) }
        } else {
            // Fallback to scalar processing
            blocks
                .iter()
                .map(|&block| {
                    (
                        Bmi2RankOps::popcount_u64(block),
                        Bmi2RankOps::leading_zeros(block),
                    )
                })
                .collect()
        }
    }

    /// AVX2 + BMI2 combined processing
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2,popcnt,lzcnt")]
    unsafe fn process_blocks_avx2_bmi2(blocks: &[u64]) -> Vec<(u32, u32)> {
        use std::arch::x86_64::*;

        let mut results = Vec::with_capacity(blocks.len());

        for chunk in blocks.chunks(4) {
            for &block in chunk {
                // SAFETY: Hardware features verified or caller ensures safety invariants
                let popcount = unsafe { Bmi2RankOps::popcount_hardware(block) };
                let lzcnt = if block != 0 {
                    _lzcnt_u64(block) as u32
                } else {
                    64
                };
                results.push((popcount, lzcnt));
            }
        }

        results
    }
}

/// Systematic BMI2 operation trait for consistent interfaces
pub trait Bmi2OperationTrait {
    /// Execute the operation with BMI2 acceleration if available
    fn execute_bmi2(&self, input: &[u64]) -> Vec<u64>;
    
    /// Get the operation name for debugging/profiling
    fn operation_name(&self) -> &'static str;
    
    /// Check if BMI2 acceleration is used for this operation
    fn uses_bmi2_acceleration(&self) -> bool {
        Bmi2Capabilities::get().has_bmi2
    }
    
    /// Get estimated performance improvement with BMI2
    fn estimated_speedup(&self) -> f64;
}

/// BMI2 operation dispatcher for automatic hardware-optimized function selection
pub struct Bmi2Dispatcher {
    capabilities: Bmi2Capabilities,
}

impl Bmi2Dispatcher {
    /// Create new dispatcher with runtime capability detection
    pub fn new() -> Self {
        Self {
            capabilities: Bmi2Capabilities::detect(),
        }
    }
    
    /// Dispatch to optimal implementation based on hardware capabilities
    pub fn dispatch_popcount(&self, word: u64) -> u32 {
        if self.capabilities.has_bmi1 {
            Bmi2RankOps::popcount_u64(word)
        } else {
            word.count_ones()
        }
    }
    
    /// Dispatch select operation to optimal implementation
    pub fn dispatch_select(&self, word: u64, k: u32) -> Option<u32> {
        if self.capabilities.has_bmi2 {
            Bmi2AdvancedPatterns::pdep_ctz_select(word, k)
        } else {
            Bmi2SelectOps::select1_u64(word, k)
        }
    }
    
    /// Dispatch bit extraction to optimal implementation
    pub fn dispatch_extract_bits(&self, src: u64, start: u32, length: u32) -> u64 {
        if self.capabilities.has_bmi2 {
            Bmi2BextrOps::extract_bits_bextr(src, start, length)
        } else {
            let shifted = src >> start;
            if length >= 64 {
                shifted
            } else {
                shifted & ((1u64 << length) - 1)
            }
        }
    }
    
    /// Dispatch mask operation to optimal implementation
    pub fn dispatch_mask_operation(&self, word: u64, bit_count: u32) -> u64 {
        if self.capabilities.has_bmi2 {
            #[cfg(target_arch = "x86_64")]
            {
                if bit_count >= 64 {
                    return word;
                }
                //// SAFETY: has_bmi2 guarantees bzhi availability
                return unsafe { std::arch::x86_64::_bzhi_u64(word, bit_count) };
            }
        }
        
        // Fallback
        if bit_count >= 64 {
            word
        } else {
            word & ((1u64 << bit_count) - 1)
        }
    }
    
    /// Get optimization report for current hardware
    pub fn optimization_report(&self) -> Bmi2OptimizationReport {
        Bmi2OptimizationReport {
            has_bmi1: self.capabilities.has_bmi1,
            has_bmi2: self.capabilities.has_bmi2,
            has_popcnt: self.capabilities.simd_caps.cpu_features.has_popcnt,
            optimization_tier: if self.capabilities.has_bmi2 {
                "Tier 3: Full BMI2 Acceleration"
            } else if self.capabilities.has_bmi1 {
                "Tier 2: BMI1 Acceleration"
            } else {
                "Tier 1: Software Fallback"
            },
            available_operations: self.get_available_operations(),
            estimated_speedups: self.get_estimated_speedups(),
        }
    }
    
    fn get_available_operations(&self) -> Vec<&'static str> {
        let mut ops = vec!["Basic bit operations"];
        
        if self.capabilities.has_bmi1 {
            ops.extend_from_slice(&[
                "Hardware POPCNT",
                "LZCNT/TZCNT",
                "BLSR/BLSI/BLSMSK",
                "BEXTR",
            ]);
        }
        
        if self.capabilities.has_bmi2 {
            ops.extend_from_slice(&[
                "PDEP/PEXT",
                "BZHI",
                "PDEP+CTZ select",
                "Advanced patterns",
            ]);
        }
        
        ops
    }
    
    fn get_estimated_speedups(&self) -> std::collections::HashMap<&'static str, f64> {
        let mut speedups = std::collections::HashMap::new();
        
        if self.capabilities.has_bmi1 {
            speedups.insert("POPCNT", 2.0);
            speedups.insert("LZCNT/TZCNT", 1.5);
            speedups.insert("BEXTR", 2.5);
        }
        
        if self.capabilities.has_bmi2 {
            speedups.insert("Select (PDEP+CTZ)", 8.0);
            speedups.insert("Bit extraction (PEXT)", 3.0);
            speedups.insert("Masking (BZHI)", 2.0);
            speedups.insert("Complex patterns", 4.0);
        }
        
        speedups
    }
}

impl Default for Bmi2Dispatcher {
    fn default() -> Self {
        Self::new()
    }
}

/// BMI2 optimization report
#[derive(Debug, Clone)]
pub struct Bmi2OptimizationReport {
    pub has_bmi1: bool,
    pub has_bmi2: bool,
    pub has_popcnt: bool,
    pub optimization_tier: &'static str,
    pub available_operations: Vec<&'static str>,
    pub estimated_speedups: std::collections::HashMap<&'static str, f64>,
}

/// Hash operations using BMI2 acceleration
pub struct Bmi2HashOps;

impl Bmi2HashOps {
    /// Extract hash bucket using BMI2 for hash table operations
    /// 
    /// Performance: 2-3x faster than modulo operations for power-of-2 buckets
    #[inline]
    pub fn hash_bucket_extract(hash: u64, bucket_bits: u32) -> u32 {
        Bmi2BextrOps::extract_hash_bucket(hash, bucket_bits)
    }
    
    /// Collision resolution using BMI2 patterns
    /// 
    /// Uses PDEP/PEXT for efficient hash collision resolution in
    /// Robin Hood hashing and Hopscotch hashing schemes.
    pub fn collision_resolve_pattern(_hash: u64, occupied_mask: u64) -> Option<u32> {
        if occupied_mask == u64::MAX {
            return None; // All slots occupied
        }
        
        // Find first free slot using BMI2
        let free_mask = !occupied_mask;
        if let Some(first_free) = Bmi2SelectOps::select1_u64(free_mask, 0) {
            Some(first_free)
        } else {
            None
        }
    }
    
    /// Bulk hash bucket extraction
    pub fn hash_buckets_bulk(hashes: &[u64], bucket_bits: u32) -> Vec<u32> {
        Bmi2BextrOps::extract_hash_buckets_bulk(hashes, bucket_bits)
    }
}

/// Compression support operations using BMI2
pub struct Bmi2CompressionOps;

impl Bmi2CompressionOps {
    /// Bit field operations for entropy coding
    /// 
    /// Extracts variable-length codes from compressed bit streams
    /// using BMI2 BEXTR for optimal performance.
    pub fn extract_entropy_field(bit_stream: u64, start_bit: u32, field_length: u32) -> u32 {
        Bmi2BextrOps::extract_bits_bextr(bit_stream, start_bit, field_length) as u32
    }
    
    /// Variable-length decoding using PEXT
    /// 
    /// Decodes multiple variable-length symbols from a packed bit stream
    /// using PEXT for parallel extraction.
    pub fn decode_variable_length(packed_data: u64, symbol_masks: &[u64]) -> Vec<u32> {
        symbol_masks.iter()
            .map(|&mask| Bmi2BitOps::extract_bits(packed_data, mask) as u32)
            .collect()
    }
    
    /// Parallel bit field packing for compression
    /// 
    /// Packs multiple values into a single word using PDEP for
    /// efficient bit-level compression operations.
    pub fn pack_compression_fields(values: &[u32], field_masks: &[u64]) -> u64 {
        let mut result = 0u64;
        
        for (i, (&value, &mask)) in values.iter().zip(field_masks.iter()).enumerate() {
            if i >= field_masks.len() {
                break;
            }
            result |= Bmi2BitOps::deposit_bits(value as u64, mask);
        }
        
        result
    }
}

/// String processing operations using BMI2
pub struct Bmi2StringOps;

impl Bmi2StringOps {
    /// Character extraction for UTF-8 processing
    /// 
    /// Extracts character code points using BEXTR for optimized
    /// UTF-8 validation and processing.
    pub fn extract_utf8_char(byte_data: u64, char_start: u32, char_len: u32) -> u32 {
        Bmi2BextrOps::extract_bits_bextr(byte_data, char_start * 8, char_len * 8) as u32
    }
    
    /// Pattern matching with BMI2 acceleration
    /// 
    /// Uses PEXT for efficient character class matching and
    /// pattern extraction in string processing algorithms.
    pub fn pattern_match_extract(text_data: u64, pattern_mask: u64) -> u64 {
        Bmi2BitOps::extract_bits(text_data, pattern_mask)
    }
    
    /// Bulk character extraction for string algorithms
    pub fn extract_chars_bulk(byte_streams: &[u64], char_positions: &[(u32, u32)]) -> Vec<u32> {
        let mut results = Vec::with_capacity(char_positions.len());
        
        for (i, &(start, len)) in char_positions.iter().enumerate() {
            if i < byte_streams.len() {
                results.push(Self::extract_utf8_char(byte_streams[i], start, len));
            }
        }
        
        results
    }
}

/// Memory operations using BMI2 for cache-aligned access
pub struct Bmi2MemoryOps;

impl Bmi2MemoryOps {
    /// Cache-aligned field access using BEXTR
    /// 
    /// Extracts fields from cache-aligned memory structures
    /// using BMI2 for optimal memory access patterns.
    pub fn extract_cache_aligned_field(cache_line: u64, field_offset: u32, field_size: u32) -> u64 {
        Bmi2BextrOps::extract_bits_bextr(cache_line, field_offset, field_size)
    }
    
    /// Address calculation using BMI2 patterns
    /// 
    /// Calculates memory addresses using PDEP for efficient
    /// address computation in specialized memory layouts.
    pub fn calculate_address_pattern(base_addr: u64, offset_pattern: u64, layout_mask: u64) -> u64 {
        base_addr + Bmi2BitOps::deposit_bits(offset_pattern, layout_mask)
    }
    
    /// Bulk memory field extraction
    pub fn extract_memory_fields_bulk(
        memory_words: &[u64], 
        field_offset: u32, 
        field_size: u32
    ) -> Vec<u64> {
        Bmi2BextrOps::extract_bits_multi(memory_words, field_offset, field_size)
    }
}

/// High-level BMI2 acceleration interface
pub struct Bmi2Accelerator {
    capabilities: Bmi2Capabilities,
    dispatcher: Bmi2Dispatcher,
}

impl Bmi2Accelerator {
    /// Create new BMI2 accelerator with capability detection
    pub fn new() -> Self {
        Self {
            capabilities: Bmi2Capabilities::detect(),
            dispatcher: Bmi2Dispatcher::new(),
        }
    }

    /// Get capabilities
    pub fn capabilities(&self) -> &Bmi2Capabilities {
        &self.capabilities
    }

    /// Check if BMI2 acceleration is available
    pub fn is_available(&self) -> bool {
        self.capabilities.has_bmi2
    }

    /// Accelerated rank operation
    #[inline]
    pub fn rank1(&self, word: u64, pos: u32) -> u32 {
        Bmi2RankOps::popcount_trail(word, pos)
    }

    /// Accelerated select operation
    #[inline]
    pub fn select1(&self, word: u64, k: u32) -> Option<u32> {
        Bmi2SelectOps::select1_u64(word, k)
    }

    /// Accelerated bulk operations
    pub fn rank_bulk(&self, blocks: &[u64], positions: &[usize]) -> Vec<usize> {
        Bmi2BlockOps::rank_bulk(blocks, positions)
    }

    /// Accelerated bulk select
    pub fn select_bulk(&self, blocks: &[u64], indices: &[usize]) -> Result<Vec<usize>> {
        Bmi2BlockOps::select_bulk(blocks, indices)
    }

    /// Enhanced bit extraction using BEXTR
    pub fn extract_bits(&self, src: u64, start: u32, length: u32) -> u64 {
        self.dispatcher.dispatch_extract_bits(src, start, length)
    }
    
    /// Enhanced select using PDEP+CTZ pattern
    pub fn select1_enhanced(&self, word: u64, k: u32) -> Option<u32> {
        self.dispatcher.dispatch_select(word, k)
    }
    
    /// Hash operations interface
    pub fn hash_bucket_extract(&self, hash: u64, bucket_bits: u32) -> u32 {
        Bmi2HashOps::hash_bucket_extract(hash, bucket_bits)
    }
    
    /// Compression operations interface
    pub fn extract_entropy_field(&self, bit_stream: u64, start_bit: u32, field_length: u32) -> u32 {
        Bmi2CompressionOps::extract_entropy_field(bit_stream, start_bit, field_length)
    }
    
    /// String operations interface
    pub fn extract_utf8_char(&self, byte_data: u64, char_start: u32, char_len: u32) -> u32 {
        Bmi2StringOps::extract_utf8_char(byte_data, char_start, char_len)
    }
    
    /// Memory operations interface
    pub fn extract_cache_aligned_field(&self, cache_line: u64, field_offset: u32, field_size: u32) -> u64 {
        Bmi2MemoryOps::extract_cache_aligned_field(cache_line, field_offset, field_size)
    }
    
    /// Get comprehensive optimization report
    pub fn optimization_report(&self) -> Bmi2OptimizationReport {
        self.dispatcher.optimization_report()
    }
    
    /// Get acceleration statistics (legacy interface)
    pub fn stats(&self) -> Bmi2Stats {
        Bmi2Stats {
            has_bmi1: self.capabilities.has_bmi1,
            has_bmi2: self.capabilities.has_bmi2,
            has_popcnt: self.capabilities.simd_caps.cpu_features.has_popcnt,
            has_lzcnt: self.capabilities.has_bmi1, // LZCNT is part of BMI1
            optimization_tier: if self.capabilities.has_bmi2 {
                3
            } else if self.capabilities.has_bmi1 {
                2
            } else {
                1
            },
            estimated_speedup_rank: if self.capabilities.has_bmi2 {
                3.5
            } else if self.capabilities.has_bmi1 {
                2.0
            } else {
                1.0
            },
            estimated_speedup_select: if self.capabilities.has_bmi2 { 8.0 } else { 1.0 },
        }
    }
}

impl Default for Bmi2Accelerator {
    fn default() -> Self {
        Self::new()
    }
}

/// Example implementations of Bmi2OperationTrait for common operations
pub struct PopcountOperation;
pub struct SelectOperation;
pub struct BitExtractionOperation;

impl Bmi2OperationTrait for PopcountOperation {
    fn execute_bmi2(&self, input: &[u64]) -> Vec<u64> {
        input.iter()
            .map(|&word| Bmi2RankOps::popcount_u64(word) as u64)
            .collect()
    }
    
    fn operation_name(&self) -> &'static str {
        "Population Count"
    }
    
    fn estimated_speedup(&self) -> f64 {
        if self.uses_bmi2_acceleration() { 2.0 } else { 1.0 }
    }
}

impl Bmi2OperationTrait for SelectOperation {
    fn execute_bmi2(&self, input: &[u64]) -> Vec<u64> {
        // Example: select1(word, 0) for each word
        input.iter()
            .map(|&word| {
                Bmi2AdvancedPatterns::pdep_ctz_select(word, 0)
                    .unwrap_or(u32::MAX) as u64
            })
            .collect()
    }
    
    fn operation_name(&self) -> &'static str {
        "Select Operation"
    }
    
    fn estimated_speedup(&self) -> f64 {
        if self.uses_bmi2_acceleration() { 8.0 } else { 1.0 }
    }
}

impl Bmi2OperationTrait for BitExtractionOperation {
    fn execute_bmi2(&self, input: &[u64]) -> Vec<u64> {
        // Example: extract lower 32 bits from each word
        input.iter()
            .map(|&word| Bmi2BextrOps::extract_bits_bextr(word, 0, 32))
            .collect()
    }
    
    fn operation_name(&self) -> &'static str {
        "Bit Extraction"
    }
    
    fn estimated_speedup(&self) -> f64 {
        if self.uses_bmi2_acceleration() { 2.5 } else { 1.0 }
    }
}

/// BMI2 acceleration statistics
#[derive(Debug, Clone)]
pub struct Bmi2Stats {
    pub has_bmi1: bool,
    pub has_bmi2: bool,
    pub has_popcnt: bool,
    pub has_lzcnt: bool,
    pub optimization_tier: u8,
    pub estimated_speedup_rank: f64,
    pub estimated_speedup_select: f64,
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_bmi2_capabilities() {
        let caps = Bmi2Capabilities::detect();

        // Should detect some capability
        println!("BMI1: {}, BMI2: {}", caps.has_bmi1, caps.has_bmi2);

        // Cached access should return same result
        let caps2 = Bmi2Capabilities::get();
        assert_eq!(caps.has_bmi1, caps2.has_bmi1);
        assert_eq!(caps.has_bmi2, caps2.has_bmi2);
    }

    #[test]
    fn test_popcount_operations() {
        let test_words = vec![
            0x0000000000000000u64, // All zeros
            0xFFFFFFFFFFFFFFFFu64, // All ones
            0xAAAAAAAAAAAAAAAAu64, // Alternating bits
            0x5555555555555555u64, // Alternating bits (offset)
            0x0000000000000001u64, // Single bit
            0x8000000000000000u64, // High bit
        ];

        for &word in &test_words {
            let expected = word.count_ones();
            let actual = Bmi2RankOps::popcount_u64(word);
            assert_eq!(
                actual, expected,
                "Popcount mismatch for word {:#018x}",
                word
            );
        }
    }

    #[test]
    fn test_popcount_trail() {
        let word = 0xAAAAAAAAAAAAAAAAu64; // Alternating bits

        // Test various trailing counts
        for n in [0, 1, 4, 8, 16, 32, 63, 64] {
            let expected = if n >= 64 {
                word.count_ones()
            } else {
                let mask = (1u64 << n) - 1;
                (word & mask).count_ones()
            };

            let actual = Bmi2RankOps::popcount_trail(word, n);
            assert_eq!(actual, expected, "Popcount trail mismatch for n={}", n);
        }
    }

    #[test]
    fn test_leading_trailing_zeros() {
        let test_cases = vec![
            (0x0000000000000001u64, 63, 0),
            (0x8000000000000000u64, 0, 63),
            (0x0000000000000100u64, 55, 8),
            (0x0000000000000000u64, 64, 64), // Special case
            (0xFFFFFFFFFFFFFFFFu64, 0, 0),
        ];

        for (word, expected_lz, expected_tz) in test_cases {
            let actual_lz = if word == 0 {
                64
            } else {
                Bmi2RankOps::leading_zeros(word)
            };
            let actual_tz = if word == 0 {
                64
            } else {
                Bmi2RankOps::trailing_zeros(word)
            };

            assert_eq!(
                actual_lz, expected_lz,
                "Leading zeros mismatch for {:#018x}",
                word
            );
            assert_eq!(
                actual_tz, expected_tz,
                "Trailing zeros mismatch for {:#018x}",
                word
            );
        }
    }

    #[test]
    fn test_select_operations() {
        let word = 0x0000000000000015u64; // Binary: ...00010101 (bits 0, 2, 4 set)

        // Test valid select operations
        assert_eq!(Bmi2SelectOps::select1_u64(word, 0), Some(0)); // 1st one at position 0
        assert_eq!(Bmi2SelectOps::select1_u64(word, 1), Some(2)); // 2nd one at position 2
        assert_eq!(Bmi2SelectOps::select1_u64(word, 2), Some(4)); // 3rd one at position 4

        // Test out of bounds
        assert_eq!(Bmi2SelectOps::select1_u64(word, 3), None); // No 4th one
        assert_eq!(Bmi2SelectOps::select1_u64(0, 0), None); // Zero word
    }

    #[test]
    fn test_select0_operations() {
        let word = 0xFFFFFFFFFFFFFFEAu64; // Binary: ...11101010 (bits 0, 2, 4 clear)

        // Test select0 operations
        assert_eq!(Bmi2SelectOps::select0_u64(word, 0), Some(0)); // 1st zero at position 0
        assert_eq!(Bmi2SelectOps::select0_u64(word, 1), Some(2)); // 2nd zero at position 2
        assert_eq!(Bmi2SelectOps::select0_u64(word, 2), Some(4)); // 3rd zero at position 4
    }

    #[test]
    fn test_bit_manipulation() {
        let src = 0b11110000u64;
        let mask = 0b10101010u64;

        // Test PEXT (extract bits at positions 1, 3, 5, 7)
        let extracted = Bmi2BitOps::extract_bits(src, mask);
        // Should extract bits from positions 1,3,5,7 of src

        // Test PDEP (deposit bits into positions)
        let deposited = Bmi2BitOps::deposit_bits(0b1111u64, mask);
        // Should place bits 0,1,2,3 into positions 1,3,5,7

        println!("PEXT: {:#010x} -> {:#010x}", src, extracted);
        println!("PDEP: 0b1111 -> {:#010x}", deposited);

        // Test bit manipulation primitives
        let word = 0b10101000u64;
        let reset = Bmi2BitOps::reset_lowest_bit(word);
        let isolate = Bmi2BitOps::isolate_lowest_bit(word);
        let mask_to_lowest = Bmi2BitOps::mask_up_to_lowest_bit(word);

        println!("Original: {:#010x}", word);
        println!("Reset lowest: {:#010x}", reset);
        println!("Isolate lowest: {:#010x}", isolate);
        println!("Mask to lowest: {:#010x}", mask_to_lowest);
    }

    #[test]
    fn test_range_operations() {
        let word = 0xAAAAAAAAAAAAAAAAu64; // Alternating bits

        // Test range counting
        let count_0_8 = Bmi2RangeOps::count_ones_range(word, 0, 8);
        let count_8_8 = Bmi2RangeOps::count_ones_range(word, 8, 8);
        let count_60_8 = Bmi2RangeOps::count_ones_range(word, 60, 8);

        // Alternating pattern should have 4 ones in every 8 bits
        assert_eq!(count_0_8, 4);
        assert_eq!(count_8_8, 4);
        assert_eq!(count_60_8, 2); // Only 4 bits available

        // Test multi-range
        let ranges = vec![(0, 8), (8, 8), (16, 8), (24, 8)];
        let counts = Bmi2RangeOps::count_ones_multi_range(word, &ranges);
        assert_eq!(counts, vec![4, 4, 4, 4]);
    }

    #[test]
    fn test_bulk_operations() {
        let blocks = vec![
            0x0000000000000001u64, // 1 one
            0x0000000000000003u64, // 2 ones
            0x0000000000000007u64, // 3 ones
            0x000000000000000Fu64, // 4 ones
        ];

        // Test bulk rank
        let positions = vec![0, 64, 128, 192, 256];
        let ranks = Bmi2BlockOps::rank_bulk(&blocks, &positions);
        assert_eq!(ranks, vec![0, 1, 3, 6, 10]); // Cumulative ones

        // Test bulk select
        let indices = vec![0, 2, 5, 9];
        let selects = Bmi2BlockOps::select_bulk(&blocks, &indices).unwrap();

        println!("Bulk select results: {:?}", selects);

        // Verify select results
        for (i, &select_result) in selects.iter().enumerate() {
            let rank_at_result = if i < ranks.len() - 1 {
                Bmi2BlockOps::rank_bulk(&blocks, &[select_result])[0]
            } else {
                0
            };
            assert!(
                rank_at_result <= indices[i] + 1,
                "Select verification failed"
            );
        }
    }

    #[test]
    fn test_accelerator_interface() {
        let accel = Bmi2Accelerator::new();
        let stats = accel.stats();

        println!("BMI2 Accelerator stats: {:?}", stats);

        // Test accelerated operations
        let word = 0x0000000000000055u64; // Binary pattern with known ones

        let rank = accel.rank1(word, 8);
        let expected_rank = (word & ((1u64 << 8) - 1)).count_ones();
        assert_eq!(rank, expected_rank);

        if word.count_ones() > 0 {
            let select = accel.select1(word, 0);
            assert!(select.is_some());

            let pos = select.unwrap();
            assert!(
                (word >> pos) & 1 == 1,
                "Selected position should have a 1 bit"
            );
        }
    }

    #[test]
    fn test_popcount_bulk() {
        let words = vec![
            0x0000000000000000u64,
            0xFFFFFFFFFFFFFFFFu64,
            0xAAAAAAAAAAAAAAAAu64,
            0x5555555555555555u64,
            0x0F0F0F0F0F0F0F0Fu64,
        ];

        let bulk_result = Bmi2RankOps::popcount_bulk(&words);
        let individual_result: Vec<u32> = words
            .iter()
            .map(|&w| Bmi2RankOps::popcount_u64(w))
            .collect();

        assert_eq!(bulk_result, individual_result);
        assert_eq!(bulk_result, vec![0, 64, 32, 32, 32]);
    }

    #[test]
    fn test_bextr_operations() {
        // Test BEXTR bit field extraction
        let word = 0xABCDEF1234567890u64;
        
        // Extract lower 8 bits
        let lower_8 = Bmi2BextrOps::extract_bits_bextr(word, 0, 8);
        assert_eq!(lower_8, 0x90);
        
        // Extract bits 8-15
        let bits_8_15 = Bmi2BextrOps::extract_bits_bextr(word, 8, 8);
        assert_eq!(bits_8_15, 0x78);
        
        // Extract upper 16 bits
        let upper_16 = Bmi2BextrOps::extract_bits_bextr(word, 48, 16);
        assert_eq!(upper_16, 0xABCD);
        
        // Test edge cases
        assert_eq!(Bmi2BextrOps::extract_bits_bextr(word, 0, 0), 0);  // Zero length
        assert_eq!(Bmi2BextrOps::extract_bits_bextr(word, 64, 8), 0); // Start beyond word
        assert_eq!(Bmi2BextrOps::extract_bits_bextr(word, 60, 8), word >> 60); // Clamp length
    }
    
    #[test]
    fn test_bextr_multi_extraction() {
        let sources = vec![
            0x1111111111111111u64,
            0x2222222222222222u64,
            0x3333333333333333u64,
            0x4444444444444444u64,
        ];
        
        let results = Bmi2BextrOps::extract_bits_multi(&sources, 4, 4);
        
        // Each source has repeating nibbles, so extracting bits 4-7 should give:
        // 0x1111... -> 0x1, 0x2222... -> 0x2, etc.
        assert_eq!(results, vec![0x1, 0x2, 0x3, 0x4]);
    }
    
    #[test]
    fn test_hash_operations() {
        let hash = 0xABCDEF1234567890u64;
        
        // Test hash bucket extraction for 8-bit buckets (256 buckets)
        let bucket_8 = Bmi2HashOps::hash_bucket_extract(hash, 8);
        assert_eq!(bucket_8, 0x90);
        
        // Test hash bucket extraction for 4-bit buckets (16 buckets)
        let bucket_4 = Bmi2HashOps::hash_bucket_extract(hash, 4);
        assert_eq!(bucket_4, 0x0);
        
        // Test bulk hash bucket extraction
        let hashes = vec![0x1, 0x2, 0x3, 0x4];
        let buckets = Bmi2HashOps::hash_buckets_bulk(&hashes, 2);
        assert_eq!(buckets, vec![1, 2, 3, 0]); // Lower 2 bits
    }
    
    #[test]
    fn test_collision_resolution() {
        // Test collision resolution pattern
        let occupied_mask = 0b11110000u64; // Slots 4-7 occupied
        
        let first_free = Bmi2HashOps::collision_resolve_pattern(0, occupied_mask);
        assert_eq!(first_free, Some(0)); // First free slot at position 0
        
        // All slots occupied
        let all_occupied = Bmi2HashOps::collision_resolve_pattern(0, u64::MAX);
        assert_eq!(all_occupied, None);
    }
    
    #[test]
    fn test_bzhi_enhanced_operations() {
        let word = 0xFFFFFFFFFFFFFFFFu64;
        
        // Test enhanced BZHI popcount
        assert_eq!(Bmi2BzhiOps::popcount_bzhi_enhanced(word, 0), 0);
        assert_eq!(Bmi2BzhiOps::popcount_bzhi_enhanced(word, 32), 32);
        assert_eq!(Bmi2BzhiOps::popcount_bzhi_enhanced(word, 64), 64);
        assert_eq!(Bmi2BzhiOps::popcount_bzhi_enhanced(word, 100), 64); // Clamped
        
        // Test with alternating pattern
        let alt_word = 0xAAAAAAAAAAAAAAAAu64;
        assert_eq!(Bmi2BzhiOps::popcount_bzhi_enhanced(alt_word, 8), 4);
        assert_eq!(Bmi2BzhiOps::popcount_bzhi_enhanced(alt_word, 16), 8);
    }
    
    #[test]
    fn test_bzhi_bulk_operations() {
        let words = vec![
            0xFFFFFFFFFFFFFFFFu64,
            0xAAAAAAAAAAAAAAAAu64,
            0x5555555555555555u64,
            0x0F0F0F0F0F0F0F0Fu64,
        ];
        
        let results = Bmi2BzhiOps::bzhi_bulk_process(&words, 32);
        
        // Should mask to lower 32 bits
        assert_eq!(results[0], 0xFFFFFFFFu64);
        assert_eq!(results[1], 0xAAAAAAAAu64);
        assert_eq!(results[2], 0x55555555u64);
        assert_eq!(results[3], 0x0F0F0F0Fu64);
    }
    
    #[test]
    fn test_pdep_ctz_select() {
        let word = 0x0000000000000015u64; // Binary: ...00010101 (bits 0, 2, 4 set)
        
        // Test PDEP+CTZ select operations
        assert_eq!(Bmi2AdvancedPatterns::pdep_ctz_select(word, 0), Some(0)); // 1st one at position 0
        assert_eq!(Bmi2AdvancedPatterns::pdep_ctz_select(word, 1), Some(2)); // 2nd one at position 2
        assert_eq!(Bmi2AdvancedPatterns::pdep_ctz_select(word, 2), Some(4)); // 3rd one at position 4
        
        // Test out of bounds
        assert_eq!(Bmi2AdvancedPatterns::pdep_ctz_select(word, 3), None); // No 4th one
        assert_eq!(Bmi2AdvancedPatterns::pdep_ctz_select(0, 0), None); // Zero word
    }
    
    #[test]
    fn test_pdep_ctz_bulk_select() {
        let word = 0x000000000000001Fu64; // Binary: ...00011111 (bits 0-4 set)
        let indices = vec![0, 1, 2, 3, 4];
        
        let results = Bmi2AdvancedPatterns::pdep_ctz_select_bulk(word, &indices).unwrap();
        assert_eq!(results, vec![0, 1, 2, 3, 4]);
        
        // Test out of bounds
        let invalid_indices = vec![0, 5]; // 5 is out of bounds
        let error_result = Bmi2AdvancedPatterns::pdep_ctz_select_bulk(word, &invalid_indices);
        assert!(error_result.is_err());
    }
    
    #[test]
    fn test_pext_parallel_extract() {
        let sources = vec![
            0b11110000u64,
            0b10101010u64,
            0b11001100u64,
            0b11111111u64,
        ];
        let mask = 0b10101010u64; // Extract bits 1, 3, 5, 7
        
        let results = Bmi2AdvancedPatterns::pext_parallel_extract(&sources, mask);
        
        // Verify that bits at positions 1,3,5,7 are extracted
        for (i, result) in results.iter().enumerate() {
            let expected = Bmi2BitOps::extract_bits(sources[i], mask);
            assert_eq!(*result, expected);
        }
    }
    
    #[test]
    fn test_pdep_bzhi_composite() {
        let src = 0b1111u64;
        let mask = 0b10101010u64; // Positions 1, 3, 5, 7
        let bit_limit = 8;
        
        let result = Bmi2AdvancedPatterns::pdep_bzhi_composite(src, mask, bit_limit);
        
        // Should deposit bits into positions 1,3,5,7 and then limit to 8 bits
        let expected_deposited = Bmi2BitOps::deposit_bits(src, mask);
        let expected = expected_deposited & ((1u64 << bit_limit) - 1);
        assert_eq!(result, expected);
    }
    
    #[test]
    fn test_compression_operations() {
        let bit_stream = 0xABCDEF12u64;
        
        // Test entropy field extraction
        let field1 = Bmi2CompressionOps::extract_entropy_field(bit_stream, 0, 8);
        assert_eq!(field1, 0x12);
        
        let field2 = Bmi2CompressionOps::extract_entropy_field(bit_stream, 8, 8);
        assert_eq!(field2, 0xEF);
        
        // Test variable-length decoding
        let symbol_masks = vec![0x0000000Fu64, 0x000000F0u64, 0x00000F00u64];
        let symbols = Bmi2CompressionOps::decode_variable_length(0x123u64, &symbol_masks);
        assert_eq!(symbols[0], 0x3); // Lower 4 bits
        assert_eq!(symbols[1], 0x2); // Bits 4-7, extracted to lower bits
        assert_eq!(symbols[2], 0x1); // Bits 8-11, extracted to lower bits
    }
    
    #[test]
    fn test_string_operations() {
        let byte_data = 0x41424344u64; // ASCII "ABCD" (reversed)
        
        // Extract first character (first byte in little-endian: 0x44)
        let char1 = Bmi2StringOps::extract_utf8_char(byte_data, 0, 1);
        assert_eq!(char1, 0x44); // 'D' - first byte in little-endian 0x41424344
        
        // Extract second character (second byte)
        let char2 = Bmi2StringOps::extract_utf8_char(byte_data, 1, 1);
        assert_eq!(char2, 0x43); // 'C'
        
        // Test pattern matching
        let pattern_mask = 0xFF000000u64; // Extract highest byte
        let pattern_result = Bmi2StringOps::pattern_match_extract(byte_data, pattern_mask);
        assert_eq!(pattern_result, 0x41); // Extracted and shifted down (0x41 is the highest byte)
    }
    
    #[test]
    fn test_memory_operations() {
        let cache_line = 0xABCDEF1234567890u64;
        
        // Test cache-aligned field extraction
        let field1 = Bmi2MemoryOps::extract_cache_aligned_field(cache_line, 0, 16);
        assert_eq!(field1, 0x7890);
        
        let field2 = Bmi2MemoryOps::extract_cache_aligned_field(cache_line, 16, 16);
        assert_eq!(field2, 0x3456);
        
        // Test address calculation
        let base_addr = 0x10000000u64;
        let offset_pattern = 0x123u64;
        let layout_mask = 0xFFFu64;
        let addr = Bmi2MemoryOps::calculate_address_pattern(base_addr, offset_pattern, layout_mask);
        assert_eq!(addr, base_addr + Bmi2BitOps::deposit_bits(offset_pattern, layout_mask));
    }
    
    #[test]
    fn test_dispatcher_functionality() {
        let dispatcher = Bmi2Dispatcher::new();
        
        // Test dispatch operations
        let word = 0xAAAAAAAAAAAAAAAAu64;
        
        let popcount = dispatcher.dispatch_popcount(word);
        assert_eq!(popcount, 32);
        
        let select = dispatcher.dispatch_select(word, 0);
        assert_eq!(select, Some(1)); // First one bit at position 1
        
        let extracted = dispatcher.dispatch_extract_bits(word, 4, 4);
        assert_eq!(extracted, 0xA);
        
        let masked = dispatcher.dispatch_mask_operation(word, 32);
        assert_eq!(masked, 0xAAAAAAAAu64);
    }
    
    #[test]
    fn test_optimization_report() {
        let dispatcher = Bmi2Dispatcher::new();
        let report = dispatcher.optimization_report();
        
        // Should have basic operations available
        assert!(report.available_operations.contains(&"Basic bit operations"));
        
        // Check optimization tier is valid
        assert!(report.optimization_tier.starts_with("Tier"));
        
        println!("BMI2 Optimization Report: {:?}", report);
    }
    
    #[test]
    fn test_operation_trait_implementations() {
        let popcount_op = PopcountOperation;
        let select_op = SelectOperation;
        let extract_op = BitExtractionOperation;
        
        let test_data = vec![0xAAAAAAAAAAAAAAAAu64, 0x5555555555555555u64];
        
        // Test popcount operation
        let popcount_results = popcount_op.execute_bmi2(&test_data);
        assert_eq!(popcount_results, vec![32, 32]);
        assert_eq!(popcount_op.operation_name(), "Population Count");
        
        // Test select operation
        let select_results = select_op.execute_bmi2(&test_data);
        assert_eq!(select_results[0], 1); // First one at position 1 in 0xAAAA...
        assert_eq!(select_results[1], 0); // First one at position 0 in 0x5555...
        assert_eq!(select_op.operation_name(), "Select Operation");
        
        // Test bit extraction operation
        let extract_results = extract_op.execute_bmi2(&test_data);
        assert_eq!(extract_results, vec![0xAAAAAAAAu64, 0x55555555u64]); // Lower 32 bits
        assert_eq!(extract_op.operation_name(), "Bit Extraction");
    }
    
    #[test]
    fn test_accelerator_enhanced_interface() {
        let accel = Bmi2Accelerator::new();
        
        // Test enhanced bit extraction
        let word = 0xABCDEF1234567890u64;
        let extracted = accel.extract_bits(word, 16, 16);
        assert_eq!(extracted, 0x3456);
        
        // Test enhanced select
        let select_word = 0x0000000000000015u64; // bits 0, 2, 4 set
        let select_result = accel.select1_enhanced(select_word, 1);
        assert_eq!(select_result, Some(2));
        
        // Test hash operations
        let hash_bucket = accel.hash_bucket_extract(0xABCDEF12u64, 8);
        assert_eq!(hash_bucket, 0x12);
        
        // Test compression operations
        let entropy_field = accel.extract_entropy_field(0xABCDu64, 4, 8);
        assert_eq!(entropy_field, 0xBC);
        
        // Test string operations
        let utf8_char = accel.extract_utf8_char(0x41424344u64, 0, 1);
        assert_eq!(utf8_char, 0x44);
        
        // Test memory operations
        let cache_field = accel.extract_cache_aligned_field(0xABCDEF12u64, 8, 8);
        assert_eq!(cache_field, 0xEF);
        
        // Test optimization report
        let report = accel.optimization_report();
        println!("Enhanced Accelerator Report: {:?}", report);
    }
    
    #[test]
    fn test_edge_cases() {
        // Test edge cases for all operations

        // Zero word
        assert_eq!(Bmi2RankOps::popcount_u64(0), 0);
        assert_eq!(Bmi2RankOps::popcount_trail(0, 32), 0);
        assert_eq!(Bmi2SelectOps::select1_u64(0, 0), None);
        assert_eq!(Bmi2AdvancedPatterns::pdep_ctz_select(0, 0), None);

        // All ones word
        let all_ones = u64::MAX;
        assert_eq!(Bmi2RankOps::popcount_u64(all_ones), 64);
        assert_eq!(Bmi2RankOps::popcount_trail(all_ones, 32), 32);
        assert_eq!(Bmi2SelectOps::select1_u64(all_ones, 0), Some(0));
        assert_eq!(Bmi2SelectOps::select1_u64(all_ones, 63), Some(63));
        assert_eq!(Bmi2SelectOps::select1_u64(all_ones, 64), None);
        assert_eq!(Bmi2AdvancedPatterns::pdep_ctz_select(all_ones, 0), Some(0));
        assert_eq!(Bmi2AdvancedPatterns::pdep_ctz_select(all_ones, 63), Some(63));

        // Single bit words
        for bit_pos in [0, 1, 31, 32, 63] {
            let single_bit = 1u64 << bit_pos;
            assert_eq!(Bmi2RankOps::popcount_u64(single_bit), 1);
            assert_eq!(
                Bmi2SelectOps::select1_u64(single_bit, 0),
                Some(bit_pos as u32)
            );
            assert_eq!(Bmi2SelectOps::select1_u64(single_bit, 1), None);
            assert_eq!(
                Bmi2AdvancedPatterns::pdep_ctz_select(single_bit, 0),
                Some(bit_pos as u32)
            );
        }
        
        // BEXTR edge cases
        let word = 0xABCDEF1234567890u64;
        assert_eq!(Bmi2BextrOps::extract_bits_bextr(word, 0, 0), 0);  // Zero length
        assert_eq!(Bmi2BextrOps::extract_bits_bextr(word, 64, 8), 0); // Start beyond word
        assert_eq!(Bmi2BextrOps::extract_bits_bextr(word, 60, 8), word >> 60); // Clamp length
        
        // BZHI edge cases
        assert_eq!(Bmi2BzhiOps::popcount_bzhi_enhanced(word, 0), 0);
        assert_eq!(Bmi2BzhiOps::popcount_bzhi_enhanced(word, 100), word.count_ones()); // Clamp
    }

    #[test]
    fn test_performance_comparison() {
        // Performance comparison test (for manual verification)
        let test_words = (0..1000).map(|i| (i as u64).wrapping_mul(0x123456789ABCDEFu64)).collect::<Vec<_>>();
        
        // Test different operation types
        let start = std::time::Instant::now();
        let _popcount_results: Vec<u32> = test_words.iter()
            .map(|&w| Bmi2RankOps::popcount_u64(w))
            .collect();
        let popcount_time = start.elapsed();
        
        let start = std::time::Instant::now();
        let _select_results: Vec<Option<u32>> = test_words.iter()
            .filter(|&&w| w != 0)
            .map(|&w| Bmi2AdvancedPatterns::pdep_ctz_select(w, 0))
            .collect();
        let select_time = start.elapsed();
        
        let start = std::time::Instant::now();
        let _extract_results: Vec<u64> = test_words.iter()
            .map(|&w| Bmi2BextrOps::extract_bits_bextr(w, 8, 16))
            .collect();
        let extract_time = start.elapsed();
        
        println!("Performance test results:");
        println!("  Popcount: {:?}", popcount_time);
        println!("  Select: {:?}", select_time);
        println!("  Extract: {:?}", extract_time);
    }
    
    #[test]
    fn test_range_edge_cases() {
        let word = 0xFFFFFFFFFFFFFFFFu64;

        // Zero length range
        assert_eq!(Bmi2RangeOps::count_ones_range(word, 10, 0), 0);

        // Start beyond word
        assert_eq!(Bmi2RangeOps::count_ones_range(word, 64, 10), 0);
        assert_eq!(Bmi2RangeOps::count_ones_range(word, 100, 10), 0);

        // Range extending beyond word
        assert_eq!(Bmi2RangeOps::count_ones_range(word, 60, 10), 4); // Only 4 bits available

        // Full word range
        assert_eq!(Bmi2RangeOps::count_ones_range(word, 0, 64), 64);
        assert_eq!(Bmi2RangeOps::count_ones_range(word, 0, 100), 64); // Clamped to 64
    }
}