Algorithm Search⌘K

Proofs: Booth's multiplication

These are the human proofs of Booth's multiplication, at full length.

Booth's recoding and the sequential algorithm compute the signed product full

This proof shows: correctness, termination.

Let yy be an nn-bit two's complement integer with bits yn−1,…,y0y_{n-1}, \ldots, y_0 and y−1=0y_{-1} = 0, so y=−yn−12n−1+∑i=0n−2yi2iy = -y_{n-1}2^{n-1} + \sum_{i=0}^{n-2} y_i 2^i. Let Di=yi−1−yi∈{−1,0,1}D_i = y_{i-1} - y_i \in \{-1, 0, 1\}.

Termination. BOOTH runs exactly nn steps and BOOTH-4 exactly ⌈n/2⌉\lceil n/2 \rceil digits.

Lemma 1 (radix-2 recoding). ∑i=0n−1Di2i=y\sum_{i=0}^{n-1} D_i 2^i = y.

Proof. ∑i=0n−1yi−12i=∑i=0n−2yi2i+1\sum_{i=0}^{n-1} y_{i-1} 2^i = \sum_{i=0}^{n-2} y_i 2^{i+1}, so the sum is ∑i=0n−2yi(2i+1−2i)−yn−12n−1=∑i=0n−2yi2i−yn−12n−1=y\sum_{i=0}^{n-2} y_i (2^{i+1} - 2^i) - y_{n-1}2^{n-1} = \sum_{i=0}^{n-2} y_i 2^i - y_{n-1} 2^{n-1} = y. □\square

Lemma 2 (radix-4 recoding). For even nn and qj=y2j−1+y2j−2y2j+1q_j = y_{2j-1} + y_{2j} - 2y_{2j+1}, ∑j=0n/2−1qj4j=y\sum_{j=0}^{n/2-1} q_j 4^j = y, and qj∈{−2,…,2}q_j \in \{-2, \ldots, 2\}.

Proof. qj4j=D2j22j+D2j+122j+1q_j 4^j = D_{2j} 2^{2j} + D_{2j+1} 2^{2j+1}, since D2j+2D2j+1=y2j−1−y2j+2y2j−2y2j+1=qjD_{2j} + 2D_{2j+1} = y_{2j-1} - y_{2j} + 2y_{2j} - 2y_{2j+1} = q_j. Summing over jj gives Lemma 1. The bound is clear from y2j−1+y2j∈[0,2]y_{2j-1} + y_{2j} \in [0, 2]. For odd nn, sign extension by one bit keeps the value of yy, and the claim applies to n+1n + 1 bits. □\square

So line L8 returns ∑jqjx4j=xy\sum_j q_j x 4^j = xy, which proves BOOTH-4.

The sequential algorithm. Read the 2n+12n + 1 bits of (A,Q,q−1)(A, Q, q_{-1}) with AA in two's complement of n+1n + 1 bits. Write val⁡(A)\operatorname{val}(A) for the signed value of AA. Before step k+1k + 1 (0≤k≤n0 \le k \le n), let QQ hold the bits yn−1,…,yky_{n-1}, \ldots, y_k in its low n−kn - k positions and the kk low bits of a partial product in its top kk positions, and q−1=yk−1q_{-1} = y_{k-1}. We prove the invariant

Pk:=val⁡(A)⋅2k+(top k bits of Q)=x∑i=0k−1Di2i,P_k := \operatorname{val}(A) \cdot 2^k + (\text{top } k \text{ bits of } Q) = x \sum_{i=0}^{k-1} D_i 2^i ,

and AA never overflows.

Base. For k=0k = 0, A=0A = 0, Q=yQ = y, q−1=0=y−1q_{-1} = 0 = y_{-1}, and both sides are 0.

Step. At step k+1k + 1, q0=ykq_0 = y_k and q−1=yk−1q_{-1} = y_{k-1}, so lines L3 and L4 add DkxD_k x to AA. By the invariant, ∣Pk∣≤∣x∣(2k−1)|P_k| \le |x| (2^k - 1), and the top kk bits of QQ form a number T∈[0,2k)T \in [0, 2^k), so val⁡(A)=(Pk−T)/2k\operatorname{val}(A) = (P_k - T)/2^k satisfies −∣x∣−1<val⁡(A)<∣x∣-|x| - 1 < \operatorname{val}(A) < |x|, that is −2n−1≤val⁡(A)≤2n−1−1-2^{n-1} \le \operatorname{val}(A) \le 2^{n-1} - 1 since ∣x∣≤2n−1|x| \le 2^{n-1}. With ∣Dkx∣≤2n−1|D_k x| \le 2^{n-1}, −2n≤val⁡(A)+Dkx≤2n−1-2^n \le \operatorname{val}(A) + D_k x \le 2^n - 1, which is the range of n+1n + 1 bits; so no overflow occurs. Line L5 is an arithmetic shift of AA by one bit with the low bit of AA moved into the top of QQ; with A′=⌊(val⁡(A)+Dkx)/2⌋A' = \lfloor (\operatorname{val}(A) + D_k x)/2 \rfloor and bb the bit moved, val⁡(A)+Dkx=2A′+b\operatorname{val}(A) + D_k x = 2A' + b. Then

Pk+1=A′2k+1+b 2k+(top k bits)=(val⁡(A)+Dkx)2k+(top k bits)=Pk+Dkx2k,P_{k+1} = A' 2^{k+1} + b\, 2^k + (\text{top } k \text{ bits}) = (\operatorname{val}(A) + D_k x) 2^k + (\text{top } k \text{ bits}) = P_k + D_k x 2^k ,

which is the invariant for k+1k + 1. Line L5 also moves yky_k into q−1q_{-1} and exposes yk+1y_{k+1} in q0q_0.

End. After nn steps, Pn=val⁡(A)2n+Q=x∑i<nDi2i=xyP_n = \operatorname{val}(A) 2^n + Q = x \sum_{i<n} D_i 2^i = xy by Lemma 1. Since ∣xy∣≤22n−2<22n−1|xy| \le 2^{2n-2} < 2^{2n-1}, the low 2n2n bits of (A,Q)(A, Q), read in two's complement, are xyxy, which line L6 returns. ■\blacksquare

Back to Booth's multiplication