在D中如何64位mulhi
原文
Mulhi是一条可在任何64位CPU上有效地使用如下操作的指令:
ulong mulhi(ulong a, ulong b) {
return (ucent(a) * ucent(b)) >> 64;
}
在C++用__int128实现它.
这种运算,因为允许一次完成大量移动和加法,对快速哈希很好用.你可能会问,这不就是传统的乘法?不,它不是,因为它不会右移,高位不会影响低位.
LDC可用inline-(LLVM-)IR:
ulong mulhi(ulong a, ulong b)
{
import ldc.llvmasm;
return __ir!(`
%a = zext i64 %0 to i128
%b = zext i64 %1 to i128
%r = mul i128 %a, %b
%r2 = lshr i128 %r, 64
%r3 = trunc i128 %r2 to i64
ret i64 %r3`, ulong)(a, b);
}
为了完整,GDC的x86后端有内置函数:
import gcc.builtins;
ulong mulhi(ulong a, ulong b)
{
alias __m64 = __vector(short[4]);
__m64 res = __builtin_ia32_pmulhw(*cast(__m64*)&a, *cast(__m64*)&b);
return *cast(ulong*)&res;
}
__EOF__
_D3vec5mulhiFmmZm:
.cfi_startproc
movq %rdi, %xmm0
movq %rsi, %xmm1
pmulhw %xmm1, %xmm0
movq %xmm0, %rax
ret
.cfi_endproc
浙公网安备 33010602011771号