gpt4 book ai didi

c++ - SSE - 从 double 到 char 的 AVX 转换

转载 作者:行者123 更新时间:2023-11-30 01:13:57 24 4
gpt4 key购买 nike

我想将 double 值 vector 转换为 char。我必须采用两种截然不同的方法,一种用于 SSE2,另一种用于 AVX2。

我从 AVX2 开始。

__m128i sub_proc(__m256d& in)
{
__m256d _zero_pd = _mm256_setzero_pd();

__m256d ih_pd = _mm256_unpackhi_pd(in,_zero_pd);
__m256d il_pd = _mm256_unpacklo_pd(in,_zero_pd);

__m128i ih_si = _mm256_cvtpd_epi32(ih_pd);
__m128i il_si = _mm256_cvtpd_epi32(il_pd);

ih_si = _mm_shuffle_epi32(ih_si,_MM_SHUFFLE(3,1,2,0));
il_si = _mm_shuffle_epi32(il_si,_MM_SHUFFLE(3,1,2,0));

ih_si = _mm_packs_epi32(_mm_unpacklo_epi32(il_si,ih_si),_mm_unpackhi_epi32(il_si,ih_si));

return ih_si;
}

__m128i proc(__m256d& in1,__m256d& in2)
{
__m256d _zero_pd = _mm_setzeros_pd();

__m128i in1_si = sub_proc(in1);
__m128i in2_si = sub_proc(in2);

return _mm_packs_epi16(in1_si,in2_si);
}
int main()
{

double input[32] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};

char output[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

char check[8];

double* ibeg = input;
char* obeg = output;

for(int i=0;i<32;i+=8)
{

__m256d in1 = _mm256_loadu_pd(ibeg);
__m256d in2 = _mm256_loadu_pd(ibeg+4);


__m128i tmp = proc(in1,in2);

_mm_storeu_si128(reinterpret_cast<__m128i*>(check),tmp);

std::copy(check,check+8,std::ostream_iterator<int>(std::cout," "));
std::cout<<std::endl;

_mm_storeu_si128(reinterpret_cast<__m128i*>(obeg+i),tmp);
}

}

在此算法结束时,输出包含:

1,2,3,4,0,0,0,0,9,10,11,12,0,0,0,0,17,18,19,20,0,0,0,0,25,26,27,28,0,0,0,0

我的第一个调查显示,如果在函数 proc 中我更改:

      return _mm_packs_epi16(in1_si,in2_si);

到:

      return _mm_packs_epi16(in2_si,in1_si);

然后输出包含:

5,6,7,8,0,0,0,0,13,14,15,16,0,0,0,0,21,22,23,24,0,0,0,0,29,30,31,31,0,0,0,0

我还没有想出如何打乱 in2_si 的低和高部分。

是否有更好(更快、更有效)的方法使用 SIMD 将 double 转换为 char?

最佳答案

如果你想转换,例如使用 AVX/SSE 每次迭代 16 个 double 到 16 个 char 然后这里是一些有效的代码:

#include <iostream>
#include <immintrin.h>

__m128i proc(const __m256d in0, const __m256d in1, const __m256d in2, const __m256d in3)
{
__m128i v0 = _mm256_cvtpd_epi32(in0);
__m128i v1 = _mm256_cvtpd_epi32(in1);
__m128i v2 = _mm256_cvtpd_epi32(in2);
__m128i v3 = _mm256_cvtpd_epi32(in3);
__m128i v01 = _mm_packs_epi32(v0, v1);
__m128i v23 = _mm_packs_epi32(v2, v3);
return _mm_packs_epi16(v01, v23);
}

int main()
{
double input[32] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32};

char output[32] = {0};

for (int i = 0; i < 32; i += 16) // two iterations
{
__m256d in0 = _mm256_loadu_pd(&input[i]); // load 4 x 4 doubles
__m256d in1 = _mm256_loadu_pd(&input[i + 4]);
__m256d in2 = _mm256_loadu_pd(&input[i + 8]);
__m256d in3 = _mm256_loadu_pd(&input[i + 12]);

__m128i out = proc(in0, in1, in2, in3); // pack to 16 chars

_mm_storeu_si128(reinterpret_cast<__m128i*>(&output[i]), out);
}

for (int i = 0; i < 32; ++i)
{
std::cout << (int)output[i] << " ";
}
std::cout << std::endl;

return 0;
}

编译运行:

$ g++ -Wall -mavx double_to_char.cpp && ./a.out 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

请注意,上面的代码只需要 AVX(不需要 AVX2)。

关于c++ - SSE - 从 double 到 char 的 AVX 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30853773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com