Algorithm Search⌘K

Proofs: Kogge-Stone adder

These are the human proofs of Kogge-Stone adder, at full length.

The Kogge-Stone adder computes the sum full

This proof shows: correctness, termination.

Let a=∑i<nai2ia = \sum_{i<n} a_i2^i and b=∑i<nbi2ib = \sum_{i<n} b_i2^i with bits ai,bia_i, b_i, and let c0∈{0,1}c_0 \in \{0, 1\}. The carries of the addition are defined by the recurrence

ci+1=gi∨(pi∧ci),gi=ai∧bi,pi=ai⊕bi,c_{i+1} = g_i \vee (p_i \wedge c_i), \qquad g_i = a_i \wedge b_i, \quad p_i = a_i \oplus b_i ,

and the full adder identity ai+bi+ci=(pi⊕ci)+2ci+1a_i + b_i + c_i = (p_i \oplus c_i) + 2c_{i+1} gives, by induction on ii (as on the page of the ripple-carry adder), a+b+c0=∑i<n(pi⊕ci)2i+cn2na + b + c_0 = \sum_{i<n} (p_i \oplus c_i)2^i + c_n2^n. So it suffices to show that the network computes c1,…,cnc_1, \ldots, c_n.

Block signals. For 0≤k≤i<n0 \le k \le i < n define the block function Fi:k(x)F_{i:k}(x), the carry out of position ii when the carry into position kk is xx: Fk:k(x)=gk∨(pk∧x)F_{k:k}(x) = g_k \vee (p_k \wedge x) and Fi:k(x)=gi∨(pi∧Fi−1:k(x))F_{i:k}(x) = g_i \vee (p_i \wedge F_{i-1:k}(x)). Every such function has the form F(x)=G∨(P∧x)F(x) = G \vee (P \wedge x) for bits G,PG, P, since compositions of maps of this form keep the form:

G1∨(P1∧(G2∨(P2∧x)))=(G1∨(P1∧G2))∨((P1∧P2)∧x).G_1 \vee (P_1 \wedge (G_2 \vee (P_2 \wedge x))) = (G_1 \vee (P_1 \wedge G_2)) \vee ((P_1 \wedge P_2) \wedge x) .

Write (Gi:k,Pi:k)(G_{i:k}, P_{i:k}) for the pair of Fi:kF_{i:k}. The identity shows that, for k≤j<ik \le j < i,

(Gi:k,Pi:k)=(Gi:j+1,Pi:j+1)∘(Gj:k,Pj:k),(G,P)∘(G′,P′)=(G∨(P∧G′),P∧P′),(G_{i:k}, P_{i:k}) = (G_{i:j+1}, P_{i:j+1}) \circ (G_{j:k}, P_{j:k}), \qquad (G, P) \circ (G', P') = (G \vee (P \wedge G'), P \wedge P'),

because Fi:k=Fi:j+1∘Fj:kF_{i:k} = F_{i:j+1} \circ F_{j:k} as functions. Composition of functions is associative, so ∘\circ is associative on the pairs that arise.

Folding the carry in. Line L2 replaces g0g_0 by g0′=g0∨(p0∧c0)=c1g_0' = g_0 \vee (p_0 \wedge c_0) = c_1. With g0′g_0' in place of g0g_0, the block function of 0..i0..i satisfies Fi:0′(0)=Fi:0(c0)=ci+1F'_{i:0}(0) = F_{i:0}(c_0) = c_{i+1}, and its GG part is Gi:0′=Fi:0′(0)=ci+1G'_{i:0} = F'_{i:0}(0) = c_{i+1}. From now on g0g_0 means g0′g_0'.

Lemma. For t=0,1,…,Lt = 0, 1, \ldots, L with L=⌈log⁡2n⌉L = \lceil \log_2 n \rceil, after tt iterations of the loop of line L4 (so d=2td = 2^t), position ii holds (G[i],P[i])=(Gi:k,Pi:k)(G[i], P[i]) = (G_{i:k}, P_{i:k}) with k=max⁡(0,i−2t+1)k = \max(0, i - 2^t + 1).

Proof. By induction on tt. For t=0t = 0, line L3 sets (G[i],P[i])=(gi,pi)=(Gi:i,Pi:i)(G[i], P[i]) = (g_i, p_i) = (G_{i:i}, P_{i:i}), and max⁡(0,i)=i\max(0, i) = i.

Assume the claim after tt iterations, with d=2td = 2^t, and consider line L5, which uses the values of iteration tt for all positions. For i<di < d, position ii is not updated, and max⁡(0,i−2d+1)=0=max⁡(0,i−d+1)\max(0, i - 2d + 1) = 0 = \max(0, i - d + 1), so the claim holds. For i≥di \ge d, position ii holds the block i−d+1..ii - d + 1..i, since i−d+1≥1>0i - d + 1 \ge 1 > 0, and position i−di - d holds the block k′..i−dk'..i - d with k′=max⁡(0,i−2d+1)k' = \max(0, i - 2d + 1). These two blocks are adjacent, so their combination is the pair of the block k′..ik'..i, as claimed, with 2d=2t+12d = 2^{t+1}. □\square

Theorem. KOGGE-STONE-ADD terminates and returns the n+1n + 1 bits of a+b+c0a + b + c_0.

Proof. The variable dd doubles in each iteration, so the loop stops after L=⌈log⁡2n⌉L = \lceil \log_2 n \rceil iterations, with 2L≥n2^L \ge n. By the lemma with t=Lt = L, every position i≤n−1<2Li \le n - 1 < 2^L holds the pair of the block 0..i0..i, so G[i]=Gi:0′=ci+1G[i] = G'_{i:0} = c_{i+1}. Line L7 therefore sets the true carries c0,…,cnc_0, \ldots, c_n, and line L8 computes si=pi⊕cis_i = p_i \oplus c_i. By the identity at the start, ∑i<nsi2i+cn2n=a+b+c0\sum_{i<n} s_i2^i + c_n2^n = a + b + c_0. ■\blacksquare

Only GG is needed for short blocks. If i<2di < 2d, the block held by position ii after the update starts at 0, and only its GG part is used later (by line L7, or by position i+d′i + d' for larger d′d', which needs only GG from a block that starts at 0, since the combination (G,P)∘(G′,P′)(G, P) \circ (G', P') uses P′P' only in the PP part of the result). So the implementation computes the PP part only for i≥2di \ge 2d.

Back to Kogge-Stone adder