Emulate 64-bit mulh instruction

lithium iodate whatdoiknow at doesntexist.net
Wed Mar 13 18:21:18 UTC 2019


On Wednesday, 13 March 2019 at 16:06:34 UTC, Kagamin wrote:
> Apparently this has no intrinsic, so wrote this code for x86 to 
> compute 128 bit product:

I cannot help you with your code directly, but I can propose an 
alternative:

import ldc.intrinsics;
pragma(LDC_inline_ir)
     R inlineIR(string s, R, P...)(P);

ulong[2] mul(ulong a, ulong b)
{
     ulong[2] result;
     inlineIR!(`
     %a = zext i64 %0 to i128
     %b = zext i64 %1 to i128
     %c = mul i128 %a, %b
     %d = bitcast [2 x i64]* %2 to i128*
     store i128 %c, i128* %d
     ret void`, void)(a, b, &result);
     return result;
}

This is optimized down to mul.



More information about the digitalmars-d-ldc mailing list