我尝试表示一个 __int16
数组为 __m128i
元素。
类型转换__m128i
元素到 __int16
阵列效果很好。
我的示例代码:
void example() {
__m128i v = _mm_set_epi16(1, 2, 3, 4, 5, 6, 7, 8);
__int16 *p_i = (__int16 *)&v;
for (int i = 0; i < 8; i++)
std::cout <<p_i[i] << " "; // 8 7 6 5 4 3 2 1
std::cout << "\n";
__int16 i2[8] = {1, 2, 3, 4, 5, 6, 7, 8};
__m128i *p_v2 = (__m128i *) i2;
std::cout << __m128i_toString<__int16>(p_v2[0])<< "\n"; //error here
}
__m128i_toString<>()
来自 this
我错过了什么?
最佳答案
在 C++11 中,您可以使用 alignas(16) int16_t i2[8] = ...
以可移植的方式获得 16B 对齐,无需任何特定于编译器的扩展,例如 __attribute__((aligned(16)))
或 __declspec(align(16))
。
参见 the code on godbolt compiled with alignas
.
请注意,通常应避免使用相同长度的短整数数组为 __m128i
别名。以这种方式将数据放入 vector 会导致存储转发失败导致停顿。通过存储到数组然后使用标量代码处理来执行水平操作 sucks compared to SIMD .
使用 _mm_set_epi16()
可能会产生更好的代码,因为编译器不必优化掉实际的数组和指针操作。在这种情况下,它能够(clang 只是从只读常量执行 movaps
,而不首先存储到数组)。如果初始化程序不是编译时常量,您可能不会得到这么好的结果。
关于c++ - 如何将整数数组转换为 SIMD vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36160336/