Lui*_*lli 15 x86 sse simd vector-processing avx
我有一个四个64位浮点值的打包向量.
我想得到向量元素的总和.
使用SSE(并使用32位浮点数),我可以执行以下操作:
v_sum = _mm_hadd_ps(v_sum, v_sum);
v_sum = _mm_hadd_ps(v_sum, v_sum);
Run Code Online (Sandbox Code Playgroud)
不幸的是,即使AVX具有_mm256_hadd_pd指令,它的结果也与SSE版本不同.我相信这是因为大多数AVX指令分别用作每个低128位和高128位的SSE指令,而不会跨越128位边界.
理想情况下,我要寻找的解决方案应遵循以下准则:
1)仅使用AVX/AVX2指令.(没有SSE)
2)不超过2-3条指令.
但是,任何有效/优雅的方式(即使不遵循上述指导原则)总是被广泛接受.
非常感谢您的帮助.
-Luigi Castelli
Jas*_*n R 22
如果你有两个__m256d向量x1,x2并且每个向量包含double你想要水平求和的四个s,你可以这样做:
__m256d x1, x2;
// calculate 4 two-element horizontal sums:
// lower 64 bits contain x1[0] + x1[1]
// next 64 bits contain x2[0] + x2[1]
// next 64 bits contain x1[2] + x1[3]
// next 64 bits contain x2[2] + x2[3]
__m256d sum = _mm256_hadd_pd(x1, x2);
// extract upper 128 bits of result
__m128d sum_high = _mm256_extractf128_pd(sum1, 1);
// add upper 128 bits of sum to its lower 128 bits
__m128d result = _mm_add_pd(sum_high, _mm256_castpd256_pd128(sum));
// lower 64 bits of result contain the sum of x1[0], x1[1], x1[2], x1[3]
// upper 64 bits of result contain the sum of x2[0], x2[1], x2[2], x2[3]
Run Code Online (Sandbox Code Playgroud)
因此,看起来3条指令将执行您需要的2个水平和.以上是未经测试的,但你应该得到这个概念.
| 归档时间: |
|
| 查看次数: |
16576 次 |
| 最近记录: |