Algorithm Search⌘K

Proofs: Granlund-Montgomery division

These are the human proofs of Granlund-Montgomery division, at full length.

Granlund-Montgomery and Möller-Granlund division are exact full

This proof shows: correctness, termination.

Let β=2w\beta = 2^w. Every line runs once, so the algorithms terminate. We prove that DIVIDE and DIVIDE-2BY1 return the quotient and the remainder.

DIVIDE

Let 1≤d<β1 \le d < \beta and ℓ=⌈log⁡2d⌉\ell = \lceil \log_2 d \rceil, so 2ℓ−1<d≤2ℓ2^{\ell - 1} < d \le 2^\ell for d≥2d \ge 2, and ℓ=0\ell = 0 for d=1d = 1.

Lemma 1. 0≤m<β0 \le m < \beta, and M:=β+m=⌊2w+ℓ/d⌋+1M := \beta + m = \lfloor 2^{w+\ell}/d \rfloor + 1.

Proof. β(2ℓ−d)/d+β=2w+ℓ/d\beta(2^\ell - d)/d + \beta = 2^{w+\ell}/d, and adding the integer β\beta commutes with the floor, which gives the formula for MM. If d=2ℓd = 2^\ell, then m=1m = 1. Otherwise d≥2ℓ−1+1d \ge 2^{\ell - 1} + 1 and ℓ≤w\ell \le w, so 1−(2ℓ−d)/d=(2d−2ℓ)/d≥2/d>21−w1 - (2^\ell - d)/d = (2d - 2^\ell)/d \ge 2/d > 2^{1-w}; hence β(2ℓ−d)/d<β−2\beta(2^\ell - d)/d < \beta - 2 and m<β−1m < \beta - 1. □\square

Lemma 2. For 0≤n<β0 \le n < \beta, ⌊n/d⌋=⌊Mn/2w+ℓ⌋\lfloor n/d \rfloor = \lfloor Mn / 2^{w+\ell} \rfloor.

Proof. Write Md=2w+ℓ+eMd = 2^{w+\ell} + e. Since M−1=⌊2w+ℓ/d⌋M - 1 = \lfloor 2^{w+\ell}/d \rfloor, (M−1)d≤2w+ℓ<Md(M - 1)d \le 2^{w+\ell} < Md, so 1≤e≤d1 \le e \le d. Let n=qd+rn = qd + r with 0≤r<d0 \le r < d. Then

Mn2w+ℓ=nd+end 2w+ℓ=q+rd+end 2w+ℓ.\frac{Mn}{2^{w+\ell}} = \frac{n}{d} + \frac{en}{d \, 2^{w+\ell}} = q + \frac{r}{d} + \frac{en}{d \, 2^{w+\ell}} .

The last term is nonnegative and, since e≤d≤2ℓe \le d \le 2^\ell and n<2wn < 2^w, it is less than 1/d1/d. So the sum of the last two terms is in [0,(r+1)/d)⊆[0,1)[0, (r + 1)/d) \subseteq [0, 1), and the floor is qq. □\square

Theorem 1. DIVIDE returns (⌊n/d⌋,n mod d)(\lfloor n/d \rfloor, n \bmod d), and every intermediate value fits in a word.

Proof. Let t=⌊mn/β⌋t = \lfloor mn/\beta \rfloor (line L4). Since m<βm < \beta, t≤nt \le n. By nested floors,

⌊Mn2w+ℓ⌋=⌊⌊(βn+mn)/β⌋2ℓ⌋=⌊n+t2ℓ⌋.\left\lfloor \frac{Mn}{2^{w+\ell}} \right\rfloor = \left\lfloor \frac{\lfloor (\beta n + mn)/\beta \rfloor}{2^\ell} \right\rfloor = \left\lfloor \frac{n + t}{2^\ell} \right\rfloor .

If ℓ=0\ell = 0 then d=1d = 1, m=1m = 1, t=0t = 0, s1=s2=0s_1 = s_2 = 0, and line L5 gives q=nq = n. If ℓ≥1\ell \ge 1, then s1=1s_1 = 1, s2=ℓ−1s_2 = \ell - 1, and (n+t)/2=t+(n−t)/2(n + t)/2 = t + (n - t)/2 with n−t≥0n - t \ge 0, so ⌊(n+t)/2⌋=t+⌊(n−t)/2⌋\lfloor (n + t)/2 \rfloor = t + \lfloor (n - t)/2 \rfloor, and, by nested floors again, ⌊(n+t)/2ℓ⌋=⌊(t+⌊(n−t)/2⌋)/2ℓ−1⌋\lfloor (n + t)/2^\ell \rfloor = \lfloor (t + \lfloor (n - t)/2 \rfloor)/2^{\ell - 1} \rfloor, which is line L5. By Lemma 2 the result is ⌊n/d⌋\lfloor n/d \rfloor. The values n−t∈[0,n]n - t \in [0, n] and t+⌊(n−t)/2⌋≤nt + \lfloor (n - t)/2 \rfloor \le n fit in a word, and so does n−qd∈[0,d)n - qd \in [0, d) (line L6). ■\blacksquare

DIVIDE-2BY1

Let β/2≤d<β\beta/2 \le d < \beta, u=u1β+u0u = u_1 \beta + u_0 with 0≤u1<d0 \le u_1 < d and 0≤u0<β0 \le u_0 < \beta, so q∗=⌊u/d⌋<βq^* = \lfloor u/d \rfloor < \beta. Let B=β+v=⌊(β2−1)/d⌋B = \beta + v = \lfloor (\beta^2 - 1)/d \rfloor and k=β2−Bdk = \beta^2 - Bd.

Lemma 3. 0≤v<β0 \le v < \beta, 1≤k≤d1 \le k \le d, and the value Bu1+u0Bu_1 + u_0 of line L7 is less than β2\beta^2.

Proof. Since d≤β−1d \le \beta - 1, (β2−1)/d≥(β2−1)/(β−1)=β+1(\beta^2 - 1)/d \ge (\beta^2 - 1)/(\beta - 1) = \beta + 1, so B≥β+1B \ge \beta + 1; and (β2−1)/d≤(β2−1)/(β/2)<2β(\beta^2 - 1)/d \le (\beta^2 - 1)/(\beta/2) < 2\beta, so B<2βB < 2\beta. Hence 0≤v<β0 \le v < \beta. From Bd≤β2−1<(B+1)dBd \le \beta^2 - 1 < (B + 1)d, 1≤k≤d1 \le k \le d. Finally Bu1+u0≤B(d−1)+β−1≤β2−1−B+β−1<β2Bu_1 + u_0 \le B(d - 1) + \beta - 1 \le \beta^2 - 1 - B + \beta - 1 < \beta^2, using Bd≤β2−1Bd \le \beta^2 - 1 and B≥β+1B \ge \beta + 1. □\square

So line L7 computes q1β+q0=Bu1+u0q_1 \beta + q_0 = Bu_1 + u_0 with 0≤q1,q0<β0 \le q_1, q_0 < \beta. Let q~=q1+1\tilde q = q_1 + 1 as an integer and r~=u−q~d\tilde r = u - \tilde q d.

Lemma 4. βr~=ku1+(β−d)u0+d(q0−β)\beta \tilde r = k u_1 + (\beta - d) u_0 + d(q_0 - \beta).

Proof. Multiply q1β+q0=Bu1+u0q_1\beta + q_0 = Bu_1 + u_0 by dd and use Bd=β2−kBd = \beta^2 - k: dq1β=(β2−k)u1+du0−dq0d q_1 \beta = (\beta^2 - k)u_1 + d u_0 - d q_0. Then βr~=β2u1+βu0−βdq1−βd=ku1+(β−d)u0+dq0−βd\beta \tilde r = \beta^2 u_1 + \beta u_0 - \beta d q_1 - \beta d = k u_1 + (\beta - d) u_0 + d q_0 - \beta d. □\square

Lemma 5. Let M′=max⁡(β−d,q0)M' = \max(\beta - d, q_0). Then q0−β<r~<M′q_0 - \beta < \tilde r < M' and r~≥−d\tilde r \ge -d.

Proof. Upper bound: ku1≤d(d−1)k u_1 \le d(d - 1) and (β−d)u0≤(β−d)(β−1)(\beta - d)u_0 \le (\beta - d)(\beta - 1), so by Lemma 4, βr~≤d2−d+(β−d)(β−1)+dq0−βd=(β−d)2−β+dq0≤(β−d)M′+dM′−β=βM′−β\beta \tilde r \le d^2 - d + (\beta - d)(\beta - 1) + dq_0 - \beta d = (\beta - d)^2 - \beta + d q_0 \le (\beta - d)M' + dM' - \beta = \beta M' - \beta, so r~≤M′−1\tilde r \le M' - 1. Lower bound: the first two terms of Lemma 4 are nonnegative, so βr~≥d(q0−β)\beta \tilde r \ge d(q_0 - \beta). Since q0−β<0q_0 - \beta < 0 and 0<d<β0 < d < \beta, d(q0−β)>β(q0−β)d(q_0 - \beta) > \beta(q_0 - \beta), so r~>q0−β\tilde r > q_0 - \beta; and d(q0−β)≥−dβd(q_0 - \beta) \ge -d\beta, so r~≥−d\tilde r \ge -d. □\square

Theorem 2. DIVIDE-2BY1 returns (q∗,u−q∗d)(q^*, u - q^* d).

Proof. Line L8 computes r=r~ mod βr = \tilde r \bmod \beta, because u≡u0u \equiv u_0 and the reduction of q1q_1 mod β\beta does not change q~d mod β\tilde q d \bmod \beta. Since M′<βM' < \beta and M′−β=max⁡(−d,q0−β)M' - \beta = \max(-d, q_0 - \beta), Lemma 5 places r~\tilde r in [M′−β,M′)[M' - \beta, M'), an interval of length β\beta. All arithmetic below is on true integers; the quotient is finally in [0,β)[0, \beta), so its value mod β\beta is the integer itself.

Case A: r~<0\tilde r < 0. Then r=r~+β>q0r = \tilde r + \beta > q_0 by Lemma 5, so line L9 runs and gives the pair (q~−1,r~+d)(\tilde q - 1, \tilde r + d), where r~+d∈[0,d)\tilde r + d \in [0, d) by Lemma 5, and (r~+β)+d(\tilde r + \beta) + d reduced mod β\beta is r~+d\tilde r + d. Line L10 does not run. Since u=(q~−1)d+(r~+d)u = (\tilde q - 1)d + (\tilde r + d) with remainder in [0,d)[0, d), the result is (q∗,u mod d)(q^*, u \bmod d).

Case B: r~≥0\tilde r \ge 0 and r~≤q0\tilde r \le q_0. Then r=r~r = \tilde r, line L9 does not run, and r~<M′≤max⁡(β−d,β−1)<2d\tilde r < M' \le \max(\beta - d, \beta - 1) < 2d, since β≤2d\beta \le 2d. So at most one subtraction of dd, line L10, brings it to [0,d)[0, d).

Case C: r~≥0\tilde r \ge 0 and r~>q0\tilde r > q_0. Then q0<r~<M′q_0 < \tilde r < M' forces M′=β−dM' = \beta - d, so r~<β−d≤d\tilde r < \beta - d \le d. Line L9 runs and gives (q~−1,r~+d)(\tilde q - 1, \tilde r + d) with d≤r~+d<βd \le \tilde r + d < \beta, so there is no wrap, and line L10 runs and restores (q~,r~)(\tilde q, \tilde r), with r~∈[0,d)\tilde r \in [0, d).

In every case the returned pair (q,r)(q, r) satisfies u=qd+ru = qd + r and 0≤r<d0 \le r < d, so q=q∗q = q^*. ■\blacksquare

Back to Granlund-Montgomery division