对于x64,我可以使用这个:
{ uint64_t hi,lo; // hi,lo = 64bit x 64bit multiply of c[0] and b[0] __asm__("mulq %3\n\t" : "=d" (hi),"=a" (lo) : "%a" (c[0]),"rm" (b[0]) : "cc" ); a[0] += hi; a[1] += lo; }
但我想以可移植的方式执行相同的计算.例如,在x86上工作.
解决方法
据我所知,你想要一个64位乘法的便携式纯C实现,输出为128位值,存储在两个64位值中.在这种情况下,这个
article声称拥有你需要的东西.该代码是为C编写的.将它变成C代码并不需要太多:
void mult64to128(uint64_t op1,uint64_t op2,uint64_t *hi,uint64_t *lo) { uint64_t u1 = (op1 & 0xffffffff); uint64_t v1 = (op2 & 0xffffffff); uint64_t t = (u1 * v1); uint64_t w3 = (t & 0xffffffff); uint64_t k = (t >> 32); op1 >>= 32; t = (op1 * v1) + k; k = (t & 0xffffffff); uint64_t w1 = (t >> 32); op2 >>= 32; t = (u1 * op2) + k; k = (t >> 32); *hi = (op1 * op2) + w1 + k; *lo = (t << 32) + w3; }