【问题标题】:How to load 16 bytes of memory into a Rust __m128i?如何将 16 字节的内存加载到 Rust __m128i 中?
【发布时间】:2021-06-08 19:34:22
【问题描述】:

我正在尝试从 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

【问题讨论】:

    标签: rust sse simd intrinsics


    【解决方案1】:

    通过内部负载

    内在函数是functions listed in the docs。您从内存加载的具体示例已涵盖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 _);
    

    另见:

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 2012-11-06
      • 1970-01-01
      • 2017-10-31
      相关资源
      最近更新 更多