这个问题与[1]类似。但是我不太明白它如何解决使用 GPR 插入 ymm 的高四字的问题。此外,我希望该操作不使用任何中间内存访问。
可以用 AVX2 或更低版本完成吗(我没有 AVX512)?
[1] How to move double in %rax into particular qword position on %ymm or %zmm? (Kaby Lake or later)
最佳答案
我的回答on the linked question没有展示一种方法来做到这一点,因为如果没有 AVX512F 进行屏蔽广播( vpbroadcastq zmm0{k1}, rax
),它就无法非常有效地完成。但实际上使用暂存寄存器并没有那么糟糕,与 vpinsrq
的成本大致相同。 + 立即混合。
(在 Intel 上,总共 3 uops。端口 5(vmovq + 广播)有 2 uops,并且可以在任何端口上运行的立即混合。 请参阅https://agner.org/optimize/ )。
我用 asm 更新了我的答案。在具有英特尔内在函数的 C++ 中,您可以执行以下操作:
#include <immintrin.h>
#include <stdint.h>
// integer version. An FP version would still use _mm256_set1_epi64x, then a cast
template<unsigned elem>
static inline
__m256i merge_epi64(__m256i v, int64_t newval)
{
static_assert(elem <= 3, "a __m256i only has 4 qword elements");
__m256i splat = _mm256_set1_epi64x(newval);
constexpr unsigned dword_blendmask = 0b11 << (elem*2); // vpblendd uses 2 bits per qword
return _mm256_blend_epi32(v, splat, dword_blendmask);
}
Clang 对所有 4 个可能的元素位置进行了几乎完美高效的编译,这真正展示了它的 shuffle 优化器有多么好。它利用了所有特殊情况。作为奖励,它会对其 asm 进行注释,以向您显示哪些元素来自混合和洗牌中的何处。
<强> From the Godbolt compiler explorer ,一些测试函数来查看 regs 中的 args 发生了什么。
__m256i merge3(__m256i v, int64_t newval) {
return merge_epi64<3> (v, newval);
}
// and so on for 2..0
# clang7.0 -O3 -march=haswell
merge3(long long __vector(4), long):
vmovq xmm1, rdi
vpbroadcastq ymm1, xmm1
vpblendd ymm0, ymm0, ymm1, 192 # ymm0 = ymm0[0,1,2,3,4,5],ymm1[6,7]
# 192 = 0xC0 = 0b11000000
ret
merge2(long long __vector(4), long):
vmovq xmm1, rdi
vinserti128 ymm1, ymm0, xmm1, 1 # Runs on more ports than vbroadcast on AMD Ryzen
# But it introduced a dependency on v (ymm0) before the blend for no reason, for the low half of ymm1. Could have used xmm1, xmm1.
vpblendd ymm0, ymm0, ymm1, 48 # ymm0 = ymm0[0,1,2,3],ymm1[4,5],ymm0[6,7]
ret
merge1(long long __vector(4), long):
vmovq xmm1, rdi
vpbroadcastq xmm1, xmm1 # only an *XMM* broadcast, 1c latency instead of 3.
vpblendd ymm0, ymm0, ymm1, 12 # ymm0 = ymm0[0,1],ymm1[2,3],ymm0[4,5,6,7]
ret
merge0(long long __vector(4), long):
vmovq xmm1, rdi
# broadcast optimized away, newval is already in the low element
vpblendd ymm0, ymm0, ymm1, 3 # ymm0 = ymm1[0,1],ymm0[2,3,4,5,6,7]
ret
其他编译器盲目地广播到完整的 YMM 然后混合,即使 elem=0 也是如此。 您可以专门化模板,或添加 if()
模板中的条件将被优化掉。例如splat = (elem?) set1() : v;
保存 elem==0 的广播。如果您愿意,您也可以捕获其他优化。
GCC 8.x 及更早版本使用通常不好的方式来广播整数:它们存储/重新加载。这可以避免使用任何 ALU shuffle 端口,因为广播负载在 Intel CPU 上是免费的,但它会在从整数到最终 vector 结果的链中引入存储转发延迟。
这已在 gcc9 的当前主干中修复,但我不知道是否有解决方法可以使用早期的 gcc 获得非愚蠢的代码生成。通常-march=<an intel uarch>
对于整数 -> vector ,有利于 ALU 而不是存储/重新加载,反之亦然,但在这种情况下,成本模型仍然选择存储/重新加载 -march=haswell
.
# gcc8.2 -O3 -march=haswell
merge0(long long __vector(4), long):
push rbp
mov rbp, rsp
and rsp, -32 # align the stack even though no YMM is spilled/loaded
mov QWORD PTR [rsp-8], rdi
vpbroadcastq ymm1, QWORD PTR [rsp-8] # 1 uop on Intel
vpblendd ymm0, ymm0, ymm1, 3
leave
ret
; GCC trunk: g++ (GCC-Explorer-Build) 9.0.0 20190103 (experimental)
; MSVC and ICC do this, too. (For MSVC, make sure to compile with -arch:AVX2)
merge0(long long __vector(4), long):
vmovq xmm2, rdi
vpbroadcastq ymm1, xmm2
vpblendd ymm0, ymm0, ymm1, 3
ret
对于运行时可变的元素位置,随机播放仍然有效,但您必须创建一个混合掩码 vector ,并在右侧元素中设置高位。例如与 vpmovsxbq
从 mask[3-elem]
加载在alignas(8) int8_t mask[] = { 0,0,0,-1,0,0,0 };
。但是vpblendvb
或vblendvpd
比立即混合慢,尤其是在 Haswell 上,因此如果可能的话请避免这种情况。
关于c++ - 将 int64_t 移动到 AVX2 __m256i vector 的高四字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54048226/