Chapter 07 Computer Arithmetic 1
Chapter 07 Computer Arithmetic 1
PROPRIETARY MATERIAL. © 2014 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means, without the
prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. PowerPoint Slides are being provided only
to authorized professors and instructors for use in preparing for classes using the affiliated textbook. No other use or distribution of this PowerPoint slide is permitted. The PowerPoint slide may not be sold and may
not be distributed or be used by any student or any other third party. No part of the slide may be reproduced, displayed or distributed in any form or by any means, electronic or otherwise, without the prior written
permission of McGraw Hill Education (India) Private Limited.
1
2nd version
www.basiccomparch.com
Download the pdf of the book
videos
Print version
The pdf version of the book and
(Publisher: WhiteFalcon, 2021)
all the learning resources can be
Available on e-commerce sites.
freely downloaded from the
website: www.basiccomparch.com
Outline
* Addition
* Multiplication
* Division
* Floating Point Addition
* Floating Point Multiplication
* Floating Point Division
3
Adding Two 1 bit Numbers
4
Sum and Carry
a
b
carry sum
a b s c
0 0 0 0 S =a ⊕ b=𝑎. b+ a . 𝑏
0 1 1 0
1 0 1 0 c = a.b
1 1 0 1
Truth Table
5
Half Adder
* Adds two 1 bit numbers to produce a 2 bit
result a Half S
b adder C
a
b
S
a
C
b
6
Full Adder
𝑎 +𝑏+𝑐 𝑖𝑛 =2 ∗𝑐 𝑜𝑢𝑡 +𝑠
a b cin s cout
0 0 0 0 0
0 1 0 1 0
1 0 0 1 0
1 1 0 0 1
0 0 1 1 0
0 1 1 0 1
1 0 1 0 1
1 1 1 1 1
7
Equations for the Full Adder
𝑐 𝑜𝑢𝑡 =𝑎 . 𝑏+ 𝑎 . 𝑐 𝑖𝑛 +𝑏 . 𝑐𝑖𝑛
8
Circuit for the Full Adder
a Full S
b
adder c out
cin
a
a b
a
b cin cout
cin cin
b
9
Addition of two n bit numbers
1 1 1 1
1011
0101
10000
* We start from the lsb
* Add the corresponding pair of bits and the carry in
* Produce a sum bit and a carry out
10
Observations
* We keep adding pairs of bits, and proceed from
the lsb to the msb
* If a carry is generated, we add it to the next
pair of bits
* At the last step, if a carry is generated, then it
becomes the msb of the result
* The carry effectively ripples through the bits
11
Ripple Carry Adder
Full Half
c carry adder adder
Result
12
Operation of the Ripple Carry Adder
* Problem : Add A + B
* Number the bits : A1 to An and B1 to Bn
* lsb → A1 and B1
* msb → An and Bn
13
How long does the Ripple Carry Adder take ?
* Time :
* Time of half adder : th
* Time of full adder : tf
* Total Time : th + (n-1)tf
14
Asymptotic Time Complexity
* Most of the time, we are primarily interested in
the order of the function
* For example : we are only interested in the n2
term in (2n2 + 3n + 4)
* We do not care about the constants, and terms
with smaller exponents
* 3n and 4
* We can thus say that :
* 2n2 + 3n + 4 is order of (n2)
15
The O notation
* Formally :
* We say that: f(n) = O(g(n))
* if, , for all . Here c is a positive constant.
* In simple terms:
* Beyond a certain n , g(n) is greater-than-equal to a
certain constant times f(n)
* For example, beyond 15, (n2 + 10n + 16) ≤ 2n2
16
Example of the big O Notation
f(n) = 3n2 + 2n + 3. Find its asymptotic time complexity.
Answer:
f(n) = 3n2 + 2n + 3
≤ 3n2 + 2n2 + 3n2 (n > 1)
≤ 8(n2)
Hence, f(n) = O(n2).
800
f(n)
700 8n^2
600
500
400
time
300
200
100
0
0 2 4 6 8 10
n
17
Big O Notation - II
Example:
f(n) = 0.00001n100 + 10000n99 + 234344. Find its asymptotic time complexity.
18
Ripple Carry Adders and Beyond
Yes
19
Carry Select Adder O(√n) time
* Group bits into blocks of size (k)
* If we are adding two 32 bit numbers A and B,
and k = 4, then the blocks are :
Carry propagating
across blocks
20
Carry Select Adder - II
*
In this case, the carry propagates across
blocks
* Time complexity is O(n)
* Idea :
*
Add the numbers in each block in parallel
*
Stage I : For each block, produce two results
* Assuming an input carry of 0
* Assuming an input carry of 1
21
Carry Select Adder – Stage II
* For each block we have two results available
* Result → (k sum bits), and 1 carry out bit
* Stage II
* Start at the least significant block
* The input carry is 0
* Choose the appropriate result from stage I
* We now know the input carry for the second block
* Choose the appropriate result
* Result contains the input carry for the third block
22
Carry Select Adder – Stage II
* Given the result of the second block
* Compute the carry in for the third block
* Choose the appropriate result
* Proceed till the last block
* At the last block (most significant positions)
* Choose the correct result
* The carry out value, is equal to the carry out of the
entire computation.
23
How much time did we take ?
* Our block size is k
* Stage I takes k units of time
24
Time Complexity of the Carry Select Adder
Can we do better ?
Yes
25
Carry Lookahead Adder (O(log n))
26
Generate and Propagate Functions
27
Using the G and P Functions
* If we have the generate and propagate
values for a bit pair, we can determine the
carry out
Cout = gi + pi.Cin
28
Example
Example:
Let Ai = 0, Bi = 1. Let the input carry be Cin. Compute
gi, pi, and Cout.
Answer:
29
G and P for Multi-bit Systems
C
* out i
→ output carry for i th
bit pair
* Cini → input carry for ith bit pair
* gi → generate value for ith bit pair
* pi → propagate value for ith bit pair
30
G and P for Multibit Systems - II
𝐶 1𝑜𝑢𝑡 =𝑔 1 +𝑝 1 . 𝐶 1𝑖𝑛
2 1
𝐶 =𝑔2 +𝑝2 .𝐶
𝑜𝑢𝑡 𝑜𝑢𝑡
3 2
𝐶 =𝑔 3 +𝑝 3 .𝐶
𝑜𝑢𝑡 𝑜𝑢𝑡
31
G and P for multibit Systems - III
4 3
𝐶 𝑜𝑢𝑡 =𝑔 4 + 𝑝 4 . 𝐶 𝑜𝑢𝑡
32
Patterns
1 bit 𝐶 1𝑜𝑢𝑡 = 𝑔 1
⏟ ⏟ 𝑖𝑛
1 +𝑝 1 . 𝐶
𝐺1 𝑃1
2 bit 𝐶 2𝑜𝑢𝑡 =𝑔 1
⏟ ⏟ 𝑖𝑛
2 +𝑝 2 .𝑔 1 +𝑝 2 . 𝑝 1 .𝐶
𝐺2 𝑃2
3 bit 𝐶 3𝑜𝑢𝑡 =𝑔 1
⏟ ⏟ 𝑖𝑛
3 +𝑝 3 . 𝑔2 +𝑝 3 . 𝑝 2 . 𝑔 1 + 𝑝 3 . 𝑝 2 .𝑝 1 . 𝐶
𝐺3 𝑃3
4 bit 𝐶 4𝑜𝑢𝑡 =⏟ 1
⏟ 𝑖𝑛
𝑔4 + 𝑝 4 . 𝑔3 + 𝑝 4 .𝑝 3 . 𝑔2 + 𝑝 4 .𝑝 3 . 𝑝 2 . 𝑔1 + 𝑝 4 .𝑝 3 . 𝑝 2 . 𝑝 1 . 𝐶
𝐺4 𝑃4
𝑛 1
n bit 𝐶 𝑜𝑢𝑡 =𝐺𝑛 + 𝑃 𝑛 . 𝐶 𝑖𝑛
33
Computing G and P Quickly
* Let us divide a block of n bits into two parts
C sub n
C out C in
m+1,n 1,m
34
Computing G and P Quickly - II
𝐶𝑜𝑢𝑡=𝐺𝑚+1,𝑛+𝑃𝑚+1,𝑛.𝐶𝑠𝑢𝑏
𝐶 𝑜𝑢𝑡 =𝐺1 ,𝑛 + 𝑃 1 ,𝑛 . 𝐶 𝑖𝑛
P1,n = Pm+1,n.P1,m
35
Insight into Computing G and P quickly
* Insight :
* We can compute G and P for a large block
* By first computing G and P for smaller sub-blocks
* And, then combining the solutions to find the value
of G and P for the larger block
* Fast algorithm to compute G and P
Use divide-and-conquer
Compute G and P functions in O (log (n)) time
36
Carry Lookahead Adder – Stage I
37
Carry Lookahead Adder – Stage I
Computation
Block 16 Block 1
32 31 30 29 4 3 2 1 level 0
G,P
32-31
G,P
30-29
G,P
4-3
G,P
2-1
level 1
G,P
32-29
G,P
4-1 level 2
G,P G,P
32-25
G,P
24-17
G,P
16-9 8-1 level 3
G,P G,P
32-17 16-1 level 4
G,P
32-1 level 5
38
CLA Adder – Stage I
* Compute G, P for increasing sizes of
blocks in a tree like fashion
* Time taken :
* Total : log(n) levels
* Time per level : O(1)
* Total Time : O(log(n))
39
CLA Adder – Stage II
Result Bits
2-bit RC Adder 2-bit RC Adder 2-bit RC Adder 2-bit RC Adder 2-bit RC Adder
Computation 32 31 30 29 18 17 4 3 2 1 level 0
G,P
G,P
32-31
G,P
30-29 18-17
G,P
4-3
G,P
2-1 c in1 level 1
G,P
32-29
G,P
28-25
G,P
20-17
G,P
4-1
c in1 level 2
G,P G,P
32-25
G,P
24-17
G,P
16-9 8-1 c in1 level 3
G,P block G,P
32-17
G,P
16-1
c in1 level 4
c out G,P c in
r1- r2 32
cout G,P
32-1 c in1 level 5
40
Connection of the G,P Blocks
41
Operation of CLA – Stage II
* We start at the leftmost blocks in each level
* We feed an input carry value of Cin1
* Each such block computes the output carry, and sends
it to the all the blocks that it is connected to
* Each connected block
* Computes the output carry
* Sends it to all the blocks that it is connected to
* The carry propagates to all the 2 bit RC adders
42
CLA Adder – Stage II
Result Bits
2-bit RC Adder 2-bit RC Adder 2-bit RC Adder 2-bit RC Adder 2-bit RC Adder
Computation 32 31 30 29 18 17 4 3 21 level 0
G,P
G,P
32-31
G,P
30-29 18-17
G,P
4-3
G,P
2-1 c in1 level 1
G,P
32-29
G,P
28-25
G,P
20-17
G,P
4-1
c in1 level 2
G,P G,P
32-25
G,P
24-17
G,P
16-9 8-1 c in1 level 3
G,P block G,P
32-17
G,P
16-1
c in1 level 4
c out G,P c in
r1- r2 32
cout G,P
32-1 c in1 level 5
43
Time Complexity
* In a similar manner, the carry propagates to
all the RC adders at the zeroth level
* Each of them compute the correct result
* Time taken by Stage II :
* Time taken for a carry to propagate from the (16,1)
node to the RC adders
* O(log(n))
44
Time complexities of different adders:
45
Outline
* Addition
* Multiplication
* Division
* Floating Point Addition
* Floating Point Multiplication
* Floating Point Division
46
Multiplicands
1 3 1101
9 1001
1 1 7 1101
(a) 0000 Partial sums
0000
1101
1110101
(b)
* 13 → Multiplicand
* 9 → Multiplier
* 117 → Product
47
Basic Multiplication
* Consider the lsb of the multiplier
* If it is 1, write the value of the multiplicand
* If it is 0, write 0
* Keep going ….
48
Definitions
49
Multiplying 32 bit numbers
* Let us design an iterative multiplier that multiplies
two 32 bit signed values to produce a 64 bit result
* What did we prove before
* Multiplying two signed 32 bit numbers, and saving the
result as a 32 bit number is the same as
* Multiplying two unsigned 32 bit numbers (assuming no
overflows)
* We did not prove any result regarding saving the
result as a 64 bit number
50
Class Work
51
Iterative Multiplier
Multiplicand
U V
52
Algorithm
Algorithm 1: Algorithm to multiply two 32 bit numbers and produce a 64 bit result
53
Example
Multiplicand (N) 0010 2
Product(P) 0110 6
54
3 * (-2)
Multiplicand (N) 0011 3
Product(P) 1010 -6
55
Operation of the Algorithm
* Take a look at the lsb of V
* If it is 0 → do nothing
* If it is 1 → Add N (multiplicand) to U
* Right shift
* Right shifting the partial product is the same as left
shifting the multiplicand, which
* Needs to be done in every step
56
The Last Step ...
* In the last step
* lsb of V = msb of M (multiplier)
* If it is 0 → do nothing
* If it is 1
* Multiplier is negative
* Recall : A = A1 .. n-1 - 2n-1An
* Hence, we need to subtract the multiplicand if the msb of
the multiplier is 1
57
Time Complexity
* There are n loops
* Each loop takes log(n) time
* Total time : O(n log(n))
58
Booth Multiplier
* We can make our iterative multiplier faster
* If there are a continuous sequence of 0s in the multiplier
* do nothing
* If there is a continous sequnce of 1s
* do something smart
𝑘= 𝑗
𝑀 = ∑ 2 =2
𝑘 𝑗 +1 𝑖
−2
𝑘 =𝑖
59
For a Sequence of 1s
* Sequence of 1s from position i to j
𝑘= 𝑗
𝐴= ∑ 2
𝑘
* Perform (j – i + 1) additions
𝑘=𝑖
* New method
* Subtract the multiplicand when we scan bit i ( ! count starts from
0)
* Keep shifting the partial product
* Add the multiplicand(N), when we scan bit (j+1)
* This process, effectively adds (2j+1 – 2i) * N to the partial product
* Exactly, what we wanted to do …
60
Operation of the Algorithm
* Consider bit pairs in the multiplier
* (current bit, previous bit)
* Take actions based on the bit pair
* Action table
61
Booth's Algorithm
Algorithm 2: Booth’s Algorithm to multiply two 32 bit numbers to produce a 64 bit
result
Data: Multiplier in V , U = 0, Multiplicand in N
Result: The lower 64 bits of UV contain the result
i ← 0
prevBit ← 0
for i < 32 do
i ← i + 1
currBit ← LSB of V
if (currBit,prevBit) = (1,0) then
U ← U − N
end
else if (currBit,prevBit) = (0,1) then
U ← U + N
end
prevBit ← currBit
UV ← UV >> 1 (arithmetic right shift)
end
62
Outline of a Proof
* Multiplier (M) is positive
* msb = 0
* Divide the multiplier into a sequence of continuous 0s and 1s
* 01100110111000 → 0,11, 00, 11, 0, 111, 000
* For sequence of 0s
* Both the algorithms (iterative, Booth) do not add the
multiplicand
* For a run of 1s (length k)
* The iterative algorithm performs k additions
* Booth's algorithm does one addition, and one
subtraction.
* The result is the same
63
Outline of a Proof - II
* Negative multipliers
* msb = 1
64
Outline of a Proof - III
* Case 10
* Till the (n-1)th iteration both the algorithms have
no idea if the multiplier is equal to M or M'
* At the end of the (n-1)th iteration, the partial
product is:
* Iterative algorithm : M'N
* Booth's algorithm : M'N
* If we were multiplying (M' * N), no action would have been
taken in the last iteration. The two msb bits would have
been 00. There is no way to differentiate this case from
that of computing MN in the first (n-1) iterations.
65
Outline of a Proof - IV
* Last step
* Iterative algorithm :
* Subtract 2n-1N from U
* Booth's algorithm
* The last two bits are 10 (0 → 1 transition)
* Subtract 2n-1N from U
* Both the algorithms compute :
* MN = M'N – 2n-1N
* in the last iteration
66
Outline of a Proof - V
* Case 11
* Suppose we were multiplying M' with N
* Since (M' > 0), the Booth multiplier will correctly
compute the product as M'N
* The two msb bits of M' are (01)
* In the last iteration (currBit, prevBit) is 01
* We would thus add 2n-1N in the Booth's algorithm to
the partial product in the last iteration
* The value of the partial product at the end of the (n-
1)th iteration is thus :
* M'N - 2n-1N
67
Outline of a Proof - VI
* When we multiply M with N
* In the (n-1)th iteration, the value of the partial
product is : M'N – 2n-1N
* Because, we have no way of knowing if the
multiplier is M or M' at the end of the (n-1)th
iteration
* In the last iteration the msb bits are 11
* no action is taken
* Final product : M'N – 2n-1N = MN (correct)
68
Multiplicand (N) 00011 3
Product(P) 0110 6
69
Multiplicand (N) 00011 3
Product(P) 1010 -6
70
Time Complexity
* O(n log(n))
* Worst case input
* Multiplier = 10101010... 10
71
O(log(n)2) Multiplier
* Consider an n bit multiplier and multiplicand
* Let us create n partial sums
1001
1101
1001
00000
partial sums
100100
1001000
72
Tree Based Adder for Partial Sums
log(n) levels
Final product
73
Time Complexity
* There are log(n) levels
* Each level takes
* Maximum log(2n) time
* Adds two 2n bit numbers
* Total time :
* O(log(n) * log(n)) = O(log (n)2)
74
Carry Save Adder
A
Carry D
B save
adder E
C
* A+B+C=D+E
* Takes three numbers, and produces two numbers
75
1 bit CSA Adder
76
n-bit CSA Adder
+
𝑛
¿ ∑ ( 𝐴𝑖 +𝐵𝑖 +𝐶 𝑖)2
𝑖− 1
𝑖=1
𝑛
¿ ∑ (2 𝐷𝑖 +𝐸 𝑖 )2 𝑖 −1
𝑖=1
𝑛 𝑛
¿ ∑ 𝐷 𝑖 2 +∑ 𝐸 𝑖 2
𝑖 𝑖−1
⏟⏟
𝑖=1 𝑖=1
𝐷 𝐸
¿ 𝐷+ 𝐸 ❑
77
n-bit CSA Adder - II
* How to generate D and E ?
* Add all the corresponding sets of bits (Ai, Bi, and Ci)
independently
* set Di to the carry bit produced by adding (Ai, Bi, and Ci)
* set Ei to the sum bit produced by adding (Ai, Bi, and Ci)
* Time Complexity :
* All the additions are done in parallel
* This takes O(1) time
78
Wallace Tree Multiplier
* Basic Idea
* Generate n partial sums
* Partial sum : Pi = 0, if the ith bit in the multiplier is 0
* Pi = N << (i-1), if the the ith bit in the multiplier is 1
* Can be done in parallel : O(1) time
* Add all the n partial sums
* Use a tree based adder
79
Tree of CSA Adders
n n-1 n-2 n-3 n-4 n-5 6 5 4 3 2
P P P P P P P P P P P P
1
CSA
Carry Lookahead
Adder
Final product
80
Tree of CSA Adders
81
Time Complexity
* Time to generate all the partials sums → O(1)
* Time to reduce n partial sums to sum of two numbers
* Number of levels → O(log(n))
* Time per level → O(1)
* Total time for this stage → O(log(n))
* Last step
* Size of the inputs to the CLA adder → (2n-1) bits
* Time taken → O(log(n))
82
Outline
* Addition
* Multiplication
* Division
* Floating Point Addition
* Floating Point Multiplication
* Floating Point Division
83
THE END
84