Algorithm Search⌘K

Proofs: Non-restoring division

These are the human proofs of Non-restoring division, at full length.

Non-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 and let V≥1V \ge 1. Put Pi=⌊U/2i⌋P_i = \lfloor U / 2^i \rfloor, so Pm=0P_m = 0, P0=UP_0 = U and Pi=2Pi+1+uiP_i = 2P_{i+1} + u_i. Let Rm=0R_m = 0 be the value set by line L1, and RiR_i the value of RR after the step for ii. Define the restored value

Ti={Riif Ri≥0,Ri+Vif Ri<0.T_i = \begin{cases} R_i & \text{if } R_i \ge 0, \\ R_i + V & \text{if } R_i < 0. \end{cases}

Let Qi=∑j≥iqj2j−iQ_i = \sum_{j \ge i} q_j 2^{j - i} be the number formed by the quotient digits after the step for ii, with Qm=0Q_m = 0.

Lemma. For i=m,…,0i = m, \ldots, 0: −V≤Ri<V-V \le R_i < V, 0≤Ti<V0 \le T_i < V, and Pi=QiV+TiP_i = Q_iV + T_i. Moreover, for i<mi < m, Ri=2Ti+1+ui−VR_i = 2T_{i+1} + u_i - V.

Proof. By downward induction. For i=mi = m: Rm=Tm=0R_m = T_m = 0 and Pm=0=0⋅V+0P_m = 0 = 0 \cdot V + 0.

Assume the claims for i+1i + 1. First, Ri=2Ti+1+ui−VR_i = 2T_{i+1} + u_i - V:

  • if Ri+1≥0R_{i+1} \ge 0, line L3 gives Ri=2Ri+1+ui−V=2Ti+1+ui−VR_i = 2R_{i+1} + u_i - V = 2T_{i+1} + u_i - V;
  • if Ri+1<0R_{i+1} < 0, then Ri+1=Ti+1−VR_{i+1} = T_{i+1} - V, and line L4 gives Ri=2(Ti+1−V)+ui+V=2Ti+1+ui−VR_i = 2(T_{i+1} - V) + u_i + V = 2T_{i+1} + u_i - V.

Let S=2Ti+1+uiS = 2T_{i+1} + u_i. Since 0≤Ti+1≤V−10 \le T_{i+1} \le V - 1 and ui∈{0,1}u_i \in \{0, 1\}, 0≤S≤2V−10 \le S \le 2V - 1, so Ri=S−V∈[−V,V−1]R_i = S - V \in [-V, V - 1]. Also Pi=2Pi+1+ui=2Qi+1V+SP_i = 2P_{i+1} + u_i = 2Q_{i+1}V + S.

  • If Ri≥0R_i \ge 0: qi=1q_i = 1, Qi=2Qi+1+1Q_i = 2Q_{i+1} + 1 and Ti=Ri=S−V∈[0,V)T_i = R_i = S - V \in [0, V), so QiV+Ti=2Qi+1V+S=PiQ_iV + T_i = 2Q_{i+1}V + S = P_i.
  • If Ri<0R_i < 0: qi=0q_i = 0, Qi=2Qi+1Q_i = 2Q_{i+1} and Ti=Ri+V=S∈[0,V)T_i = R_i + V = S \in [0, V), so QiV+Ti=2Qi+1V+S=PiQ_iV + T_i = 2Q_{i+1}V + S = P_i.

So the claims hold for ii. □\square

Theorem. NON-RESTORING-DIV terminates after mm steps and one optional correction, and returns Q=⌊U/V⌋Q = \lfloor U/V \rfloor and R=U mod VR = U \bmod V.

Proof. The loop runs over mm values of ii with a bounded amount of work each, and line L6 runs at most once, so the algorithm terminates. Line L6 replaces R0R_0 by T0T_0. By the lemma for i=0i = 0, U=Q0V+T0U = Q_0V + T_0 with 0≤T0<V0 \le T_0 < V, so by the uniqueness of division with remainder Q0=⌊U/V⌋Q_0 = \lfloor U/V \rfloor and T0=U mod VT_0 = U \bmod V. The returned digits qm−1,…,q0q_{m-1}, \ldots, q_0 are the binary digits of Q0Q_0. □\square

The form with digits ±1\pm 1. Let di=1d_i = 1 if Ri+1≥0R_{i+1} \ge 0 and di=−1d_i = -1 otherwise, the operation chosen in step ii. Then Ri=2Ri+1+ui−diVR_i = 2R_{i+1} + u_i - d_iV in both lines L3 and L4. Multiplying by 2i2^i and summing over i=m−1,…,0i = m - 1, \ldots, 0, the terms 2i+1Ri+12^{i+1}R_{i+1} and 2iRi2^iR_i telescope, and with Rm=0R_m = 0,

R0=∑i<mui2i−V∑i<mdi2i=U−DV,D=∑i<mdi2i.R_0 = \sum_{i<m} u_i 2^i - V \sum_{i<m} d_i 2^i = U - DV, \qquad D = \sum_{i<m} d_i 2^i .

Since R0∈[−V,V)R_0 \in [-V, V), Q=DQ = D and R=R0R = R_0 if R0≥0R_0 \ge 0, and Q=D−1Q = D - 1 and R=R0+VR = R_0 + V if R0<0R_0 < 0. This is the textbook correction.

Register widths. The implementation holds RR in two's complement in n+1n + 1 limbs of w≥2w \ge 2 binary digits, with V<2nwV < 2^{nw}. The intermediate value 2R+ui2R + u_i lies in [−2V,2V)[-2V, 2V), and 2V≤2nw+1≤2(n+1)w−12V \le 2^{nw + 1} \le 2^{(n+1)w - 1} because w≥2w \ge 2, so every intermediate value lies in the range [−2(n+1)w−1,2(n+1)w−1)[-2^{(n+1)w - 1}, 2^{(n+1)w - 1}) of the register. Arithmetic modulo 2(n+1)w2^{(n+1)w} therefore represents each value exactly, and the top binary digit is its sign. ■\blacksquare

Back to Non-restoring division