【发布时间】:2012-04-22 23:10:28
【问题描述】:
我无法弄清楚非恢复整数除法的后更正。出于某种原因,我不断收到一些不需要更正或在需要时不更正的情况
这里是算法的伪代码。 Dividend 是 16 位,其他是 8 位。 Dividend_Sign,Remainder_Sign 我的意思是它们的 MSB 是 1,所以它们是 2 的补码的负数。
LoopCounter = 8;
do {
Shift Dividend Left with 0 in LSB;
if (Dividend_Sign XOR Divisor_Sign) {
Shift 0 into Quotient;
DividendHighByte = DividendHighByte + Divisor;
} else {
shift 1 into Quotient;
DividendHighByte = DividendHighByte - Divisor; // subtraction by 2's complement
}
} while (loopCounter != 0);
Remainder = DividendHighByte;
// here i do the Quotient conversion
invert MSB; // shifted out anyway. Probably should be used for overflow check, not important atm.
shift 1 into Quotient;
现在我基本上有了正确的答案,它只需要以一种或另一种方式进行后校正......或者根本不进行后校正。我不确定所有更正案例是什么。现在我有一半的时间不能工作,但无论如何都在这里:
if (Dividend_Sign XOR Remainder_sign) { // diff signs so correct
if (Remainder_Sign XOR Divisor_Sign) { // diff signs just add
Remainder = Remainder + Divisor;
Quotient = Quotient - 1;
} else {
Remainder = Remainder - Divisor;
Quotient = Quotient + 1;
}
}
http://en.wikipedia.org/wiki/Division_%28digital%29
http://www.acsel-lab.com/arithmetic/papers/ARITH17/ARITH17_Takagi.pdf
【问题讨论】:
-
您能否详细说明,或者至少列举一下不起作用的情况?
-
atm found -10:2 和 -10:-2 是正确的预校正,校正会搞砸。 -16:4 需要更正,但不需要。我认为这与他们的余数为 0 有关。
-
请说明修正完成后剩余部分的要求(暂时忽略修正是如何完成的)。
-
嗯,我需要它是余数的正确二进制表示。不知道你的要求是什么意思是正确的 - 商 * 除数 + 余数 = 股息。当余数的符号与最初的股息不同时,我会更正。 -10:2 产生 11111011 商,0000000 余数预校正。 11111100 Q,11111110 余数校正后。在这种情况下,我不需要更正,但无论如何算法都需要它。
-
产生不正确的结果。应该更正,但不是因为余数和股息都是负数。我不禁再次注意到,余数为0的循环。
标签: algorithm math binary integer-division divider