Search code examples
rustssesimdintrinsics

How to load 16 bytes of memory into a Rust __m128i?


I am trying to load 16 bytes of memory into an __m128i type from the std::arch module:

#[cfg(all(target_arch = "x86_64", target_feature = "sse2"))]
use std::arch::x86_64::__m128i;

fn foo() {
    #[cfg(all(target_arch = "x86_64", target_feature = "sse2"))]
    use std::arch::x86_64::_mm_load_si128;

    unsafe {
        let mut f: [i8; 16] = [0; 16];
        f[0] = 5;
        f[1] = 66;
        let g = _mm_load_si128(f as *const __m128i);
    }
}

fn main() {
    foo();
}

My code results in an error:

error[E0605]: non-primitive cast: `[i8; 16]` as `*const __m128i`
  --> src/main.rs:12:32
   |
12 |         let g = _mm_load_si128(f as *const __m128i);
   |                                ^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

It's not clear from the documentation how one goes about using _mm_load_si128 to load bytes from existing memory or an existing type. I want to be able to load bytes into __m128i from some existing type via a load intrinsic.


Solution

  • via a load intrinsic

    Intrinsics are the functions listed in the docs. Your specific example of loading from memory is covered by the examples in the module:

    let invec = _mm_loadu_si128(src.as_ptr() as *const _);
    

    For your case:

    let g = _mm_load_si128(f.as_ptr() as *const _);
    

    See also: