Title: Topic 3c Integer Multiply and Divide
1Topic 3cInteger Multiply and Divide
- Introduction to Computer Systems Engineering
- (CPEG 323)
2Unsigned Integer Multiply
- Paper and pencil example
- Multiplicand 1000 Multiplier
1001 1000 0000 0000
1000 Product
01001000
3Observation
- m bits n bits mn bit product
- Stage i accumulates A 2 i if Bi 1
4How does it work?
- at each stage shift A left ( x 2)
- use next bit of B to determine whether to add in
shifted multiplicand - accumulate 2n bit partial product at each stage
5Simple Mathematics
- AB
- If B has n bits, let the product be productn
- 2iA shift A left for i times.
- It is clear that multiply is composed of
iterative - Shift and add
6Multiply hardware (V1)
Shift Left
Multiplicand
64 bits
Multiplier
64-bit ALU
32 bits
Shift Right
Write
Product
Control
64 bits
Note The multiplicand, Product and ALU are all
64-bits, while the Multiplier is 32-bits.
7Multiply Algorithm (V1)
8Observations on Multiply Version 1
- 1/2 bits in multiplicand always 0gt 64-bit adder
is wasted - 0s inserted in right of of multiplicand as
shiftedgt least significant bits of product
never changed once formed - How many cycles it takes to multiply two 32-bit
numbers ? -
9- Instead of shifting multiplicand to left, shift
product to right?
10Multiply hardware (V2)
Multiplicand
32 bits
Multiplier
32-bit ALU
32 bits
Shift right
Shift Right
Product
Control
Write
64 bits
NOTE Note Only Product are 64-bits, while the
rest are all 32-bits.
11Multiply Algorithm V2
Start
Multiplier01
Multiplier00
2. Shift the Product register right 1 bit.
3. Shift the Multiplier register right 1 bit.
32nd repetition?
No lt 32 repetitions
Yes 32 repetitions
Done
12Whats going on?
- Multiplicand stays still and product moves right
13Observations on Multiply Version 2
- Product register wastes space that exactly
matches size of multipliergt combine Multiplier
register and Product register
14Multiply hardware V3
Multiplicand
32 bits
32-bit ALU
Shift right
Product
(Multiplier)
Control
Write
64 bits
NOTE now, the multiplier does need a separate
register!!
15Multiply Algorithm V3
Start
Product0 0
Product0 1
2. Shift the Product register right 1 bit.
32nd repetition?
Note the multiplier is initially Placed in the
right ½ of the Product Register
No lt 32 repetitions
Yes 32 repetitions
Done
16Sign Multiplication
- Easiest solution is to
- make both positive
- remember whether to
- complement product when done
- That is calculate the sign of the product,
convert the operands into positive numbers, leave
out the sign bit, run for 31 steps, then fix the
result.
17Faster Algorithms for Multiplication
- Booths Algorithm
- multiply signed numbers using same hardware as
before and save cycles - can handle multiple bits at a time
- Using an array of adders
- Observation whether to add or not add a
particular shifted multiplicand a decision can
be made all in parallel ..
18Divide Paper Pencil
- 1001 Quotient
- Divisor 1000 1001010 Dividend 1000
10 101 1010 1000
10 Remainder - Dividend Quotient Divisor Remainder
19Division Algorithm
- Input Remainder register is initialized with the
dividend Divisor register is initialized in the
left half Quotient register is initialized with
zero.
Remainder register lt Subtract the Divisor
register from the Remainder register
0
lt0
Test Remainder register
Shift the Quotient register to the left and
setting the new rightmost to 1,
Remainder register lt RemainderDivisor Shift the
Quotient register to the left and setting the
new rightmost to 0
Shift the Divisor register right 1 bit
no
Repetition n1 times?
yes
20How to do Division ?
Note Dividend is NOT shifting, and Divisor is
moving right!
How to do division here ? Note Left ½ of
Divisor R is initialized with the divisor,
Remainder R is initialized with dividend,
Quotient R is initialized with 0.
Try to go over the example on P186.