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

unroll contains #14

Merged
merged 1 commit into from
Jul 28, 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
22 changes: 17 additions & 5 deletions src/contains.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::SIMD_LEN;
use crate::UNROLL_FACTOR;
use std::simd::cmp::SimdPartialEq;
use std::simd::Mask;
use std::simd::{Simd, SimdElement};
Expand All @@ -19,16 +20,27 @@ where
{
fn contains_simd(&self, needle: &T) -> bool {
let arr = self.as_slice();
let (prefix, aligned_chunks, suffix) = arr.as_simd::<SIMD_LEN>();
let (prefix, simd_data, suffix) = arr.as_simd::<SIMD_LEN>();
// Prefix
if prefix.contains(&needle) {
return true;
}
// SIMD
let simd_needle = Simd::splat(*needle);
for chunk in aligned_chunks {
let mask = chunk.simd_eq(simd_needle).to_bitmask();
if mask != 0 {
// Unrolled loops
let mut chunks_iter = simd_data.chunks_exact(UNROLL_FACTOR);
for chunks in chunks_iter.by_ref() {
let mut mask = Mask::default();
for chunk in chunks {
mask |= chunk.simd_eq(simd_needle);
}
if mask.any() {
return true;
}
}
for chunk in chunks_iter.remainder() {
let mask = chunk.simd_eq(simd_needle);
if mask.any() {
return true;
}
}
Expand Down Expand Up @@ -57,7 +69,7 @@ mod tests {
Simd<T, SIMD_LEN>: SimdPartialEq<Mask = Mask<T::Mask, SIMD_LEN>>,
Standard: Distribution<T>,
{
for len in 0..100 {
for len in 0..500 {
for _ in 0..5 {
let mut v: Vec<T> = vec![T::default(); len];
let mut rng = rand::thread_rng();
Expand Down