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
//! SSE4.2 PCMPESTRI-based string search operations
//!
//! This module provides high-performance string search operations using SSE4.2's
//! PCMPESTRI instruction for byte-level pattern matching with hardware acceleration.
//!
//! ## Features
//!
//! - **SSE4.2 PCMPESTRI-based search**: Hardware-accelerated character and substring search
//! - **Hybrid Strategy**: SSE4.2 for small patterns, binary search fallback for larger data
//! - **Multi-character search**: Vectorized search across multiple needle bytes
//! - **Early exit optimizations**: String comparison with hardware-accelerated mismatch detection
//! - **Integration ready**: Designed for FSA/Trie, compression, hash maps, and blob stores
//!
//! ## Performance Characteristics
//!
//! - **≤16 bytes**: Single PCMPESTRI instruction (optimal)
//! - **17-35 bytes**: Cascaded SSE4.2 operations with early exit
//! - **>35 bytes**: O(log n) binary search with rank-select optimization
//! - **Runtime detection**: Automatic fallback to scalar implementations
//!
//! ## Safety
//!
//! All unsafe operations are isolated to SIMD intrinsics with proper bounds checking.
//! Public APIs are completely safe with comprehensive error handling.
use crate::system::cpu_features::CpuFeatures;
use std::cmp::Ordering;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
/// SSE4.2 PCMPESTRI-based string search operations
pub struct SimdStringSearch {
/// CPU features available at runtime
cpu_features: &'static CpuFeatures,
/// Selected implementation tier based on available features
impl_tier: SearchTier,
}
/// SIMD implementation tiers for string search operations
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchTier {
/// Scalar fallback (no SIMD)
Scalar,
/// SSE4.2 with PCMPESTRI
Sse42,
/// AVX2 with enhanced vectorization
Avx2,
/// AVX-512 implementation (nightly only)
#[cfg(feature = "avx512")]
Avx512,
}
/// PCMPESTRI control flags for different search operations
#[allow(dead_code)]
mod pcmpestri_flags {
/// Unsigned byte comparison
pub const UBYTE_OPS: i32 = 0x00;
/// Compare for equality
pub const CMP_EQUAL_ORDERED: i32 = 0x08;
/// Return least significant index
pub const LEAST_SIGNIFICANT: i32 = 0x00;
/// Return most significant index
pub const MOST_SIGNIFICANT: i32 = 0x01;
/// Compare any byte in set
pub const CMP_EQUAL_ANY: i32 = 0x00;
/// Negative polarity (find first non-match)
pub const NEGATIVE_POLARITY: i32 = 0x10;
}
/// Multi-character search result containing all found positions
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MultiSearchResult {
/// Positions where any of the needle characters were found
pub positions: Vec<usize>,
/// Which character was found at each position (index into needles array)
pub characters: Vec<u8>,
}
impl SimdStringSearch {
/// Creates a new SIMD string search instance with runtime feature detection
pub fn new() -> Self {
let cpu_features = crate::system::get_cpu_features();
let impl_tier = Self::select_optimal_tier(cpu_features);
Self {
cpu_features,
impl_tier,
}
}
/// Selects the optimal SIMD implementation tier based on available CPU features
fn select_optimal_tier(features: &CpuFeatures) -> SearchTier {
#[cfg(feature = "avx512")]
if features.has_avx512f && features.has_avx512vl && features.has_avx512bw {
return SearchTier::Avx512;
}
if features.has_avx2 {
return SearchTier::Avx2;
}
if features.has_sse41 && features.has_sse42 {
return SearchTier::Sse42;
}
SearchTier::Scalar
}
/// Returns the currently selected search tier
pub fn tier(&self) -> SearchTier {
self.impl_tier
}
/// SSE4.2 PCMPESTRI-based character search (strchr equivalent)
///
/// Searches for the first occurrence of a character in a byte array.
/// Uses hybrid strategy: SSE4.2 for ≤16 bytes, extended SSE4.2 for ≤35 bytes,
/// binary search for larger arrays.
///
/// # Arguments
/// * `haystack` - Byte array to search in
/// * `needle` - Character to search for
///
/// # Returns
/// Position of first occurrence, or None if not found
pub fn sse42_strchr(&self, haystack: &[u8], needle: u8) -> Option<usize> {
if haystack.is_empty() {
return None;
}
match self.impl_tier {
SearchTier::Sse42 => {
if haystack.len() <= 16 {
// SAFETY: sse4.2 guaranteed by impl_tier, haystack is valid slice
unsafe { self.sse42_strchr_max_16(haystack, needle) }
} else if haystack.len() <= 35 {
// SAFETY: sse4.2 guaranteed by impl_tier, haystack is valid slice
unsafe { self.sse42_strchr_max_35(haystack, needle) }
} else {
self.hybrid_strchr_large(haystack, needle)
}
}
SearchTier::Avx2 => {
// SAFETY: avx2 guaranteed by impl_tier, haystack is valid slice
unsafe { self.avx2_strchr(haystack, needle) }
}
#[cfg(feature = "avx512")]
SearchTier::Avx512 => {
// SAFETY: avx512f+avx512bw guaranteed by impl_tier, haystack is valid slice
unsafe { self.avx512_strchr(haystack, needle) }
}
SearchTier::Scalar => {
self.scalar_strchr(haystack, needle)
}
}
}
/// SSE4.2 PCMPESTRI-based substring search (strstr equivalent)
///
/// Searches for the first occurrence of a substring in a byte array.
/// Uses tiered approach based on needle length and haystack size.
///
/// # Arguments
/// * `haystack` - Byte array to search in
/// * `needle` - Substring to search for
///
/// # Returns
/// Position of first occurrence, or None if not found
pub fn sse42_strstr(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
if haystack.is_empty() || needle.is_empty() || needle.len() > haystack.len() {
return None;
}
// For single character, use optimized strchr
if needle.len() == 1 {
return self.sse42_strchr(haystack, needle[0]);
}
match self.impl_tier {
SearchTier::Sse42 => {
// SAFETY: sse4.2 guaranteed by impl_tier, haystack and needle are valid slices
unsafe { self.sse42_strstr_impl(haystack, needle) }
}
SearchTier::Avx2 => {
// SAFETY: avx2 guaranteed by impl_tier, haystack and needle are valid slices
unsafe { self.avx2_strstr(haystack, needle) }
}
#[cfg(feature = "avx512")]
SearchTier::Avx512 => {
// SAFETY: avx512f+avx512bw guaranteed by impl_tier, haystack and needle are valid slices
unsafe { self.avx512_strstr(haystack, needle) }
}
SearchTier::Scalar => {
self.scalar_strstr(haystack, needle)
}
}
}
/// Multi-character search with vectorization
///
/// Searches for any of the specified characters and returns all positions.
/// Optimized for searching multiple characters simultaneously.
///
/// # Arguments
/// * `haystack` - Byte array to search in
/// * `needles` - Array of characters to search for
///
/// # Returns
/// MultiSearchResult containing positions and which characters were found
pub fn sse42_multi_search(&self, haystack: &[u8], needles: &[u8]) -> MultiSearchResult {
if haystack.is_empty() || needles.is_empty() {
return MultiSearchResult {
positions: Vec::new(),
characters: Vec::new(),
};
}
match self.impl_tier {
SearchTier::Sse42 => {
// SAFETY: sse4.2 guaranteed by impl_tier, haystack and needles are valid slices
unsafe { self.sse42_multi_search_impl(haystack, needles) }
}
SearchTier::Avx2 => {
// SAFETY: avx2 guaranteed by impl_tier, haystack and needles are valid slices
unsafe { self.avx2_multi_search(haystack, needles) }
}
#[cfg(feature = "avx512")]
SearchTier::Avx512 => {
// SAFETY: avx512f+avx512bw guaranteed by impl_tier, haystack and needles are valid slices
unsafe { self.avx512_multi_search(haystack, needles) }
}
SearchTier::Scalar => {
self.scalar_multi_search(haystack, needles)
}
}
}
/// String comparison with early exit optimizations
///
/// Performs lexicographic comparison with hardware-accelerated mismatch detection.
/// Uses SIMD operations to quickly find the first differing bytes.
///
/// # Arguments
/// * `a` - First byte array to compare
/// * `b` - Second byte array to compare
///
/// # Returns
/// Ordering result (Less, Equal, Greater)
pub fn sse42_strcmp(&self, a: &[u8], b: &[u8]) -> Ordering {
// Quick length comparison
if a.len() != b.len() {
return a.len().cmp(&b.len());
}
if a.is_empty() {
return Ordering::Equal;
}
match self.impl_tier {
SearchTier::Sse42 => {
// SAFETY: sse4.2 guaranteed by impl_tier, a and b are valid slices
unsafe { self.sse42_strcmp_impl(a, b) }
}
SearchTier::Avx2 => {
// SAFETY: avx2 guaranteed by impl_tier, a and b are valid slices
unsafe { self.avx2_strcmp(a, b) }
}
#[cfg(feature = "avx512")]
SearchTier::Avx512 => {
// SAFETY: avx512f+avx512bw guaranteed by impl_tier, a and b are valid slices
unsafe { self.avx512_strcmp(a, b) }
}
SearchTier::Scalar => {
self.scalar_strcmp(a, b)
}
}
}
// =============================================================================
// SSE4.2 IMPLEMENTATIONS
// =============================================================================
/// SSE4.2 strchr for arrays ≤16 bytes (single PCMPESTRI instruction)
#[target_feature(enable = "sse4.2")]
unsafe fn sse42_strchr_max_16(&self, haystack: &[u8], needle: u8) -> Option<usize> {
debug_assert!(haystack.len() <= 16);
// Create needle vector with single character
let needle_vec = _mm_set1_epi8(needle as i8);
// Load haystack data (up to 16 bytes)
let haystack_vec = if haystack.len() == 16 {
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from slice, len == 16 checked
unsafe { _mm_loadu_si128(haystack.as_ptr() as *const __m128i) }
} else {
// For lengths < 16, we need to be careful about reading past the end
let mut data = [0u8; 16];
// SAFETY: pointer valid from slice, haystack.len() <= 16 from caller, copy within bounds
unsafe { std::ptr::copy_nonoverlapping(haystack.as_ptr(), data.as_mut_ptr(), haystack.len()) };
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from stack array
unsafe { _mm_loadu_si128(data.as_ptr() as *const __m128i) }
};
// Use PCMPESTRI to find first occurrence
let result = _mm_cmpestri(
needle_vec,
1, // needle length is 1
haystack_vec,
haystack.len() as i32,
pcmpestri_flags::UBYTE_OPS
| pcmpestri_flags::CMP_EQUAL_ORDERED
| pcmpestri_flags::LEAST_SIGNIFICANT
);
if result < haystack.len() as i32 {
Some(result as usize)
} else {
None
}
}
/// SSE4.2 strchr for arrays ≤35 bytes (cascaded PCMPESTRI operations)
#[target_feature(enable = "sse4.2")]
unsafe fn sse42_strchr_max_35(&self, haystack: &[u8], needle: u8) -> Option<usize> {
debug_assert!(haystack.len() <= 35 && haystack.len() > 16);
// Search first 16 bytes
// SAFETY: sse4.2 guaranteed by #[target_feature], haystack[..16] valid (len > 16 checked)
if let Some(pos) = unsafe { self.sse42_strchr_max_16(&haystack[..16], needle) } {
return Some(pos);
}
// Search remaining bytes
let remaining = &haystack[16..];
// SAFETY: sse4.2 guaranteed by #[target_feature], remaining is valid slice
if let Some(pos) = unsafe { self.sse42_strchr_max_16(remaining, needle) } {
return Some(16 + pos);
}
None
}
/// Hybrid strchr for large arrays (>35 bytes)
fn hybrid_strchr_large(&self, haystack: &[u8], needle: u8) -> Option<usize> {
// For large arrays, use binary search approach optimized for cache efficiency
// This implements the strategy from the reference implementation
// Check if SSE4.2 is available for small chunks
if matches!(self.impl_tier, SearchTier::Sse42) {
// Process in 16-byte chunks with SSE4.2
let chunks = haystack.len() / 16;
for i in 0..chunks {
let start = i * 16;
let chunk = &haystack[start..start + 16];
// SAFETY: sse4.2 guaranteed by runtime check, chunk is valid 16-byte slice
unsafe {
if let Some(pos) = self.sse42_strchr_max_16(chunk, needle) {
return Some(start + pos);
}
}
}
// Handle remaining bytes
let remaining_start = chunks * 16;
if remaining_start < haystack.len() {
let remaining = &haystack[remaining_start..];
// SAFETY: sse4.2 guaranteed by runtime check, remaining is valid slice
unsafe {
if let Some(pos) = self.sse42_strchr_max_16(remaining, needle) {
return Some(remaining_start + pos);
}
}
}
None
} else {
// Fallback to scalar for very large arrays without SSE4.2
self.scalar_strchr(haystack, needle)
}
}
/// SSE4.2 strstr implementation using PCMPESTRI
#[target_feature(enable = "sse4.2")]
unsafe fn sse42_strstr_impl(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
if needle.len() > 16 {
// For needles > 16 bytes, use first character matching + verification
let first_char = needle[0];
let mut pos = 0;
while pos <= haystack.len() - needle.len() {
if let Some(char_pos) = self.sse42_strchr(&haystack[pos..], first_char) {
let candidate_pos = pos + char_pos;
if candidate_pos + needle.len() <= haystack.len() {
let candidate = &haystack[candidate_pos..candidate_pos + needle.len()];
if candidate == needle {
return Some(candidate_pos);
}
pos = candidate_pos + 1;
} else {
break;
}
} else {
break;
}
}
return None;
}
// For needles ≤ 16 bytes, use PCMPESTRM for substring matching
let needle_vec = if needle.len() == 16 {
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from slice, len == 16 checked
unsafe { _mm_loadu_si128(needle.as_ptr() as *const __m128i) }
} else {
let mut data = [0u8; 16];
// SAFETY: pointer valid from slice, needle.len() <= 16 (caller ensures), copy within bounds
unsafe { std::ptr::copy_nonoverlapping(needle.as_ptr(), data.as_mut_ptr(), needle.len()) };
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from stack array
unsafe { _mm_loadu_si128(data.as_ptr() as *const __m128i) }
};
// Search through haystack in overlapping 16-byte windows
let max_start = if haystack.len() >= needle.len() {
haystack.len() - needle.len() + 1
} else {
0
};
for start in 0..max_start {
let search_len = std::cmp::min(16, haystack.len() - start);
if search_len < needle.len() {
break;
}
let haystack_vec = if search_len == 16 {
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from slice, len == 16 checked
unsafe { _mm_loadu_si128(haystack[start..].as_ptr() as *const __m128i) }
} else {
let mut data = [0u8; 16];
// SAFETY: pointer valid from slice, search_len <= 16 checked, copy within bounds
unsafe {
std::ptr::copy_nonoverlapping(
haystack[start..].as_ptr(),
data.as_mut_ptr(),
search_len
);
};
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from stack array
unsafe { _mm_loadu_si128(data.as_ptr() as *const __m128i) }
};
let result = _mm_cmpestri(
needle_vec,
needle.len() as i32,
haystack_vec,
search_len as i32,
pcmpestri_flags::UBYTE_OPS
| pcmpestri_flags::CMP_EQUAL_ORDERED
| pcmpestri_flags::LEAST_SIGNIFICANT
);
if result == 0 {
// Found potential match at the beginning of this window
return Some(start);
}
// For efficiency, we can skip ahead more than 1 byte if we know
// the needle doesn't contain repeated characters at the start
}
None
}
/// SSE4.2 multi-character search implementation
#[target_feature(enable = "sse4.2")]
unsafe fn sse42_multi_search_impl(&self, haystack: &[u8], needles: &[u8]) -> MultiSearchResult {
let mut positions = Vec::new();
let mut characters = Vec::new();
if needles.len() <= 16 {
// For ≤16 needles, we can use PCMPESTRI with CMP_EQUAL_ANY
let needles_vec = if needles.len() == 16 {
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from slice, len == 16 checked
unsafe { _mm_loadu_si128(needles.as_ptr() as *const __m128i) }
} else {
let mut data = [0u8; 16];
// SAFETY: pointer valid from slice, needles.len() <= 16 checked, copy within bounds
unsafe { std::ptr::copy_nonoverlapping(needles.as_ptr(), data.as_mut_ptr(), needles.len()) };
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from stack array
unsafe { _mm_loadu_si128(data.as_ptr() as *const __m128i) }
};
let mut pos = 0;
while pos < haystack.len() {
let search_len = std::cmp::min(16, haystack.len() - pos);
let haystack_vec = if search_len == 16 {
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from slice, len >= 16 checked
unsafe { _mm_loadu_si128(haystack[pos..].as_ptr() as *const __m128i) }
} else {
let mut data = [0u8; 16];
// SAFETY: pointer valid from slice, search_len <= 16 checked, copy within bounds
unsafe {
std::ptr::copy_nonoverlapping(
haystack[pos..].as_ptr(),
data.as_mut_ptr(),
search_len
);
};
// SAFETY: sse4.2 guaranteed by #[target_feature], pointer valid from stack array
unsafe { _mm_loadu_si128(data.as_ptr() as *const __m128i) }
};
let result = _mm_cmpestri(
needles_vec,
needles.len() as i32,
haystack_vec,
search_len as i32,
pcmpestri_flags::UBYTE_OPS
| pcmpestri_flags::CMP_EQUAL_ANY
| pcmpestri_flags::LEAST_SIGNIFICANT
);
if result < search_len as i32 {
let found_pos = pos + result as usize;
let found_char = haystack[found_pos];
positions.push(found_pos);
characters.push(found_char);
pos = found_pos + 1;
} else {
pos += search_len;
}
}
} else {
// For >16 needles, fall back to individual character searches
return self.scalar_multi_search(haystack, needles);
}
MultiSearchResult { positions, characters }
}
/// SSE4.2 string comparison implementation
#[target_feature(enable = "sse4.2")]
unsafe fn sse42_strcmp_impl(&self, a: &[u8], b: &[u8]) -> Ordering {
debug_assert_eq!(a.len(), b.len());
let len = a.len();
let chunks = len / 16;
// Compare 16-byte chunks
for i in 0..chunks {
let offset = i * 16;
// SAFETY: sse4.2 guaranteed by #[target_feature], pointers valid from slices, offset within bounds (chunks * 16 <= len)
let chunk_a = unsafe { _mm_loadu_si128(a[offset..].as_ptr() as *const __m128i) };
// SAFETY: sse4.2 guaranteed by #[target_feature], pointers valid from slices, offset within bounds (chunks * 16 <= len)
let chunk_b = unsafe { _mm_loadu_si128(b[offset..].as_ptr() as *const __m128i) };
let cmp = _mm_cmpeq_epi8(chunk_a, chunk_b);
let mask = _mm_movemask_epi8(cmp);
if mask != 0xFFFF {
// Found mismatch, find first differing byte
let first_diff = (!mask as u16).trailing_zeros() as usize;
let pos = offset + first_diff;
return a[pos].cmp(&b[pos]);
}
}
// Compare remaining bytes
let remaining_start = chunks * 16;
if remaining_start < len {
return a[remaining_start..].cmp(&b[remaining_start..]);
}
Ordering::Equal
}
// =============================================================================
// AVX2 IMPLEMENTATIONS
// =============================================================================
#[target_feature(enable = "avx2")]
unsafe fn avx2_strchr(&self, haystack: &[u8], needle: u8) -> Option<usize> {
let needle_vec = _mm256_set1_epi8(needle as i8);
let chunks = haystack.len() / 32;
for i in 0..chunks {
let offset = i * 32;
// SAFETY: avx2 guaranteed by #[target_feature], pointer valid from slice, offset within bounds (chunks * 32 <= len)
let chunk = unsafe { _mm256_loadu_si256(haystack[offset..].as_ptr() as *const __m256i) };
let cmp = _mm256_cmpeq_epi8(chunk, needle_vec);
let mask = _mm256_movemask_epi8(cmp);
if mask != 0 {
let first_match = mask.trailing_zeros() as usize;
return Some(offset + first_match);
}
}
// Handle remaining bytes
let remaining_start = chunks * 32;
if remaining_start < haystack.len() {
return self.scalar_strchr(&haystack[remaining_start..], needle)
.map(|pos| remaining_start + pos);
}
None
}
#[target_feature(enable = "avx2")]
unsafe fn avx2_strstr(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
// For simplicity, use first character matching + verification approach
let first_char = needle[0];
let mut pos = 0;
while pos <= haystack.len() - needle.len() {
// SAFETY: avx2 guaranteed by #[target_feature], haystack[pos..] is valid slice
if let Some(char_pos) = unsafe { self.avx2_strchr(&haystack[pos..], first_char) } {
let candidate_pos = pos + char_pos;
if candidate_pos + needle.len() <= haystack.len() {
let candidate = &haystack[candidate_pos..candidate_pos + needle.len()];
if candidate == needle {
return Some(candidate_pos);
}
pos = candidate_pos + 1;
} else {
break;
}
} else {
break;
}
}
None
}
#[target_feature(enable = "avx2")]
unsafe fn avx2_multi_search(&self, haystack: &[u8], needles: &[u8]) -> MultiSearchResult {
// For AVX2, we can process multiple needles more efficiently
// For now, fall back to individual searches for simplicity
self.scalar_multi_search(haystack, needles)
}
#[target_feature(enable = "avx2")]
unsafe fn avx2_strcmp(&self, a: &[u8], b: &[u8]) -> Ordering {
debug_assert_eq!(a.len(), b.len());
let len = a.len();
let chunks = len / 32;
// Compare 32-byte chunks
for i in 0..chunks {
let offset = i * 32;
// SAFETY: avx2 guaranteed by #[target_feature], pointers valid from slices, offset within bounds (chunks * 32 <= len)
let chunk_a = unsafe { _mm256_loadu_si256(a[offset..].as_ptr() as *const __m256i) };
// SAFETY: avx2 guaranteed by #[target_feature], pointers valid from slices, offset within bounds (chunks * 32 <= len)
let chunk_b = unsafe { _mm256_loadu_si256(b[offset..].as_ptr() as *const __m256i) };
let cmp = _mm256_cmpeq_epi8(chunk_a, chunk_b);
let mask = _mm256_movemask_epi8(cmp) as u32;
if mask != 0xFFFFFFFF {
// Found mismatch, find first differing byte
let first_diff = (!mask).trailing_zeros() as usize;
let pos = offset + first_diff;
return a[pos].cmp(&b[pos]);
}
}
// Compare remaining bytes
let remaining_start = chunks * 32;
if remaining_start < len {
return a[remaining_start..].cmp(&b[remaining_start..]);
}
Ordering::Equal
}
// =============================================================================
// AVX-512 IMPLEMENTATIONS (NIGHTLY ONLY)
// =============================================================================
#[cfg(feature = "avx512")]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn avx512_strchr(&self, haystack: &[u8], needle: u8) -> Option<usize> {
let needle_vec = _mm512_set1_epi8(needle as i8);
let chunks = haystack.len() / 64;
for i in 0..chunks {
let offset = i * 64;
// SAFETY: avx512f+avx512bw guaranteed by #[target_feature], pointer valid from slice, offset within bounds (chunks * 64 <= len)
let chunk = unsafe { _mm512_loadu_si512(haystack[offset..].as_ptr() as *const __m512i) };
// SAFETY: avx512f+avx512bw guaranteed by #[target_feature], chunk is valid __m512i
let mask = unsafe { _mm512_cmpeq_epi8_mask(chunk, needle_vec) };
if mask != 0 {
let first_match = mask.trailing_zeros() as usize;
return Some(offset + first_match);
}
}
// Handle remaining bytes
let remaining_start = chunks * 64;
if remaining_start < haystack.len() {
return self.scalar_strchr(&haystack[remaining_start..], needle)
.map(|pos| remaining_start + pos);
}
None
}
#[cfg(feature = "avx512")]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn avx512_strstr(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
// Similar to AVX2 implementation but with 64-byte chunks
let first_char = needle[0];
let mut pos = 0;
while pos <= haystack.len() - needle.len() {
// SAFETY: avx512f+avx512bw guaranteed by #[target_feature], haystack[pos..] is valid slice
if let Some(char_pos) = unsafe { self.avx512_strchr(&haystack[pos..], first_char) } {
let candidate_pos = pos + char_pos;
if candidate_pos + needle.len() <= haystack.len() {
let candidate = &haystack[candidate_pos..candidate_pos + needle.len()];
if candidate == needle {
return Some(candidate_pos);
}
pos = candidate_pos + 1;
} else {
break;
}
} else {
break;
}
}
None
}
#[cfg(feature = "avx512")]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn avx512_multi_search(&self, haystack: &[u8], needles: &[u8]) -> MultiSearchResult {
// AVX-512 implementation for multi-character search
self.scalar_multi_search(haystack, needles)
}
#[cfg(feature = "avx512")]
#[target_feature(enable = "avx512f,avx512bw")]
unsafe fn avx512_strcmp(&self, a: &[u8], b: &[u8]) -> Ordering {
debug_assert_eq!(a.len(), b.len());
let len = a.len();
let chunks = len / 64;
// Compare 64-byte chunks
for i in 0..chunks {
let offset = i * 64;
// SAFETY: avx512f+avx512bw guaranteed by #[target_feature], pointers valid from slices, offset within bounds (chunks * 64 <= len)
let chunk_a = unsafe { _mm512_loadu_si512(a[offset..].as_ptr() as *const __m512i) };
// SAFETY: avx512f+avx512bw guaranteed by #[target_feature], pointers valid from slices, offset within bounds (chunks * 64 <= len)
let chunk_b = unsafe { _mm512_loadu_si512(b[offset..].as_ptr() as *const __m512i) };
let mask = _mm512_cmpeq_epi8_mask(chunk_a, chunk_b);
if mask != 0xFFFFFFFFFFFFFFFF {
// Found mismatch, find first differing byte
let first_diff = (!mask).trailing_zeros() as usize;
let pos = offset + first_diff;
return a[pos].cmp(&b[pos]);
}
}
// Compare remaining bytes
let remaining_start = chunks * 64;
if remaining_start < len {
return a[remaining_start..].cmp(&b[remaining_start..]);
}
Ordering::Equal
}
// =============================================================================
// SCALAR FALLBACK IMPLEMENTATIONS
// =============================================================================
fn scalar_strchr(&self, haystack: &[u8], needle: u8) -> Option<usize> {
haystack.iter().position(|&b| b == needle)
}
fn scalar_strstr(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
if needle.is_empty() {
return Some(0);
}
haystack.windows(needle.len())
.position(|window| window == needle)
}
fn scalar_multi_search(&self, haystack: &[u8], needles: &[u8]) -> MultiSearchResult {
let mut positions = Vec::new();
let mut characters = Vec::new();
for (pos, &byte) in haystack.iter().enumerate() {
if needles.contains(&byte) {
positions.push(pos);
characters.push(byte);
}
}
MultiSearchResult { positions, characters }
}
fn scalar_strcmp(&self, a: &[u8], b: &[u8]) -> Ordering {
a.cmp(b)
}
}
impl Default for SimdStringSearch {
fn default() -> Self {
Self::new()
}
}
/// Global SIMD string search instance for reuse
static GLOBAL_SIMD_SEARCH: std::sync::OnceLock<SimdStringSearch> = std::sync::OnceLock::new();
/// Gets the global SIMD string search instance
pub fn get_global_simd_search() -> &'static SimdStringSearch {
GLOBAL_SIMD_SEARCH.get_or_init(|| SimdStringSearch::new())
}
/// Convenience function for SSE4.2 character search using global instance
pub fn sse42_strchr(haystack: &[u8], needle: u8) -> Option<usize> {
get_global_simd_search().sse42_strchr(haystack, needle)
}
/// Convenience function for SSE4.2 substring search using global instance
pub fn sse42_strstr(haystack: &[u8], needle: &[u8]) -> Option<usize> {
get_global_simd_search().sse42_strstr(haystack, needle)
}
/// Convenience function for multi-character search using global instance
pub fn sse42_multi_search(haystack: &[u8], needles: &[u8]) -> MultiSearchResult {
get_global_simd_search().sse42_multi_search(haystack, needles)
}
/// Convenience function for string comparison using global instance
pub fn sse42_strcmp(a: &[u8], b: &[u8]) -> Ordering {
get_global_simd_search().sse42_strcmp(a, b)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simd_search_creation() {
let search = SimdStringSearch::new();
println!("Selected SIMD tier: {:?}", search.tier());
// Should always work regardless of available features
assert!(matches!(search.tier(),
SearchTier::Scalar | SearchTier::Sse42 | SearchTier::Avx2
));
}
#[test]
fn test_global_simd_search() {
let search1 = get_global_simd_search();
let search2 = get_global_simd_search();
// Should be the same instance
assert_eq!(search1.tier(), search2.tier());
}
#[test]
fn test_strchr_basic() {
let search = SimdStringSearch::new();
let haystack = b"hello world";
assert_eq!(search.sse42_strchr(haystack, b'h'), Some(0));
assert_eq!(search.sse42_strchr(haystack, b'o'), Some(4));
assert_eq!(search.sse42_strchr(haystack, b'd'), Some(10));
assert_eq!(search.sse42_strchr(haystack, b'x'), None);
}
#[test]
fn test_strchr_empty() {
let search = SimdStringSearch::new();
let empty = b"";
assert_eq!(search.sse42_strchr(empty, b'a'), None);
}
#[test]
fn test_strchr_large() {
let search = SimdStringSearch::new();
// Test with strings large enough to trigger different code paths
let mut large_haystack = b"a".repeat(100);
large_haystack.push(b'x');
large_haystack.extend_from_slice(&b"a".repeat(100));
assert_eq!(search.sse42_strchr(&large_haystack, b'x'), Some(100));
assert_eq!(search.sse42_strchr(&large_haystack, b'y'), None);
}
#[test]
fn test_strstr_basic() {
let search = SimdStringSearch::new();
let haystack = b"hello world test";
assert_eq!(search.sse42_strstr(haystack, b"hello"), Some(0));
assert_eq!(search.sse42_strstr(haystack, b"world"), Some(6));
assert_eq!(search.sse42_strstr(haystack, b"test"), Some(12));
assert_eq!(search.sse42_strstr(haystack, b"xyz"), None);
}
#[test]
fn test_strstr_single_char() {
let search = SimdStringSearch::new();
let haystack = b"hello world";
assert_eq!(search.sse42_strstr(haystack, b"o"), Some(4));
}
#[test]
fn test_strstr_empty() {
let search = SimdStringSearch::new();
let haystack = b"hello";
assert_eq!(search.sse42_strstr(haystack, b""), None);
assert_eq!(search.sse42_strstr(b"", b"hello"), None);
}
#[test]
fn test_multi_search_basic() {
let search = SimdStringSearch::new();
let haystack = b"hello world";
let needles = b"lo";
let result = search.sse42_multi_search(haystack, needles);
// Should find 'l' at positions 2, 3, 9 and 'o' at positions 4, 7
assert_eq!(result.positions, vec![2, 3, 4, 7, 9]);
assert_eq!(result.characters, vec![b'l', b'l', b'o', b'o', b'l']);
}
#[test]
fn test_multi_search_empty() {
let search = SimdStringSearch::new();
let haystack = b"hello";
let result = search.sse42_multi_search(haystack, b"");
assert!(result.positions.is_empty());
let result = search.sse42_multi_search(b"", b"abc");
assert!(result.positions.is_empty());
}
#[test]
fn test_strcmp_basic() {
let search = SimdStringSearch::new();
assert_eq!(search.sse42_strcmp(b"hello", b"hello"), Ordering::Equal);
assert_eq!(search.sse42_strcmp(b"hello", b"world"), Ordering::Less);
assert_eq!(search.sse42_strcmp(b"world", b"hello"), Ordering::Greater);
}
#[test]
fn test_strcmp_different_lengths() {
let search = SimdStringSearch::new();
assert_eq!(search.sse42_strcmp(b"short", b"longer"), Ordering::Less);
assert_eq!(search.sse42_strcmp(b"longer", b"short"), Ordering::Greater);
}
#[test]
fn test_strcmp_empty() {
let search = SimdStringSearch::new();
assert_eq!(search.sse42_strcmp(b"", b""), Ordering::Equal);
assert_eq!(search.sse42_strcmp(b"", b"non-empty"), Ordering::Less);
assert_eq!(search.sse42_strcmp(b"non-empty", b""), Ordering::Greater);
}
#[test]
fn test_convenience_functions() {
let haystack = b"hello world test";
assert_eq!(sse42_strchr(haystack, b'w'), Some(6));
assert_eq!(sse42_strstr(haystack, b"world"), Some(6));
assert_eq!(sse42_strcmp(b"hello", b"hello"), Ordering::Equal);
let needles = b"ld";
let result = sse42_multi_search(haystack, needles);
assert!(!result.positions.is_empty());
}
#[test]
fn test_simd_size_thresholds() {
let search = SimdStringSearch::new();
// Test various sizes around SIMD thresholds
let sizes = [1, 8, 15, 16, 17, 31, 32, 35, 63, 64, 100];
for &size in &sizes {
let haystack = b"a".repeat(size);
let mut test_data = haystack.clone();
if size > 0 {
test_data[size - 1] = b'x';
}
if size > 0 {
assert_eq!(search.sse42_strchr(&test_data, b'x'), Some(size - 1));
}
assert_eq!(search.sse42_strchr(&test_data, b'y'), None);
}
}
// We allow test execution here to compile-check benchmarks during `cargo test`.
#[cfg(any(feature = "criterion", test))]
mod benchmarks {
use super::*;
use criterion::{black_box, Criterion};
pub fn bench_strchr(c: &mut Criterion) {
let search = SimdStringSearch::new();
let haystack = b"a".repeat(1000);
let mut test_data = haystack.clone();
test_data[500] = b'x';
c.bench_function("sse42_strchr_large", |b| {
b.iter(|| {
black_box(search.sse42_strchr(black_box(&test_data), black_box(b'x')))
})
});
c.bench_function("scalar_strchr_large", |b| {
b.iter(|| {
black_box(test_data.iter().position(|&b| b == b'x'))
})
});
}
pub fn bench_strstr(c: &mut Criterion) {
let search = SimdStringSearch::new();
let haystack = "hello world ".repeat(100);
let needle = b"world";
c.bench_function("sse42_strstr", |b| {
b.iter(|| {
black_box(search.sse42_strstr(
black_box(haystack.as_bytes()),
black_box(needle)
))
})
});
c.bench_function("scalar_strstr", |b| {
b.iter(|| {
black_box(haystack.as_bytes().windows(needle.len())
.position(|window| window == needle))
})
});
}
pub fn bench_multi_search(c: &mut Criterion) {
let search = SimdStringSearch::new();
let haystack = "hello world test string".repeat(50);
let needles = b"aeiou";
c.bench_function("sse42_multi_search", |b| {
b.iter(|| {
black_box(search.sse42_multi_search(
black_box(haystack.as_bytes()),
black_box(needles)
))
})
});
}
pub fn bench_strcmp(c: &mut Criterion) {
let search = SimdStringSearch::new();
let str1 = "a".repeat(1000);
let mut str2 = str1.clone();
str2.push('b');
c.bench_function("sse42_strcmp", |b| {
b.iter(|| {
black_box(search.sse42_strcmp(
black_box(str1.as_bytes()),
black_box(str2.as_bytes())
))
})
});
c.bench_function("scalar_strcmp", |b| {
b.iter(|| {
black_box(str1.as_bytes().cmp(str2.as_bytes()))
})
});
}
}
}