gpt4 book ai didi

c++ - SSE加法和转换

转载 作者:行者123 更新时间:2023-11-27 23:53:23 25 4
gpt4 key购买 nike

问题是,如何使用 SSE 添加两个无符号字符数组并将结果存储在无符号短数组中。谁能给我一些帮助或提示。这是我到目前为止所做的。我只是不知道错误在哪里..需要一些帮助

#include<iostream>
#include<intrin.h>
#include<windows.h>
#include<emmintrin.h>
#include<iterator>

using namespace std;

void sse_add(unsigned char * input1, unsigned char *input2, unsigned short *output, const int N)
{

unsigned char *op3 = new unsigned char[N];
unsigned char *op4 = new unsigned char[N];

__m128i *sse_op3 = (__m128i*)op3;
__m128i *sse_op4 = (__m128i*)op4;
__m128i *sse_result = (__m128i*)output;

for (int i = 0; i < N; i = i + 16)
{
__m128i src = _mm_loadu_si128((__m128i*)input1);
__m128i zero = _mm_setzero_si128();
__m128i higher = _mm_unpackhi_epi8(src, zero);
__m128i lower = _mm_unpacklo_epi8(src, zero);

_mm_storeu_si128(sse_op3, lower);
sse_op3 = sse_op3 + 1;
_mm_storeu_si128(sse_op3, higher);
sse_op3 = sse_op3 + 1;
input1 = input1 + 16;

}

for (int j = 0; j < N; j = j + 16)
{
__m128i src1 = _mm_loadu_si128((__m128i*)input2);
__m128i zero1 = _mm_setzero_si128();
__m128i higher1 = _mm_unpackhi_epi8(src1, zero1);
__m128i lower1 = _mm_unpacklo_epi8(src1, zero1);

_mm_storeu_si128(sse_op4, lower1);
sse_op4 = sse_op4 + 1;
_mm_storeu_si128(sse_op4, higher1);
sse_op4 = sse_op4 + 1;
input2 = input2 + 16;

}

__m128i *sse_op3_new = (__m128i*)op3;
__m128i *sse_op4_new = (__m128i*)op4;

for (int y = 0; y < N; y = y + 8)
{
*sse_result = _mm_adds_epi16(*sse_op3_new, *sse_op4_new);
sse_result = sse_result + 1;
sse_op3_new = sse_op3_new + 1;
sse_op4_new = sse_op4_new + 1;
}

}

void C_add(unsigned char * input1, unsigned char *input2, unsigned short *output, int N)
{
for (int i = 0; i < N; i++)
output[i] = (unsigned short)input1[i] + (unsigned short)input2[i];

}



int main()
{
int n = 1023;
unsigned char *p0 = new unsigned char[n];
unsigned char *p1 = new unsigned char[n];
unsigned short *p21 = new unsigned short[n];
unsigned short *p22 = new unsigned short[n];
for (int j = 0; j < n; j++)
{
p21[j] = rand() % 256;
p22[j] = rand() % 256;
}

C_add(p0, p1, p22, n);
cout << "C_add finished!" << endl;
sse_add(p0, p1, p21, n);
cout << "sse_add finished!" << endl;

for (int j = 0; j < n; j++)
{
if (p21[j] != p22[j])
{
cout << "diff!!!!!@@@@@@@" << endl;
}
}
//system("pause");

delete[] p0;
delete[] p1;
delete[] p21;
delete[] p22;
return 0;


}

最佳答案

假设所有内容都与 _Alignof(__m128i) 对齐并且数组的大小是 sizeof(__m128i) 的倍数,这样的事情应该可行:

void addw(size_t size, uint16_t res[size], uint8_t a[size], uint8_t b[size]) {
__m128i* r = (__m128i*) res;
__m128i* ap = (__m128i*) a;
__m128i* bp = (__m128i*) b;

for (size_t i = 0 ; i < (size / sizeof(__m128i)) ; i++) {
r[(i * 2)] = _mm_add_epi16(_mm_cvtepu8_epi16(ap[i]), _mm_cvtepu8_epi16(bp[i]));
r[(i * 2) + 1] = _mm_add_epi16(_mm_cvtepu8_epi16(_mm_srli_si128(ap[i], 8)), _mm_cvtepu8_epi16(_mm_srli_si128(bp[i], 8)));
}
}

FWIW,NEON 会更简单一些(使用 vaddl_u8vaddl_high_u8)。

如果您正在处理未对齐的数据,您可以使用 _mm_loadu_si128/_mm_storeu_si128。如果大小不是 16 的倍数,您只需在没有 SSE 的情况下完成余数。

请注意,这可能是您的编译器可以自动执行的操作(我还没有检查过)。您可能想尝试这样的事情:

#pragma omp simd
for (size_t i = 0 ; i < size ; i++) {
res[i] = ((uint16_t) a[i]) + ((uint16_t) b[i]);
}

它使用 OpenMP 4,但也有 Cilk++ (#pragma simd)、clang (#pragma clang loop vectorize(enable))、gcc ( #pragma GCC ivdep),或者您可能只是希望编译器足够智能而没有 pragma 提示。

关于c++ - SSE加法和转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44511386/

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