我想不出非恢复整数除法的后修正由于某些原因,我不断收到一些不需要更正或不需要更正的案例
这是算法的伪码。Dividend
为16位,其他为8位。我的意思是它们的msb是1,所以它们的补体是负的。
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
最佳答案
算法有效,问题是2s补码有负零。如果最后余数为0,则无需进行任何更正。但该算法必须检测循环内的0余数,如果遇到余数,则必须进行修正。
只是添加了一个0余数标志并执行了以下操作:
if (!Remainder.isEmpty() && (zeroFlag || (Dividend.Sign() XOR Remainder.Sign())))
...do corrections