Skip to content

Commit 2ef2b3e

Browse files
committed
vectorized SliceContains
1 parent a772336 commit 2ef2b3e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

library/core/src/slice/cmp.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,29 @@ impl SliceContains for i8 {
257257
memchr::memchr(byte, bytes).is_some()
258258
}
259259
}
260+
261+
macro_rules! impl_slice_contains {
262+
($($t:ty),*) => {
263+
$(
264+
impl SliceContains for $t {
265+
#[inline]
266+
fn slice_contains(&self, arr: &[$t]) -> bool {
267+
// Make our LANE_COUNT 4x the normal lane count. The compiler will nicely unroll it.
268+
const LANE_COUNT: usize = 4 * (128 / (mem::size_of::<$t>() * 8));
269+
// SIMD
270+
let mut matches = [false; LANE_COUNT];
271+
for chunk in arr.chunks_exact(LANE_COUNT){
272+
matches.iter_mut().zip(chunk).for_each(|(m, x)| *m = *x == *self);
273+
if matches.iter().fold(false, |acc, x| acc | x) {
274+
return true;
275+
}
276+
}
277+
// Scalar remainder
278+
return arr.chunks_exact(LANE_COUNT).remainder().iter().any(|x| *x == *self);
279+
}
280+
}
281+
)*
282+
};
283+
}
284+
285+
impl_slice_contains!(u16, u32, u64, i16, i32, i64, f32, f64, usize, isize);

0 commit comments

Comments
 (0)