Skip to content

Commit 01ed719

Browse files
LaihoENoratrieb
authored andcommitted
vectorized SliceContains
1 parent ba6158c commit 01ed719

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

core/src/slice/cmp.rs

+26
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 (aiming for 128 bit vectors).
268+
// The compiler will nicely unroll it.
269+
const LANE_COUNT: usize = 4 * (128 / (mem::size_of::<$t>() * 8));
270+
// SIMD
271+
let mut chunks = arr.chunks_exact(LANE_COUNT);
272+
for chunk in &mut chunks {
273+
if chunk.iter().fold(false, |acc, x| acc | (*x == *self)) {
274+
return true;
275+
}
276+
}
277+
// Scalar remainder
278+
return chunks.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)