15
Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Embed Size (px)

Citation preview

Page 1: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Multiplication

CPSC 252 Computer Organization

Ellen Walker, Hiram College

Page 2: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Multiplication

• Multiplication result needs twice as many bits– E.g. 7*7 = 49 (7 is 3 bits, 49 is 6 bits)

• Multiplication is more complex than addition/subtraction– Develop algorithm, implement using

simpler hardware

Page 3: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Multiplication Algorithms

• Repeated addition– Easy to implement in software– Can only multiply positive numbers directly– Time depends on magnitude of operands

• Shift-and add– Efficient use of hardware– Time depends only on width of operands– No special case for negative numbers

Page 4: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Shift and Add Algorithm

• For each bit in multiplier– If bit = 1, add multiplicand to result– Shift multiplicand left by 1

• This is the “usual” multiplication algorithm you learned many years ago (but simpler in binary)

Page 5: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Shift and Add example

10101 21

X 101 5

---------

10101

000000 (included for completeness)

+ 1010100

------------------

1101001 105 (1+8+32+64)

Page 6: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Hardware to Multiply

• 64-bit register for multiplicand– 32 bits for original value– Option to shift left 32 times

• 64-bit ALU– Add shifted multiplicand to product

• 32-bit register for multiplier– Shift right after each step (low bits fall off)

• Control hardware

Page 7: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Multiplication: Implementation

DatapathControl

MultiplicandShift left

64 bits

64-bit ALU

ProductWrite

64 bits

Control test

MultiplierShift right

32 bits

32nd repetition?

1a. Add multiplicand to product and

place the result in Product register

Multiplier0 = 01. Test

Multiplier0

Start

Multiplier0 = 1

2. Shift the Multiplicand register left 1 bit

3. Shift the Multiplier register right 1 bit

No: < 32 repetitions

Yes: 32 repetitions

Done

Page 8: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

How Long Does it Take?

• 32 shift/add cycles

• Each cycle has 3 steps (add, shift left, shift right)

• Without parallelism, that’s 96 cycles per multiply

Page 9: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Improvements

• Save hardware by using 1 64-bit register for the product and multiplier– Left part is product (so far)– Right part is unused multiplier– Add a bit to the product and lose a bit from the

multiplier each cycle– Shift product right & add instead of shift

multiplicand left & add!

• Shift and add in the same cycle (in parallel)• This revision requires only 32 cycles

Page 10: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Final Version

Multiplicand

32 bits

32-bit ALU

ProductWrite

64 bits

Controltest

Shift right

32nd repetition?

Product0 = 01. Test

Product0

Start

Product0 = 1

3. Shift the Product register right 1 bit

No: < 32 repetitions

Yes: 32 repetitions

Done

What goes here?

•Multiplier starts in right half of product

Page 11: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Parallel Multiplication

• Instead of 32 cycles, use 32 adders

• Each adds 0 or multiplicand

• Arrange in tree so results cascade properly

• Result pulls each bit from the appropriate adder

Page 12: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Parallel Multiplication (cont)

Page 13: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

010101 x 000101 (6 bits) 000000 000101 (initial product)

+010101 Rightmost bit is 1, add multiplicand

010101 000101

0010101 00010 Shift product right

00010101 0001 Shift product right

+010101 Rightmost bit is 1, add multiplicand

01101001 0001

001101001 000 Shift product right

0001101001 00 Shift product right

00001101001 0 Shift product right

000001101001 Shift product right, answer is 105

Page 14: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

Negative numbers

• Convert to positive, multiply, then negate if initial signs were different

• Or, consider 3 cases:– Both negative: convert to positive and

multiply– One negative: make multiplier positive,

multiplicand negative– Negative multiplicand: when high bit is 1,

shift in 1’s (shift right arithmetic)

Page 15: Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College

MIPS multiplication

• 2 32-bit registers: Hi and Lomflo $s0 move from lo (to

$s0)

mfhi $s0 move from hi (to $s0)

• Multiplication Operationsmult $s0, $s1 {Hi,Lo}= $s0 x $s1

multu $s0, $s1 unsigned version