【发布时间】:2019-12-02 16:02:22
【问题描述】:
我正在搞乱 SIMD 优化并编写了 3 个非常简单的向量类,并以 2 种不同的方式实现了加法,一种是手写组件,另一种是使用 _mm_add_ps https://godbolt.org/z/fPAERV。 有趣的是,GCC 不能(或者我没有正确地告诉它 x))使用 SSE 实现 vector2 的加法,只有在明确地向向量添加第四个浮点数之后(如在 vector3 中) gcc 使用 SEE 指令生成加法,即使我将向量对齐在 16 字节边界上。谁能告诉我为什么?
#include <xmmintrin.h>
struct alignas(16) vector final {
union {
struct {
float x, y, z;
};
float axes[3];
__m128 v;
};
vector(float x, float y, float z) noexcept : x(x), y(y), z(z) {};
vector(__m128 v) noexcept : v(v){};
};
vector operator+(const vector& v0, const vector& v1) noexcept {
return {_mm_add_ps(v0.v, v1.v)};
}
struct alignas(16) vector2 final {
union {
struct {
float x, y, z;
};
float axes[3];
__m128 v;
};
vector2(float x, float y, float z) noexcept : x(x), y(y), z(z) {};
vector2(__m128 v) noexcept : v(v){};
};
vector2 operator+(const vector2& v0, const vector2& v1) noexcept {
return {v0.x + v1.x, v0.y + v1.y, v0.z + v1.z};
}
struct alignas(16) vector3 final {
union {
struct {
float x, y, z, w;
};
float axes[4];
__m128 v;
};
vector3(float x, float y, float z, float w) noexcept : x(x), y(y), z(z), w(w) {};
vector3(__m128 v) noexcept : v(v){};
};
vector3 operator+(const vector3& v0, const vector3& v1) noexcept {
return {v0.x + v1.x, v0.y + v1.y, v0.z + v1.z, v0.w + v1.w};
}
使用带有 -std=c++17 -O3 -Wall -Wextra 的 gcc9.2 生成的程序集
operator+(vector const&, vector const&):
movaps xmm1, XMMWORD PTR [rsi]
addps xmm1, XMMWORD PTR [rdi]
movdqa xmm0, xmm1
movaps XMMWORD PTR [rsp-24], xmm1
movq xmm1, QWORD PTR [rsp-16]
ret
operator+(vector2 const&, vector2 const&):
movss xmm1, DWORD PTR [rdi+4]
movss xmm0, DWORD PTR [rdi+8]
addss xmm1, DWORD PTR [rsi+4]
addss xmm0, DWORD PTR [rsi+8]
movss xmm2, DWORD PTR [rdi]
addss xmm2, DWORD PTR [rsi]
movss DWORD PTR [rsp-20], xmm1
movss DWORD PTR [rsp-16], xmm0
movq xmm1, QWORD PTR [rsp-16]
movss DWORD PTR [rsp-24], xmm2
movq xmm0, QWORD PTR [rsp-24]
ret
operator+(vector3 const&, vector3 const&):
movaps xmm0, XMMWORD PTR [rdi]
addps xmm0, XMMWORD PTR [rsi]
movaps XMMWORD PTR [rsp-40], xmm0
mov rax, QWORD PTR [rsp-32]
movq xmm0, QWORD PTR [rsp-40]
movq xmm1, rax
mov QWORD PTR [rsp-16], rax
ret
【问题讨论】:
-
我将带有永久链接的代码添加到 Godbolt?
-
请将您的代码编辑到问题中。问题应该是自包含的,链接仅供参考。
-
你试过
-ffast-math,或者至少-fno-trapping-math?如果未屏蔽,高元素中的垃圾可能会引发 FP 异常,因此默认的-ftrapping-math可能会因此而阻塞优化。但总的来说,编译器真的不喜欢做比你告诉他们更多的事情,即使这会导致错过优化,比如单独做所有 3 个。 -
这两个标志的输出相同
-
请注意,在 C++ 中使用
union进行类型双关语是未定义的行为(stackoverflow.com/questions/11373203/…)——它可能在您的编译器上运行良好,但随时可能中断!