Algorithm Search⌘K

Proofs: Array multiplier

These are the human proofs of Array multiplier, at full length.

The array multiplier computes the product of its inputs full

This proof shows: correctness.

Let n≥1n \ge 1, a=∑i=0n−1ai2ia = \sum_{i=0}^{n-1} a_i 2^i and b=∑j=0n−1bj2jb = \sum_{j=0}^{n-1} b_j 2^j with all ai,bj∈{0,1}a_i, b_j \in \{0, 1\}. Line L1 sets pp[j][i]=aibjpp[j][i] = a_i b_j, so ∑ipp[j][i] 2i=a bj\sum_i pp[j][i]\, 2^i = a\, b_j. A full adder satisfies x+y+z=s+2cx + y + z = s + 2c with s,c∈{0,1}s, c \in \{0, 1\}, and we read s[n]=0s[n] = 0 in line L5.

For j=0,…,n−1j = 0, \ldots, n-1 let s(j)s^{(j)} and c(j)c^{(j)} be the vectors ss and cc after row jj (after line L2 for j=0j = 0, after line L6 for j≥1j \ge 1), and define

Pj=∑k=0j−1pk2k,Vj=∑i=0n−1s(j)[i] 2j+i+∑i=0n−1c(j)[i] 2j+i+1.P_j = \sum_{k=0}^{j-1} p_k 2^k, \qquad V_j = \sum_{i=0}^{n-1} s^{(j)}[i]\, 2^{j+i} + \sum_{i=0}^{n-1} c^{(j)}[i]\, 2^{j+i+1} .

Lemma. For each jj, (a) Pj+Vj=a⋅(b mod 2j+1)P_j + V_j = a \cdot (b \bmod 2^{j+1}), and (b) c(j)[n−1]=0c^{(j)}[n-1] = 0.

Proof. By induction on jj.

Base, j=0j = 0. P0=0P_0 = 0, s(0)=pp[0]s^{(0)} = pp[0] and c(0)=0c^{(0)} = 0, so V0=ab0=a⋅(b mod 2)V_0 = a b_0 = a \cdot (b \bmod 2), and (b) holds.

Step, from j−1j - 1 to jj, for 1≤j≤n−11 \le j \le n-1. Assume (a) and (b) for j−1j - 1, and write s=s(j−1)s = s^{(j-1)}, c=c(j−1)c = c^{(j-1)}, s′=s(j)s' = s^{(j)}, c′=c(j)c' = c^{(j)}. Line L4 sets pj−1=s[0]p_{j-1} = s[0], so Pj=Pj−1+s[0] 2j−1P_j = P_{j-1} + s[0]\, 2^{j-1}. Line L5 gives, for each ii,

s′[i]+2c′[i]=pp[j][i]+s[i+1]+c[i].s'[i] + 2c'[i] = pp[j][i] + s[i+1] + c[i] .

Multiply by 2j+i2^{j+i} and sum over i=0,…,n−1i = 0, \ldots, n-1. The left side is VjV_j. On the right side, ∑ipp[j][i] 2j+i=abj2j\sum_i pp[j][i]\, 2^{j+i} = a b_j 2^j; ∑i=0n−1s[i+1] 2j+i=∑i=1n−1s[i] 2j−1+i\sum_{i=0}^{n-1} s[i+1]\, 2^{j+i} = \sum_{i=1}^{n-1} s[i]\, 2^{j-1+i} because s[n]=0s[n] = 0; and ∑ic[i] 2j+i\sum_i c[i]\, 2^{j+i} is the cc part of Vj−1V_{j-1}. So

Vj=abj2j+Vj−1−s[0] 2j−1.V_j = a b_j 2^j + V_{j-1} - s[0]\, 2^{j-1} .

Adding the formula for PjP_j gives Pj+Vj=Pj−1+Vj−1+abj2j=a⋅(b mod 2j)+abj2j=a⋅(b mod 2j+1)P_j + V_j = P_{j-1} + V_{j-1} + a b_j 2^j = a \cdot (b \bmod 2^j) + a b_j 2^j = a \cdot (b \bmod 2^{j+1}), which is (a). For (b), the top cell i=n−1i = n - 1 adds pp[j][n−1]+s[n]+c[n−1]=pp[j][n−1]+0+0≤1pp[j][n-1] + s[n] + c[n-1] = pp[j][n-1] + 0 + 0 \le 1 by the hypothesis (b), so its carry c′[n−1]c'[n-1] is 0. □\square

Theorem. The bits p0,…,p2n−1p_0, \ldots, p_{2n-1} computed by ARRAY-MULTIPLY satisfy ∑k=02n−1pk2k=ab\sum_{k=0}^{2n-1} p_k 2^k = ab.

Proof. Apply the lemma with j=n−1j = n - 1, and write s=s(n−1)s = s^{(n-1)}, c=c(n−1)c = c^{(n-1)}. Since b<2nb < 2^n,

Pn−1+∑i=0n−1s[i] 2n−1+i+∑i=0n−2c[i] 2n+i=ab,P_{n-1} + \sum_{i=0}^{n-1} s[i]\, 2^{n-1+i} + \sum_{i=0}^{n-2} c[i]\, 2^{n+i} = ab ,

where the term c[n−1]c[n-1] is dropped by (b). Line L7 sets pn−1=s[0]p_{n-1} = s[0], so the left side is Pn+2n(X+Y)P_n + 2^n (X + Y) with Pn=Pn−1+s[0] 2n−1P_n = P_{n-1} + s[0]\, 2^{n-1},

X=∑i=1n−1s[i] 2i−1,Y=∑i=1n−1c[i−1] 2i−1.X = \sum_{i=1}^{n-1} s[i]\, 2^{i-1}, \qquad Y = \sum_{i=1}^{n-1} c[i-1]\, 2^{i-1} .

Lines L8 to L11 are a ripple-carry adder for the (n−1)(n-1)-bit numbers XX and YY with carry-in 0: by induction on ii, after the step ii of line L10,

∑k=1ipn−1+k 2k−1+carry⋅2i=∑k=1i(s[k]+c[k−1]) 2k−1,\sum_{k=1}^{i} p_{n-1+k}\, 2^{k-1} + \textit{carry} \cdot 2^{i} = \sum_{k=1}^{i} (s[k] + c[k-1])\, 2^{k-1},

since the step adds (s[i]+c[i−1])2i−1(s[i] + c[i-1]) 2^{i-1} to the right side and replaces carry⋅2i−1\textit{carry} \cdot 2^{i-1} on the left by pn−1+i 2i−1+carry′⋅2ip_{n-1+i}\, 2^{i-1} + \textit{carry}' \cdot 2^{i}, which has the same value by the full adder equation. After i=n−1i = n - 1 and line L11, ∑k=1npn−1+k 2k−1=X+Y\sum_{k=1}^{n} p_{n-1+k}\, 2^{k-1} = X + Y. Hence

∑k=02n−1pk2k=Pn+2n(X+Y)=ab.■\sum_{k=0}^{2n-1} p_k 2^k = P_n + 2^n (X + Y) = ab . \qquad \blacksquare

Cell counts. By (b), the top cell of each row has at most one live input. In row 1, c(0)=0c^{(0)} = 0, so cells 0,…,n−20, \ldots, n-2 have two live inputs (half adders) and cell n−1n - 1 has one (a wire). In rows j≥2j \ge 2, cell i≤n−2i \le n - 2 has three live inputs, because c(j−1)[i]c^{(j-1)}[i] comes from an adder of row j−1j - 1; so these rows have n−1n - 1 full adders each. The final adder has a half adder at i=1i = 1 and full adders at i=2,…,n−1i = 2, \ldots, n-1. This gives (n−1)(n−2)+(n−2)=n2−2n(n-1)(n-2) + (n-2) = n^2 - 2n full adders and (n−1)+1=n(n - 1) + 1 = n half adders for n≥2n \ge 2.

Back to Array multiplier