Granlund-Montgomery and Möller-Granlund division are exact full
This proof shows: correctness, termination.
Let β=2w. 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<β and ℓ=⌈log2d⌉, so 2ℓ−1<d≤2ℓ for d≥2, and ℓ=0 for d=1.
Lemma 1. 0≤m<β, and M:=β+m=⌊2w+ℓ/d⌋+1.
Proof. β(2ℓ−d)/d+β=2w+ℓ/d, and adding the integer β commutes with the floor, which gives the formula for M. If d=2ℓ, then m=1. Otherwise d≥2ℓ−1+1 and ℓ≤w, so 1−(2ℓ−d)/d=(2d−2ℓ)/d≥2/d>21−w; hence β(2ℓ−d)/d<β−2 and m<β−1. □
Lemma 2. For 0≤n<β, ⌊n/d⌋=⌊Mn/2w+ℓ⌋.
Proof. Write Md=2w+ℓ+e. Since M−1=⌊2w+ℓ/d⌋, (M−1)d≤2w+ℓ<Md, so 1≤e≤d. Let n=qd+r with 0≤r<d. Then
2w+ℓMn=dn+d2w+ℓen=q+dr+d2w+ℓen. The last term is nonnegative and, since e≤d≤2ℓ and n<2w, it is less than 1/d. So the sum of the last two terms is in [0,(r+1)/d)⊆[0,1), and the floor is q. □
Theorem 1. DIVIDE returns (⌊n/d⌋,nmodd), and every intermediate value fits in a word.
Proof. Let t=⌊mn/β⌋ (line L4). Since m<β, t≤n. By nested floors,
⌊2w+ℓMn⌋=⌊2ℓ⌊(βn+mn)/β⌋⌋=⌊2ℓn+t⌋. If ℓ=0 then d=1, m=1, t=0, s1=s2=0, and line L5 gives q=n. If ℓ≥1, then s1=1, s2=ℓ−1, and (n+t)/2=t+(n−t)/2 with n−t≥0, so ⌊(n+t)/2⌋=t+⌊(n−t)/2⌋, and, by nested floors again, ⌊(n+t)/2ℓ⌋=⌊(t+⌊(n−t)/2⌋)/2ℓ−1⌋, which is line L5. By Lemma 2 the result is ⌊n/d⌋. The values n−t∈[0,n] and t+⌊(n−t)/2⌋≤n fit in a word, and so does n−qd∈[0,d) (line L6). ■
DIVIDE-2BY1
Let β/2≤d<β, u=u1β+u0 with 0≤u1<d and 0≤u0<β, so q∗=⌊u/d⌋<β. Let B=β+v=⌊(β2−1)/d⌋ and k=β2−Bd.
Lemma 3. 0≤v<β, 1≤k≤d, and the value Bu1+u0 of line L7 is less than β2.
Proof. Since d≤β−1, (β2−1)/d≥(β2−1)/(β−1)=β+1, so B≥β+1; and (β2−1)/d≤(β2−1)/(β/2)<2β, so B<2β. Hence 0≤v<β. From Bd≤β2−1<(B+1)d, 1≤k≤d. Finally Bu1+u0≤B(d−1)+β−1≤β2−1−B+β−1<β2, using Bd≤β2−1 and B≥β+1. □
So line L7 computes q1β+q0=Bu1+u0 with 0≤q1,q0<β. Let q~=q1+1 as an integer and r~=u−q~d.
Lemma 4. βr~=ku1+(β−d)u0+d(q0−β).
Proof. Multiply q1β+q0=Bu1+u0 by d and use Bd=β2−k: dq1β=(β2−k)u1+du0−dq0. Then βr~=β2u1+βu0−βdq1−βd=ku1+(β−d)u0+dq0−βd. □
Lemma 5. Let M′=max(β−d,q0). Then q0−β<r~<M′ and r~≥−d.
Proof. Upper bound: ku1≤d(d−1) and (β−d)u0≤(β−d)(β−1), so by Lemma 4, βr~≤d2−d+(β−d)(β−1)+dq0−βd=(β−d)2−β+dq0≤(β−d)M′+dM′−β=βM′−β, so r~≤M′−1. Lower bound: the first two terms of Lemma 4 are nonnegative, so βr~≥d(q0−β). Since q0−β<0 and 0<d<β, d(q0−β)>β(q0−β), so r~>q0−β; and d(q0−β)≥−dβ, so r~≥−d. □
Theorem 2. DIVIDE-2BY1 returns (q∗,u−q∗d).
Proof. Line L8 computes r=r~modβ, because u≡u0 and the reduction of q1 mod β does not change q~dmodβ. Since M′<β and M′−β=max(−d,q0−β), Lemma 5 places r~ in [M′−β,M′), an interval of length β. All arithmetic below is on true integers; the quotient is finally in [0,β), so its value mod β is the integer itself.
Case A: r~<0. Then r=r~+β>q0 by Lemma 5, so line L9 runs and gives the pair (q~−1,r~+d), where r~+d∈[0,d) by Lemma 5, and (r~+β)+d reduced mod β is r~+d. Line L10 does not run. Since u=(q~−1)d+(r~+d) with remainder in [0,d), the result is (q∗,umodd).
Case B: r~≥0 and r~≤q0. Then r=r~, line L9 does not run, and r~<M′≤max(β−d,β−1)<2d, since β≤2d. So at most one subtraction of d, line L10, brings it to [0,d).
Case C: r~≥0 and r~>q0. Then q0<r~<M′ forces M′=β−d, so r~<β−d≤d. Line L9 runs and gives (q~−1,r~+d) with d≤r~+d<β, so there is no wrap, and line L10 runs and restores (q~,r~), with r~∈[0,d).
In every case the returned pair (q,r) satisfies u=qd+r and 0≤r<d, so q=q∗. ■