我无法弄清楚非恢复整数除法的后更正。出于某种原因,我不断收到不需要更正或在需要时不更正的情况
这是算法的伪代码。Dividend
是 16 位,其他是 8 位。,我的意思是它们的 MSB 是 1 Dividend_Sign
,Remainder_Sign
所以它们是 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