Idx of first elem in [u32; 16] where x % 4 == k

for i in 0 .. 16 {
  if a[i] % 4 == k { return i; }
}
return 16;
}

Is there a simple way to use simd to get the idx of the first elem where x % 4 == k in a faster manner ? (pure rust preferred; needs to also work on wasm32)

Have you looked at position?

(playground)

2 Likes

Why is this faster? How does it use simd? I don't see how this is an improvement.

I completely overlooked the SIMD part. Sorry.

1 Like

No harm, thanks for verifying. I am trying to find a solution that exploits the fact that (1) we know before hand that [u32; 16] has 16 elements, and (2) with proper alignment this can be in a cache line. Any solution that works on arbitrary n rather than n == 16 is probably not what I'm looking for.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.