Division - Zeda/z80float GitHub Wiki
This is incomplete. It is mostly to remind me how my own algorithm works :P
Suppose we want to find N/(Ax+B)
. It may be easier instead to compute N/(Ax)
.
If we have the quotient, Q
, and remainder, r
of N/(Ax)
, then we rewrite it as N=Q(Ax)+b
. Then
N/(Ax+B) = Q(Ax)/(Ax+B)+r/(Ax+B)
= Q(Ax+B-B)/(Ax+B)+r/(Ax+B)
= Q+(r-Q*B)/(Ax+B)
I use this to build a divide-and-conquer division. As well, I take advantage of this in smaller divisions, too. This allows me to adjust a division so that the top bit of the denominator is 0, ensuring that intermediate results don't overflow 16 or 8 bits. It removes an otherwise necessary overflow check. In such cases, x=2
, so B
is either 0 or 1, so Q*B
is pretty easy ;)