Algorithm Search⌘K

Proofs: Carry-select adder

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

The carry-select 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\}, and block sizes k0,…,kt−1≥1k_0, \ldots, k_{t-1} \ge 1 with ∑jkj=n\sum_j k_j = n. Block jj covers the positions ℓj..uj\ell_j .. u_j with ℓ0=0\ell_0 = 0, uj=ℓj+kj−1u_j = \ell_j + k_j - 1 and ℓj+1=uj+1\ell_{j+1} = u_j + 1. Write Aj=∑i=ℓjujai2i−ℓjA_j = \sum_{i=\ell_j}^{u_j} a_i 2^{i - \ell_j} and BjB_j likewise.

Termination. Every loop runs over a finite list of blocks or positions, once.

Ripple steps. For a block and a carry-in c∈{0,1}c \in \{0, 1\}, the ripple of lines L2 and L4 sets cℓ=cc_{\ell} = c, si=pi⊕cis_i = p_i \oplus c_i and ci+1=gi∨(pi∧ci)c_{i+1} = g_i \vee (p_i \wedge c_i) for i=ℓ..ui = \ell..u, with gi=ai∧big_i = a_i \wedge b_i and pi=ai⊕bip_i = a_i \oplus b_i. The simplified first steps with a constant carry compute the same values: with c=0c = 0, s=ps = p and cℓ+1=gc_{\ell+1} = g; with c=1c = 1, s=p⊕1s = p \oplus 1 and cℓ+1=g∨pc_{\ell+1} = g \vee p.

Lemma 1. The ripple through block jj with carry-in cc returns bits sℓj,…,sujs_{\ell_j}, \ldots, s_{u_j} and a carry out CC with ∑isi2i−ℓj+C 2kj=Aj+Bj+c\sum_i s_i 2^{i - \ell_j} + C\, 2^{k_j} = A_j + B_j + c.

Proof. For each position, ai+bi+ci=si+2ci+1a_i + b_i + c_i = s_i + 2c_{i+1}: if pi=1p_i = 1 then ai+bi=1a_i + b_i = 1 and gi=0g_i = 0, so both sides are 1+ci1 + c_i; if pi=0p_i = 0 then ai=bi=gia_i = b_i = g_i, si=cis_i = c_i and ci+1=gic_{i+1} = g_i, so both sides are 2gi+ci2g_i + c_i. Multiply by 2i−ℓj2^{i - \ell_j} and sum over the block; the terms cic_i for ℓj<i≤uj\ell_j < i \le u_j cancel, which leaves the claim with C=cuj+1C = c_{u_j + 1}. □\square

Lemma 2 (monotone carries). In line L4, C0[j]≤C1[j]C^0[j] \le C^1[j].

Proof. By Lemma 1, Cc[j]=⌊(Aj+Bj+c)/2kj⌋C^c[j] = \lfloor (A_j + B_j + c) / 2^{k_j} \rfloor, which does not decrease when cc goes from 0 to 1. □\square

Lemma 3 (selection). If x∈{0,1}x \in \{0, 1\}, then si0⊕(x∧(si0⊕si1))=sixs^0_i \oplus (x \wedge (s^0_i \oplus s^1_i)) = s^x_i and C0[j]∨(C1[j]∧x)=Cx[j]C^0[j] \vee (C^1[j] \wedge x) = C^x[j].

Proof. For x=0x = 0, the first expression is si0s^0_i and the second is C0[j]C^0[j]. For x=1x = 1, the first is si0⊕si0⊕si1=si1s^0_i \oplus s^0_i \oplus s^1_i = s^1_i, and the second is C0[j]∨C1[j]=C1[j]C^0[j] \vee C^1[j] = C^1[j] by Lemma 2. □\square

Lemma 4. Let cj∗c^{\ast}_j be the true carry into block jj, that is, c0∗=cinc^{\ast}_0 = c_{\text{in}} and cj+1∗=⌊(Aj+Bj+cj∗)/2kj⌋c^{\ast}_{j+1} = \lfloor (A_j + B_j + c^{\ast}_j) / 2^{k_j} \rfloor. Then the value xx of line L6 at step jj is cj∗c^{\ast}_j, and the value CC after line L8 at step jj is cj+1∗c^{\ast}_{j+1}.

Proof. By induction on jj. Before the loop, CC is the carry out of block 0 with carry-in cinc_{\text{in}}, which is c1∗c^{\ast}_1 by Lemma 1. At step jj, line L6 sets x=C=cj∗x = C = c^{\ast}_j by the induction hypothesis, and line L8 sets C=Cx[j]C = C^x[j] by Lemma 3, which is ⌊(Aj+Bj+cj∗)/2kj⌋=cj+1∗\lfloor (A_j + B_j + c^{\ast}_j) / 2^{k_j} \rfloor = c^{\ast}_{j+1} by Lemma 1. □\square

Theorem. CARRY-SELECT-ADD returns the n+1n + 1 bits of a+b+cina + b + c_{\text{in}}.

Proof. By Lemmas 1, 3 and 4, the bits that the algorithm writes for block jj are the low kjk_j bits of Aj+Bj+cj∗A_j + B_j + c^{\ast}_j, so with SjS_j their value, Sj+cj+1∗2kj=Aj+Bj+cj∗S_j + c^{\ast}_{j+1} 2^{k_j} = A_j + B_j + c^{\ast}_j. Multiply by 2ℓj2^{\ell_j} and sum over jj. Since ℓj+kj=ℓj+1\ell_j + k_j = \ell_{j+1}, the carry terms telescope, and

∑jSj2ℓj+ct∗2n=∑j(Aj+Bj)2ℓj+c0∗=a+b+cin.\sum_j S_j 2^{\ell_j} + c^{\ast}_t 2^n = \sum_j (A_j + B_j) 2^{\ell_j} + c^{\ast}_0 = a + b + c_{\text{in}} .

The returned carry is C=ct∗C = c^{\ast}_t by Lemma 4 (or by Lemma 1 when t=1t = 1), so the output represents a+b+cina + b + c_{\text{in}}. ■\blacksquare

Back to Carry-select adder