如何在__m128i的特定位置加载字节

How to load bytes in a __m128i in a specific position

本文关键字:位置 定位 加载 字节 m128i      更新时间:2023-10-16

我需要加载 4 个字节连续存储在数组中的 __m128i 变量的特定位置,即能够执行许多int32_t和,一次 4 个,存储所有部分结果。

例如:

const unsigned int SIZE = 2000000;
const unsigned int STEP = 100;
unsigned char* inBuffer = new char[SIZE];
//Fill inBuffer
const unsigned char* a = inBuffer;
int32_t* outBuffer = new int32_t[SIZE/STEP*4];
int32_t* result = outBuffer;
__m128i sum = _mm_setzero_si128 ()
for (int i = 0; i < SIZE; i+=STEP) {
    __m128i value = _mm_set_epi32 (a[3],a[2],a[1],a[0]);
    sum = __mm_add_epi32(sum,value);
    _mm_storeu_si128 ((__m128i*)result,sum);
    a+=STEP;
    result+=4;
    }
//Print outBuffer
delete[] inBuffer;
delete[] outBuffer;

我想知道是否有更有效的方法可以做到这一点

这里的主要问题当然是这一行:

__m128i value = _mm_set_epi32 (a[3],a[2],a[1],a[0]);

但是,一个像样的编译器应该为此生成相当有效的代码。看看输出(gcc -O3 -S ...) - 如果它不仅仅是几个指令,那么您可能需要考虑自己进行加载/解包操作。