开发者

How would you write code for unsigned addition likely to be optimized into one SSE instruction?

开发者 https://www.devze.com 2023-03-08 13:56 出处:网络
In C or C++ how would you write code for unsigned addition of two arrays likely to be optimized, by say GCC, into o开发者_Go百科ne 128bit SSE unsigned addition instruction?// N number of ints to be ad

In C or C++ how would you write code for unsigned addition of two arrays likely to be optimized, by say GCC, into o开发者_Go百科ne 128bit SSE unsigned addition instruction?


// N number of ints to be added
// a, b input array
// c sum array
// nReg number of required vector registers

const unsigned nReg = N*sizeof(uint32_t)/sizeof(__v4si);
__v4si a[nReg], b[nReg], c[nReg];
for (unsigned i=0; i<nReg; ++i)
    c[i] = _mm_add_epi32(a[i], b[i]);

// in c++ simply
for (unsigned i=0; i<nReg; ++i)
    c[i] = a[i] + b[i];

Unroll loop and prefetch elements at your desire. Profiling is recommended. Substitute __v4si with __v16qi, __v8hi, __v2di for 8, 16, 64 bit ints.


for (i=0; i<N; i++) c[i] = a[i] + b[i];
0

精彩评论

暂无评论...
验证码 换一张
取 消