我试图将两个32位数a和b相乘,这应该得到64位结果。 a和b是无符号32位整数,我想出了这个:
r = a * b
r = ((ah << 16) + al) * ((bh << 16) + bl)
= ((ah * 2^16) + al) * ((bh * 2^16) + bl)
= (ah * 2^16) * (bh * 2^16) + (ah * 2^16) * bl + al * (bh * 2^16) + al * bl
= (ah * bh * 2^32) + (ah * bl * 2^16) + (al * bh * 2^16) + (al * bl)
= ((ah * bh) << 32) + ((ah * bl) << 16) + ((al * bh) << 16) + (al * bl)
= ((ah * bh) << 32) + ((ah * bl + al * bh) << 16) + (al * bl)
然后我将其翻译成c如下
static void _mul64(unsigned int a, unsigned int b, unsigned int *hi, unsigned int *lo) {
unsigned int ah = (a >> 16), al = a & 0xffff,
bh = (b >> 16), bl = b & 0xffff,
rh = (ah * bh), rl = (al * bl),
rm1 = ah * bl, rm2 = al * bh,
rm1h = rm1 >> 16, rm2h = rm2 >> 16,
rm1l = rm1 & 0xffff, rm2l = rm2 & 0xffff,
rmh = rm1h + rm2h, rml = rm1l + rm2l;
rl = rl + (rml << 16);
rh = rh + rmh;
if(rml & 0xffff0000)
rh = rh + 1;
*lo = rl;
*hi = rh;
}
然而,当我运行这个小的测试,它将a = 0xFFFFFFFF与b = 0xFFFFFFFF相乘并且应该产生0xFFFFFFFE00000001时,我得到的是0xFFFFFFFD00000001。我做错了吗?
int main(int argc, char **argv) {
unsigned int a, b, rl, rh;
unsigned long long r;
unsigned long long r1, r2, r3;
a = 0xffffffff;
b = 0xffffffff;
mul64(a, b, &rh, &rl);
r1 = ((unsigned long long) rh << 32) + rl;
r2 = (unsigned long long) a * b;
_mul64(a, b, &rh, &rl);
r3 = ((unsigned long long) rh << 32) + rl;
printf("a = 0x%08x, b = 0x%08x\n", (unsigned) a, (unsigned) b);
printf("_mul64: 0x%16llx\n", (unsigned long long) r3);
printf("a * b = 0x%16llx\n", (unsigned long long) r2);
return 0;
}
答案 0 :(得分:1)
您在此处添加了16位数量
rm1l = rm1 & 0xffff, rm2l = rm2 & 0xffff,
rmh = rm1h + rm2h, rml = rm1l + rm2l;
并将rml
向左移动16位到rl
,
rl = rl + (rml << 16);
当两个16位量的总和变为17位数时,丢弃进位。
此外,后一个和可能超过32位范围,在这种情况下,您将丢失另一个进位。
答案 1 :(得分:0)
在初始化器中完成所有算法后,调试将变得困难。将所有这些计算移出初始化程序,然后在禁用优化的情况下编译代码。在调试器中逐步执行它,并确保每个步骤都生成您希望生成的值。当您在遵循手动解决的算法的同时浏览代码时,应该很容易发现代码和算法偏离的任何位置。