在我打开bug之前,我想检查一下这里发生了什么。
我把这个C代码移植到Rust:
unsigned __int128 r = (unsigned __int128)a * (unsigned __int128)b;
很容易(我想):
let r = (a as u128) * (b as u128);
现在有了这个输入参数,我在C和Rust中获得了不同的乘法结果:
(0x56eaa5f5f650a9e3 as u128) * (0xa0cf24341e75bda9 as u128)
Rust和C的结果不同:
Rust: 0x3698fbc09d2c5b15e8889b1b676bbddb
C: 0x3698fbc0f417010bded944fe676bbddb
^^^^^^^^^^^^^^^^
我cross-checked the result,得到了与C代码相同的结果。
我错过了什么吗?
===添加了上下文信息:
来自xmr-stak(https://github.com/fireice-uk/xmr-stak)的这个函数thas表现不同:
static inline uint64_t _umul128(uint64_t a, uint64_t b, uint64_t* hi)
{
unsigned __int128 r = (unsigned __int128)a * (unsigned __int128)b;
*hi = r >> 64;
return (uint64_t)r;
}
无论C实现是否错误,我都必须在Rust中重新创建精确的计算,因为这是哈希计算所需要的。
我看起来你必须用两种语言写错字:
>>> hex(0x3698fbc09d2c5b15e8889b1b676bbddb//0x56eaa5f5f650a9e3)
'0xa0cf24341e75bda9' # what your Rust code uses
>>> hex(0x3698fbc0f417010bded944fe676bbddb//0x56eaa5f5f650a9e3)
'0xa0cf24351e75bda9' # what your online calculator uses
^
关于0x100000000错误的经典案例:)