我有两个sse整数,我按如下方式分配它:
__m128i m1 = _mm_set_epi32(4,3,2,1);
__m128i m2 = _mm_set_epi32(40,30,20,10);
现在,我必须在这两个寄存器之间进行一些改组,并将结果存储在另外两个寄存器中,以便输出如下:
m3 = (30,3,10,1);
m4 = (40,4,20,2);
有什么办法可以实现这个目标!!
由于
答案 0 :(得分:2)
你可以这样做:
t1 = _mm_shuffle_epi32(m1, 0xd8);
t2 = _mm_shuffle_epi32(m2, 0xd8);
m4 = _mm_unpackhi_epi32(t1,t2);
m3 = _mm_unpacklo_epi32(t1,t2);
这是一个完整的例子
#include <x86intrin.h>
#include <stdio.h>
int main() {
__m128i m1 = _mm_set_epi32(4,3,2,1);
__m128i m2 = _mm_set_epi32(40,30,20,10);
__m128i m3, m4, t1, t2;
t1 = _mm_shuffle_epi32(m1, 0xd8);
t2 = _mm_shuffle_epi32(m2, 0xd8);
m4 = _mm_unpackhi_epi32(t1,t2);
m3 = _mm_unpacklo_epi32(t1,t2);
int out3[4], out4[4];
_mm_store_si128((__m128i*)out3, m3);
_mm_store_si128((__m128i*)out4, m4);
printf("%d %d %d %d\n", out3[3], out3[2], out3[1], out3[0]);
printf("%d %d %d %d\n", out4[3], out4[2], out4[1], out4[0]);
}
输出
30 3 10 1
40 4 20 2
答案 1 :(得分:1)
我想出了一个与Z Boson略有不同的解决方案:
#include <emmintrin.h>
#include <iostream>
#include <cstring>
void print_data(const char *name, __m128i v)
{
struct { int a, b, c, d; } unpacked;
std::memcpy((void *)&unpacked, (void *)&v, sizeof(v));
std::cout << name << ":";
std::cout << unpacked.d << " " << unpacked.c << " "
<< unpacked.b << " " << unpacked.a << std::endl;
}
int main()
{
__m128i m1 = _mm_set_epi32(4,3,2,1);
__m128i m2 = _mm_set_epi32(40,30,20,10);
__m128i mask = _mm_set_epi32(-1,0,-1,0);
/*
m3 = (30,3,10,1);
m4 = (40,4,20,2);
*/
__m128i tmp1 = _mm_shuffle_epi32(m2, 0x80);
__m128i tmp2 = _mm_shuffle_epi32(m1, 0x31);
__m128i m3 = _mm_or_si128(_mm_and_si128(mask, tmp1), _mm_andnot_si128(mask, m1));
__m128i m4 = _mm_or_si128(_mm_and_si128(mask, m2), _mm_andnot_si128(mask, tmp2));
print_data("m3", m3);
print_data("m4", m4);
}
我确定解包变体稍微好一些......