Algorithm Search⌘K

Proofs: Wallace tree multiplier

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

The Wallace tree computes the product in few levels full

This proof shows: correctness, termination.

Let a=∑i<nai2ia = \sum_{i<n} a_i 2^i and b=∑j<mbj2jb = \sum_{j<m} b_j 2^j with bits ai,bja_i, b_j. A configuration is a finite multiset of bits placed in columns 0,1,2,…0, 1, 2, \ldots; its value is V=∑k2kσkV = \sum_k 2^k \sigma_k, where σk\sigma_k is the number of ones in column kk, and its height in column kk is the number hkh_k of bits in column kk.

Lemma 1 (the adders keep the value). For bits x,y,zx, y, z,

x+y+z=(x⊕y⊕z)+2 maj(x,y,z),x+y=(x⊕y)+2(x∧y).x + y + z = (x \oplus y \oplus z) + 2\,\mathrm{maj}(x, y, z), \qquad x + y = (x \oplus y) + 2(x \wedge y).

Proof. Both sides of the first identity are symmetric in x,y,zx, y, z and depend only on the number tt of ones among them: for t=0,1,2,3t = 0, 1, 2, 3 the right side is 0,1,0+2,1+20, 1, 0 + 2, 1 + 2. The second identity is the first with z=0z = 0. □\square

The full adder of the implementation computes s=(x⊕y)⊕zs = (x \oplus y) \oplus z and c=(x∧y)∨((x⊕y)∧z)c = (x \wedge y) \vee ((x \oplus y) \wedge z), and the second expression is maj(x,y,z)\mathrm{maj}(x, y, z): if x=yx = y it is xx, and if x≠yx \ne y it is zz. So replacing three bits of column kk by a full adder's sum bit in column kk and carry bit in column k+1k + 1 changes VV by 2k(s−x−y−z)+2k+1c=02^k(s - x - y - z) + 2^{k+1} c = 0, and the same holds for a half adder.

Lemma 2 (invariant). After line L1 and after every level of lines L2 to L6, V=abV = ab.

Proof. After line L1, column kk holds the bits ai∧bj=aibja_i \wedge b_j = a_i b_j with i+j=ki + j = k, so V=∑i,jaibj2i+j=abV = \sum_{i,j} a_i b_j 2^{i+j} = ab. A level replaces disjoint groups of bits by adder outputs, each replacement keeps VV by Lemma 1, and bits outside the groups stay where they are; so VV is unchanged. □\square

Lemma 3 (termination). The loop of line L2 ends after finitely many levels.

Proof. A full adder replaces three bits by two, and a half adder two bits by two, so the total number of bits never grows. If the loop runs, some column holds h≥3h \ge 3 bits, and line L5 applies ⌊h/3⌋≥1\lfloor h/3 \rfloor \ge 1 full adders there, so the total number of bits falls by at least one. It starts at nmnm, so there are at most nmnm levels. Lemma 4 gives a much better bound. □\square

Theorem (correctness). The algorithm returns abab.

Proof. When the loop ends every column holds at most two bits, so line L7 can place them in two rows XX and YY with X+Y=∑k2kσk=V=abX + Y = \sum_k 2^k \sigma_k = V = ab by Lemma 2. Line L8 adds them exactly, for example by the Kogge-Stone adder. No bit is lost by the width n+mn + m: a bit in a column k≥n+mk \ge n + m would have weight at least 2n+m2^{n+m}, and since all weights are nonnegative and V=ab<2n+mV = ab < 2^{n+m}, every such bit is 0. □\square

Lemma 4 (levels). Let d0=2d_0 = 2 and dj+1=⌊3dj/2⌋d_{j+1} = \lfloor 3 d_j / 2 \rfloor. If every column has height at most dj+1d_{j+1}, then after one level every column has height at most djd_j. Hence, if LL is the least index with dL≥min⁡(n,m)d_L \ge \min(n, m), the loop runs at most LL times.

Proof. Consider column kk of height hh and column k−1k - 1 of height h′h'. Write h=3q+rh = 3q + r with 0≤r<30 \le r < 3. In column kk the level leaves qq sum bits of full adders and, if r>0r > 0, one more bit (a half adder's sum or a single bit); that is ⌈h/3⌉\lceil h/3 \rceil bits. From column k−1k - 1 it receives one carry for each full adder and one for a half adder, which is ⌊h′/3⌋+[h′ mod 3=2]=⌊(h′+1)/3⌋\lfloor h'/3 \rfloor + [h' \bmod 3 = 2] = \lfloor (h' + 1)/3 \rfloor carries. Both counts are nondecreasing in hh and h′h', so for h,h′≤D=dj+1h, h' \le D = d_{j+1} the new height is at most

⌈D/3⌉+⌊(D+1)/3⌋=⌈2D/3⌉,\lceil D/3 \rceil + \lfloor (D + 1)/3 \rfloor = \lceil 2D/3 \rceil ,

as the three cases D=3q,3q+1,3q+2D = 3q, 3q + 1, 3q + 2 give 2q,2q+1,2q+22q, 2q + 1, 2q + 2. Since D=⌊3dj/2⌋≤3dj/2D = \lfloor 3 d_j/2 \rfloor \le 3 d_j / 2, we have 2D/3≤dj2D/3 \le d_j, and djd_j is an integer, so ⌈2D/3⌉≤dj\lceil 2D/3 \rceil \le d_j.

After line L1 column kk has min⁡(k,n−1,m−1,n+m−2−k)+1≤min⁡(n,m)≤dL\min(k, n - 1, m - 1, n + m - 2 - k) + 1 \le \min(n, m) \le d_L bits. By induction, after tt levels every height is at most dL−td_{L - t}, so after LL levels every height is at most d0=2d_0 = 2, and the loop has stopped. □\square

Since dj+1≥32dj−12d_{j+1} \ge \frac{3}{2} d_j - \frac12, we have dj≥(3/2)j+1d_j \ge (3/2)^j + 1, so L≤log⁡3/2(min⁡(n,m))+1L \le \log_{3/2}(\min(n, m)) + 1.

Corollary (depth). The AND gates of line L1 have depth 1. A full adder's outputs have depth at most 3 more than its inputs, and a half adder's at most 1 more, so after LL levels every bit has depth at most 1+3L1 + 3L. A Kogge-Stone adder of width W=n+mW = n + m adds at most 2⌈log⁡2W⌉+22\lceil \log_2 W \rceil + 2: one level for the generate and propagate signals, two gates for each of the ⌈log⁡2W⌉\lceil \log_2 W \rceil prefix levels, and one XOR for the sum bits. So the depth is at most 3L+2⌈log⁡2(n+m)⌉+33L + 2 \lceil \log_2 (n + m) \rceil + 3, which is O(log⁡n)O(\log n) for m=nm = n. ■\blacksquare

Back to Wallace tree multiplier