Algorithm Search⌘K

Proofs: Carry-lookahead adder

These are the human proofs of Carry-lookahead adder, at full length.

The carry-lookahead adder computes the sum of its inputs full

This proof shows: correctness, termination.

Let n≥1n \ge 1, r≥2r \ge 2, and let ai,bi,c0∈{0,1}a_i, b_i, c_0 \in \{0, 1\}. Define the true carries by c0∗=c0c^{\ast}_0 = c_0 and ci+1∗=gi∨(pi∧ci∗)c^{\ast}_{i+1} = g_i \vee (p_i \wedge c^{\ast}_i) with gi=ai∧big_i = a_i \wedge b_i and pi=ai⊕bip_i = a_i \oplus b_i (line L1). Then ai+bi+ci∗=(ai⊕bi⊕ci∗)+2ci+1∗a_i + b_i + c^{\ast}_i = (a_i \oplus b_i \oplus c^{\ast}_i) + 2 c^{\ast}_{i+1} for each ii: if pi=1p_i = 1 the left side is 1+ci∗1 + c^{\ast}_i, and if pi=0p_i = 0 it is 2gi+ci∗2g_i + c^{\ast}_i. Multiplying by 2i2^i and summing gives

a+b+c0=∑i=0n−1(pi⊕ci∗) 2i+cn∗ 2n,a + b + c_0 = \sum_{i=0}^{n-1} (p_i \oplus c^{\ast}_i)\, 2^i + c^{\ast}_n\, 2^n ,

so it suffices to show that leaf ii receives the carry-in ci∗c^{\ast}_i and that line L10 gives cn∗c^{\ast}_n.

Termination. Each pass of the loop L3 to L6 replaces a level of t≥1t \ge 1 nodes by ⌈t/r⌉\lceil t / r \rceil nodes. If t≥2t \ge 2 this is less than tt, and if t=1t = 1 the pass makes one group, and the loop stops. So the loop runs h=max⁡(1,⌈log⁡rn⌉)h = \max(1, \lceil \log_r n \rceil) times. The downward pass visits each of the finitely many nodes once.

Notation. Each node vv covers an interval of positions [ℓv,uv][\ell_v, u_v]: a leaf ii covers [i,i][i, i], and a group covers the union of the intervals of its children, which are consecutive and listed from low to high, because line L4 cuts each level into runs of consecutive nodes and the leaves are in order.

Lemma 1 (unrolling). Let (x0,y0),…,(xm−1,ym−1)(x_0, y_0), \ldots, (x_{m-1}, y_{m-1}) and bits d0,…,dmd_0, \ldots, d_m satisfy dj+1=xj∨(yj∧dj)d_{j+1} = x_j \vee (y_j \wedge d_j) for 0≤j<m0 \le j < m. Then for each kk with 1≤k≤m1 \le k \le m,

dk=xk−1∨yk−1xk−2∨⋯∨yk−1⋯y1x0∨yk−1⋯y0 d0.d_k = x_{k-1} \vee y_{k-1} x_{k-2} \vee \cdots \vee y_{k-1} \cdots y_1 x_0 \vee y_{k-1} \cdots y_0\, d_0 .

Proof. By induction on kk. For k=1k = 1 it is the hypothesis. If it holds for kk, then dk+1=xk∨ykdkd_{k+1} = x_k \vee y_k d_k; substituting the formula for dkd_k and distributing yky_k over the OR gives the formula for k+1k + 1. □\square

Lemma 2 (upward invariant). For each node vv that has signals (Gv,Pv)(G_v, P_v) (a leaf, or a group built in line L5), cuv+1∗=Gv∨(Pv∧cℓv∗)c^{\ast}_{u_v + 1} = G_v \vee (P_v \wedge c^{\ast}_{\ell_v}).

Proof. By induction on the level. For a leaf ii, (Gv,Pv)=(gi,pi)(G_v, P_v) = (g_i, p_i) and the claim is the definition of ci+1∗c^{\ast}_{i+1}. For a group with one child, the signals are the child's and the interval is the same. For a group vv with children w0,…,wm−1w_0, \ldots, w_{m-1}, m≥2m \ge 2, set xj=Gwjx_j = G_{w_j}, yj=Pwjy_j = P_{w_j} and dj=cℓwj∗d_j = c^{\ast}_{\ell_{w_j}} for j<mj < m, and dm=cuv+1∗d_m = c^{\ast}_{u_v + 1}. Since the intervals are consecutive, uwj+1=ℓwj+1u_{w_j} + 1 = \ell_{w_{j+1}}, so the induction hypothesis for the children says dj+1=xj∨yjdjd_{j+1} = x_j \vee y_j d_j. Lemma 1 with k=mk = m gives dm=Gv∨Pvd0d_m = G_v \vee P_v d_0 where GvG_v and PvP_v are exactly the formulas of line L5, and d0=cℓv∗d_0 = c^{\ast}_{\ell_v}. □\square

Lemma 3 (downward invariant). Each node vv receives the carry-in cℓv∗c^{\ast}_{\ell_v}.

Proof. By induction from the root down. The root covers [0,n−1][0, n-1] and receives c0=c0∗c_0 = c^{\ast}_0 in line L7. Let vv be a group with carry-in cℓv∗c^{\ast}_{\ell_v} and children w0,…,wm−1w_0, \ldots, w_{m-1}, and define xj,yj,djx_j, y_j, d_j as in the proof of Lemma 2, where now each child has signals by Lemma 2 (children of any group are leaves or groups built in line L5, since only the root skips line L5). Child w0w_0 receives cℓv∗=d0c^{\ast}_{\ell_v} = d_0, and ℓw0=ℓv\ell_{w_0} = \ell_v. For 1≤k<m1 \le k < m, line L9 gives child wkw_k the value of the formula of Lemma 1 with d0=cℓv∗d_0 = c^{\ast}_{\ell_v}, which equals dk=cℓwk∗d_k = c^{\ast}_{\ell_{w_k}}. □\square

Theorem. CLA-ADD returns the sum bits and the carry out of a+b+c0a + b + c_0.

Proof. By Lemma 3, leaf ii receives ci∗c^{\ast}_i, so line L11 sets si=pi⊕ci∗s_i = p_i \oplus c^{\ast}_i. Line L10 applies the formula of Lemma 1 with k=mk = m to the root's children and d0=c0d_0 = c_0; by Lemma 2 for the children it equals dm=cu+1∗d_m = c^{\ast}_{u + 1} with u=n−1u = n - 1, so it is cn∗c^{\ast}_n. By the identity at the start, the outputs represent a+b+c0a + b + c_0. ■\blacksquare

Back to Carry-lookahead adder