Algorithm Search⌘K

Proofs: Carry-save addition

These are the human proofs of Carry-save addition, at full length.

Carry-save addition preserves the sum full

This proof shows: correctness, termination.

Fix a base β≥2\beta \ge 2 and n≥1n \ge 1. A digit vector d=(d0,…,dn−1)d = (d_0, \ldots, d_{n-1}) stands for ⟨d⟩=∑i=0n−1diβi\langle d \rangle = \sum_{i=0}^{n-1} d_i \beta^i; the digits of cc below are allowed to be any nonnegative integers in this formula.

Termination. CSA runs one pass over nn positions. CARRY-SAVE-SUM runs m−2m - 2 calls of CSA and one addition. The tree form strictly decreases the number of operands at each level while there are more than 2, since a group of three becomes two.

Lemma 1 (one step). Let x,y,zx, y, z have digits in [0,β)[0, \beta) and ⟨x⟩+⟨y⟩+⟨z⟩<βn\langle x \rangle + \langle y \rangle + \langle z \rangle < \beta^n. Then CSA returns ss and cc with ⟨s⟩+⟨c⟩=⟨x⟩+⟨y⟩+⟨z⟩\langle s \rangle + \langle c \rangle = \langle x \rangle + \langle y \rangle + \langle z \rangle, all si∈[0,β)s_i \in [0, \beta), c0=0c_0 = 0, and ci≤2c_i \le 2, with ci≤1c_i \le 1 when β=2\beta = 2.

Proof. At position ii, line L1 forms ti=xi+yi+zit_i = x_i + y_i + z_i with 0≤ti≤3(β−1)0 \le t_i \le 3(\beta - 1), and line L2 writes ti=si+βqit_i = s_i + \beta q_i with si=ti mod β∈[0,β)s_i = t_i \bmod \beta \in [0, \beta) and qi=⌊ti/β⌋q_i = \lfloor t_i / \beta \rfloor. Since 3(β−1)<3β3(\beta - 1) < 3\beta, qi≤2q_i \le 2; for β=2\beta = 2, ti≤3t_i \le 3 gives qi≤1q_i \le 1. Line L2 stores ci+1=qic_{i+1} = q_i for i<n−1i < n - 1 and line L3 sets c0=0c_0 = 0. Then

∑i=0n−1tiβi=∑i=0n−1siβi+∑i=0n−2qiβi+1+qn−1βn=⟨s⟩+⟨c⟩+qn−1βn.\sum_{i=0}^{n-1} t_i \beta^i = \sum_{i=0}^{n-1} s_i \beta^i + \sum_{i=0}^{n-2} q_i \beta^{i+1} + q_{n-1} \beta^n = \langle s \rangle + \langle c \rangle + q_{n-1}\beta^n .

The left side is ⟨x⟩+⟨y⟩+⟨z⟩<βn\langle x \rangle + \langle y \rangle + \langle z \rangle < \beta^n, and the right side is at least qn−1βnq_{n-1} \beta^n, so qn−1=0q_{n-1} = 0, and the identity is the claim. □\square

Lemma 2 (the outputs are valid inputs). The digits of cc lie in [0,β)[0, \beta).

Proof. For β=2\beta = 2, ci≤1<2c_i \le 1 < 2. For β≥3\beta \ge 3, ci≤2<βc_i \le 2 < \beta. □\square

Theorem (many operands). Let x1,…,xmx_1, \ldots, x_m, m≥2m \ge 2, have digits in [0,β)[0, \beta) and ∑j⟨xj⟩<βn\sum_j \langle x_j \rangle < \beta^n. Then CARRY-SAVE-SUM returns ∑j⟨xj⟩\sum_j \langle x_j \rangle.

Proof. We show by induction on j≥2j \ge 2 that after the step for jj (line L5 for j=2j = 2, line L6 for j≥3j \ge 3), the vectors SS and CC have digits in [0,β)[0, \beta) and ⟨S⟩+⟨C⟩=∑k=1j⟨xk⟩\langle S \rangle + \langle C \rangle = \sum_{k=1}^{j} \langle x_k \rangle. For j=2j = 2 this is the input. For the step to jj, the three inputs SS, CC and xjx_j have digits in [0,β)[0, \beta) by the induction hypothesis, and their values add up to ∑k≤j⟨xk⟩≤∑k≤m⟨xk⟩<βn\sum_{k \le j} \langle x_k \rangle \le \sum_{k \le m} \langle x_k \rangle < \beta^n. Lemma 1 gives the sum identity and Lemma 2 the digit bounds. After j=mj = m, line L7 adds SS and CC by an ordinary addition, which returns ⟨S⟩+⟨C⟩=∑k=1m⟨xk⟩\langle S \rangle + \langle C \rangle = \sum_{k=1}^{m} \langle x_k \rangle; it fits in nn digits since this is less than βn\beta^n. ■\blacksquare

The tree form. Each level applies Lemma 1 to disjoint groups of three operands and keeps the others. By Lemmas 1 and 2, the multiset of operands after the level has digits in [0,β)[0, \beta) and the same total, which is below βn\beta^n, so the hypothesis of Lemma 1 holds for every group of the next level. When at most two operands remain, the final addition returns the total. ■\blacksquare

Back to Carry-save addition