问题描述
我有一个结构,其中包含各种数据成员之间的并集和一个AVX类型,以一次加载所有字节.我的代码如下:
I have a struct which contains a union between various data members and an AVX type to load all the bytes in one load. My code looks like:
#include <immintrin.h>
union S{
struct{
int32_t a;
int32_t b;
int16_t c;
int16_t d;
};
__m128i x;
}
我想使用AVX寄存器将数据全部加载在一起,然后分别将四个成员提取到int32_t
和int16_t
局部变量中.
I'd like to use the AVX register to load the data all together and then separately extract the four members in to int32_t
and int16_t
local variables.
我将如何去做?我不确定从AVX寄存器提取数据时如何将数据成员彼此分开?
How would I go about doing this? I am unsure how I can separate the data members from each other when extracting from the AVX register?
正在寻找有关GCC内在函数的答案.
Was looking for answers in terms of GCC intrinsics.
已更新代码以用union交换结构.
Have updated the code to swap struct with union.
推荐答案
您可以使用_mm_extract_epi16
(需要SSE2)从__m128i
中提取16位元素:
You can extract 16 bit elements from an __m128i
using _mm_extract_epi16
(requires SSE2):
int16_t v = _mm_extract_epi16 (v, 4); // extract element 4
对于32位元素,请使用_mm_extract_epi32
(需要SSE4.1)
For 32 bit elements use _mm_extract_epi32
(requires SSE4.1)
int32_t v = _mm_extract_epi32 (v, 0); // extract element 0
请参阅:《英特尔内在指南》
假设您的结构声明为:
union S{
struct{
int32_t a;
int32_t b;
int16_t c;
int16_t d;
};
__m128i x;
}
然后您将提取元素a,b,c,d如下:
then you would extract elements a, b, c, d as follows:
S s = { { 1, 2, 3, 4 } };
__m128i v = _mm_loadu_si128((__m128i *)&s);
int32_t a = _mm_extract_epi32 (v, 0);
int32_t b = _mm_extract_epi32 (v, 1);
int16_t c = _mm_extract_epi16 (v, 4);
int16_t d = _mm_extract_epi16 (v, 5);
这篇关于使用AVX从结构中提取整数和短裤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!