我想用 C++ 编写以下函数(使用 gcc 11.1 编译-O3 -mavx -std=c++17)
void f( float * __restrict__ a, float * __restrict__ b, float * __restrict__ c, int64_t n) {
for (int64_t i = 0; i != n; ++i) {
a[i] = b[i] + c[i];
}
}
Run Code Online (Sandbox Code Playgroud)
这会生成大约 60 行汇编代码,其中许多用于处理 n 不是 8 的倍数的情况。https://godbolt.org/z/61MYPG7an
我知道这n始终是 8 的倍数。我可以更改此代码的一种方法是将其替换for (int64_t i = 0; i != n; ++i)为for (int64_t i = 0; i != (n / 8 * 8); ++i) …