Rollup merge of #130991 - LaihoE:vectorized_slice_contains, r=Noratrieb · qinheping/verify-rust-std@76e75ae (original) (raw)
`@@ -257,3 +257,29 @@ impl SliceContains for i8 {
`
257
257
` memchr::memchr(byte, bytes).is_some()
`
258
258
`}
`
259
259
`}
`
``
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);
`