Algorithm Search⌘K

Proofs: Restoring division

These are the human proofs of Restoring division, at full length.

Restoring division computes the quotient and the remainder full

This proof shows: correctness, termination.

Let U≥0U \ge 0 have the binary digits um−1…u0u_{m-1} \ldots u_0, so U=∑i<mui2iU = \sum_{i<m} u_i 2^i, and let V≥1V \ge 1. For 0≤i≤m0 \le i \le m let Pi=⌊U/2i⌋=∑j≥iuj2j−iP_i = \lfloor U / 2^i \rfloor = \sum_{j \ge i} u_j 2^{j - i}. Then Pm=0P_m = 0, P0=UP_0 = U, and

Pi=2Pi+1+ui(0≤i<m).P_i = 2P_{i+1} + u_i \qquad (0 \le i < m).

Let Qi=∑j≥iqj2j−iQ_i = \sum_{j \ge i} q_j 2^{j-i} be the number formed by the quotient digits found so far, after the step for ii, and let RiR_i be the value of RR after that step. Put Qm=0Q_m = 0 and Rm=0R_m = 0, the values set by line L1.

Lemma (invariant). For i=m,m−1,…,0i = m, m - 1, \ldots, 0: Pi=QiV+RiP_i = Q_i V + R_i and 0≤Ri<V0 \le R_i < V.

Proof. By downward induction on ii. For i=mi = m: 0=0⋅V+00 = 0 \cdot V + 0 and 0≤0<V0 \le 0 < V, since V≥1V \ge 1.

Assume the claim for i+1i + 1, and consider the step for ii. Line L3 forms R′=2Ri+1+uiR' = 2R_{i+1} + u_i. From 0≤Ri+1≤V−10 \le R_{i+1} \le V - 1 and ui∈{0,1}u_i \in \{0, 1\},

0≤R′≤2V−1.0 \le R' \le 2V - 1 .

Also Pi=2Pi+1+ui=2Qi+1V+2Ri+1+ui=2Qi+1V+R′P_i = 2P_{i+1} + u_i = 2Q_{i+1}V + 2R_{i+1} + u_i = 2Q_{i+1}V + R'. Line L4 forms R′−VR' - V, which lies in [−V,V−1][-V, V - 1].

  • If R′−V≥0R' - V \ge 0, lines L5 and L7 set qi=1q_i = 1 and Ri=R′−VR_i = R' - V, so 0≤Ri≤V−10 \le R_i \le V - 1, and Qi=2Qi+1+1Q_i = 2Q_{i+1} + 1. Then QiV+Ri=2Qi+1V+V+R′−V=PiQ_iV + R_i = 2Q_{i+1}V + V + R' - V = P_i.
  • If R′−V<0R' - V < 0, line L6 adds VV back, so Ri=R′R_i = R' with 0≤R′<V0 \le R' < V, and qi=0q_i = 0, Qi=2Qi+1Q_i = 2Q_{i+1}. Then QiV+Ri=2Qi+1V+R′=PiQ_iV + R_i = 2Q_{i+1}V + R' = P_i.

In both cases the claim holds for ii. □\square

Theorem. RESTORING-DIV terminates after mm steps and returns Q=⌊U/V⌋Q = \lfloor U / V \rfloor and R=U−QVR = U - QV, with 0≤R<V0 \le R < V.

Proof. The loop of line L2 runs over the mm values i=m−1,…,0i = m - 1, \ldots, 0, and each step does a bounded amount of work, so it terminates. By the lemma for i=0i = 0, U=P0=Q0V+R0U = P_0 = Q_0V + R_0 with 0≤R0<V0 \le R_0 < V. By the uniqueness of division with remainder, Q0=⌊U/V⌋Q_0 = \lfloor U/V \rfloor and R0=U mod VR_0 = U \bmod V, and these are the returned values, since the digits qm−1,…,q0q_{m-1}, \ldots, q_0 are the binary digits of Q0Q_0.

The sign test in finite registers. The implementation holds RR in n+1n + 1 limbs of ww binary digits, where V<2nwV < 2^{nw}. By the bounds above, 0≤R′≤2V−1<2nw+1≤2(n+1)w0 \le R' \le 2V - 1 < 2^{nw + 1} \le 2^{(n+1)w}, so line L3 never overflows. The subtraction of line L4 is done modulo 2(n+1)w2^{(n+1)w}, and its borrow out is 1 exactly when R′<VR' < V, which is the test of line L5. In that case the addition of line L6, also modulo 2(n+1)w2^{(n+1)w}, gives (R′−V)+V≡R′(R' - V) + V \equiv R', and since 0≤R′<2(n+1)w0 \le R' < 2^{(n+1)w} the stored value is R′R'. So the register computation agrees with the integer one. ■\blacksquare

Back to Restoring division