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

Posted

技术标签:

【中文标题】如何将 16 字节的内存加载到 Rust __m128i 中?【英文标题】:How to load 16 bytes of memory into a 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

【问题讨论】:

【参考方案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 _);

另见:

arch::x86_64::_mm_loadu_si128

【讨论】:

以上是关于如何将 16 字节的内存加载到 Rust __m128i 中?的主要内容,如果未能解决你的问题,请参考以下文章

JVM核心机制_类加载的全过程_java的内存分析_初始化时机。

易语言如何转换16进制?

java_反射

正确使用加载/存储

Linux性能学习(2.3):内存_为什么分配的内存比申请的内存大16个字节

将 C++ 'extern "C" __declspec(dllexport)' 结构转换为 Rust 的问题