Skip to content

Commit a7d4556

Browse files
committed
Regression introduced by rust 1.56.0
1 parent 820a28f commit a7d4556

File tree

2 files changed

+67
-40
lines changed

2 files changed

+67
-40
lines changed

src/lib.rs

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl MemchrSearcher {
150150
}
151151

152152
/// Represents a generic SIMD register type.
153-
trait Vector: Copy {
153+
trait Vector: Copy + std::fmt::Debug {
154154
const LANES: usize;
155155

156156
unsafe fn set1_epi8(a: i8) -> Self;
@@ -208,18 +208,28 @@ trait Searcher<N: NeedleWithSize + ?Sized> {
208208
let first = Vector::loadu_si(start);
209209
let last = Vector::loadu_si(start.add(self.position()));
210210

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);
211214
let eq_first = Vector::cmpeq_epi8(hash.first, first);
212215
let eq_last = Vector::cmpeq_epi8(hash.last, last);
213216

214217
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+
);
215222
let mut eq = (Vector::movemask_epi8(eq) & mask) as u32;
216223

217224
let start = start as usize - haystack.as_ptr() as usize;
218225
let chunk = haystack.as_ptr().add(start + 1);
219226
let needle = self.needle().as_bytes().as_ptr().add(1);
220227

228+
println!("[vector_search_in_chunk] eq={}", eq);
229+
221230
while eq != 0 {
222231
let chunk = chunk.add(eq.trailing_zeros() as usize);
232+
println!("[vector_search_in_chunk] chunk={:?}", chunk);
223233
let equal = match N::SIZE {
224234
Some(0) => unreachable!(),
225235
Some(1) => memcmp::specialized::<0>(chunk, needle),
@@ -249,6 +259,45 @@ trait Searcher<N: NeedleWithSize + ?Sized> {
249259

250260
false
251261
}
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+
}
252301
}
253302

254303
#[cfg(test)]
@@ -331,11 +380,13 @@ mod tests {
331380
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
332381
use crate::x86::{Avx2Searcher, DynamicAvx2Searcher};
333382

383+
println!("test for {}", position);
384+
334385
let searcher = unsafe { Avx2Searcher::with_position(needle, position) };
335386
assert_eq!(unsafe { searcher.search_in(haystack) }, result);
336387

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);*/
339390
} else {
340391
compile_error!("Unsupported architecture");
341392
}
@@ -453,25 +504,25 @@ mod tests {
453504

454505
#[test]
455506
fn search_middle() {
456-
assert!(search(b"xyz", b"y"));
507+
/*assert!(search(b"xyz", b"y"));
457508
458509
assert!(search(b"wxyz", b"xy"));
459510
460-
assert!(search(b"foobarfoo", b"bar"));
511+
assert!(search(b"foobarfoo", b"bar"));*/
461512

462513
assert!(search(
463514
b"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
464515
b"consectetur"
465516
));
466517

467-
assert!(search(
518+
/*assert!(search(
468519
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",
469520
b"orci"
470521
));
471522
472523
assert!(search(
473524
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",
474525
b"Maecenas commodo posuere orci a consectetur"
475-
));
526+
));*/
476527
}
477528
}

src/x86.rs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::arch::x86::*;
77
#[cfg(target_arch = "x86_64")]
88
use std::arch::x86_64::*;
99

10-
#[derive(Clone, Copy)]
10+
#[derive(Clone, Copy, Debug)]
1111
#[repr(transparent)]
1212
#[allow(non_camel_case_types)]
1313
struct __m16i(__m128i);
@@ -53,7 +53,7 @@ impl From<__m128i> for __m16i {
5353
}
5454
}
5555

56-
#[derive(Clone, Copy)]
56+
#[derive(Clone, Copy, Debug)]
5757
#[repr(transparent)]
5858
#[allow(non_camel_case_types)]
5959
struct __m32i(__m128i);
@@ -99,7 +99,7 @@ impl From<__m128i> for __m32i {
9999
}
100100
}
101101

102-
#[derive(Clone, Copy)]
102+
#[derive(Clone, Copy, Debug)]
103103
#[repr(transparent)]
104104
#[allow(non_camel_case_types)]
105105
struct __m64i(__m128i);
@@ -294,36 +294,6 @@ impl<N: Needle> Avx2Searcher<N> {
294294
}
295295
}
296296

297-
#[inline]
298-
#[target_feature(enable = "avx2")]
299-
unsafe fn vector_search_in<V: Vector>(
300-
&self,
301-
haystack: &[u8],
302-
end: usize,
303-
hash: &VectorHash<V>,
304-
) -> bool {
305-
debug_assert!(haystack.len() >= self.needle.size());
306-
307-
let mut chunks = haystack[..end].chunks_exact(V::LANES);
308-
for chunk in &mut chunks {
309-
if self.vector_search_in_chunk(haystack, hash, chunk.as_ptr(), -1) {
310-
return true;
311-
}
312-
}
313-
314-
let remainder = chunks.remainder().len();
315-
if remainder > 0 {
316-
let start = haystack.as_ptr().add(end - V::LANES);
317-
let mask = -1 << (V::LANES - remainder);
318-
319-
if self.vector_search_in_chunk(haystack, hash, start, mask) {
320-
return true;
321-
}
322-
}
323-
324-
false
325-
}
326-
327297
#[inline]
328298
#[target_feature(enable = "avx2")]
329299
unsafe fn sse2_2_search_in(&self, haystack: &[u8], end: usize) -> bool {
@@ -361,6 +331,11 @@ impl<N: Needle> Avx2Searcher<N> {
361331
#[inline]
362332
#[target_feature(enable = "avx2")]
363333
pub unsafe fn inlined_search_in(&self, haystack: &[u8]) -> bool {
334+
println!(
335+
"[inlined_search_in] haystack({})={:?}",
336+
haystack.len(),
337+
haystack
338+
);
364339
if haystack.len() <= self.needle.size() {
365340
return haystack == self.needle.as_bytes();
366341
}
@@ -385,6 +360,7 @@ impl<N: Needle> Avx2Searcher<N> {
385360
/// Performs a substring search for the `needle` within `haystack`.
386361
#[target_feature(enable = "avx2")]
387362
pub unsafe fn search_in(&self, haystack: &[u8]) -> bool {
363+
println!("[search_in] haystack({})={:?}", haystack.len(), haystack);
388364
self.inlined_search_in(haystack)
389365
}
390366
}

0 commit comments

Comments
 (0)