Access value from __m128 in rust by index

318 Views Asked by At

I have seen that it's rather simple in C to access values in a __m128 register by index. However, it is not possible to do that in rust. How can I access those values?

Concretely, I am calculating four values at once, then I compare them using _mm_cmplt_ps. I use that return register as a mask for further computation.

1

There are 1 best solutions below

1
orlp On

There are SIMD instructions precisely for this, e.g. such as _mm_extract_epi32, but they often require newer versions of SIMD than just SSE2.

I think the easiest way to do this using foolproof safe Rust is using the bytemuck crate, in your example:

let cmp = _mm_cmplt_ps(a, b);
let cmp_arr: [u32; 4] = bytemuck::cast(cmp);

Now cmp_arr[0], cmp_arr[1], ..., contain your results.