【发布时间】:2009-07-06 21:18:20
【问题描述】:
假设您有一个机器指令 udive,它通过(32 位被除数
// assume: a / b guaranteed not to overflow
a = 64bit dividend, a.h & a.l are hi & lo 32bits respectively
b = 32bit divisor
q1 = udive(a.h, b) // (a.h << 32) / b
r1 = -(q1 * b) // remainder of the above, shortcut since a.h & 0xffffffff == 0
q2 = a.l / b // a.l / b using regular unsigned division
r2 = a.l - (q2 * b) // remainder of the above
q = q1 + q2
r = r1 + r2
// r < r2, r overflowed and is >32bits, implies r > b since b is 32bits
// r >= b, quotient too small by 1, adjust
if (r < r2) or (r >= b)
q = q + 1
return q
但是,签署的案例给我带来了问题。假设一个等效的 sdive 指令执行 udive 的签名版本,我无法完全弄清楚如何处理余数等等。
【问题讨论】:
-
本能地这感觉像是一个二进制补码问题。由于我不是这方面的专家,我无法进一步建议您,但也许这是一个线索。
标签: integer precision division signed