COA Module6 Arithmetic
COA Module6 Arithmetic
Module 6
Computer Arithmetic
• To design arithmetic circuits for use in computers, we need to work with binary
numbers.
• How to carry out various arithmetic operations in binary?
• How to implement them efficiently in hardware?
3
Addition / Subtraction
Addition of Two Binary Digits (Bits)
• When two bits A and B are added, a sum (S) and carry (C) are generated as per
the following truth table:
5
Addition of Multi-bit Binary Numbers
6
Full Adder
Inputs Outputs A S
B FA
A B Cin S Cout Cin Cout
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0 S = A’.B’.Cin + A’.[Link]’ + A.B’Cin’ + A.B.C
0 1 1 0 1 = A B Cin
1 0 0 1 0 Cout = [Link] + [Link] + A.B + [Link]
1 0 1 0 1 = A.B + [Link] + [Link]
1 1 0 0 1
1 1 1 1 1
7
Various Implementations of Full Adder
A A C
HALF OR Cout
B B ADDER S A C
HALF
Cin B ADDER S S
8
• Delay of a full adder:
• Assume that the delay of all basic gates (AND, OR,
NAND, NOR, NOT) is δ
• Delay for Carry = 2δ
• Delay for Sum = 3δ
(AND-OR delay plus one inverter delay)
9
Parallel Adder Design
10
Ripple Carry Adder
1111110 Carry
0111111 Number A
Number B
+ 0000001
Sum S
1000000
11
An-1 Bn-1 A2 B2 A1 B1 A0 B0
FAn-1
Cn-1
…… C3
FA2
C2
FA1
C1
FA0
C0
Delay for C1 = 2δ
Cn Sn-1 S2 S1 S0 Delay for C2 = 4δ
Delay for Cn-1 = 2(n-1)δ
Delay for Cn = 2nδ
Two numbers: An-1…A2A1A0 and Bn-1…B2B1B0
Input carry: C0
Sum: Sn-1…S2S1S0 Delay for S0 = 3δ
Output carry: Cn Delay for S1 = 2δ + 3δ = 5δ
Delay for S2 = 4δ + 3δ = 7δ
Delay for Sn-1 = 2(n-1)δ + 3δ = (2n+1) δ
Delay is proportional to n
12
How to Design a Parallel Subtractor?
• Observation:
• Computing A - B is the same as adding the 2’s complement of B to A.
• 2’s complement is equal to 1’s complement plus 1.
• Let Xi = Bi’.
An-1 Xn-1 A2 X2 A1 X1 A0 X0
FAn-1
Cn-1
…… C3
FA2
C2
FA1
C1
FA0
C0 = 1
Cn Sn-1 S2 S1 S0
13
xor gate is a
controlled inverter
A Parallel Adder/Subtractor
An-1 A1 A0 Bn-1 B1 B0
ADD’ / SUB
… …
xor xor xor
Sn-1 S2 S1 S0
14
Delay of a Carry-Ripple Adder
• Simplest design: cascade full adders Ripple-carry adder: n-bit adder built
from full adders.
• Critical path goes from Cin to Cout Delay of ripple-carry adder goes
• Design full adder to have fast carry delay through all carry bits.
A4 B4 A3 B3 A2 B2 A1 B1
Cout Cin
C3 C2 C1
S4 S3 S2 S1
Dealing with the
problem of carry propagation
1. Reduce the carry propagation time.
We have seen some ways to do the former. How do we do the second one?
Motivation
Carry Completion Sensing
A=0 0 1 1 1 0 1 1 0 1 1 0 1 1 0 1
B=0 1 0 0 1 1 1 0 0 0 0 1 0 1 0 1
4 1 5 1
Can we compute the average length of carry
chain?
• What is the probability that a chain generated at position i terminates
at j?
• It terminates if both the inputs A[j] and B[j] are zero or 1.
• From i+1 to j-1 the carry has to propagate.
• p=(1/2)j-i
• So, what is the expected length?
• Define a random variable L, which denotes the length of the chain.
Can we compute the average length of carry chain? (Contd.)
( j i)2
j i 1
( j i )
( k i )2 ( k 1i )
2 2 ( k 1i )
p
[Using, l2
l 1
l
2 ( p 2)2 p ]
Carry Look-ahead Adder
• The propagation delay of an n-bit ripple carry order has been seen to be
proportional to n.
• Due to the rippling effect of carry sequentially from one stage to the next.
21
• Consider the i-th stage in the addition process.
• We define the carry generate and carry propagate functions as:
Ai Si
Gi = [Link]
Bi FA
Pi = Ai Bi
Ci Ci+1
• Gi = 1 represents the condition when a carry is generated in
stage-i independent of the other stages.
Ci+1 = Gi + [Link]
• Pi = 1 represents the condition when an input carry Ci will be
propagated to the output carry Ci+1.
22
Looking into Generate and Propagate
Spot the
difference! G[i ] A[i ].B[i ] G[i ] A[i ].B[i ]
P[i ] A[i ] B[i ] P[i ] A[i ] B[i ]
C[i ] G[i ] P[i ].C[i 1] C[i ] G[i ] P[i ].C[i 1]
S[i ] P[i ] C[i 1] S [i ] A[i ] B[i ] C[i 1]
Is it really different?
Both are correct!
i-1 i i
Ci+1 = Gi + Gk Pj + C0 Pj
k=0 j=k+1 j=0
25
Design of 4-bit CLA Adder
4 AND2 gates
C4 = G3 + G2P3 + G1P2P3 + G0P1P2P3 + C0P0P1P2P3 3 AND3 gates
C3 = G2 + G1P2 + G0P1P2 + C0P0P1P2 2 AND4 gates
1 AND5 gate
C2 = G1 + G0P1 + C0P0P1 1 OR2, 1 OR3, 1 OR4 and
C1 = G0 + C0P0 1 OR5 gate
S0 = A0 B0 C0 = P0 C0
4 XOR2 gates
S1 = P1 C1
S2 = P2 C2
S3 = P3 C3
26
Design of 4-bit CLA Adder
4 AND2 gates
C4 = G3 + G2P3 + G1P2P3 + G0P1P2P3 + C0P0P1P2P3 3 AND3 gates
2 AND4 gates
C3 = G2 + G1P2 + G0P1P2 + C0P0P1P2
1 AND5 gate
C2 = G1 + G0P1 + C0P0P1 1 OR2, 1 OR3, 1 OR4 and
1 OR5 gate
C1 = G0 + C0P0
S0 = A0 B0 C0 = P0 C0
4 XOR2 gates
S1 = P1 C1
S2 = P2 C2
S3 = P3 C3
27
Design of 4-bit CLA Adder
4 AND2 gates
C4 = G3 + C3P3 2 AND3 gates
1 AND4 gates
C3 = G2 + G1P2 + G0P1P2 + C0P0P1P2
1 AND5 gate
C2 = G1 + G0P1 + C0P0P1 1 OR2, 1 OR3, 1 OR4 and
1 OR2 gate
C1 = G0 + C0P0
S0 = A0 B0 C0 = P0 C0
4 XOR2 gates
S1 = P1 C1
S2 = P2 C2
S3 = P3 C3
28
C4
The 4-bit CLA Circuit P3
G3
C3
P2
G2
C2 P1
G1
P0
C1 G0
C0
29
c4
Four-Bit Carry-Lookahead Adder
p3
g3
Complexity
reduced by
c3
deriving the
carry-out p2
indirectly g2
Full carry lookahead is quite practical
for a 4-bit adder c2 p1
c1 = g0 c0 p0 g1
c2 = g1 g0 p1 c0 p0 p1 p0
c3 = g2 g1 p2 g0 p1 p2 c0 p0 p1 p2 c1
c4 = g3 g2 p3 g1 p2 p3 g0 p1 p2 p3 g0
c0
c0 p0 p1 p2 p3
Delay for all generate and propagate signals are 1
Four-bit carry network with
full lookahead.
gate delay, followed by another 2 gate delays for
the carry bits (total 3 gate delay). A 4-bit RCA requires 8 gate delay for carry, and 9
Sum bits are another, hence total 4 gate delays. gate delay for sum!
B3 A3 B2 A2 B1 A1 B0 A0
Gi and Pi Generator 3δ
G3 P3 G2 P2 G1 P1 G0 P0
C3 C2 C1 C0
31
Carry Lookahead Beyond 4 Bits
...
High fan-ins necessitate
32-input OR tree-structured circuits
Solution to the Fan-in Problem
High-radix addition (i.e., radix 2h)
Increases the latency for generating g and p signals and sum digits,
but simplifies the carry network (optimal radix?)
Multilevel lookahead
Either way, the carries c4, c8, and c12 are determined first
Problem: Carry propagation between modules still slows down the adder
34
Carry-Lookahead Adder Design
Block generate and propagate signals
g [i,i+3] = gi+3 gi+2 pi+3 gi+1 pi+2 pi+3 gi pi+1 pi+2 pi+3
p [i,i+3] = pi pi+1 pi+2 pi+3
g[i,i+3] p[i,i+3]
c3 ci+3
p2 pi+2
A 4-bit
carry g2 gi+2
network
p1 ci+2 pi+1
c2
g1 gi+1
p0 pi
c1 ci+1
g0 gi
ci
c0
A Two-Level Carry-Lookahead Adder
c12 c8 c4 c0
• Once the correct carry-in is known, the correct sum is selected by a multiplexer.
43
Basic building block of a
carry-select adder, with
block size of 4.
44
Uniform sized adder
• A 16-bit carry select adder with a uniform block size of 4 is shown.
• The least significant block needs a single adder (since the carry-in is known).
• Total delay is 4 full adder delays, plus 3 MUX delays.
45
Variable-sized adder
• A 16-bit carry select adder with variable block sizes of 2-2-3-4-5 is shown.
• Total delay is 2 full adder delays, plus 4 MUX delays.
46
Carry Save Adder
• Each carry save adder is simply an independent full adder without carry
propagation.
• A parallel adder is required only at the last stage.
47
• An illustrative example: X: 10011
Y: + 11001
X: 10011 X: 10011 Z: + 01011
Y: + 11001 Y: + 11001 S: 00001
Z: + 01011 Z: + 01011 C: 11011
C: 11011 S: 00001 Sum: 110111
A set of full adders generate carry The sum and carry vectors are
and sum bits in parallel added later (with proper shifting)
48
An n-bit Carry Save Adder
Xn-1 Yn-1 Zn-1 X2 Y 2 Z2 X1 Y 1 Z1 X0 Y 0 Z0
Cn-1 Sn-1 C2 S2 C1 S1 C0 S0
49
Adding m Numbers: Some
Examples CSA CSA
CSA
CSA
CSA CSA
CSA
• Many contemporary processors have a flag register that contains the status of
the last arithmetic / logic operation.
• Zero (Z): tells whether the result is zero.
• Can be used for both arithmetic and logic operations.
• Sign (S): tells whether the result is positive (=0) or negative (=1).
• Can be used for both arithmetic and logic operations.
• Carry (C): tells whether there has been a carry out of the most significant stage.
• Used only for arithmetic operations.
• Overflow (V): tells whether the result is too large to fit in the target register.
• Used only for arithmetic operations (addition and subtraction).
51
Fn-1 F1 F0
A B F
… Carry out
…
NOR
C Flag
ALU
52
• The MIPS32 processor does not have any status flags.
• Why?
• MIPS ISA is designed for efficient pipeline implementation.
• Several instructions can be in various stages of execution in the pipeline.
• Flag registers result in side effects among instructions.
• MIPS stores information about the flags temporarily in a GPR.
53
Multiplication
Multiplication of Unsigned Numbers
• Multiplication requires substantially more 1 0 1 0 Multiplicand M (10)
hardware than addition. 1 1 0 1 Multiplier Q (13)
• Multiplication of two n-bit number --------
1 0 1 0
generates a 2n-bit product.
0 0 0 0
• We can use shift-and-add method. 1 0 1 0
• Repeated additions of shifted versions of the 1 0 1 0
multiplicand. ---------------
1 0 0 0 0 0 1 0 Product P (130)
55
A General Case
A3 A2 A1 A0
• Each [Link] is called a partial
B3 B2 B1 B0
product.
----------------------
• Generating the partial
A3B0 A2B0 A1B0 A0B0
products is easy.
A3B1 A2B1 A1B1 A0B1
• Requires just an AND gate for
A3B2 A2B2 A1B2 A0B2 each partial product.
A3B3 A2B3 A1B3 A0B3 • Adding all the n-bit partial
------------------------------------- products in hardware is more
difficult.
56
Design of a Combinational Array Multiplier
57
• Extremely inefficient, and Multiplicand
of hardware. q0
0
• Requires n2 multiplication PP1 p0
q1
cells for an n x n multiplier. 0
fast. 0
PP3 p2
q3
0
,
p7 p6 p5 p4 p3
Product: p7 p6 p5 p4 p3 p2 p1 p0
58
Unsigned Sequential Multiplication
• Requires much less hardware, but requires several clock cycles to perform
multiplication of two n-bit numbers.
• Typical hardware complexity: O(n)
• Typical time complexity: O(n)
59
• In the “shift-and-add” multiplication that we discuss now, we make the following
modifications.
• We do not shift the multiplicand (i.e., keep its position fixed).
• We right shift an 2n-bit partial product at every step.
60
START
A = 0; C = 0; M: n-bit multiplicand
COUNT = n;
M = multiplicand; Q: n-bit multiplier
Q = multiplier;
A: n-bit temporary register
0 1
Q0
C: 1-bit carry out from adder
A=A+0 A=A+M
COUNT = 0? STOP
61
C A Q
Example 1: (10) x (13)
0 0 0 0 0 0 0 1 1 0 1 Initialization
Assume 5-bit numbers.
0 0 1 0 1 0 0 1 1 0 1 A = A + M Step 1
M: (0 1 0 1 0)2 0 0 0 1 0 1 0 0 1 1 0 Shift
Q: (0 1 1 0 1)2 0 0 0 1 0 1 0 0 1 1 0 A = A + 0 Step 2
0 0 0 0 1 0 1 0 0 1 1 Shift
Product = 130
= (0 0 1 0 0 0 0 0 1 0)2 0 0 1 1 0 0 1 0 0 1 1 A = A + M Step 3
0 0 0 1 1 0 0 1 0 0 1 Shift
0 1 0 0 0 0 0 1 0 0 1 A = A + M Step 4
0 0 1 0 0 0 0 0 1 0 0 Shift
0 0 1 0 0 0 0 0 1 0 0 A = A + 0 Step 5
0 0 0 1 0 0 0 0 0 1 0 Shift
62
C A Q
Example 2: (29) x (21)
0 0 0 0 0 0 1 0 1 0 1 Initialization
Assume 5-bit numbers.
0 1 1 1 0 1 1 0 1 0 1 A = A + M Step 1
M: (1 1 1 0 1)2 0 0 1 1 1 0 1 1 0 1 0 Shift
Q: (1 0 1 0 1)2 0 0 1 1 1 0 1 1 0 1 0 A = A + 0 Step 2
0 0 0 1 1 1 0 1 1 0 1 Shift
Product = 609
= (1 0 0 1 1 0 0 0 0 1)2 1 0 0 1 0 0 0 1 1 0 1 A = A + M Step 3
0 1 0 0 1 0 0 0 1 1 0 Shift
0 1 0 0 1 0 0 0 1 1 0 A = A + 0 Step 4
0 0 1 0 0 1 0 0 0 1 1 Shift
1 0 0 1 1 0 0 0 0 1 1 A = A + M Step 5
0 1 0 0 1 1 0 0 0 0 1 Shift
63
Data Path for Shift-and-Add Multiplier
C A Q Q0
Carry
out n-bit registers
ADDER
M Control Unit ..
.
MUX
64
Signed Multiplication
• We can extend the basic shift-and-add multiplication method to handle signed numbers.
• One important difference:
• Require to sign-extend all the partial products before they are added.
• Recall that for 2’s complement representation, sign extension can be done by replicating the sign bit
any number of times.
0101 = 0000 0101 = 0000 0000 0000 0101 = 0000 0000 0000 0000 0000 0000 0000 0101
1011 = 1111 1011 = 1111 1111 1111 1011 = 1111 1111 1111 1111 1111 1111 1111 1011
65
An Example: 6-bit 2’s 1 1 0 1 0 1 (-11)
complement multiplication X 0 1 1 0 1 0 (+26)
-------------------------
0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 0 1 0 1
Note: For n-bit multiplication, 0 0 0 0 0 0 0 0 0 0
since we are generating a 2n- 1 1 1 1 1 0 1 0 1
bit product, overflow can never 1 1 1 1 0 1 0 1
occur.
0 0 0 0 0 0 0
-------------------------
1 1 1 0 1 1 1 0 0 0 1 0 (-286)
66
Booth’s Algorithm for Signed Multiplication
67
Basic Idea Behind Booth’s Algorithm
• We inspect two bits of the multiplier (Qi, Qi-1) at a time.
• If the bits are same (00 or 11), we only shift the partial product.
• If the bits are 01, we do an addition and then shift.
• If the bits are 10, we do a subtraction and then shift.
• Inspecting bit pairs as mentioned can also be expressed in terms of Booth’s Encoding.
• Use the symbols +1, -1 and 0 to indicate changes w.r.t. Qi and Qi-1.
• 01 +1, 10 -1, 00 or 11 0.
• For encoding the least significant bit Q0, we assume Q -1 = 0.
68
• Examples of Booth encoding:
a) 0 1 1 1 0 0 0 0 :: +1 0 0 -1 0 0 0 0
b) 0 1 1 1 0 1 1 0 :: +1 0 0 -1 +1 0 -1 0
c) 0 0 0 0 0 1 1 1 :: 0 0 0 0 +1 0 0 -1
d) 0 1 0 1 0 1 0 1 :: +1 -1 +1 -1 +1 -1 +1 -1
• The last example illustrates the worst case for Booth’s multiplication (alternating 0’s
and 1’s in multiplier).
• In the illustrations, we shall show the two multiplier bits explicitly instead of showing the
encoded digits.
69
START
A = 0; Q -1 = 0; M: n-bit multiplicand
COUNT = n;
M = multiplicand; Q: n-bit multiplier
Q = multiplier;
A: n-bit temporary register
01 10
Q0Q -1
Q -1: 1-bit flip-flop
A=A+M 00 or A=A–M
11
Arithmetic Shift Right (A, Q , Q -1) Skips over consecutive 0’s
COUNT = COUNT – 1; and 1’s of the multiplier Q
COUNT = 0? STOP
70
A Q Q -1
Example 1: (-10) x (13)
0 0 0 0 0 0 1 1 0 1 0 Initialization
Assume 5-bit numbers.
0 1 0 1 0 0 1 1 0 1 0 A = A - M Step 1
M: (1 0 1 1 0)2 0 0 1 0 1 0 0 1 1 0 1 Shift
-M: (0 1 0 1 0)2 1 1 0 1 1 0 0 1 1 0 1 A = A + M Step 2
Q: (0 1 1 0 1)2 1 1 1 0 1 1 0 0 1 1 0 Shift
Product = -130 0 0 1 1 1 1 0 0 1 1 0 A = A - M Step 3
= (1 1 0 1 1 1 1 1 1 0)2 0 0 0 1 1 1 1 0 0 1 1 Shift
0 0 0 0 1 1 1 1 1 0 1 Shift Step 4
1 0 1 1 1 1 1 1 0 0 1 A = A + M
Step 5
1 1 0 1 1 1 1 1 1 0 0 Shift
71
A Q Q -1
Example 2:
0 0 0 0 0 0 0 1 1 1 0 0 0 Initialization
(-31) x (28)
0 0 0 0 0 0 0 0 1 1 1 0 0 Shift Step 1
Assume 6-bit
numbers. 0 0 0 0 0 0 0 0 0 1 1 1 0 Shift Step 2
M: (1 0 0 0 0 1)2 0 1 1 1 1 1 0 0 0 1 1 1 0 A = A - M Step 3
-M: (0 1 1 1 1 1)2 0 0 1 1 1 1 1 0 0 0 1 1 1 Shift
Q: (0 1 1 1 0 0)2
0 0 0 1 1 1 1 1 0 0 0 1 1 Shift Step 4
Product = -868 0 0 0 0 1 1 1 1 1 0 0 0 1 Shift Step 5
= (1 1 0 0 1 0
0 1 1 1 0 0)2 1 0 0 1 0 0 1 1 1 0 0 0 1 A = A + M Step 6
1 1 0 0 1 0 0 1 1 1 0 0 0 Shift
72
Arithmetic Data Path for Booth’s Algorithm
shift right
An-1 A Q Q0 Q- -1
n-bit registers
SUBTRACT
ADD /
Control Unit ..
M .
Add / Subtract
73
Design of Fast Multiplier
74
Original Booth-coded Pair Equivalent Recoded Pair
(+1, 0) (0, +2) • Every equivalent recoded pair
has at least one 0.
(-1, +1) (0, -1) • Worst-case number of
(0, 0) (0, 0) additions or subtractions is
50% of the number of
(0, 1) (0, 1) multiplier bits.
(+1, 1) -- • Reduces the worst-case time
required for multiplication.
(+1, -1) (0, +1)
(-1, 0) (0, -2)
75
Example: (+13) X (-22) in 6-bits.
Original: Multiplier -- 1 0 1 0 1 0
Booth: Multiplier -- -1 +1 -1 +1 -1 0
Recoded: Multiplier -- 0 -1 0 -1 0 -2
0 0 1 1 0 1
. -1 . -1 . -2
• M = 0 0 1 1 0 1 (+13)
--------------------------
1 1 1 1 1 1 1 0 0 1 1 0
• -1 * M = 1 1 0 0 1 1
1 1 1 1 1 1 0 0 1 1 • -2 * M = 1 0 0 1 1 0
1 1 1 1 0 0 1 1
--------------------------
1 1 0 1 1 1 1 0 0 0 1 0
76
b) Carry Save Multiplier
• We have seen earlier how carry save adders (CSA) can be used to add several numbers with
carry propagation only in the last stage.
• The partial products can be generated in parallel using n2 AND gates.
• The n partial products can then be added using a CSA tree.
• Instead of letting the carries ripple through during addition, we save them and feed it to the
next row, at the correct weight positions.
77
4 x 4 Carry
Save
Multiplier
p7 p6 p5 p4 p3 p2 p1 p0
78
• Wallace Tree Multiplier
• A Wallace tree is a circuit that reduces the problem of summing n n-bit numbers to the problem
of summing two Θ(n)-bit numbers.
• It uses n/3 (floor of) carry-save adders in parallel to convert the sum of n numbers to the sum of
2n/3 (ceiling of) numbers.
• It then recursively constructs a Wallace tree on the 2n/3 (ceiling of) resulting numbers.
• The set of numbers is progressively reduced until there are only two numbers left.
• By performing many carry-save additions in parallel, Wallace trees allow two n-bit numbers to be
multiplied in Θ(log2 n) time using a circuit of size Θ(n2).
79
m(0) m(1) m(2) m(3) m(4) m(5) m(6) m(7)
8 9 10 11 12 13 14 15
• The figure shows a Wallace tree that adds 8
CSA CSA
partial products m(0), m(1), …, m(7).
• The partial product m(i) consists of (n + i) 10 10 13 13
bits.
CSA CSA
• Each line represents an entire number – 13 15
13
the label of an edge indicates the number
of bits. CSA 15
• The carry-lookahead adder at the bottom 15 15
adds a (2n-1)-bit number to a 2n-bit
number to give the 2n-bit product. CSA
15 15
Parallel Adder
Product 16
80
Division
Introduction
82
• Latency:
• Minimum delay after which the first result is obtained, starting from the time when the first set of
inputs is applied.
• Cycles/Issue:
• Whenever a new set of inputs is applied to a functional unit (e.g. adder), it is called an issue.
• Pipelined implementation of arithmetic unit reduces the number of clock cycles between successive
issues.
• For non-pipelined arithmetic units (e.g. divider), the number of clock cycles between successive
issues is much higher.
• Next input can be applied only after the previous operation is complete.
83
The Process of Integer Division
84
• One of the simplest division methods is the sequential digit-by-digit algorithm similar to that
used in pencil-and-paper methods.
0 1 1 0 Quotient Q = Q0Q1Q2Q3
Divisor M 1 1 0 1 0 0 1 0 1 Dividend D = R0
1 1 0 Q0.M (Does not go; Q0 = 0)
-------------
1 0 0 1 0 1 R1
D = 37 = (1 0 0 1 0 1)2 - 1 1 0 Q1.2-1.M (Does go; Q1 = 1)
M = 6 = (1 1 0)2 -------------
0 1 1 0 1 R2
Quotient Q = 6
- 1 1 0 Q2.2-2.M (Does go; Q2 = 1)
Remainder R = 1
-------------
0 0 0 1 R3
1 1 0 Q3.2-3.M (Does not go; Q3 = 0)
-------------
0 0 1 R4 = Remainder R
85
• In the example, the quotient Q = Q0Q1Q2… is computed one bit at a time.
• At each step i, the divisor shifted i bits to the right (i.e. 2-i.M) is compared with the current partial
remainder Ri.
• The quotient bit Qi is set to 0 (1) if 2-i.M is greater than (less than) Ri .
• The new partial remainder Ri+1 is computed as:
Ri+1 = Ri - Qi.2-i.M
86
• Machine implementation:
• For hardware implementation, it is more convenient to shift the partial remainder to the left
relative to a fixed divisor; thus
Ri+1 = 2Ri - Qi.M (instead of Ri+1 = Ri - Qi.2-i.M)
• The final partial remainder is the required remainder shifted to the left, so that R = 2-3.R4
(see next slide).
87
Divisor M Quotient Q
1 1 0 1 0 0 1 0 1 Dividend = 2R0
1 1 0 Q0.M 0
Do not ---------------
subtract 1 0 0 1 0 1 R1
1 0 0 1 0 1 0 2R1
1 1 0 Q1.M 0 1
D = 37 = (1 0 0 1 0 1)2 ---------------
0 1 1 0 1 0 R2
M = 6 = (1 1 0)2
0 1 1 0 1 0 0 2R2
Quotient Q = 6 1 1 0 Q2.M 0 1 1
Remainder R = 1 ---------------
0 0 0 1 0 0 R3
0 0 0 1 0 0 0 2R3
1 1 0 Q3.M 0 1 1 0
---------------
0 0 1 0 0 0 R4 = 23.R
88
Two alternatives to division
Control Unit ..
Divisor M .
Add / Subtract
90
Basic Steps (Restoring Division)
Repeat the following steps n times:
a) Shift the dividend left one bit at a time into register A.
b) Subtract the divisor M from this register A (trial subtraction).
c) If the result is negative (i.e. not going):
• Add the divisor M back into the register A (i.e. restoring back).
• Record 0 as the next quotient bit.
d) If the result is positive:
A Q
• Do not restore the intermediate result.
• Record 1 as the next quotient bit.
M
91
START
Restoring Division
A = 0; M = divisor; • Quotient in Q
Q = dividend; COUNT = n • Remainder in A
Shift left A, Q
A Q
A=A–M Trial subtraction
No Yes M
A –ve ?
Q0=1 Q0=0
A=A+M Restoration No
Yes
COUNT = COUNT – 1 COUNT = 0? STOP
92
• Analysis:
• For n-bit divisor and n-bit dividend, we iterate n times.
• Number of trial subtractions: n
• Number of restoring additions: n/2 on the average
• Best case: 0
• Worst case: n
93
A Simple Example: 8/3 for 4-bit representation (n=4)
Initially: 0 0 0 0 0 1 0 0 0 Shift: 0 0 1 0 0 0 0 0 –
Subtract: 0 0 1 1
Shift: 0 0 0 0 1 0 0 0 – Set Q0: 0 0 0 0 1
Subtract: 0 0 1 1 0 0 0 0 0 0 0 0 1
Set Q0: 1 1 1 1 0 Shift: 0 0 0 1 0 0 0 1 -
Restore: 0 0 1 1 Subtract: 0 0 1 1
0 0 0 0 1 0 0 0 0 Set Q0: 1 1 1 1 1
Shift: 0 0 0 1 0 0 0 0 – Restore: 0 0 1 1
Subtract: 0 0 1 1 0 0 0 1 0 0 0 1 0
Set Q0: 1 1 1 1 1
Remainder Quotient
Restore: 0 0 1 1
00010 = 2 0010 = 2
0 0 0 1 0 0 0 0 0
94
(b) Non-Restoring Division A Q
95
• Basic steps in non-restoring division:
a) Start by initializing register A to 0, and repeat steps (b)-(d) n times.
b) If the value in register A is positive,
• Shift A and Q left by one bit position.
• Subtract M from A.
c) If the value in register A is negative,
• Shift A and Q left by one bit position.
• Add M to A.
d) If A is positive, set Q0 = 1; else, set Q0 = 0.
e) If A is negative, add M to A as a final corrective step.
96 96
START
Non-Restoring Division A Q
A = 0; M = divisor;
Q = dividend; COUNT = n M
No A -ve ? Yes No
Shift left A, Q Shift left A, Q COUNT = 0?
A=A–M A=A+M
Yes
No
No Yes A<0?
Q0 = 1 A -ve ? Q0 = 0
Yes
Correction
97
A Simple Example: 8/3 for n=4
Initially: 0 0 0 0 0 1 0 0 0 Shift: 0 0 0 1 0 0 0 1 –
Subtract: - 0 0 1 1
Shift: 0 0 0 0 1 0 0 0 – Set Q0: 1 1 1 1 1 0 0 1 0
Subtract: - 0 0 1 1 Correction Add: Quotient
Set Q0: 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0010 = 2
Shift: 1 1 1 0 0 0 0 0 – 0 0 0 1 1
0 0 0 1 0
Add: 0 0 1 1
Set Q0: 1 1 1 1 1 0 0 0 0 Remainder
00010 = 2
Shift: 1 1 1 1 0 0 0 0 –
Add: 0 0 1 1
Set Q0: 0 0 0 0 1 0 0 0 1
98
Data Path for Non-Restoring Division
Control Unit ..
M .
Add / Subtract
99
High Speed Dividers
• Some of the methods used to increase the speed of multiplication can also be
modified to speed up division.
• High-speed addition and subtraction.
• High-speed shifting.
• Combinational array divider (implementing restoring division).
100
Floating-Point Numbers
Representing Fractional Numbers
102
Some Examples
1011.1 1x23 + 0x22 + 1x21 + 1x20 + 1x2-1 = 11.5
101.11 1x22 + 0x21 + 1x20 + 1x2-1 + 1x2-2 = 5.75
10.111 1x21 + 0x20 + 1x2-1 + 1x2-2 + 1x2-3 = 2.875
Some Observations:
• Shift right by 1 bit means divide by 2
• Shift left by 1 bit means multiply by 2
• Numbers of the form 0.111111…2 has a value less than 1.0 (one).
103
Limitations of Representation
• In the fractional part, we can only represent numbers of the form x/2k exactly.
• Other numbers have repeating bit representations (i.e. never converge).
• Examples:
3/4 = 0.11
7/8 = 0.111 • More the number of bits, more
5/8 = 0.101 accurate is the representation.
1/3 = 0.10101010101 [01] …. • We sometimes see: (1/3)*3 ≠ 1.
1/5 = 0.001100110011 [0011] ….
1/10 = 0.0001100110011 [0011] ….
104
Floating-Point Number Representation (IEEE-754)
• For representing numbers with fractional parts, we can assume that the fractional point
is somewhere in between the number (say, n bits in integer part, m bits in fraction
part). Fixed-point representation
• Lacks flexibility.
• Cannot be used to represent very small or very large numbers
(for example: 2.53 x 10-26, 1.7562 x 10+35, etc.).
105
F = (-1)s M x 2E
• s is the sign bit indicating whether the number is negative (=1) or positive (=0).
• M is called the mantissa, and is normally a fraction in the range [1.0,2.0].
• E is called the exponent, which weights the number by power of 2.
Encoding:
• Single-precision numbers: total 32 bits, E 8 bits, M 23 bits
• Double-precision numbers: total 64 bits, E 11 bits, M 52 bits
s E M
106
Points to Note
• The number of significant digits depends on the number of bits in M.
• 7 significant digits for 24-bit mantissa (23 bits + 1 implied bit).
• The range of the number depends on the number of bits in E.
• 1038 to 1038 for 8-bit exponent.
107
“Normalized” Representation
• We shall now see how E and M are actually encoded.
• Assume that the actual exponent of the number is EXP
(i.e. number is M x 2EXP).
• Permissible range of E: 1 ≤ E ≤ 254 (the all-0 and all-1 patterns are not allowed).
• Encoding of the exponent E:
The exponent is encoded as a biased value: E = EXP + BIAS
where BIAS = 127 (28-1 – 1) for single-precision, and
BIAS = 1023 (211-1 – 1) for double-precision.
108
• Encoding of the mantissa M:
• The mantissa is coded with an implied leading 1 (i.e. in 24 bits).
M = 1 . xxxx...x
• Here, xxxx…x denotes the bits that are actually stored for the mantissa. We get the extra
leading bit for free.
• When xxxx…x = 0000…0, M is minimum (= 1.0).
• When xxxx…x = 1111…1, M is maximum (= 2.0 – ε).
109
An Encoding Example
110
Another Encoding Example
111
Special Values Zero is represented by the
all-zero string.
• When E = 000…0
Also referred to as de-
• M = 000…0 represents the value 0.
normalized numbers.
• M ≠ 000…0 represents numbers very close to 0.
• When E = 111…1
• M = 000…0 represents the value ∞ (infinity).
NaN represents cases
when no numeric value
• M ≠ 000…0 represents Not-a-Number (NaN).
can be determined, like
uninitialized values, ∞*0,
∞-∞, square root of a
negative number, etc.
112
Summary of Number Encodings
-Normalized -Denorm +Denorm +Normalized +
NaN 0 +0 NaN
Denormal numbers have very small magnitudes (close to 0) such that trying to
normalize them will lead to an exponent that is below the minimum possible value.
• Mantissa with leading 0’s and exponent field equal to zero.
• Number of significant digits gets reduced in the process.
113
Rounding
• Suppose we are adding two numbers (say, in single-precision).
• We add the mantissa values after shifting one of them right for exponent alignment.
• We take the first 23 bits of the sum, and discard the residue R (remaining bits).
114
• To implement rounding, two temporary bits are maintained:
• Round Bit (r): This is equal to the MSB of the residue R.
• Sticky Bit (s): This the logical OR of the rest of the bits of the residue R.
115
Some Exercises
116
Floating-Point Arithmetic
Floating Point Addition/Subtraction
• Two numbers: M1 x 2E1 and M2 x 2E2 , where E1 > E2 (say).
• Basic steps:
• Select the number with the smaller exponent (i.e. E2) and shift its mantissa right by (E1-E2) positions.
• Set the exponent of the result equal to the larger exponent (i.e. E1).
• Carry out M1 ± M2, and determine the sign of the result.
• Normalize the resulting value, if necessary.
118
Addition Example
• Suppose we want to add F1 = 270.75 and F2 = 2.375
F1 = (270.75)10 = (100001110.11)2 = 1.0000111011 x 28
F2 = (2.375)10 = (10.011)2 = 1.0011 x 21
• Shift the mantissa of F2 right by 8 – 1 = 7 positions, and add:
1000 0111 0110 0000 0000 0000
1 0011 0000 0000 0000 0000 000
1000 1000 1001 0000 0000 0000 0000 000
• Result: 1.00010001001 x 28
Residue
119
Subtraction Example
• Suppose we want to subtract F2 = 224 from F1 = 270.75
F1 = (270.75)10 = (100001110.11)2 = 1.0000111011 x 28
F2 = (224)10 = (11100000)2 = 1.111 x 27
• Shift the mantissa of F2 right by 8 – 7 = 1 position, and subtract:
1000 0111 0110 0000 0000 0000
111 0000 0000 0000 0000 0000 000
0001 0111 0110 0000 0000 0000 000
• For normalization, shift mantissa left 3 positions, and decrement E by 3.
• Result: 1.01110110 x 25
120
121
Floating-Point Multiplication
122
Multiplication Example
123
s1 E1 M1 s2 E2 M2
23
8 23
1 1
8
8-bit Adder 24 x 24 Multiplier
9 1111111
s1 s2 48
9-bit Subtractor
8
XOR
Normalizer
8 23
s3 E3 M3
124
Floating-Point Division
125
Division Example
126
s1 E1 M1 s2 E2 M2
23
8 23
1 1
8
8-bit Subtractor 24-bit Divider
9 1111111
s1 s2 48
9-bit Adder
8
XOR
Normalizer
8 23
s3 E3 M3
127
FLOATING-POINT ARITHMETIC in MIPS32
128
• The MIPS32 architecture defines the following floating-point registers (FPRs).
• 32 32-bit floating-point registers F0 to F31, each of which is capable of storing a single-
precision floating-point number.
• Double-precision floating-point numbers can be stored in even-odd pairs of FPRs (e.g.,
(F0,F1), (F10,F11), etc.).
129
F0
F1 FIR
F2 FCCR
F3 FEXR
F4 FENR
FPRs
F5 FCSR
..
. Special-purpose
Registers
F30
F31
130
Typical Floating Point Instructions in MIPS32
131
• Arithmetic instructions
• Floating-point absolute value
• Floating-point compare
• Floating-point negate
• Floating-point add
• Floating-point subtract
• Floating-point multiply
• Floating-point divide
• Floating-point square root
• Floating-point multiply add
• Floating-point multiply subtract
132
• Rounding instructions:
• Floating-point truncate
• Floating-point ceiling
• Floating-point floor
• Floating-point round
• Format conversions:
• Single-precision to double-precision
• Double-precision to single-precision
133
Example: Add a scalar s to a vector A
134