Burnikel-Ziegler division returns the quotient and the remainder full
This proof shows: correctness, termination.
Let β=2w. A number X of k limbs is normalized if βk/2≤X<βk.
Termination. DIV2N1N on n limbs either stops at line L1 or calls DIV3N2N twice with h=n/2, and DIV3N2N calls DIV2N1N at most once with h limbs. So the size halves every two levels and the recursion has depth at most 2log2n. The loop of line L9 runs at most twice by Lemma 2. SCHOOLBOOK-DIVREM terminates (Knuth, Algorithm D).
Lemma 1 (trial quotient). Let b=βh, B=B1b+B2 with 0≤B2<b and b/2≤B1<b, and A=A1b2+A2b+A3 with 0≤A2,A3<b and A<bB. Let Q∗=⌊A/B⌋ and Q^=min(⌊(A1b+A2)/B1⌋,b−1). Then Q∗<b and Q∗≤Q^≤Q∗+2.
Proof. Q∗<b since A<bB. Q^≥Q∗: if Q^=b−1 this holds. Otherwise Q^=⌊(A1b+A2)/B1⌋, so A1b+A2−Q^B1≤B1−1, and then
A−Q^B≤A−Q^B1b=(A1b+A2−Q^B1)b+A3≤(B1−1)b+A3<B1b≤B, so A−(Q^+1)B<0 and Q∗≤Q^. Q^≤Q∗+2: if B2=0, then B=B1b and ⌊(A1b+A2)/B1⌋=⌊(A−A3)/B⌋≤Q∗, so Q^=Q∗. Otherwise B>B1b≥b, and B1b>B−b>0 since B<(B1+1)b. Suppose Q^≥Q∗+3. Since Q^≤(A1b+A2)/B1=(A−A3)/(B1b)≤A/(B1b)<A/(B−b) and Q∗>A/B−1,
3≤Q^−Q∗<B−bA−BA+1=BA⋅B−bb+1, so A/B>2(B−b)/b≥2(B1−1), using B≥B1b. As 2(B1−1) is an integer, Q∗=⌊A/B⌋≥2(B1−1). With Q^≤b−1 this gives 2(B1−1)≤Q∗≤Q^−3≤b−4, that is B1≤b/2−1, which contradicts B1≥b/2. □
(This is Knuth's Theorems A and B of Section 4.3.1, with base b=βh and a divisor of two b-digits.)
Lemma 2 (DIV3N2N). Under the hypotheses of Lemma 1, and provided the call of line L6 returns the correct quotient and remainder, DIV3N2N returns (Q∗,A−Q∗B), and line L9 runs at most twice.
Proof. Since A<bB, A1≤B1: otherwise A≥(B1+1)b2>bB. If A1<B1, then A1b+A2<B1b, which is the precondition of DIV2N1N with the normalized h-limb divisor B1, and line L6 returns Q=⌊(A1b+A2)/B1⌋≤b−1 and R1=A1b+A2−QB1. If A1=B1, then ⌊(A1b+A2)/B1⌋≥b, and line L7 sets Q=b−1 and R1 the same way. In both cases Q=Q^, and line L8 gives
R=R1b+A3−Q^B2=(A1b+A2−Q^B1)b+A3−Q^B2=A−Q^B. By Lemma 1, Q^−Q∗∈{0,1,2}, so R=(A−Q∗B)−(Q^−Q∗)B≥−2B, and R<B. Each pass of line L9 lowers Q by 1 and raises R by B, keeping R=A−QB, and it stops at the first Q with R≥0, which is Q∗, after Q^−Q∗≤2 passes. □
Theorem. If B is a normalized n-limb number and 0≤A<βnB, DIV2N1N returns (⌊A/B⌋,AmodB).
Proof. By strong induction on n. If line L1 applies, SCHOOLBOOK-DIVREM is correct (Knuth, Algorithm D, whose trial quotients are bounded by Lemma 1 with h=1). Otherwise let h=n/2, b=βh, and A=A1b3+A2b2+A3b+A4. The top half B1=⌊B/b⌋ is a normalized h-limb number, so the calls of DIV2N1N inside DIV3N2N are correct by the induction hypothesis.
First call (line L3). A′=⌊A/b⌋=A1b2+A2b+A3 satisfies A′≤A/b<bB. By Lemma 2 it returns Q1=⌊A′/B⌋ and R=A′−Q1B∈[0,B).
Second call (line L4). A′′=Rb+A4≤(B−1)b+b−1<bB, so Lemma 2 applies and returns Q2=⌊A′′/B⌋<b and S=A′′−Q2B∈[0,B).
Result. A=A′b+A4=(Q1B+R)b+A4=Q1bB+A′′=(Q1b+Q2)B+S with 0≤S<B, so by the uniqueness of division with remainder, Q1b+Q2=⌊A/B⌋ and S=AmodB; line L5 returns them. ■