Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vectorized SliceContains #130991

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions library/core/src/slice/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,29 @@ impl SliceContains for i8 {
memchr::memchr(byte, bytes).is_some()
}
}

macro_rules! impl_slice_contains {
($($t:ty),*) => {
$(
impl SliceContains for $t {
#[inline]
fn slice_contains(&self, arr: &[$t]) -> bool {
// Make our LANE_COUNT 4x the normal lane count (aiming for 128 bit vectors).
// The compiler will nicely unroll it.
const LANE_COUNT: usize = 4 * (128 / (mem::size_of::<$t>() * 8));
// SIMD
let mut chunks = arr.chunks_exact(LANE_COUNT);
for chunk in &mut chunks {
if chunk.iter().fold(false, |acc, x| acc | (*x == *self)) {
return true;
}
}
// Scalar remainder
return chunks.remainder().iter().any(|x| *x == *self);
}
}
)*
};
}

impl_slice_contains!(u16, u32, u64, i16, i32, i64, f32, f64, usize, isize);
Loading