Algorithm Search⌘K

Proofs: Brent-Kung adder

These are the human proofs of Brent-Kung adder, at full length.

The Brent-Kung adder computes the sum of its inputs full

This proof shows: correctness, termination.

Let n≥1n \ge 1, ai,bi,cin∈{0,1}a_i, b_i, c_{\text{in}} \in \{0, 1\}, gi=ai∧big_i = a_i \wedge b_i and pi=ai⊕bip_i = a_i \oplus b_i. Define the true carries c0∗=cinc^{\ast}_0 = c_{\text{in}} and ci+1∗=gi∨pici∗c^{\ast}_{i+1} = g_i \vee p_i c^{\ast}_i. As in a full adder, ai+bi+ci∗=(pi⊕ci∗)+2ci+1∗a_i + b_i + c^{\ast}_i = (p_i \oplus c^{\ast}_i) + 2c^{\ast}_{i+1}, so the bits pi⊕ci∗p_i \oplus c^{\ast}_i for i<ni < n and cn∗c^{\ast}_n represent a+b+cina + b + c_{\text{in}}. It suffices to show that G[i]=ci+1∗G[i] = c^{\ast}_{i+1} for each ii at line L11.

Termination. The loop L4 to L6 doubles dd until d≥nd \ge n, and the loop L8 to L10 halves dd until it is 0. Each level updates finitely many positions.

Block pairs. Let g0′=g0∨p0cing'_0 = g_0 \vee p_0 c_{\text{in}} (line L2) and gi′=gig'_i = g_i for i≥1i \ge 1. For k≤ik \le i define (Gi:k,Pi:k)(G_{i:k}, P_{i:k}) by (Gi:i,Pi:i)=(gi′,pi)(G_{i:i}, P_{i:i}) = (g'_i, p_i) and Gi:k=gi′∨piGi−1:kG_{i:k} = g'_i \vee p_i G_{i-1:k}, Pi:k=piPi−1:kP_{i:k} = p_i P_{i-1:k} for k<ik < i.

Lemma 1. For k≤j<ik \le j < i, (Gi:k,Pi:k)=(Gi:j+1,Pi:j+1)∘(Gj:k,Pj:k)(G_{i:k}, P_{i:k}) = (G_{i:j+1}, P_{i:j+1}) \circ (G_{j:k}, P_{j:k}).

Proof. By induction on i−ji - j. If j=i−1j = i - 1, it is the definition. Otherwise, Gi:k=gi′∨piGi−1:kG_{i:k} = g'_i \vee p_i G_{i-1:k}, and by induction Gi−1:k=Gi−1:j+1∨Pi−1:j+1Gj:kG_{i-1:k} = G_{i-1:j+1} \vee P_{i-1:j+1} G_{j:k}; substituting gives Gi:k=(gi′∨piGi−1:j+1)∨piPi−1:j+1Gj:k=Gi:j+1∨Pi:j+1Gj:kG_{i:k} = (g'_i \vee p_i G_{i-1:j+1}) \vee p_i P_{i-1:j+1} G_{j:k} = G_{i:j+1} \vee P_{i:j+1} G_{j:k}. The PP part is a product of the same pp's. □\square

Lemma 2. Gi:0=ci+1∗G_{i:0} = c^{\ast}_{i+1} for each ii.

Proof. By induction on ii. G0:0=g0∨p0cin=c1∗G_{0:0} = g_0 \vee p_0 c_{\text{in}} = c^{\ast}_1, and Gi:0=gi∨piGi−1:0=gi∨pici∗=ci+1∗G_{i:0} = g_i \vee p_i G_{i-1:0} = g_i \vee p_i c^{\ast}_i = c^{\ast}_{i+1}. □\square

Say that position ii holds k..ik..i when (G[i],P[i])=(Gi:k,Pi:k)(G[i], P[i]) = (G_{i:k}, P_{i:k}), ignoring P[i]P[i] when k=0k = 0, since no later cell reads it: a cell reads P[i−d]P[i - d] only to compute a PP, and it computes PP only when the block of i−di - d does not start at 0. After line L3, position ii holds i..ii..i. By Lemma 1, a cell at position ii with distance dd that finds ii holding j+1..ij + 1..i and i−d=ji - d = j holding k..jk..j leaves ii holding k..ik..i.

For m≥1m \ge 1 write ν(m)\nu(m) for the exponent of the largest power of 2 dividing mm.

Lemma 3 (up-sweep). After the up-sweep level with distance d=2td = 2^t, each position ii holds the block of length 2min⁡(ν(i+1),t+1)2^{\min(\nu(i+1), t+1)} ending at ii.

Proof. By induction on tt; before the first level the claim holds with "t=−1t = -1", lengths 20=12^0 = 1. Level d=2td = 2^t updates the positions with 2t+1∣i+12^{t+1} \mid i + 1. For such ii, by induction ii holds the block of length 2t2^t ending at ii, and i−di - d, with ν(i−d+1)=t\nu(i - d + 1) = t, holds the block of length 2t2^t ending at i−di - d, which is adjacent. So ii now holds the block of length 2t+12^{t+1}. Positions with ν(i+1)≤t\nu(i + 1) \le t are not updated and already hold blocks of length 2ν(i+1)2^{\nu(i+1)}. □\square

Since the up-sweep runs up to the first d≥nd \ge n, and 2ν(i+1)≤i+1≤n2^{\nu(i+1)} \le i + 1 \le n, after it each position ii holds the block of length 2ν(i+1)2^{\nu(i+1)} ending at ii, that is, i+1−2ν(i+1)+1..ii + 1 - 2^{\nu(i+1)} + 1 .. i. If i+1i + 1 is a power of 2, this is 0..i0..i.

Lemma 4 (down-sweep). After the down-sweep level with distance dd, each position i<ni < n with 2ν(i+1)≥d2^{\nu(i+1)} \ge d holds 0..i0..i.

Proof. Line L7 sets dd to 2L−22^{L-2} where 2L2^L is the first power of 2 that is at least nn, or to 0. Positions with 2ν(i+1)≥2L−12^{\nu(i+1)} \ge 2^{L-1} have i+1∈{2L−1,2L}i + 1 \in \{2^{L-1}, 2^L\} (any larger multiple would exceed n≤2Ln \le 2^L), so they are powers of 2 and hold 0..i0..i after the up-sweep. This is the base. At level dd, let ii satisfy ν(i+1)=log⁡2d\nu(i + 1) = \log_2 d exactly. If i+1=di + 1 = d, it is a power of 2 and is done. Otherwise i+1=(2m+1)di + 1 = (2m + 1)d with m≥1m \ge 1, so i≥3d−1i \ge 3d - 1 and i≡d−1(mod2d)i \equiv d - 1 \pmod{2d}, and line L9 updates ii. Position ii holds the block of length dd ending at ii, which starts at i−d+1i - d + 1. Position i−di - d has i−d+1=2mdi - d + 1 = 2md, so 2ν(i−d+1)≥2d2^{\nu(i - d + 1)} \ge 2d, and by the induction hypothesis (the previous level, or the base) it holds 0..i−d0..i - d. The cell gives ii the block 0..i0..i. Positions with ν(i+1)>log⁡2d\nu(i+1) > \log_2 d are not updated at this level and hold 0..i0..i by induction. No other position is updated, since line L9 updates only ii with ν(i+1)=log⁡2d\nu(i + 1) = \log_2 d. □\square

Theorem. BRENT-KUNG-ADD returns the bits of a+b+cina + b + c_{\text{in}}.

Proof. After the level d=1d = 1, or directly after the up-sweep when the down-sweep is empty (n≤2n \le 2, where every i+1≤2i + 1 \le 2 is a power of 2), every position ii holds 0..i0..i by Lemma 4 or Lemma 3. So G[i]=Gi:0=ci+1∗G[i] = G_{i:0} = c^{\ast}_{i+1} by Lemma 2, line L11 sets c[i]=ci∗c[i] = c^{\ast}_i, and line L12 computes si=pi⊕ci∗s_i = p_i \oplus c^{\ast}_i. By the identity at the start, the output is a+b+cina + b + c_{\text{in}}. ■\blacksquare

Back to Brent-Kung adder