1

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.

Marc
  • 41
  • 3

1 Answers1

2

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.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • Presumably would compile to the same asm as C++ `std::bit_cast` to an array. For an `__m128` (float vector) example of how that compiles, see [Why can't Clang get \_\_m128's data by index in constexpr function](https://stackoverflow.com/q/73087452) which coincidentally was also asked in the last day. (An FP compare result is appropriate to extract as `u32`, typically into an integer register, not still FP. So this case is a little different). Also note that `_mm_extract_epi32` requires a compile-time-constant index, because `pextrd` needs an immediate operand. – Peter Cordes Jul 23 '22 at 18:46