How do I do a 64 bits mulhi in D?

Iain Buclaw ibuclaw at gdcproject.org
Sun Nov 27 19:20:16 UTC 2022


On Sunday, 27 November 2022 at 03:26:30 UTC, kinke wrote:
> On Sunday, 27 November 2022 at 01:55:45 UTC, deadalnix wrote:
>> That's a clever trick, but that only work on x86, but also 
>> will blind the optimizer as to what's going on.
>
> A solution with LDC, using inline-(LLVM-)IR:
>

For completeness sake, the X86 backend has built-in functions 
with GDC (LDC might have the same too).
```
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
```


More information about the Digitalmars-d mailing list