Algorithm Search⌘K

Proofs: Ripple-carry adder

These are the human proofs of Ripple-carry adder, at full length.

The ripple-carry adder computes the exact sum full

This proof shows: correctness, termination.

Let n≥0n \ge 0, let a0,…,an−1a_0, \ldots, a_{n-1} and b0,…,bn−1b_0, \ldots, b_{n-1} be bits, and let c0c_0 be a bit. Write A=∑j<naj2jA = \sum_{j<n} a_j 2^j and B=∑j<nbj2jB = \sum_{j<n} b_j 2^j.

Theorem. RIPPLE-ADD(a,b,c0)(a, b, c_0) terminates and returns bits s0,…,sns_0, \ldots, s_n with ∑j=0nsj2j=A+B+c0\sum_{j=0}^{n} s_j 2^j = A + B + c_0.

Proof. The loop of line L2 has nn rounds, so the procedure terminates; as a circuit, it is a finite acyclic network of 5n5n gates, since each carry ci+1c_{i+1} depends only on inputs and on cic_i.

Lemma (full adder). For bits x,y,zx, y, z, let p=x⊕yp = x \oplus y, g=x∧yg = x \wedge y, s=p⊕zs = p \oplus z and c′=g∨(p∧z)c' = g \vee (p \wedge z). Then x+y+z=s+2c′x + y + z = s + 2c', and s,c′s, c' are bits.

Proof of the lemma. There are eight cases; group them by k=x+yk = x + y. If k=0k = 0, then p=g=0p = g = 0, s=zs = z and c′=0c' = 0, and x+y+z=z=sx + y + z = z = s. If k=1k = 1, then p=1p = 1, g=0g = 0, s=1−zs = 1 - z and c′=zc' = z, and s+2c′=1−z+2z=1+zs + 2c' = 1 - z + 2z = 1 + z. If k=2k = 2, then p=0p = 0, g=1g = 1, s=zs = z and c′=1c' = 1, and s+2c′=z+2s + 2c' = z + 2. In each case s+2c′=k+zs + 2c' = k + z. □\square

Invariant. Let cic_i be the value of cc before round ii, so c0c_0 is set by line L1. We show by induction on ii, for 0≤i≤n0 \le i \le n, that cic_i is a bit and

∑j=0i−1sj2j+ci2i=∑j=0i−1(aj+bj)2j+c0.\sum_{j=0}^{i-1} s_j 2^j + c_i 2^i = \sum_{j=0}^{i-1} (a_j + b_j) 2^j + c_0 .

For i=0i = 0 both sides are c0c_0. Assume it for i<ni < n. Round ii computes, by lines L3 to L6, sis_i and ci+1c_{i+1} as in the lemma with x=aix = a_i, y=biy = b_i, z=ciz = c_i, so ai+bi+ci=si+2ci+1a_i + b_i + c_i = s_i + 2c_{i+1}, and ci+1c_{i+1} is a bit. Adding (ai+bi)2i(a_i + b_i) 2^i to both sides of the invariant for ii gives

∑j=0i−1sj2j+(ai+bi+ci)2i=∑j=0i−1sj2j+si2i+ci+12i+1=∑j=0i(aj+bj)2j+c0,\sum_{j=0}^{i-1} s_j 2^j + (a_i + b_i + c_i) 2^i = \sum_{j=0}^{i-1} s_j 2^j + s_i 2^i + c_{i+1} 2^{i+1} = \sum_{j=0}^{i} (a_j + b_j) 2^j + c_0 ,

which is the invariant for i+1i + 1.

Result. For i=ni = n the invariant reads ∑j<nsj2j+cn2n=A+B+c0\sum_{j<n} s_j 2^j + c_n 2^n = A + B + c_0. Line L7 sets sn=cns_n = c_n, so ∑j=0nsj2j=A+B+c0\sum_{j=0}^{n} s_j 2^j = A + B + c_0. Since A+B+c0≤2(2n−1)+1<2n+1A + B + c_0 \le 2(2^n - 1) + 1 < 2^{n+1}, the n+1n + 1 bits hold the sum, and each sjs_j is a bit by the lemma.

Radix 2w2^w. The same argument works for limbs in [0,2w−1][0, 2^w - 1] with t=ai+bi+ci≤2w+1−1t = a_i + b_i + c_i \le 2^{w+1} - 1, si=t mod 2ws_i = t \bmod 2^w and ci+1=⌊t/2w⌋∈{0,1}c_{i+1} = \lfloor t / 2^w \rfloor \in \{0, 1\}, by division with remainder: t=si+2wci+1t = s_i + 2^w c_{i+1}, and the invariant holds with 2j2^j replaced by 2wj2^{wj}.

Two's complement overflow. Let n≥1n \ge 1 and let A′A', B′B' be the signed values, so A′=A−an−12nA' = A - a_{n-1}2^n and likewise for B′B'. The exact signed sum (with c0=0c_0 = 0) is A′+B′=∑j<n−1sj2j+cn−12n−1+(an−1+bn−1)2n−1−(an−1+bn−1)2nA' + B' = \sum_{j<n-1} s_j 2^j + c_{n-1}2^{n-1} + (a_{n-1} + b_{n-1})2^{n-1} - (a_{n-1} + b_{n-1})2^n, by the invariant for i=n−1i = n - 1. With an−1+bn−1+cn−1=sn−1+2cna_{n-1} + b_{n-1} + c_{n-1} = s_{n-1} + 2c_n this equals S′+(cn−1−cn)2nS' + (c_{n-1} - c_n) 2^n, where S′=∑j<n−1sj2j−sn−12n−1S' = \sum_{j<n-1} s_j 2^j - s_{n-1} 2^{n-1} is the signed value of the nn-bit result, in [−2n−1,2n−1−1][-2^{n-1}, 2^{n-1} - 1]. So the exact sum equals the stored result, and is in range, if and only if cn−1=cnc_{n-1} = c_n: when cn−1≠cnc_{n-1} \ne c_n the two differ by ±2n\pm 2^n, and the exact sum lies outside the range. ■\blacksquare

Back to Ripple-carry adder