@@ -150,7 +150,7 @@ impl MemchrSearcher {
150
150
}
151
151
152
152
/// Represents a generic SIMD register type.
153
- trait Vector : Copy {
153
+ trait Vector : Copy + std :: fmt :: Debug {
154
154
const LANES : usize ;
155
155
156
156
unsafe fn set1_epi8 ( a : i8 ) -> Self ;
@@ -208,18 +208,28 @@ trait Searcher<N: NeedleWithSize + ?Sized> {
208
208
let first = Vector :: loadu_si ( start) ;
209
209
let last = Vector :: loadu_si ( start. add ( self . position ( ) ) ) ;
210
210
211
+ // Uncommenting the following lines makes it work
212
+ // println!("[vector_search_in_chunk] hash.first={:?}", hash.first);
213
+ println ! ( "[vector_search_in_chunk] first={:?}" , first) ;
211
214
let eq_first = Vector :: cmpeq_epi8 ( hash. first , first) ;
212
215
let eq_last = Vector :: cmpeq_epi8 ( hash. last , last) ;
213
216
214
217
let eq = Vector :: and_si ( eq_first, eq_last) ;
218
+ println ! (
219
+ "[vector_search_in_chunk] eq_first={:?}, eq_last={:?}" ,
220
+ eq_first, eq_last
221
+ ) ;
215
222
let mut eq = ( Vector :: movemask_epi8 ( eq) & mask) as u32 ;
216
223
217
224
let start = start as usize - haystack. as_ptr ( ) as usize ;
218
225
let chunk = haystack. as_ptr ( ) . add ( start + 1 ) ;
219
226
let needle = self . needle ( ) . as_bytes ( ) . as_ptr ( ) . add ( 1 ) ;
220
227
228
+ println ! ( "[vector_search_in_chunk] eq={}" , eq) ;
229
+
221
230
while eq != 0 {
222
231
let chunk = chunk. add ( eq. trailing_zeros ( ) as usize ) ;
232
+ println ! ( "[vector_search_in_chunk] chunk={:?}" , chunk) ;
223
233
let equal = match N :: SIZE {
224
234
Some ( 0 ) => unreachable ! ( ) ,
225
235
Some ( 1 ) => memcmp:: specialized :: < 0 > ( chunk, needle) ,
@@ -249,6 +259,45 @@ trait Searcher<N: NeedleWithSize + ?Sized> {
249
259
250
260
false
251
261
}
262
+
263
+ #[ inline]
264
+ // Uncommenting the following lines makes it work
265
+ // #[target_feature(enable = "avx2")]
266
+ unsafe fn vector_search_in < V : Vector > (
267
+ & self ,
268
+ haystack : & [ u8 ] ,
269
+ end : usize ,
270
+ hash : & VectorHash < V > ,
271
+ ) -> bool {
272
+ println ! (
273
+ "[vector_search_in] haystack({})={:?}, end={}" ,
274
+ haystack. len( ) ,
275
+ haystack,
276
+ end
277
+ ) ;
278
+ debug_assert ! ( haystack. len( ) >= self . needle( ) . size( ) ) ;
279
+
280
+ let mut chunks = haystack[ ..end] . chunks_exact ( V :: LANES ) ;
281
+ for chunk in & mut chunks {
282
+ println ! ( "[vector_search_in] chunk({})={:?}" , chunk. len( ) , chunk) ;
283
+ if self . vector_search_in_chunk ( haystack, hash, chunk. as_ptr ( ) , -1 ) {
284
+ return true ;
285
+ }
286
+ }
287
+
288
+ let remainder = chunks. remainder ( ) . len ( ) ;
289
+ println ! ( "[vector_search_in] remainder: {}" , remainder) ;
290
+ if remainder > 0 {
291
+ let start = haystack. as_ptr ( ) . add ( end - V :: LANES ) ;
292
+ let mask = -1 << ( V :: LANES - remainder) ;
293
+
294
+ if self . vector_search_in_chunk ( haystack, hash, start, mask) {
295
+ return true ;
296
+ }
297
+ }
298
+
299
+ false
300
+ }
252
301
}
253
302
254
303
#[ cfg( test) ]
@@ -331,11 +380,13 @@ mod tests {
331
380
if #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ] {
332
381
use crate :: x86:: { Avx2Searcher , DynamicAvx2Searcher } ;
333
382
383
+ println!( "test for {}" , position) ;
384
+
334
385
let searcher = unsafe { Avx2Searcher :: with_position( needle, position) } ;
335
386
assert_eq!( unsafe { searcher. search_in( haystack) } , result) ;
336
387
337
- let searcher = unsafe { DynamicAvx2Searcher :: with_position( needle, position) } ;
338
- assert_eq!( unsafe { searcher. search_in( haystack) } , result) ;
388
+ /* let searcher = unsafe { DynamicAvx2Searcher::with_position(needle, position) };
389
+ assert_eq!(unsafe { searcher.search_in(haystack) }, result);*/
339
390
} else {
340
391
compile_error!( "Unsupported architecture" ) ;
341
392
}
@@ -453,25 +504,25 @@ mod tests {
453
504
454
505
#[ test]
455
506
fn search_middle ( ) {
456
- assert ! ( search( b"xyz" , b"y" ) ) ;
507
+ /* assert!(search(b"xyz", b"y"));
457
508
458
509
assert!(search(b"wxyz", b"xy"));
459
510
460
- assert ! ( search( b"foobarfoo" , b"bar" ) ) ;
511
+ assert!(search(b"foobarfoo", b"bar"));*/
461
512
462
513
assert ! ( search(
463
514
b"Lorem ipsum dolor sit amet, consectetur adipiscing elit" ,
464
515
b"consectetur"
465
516
) ) ;
466
517
467
- assert ! ( search(
518
+ /* assert!(search(
468
519
b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas commodo posuere orci a consectetur. Ut mattis turpis ut auctor consequat. Aliquam iaculis fringilla mi, nec aliquet purus",
469
520
b"orci"
470
521
));
471
522
472
523
assert!(search(
473
524
b"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas commodo posuere orci a consectetur. Ut mattis turpis ut auctor consequat. Aliquam iaculis fringilla mi, nec aliquet purus",
474
525
b"Maecenas commodo posuere orci a consectetur"
475
- ) ) ;
526
+ ));*/
476
527
}
477
528
}
0 commit comments