The Brent-Kung adder computes the sum of its inputs full
This proof shows: correctness, termination.
Let n≥1, ai,bi,cin∈{0,1}, gi=ai∧bi and pi=ai⊕bi. Define the true carries c0∗=cin and ci+1∗=gi∨pici∗. As in a full adder, ai+bi+ci∗=(pi⊕ci∗)+2ci+1∗, so the bits pi⊕ci∗ for i<n and cn∗ represent a+b+cin. It suffices to show that G[i]=ci+1∗ for each i at line L11.
Termination. The loop L4 to L6 doubles d until d≥n, and the loop L8 to L10 halves d until it is 0. Each level updates finitely many positions.
Block pairs. Let g0′=g0∨p0cin (line L2) and gi′=gi for i≥1. For k≤i define (Gi:k,Pi:k) by (Gi:i,Pi:i)=(gi′,pi) and Gi:k=gi′∨piGi−1:k, Pi:k=piPi−1:k for k<i.
Lemma 1. For k≤j<i, (Gi:k,Pi:k)=(Gi:j+1,Pi:j+1)∘(Gj:k,Pj:k).
Proof. By induction on i−j. If j=i−1, it is the definition. Otherwise, Gi:k=gi′∨piGi−1:k, and by induction Gi−1:k=Gi−1:j+1∨Pi−1:j+1Gj:k; substituting gives Gi:k=(gi′∨piGi−1:j+1)∨piPi−1:j+1Gj:k=Gi:j+1∨Pi:j+1Gj:k. The P part is a product of the same p's. □
Lemma 2. Gi:0=ci+1∗ for each i.
Proof. By induction on i. G0:0=g0∨p0cin=c1∗, and Gi:0=gi∨piGi−1:0=gi∨pici∗=ci+1∗. □
Say that position i holds k..i when (G[i],P[i])=(Gi:k,Pi:k), ignoring P[i] when k=0, since no later cell reads it: a cell reads P[i−d] only to compute a P, and it computes P only when the block of i−d does not start at 0. After line L3, position i holds i..i. By Lemma 1, a cell at position i with distance d that finds i holding j+1..i and i−d=j holding k..j leaves i holding k..i.
For m≥1 write ν(m) for the exponent of the largest power of 2 dividing m.
Lemma 3 (up-sweep). After the up-sweep level with distance d=2t, each position i holds the block of length 2min(ν(i+1),t+1) ending at i.
Proof. By induction on t; before the first level the claim holds with "t=−1", lengths 20=1. Level d=2t updates the positions with 2t+1∣i+1. For such i, by induction i holds the block of length 2t ending at i, and i−d, with ν(i−d+1)=t, holds the block of length 2t ending at i−d, which is adjacent. So i now holds the block of length 2t+1. Positions with ν(i+1)≤t are not updated and already hold blocks of length 2ν(i+1). □
Since the up-sweep runs up to the first d≥n, and 2ν(i+1)≤i+1≤n, after it each position i holds the block of length 2ν(i+1) ending at i, that is, i+1−2ν(i+1)+1..i. If i+1 is a power of 2, this is 0..i.
Lemma 4 (down-sweep). After the down-sweep level with distance d, each position i<n with 2ν(i+1)≥d holds 0..i.
Proof. Line L7 sets d to 2L−2 where 2L is the first power of 2 that is at least n, or to 0. Positions with 2ν(i+1)≥2L−1 have i+1∈{2L−1,2L} (any larger multiple would exceed n≤2L), so they are powers of 2 and hold 0..i after the up-sweep. This is the base. At level d, let i satisfy ν(i+1)=log2d exactly. If i+1=d, it is a power of 2 and is done. Otherwise i+1=(2m+1)d with m≥1, so i≥3d−1 and i≡d−1(mod2d), and line L9 updates i. Position i holds the block of length d ending at i, which starts at i−d+1. Position i−d has i−d+1=2md, so 2ν(i−d+1)≥2d, and by the induction hypothesis (the previous level, or the base) it holds 0..i−d. The cell gives i the block 0..i. Positions with ν(i+1)>log2d are not updated at this level and hold 0..i by induction. No other position is updated, since line L9 updates only i with ν(i+1)=log2d. □
Theorem. BRENT-KUNG-ADD returns the bits of a+b+cin.
Proof. After the level d=1, or directly after the up-sweep when the down-sweep is empty (n≤2, where every i+1≤2 is a power of 2), every position i holds 0..i by Lemma 4 or Lemma 3. So G[i]=Gi:0=ci+1∗ by Lemma 2, line L11 sets c[i]=ci∗, and line L12 computes si=pi⊕ci∗. By the identity at the start, the output is a+b+cin. ■