One step of binary long division by b: given the current quotient/remainder pair qr and the
next bit bit of the dividend, shift the partial quotient/remainder left by one (i.e., multiply by
2), add bit to the remainder, and conditionally subtract b to keep the remainder smaller
than b.
Instances For
Recursive driver of binary long division: starting from the most significant bit m of a,
folds longDivStep over k+1 iterations consuming successive bits from m down to m - k - 1.
The result is the quotient/remainder pair after processing those bits.
Instances For
Single-step invariant for longDivStep: if the incoming remainder r is < b, then the
output pair (q', r') satisfies q' * b + r' = 2 * (q * b + r) + bit and r' < b.
Loop invariant for longDivLoop: after processing k+1 bits, the result pair (q, r) satisfies
q * b + r = a / 2^(m - k) and r < b. This expresses that the loop computes the correct partial
quotient/remainder for the top k+1 bits of a.