Fast multiplication

## Booth's multiplication algorithm

See also: Signed-digit representation
Booth's multiplication algorithm is a multiplication algorithm that multiplies two signed binary numbers in two's complement notation.

Consider a positive multiplier consisting of a block of 1s surrounded by 0s. For example, 00111110. The product is given by:

M
×

′
′

0

0

1

1

1

1

1

0

′
′

=
M
×
(

2

5

2

4

2

3

2

2

2

1

# )
M
×
62

{\displaystyle M\times \,^{\prime \prime }0\;0\;1\;1\;1\;1\;1\;0\,^{\prime \prime }=M\times (2^{5}+2^{4}+2^{3}+2^{2}+2^{1})=M\times 62}

where M is the multiplicand. The number of operations can be reduced to two by rewriting the same as

M
×

′
′

0

1

0

0

0

0

-1

0

′
′

=
M
×
(

2

6

−

2

1

# )
M
×

{\displaystyle M\times \,^{\prime \prime }0\;1\;0\;0\;0\;0{\mbox{-1}}\;0\,^{\prime \prime }=M\times (2^{6}-2^{1})=M\times 62.}

In fact, it can be shown that any sequence of 1s in a binary number can be broken into the difference of two binary numbers:

(
…
0

1
…
1

⏞

n

0
…

)

2

≡
(
…
1

0
…
0

⏞

n

0
…

)

2

−
(
…
0

0
…
1

⏞

n

0
…

)

2

.

{\displaystyle (\ldots 0\overbrace {1\ldots 1} ^{n}0\ldots )\_{2}\equiv (\ldots 1\overbrace {0\ldots 0} ^{n}0\ldots )\_{2}-(\ldots 0\overbrace {0\ldots 1} ^{n}0\ldots )\_{2}.}

Hence, the multiplication can actually be replaced by the string of ones in the original number by simpler operations, adding the multiplier, shifting the partial product thus formed by appropriate places, and then finally subtracting the multiplier. It is making use of the fact that it is not necessary to do anything but shift while dealing with 0s in a binary multiplier, and is similar to using the mathematical property that 99 = 100 − 1 while multiplying by 99.

This scheme can be extended to any number of blocks of 1s in a multiplier (including the case of a single 1 in a block). Thus,

M
×

′
′

0

0

1

1

1

0

1

0

′
′

=
M
×
(

2

5

2

4

2

3

2

1

# )
M
×
58

{\displaystyle M\times \,^{\prime \prime }0\;0\;1\;1\;1\;0\;1\;0\,^{\prime \prime }=M\times (2^{5}+2^{4}+2^{3}+2^{1})=M\times 58}

M
×

′
′

0

1

0

0

-1

1

-1

0

′
′

=
M
×
(

2

6

−

2

3

2

2

−

2

1

# )
M
×

{\displaystyle M\times \,^{\prime \prime }0\;1\;0\;0{\mbox{-1}}\;1{\mbox{-1}}\;0\,^{\prime \prime }=M\times (2^{6}-2^{3}+2^{2}-2^{1})=M\times 58.}

Booth's algorithm follows this old scheme by performing an addition when it encounters the first digit of a block of ones (0 1) and a subtraction when it encounters the end of the block (1 0). This works for a negative multiplier as well. When the ones in a multiplier are grouped into long blocks, Booth's algorithm performs fewer additions and subtractions than the normal multiplication algorithm.

### In decimal

999
×
999

=
(
1000
−
1
)
(
1000
−
1
)

=
1
,
000
,
000
−
2000
1

=
998
,
001

{\displaystyle {\begin{aligned}999\times 999&=(1000-1)(1000-1)\\&=1,000,000-2000+1\\&=998,001\end{aligned}}}

## Complex multiplication algorithm

Complex multiplication normally involves four multiplications and two additions.

(
a
b
i
)
(
c
d
i
# )
(
a
c
−
b
d
)
(
b
c
a
d
)
i
.

{\displaystyle (a+bi)(c+di)=(ac-bd)+(bc+ad)i.}

Or

×

a

b
i

c

a
c

b
c
i

d
i

a
d
i

−
b
d

{\displaystyle {\begin{array}{c|c|c}\times &a&bi\\\hline c&ac&bci\\\hline di&adi&-bd\end{array}}}

But there is a way of reducing the number of multiplications to three.

The product (*a* + *bi*) · (*c* + *di*) can be calculated in the following way.

*k*1 = *c* · (*a* + *b*)
*k*2 = *a* · (*d* - *c*)
*k*3 = *b* · (*c* + *d*)
Real part = *k*1 - *k*3
Imaginary part = *k*1 + *k*2.
This algorithm uses only three multiplications, rather than four, and five additions or subtractions rather than two. If a multiply is more expensive than three adds or subtracts, as when calculating by hand, then there is a gain in speed. On modern computers a multiply and an add can take about the same time so there may be no speed gain. There is a trade-off in that there may be some loss of precision when using floating point.

For fast Fourier transforms (FFTs) (or any linear transformation) the complex multiplies are by constant coefficients *c* + *di* (called twiddle factors in FFTs), in which case two of the additions (*d*-*c* and *c*+*d*) can be precomputed. Hence, only three multiplies and three adds are required. However, trading off a multiplication for an addition in this way may no longer be beneficial with modern floating-point units.

## Karatsuba algorithm

The **Karatsuba algorithm** is a fast multiplication algorithm. It was discovered by Anatoly Karatsuba in 1960 and published in 1962. It is a generalization of the complex multiplication algorithm above, where the imaginary unit i is replaced by a power of the base.

It reduces the multiplication of two *n*-digit numbers to at most

n

log

2

⁡
3

≈

n

1.58

{\displaystyle n^{\log \_{2}3}\approx n^{1.58}}

 single-digit multiplications in general (and exactly

n

log

2

⁡
3

{\displaystyle n^{\log \_{2}3}}

 when *n* is a power of 2). It is therefore faster than the classical algorithm, which requires

n

2

{\displaystyle n^{2}}

 single-digit products.

For example, the Karatsuba algorithm requires 310 = 59,049 single-digit multiplications to multiply two 1024-digit numbers (*n* = 1024 = 210), whereas the classical algorithm requires (210)2 = 1,048,576 (a speedup of 17.75 times).
The Karatsuba algorithm was the first multiplication algorithm asymptotically faster than the quadratic "grade school" algorithm.

### Example

The basic step of Karatsuba's algorithm is a formula that allows one to compute the product of two large numbers

x

{\displaystyle x}

 and

y

{\displaystyle y}

 using three multiplications of smaller numbers, each with about half as many digits as

x

{\displaystyle x}

 or

y

{\displaystyle y}

, plus some additions and digit shifts.

To compute the product of 12345 and 6789 we decompose the input operands as:

12345 = **12** · *1000* + **345**
6789 = **6** · *1000* + **789**
Only three multiplications, which operate on smaller integers, are used to compute three partial results:

*z*2 = **12** **×** **6** = 72
*z*0 = **345** **×** **789** = 272205
*z*1 = (**12** + **345**) **×** (**6** + **789**) − *z*2 − *z*0 = 357 **×** 795 − 72 − 272205 = 283815 − 72 − 272205 = 11538
We get the final result by just adding these three partial results, shifted accordingly:

result = *z*2 · (*B**m*)*2* + *z*1 · (*B**m*)*1* + *z*0 · (*B**m*)*0*, i.e.
result = 72 · *1000*2 + 11538 · *1000* + 272205 = **83810205**.

### Explanation

Let

x

{\displaystyle x}

 and

y

{\displaystyle y}

 be represented as

n

{\displaystyle n}

-digit strings in some base

B

{\displaystyle B}

. For any positive integer

m

{\displaystyle m}

 less than

n

{\displaystyle n}

, one can write the two given numbers as

# x

x

1

B

m

x

0

,

{\displaystyle x=x\_{1}B^{m}+x\_{0},}

# y

y

1

B

m

y

0

,

{\displaystyle y=y\_{1}B^{m}+y\_{0},}

where

x

0

{\displaystyle x\_{0}}

 and

y

0

{\displaystyle y\_{0}}

 are less than

B

m

{\displaystyle B^{m}}

.

Long multiplication of x and y requires four multiplications:

x
# y
(

x

1

B

m

x

0

)
(

y

1

B

m

y

0

)
,

{\displaystyle xy=(x\_{1}B^{m}+x\_{0})(y\_{1}B^{m}+y\_{0}),}

x
# y

x

1

y

1

B

2
m

x

1

y

0

B

m

x

0

y

1

B

m

x

0

y

0

{\displaystyle xy=x\_{1}y\_{1}B^{2m}+x\_{1}y\_{0}B^{m}+x\_{0}y\_{1}B^{m}+x\_{0}y\_{0}}

Karatsuba observed that

x
y

{\displaystyle xy}

 can be computed in only three multiplications, at the cost of a few extra additions.

x
# y

x

1

y

1

B

2
m

x

1

y

0

B

m

x

0

y

1

B

m

x

0

y

0

{\displaystyle xy=x\_{1}y\_{1}B^{2m}+x\_{1}y\_{0}B^{m}+x\_{0}y\_{1}B^{m}+x\_{0}y\_{0}}

x
# y

x

1

y

1

B

2
m

(

x

1

y

0

x

0

y

1

)

B

m

x

0

y

0

{\displaystyle xy=x\_{1}y\_{1}B^{2m}+(x\_{1}y\_{0}+x\_{0}y\_{1})B^{m}+x\_{0}y\_{0}}

x
# y

z

2

B

2
m

z

1

B

m

z

0

,

{\displaystyle xy=z\_{2}B^{2m}+z\_{1}B^{m}+z\_{0},}

where

z

2

=

x

1

y

1

{\displaystyle z\_{2}={\color {red}x\_{1}y\_{1}}}

z

1

=

x

1

y

0

x

0

y

1

{\displaystyle z\_{1}=x\_{1}y\_{0}+x\_{0}y\_{1}}

z

0

=

x

0

y

0

{\displaystyle z\_{0}={\color {red}x\_{0}y\_{0}}}

and

z

1

=

(

x

1

x

0

)
(

y

1

y

0

)

−

z

2

−

z

0

{\displaystyle z\_{1}={\color {red}(x\_{1}+x\_{0})(y\_{1}+y\_{0})}-z\_{2}-z\_{0}}

z

1

=

x

1

y

1

x

1

y

0

x

0

y

1

x

0

y

0

−

x

1

y

1

−

x

0

y

0

{\displaystyle z\_{1}=x\_{1}y\_{1}+x\_{1}y\_{0}+x\_{0}y\_{1}+x\_{0}y\_{0}-x\_{1}y\_{1}-x\_{0}y\_{0}}

z

1

=

x

1

y

0

x

0

y

1

{\displaystyle z\_{1}=x\_{1}y\_{0}+x\_{0}y\_{1}}

If *n* is four or more, the three multiplications in Karatsuba's basic step can be computed by recursive calls of the Karatsuba algorithm. The recursion can be applied until the numbers are so small that they can (or must) be computed directly. See Toom–Cook multiplication.

### Issue

With

z

0

{\displaystyle z\_{0}}

 and

z

2

{\displaystyle z\_{2}}

 as before one can observe that

z

1

=
(

x

1

x

0

)
(

y

1

y

0

)
−

z

2

−

z

0

.

{\displaystyle z\_{1}=(x\_{1}+x\_{0})(y\_{1}+y\_{0})-z\_{2}-z\_{0}.}

An issue that occurs, however, when computing

z

1

{\displaystyle z\_{1}}

 is that the above computation of

(

x

1

x

0

)

{\displaystyle (x\_{1}+x\_{0})}

 and

(

y

1

y

0

)

{\displaystyle (y\_{1}+y\_{0})}

 may result in overflow (will produce a result in the range

0
≤

result

<
2

B

m

{\displaystyle 0\leq {\text{result}}<2B^{m}}

), which require a multiplier having one extra bit. This can be avoided by noting that

z

1

=
(

x

0

−

x

1

)
(

y

1

−

y

0

)

z

2

z

0

.

{\displaystyle z\_{1}=(x\_{0}-x\_{1})(y\_{1}-y\_{0})+z\_{2}+z\_{0}.}

This computation of

(

x

0

−

x

1

)

{\displaystyle (x\_{0}-x\_{1})}

 and

(

y

1

−

y

0

)

{\displaystyle (y\_{1}-y\_{0})}

 will produce a result in the range of

−

B

m

<

result

<

B

m

{\displaystyle -B^{m}<{\text{result}}<B^{m}}

. This method may produce negative numbers, which require one extra bit to encode signedness, and would still require one extra bit for the multiplier. However, one way to avoid this is to record the sign and then use the absolute value of

(

x

0

−

x

1

)

{\displaystyle (x\_{0}-x\_{1})}

 and

(

y

1

−

y

0

)

{\displaystyle (y\_{1}-y\_{0})}

 to perform an unsigned multiplication, after which the result may be negated when both signs originally differed. Another advantage is that even though

(

x

0

−

x

1

)
(

y

1

−

y

0

)

{\displaystyle (x\_{0}-x\_{1})(y\_{1}-y\_{0})}

 may be negative, the final computation of

z

1

{\displaystyle z\_{1}}

 only involves additions.

## See also

* Schönhage–Strassen algorithm
* Wallace tree