Listening To Booth’s Algorithm using Rust
and Orca
Muhammad Areeb Kazmi
September 2020
1 Booth’s Algorithm
Booth’s multiplication algorithm efficiently multiplies two signed binary
numbers in two’s complement notation. The inventor of the algorithm,
Andrew Donald Booth, came up with it while researching crystallography at
Birkbeck College in Bloomsbury, London in 1950. The Booth’s algorithm is
related to computer architecture and organization [1].
1.1 Problem solved by the Booth’s algorithm
Humans can normally multiply small numbers, but they find it difficult to
multiply larger numbers. Computers, which deal with binary language only,
need to use different methods of multiplication than humans. Booth’s al-
gorithm results in computers effectively multiplying signed numbers with
fewer additions and subtractions required.
1.2 The algorithm
Like all multiplication algorithms, Booth’s algorithm requires the examina-
tion of multiplier bits and partial product. The algorithm is based on the
add-and-shift method. Partial products are generated by the shift opera-
tion; the add operation then sums them up.
Adjacent bits of the ’n’-bit multiplier q are examined in signed two’s com-
plement representation along with an implicit bit below the least significant
bit q−1 = 0. Let m be the multiplicand and q be the multiplier. For each bit
qi , for i running from n − 1 to 0, the bits qi and qi+1 are considered.
Though the representation of the multiplicand and product are not spec-
ified, both are in two’s complement representation. Any number system
that supports addition and subtraction will work as well. The order of the
steps is not determined. It generally proceeds from the Least Significant
1
Bit (LSB) to the Most Significant Bit (MSB). The algorithm works with an
accumulator register which will be discussed later. Before shift operation,
the multiplicand m may be added to or subtracted from the partial product
or left unchanged according to the following conditions.
1. When the qi bit of the multiplier q is 1 whereas qi+1 is 0, the multipli-
cand m is subtracted from the partial product that is, the accumulator.
Then, multiply the existing sum of partial products by 2−1 - shift one
place to the right or Arithmetic Shift Right (ASR).
2. When the qi bit of multiplier q is 0 whereas qi+1 is 1, m is added to the
partial product, and the result is multiplied by 2−1 that is, Arithmetic
Shift Right.
3. When both the bits are identical, partial product is not changed, and
Arithmetic Shift Right takes place.
4. Do not multiply by 2−1 at q0 in the above processes.
The Booth’s algorithm can be comprehended observing the steps below
where −7 is multiplied by −3:
Figure 1: How Booth’s algorithm multiplies two numbers
2
1.3 An example for implementation
Let m = −7 and q = −3. Humans can easily tell that the product will be
+21, as m × q = +21. But for computers to find the result, certain working
is required. The working is shown on the next page, where ACC is the
product accumulator. The binary equivalent for −7 in two’s complement
will be (1001)2 . Subsequently, the value of −m will be positive 7 i.e. (0111)2 .
Correspondingly we know the value of q .
In the operations, when the condition results in ACC = ACC − m, the
current value of ACC is added to the −m (negative equivalent of m) variable
via binary addition. So, it is actually ACC = ACC + (−m). When the
conditions result in ACC = ACC + m, the current value of ACC is added to
that of m through binary addition.
After each condition, Arithmetic Shift Right (ASR in figure on the next page)
takes place on ACC, q, q−1 : where each bit is pushed towards the right. Only
the MSB of ACC is the exception, it is not only copied to the right, but also
remains the MSB of the ACC that is, copied to the left of the shifted MSB.
The algorithm runs until the count = 0, after which the bits in ACC and q
combined are the answer of the product. The binary value can be converted
into its respective base 10 value to prove it. The red slashes in the figure
show how ASR is different from Shift Right.
3
4
2 Comparison with other algorithms
Different algorithms will be explained and compared with Booth’s Algorithm
in this section. The time complexity of Booth’s algorithm depends upon the
complexity of addition and bit shift. If they are both O(1), then the time
complexity of Booth’s algorithm is O(n). However, that is not the case. The
general formula is O(n) ∗ (complexity of addition + complexity of bit shift)
that is, although very optimized, near O(n2 ). The Radix-2 Booth’s algorithm
is disadvantageous because the process requires n shifts and n/2 additions
on average for an n bit multiplier. This variable number of add-and-shift op-
erations is inconvenient for designing parallel multipliers. It also becomes
inefficient when there are isolated 1’s.
2.1 Peasant multiplication
Peasant multiplication [2] is also know as Ethiopian multiplication, Egyp-
tian multiplication, and Russian multiplication. It was a systematic method
for multiplying two numbers, one of two multiplication methods used by
scribes, that does not require the multiplication table. It required the abil-
ity to multiply and divide by two, and to add. One of the multiplicands,
preferably the smaller one, is decomposed into a sum of powers of two and
a table of doubling of the second multiplicand is created
Although in ancient Egypt the concept of base 2 did not exist, the algorithm
is essentially the same algorithm as long multiplication after the multiplier
and multiplicand are converted to binary. In modern computer processors,
this method is still in wide use today as implemented by binary multiplier
circuits. The algorithm can be only used for unsigned binary numbers, un-
like Booth’s algorithm which multiplies signed binary numbers.
Let a ∗ b, where a and b are to be multiplied. The Russian algorithm is
implemented under the following steps:
1. The value of a ∗ b is same as (a ∗ 2) ∗ (b/2) if b is even, otherwise the
value is same as ((a ∗ 2) ∗ (b/2) + a).The multiplicand a is multiplied
with 2, that is, shifted to left. The multiplier b is divided by 2, that is,
shifted to right on the other hand.
2. If b becomes odd, we add the current value of a into a sum.
3. The final sum after b = 1 is the product of the original a ∗ b.
An example has been inserted below where b = 9 is multiplied with a = 13.
5
Here, the binary numbers, equivalent to 13 and 104 were added as, in other
cases, b was even.
The Peasant multiplication algorithm uses the process of Worst case time
complexity O(logn); Space complexity of O(1). It takes more steps than
long multiplication and Booth’s algorithm, so it can be unwieldy of large
numbers.
2.2 Long multiplication
Sometimes called Standard algorithm, Long multiplication is also used in
base 2. The multiplicand is multiplied with each digit or bit of the multiplier,
and all the properly shifted results are added up. Computers initially used a
very similar shift and add algorithm in base 2, but modern processors have
optimized circuitry for fast multiplications using more efficient algorithms.
The efficient algorithms came at the price of more complex hardware real-
ization. Below is an example of long multiplication in base 2:
6
Let n be the total number of digits in the two input numbers. If the result
must be kept in memory, then the space complexity is O(n2 ). Nevertheless,
in certain applications, the entire result need not to be kept in memory and
instead the digits of the result can be streamed out as they are computed to
system console or file for example, in situations like these, long multiplica-
tion is advantageous that it can be formulated as a log space algorithm fea-
sibly; to be specific, and algorithm that needs working space proportional
to logarithm of the number of digits in input O(logn). It is to be considered
that the operands themselves still need to be kept in memory and their O(n)
space is not considered in this analysis.
To multiply two numbers with n bits, one needs about n2 operations. Using
a neutral size metric of number of digits, the time complexity of multiplying
two n-digit numbers using long multiplication is O(n2 ).
2.3 Modified Booth’s algorithm (Radix-4)
The modified Booth’s algorithm [4][6], also known as Booth recoding, is
used to perform high-speed multiplications. The modified Booth’s algorithm
reduces the number of partial products to half of the partial products in
Booth’s algorithm. The modified Booth’s algorithm increases the speed of
the multiplier and reduces the area of the multiplier circuit. Therefore, the
speed and circuit complexity of Radix-4 modified Booth’s algorithm is bet-
ter than Radix-2 Booth’s algorithm. The algorithm also has less complexity
compared to other high Radix Booth algorithms. Radix-4 Booth’s algorithm
overcomes all the limitations of Booth’s algorithm discussed previously on
page 4. Every second column of the multiplier term is taken, and it is mul-
tiplied by ±1, ±2, or 0. The multiplicand is encoded based on multiplier bits
in the Radix-4 Booth encoder. Unlike Booth’s algorithm, which examines
every two bits, the Radix-4 Booth encoder examines every three bits with
an overlapping technique. Where the grouping is started from LSB, the first
block only uses two bits of the multiplier and a zero is assumed as the third
bit. The table below shows the functional operations of the algorithm:
7
Radix-4 Booth algorithm is:
1. Extend the sign bit to ensure if n is even.
2. Append a 0 to the right of LSB of the multiplier
3. According to the value of each vector, each Partial Product will be 0,
+q , −q , +2q , or −2q .
The negative values of q are made by taking the 2’s complement. The multi-
plication of q is done by shifting q one place to the left and adding a zero af-
ter the LSB. Therefore, in any case, only n/2 partial products are generated.
The halving of partial products is advantageous because it is important in
circuit design, as it relates to the propagation delay in the running of the
circuit, and the complexity and power consumption of its implementation.
For operands equal to or greater than 16 bits, the modified Booth’s algo-
rithm is widely used. The same inputs as before in Booth’s algorithm have
been multiplied using the modified Booth’s algorithm below:
8
2.4 Radix-8 Booth Algorithm
The same algorithm as that of the Radix-4 Booth algorithm is applied to
Radix-8 Booth recoding. However, quartets of bits are taken instead of
triplets. Each block of four bits is codified using the table on the next page:
9
The Radix-8 algorithm reduces the number of partial products generated
to n/3 where n is the number of multiplier bits. Therefore, it allows in
time gain in the summation of the partial products, but the computation
of each partial product is more complex as circuit complexity is increased.
the reduction in the number of partial products may represent a significant
gain in efficiency. The Radix-8 multiplier also lacks parameters like delay
and speed due to the complexity of the circuit. When a higher number of bits
are considered, the Radix-8 Booth algorithm has lower power consumption
than Radix-4 and Booth’s algorithm.
3 Writing pseudocode
The pseudocode was written with the syntax of the Rust programming lan-
guage in mind. The data type for the variables used is a vector data type,
but the user can work with array data types as well, keeping in mind the
restraints of the language. The user must also make sure that the number
of bits of m and q is kept equal to avoid errors and to make the code robust.
One way to do this can be found in the link of the implementation of the
algorithm in Rust. [3]
3.1 Pseudocode for Booth’s Algorithm
This pseudocode specifically shows how Booth’s Algorithm might work in
the case of finding the product of two signed numbers.
Definition. Let ACC, m, minusm, q be the vector data types. Let the other
variables be signed integer data types.
10
Algorithm 1: Booth’s Algorithm
Data: ACC = [0..LEN GT H(m)], m = multiplicand, q = multiplier,
minusm = negative equivalent of multiplicand
1 Main()
2 q0 = 0
3 count = LEN GT H(ACC)
4 msbindex = 0
5 lsbindex = (LEN GT H(ACC) − 1)
6 pos = (LEN GT H(q) − 2)
7 accmsb = 0
8 position = (LEN GT H(ACC) − 1)
9 while count 6= 0 do
10 if value of final entry of q is 1 AND q0 = 0 then
11 J = (LEN GT H(ACC) − 1)
12 ACC = subtratedm[J , minusm, ACC ]
13 end
14 if Value of final index of q = 1 AND q0 = 1 then
15 K = LEN GT H(ACC) − 1
16 ACC = addedm[K , m, ACC ]
17 end
18 q0 = q[lsbindex]
19 while pos ≥ 0 do
20 q[pos + 1] = q[pos]
21 pos = pos − 1
22 end
23 q[msbindex] = ACC[lsbindex]
24 accmsb = ACC[msbindex]
25 while position ≥ 0 do
26 ACC[position + 1] = ACC[position]
27 position = position − 1
28 end
29 ACC[0] = accmsb
30 count = count − 1 . Arithmetic Shift Right
31 end
32 PRINT ACC, q
11
Algorithm 2: Booth’s Algorithm (continued)
1 Function addedm(acc, k , M ):
2 carry = 0 while K ≥ 0 do
3 binsum = acc[k] + m[k] + carry
4 if binsum < 2 then
5 acc[k] = binsum
6 carry = 0
7 if binsum = 2 then
8 carry = 1
9 acc[k] = 0
10 else
11 carry = 1
12 acc[k] = 1
13 k =k−1
14 if k = −1 then
15 carry = 0
16 return acc
17 Function subtractedm(acc, j , minusM ):
18 carry = 0 while J ≥ 0 do
19 binsum = acc[j] + minusM [j] + carry
20 if binsum < 2 then
21 acc[j] = binsum carry = 0
22 if binsum = 2 then
23 carry = 1 acc[j] = 0
24 else
25 carry = 1
26 acc[j] = 1
27 j =j−1
28 if j = −1 then
29 carry = 0
30 return acc
12
4 Listening to Booth’s Algorithm with Orca
Orca is an esoteric programming language designed to create procedural
sequencers quickly. In Orca, every letter of the alphabet is an operation,
where lower case letters operate as bangs and upper case letters operate
each frame. The Orca commands can be understood following the link.
[5] Orca is designed to control a synthesizer to make music. Orca also
possesses many of the core programming elements of languages like Rust.
Therefore, this section details an attempt to make Orca accomplish what it
was not originally meant to accomplish.
4.1 Orca Code
The Orca code below follows the pseudo code on the previous page. The
code is not entirely robust, and it requires the user to manually make changes.
The required changes can be made in section 4.3. The main accomplish-
ments of the code were binary addition and Arithmetic Shift Right (ASR).
Figure 2: Orca code for Booth’s algorithm
13
4.2 Explanation of Orca Code
The main two accomplishments, as mentioned previously, were binary addi-
tion and ASR, which play a great role in Booth’s algorithm.
4.2.1 Binary Addition
The leftmost column of the code is focused on the current as well as section
4.2.2. Apart from the code under the red lines, the entire code is that of
binary addition.
Figure 3: Orca code for Binary addition
Where the user needs to input the values in the block of bits next to red
lines. The values of ACC, q, and q0 are to be input in the bits before the
comment ’add this’, whereas the value of m that is to be added should be
input in the first four Most Significant Bits (MSBs). The number of bits
which should be taken into account to be added is assigned to the variable
’n’.
The purpose of each block of code in the binary addition can be inferred by
reading the comments that are represented by hashes #. The results of the
binary addition can be seen before the comment ’#result#.’
14
4.2.2 Arithmetic Shift Right (ASR)
The code under the red lines is that of ASR in the continuation of Figure 3.
Figure 4: Orca code for Arithmetic Shift Right (ASR)
With the help of certain operations, the first block of code reads all binary
digits into a variable except the Least Significant Bit (LSB). The second
block of code prints the binary digits read. Variable ’b’ prints the MSB after
it has been shifted towards the right. As a result, ASR is accomplished in
Orca.
4.3 How to implement Booth’s Algorithm for desired in-
puts
The user must manually run the Orca code if one needs to have the inputs
of their wish. This is because, in order to avoid a much bigger code and
due to the limitations of Orca, there is no implementation of ’IF’ statements
if the Least Significant Bit (LSB) and the second most LSB are identical,
or ’10’, or ’01’. To show that in the code, comments were used for the
user to comprehend why certain steps were taken next. As a result, the
user should check the conditions and do certain operations according to the
rules specified for implementation.
Also, in each binary addition, the user needs to input the updated values
of the block of bits that is to be added each time binary addition takes
place. Consequently, the user should input the correspondent value of m
or minusm in each case depending upon the conditions. The user should
follow the same procedure as the implementation on page 4 to determine
where certain variables like m or minusm need to be placed. To place the
values of variables on the correct place is very important because the slight-
est change can lead to the wrong answer. By examining the code closely, the
15
user can infer where values need to be updated. This could have been pos-
sible by copying and pasting the values to an appropriate place with Orca
commands, but it would have gained unnecessary space that could have
confused the reader.
The user should keep in mind that the Orca code runs from the top left to
right line-by-line. The names of the variables should be different if they are
in different blocks of code, but adjacent by line. Also, the user must place
the blocks of code with the same spaces as they are distanced between
and below one another; otherwise, the results would not come as expected.
There would need to be certain changes if the user changed their positions.
4.3.1 Acknowledgement
I would especially like to thank Qijun (Mary) Hu and Professor David Perkins
for assisting me in the Orca binary addition code. The binary adder was
coded by Qijun Hu, and it was finished by Professor Perkins in a mini-project
assignment where we tried to implement things on Orca that it was not
designed to.
5 Conclusion
Although Booth’s algorithm (Radix-2) might be less important and efficient
in today’s world of advanced computers and processors, it revolutionized
the way numbers are multiplied in binary. The algorithm sowed the seeds
for Radix 4, 8, 16, and even higher. The implementation of Booth’s algorithm
in Rust and Orca can be seen by following the link provided [3].
References
[1] Andrew Donald Booth. “A Signed Binary Multiplication Technique”.
In: The Quarterly Journal of Mechanics and Applied Mathematics IV.2
(1950), pp. 236–240. DOI: 10.1093/qjmam/4.2.236.
[2] Bekir Cevizci. “How and Why does the Multiplication Method Devel-
oped by the Russian Peasants Work?” In: Journal of Inquiry Based Ac-
tivities (JIBA)/ Araştırma Temelli Etkinlik Dergisi VIII.1 (2018), pp. 24–
36. ISSN: 2146-5711 (Online).
[3] “Implementation of Booth’s Algorithm in Rust and Orca”. In: (). DOI:
https : / / github . com / MuhammadAreebKazmi / Implementation - of -
Booth-s-Algorithm.
[4] Pradip K.Srimani. “Generalized proof of modified Booth’s algorithm”.
In: Computer and Electrical Engineering VIII.1 (1981), pp. 7–9. DOI:
10.1016/0045-7906(81)90015-X.
16
[5] “Orca”. In: (). DOI: https://github.com/hundredrabbits/Orca.
[6] L.P. Rubinfield. “A Proof of the Modified Booth’s Algorithm for Multipli-
cation”. In: IEEE Transactions on Computers C-24.10 (1975), pp. 1014–
1015. DOI: 10.1109/T-C.1975.224114.
17