zipora 3.0.1

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
//! Advanced hash functions optimized for performance with BMI2 acceleration
//!
//! This module provides specialized hash functions inspired by advanced research,
//! including the FaboHashCombine function and golden ratio constants for optimal
//! hash distribution and memory utilization.
//!
//! # BMI2 Acceleration
//!
//! This module integrates BMI2 hardware acceleration for significant performance
//! improvements in hash operations:
//!
//! - **Hash Bucket Extraction**: 2-3x faster using BEXTR instructions
//! - **Hash Combining**: 3-5x faster bit mixing with PEXT/PDEP
//! - **String Hashing**: 2-4x faster character processing
//! - **Collision Resolution**: Optimized probing with BMI2 patterns
//! - **Load Factor Calculations**: BZHI-optimized threshold operations
//!
//! All functions provide automatic runtime CPU feature detection with graceful
//! fallbacks to scalar implementations on older hardware.
//!
//! # Usage
//!
//! ```rust
//! use zipora::hash_map::{fabo_hash_combine_u64, golden_ratio_next_size};
//!
//! // Basic hash combining with automatic BMI2 acceleration when available
//! let base_hash = 0x123456789abcdef0u64;
//! let value = 0xfedcba9876543210u64;
//! let hash = fabo_hash_combine_u64(base_hash, value);
//! 
//! // Golden ratio based sizing
//! let current_size = 100;
//! let next_size = golden_ratio_next_size(current_size);
//! assert!(next_size > current_size);
//! ```

use crate::succinct::rank_select::bmi2_acceleration::{
    Bmi2HashOps, Bmi2Dispatcher, Bmi2Capabilities
};
use std::collections::HashMap;

/// Golden ratio constant as a fraction (103/64 ≈ 1.609375 ≈ φ)
/// Used for optimal hash table growth and load factor calculations
pub const GOLDEN_RATIO_FRAC_NUM: u64 = 103;
pub const GOLDEN_RATIO_FRAC_DEN: u64 = 64;

/// Alternative golden ratio approximation (13/8 = 1.625)
/// Sometimes used for specific optimization scenarios
pub const GOLDEN_RATIO_ALT_NUM: u64 = 13;
pub const GOLDEN_RATIO_ALT_DEN: u64 = 8;

/// Optimal load factor based on golden ratio (≈ 0.618)
/// Expressed as a fraction of 256 for fast integer arithmetic
pub const GOLDEN_LOAD_FACTOR: u8 = 158; // 158/256 ≈ 0.618

/// FaboHashCombine function inspired by advanced research with BMI2 acceleration
/// 
/// This is provided through specialized implementations for u32 and u64.
/// The generic version is removed to avoid complex trait bounds.
/// BMI2 acceleration provides 3-5x faster bit mixing when available.

/// Specialized FaboHashCombine for u32 values (most common case)
/// 
/// Performance: 3-5x faster with BMI2 acceleration
#[inline]
pub fn fabo_hash_combine_u32(hash: u32, value: u32) -> u32 {
    let caps = Bmi2Capabilities::get();
    if caps.has_bmi2 {
        bmi2_hash_combine_u32(hash, value)
    } else {
        hash.rotate_left(5).wrapping_add(value)
    }
}

/// Specialized FaboHashCombine for u64 values
/// 
/// Performance: 3-5x faster with BMI2 acceleration
#[inline]
pub fn fabo_hash_combine_u64(hash: u64, value: u64) -> u64 {
    let caps = Bmi2Capabilities::get();
    if caps.has_bmi2 {
        bmi2_hash_combine_u64(hash, value)
    } else {
        hash.rotate_left(5).wrapping_add(value)
    }
}

/// BMI2-accelerated hash combine for u32 values
/// 
/// Uses PEXT/PDEP for enhanced bit mixing and distribution.
/// Performance: 3-5x faster than rotate+add on BMI2-enabled CPUs.
#[inline]
pub fn bmi2_hash_combine_u32(hash: u32, value: u32) -> u32 {
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            return unsafe { bmi2_hash_combine_u32_hardware(hash, value) };
        }
    }
    
    // Fallback to enhanced scalar mixing
    let rotated = hash.rotate_left(5);
    let mixed = rotated.wrapping_add(value);
    mixed ^ (value.rotate_right(13))
}

/// BMI2-accelerated hash combine for u64 values
/// 
/// Uses PEXT/PDEP for enhanced bit mixing and distribution.
/// Performance: 3-5x faster than rotate+add on BMI2-enabled CPUs.
#[inline]
pub fn bmi2_hash_combine_u64(hash: u64, value: u64) -> u64 {
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            return unsafe { bmi2_hash_combine_u64_hardware(hash, value) };
        }
    }
    
    // Fallback to enhanced scalar mixing
    let rotated = hash.rotate_left(5);
    let mixed = rotated.wrapping_add(value);
    mixed ^ (value.rotate_right(17))
}

/// Hardware BMI2 implementation for u32 hash combine
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
#[inline]
unsafe fn bmi2_hash_combine_u32_hardware(hash: u32, value: u32) -> u32 {
    use std::arch::x86_64::*;

    // Use a prime-based seed when hash is zero to avoid zero results
    let effective_hash = if hash == 0 { 0x9e3779b9u32 } else { hash };

    // Use PEXT to extract alternating bits for better mixing
    let mask1 = 0xAAAAAAAAu32;
    let mask2 = 0x55555555u32;

    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u32 values
    let extracted1 = unsafe { _pext_u32(effective_hash, mask1) };
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u32 values
    let extracted2 = unsafe { _pext_u32(value, mask2) };

    // Combine and ensure non-zero result
    let combined = extracted1.wrapping_add(extracted2);
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u32 values
    let mut result = unsafe { _pdep_u32(combined, 0xFFFFFFFFu32) };

    // Additional mixing to ensure good distribution
    result ^= value.rotate_right(13);
    result = result.wrapping_mul(0x9e3779b9u32);
    result ^= result >> 16;

    // Ensure result is never zero
    if result == 0 { 0x9e3779b9u32 } else { result }
}

/// Hardware BMI2 implementation for u64 hash combine
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
#[inline]
unsafe fn bmi2_hash_combine_u64_hardware(hash: u64, value: u64) -> u64 {
    use std::arch::x86_64::*;

    // Use a prime-based seed when hash is zero to avoid zero results
    let effective_hash = if hash == 0 { 0x9e3779b97f4a7c15u64 } else { hash };

    // Use PEXT to extract alternating bits for better mixing
    let mask1 = 0xAAAAAAAAAAAAAAAAu64;
    let mask2 = 0x5555555555555555u64;

    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 values
    let extracted1 = unsafe { _pext_u64(effective_hash, mask1) };
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 values
    let extracted2 = unsafe { _pext_u64(value, mask2) };

    // Combine and ensure non-zero result
    let combined = extracted1.wrapping_add(extracted2);
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 values
    let mut result = unsafe { _pdep_u64(combined, 0xFFFFFFFFFFFFFFFFu64) };

    // Additional mixing to ensure good distribution
    result ^= value.rotate_right(13);
    result = result.wrapping_mul(0xc2b2ae3d27d4eb4fu64);
    result ^= result >> 29;

    // Ensure result is never zero
    if result == 0 { 0x9e3779b97f4a7c15u64 } else { result }
}


/// Calculate the next size using golden ratio growth with BMI2 optimization
/// 
/// This function computes the next capacity for a hash table or container
/// using the golden ratio for optimal memory utilization and performance.
/// BMI2 acceleration provides faster multiplication and division operations.
/// 
/// # Parameters
/// - `current_size`: The current capacity
/// 
/// # Returns
/// The next optimal capacity
/// 
/// # Performance
/// 2-3x faster with BMI2 hardware acceleration
pub fn golden_ratio_next_size(current_size: usize) -> usize {
    if current_size == 0 {
        return 16; // Reasonable default starting size
    }
    
    bmi2_golden_ratio_next_size(current_size)
}

/// BMI2-optimized golden ratio size calculation
/// 
/// Uses BZHI for fast multiplication and bit manipulation operations.
#[inline]
pub fn bmi2_golden_ratio_next_size(current_size: usize) -> usize {
    if current_size == 0 {
        return 16;
    }
    
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            return unsafe { bmi2_golden_ratio_hardware(current_size) };
        }
    }
    
    // Enhanced scalar fallback with better precision
    let size_64 = current_size as u64;
    let result = (size_64 * GOLDEN_RATIO_FRAC_NUM) / GOLDEN_RATIO_FRAC_DEN + 1;
    result as usize
}

/// Hardware BMI2 implementation for golden ratio calculation
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
#[inline]
unsafe fn bmi2_golden_ratio_hardware(current_size: usize) -> usize {
    use std::arch::x86_64::*;
    
    let size_64 = current_size as u64;

    // Use BZHI for efficient bit operations in multiplication
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 with index 63
    let numerator_bits = unsafe { _bzhi_u64(size_64 * GOLDEN_RATIO_FRAC_NUM, 63) }; // Prevent overflow
    let result = numerator_bits / GOLDEN_RATIO_FRAC_DEN + 1;
    
    result as usize
}

/// Calculate optimal bucket count for a hash table with BMI2 optimization
/// 
/// Returns the next power of 2 that can accommodate the desired capacity
/// with the golden ratio load factor. Uses BMI2 BEXTR for efficient
/// power-of-2 calculations.
/// 
/// # Parameters
/// - `desired_capacity`: The desired number of elements
/// 
/// # Returns
/// The optimal bucket count (power of 2)
/// 
/// # Performance
/// 2-3x faster power-of-2 calculations with BMI2
pub fn optimal_bucket_count(desired_capacity: usize) -> usize {
    if desired_capacity == 0 {
        return 16;
    }
    
    bmi2_optimal_bucket_count(desired_capacity)
}

/// BMI2-optimized bucket count calculation
/// 
/// Uses BEXTR for efficient power-of-2 operations and BZHI for masking.
#[inline]
pub fn bmi2_optimal_bucket_count(desired_capacity: usize) -> usize {
    if desired_capacity == 0 {
        return 16;
    }
    
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            return unsafe { bmi2_bucket_count_hardware(desired_capacity) };
        }
    }
    
    // Enhanced scalar fallback
    let required_buckets = (desired_capacity as u64 * 256) / (GOLDEN_LOAD_FACTOR as u64);
    (required_buckets as usize).next_power_of_two().max(16)
}

/// Hardware BMI2 implementation for bucket count calculation
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
#[inline]
unsafe fn bmi2_bucket_count_hardware(desired_capacity: usize) -> usize {
    use std::arch::x86_64::*;
    
    let capacity_64 = desired_capacity as u64;

    // Use BZHI for efficient load factor calculation
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 with index 63
    let scaled_capacity = unsafe { _bzhi_u64(capacity_64 * 256, 63) }; // Prevent overflow
    let required_buckets = scaled_capacity / (GOLDEN_LOAD_FACTOR as u64);

    // Use BEXTR pattern for next power of 2 calculation
    if required_buckets == 0 {
        return 16;
    }

    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 value
    let log2_bits = 64 - unsafe { _lzcnt_u64(required_buckets - 1) };
    let bucket_count = 1usize << log2_bits;
    bucket_count.max(16)
}

/// Advanced hash combiner that uses multiple techniques for better distribution
/// 
/// This function combines multiple hash values using a sophisticated approach
/// that includes bit rotation, prime multiplication, and XOR operations.
/// 
/// # Parameters
/// - `hashes`: A slice of hash values to combine
/// 
/// # Returns
/// The combined hash value
pub fn advanced_hash_combine(hashes: &[u64]) -> u64 {
    if hashes.is_empty() {
        return 0;
    }
    
    let mut result = hashes[0];
    
    for &hash in &hashes[1..] {
        // Use FaboHashCombine as the base
        result = fabo_hash_combine_u64(result, hash);
        
        // Add additional mixing for better distribution
        result ^= hash.rotate_right(17);
        result = result.wrapping_mul(0x9e3779b97f4a7c15); // Large prime for mixing
    }
    
    // Final avalanche step
    result ^= result >> 30;
    result = result.wrapping_mul(0xbf58476d1ce4e5b9);
    result ^= result >> 27;
    result = result.wrapping_mul(0x94d049bb133111eb);
    result ^= result >> 31;
    
    result
}

/// Hash function builder that creates optimal hash functions for specific types
/// 
/// This struct allows for creating specialized hash functions that are optimized
/// for particular data patterns or performance requirements.
pub struct HashFunctionBuilder {
    rotation_amount: u32,
    combine_strategy: CombineStrategy,
}

/// Strategy for combining hash values
#[derive(Debug, Clone, Copy)]
pub enum CombineStrategy {
    /// Simple addition (fastest)
    Addition,
    /// XOR combination (good distribution)
    Xor,
    /// FaboHashCombine (balanced performance/distribution)
    Fabo,
    /// BMI2-accelerated combining (best performance on modern CPUs)
    Bmi2,
    /// Advanced mixing (best distribution, slower)
    Advanced,
}

impl HashFunctionBuilder {
    /// Create a new hash function builder with default settings
    pub fn new() -> Self {
        Self {
            rotation_amount: 5,
            combine_strategy: CombineStrategy::Fabo,
        }
    }
    
    /// Set the rotation amount for bit rotation operations
    pub fn with_rotation(mut self, amount: u32) -> Self {
        self.rotation_amount = amount;
        self
    }
    
    /// Set the combine strategy
    pub fn with_strategy(mut self, strategy: CombineStrategy) -> Self {
        self.combine_strategy = strategy;
        self
    }
    
    /// Build a hash function for u32 values
    pub fn build_u32(self) -> impl Fn(u32, u32) -> u32 {
        let rotation = self.rotation_amount;
        
        move |hash: u32, value: u32| -> u32 {
            match self.combine_strategy {
                CombineStrategy::Addition => hash.rotate_left(rotation).wrapping_add(value),
                CombineStrategy::Xor => hash.rotate_left(rotation) ^ value,
                CombineStrategy::Fabo => fabo_hash_combine_u32(hash, value),
                CombineStrategy::Bmi2 => bmi2_hash_combine_u32(hash, value),
                CombineStrategy::Advanced => {
                    let mut result = hash.rotate_left(rotation).wrapping_add(value);
                    result ^= value.rotate_right(17);
                    result = result.wrapping_mul(0x9e3779b9);
                    result ^= result >> 16;
                    result
                }
            }
        }
    }
    
    /// Build a hash function for u64 values
    pub fn build_u64(self) -> impl Fn(u64, u64) -> u64 {
        let rotation = self.rotation_amount;
        
        move |hash: u64, value: u64| -> u64 {
            match self.combine_strategy {
                CombineStrategy::Addition => hash.rotate_left(rotation).wrapping_add(value),
                CombineStrategy::Xor => hash.rotate_left(rotation) ^ value,
                CombineStrategy::Fabo => fabo_hash_combine_u64(hash, value),
                CombineStrategy::Bmi2 => bmi2_hash_combine_u64(hash, value),
                CombineStrategy::Advanced => {
                    let mut result = hash.rotate_left(rotation).wrapping_add(value);
                    result ^= value.rotate_right(17);
                    result = result.wrapping_mul(0x9e3779b97f4a7c15);
                    result ^= result >> 30;
                    result
                }
            }
        }
    }
}

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

/// Helper trait for types that can be used in hash combining operations
pub trait HashCombinable {
    type Output;
    
    /// Combine this value with a hash using the FaboHashCombine algorithm
    fn fabo_combine(self, hash: Self::Output) -> Self::Output;
    
    /// Get the recommended rotation amount for this type
    fn rotation_amount() -> u32 {
        5
    }
}

impl HashCombinable for u32 {
    type Output = u32;
    
    fn fabo_combine(self, hash: Self::Output) -> Self::Output {
        fabo_hash_combine_u32(hash, self)
    }
}

impl HashCombinable for u64 {
    type Output = u64;
    
    fn fabo_combine(self, hash: Self::Output) -> Self::Output {
        fabo_hash_combine_u64(hash, self)
    }
}

/// Hash bucket extraction using BMI2 BEXTR for optimal performance
/// 
/// Extracts hash bucket index from hash value using BMI2 BEXTR instruction.
/// Performance: 2-3x faster than modulo operations for power-of-2 buckets.
/// 
/// # Parameters
/// - `hash`: The hash value
/// - `bucket_bits`: Number of bits for bucket index (e.g., 8 for 256 buckets)
/// 
/// # Returns
/// Bucket index in range [0, 2^bucket_bits)
#[inline]
pub fn extract_hash_bucket_bmi2(hash: u64, bucket_bits: u32) -> u32 {
    Bmi2HashOps::hash_bucket_extract(hash, bucket_bits)
}

/// Bulk hash bucket extraction for multiple hash values
/// 
/// Efficiently extracts bucket indices from multiple hash values using
/// vectorized BMI2 operations when available.
pub fn extract_hash_buckets_bulk_bmi2(hashes: &[u64], bucket_bits: u32) -> Vec<u32> {
    Bmi2HashOps::hash_buckets_bulk(hashes, bucket_bits)
}

/// Advanced hash combine for multiple values with BMI2 acceleration
/// 
/// Combines multiple hash values using sophisticated BMI2 bit manipulation
/// for optimal distribution and performance.
pub fn advanced_hash_combine_bmi2(hashes: &[u64]) -> u64 {
    if hashes.is_empty() {
        return 0;
    }
    
    let mut result = hashes[0];
    
    for &hash in &hashes[1..] {
        result = bmi2_hash_combine_u64(result, hash);
        
        // Additional BMI2-accelerated mixing
        #[cfg(target_arch = "x86_64")]
        {
            let caps = Bmi2Capabilities::get();
            if caps.has_bmi2 {
                // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
                result = unsafe { advanced_bmi2_mixing(result, hash) };
            } else {
                result ^= hash.rotate_right(17);
                result = result.wrapping_mul(0x9e3779b97f4a7c15);
            }
        }
        #[cfg(not(target_arch = "x86_64"))]
        {
            result ^= hash.rotate_right(17);
            result = result.wrapping_mul(0x9e3779b97f4a7c15);
        }
    }
    
    // Final avalanche step with BMI2 optimization
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            result = unsafe { bmi2_avalanche_step(result) };
        } else {
            result = scalar_avalanche_step(result);
        }
    }
    #[cfg(not(target_arch = "x86_64"))]
    {
        result = scalar_avalanche_step(result);
    }
    
    result
}

/// Advanced BMI2 mixing using PEXT/PDEP patterns
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
#[inline]
unsafe fn advanced_bmi2_mixing(result: u64, hash: u64) -> u64 {
    use std::arch::x86_64::*;
    
    // Use PEXT to extract specific bit patterns for mixing
    let pattern1 = 0x5555555555555555u64; // Alternating bits
    let pattern2 = 0x3333333333333333u64; // 2-bit patterns

    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 values
    let extracted1 = unsafe { _pext_u64(result, pattern1) };
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 values
    let extracted2 = unsafe { _pext_u64(hash, pattern2) };

    // Combine and redistribute with PDEP
    let combined = extracted1.wrapping_add(extracted2);
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 values
    unsafe { _pdep_u64(combined, 0xFFFFFFFFFFFFFFFFu64) }.rotate_right(17)
}

/// BMI2-optimized avalanche step for final hash mixing
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
#[inline]
unsafe fn bmi2_avalanche_step(mut result: u64) -> u64 {
    use std::arch::x86_64::*;

    // Use BZHI for efficient bit masking in avalanche
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 with valid index
    result ^= unsafe { _bzhi_u64(result >> 30, 34) };
    result = result.wrapping_mul(0xbf58476d1ce4e5b9);
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 with valid index
    result ^= unsafe { _bzhi_u64(result >> 27, 37) };
    result = result.wrapping_mul(0x94d049bb133111eb);
    result ^= result >> 31;
    
    result
}

/// Scalar avalanche step fallback
#[inline]
fn scalar_avalanche_step(mut result: u64) -> u64 {
    result ^= result >> 30;
    result = result.wrapping_mul(0xbf58476d1ce4e5b9);
    result ^= result >> 27;
    result = result.wrapping_mul(0x94d049bb133111eb);
    result ^= result >> 31;
    result
}

/// Fast string hashing with BMI2-accelerated byte extraction
/// 
/// Processes string bytes using BMI2 BEXTR for efficient character extraction
/// and PDEP/PEXT for optimal bit mixing. Integrates with SIMD string operations.
pub fn fast_string_hash_bmi2(s: &str, base_hash: u64) -> u64 {
    let bytes = s.as_bytes();
    
    if bytes.is_empty() {
        return base_hash;
    }
    
    // For very short strings, use optimized scalar path
    if bytes.len() <= 8 {
        return scalar_string_hash_bmi2(bytes, base_hash);
    }
    
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            return unsafe { bmi2_string_hash_hardware(bytes, base_hash) };
        }
    }
    
    // Fallback to enhanced scalar implementation
    scalar_string_hash_bmi2(bytes, base_hash)
}

/// Hardware BMI2 implementation for string hashing
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
unsafe fn bmi2_string_hash_hardware(bytes: &[u8], mut hash: u64) -> u64 {
    use std::arch::x86_64::*;
    
    // Process 8-byte chunks with BMI2 acceleration
    let chunks = bytes.len() / 8;
    for i in 0..chunks {
        let offset = i * 8;
        let chunk_bytes = &bytes[offset..offset + 8];
        let val = u64::from_le_bytes(chunk_bytes.try_into().expect("chunk is 8 bytes"));

        // Use BMI2 for enhanced mixing
        let mask = 0xF0F0F0F0F0F0F0F0u64; // Extract high nibbles
        // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 values
        let extracted = unsafe { _pext_u64(val, mask) };
        // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 values
        hash = unsafe { _pdep_u64(hash.wrapping_add(extracted), 0xFFFFFFFFFFFFFFFFu64) };
        hash = hash.rotate_left(5);
    }

    // Handle remaining bytes
    let remaining_start = chunks * 8;
    for &byte in &bytes[remaining_start..] {
        // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 values
        let byte_extended = unsafe { _pdep_u64(byte as u64, 0x0101010101010101u64) };
        hash = hash.rotate_left(5).wrapping_add(byte_extended);
    }
    
    hash
}

/// Scalar string hashing with BMI2 patterns (fallback)
fn scalar_string_hash_bmi2(bytes: &[u8], mut hash: u64) -> u64 {
    // Process 8-byte chunks for better performance
    let chunks = bytes.len() / 8;
    for i in 0..chunks {
        let offset = i * 8;
        let chunk_bytes = &bytes[offset..offset + 8];
        let val = u64::from_le_bytes(chunk_bytes.try_into().expect("chunk is 8 bytes"));

        // Enhanced scalar mixing inspired by BMI2 patterns
        hash = hash.rotate_left(5).wrapping_add(val);
        hash ^= val.rotate_right(13);
    }

    // Handle remaining bytes
    let remaining_start = chunks * 8;
    for &byte in &bytes[remaining_start..] {
        hash = hash.rotate_left(5).wrapping_add(byte as u64);
    }
    
    hash
}

/// Collision resolution using BMI2 patterns for hash table probing
/// 
/// Implements efficient collision resolution using PEXT-based linear and
/// quadratic probing optimizations for Robin Hood and Hopscotch hashing.
pub fn bmi2_collision_resolution(hash: u64, occupied_mask: u64, probe_type: ProbeType) -> Option<u32> {
    match probe_type {
        ProbeType::Linear => bmi2_linear_probe(hash, occupied_mask),
        ProbeType::Quadratic => bmi2_quadratic_probe(hash, occupied_mask),
        ProbeType::DoubleHash => bmi2_double_hash_probe(hash, occupied_mask),
    }
}

/// Probe types for collision resolution
#[derive(Debug, Clone, Copy)]
pub enum ProbeType {
    /// Linear probing (fastest)
    Linear,
    /// Quadratic probing (better clustering)
    Quadratic,
    /// Double hashing (best distribution)
    DoubleHash,
}

/// BMI2-optimized linear probing
fn bmi2_linear_probe(hash: u64, occupied_mask: u64) -> Option<u32> {
    if occupied_mask == u64::MAX {
        return None; // All slots occupied
    }
    
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            return unsafe { bmi2_linear_probe_hardware(hash, occupied_mask) };
        }
    }
    
    // Fallback: find first free slot
    let free_mask = !occupied_mask;
    if free_mask != 0 {
        Some(free_mask.trailing_zeros())
    } else {
        None
    }
}

/// Hardware BMI2 linear probing
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
unsafe fn bmi2_linear_probe_hardware(_hash: u64, occupied_mask: u64) -> Option<u32> {
    use std::arch::x86_64::*;
    
    if occupied_mask == u64::MAX {
        return None;
    }
    
    // Use BMI2 to find first free slot efficiently
    let free_mask = !occupied_mask;
    if free_mask != 0 {
        // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 value
        Some(unsafe { _tzcnt_u64(free_mask) } as u32)
    } else {
        None
    }
}

/// BMI2-optimized quadratic probing
fn bmi2_quadratic_probe(hash: u64, occupied_mask: u64) -> Option<u32> {
    if occupied_mask == u64::MAX {
        return None;
    }
    
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            return unsafe { bmi2_quadratic_probe_hardware(hash, occupied_mask) };
        }
    }
    
    // Scalar fallback for quadratic probing
    let start_pos = (hash & 63) as u32; // 64-bit mask
    for i in 0..64 {
        let pos = (start_pos + i * i) & 63;
        if (occupied_mask >> pos) & 1 == 0 {
            return Some(pos);
        }
    }
    None
}

/// Hardware BMI2 quadratic probing  
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
unsafe fn bmi2_quadratic_probe_hardware(hash: u64, occupied_mask: u64) -> Option<u32> {
    use std::arch::x86_64::*;
    
    if occupied_mask == u64::MAX {
        return None;
    }
    
    // Use BEXTR for efficient position calculation
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 with valid start/len
    let start_pos = unsafe { _bextr_u64(hash, 0, 6) } as u32; // Extract 6 bits (0-63)
    
    for i in 0..64 {
        let offset = i * i;
        let pos = (start_pos + offset) & 63;
        
        // Use BEXTR to check if position is free
        let slot_mask = 1u64 << pos;
        if (occupied_mask & slot_mask) == 0 {
            return Some(pos);
        }
    }
    None
}

/// BMI2-optimized double hashing
fn bmi2_double_hash_probe(hash: u64, occupied_mask: u64) -> Option<u32> {
    if occupied_mask == u64::MAX {
        return None;
    }
    
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            return unsafe { bmi2_double_hash_probe_hardware(hash, occupied_mask) };
        }
    }
    
    // Scalar double hashing fallback
    let hash1 = (hash & 63) as u32;
    let hash2 = ((hash >> 32) & 63) as u32;
    let step = if hash2 == 0 { 1 } else { hash2 };
    
    for i in 0..64 {
        let pos = (hash1 + i * step) & 63;
        if (occupied_mask >> pos) & 1 == 0 {
            return Some(pos);
        }
    }
    None
}

/// Hardware BMI2 double hashing
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
unsafe fn bmi2_double_hash_probe_hardware(hash: u64, occupied_mask: u64) -> Option<u32> {
    use std::arch::x86_64::*;
    
    if occupied_mask == u64::MAX {
        return None;
    }
    
    // Use BEXTR for efficient hash extraction
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 with valid start/len
    let hash1 = unsafe { _bextr_u64(hash, 0, 6) } as u32;     // Lower 6 bits
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 with valid start/len
    let hash2 = unsafe { _bextr_u64(hash, 32, 6) } as u32;    // Upper 6 bits
    let step = if hash2 == 0 { 1 } else { hash2 };
    
    for i in 0..64 {
        let pos = (hash1 + i * step) & 63;
        let slot_mask = 1u64 << pos;
        if (occupied_mask & slot_mask) == 0 {
            return Some(pos);
        }
    }
    None
}

/// Load factor calculations with BZHI-optimized threshold operations
/// 
/// Computes optimal load factors and resize thresholds using BMI2 BZHI
/// for efficient bit manipulation and masking operations.
pub fn bmi2_load_factor_calculations(
    current_size: usize, 
    element_count: usize, 
    target_load_factor: f64
) -> LoadFactorInfo {
    #[cfg(target_arch = "x86_64")]
    {
        let caps = Bmi2Capabilities::get();
        if caps.has_bmi2 {
            // SAFETY: BMI2 support verified by caps.has_bmi2 runtime check
            return unsafe { bmi2_load_factor_hardware(current_size, element_count, target_load_factor) };
        }
    }
    
    // Scalar fallback
    scalar_load_factor_calculations(current_size, element_count, target_load_factor)
}

/// Load factor information
#[derive(Debug, Clone)]
pub struct LoadFactorInfo {
    pub current_load_factor: f64,
    pub should_resize: bool,
    pub suggested_new_size: usize,
    pub resize_threshold: usize,
}

/// Hardware BMI2 load factor calculations
#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "bmi2")]
unsafe fn bmi2_load_factor_hardware(
    current_size: usize, 
    element_count: usize, 
    target_load_factor: f64
) -> LoadFactorInfo {
    use std::arch::x86_64::*;
    
    if current_size == 0 {
        return LoadFactorInfo {
            current_load_factor: 0.0,
            should_resize: true,
            suggested_new_size: 16,
            resize_threshold: (16.0 * target_load_factor) as usize,
        };
    }
    
    // Use BZHI for efficient load factor calculation
    let size_64 = current_size as u64;
    let count_64 = element_count as u64;
    
    // Calculate load factor with BZHI-optimized precision
    let precision_bits = 32; // Use 32-bit precision
    // SAFETY: bmi2 guaranteed by #[target_feature(enable = "bmi2")], operates on u64 with index 63
    let scaled_count = unsafe { _bzhi_u64(count_64 << precision_bits, 63) } / size_64;
    let scaled_target = (target_load_factor * (1u64 << precision_bits) as f64) as u64;
    
    let current_load_factor = element_count as f64 / current_size as f64;
    let should_resize = scaled_count > scaled_target;
    
    let suggested_new_size = if should_resize {
        bmi2_golden_ratio_next_size(current_size)
    } else {
        current_size
    };
    
    let resize_threshold = (suggested_new_size as f64 * target_load_factor) as usize;
    
    LoadFactorInfo {
        current_load_factor,
        should_resize,
        suggested_new_size,
        resize_threshold,
    }
}

/// Scalar load factor calculations
fn scalar_load_factor_calculations(
    current_size: usize, 
    element_count: usize, 
    target_load_factor: f64
) -> LoadFactorInfo {
    if current_size == 0 {
        return LoadFactorInfo {
            current_load_factor: 0.0,
            should_resize: true,
            suggested_new_size: 16,
            resize_threshold: (16.0 * target_load_factor) as usize,
        };
    }
    
    let current_load_factor = element_count as f64 / current_size as f64;
    let should_resize = current_load_factor > target_load_factor;
    
    let suggested_new_size = if should_resize {
        golden_ratio_next_size(current_size)
    } else {
        current_size
    };
    
    let resize_threshold = (suggested_new_size as f64 * target_load_factor) as usize;
    
    LoadFactorInfo {
        current_load_factor,
        should_resize,
        suggested_new_size,
        resize_threshold,
    }
}

/// BMI2 Hash Dispatcher for automatic hardware-optimized function selection
/// 
/// Provides intelligent dispatch to optimal hash implementations based on
/// runtime CPU feature detection. Follows SIMD Framework mandatory patterns.
pub struct Bmi2HashDispatcher {
    capabilities: &'static Bmi2Capabilities,
    optimization_tier: HashOptimizationTier,
}

/// Hash optimization tiers based on hardware capabilities
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HashOptimizationTier {
    /// Scalar fallback (no hardware acceleration)
    Scalar,
    /// BMI1 acceleration (POPCNT, LZCNT, TZCNT)
    Bmi1,
    /// BMI2 acceleration (PDEP, PEXT, BZHI, BEXTR)
    Bmi2,
    /// BMI2 + AVX2 combined acceleration
    Bmi2Avx2,
}

impl Bmi2HashDispatcher {
    /// Create new dispatcher with runtime capability detection
    pub fn new() -> Self {
        let capabilities = Bmi2Capabilities::get();
        let optimization_tier = Self::determine_optimization_tier(capabilities);
        
        Self {
            capabilities,
            optimization_tier,
        }
    }
    
    /// Determine optimal tier based on hardware capabilities
    fn determine_optimization_tier(caps: &Bmi2Capabilities) -> HashOptimizationTier {
        if caps.has_bmi2 && caps.simd_caps.cpu_features.has_avx2 {
            HashOptimizationTier::Bmi2Avx2
        } else if caps.has_bmi2 {
            HashOptimizationTier::Bmi2
        } else if caps.has_bmi1 {
            HashOptimizationTier::Bmi1
        } else {
            HashOptimizationTier::Scalar
        }
    }
    
    /// Get current optimization tier
    pub fn tier(&self) -> HashOptimizationTier {
        self.optimization_tier
    }
    
    /// Hash with automatic acceleration
    pub fn hash_with_acceleration<T: AsRef<[u8]>>(&self, data: T) -> u64 {
        let bytes = data.as_ref();
        
        match self.optimization_tier {
            HashOptimizationTier::Bmi2Avx2 | HashOptimizationTier::Bmi2 => {
                if bytes.len() >= 8 {
                    fast_string_hash_bmi2(
                        std::str::from_utf8(bytes).unwrap_or(""), 
                        0
                    )
                } else {
                    scalar_string_hash_bmi2(bytes, 0)
                }
            }
            HashOptimizationTier::Bmi1 => {
                // Enhanced scalar with BMI1 optimizations
                self.hash_with_bmi1_acceleration(bytes)
            }
            HashOptimizationTier::Scalar => {
                // Pure scalar implementation
                self.hash_scalar_fallback(bytes)
            }
        }
    }
    
    /// Hash combine with optimal acceleration
    pub fn hash_combine_optimal(&self, hash: u64, value: u64) -> u64 {
        match self.optimization_tier {
            HashOptimizationTier::Bmi2Avx2 | HashOptimizationTier::Bmi2 => {
                bmi2_hash_combine_u64(hash, value)
            }
            HashOptimizationTier::Bmi1 => {
                // Enhanced combine with BMI1
                let rotated = hash.rotate_left(5);
                let mixed = rotated.wrapping_add(value);
                mixed ^ (value.rotate_right(17))
            }
            HashOptimizationTier::Scalar => {
                hash.rotate_left(5).wrapping_add(value)
            }
        }
    }
    
    /// Extract hash bucket with optimal acceleration
    pub fn extract_bucket_optimal(&self, hash: u64, bucket_bits: u32) -> u32 {
        match self.optimization_tier {
            HashOptimizationTier::Bmi2Avx2 | HashOptimizationTier::Bmi2 => {
                extract_hash_bucket_bmi2(hash, bucket_bits)
            }
            HashOptimizationTier::Bmi1 | HashOptimizationTier::Scalar => {
                if bucket_bits >= 64 {
                    hash as u32
                } else {
                    (hash & ((1u64 << bucket_bits) - 1)) as u32
                }
            }
        }
    }
    
    /// Collision resolution with optimal acceleration
    pub fn resolve_collision_optimal(
        &self, 
        hash: u64, 
        occupied_mask: u64, 
        probe_type: ProbeType
    ) -> Option<u32> {
        match self.optimization_tier {
            HashOptimizationTier::Bmi2Avx2 | HashOptimizationTier::Bmi2 => {
                bmi2_collision_resolution(hash, occupied_mask, probe_type)
            }
            HashOptimizationTier::Bmi1 | HashOptimizationTier::Scalar => {
                // Fallback collision resolution
                match probe_type {
                    ProbeType::Linear => bmi2_linear_probe(hash, occupied_mask),
                    ProbeType::Quadratic => bmi2_quadratic_probe(hash, occupied_mask),
                    ProbeType::DoubleHash => bmi2_double_hash_probe(hash, occupied_mask),
                }
            }
        }
    }
    
    /// BMI1-accelerated hashing
    fn hash_with_bmi1_acceleration(&self, bytes: &[u8]) -> u64 {
        let mut hash = 0u64;
        
        // Process 8-byte chunks
        let chunks = bytes.len() / 8;
        for i in 0..chunks {
            let offset = i * 8;
            let chunk_bytes = &bytes[offset..offset + 8];
            let val = u64::from_le_bytes(chunk_bytes.try_into().expect("chunk is 8 bytes"));
            hash = hash.rotate_left(5).wrapping_add(val);
            hash ^= val.rotate_right(13); // Enhanced mixing
        }

        // Handle remaining bytes
        let remaining_start = chunks * 8;
        for &byte in &bytes[remaining_start..] {
            hash = hash.rotate_left(5).wrapping_add(byte as u64);
        }
        
        hash
    }
    
    /// Pure scalar fallback hashing
    fn hash_scalar_fallback(&self, bytes: &[u8]) -> u64 {
        let mut hash = 0u64;
        
        for &byte in bytes {
            hash = hash.rotate_left(5).wrapping_add(byte as u64);
        }
        
        hash
    }
    
    /// Get comprehensive performance report
    pub fn performance_report(&self) -> HashPerformanceReport {
        let dispatcher = Bmi2Dispatcher::new();
        let opt_report = dispatcher.optimization_report();
        
        HashPerformanceReport {
            optimization_tier: self.optimization_tier,
            has_bmi1: self.capabilities.has_bmi1,
            has_bmi2: self.capabilities.has_bmi2,
            has_avx2: self.capabilities.simd_caps.cpu_features.has_avx2,
            estimated_speedups: HashMap::from([
                ("hash_combine".to_string(), if self.capabilities.has_bmi2 { 3.5 } else { 1.0 }),
                ("bucket_extract".to_string(), if self.capabilities.has_bmi2 { 2.5 } else { 1.0 }),
                ("string_hash".to_string(), if self.capabilities.has_bmi2 { 2.8 } else { 1.0 }),
                ("collision_resolve".to_string(), if self.capabilities.has_bmi2 { 2.2 } else { 1.0 }),
            ]),
            available_operations: opt_report.available_operations,
        }
    }
}

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

/// Hash performance report
#[derive(Debug, Clone)]
pub struct HashPerformanceReport {
    pub optimization_tier: HashOptimizationTier,
    pub has_bmi1: bool,
    pub has_bmi2: bool, 
    pub has_avx2: bool,
    pub estimated_speedups: HashMap<String, f64>,
    pub available_operations: Vec<&'static str>,
}

/// Specialized hash functions for different data types
pub mod specialized {
    use super::*;
    
    /// Optimized integer hashing with BMI2 acceleration
    pub fn hash_integer_bmi2<T>(value: T) -> u64 
    where 
        T: Into<u64> + Copy 
    {
        let val = value.into();
        bmi2_hash_combine_u64(0, val)
    }
    
    /// Optimized string hashing with SIMD integration
    pub fn hash_string_bmi2(s: &str) -> u64 {
        fast_string_hash_bmi2(s, 0)
    }
    
    /// Complex key hashing for composite types
    pub fn hash_complex_key_bmi2(components: &[u64]) -> u64 {
        advanced_hash_combine_bmi2(components)
    }
    
    /// Floating-point hashing with BMI2 bit manipulation
    pub fn hash_float_bmi2(value: f64) -> u64 {
        let bits = value.to_bits();
        bmi2_hash_combine_u64(0, bits)
    }
    
    /// Tuple hashing for pairs
    pub fn hash_tuple_bmi2<T, U>(first: T, second: U) -> u64 
    where 
        T: Into<u64> + Copy,
        U: Into<u64> + Copy,
    {
        let components = [first.into(), second.into()];
        advanced_hash_combine_bmi2(&components)
    }
}

/// Global BMI2 hash dispatcher instance
static GLOBAL_BMI2_DISPATCHER: std::sync::OnceLock<Bmi2HashDispatcher> = std::sync::OnceLock::new();

/// Get global BMI2 hash dispatcher
pub fn get_global_bmi2_dispatcher() -> &'static Bmi2HashDispatcher {
    GLOBAL_BMI2_DISPATCHER.get_or_init(|| Bmi2HashDispatcher::new())
}

/// Convenience function for hash with automatic BMI2 acceleration
pub fn hash_with_bmi2<T: AsRef<[u8]>>(data: T) -> u64 {
    get_global_bmi2_dispatcher().hash_with_acceleration(data)
}

/// Convenience function for hash combine with BMI2 acceleration
pub fn hash_combine_with_bmi2(hash: u64, value: u64) -> u64 {
    get_global_bmi2_dispatcher().hash_combine_optimal(hash, value)
}

/// Convenience function for bucket extraction with BMI2 acceleration
pub fn extract_bucket_with_bmi2(hash: u64, bucket_bits: u32) -> u32 {
    get_global_bmi2_dispatcher().extract_bucket_optimal(hash, bucket_bits)
}

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

    #[test]
    fn test_fabo_hash_combine_u32() {
        let hash = 0x12345678u32;
        let value = 0xabcdef00u32;
        
        let result = fabo_hash_combine_u32(hash, value);
        
        // Result should be different from both inputs
        assert_ne!(result, hash);
        assert_ne!(result, value);
        
        // Function should be deterministic
        assert_eq!(result, fabo_hash_combine_u32(hash, value));
    }

    #[test]
    fn test_fabo_hash_combine_u64() {
        let hash = 0x123456789abcdef0u64;
        let value = 0xfedcba9876543210u64;
        
        let result = fabo_hash_combine_u64(hash, value);
        
        // Result should be different from both inputs
        assert_ne!(result, hash);
        assert_ne!(result, value);
        
        // Function should be deterministic
        assert_eq!(result, fabo_hash_combine_u64(hash, value));
    }

    #[test]
    fn test_golden_ratio_next_size() {
        assert_eq!(golden_ratio_next_size(0), 16);
        assert_eq!(golden_ratio_next_size(1), 2); // (1 * 103) / 64 + 1 = 2
        assert_eq!(golden_ratio_next_size(64), 104); // (64 * 103) / 64 + 1 = 104
        
        // Test that growth is consistent
        let size1 = 100;
        let size2 = golden_ratio_next_size(size1);
        assert!(size2 > size1);
        
        // Should approximate golden ratio growth
        let ratio = size2 as f64 / size1 as f64;
        assert!(ratio > 1.5 && ratio < 1.7); // Should be close to 1.609
    }

    #[test]
    fn test_optimal_bucket_count() {
        assert_eq!(optimal_bucket_count(0), 16);
        
        // Test that bucket count is always a power of 2
        for capacity in [1, 10, 100, 1000] {
            let bucket_count = optimal_bucket_count(capacity);
            assert!(bucket_count.is_power_of_two());
            assert!(bucket_count >= capacity);
        }
    }

    #[test]
    fn test_advanced_hash_combine() {
        let hashes = [0x123456789abcdef0u64, 0xfedcba9876543210u64, 0x0f0f0f0f0f0f0f0fu64];
        
        let result = advanced_hash_combine(&hashes);
        
        // Result should be different from all inputs
        for &hash in &hashes {
            assert_ne!(result, hash);
        }
        
        // Should be deterministic
        assert_eq!(result, advanced_hash_combine(&hashes));
        
        // Empty slice should return 0
        assert_eq!(advanced_hash_combine(&[]), 0);
        
        // Single element should equal input (possibly modified)
        let single_result = advanced_hash_combine(&[hashes[0]]);
        // Due to avalanche step, this will be different from input
        assert_ne!(single_result, hashes[0]);
    }

    #[test]
    fn test_hash_function_builder() {
        let builder = HashFunctionBuilder::new()
            .with_rotation(7)
            .with_strategy(CombineStrategy::Fabo);
        
        let hash_fn = builder.build_u32();
        
        let result = hash_fn(0x12345678u32, 0xabcdef00u32);
        
        // Should be deterministic
        assert_eq!(result, hash_fn(0x12345678u32, 0xabcdef00u32));
    }

    #[test]
    fn test_hash_combinable_trait() {
        let hash = 0x12345678u32;
        let value = 0xabcdef00u32;
        
        let result1 = value.fabo_combine(hash);
        let result2 = fabo_hash_combine_u32(hash, value);
        
        assert_eq!(result1, result2);
    }

    #[test]
    fn test_combine_strategies() {
        let test_cases = [
            (0x12345678u32, 0xabcdef00u32),
            (0x87654321u32, 0x00fedcbau32),
            (0xfedcba98u32, 0x12345678u32),
        ];
        
        for (hash, value) in test_cases.iter() {
            // Test all strategies produce different results
            let addition = HashFunctionBuilder::new()
                .with_strategy(CombineStrategy::Addition)
                .build_u32()(*hash, *value);
            
            let xor = HashFunctionBuilder::new()
                .with_strategy(CombineStrategy::Xor)
                .build_u32()(*hash, *value);
            
            let fabo = HashFunctionBuilder::new()
                .with_strategy(CombineStrategy::Fabo)
                .build_u32()(*hash, *value);
            
            let advanced = HashFunctionBuilder::new()
                .with_strategy(CombineStrategy::Advanced)
                .build_u32()(*hash, *value);
            
            // Verify that each strategy produces a result
            // (not necessarily all different, but should be deterministic)
            assert_ne!(addition, 0);
            assert_ne!(xor, 0);
            assert_ne!(fabo, 0);
            assert_ne!(advanced, 0);
            
            // Verify strategies are deterministic
            let addition2 = HashFunctionBuilder::new()
                .with_strategy(CombineStrategy::Addition)
                .build_u32()(*hash, *value);
            assert_eq!(addition, addition2);
        }
    }

    #[test]
    fn test_bmi2_hash_combine_functions() {
        let test_cases = [
            (0x12345678u32, 0xabcdef00u32),
            (0x87654321u32, 0x00fedcbau32),
            (0xfedcba98u32, 0x12345678u32),
            (0u32, 0xffffffffu32),
            (0xffffffffu32, 0u32),
        ];
        
        for (hash, value) in test_cases.iter() {
            // Test BMI2 u32 hash combine
            let result1 = bmi2_hash_combine_u32(*hash, *value);
            let result2 = bmi2_hash_combine_u32(*hash, *value);
            
            // Should be deterministic
            assert_eq!(result1, result2);
            
            // Should produce different results for different inputs (unless both are zero)
            if *hash != *value && *hash != 0 && *value != 0 {
                assert_ne!(result1, *hash);
                assert_ne!(result1, *value);
            }
        }
        
        let test_cases_u64 = [
            (0x123456789abcdef0u64, 0xfedcba9876543210u64),
            (0x0u64, 0xffffffffffffffffu64),
            (0xffffffffffffffffu64, 0x0u64),
            (0x5555555555555555u64, 0xaaaaaaaaaaaaaaaa_u64),
        ];
        
        for (hash, value) in test_cases_u64.iter() {
            // Test BMI2 u64 hash combine
            let result1 = bmi2_hash_combine_u64(*hash, *value);
            let result2 = bmi2_hash_combine_u64(*hash, *value);
            
            // Should be deterministic
            assert_eq!(result1, result2);
            
            // Should produce different results for different inputs (unless both are zero)
            if *hash != *value && *hash != 0 && *value != 0 {
                assert_ne!(result1, *hash);
                assert_ne!(result1, *value);
            }
        }
    }
    
    #[test]
    fn test_bmi2_strategy_in_builder() {
        let test_cases = [
            (0x12345678u32, 0xabcdef00u32),
            (0x87654321u32, 0x00fedcbau32),
            (0xfedcba98u32, 0x12345678u32),
        ];
        
        for (hash, value) in test_cases.iter() {
            // Test BMI2 strategy in builder
            let bmi2_result = HashFunctionBuilder::new()
                .with_strategy(CombineStrategy::Bmi2)
                .build_u32()(*hash, *value);
            
            // Should be deterministic
            let bmi2_result2 = HashFunctionBuilder::new()
                .with_strategy(CombineStrategy::Bmi2)
                .build_u32()(*hash, *value);
            assert_eq!(bmi2_result, bmi2_result2);
            
            // Test u64 builder
            let hash64 = *hash as u64;
            let value64 = *value as u64;
            
            let bmi2_result64 = HashFunctionBuilder::new()
                .with_strategy(CombineStrategy::Bmi2)
                .build_u64()(hash64, value64);
            
            let bmi2_result64_2 = HashFunctionBuilder::new()
                .with_strategy(CombineStrategy::Bmi2)
                .build_u64()(hash64, value64);
            assert_eq!(bmi2_result64, bmi2_result64_2);
        }
    }

    #[test]
    fn test_bmi2_golden_ratio_functions() {
        // Test BMI2 golden ratio next size
        assert_eq!(bmi2_golden_ratio_next_size(0), 16);
        assert_eq!(bmi2_golden_ratio_next_size(1), 2);
        assert_eq!(bmi2_golden_ratio_next_size(64), 104);
        
        // Test growth consistency
        let size1 = 100;
        let size2 = bmi2_golden_ratio_next_size(size1);
        assert!(size2 > size1);
        
        let ratio = size2 as f64 / size1 as f64;
        assert!(ratio > 1.5 && ratio < 1.7); // Should be close to golden ratio
        
        // Test BMI2 optimal bucket count
        assert_eq!(bmi2_optimal_bucket_count(0), 16);
        
        for capacity in [1, 10, 100, 1000] {
            let bucket_count = bmi2_optimal_bucket_count(capacity);
            assert!(bucket_count.is_power_of_two());
            assert!(bucket_count >= capacity);
        }
    }

    #[test]
    fn test_hash_bucket_extraction() {
        let test_hashes = [
            0x123456789abcdef0u64,
            0xfedcba9876543210u64,
            0x0u64,
            0xffffffffffffffffu64,
            0x5555555555555555u64,
            0xaaaaaaaaaaaaaaaa_u64,
        ];
        
        for &hash in &test_hashes {
            // Test various bucket bit sizes
            for bucket_bits in [1, 4, 8, 16, 24, 32] {
                let bucket = extract_hash_bucket_bmi2(hash, bucket_bits);
                
                // Bucket should be within valid range
                if bucket_bits < 32 {
                    assert!(bucket < (1u32 << bucket_bits));
                } else {
                    // For bucket_bits >= 32, all bucket values are valid
                    assert!(bucket <= u32::MAX);
                }
                
                // Should be deterministic
                let bucket2 = extract_hash_bucket_bmi2(hash, bucket_bits);
                assert_eq!(bucket, bucket2);
            }
        }
        
        // Test bulk extraction
        let buckets = extract_hash_buckets_bulk_bmi2(&test_hashes, 8);
        assert_eq!(buckets.len(), test_hashes.len());
        
        for (i, &bucket) in buckets.iter().enumerate() {
            let individual_bucket = extract_hash_bucket_bmi2(test_hashes[i], 8);
            assert_eq!(bucket, individual_bucket);
            assert!(bucket < 256); // 8-bit bucket
        }
    }

    #[test]
    fn test_advanced_hash_combine_bmi2() {
        // Test empty slice
        assert_eq!(advanced_hash_combine_bmi2(&[]), 0);
        
        // Test single value
        let single_hash = advanced_hash_combine_bmi2(&[0x123456789abcdef0u64]);
        assert_ne!(single_hash, 0x123456789abcdef0u64); // Should be modified by avalanche
        
        // Test multiple values
        let hashes = [
            0x123456789abcdef0u64,
            0xfedcba9876543210u64,
            0x0f0f0f0f0f0f0f0fu64,
        ];
        
        let combined = advanced_hash_combine_bmi2(&hashes);
        
        // Should be deterministic
        let combined2 = advanced_hash_combine_bmi2(&hashes);
        assert_eq!(combined, combined2);
        
        // Should be different from all inputs
        for &hash in &hashes {
            assert_ne!(combined, hash);
        }
        
        // Different order should produce different results
        let hashes_reversed = [hashes[2], hashes[1], hashes[0]];
        let combined_reversed = advanced_hash_combine_bmi2(&hashes_reversed);
        assert_ne!(combined, combined_reversed);
    }

    #[test]
    fn test_fast_string_hash_bmi2() {
        let test_strings = [
            "",
            "a",
            "hello",
            "hello world",
            "The quick brown fox jumps over the lazy dog",
            &"A".repeat(100),
            "混合UTF-8字符串测试",
        ];
        
        for test_str in &test_strings {
            let hash1 = fast_string_hash_bmi2(test_str, 0);
            let hash2 = fast_string_hash_bmi2(test_str, 0);
            
            // Should be deterministic
            assert_eq!(hash1, hash2);
            
            // Different base hash should produce different results
            if !test_str.is_empty() {
                let hash_with_base = fast_string_hash_bmi2(test_str, 0x123456789abcdef0u64);
                assert_ne!(hash1, hash_with_base);
            }
        }
        
        // Different strings should produce different hashes (with high probability)
        let hash_hello = fast_string_hash_bmi2("hello", 0);
        let hash_world = fast_string_hash_bmi2("world", 0);
        assert_ne!(hash_hello, hash_world);
    }

    #[test]
    fn test_collision_resolution() {
        let test_hash = 0x123456789abcdef0u64;
        
        // Test with no occupied slots
        let empty_mask = 0u64;
        
        let linear_result = bmi2_collision_resolution(test_hash, empty_mask, ProbeType::Linear);
        assert_eq!(linear_result, Some(0)); // Should find slot 0
        
        let quadratic_result = bmi2_collision_resolution(test_hash, empty_mask, ProbeType::Quadratic);
        assert!(quadratic_result.is_some());
        
        let double_hash_result = bmi2_collision_resolution(test_hash, empty_mask, ProbeType::DoubleHash);
        assert!(double_hash_result.is_some());
        
        // Test with all slots occupied
        let full_mask = u64::MAX;
        
        let linear_full = bmi2_collision_resolution(test_hash, full_mask, ProbeType::Linear);
        assert_eq!(linear_full, None);
        
        let quadratic_full = bmi2_collision_resolution(test_hash, full_mask, ProbeType::Quadratic);
        assert_eq!(quadratic_full, None);
        
        let double_hash_full = bmi2_collision_resolution(test_hash, full_mask, ProbeType::DoubleHash);
        assert_eq!(double_hash_full, None);
        
        // Test with some slots occupied
        let partial_mask = 0x0F0F0F0F0F0F0F0Fu64; // Every other nibble
        
        let linear_partial = bmi2_collision_resolution(test_hash, partial_mask, ProbeType::Linear);
        assert!(linear_partial.is_some());
        
        if let Some(pos) = linear_partial {
            // Verify the found position is actually free
            assert_eq!((partial_mask >> pos) & 1, 0);
        }
    }

    #[test]
    fn test_load_factor_calculations() {
        // Test with empty table
        let info = bmi2_load_factor_calculations(0, 0, 0.75);
        assert_eq!(info.current_load_factor, 0.0);
        assert!(info.should_resize);
        assert_eq!(info.suggested_new_size, 16);
        
        // Test normal case
        let info = bmi2_load_factor_calculations(100, 50, 0.75);
        assert_eq!(info.current_load_factor, 0.5);
        assert!(!info.should_resize); // 0.5 < 0.75
        assert_eq!(info.suggested_new_size, 100);
        
        // Test resize trigger
        let info = bmi2_load_factor_calculations(100, 80, 0.75);
        assert!(info.current_load_factor > 0.75);
        assert!(info.should_resize);
        assert!(info.suggested_new_size > 100);
        
        // Test resize threshold calculation
        assert!(info.resize_threshold > 0);
        assert!(info.resize_threshold as f64 <= info.suggested_new_size as f64 * 0.75);
    }

    #[test]
    fn test_bmi2_hash_dispatcher() {
        let dispatcher = Bmi2HashDispatcher::new();
        
        // Test tier detection
        let tier = dispatcher.tier();
        println!("BMI2 Hash Dispatcher tier: {:?}", tier);
        
        // Test hash with acceleration
        let test_data = b"hello world test data";
        let hash1 = dispatcher.hash_with_acceleration(test_data);
        let hash2 = dispatcher.hash_with_acceleration(test_data);
        
        // Should be deterministic
        assert_eq!(hash1, hash2);
        
        // Different data should produce different hashes
        let hash3 = dispatcher.hash_with_acceleration(b"different data");
        assert_ne!(hash1, hash3);
        
        // Test hash combine
        let combined1 = dispatcher.hash_combine_optimal(0x123456789abcdef0u64, 0xfedcba9876543210u64);
        let combined2 = dispatcher.hash_combine_optimal(0x123456789abcdef0u64, 0xfedcba9876543210u64);
        assert_eq!(combined1, combined2);
        
        // Test bucket extraction
        let bucket1 = dispatcher.extract_bucket_optimal(0x123456789abcdef0u64, 8);
        let bucket2 = dispatcher.extract_bucket_optimal(0x123456789abcdef0u64, 8);
        assert_eq!(bucket1, bucket2);
        assert!(bucket1 < 256);
        
        // Test collision resolution
        let collision_result = dispatcher.resolve_collision_optimal(
            0x123456789abcdef0u64,
            0x0F0F0F0F0F0F0F0Fu64,
            ProbeType::Linear
        );
        assert!(collision_result.is_some());
        
        // Test performance report
        let report = dispatcher.performance_report();
        println!("BMI2 Hash Performance Report: {:?}", report);
        
        assert!(!report.estimated_speedups.is_empty());
        assert!(!report.available_operations.is_empty());
    }

    #[test]
    fn test_specialized_hash_functions() {
        use specialized::*;
        
        // Test integer hashing
        let int_hash_32 = hash_integer_bmi2(42u32);
        let int_hash_64 = hash_integer_bmi2(42u64);
        assert_ne!(int_hash_32, 0);
        assert_ne!(int_hash_64, 0);
        
        // Test string hashing
        let str_hash = hash_string_bmi2("test string");
        assert_ne!(str_hash, 0);
        
        // Should be same as fast_string_hash_bmi2
        let str_hash2 = fast_string_hash_bmi2("test string", 0);
        assert_eq!(str_hash, str_hash2);
        
        // Test complex key hashing
        let components = [0x123456789abcdef0u64, 0xfedcba9876543210u64, 0x0f0f0f0f0f0f0f0fu64];
        let complex_hash = hash_complex_key_bmi2(&components);
        assert_ne!(complex_hash, 0);
        
        // Test float hashing
        let float_hash = hash_float_bmi2(3.14159);
        assert_ne!(float_hash, 0);
        
        // Different floats should produce different hashes
        let float_hash2 = hash_float_bmi2(2.71828);
        assert_ne!(float_hash, float_hash2);
        
        // Test tuple hashing
        let tuple_hash = hash_tuple_bmi2(42u32, 84u64);
        assert_ne!(tuple_hash, 0);
        
        // Different tuples should produce different hashes
        let tuple_hash2 = hash_tuple_bmi2(84u32, 42u64);
        assert_ne!(tuple_hash, tuple_hash2);
    }

    #[test]
    fn test_global_bmi2_functions() {
        // Test global dispatcher
        let dispatcher1 = get_global_bmi2_dispatcher();
        let dispatcher2 = get_global_bmi2_dispatcher();
        
        // Should be same instance
        assert_eq!(dispatcher1.tier(), dispatcher2.tier());
        
        // Test convenience functions
        let test_data = b"global test data";
        
        let hash1 = hash_with_bmi2(test_data);
        let hash2 = hash_with_bmi2(test_data);
        assert_eq!(hash1, hash2);
        
        let combined = hash_combine_with_bmi2(hash1, 0xdeadbeefcafebabeu64);
        assert_ne!(combined, hash1);
        
        let bucket = extract_bucket_with_bmi2(combined, 8);
        assert!(bucket < 256);
    }

    #[test]
    fn test_bmi2_performance_characteristics() {
        // Test with various data sizes to ensure performance characteristics
        let small_data = b"small";
        let medium_data = "medium size data string with more content".repeat(10);
        let large_data = "large data content".repeat(1000);
        
        let dispatcher = Bmi2HashDispatcher::new();
        
        // All should complete without errors
        let _hash_small = dispatcher.hash_with_acceleration(small_data);
        let _hash_medium = dispatcher.hash_with_acceleration(medium_data.as_bytes());
        let _hash_large = dispatcher.hash_with_acceleration(large_data.as_bytes());
        
        // Test bulk operations
        let test_hashes: Vec<u64> = (0..1000).map(|i| (i as u64).wrapping_mul(0x123456789abcdef)).collect();
        let buckets = extract_hash_buckets_bulk_bmi2(&test_hashes, 8);
        assert_eq!(buckets.len(), 1000);
        
        for bucket in buckets {
            assert!(bucket < 256);
        }
        
        // Test complex combining
        let combined = advanced_hash_combine_bmi2(&test_hashes[0..10]);
        assert_ne!(combined, 0);
    }

    #[test]
    fn test_edge_cases() {
        // Test edge cases for BMI2 functions
        
        // Zero values
        assert_eq!(bmi2_hash_combine_u32(0, 0), bmi2_hash_combine_u32(0, 0));
        assert_eq!(bmi2_hash_combine_u64(0, 0), bmi2_hash_combine_u64(0, 0));
        
        // Maximum values
        let max_u32 = u32::MAX;
        let max_u64 = u64::MAX;
        
        let max_result_32 = bmi2_hash_combine_u32(max_u32, max_u32);
        assert_ne!(max_result_32, max_u32); // Should be different due to rotation/mixing
        
        let max_result_64 = bmi2_hash_combine_u64(max_u64, max_u64);
        assert_ne!(max_result_64, max_u64);
        
        // Bucket extraction edge cases
        assert_eq!(extract_hash_bucket_bmi2(0, 1), 0);
        assert_eq!(extract_hash_bucket_bmi2(1, 1), 1);
        assert_eq!(extract_hash_bucket_bmi2(max_u64, 64), max_u64 as u32);
        
        // String hashing edge cases
        assert_eq!(fast_string_hash_bmi2("", 0), 0);
        let single_char = fast_string_hash_bmi2("a", 0);
        assert_ne!(single_char, 0);
        
        // Load factor edge cases
        let info = bmi2_load_factor_calculations(1, 0, 0.75);
        assert_eq!(info.current_load_factor, 0.0);
        assert!(!info.should_resize);
    }
}