![](/img/trans.png)
[英]AVX 256-bit code performing slightly worse than equivalent 128-bit SSSE3 code
[英]Most efficient way to test a 256-bit YMM AVX register element for equal or less than zero
我正在使用Intel AVX内部函数实现粒子系统。 当粒子的Y位置小于或等于零时,我想重置粒子。
粒子系统按如下SOA模式排序:
class ParticleSystem
{
private:
float* mXPosition;
float* mYPosition;
float* mZPosition;
.... Rest of code not important for this question
我想到的最初方法只是迭代mYPosition数组,并检查开头所述的情况。 也许可以用这种方法改进一些性能?
但是,问题是是否存在使用AVX内在函数实现此目的的有效方法? 谢谢!
如果<= 0的元素相对稀疏,则一种简单的方法是使用AVX一次测试8,然后在识别包含一个或多个此类元素的向量时放入标量代码,例如:
#include <immintrin.h> // AVX intrinsics
const __m256 vk0 = _mm256_setzero_ps(); // const vector of zeros
for (int i = 0; i + 8 <= n; i += 8)
{
__m256 vy = _mm256_loadu_ps(&mYPosition[i]); // load 8 x floats
__m256 vcmp = _mm256_cmp_ps(vy, vk0, _CMP_LE_OS); // compare for <= 0
int mask = _mm256_movemask_ps(vcmp); // get MS bits from comparison result
if (mask != 0) // if any bits set
{ // then we have 1 or more elements <= 0
for (int k = 0; k < 8; ++k) // test each element in vector
{ // using scalar code...
if ((mask & 1) != 0)
{
// found element at index i + k
// do something with it...
}
mask >>= 1;
}
}
}
// deal with any remaining elements in case where n is not a multiple of 8
for (int j = i; j < n; ++j)
{
if (mYPosition[j] <= 0.0f)
{
// found element at index j
// do something with it...
}
}
当然,如果匹配的元素不是稀疏的,即,如果通常在8的每个向量中找到一个或多个,则不会为您带来任何性能提升。 但是,如果元素稀疏,可以跳过大多数矢量,那么您应该会看到很大的好处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.