对于编译时常量移位计数,您可以获得相当好的结果。否则不是真的。
这只是您问题中r0 / r1 代码的 SSE 实现,因为没有其他明显的方法可以做到这一点。可变计数移位仅适用于向量元素内的移位,而不适用于整个寄存器的字节移位。因此,我们只需将低 64 位提升到高 64 位,并使用可变计数移位将它们放在正确的位置。
// untested
#include <immintrin.h>
/* some compilers might choke on slli / srli with non-compile-time-constant args
* gcc generates the xmm, imm8 form with constants,
* and generates the xmm, xmm form with otherwise. (With movd to get the count in an xmm)
*/
// doesn't optimize for the special-case where count%8 = 0
// could maybe do that in gcc with if(__builtin_constant_p(count)) { if (!count%8) return ...; }
__m128i mm_bitshift_left(__m128i x, unsigned count)
{
__m128i carry = _mm_bslli_si128(x, 8); // old compilers only have the confusingly named _mm_slli_si128 synonym
if (count >= 64)
return _mm_slli_epi64(carry, count-64); // the non-carry part is all zero, so return early
// else
carry = _mm_srli_epi64(carry, 64-count); // After bslli shifted left by 64b
x = _mm_slli_epi64(x, count);
return _mm_or_si128(x, carry);
}
__m128i mm_bitshift_left_3(__m128i x) { // by a specific constant, to see inlined constant version
return mm_bitshift_left(x, 3);
}
// by a specific constant, to see inlined constant version
__m128i mm_bitshift_left_100(__m128i x) { return mm_bitshift_left(x, 100); }
我认为这会不如实际方便。 _mm_slli_epi64 适用于 gcc/clang/icc,即使计数不是编译时常量(从整数 reg 生成 movd 到 xmm reg)。有一个_mm_sll_epi64 (__m128i a, __m128i count)(注意缺少i),但至少现在,i 内在函数可以生成psllq 的任何一种形式。
编译时常量计数版本相当高效,compiling to 4 instructions(或 5 个不带 AVX):
mm_bitshift_left_3(long long __vector(2)):
vpslldq xmm1, xmm0, 8
vpsrlq xmm1, xmm1, 61
vpsllq xmm0, xmm0, 3
vpor xmm0, xmm0, xmm1
ret
Performance:
这在 Intel SnB/IvB/Haswell 上具有 3 个周期延迟 (vpslldq(1) -> vpsrlq(1) -> vpor(1)),吞吐量限制为每 2 个周期一个(使端口上的向量移位单元饱和0)。字节移位在不同端口上的随机播放单元上运行。立即数向量移位都是单 uop 指令,因此当与其他代码混合时,这只是 4 个融合域 uop 占用了流水线空间。 (可变计数向量移位是 2 uop,2 个周期延迟,因此该函数的可变计数版本比从计数指令中看起来要差。)
或计数 >= 64:
mm_bitshift_left_100(long long __vector(2)):
vpslldq xmm0, xmm0, 8
vpsllq xmm0, xmm0, 36
ret
如果你的移位计数不是一个编译时常量,你必须在 count > 64 上进行分支,以确定是左移还是右移进位。我相信移位计数被解释为无符号整数,因此负计数是不可能的。
还需要额外的指令才能将int 计数和 64 计数放入向量寄存器。使用向量比较和混合指令以无分支方式执行此操作可能是可能的,但分支可能是个好主意。
GP 寄存器中__uint128_t 的可变计数版本看起来相当不错;优于 SSE 版本。 Clang does a slightly better job than gcc, emitting fewer mov instructions,但它仍然使用两个 cmov 指令来处理计数 >= 64 的情况。 (因为 x86 整数移位指令会屏蔽计数,而不是饱和。)
__uint128_t leftshift_int128(__uint128_t x, unsigned count) {
return x << count; // undefined if count >= 128
}