如何将 16 字节的内存加载到 Rust __m128i?

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

我正在尝试从 std::arch 模块将 16 字节的内存加载到 __m128i 类型中:

#[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();
}

我的代码导致错误:

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

documentation 中不清楚如何使用 _mm_load_si128 从现有内存或现有类型加载字节。我希望能够通过加载内在函数将一些现有类型的字节加载到 __m128i 中。

via a load intrinsic

内在函数是 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 _);

针对您的情况:

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

另请参阅: