c++ - 如何从 AVX 寄存器中获取数据?

标签 c++ visual-c++ avx fma

使用 MSVC 2013 和 AVX 1,我在寄存器中有 8 个 float :

__m256 foo = mm256_fmadd_ps(a,b,c);

现在我想为所有 8 个 float 调用 inline void print(float) {...}。看起来 Intel AVX intrisics 会使这变得相当复杂:

print(_castu32_f32(_mm256_extract_epi32(foo, 0)));
print(_castu32_f32(_mm256_extract_epi32(foo, 1)));
print(_castu32_f32(_mm256_extract_epi32(foo, 2)));
// ...

但是 MSVC 甚至没有这两个内在函数。当然,我可以将值写回内存并从那里加载,但我怀疑在汇编级别没有必要溢出寄存器。

奖金问题:我当然想写

for(int i = 0; i !=8; ++i) 
    print(_castu32_f32(_mm256_extract_epi32(foo, i)))

但 MSVC 不理解许多内在函数需要循环展开。如何在 __m256 foo 中的 8x32 float 上编写循环?

最佳答案

假设您只有 AVX(即没有 AVX2),那么您可以这样做:

float extract_float(const __m128 v, const int i)
{
    float x;
    _MM_EXTRACT_FLOAT(x, v, i);
    return x;
}

void print(const __m128 v)
{
    print(extract_float(v, 0));
    print(extract_float(v, 1));
    print(extract_float(v, 2));
    print(extract_float(v, 3));
}

void print(const __m256 v)
{
    print(_mm256_extractf128_ps(v, 0));
    print(_mm256_extractf128_ps(v, 1));
}

但是我认为我可能只使用 union :

union U256f {
    __m256 v;
    float a[8];
};

void print(const __m256 v)
{
    const U256f u = { v };

    for (int i = 0; i < 8; ++i)
        print(u.a[i]);
}

关于c++ - 如何从 AVX 寄存器中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37612455/

相关文章:

c++ - C++中的基本问题

c++ - 视觉C++专业

c++ - VC++ 10 提示很多类型没有定义,C99

sse - 将 32 位 int 中打包的 8 个 4 位值零扩展到 __m256i 的英特尔矢量指令?

performance - 快速矢量化 rsqrt 并根据精度与 SSE/AVX 互惠

x86 - 将 32 位解包为 32 字节 SIMD 向量的最快方法

c++ - OpenGL 冒名顶替者球体 : problem when calculating the depth value

c++ - 在 Qt 中清除标志/提示

C++ 用 ostream 以外的东西重载运算符 <<

c++ - 如何通过 TAB 键在窗口中的控件之间导航?