1 Lecture 5Multiplication and Division ECE 0142 Computer Organization
2 Multiplication More complicated than addition –A straightforward implementation will involve shifts and adds More complex operation can lead to –More area (on silicon) and/or –More time (multiple cycles or longer clock cycle time) Let ’ s begin from a simple, straightforward method
3 Straightforward Algorithm (multiplicand) (multiplier)x
4 Straightforward Algorithm (multiplicand) (multiplier)x (partial sum)
5 Straightforward Algorithm (multiplicand) (multiplier)x (partial sum)
6 Straightforward Algorithm (multiplicand) (multiplier)x
(partial sum) Straightforward Algorithm (multiplicand) (multiplier)x
(partial sum) Straightforward Algorithm (multiplicand) (multiplier)x
9 Straightforward Algorithm (multiplicand) (multiplier)x (partial sum)
(partial sum) Straightforward Algorithm (multiplicand) (multiplier)x
11 Straightforward Algorithm (multiplicand) (multiplier)x (partial sum)
(partial sum) Straightforward Algorithm (multiplicand) (multiplier)x
13 Straightforward Algorithm (multiplicand) (multiplier)x (partial sum)
(partial sum) Straightforward Algorithm (multiplicand) (multiplier)x
15 Straightforward Algorithm (multiplicand) (multiplier)x (partial sum)
16 Straightforward Algorithm (multiplicand) (multiplier)x (partial sum)
17 Straightforward Algorithm (multiplicand) (multiplier)x (partial sum)
(partial sum) Straightforward Algorithm (multiplicand) (multiplier)x
(full sum) Straightforward Algorithm (multiplicand) (multiplier)x
20 Implementation 1
21 Example ( Implementation 1 ) Let ’ s do 0010 x 0110 (2 x 6), unsigned Iterati on Implementation 1 StepMultiplierMultiplicandProduct 0 initial values : 0 -> no op : Multiplier shift right/ Multiplicand shift left : 1 -> product = product + multiplicand : Multiplier shift right/ Multiplicand shift left : 1 -> product = product + multiplicand : Multiplier shift right/ Multiplicand shift left : 0 -> no op : Multiplier shift right/ Multiplicand shift left
22 Drawbacks The ALU is twice as wide as necessary The multiplicand register takes twice as many bits as needed The product register won’t need 2n bits till the last step –Being filled The multiplier register is being emptied during the process
23 Implementation 2 Multiplicand stationary - Multiplier right - PP right
24 Example ( Implementation 2 ) Let ’ s do 0010 x 0110 (2 x 6), unsigned Iteration Implementation 2 StepMultiplierMultiplicandProduct 0 initial values ×××× 1 1: 0 -> no op ×××× 2: Multiplier shift right/ Product shift right × ××× 2 1: 1 -> product = product + multiplicand × ××× 2: Multiplier shift right/ Product shift right ×× ×× 3 1: 1 -> product = product + multiplicand ×× ×× 2: Multiplier shift right/ Product shift right ×x× × 4 1: 0 -> no op ××× × 2: Multiplier shift right/ Product shift right ××××
25 Implementation 3 Multiplier on right half of PP reg Multiplicand stationary - Multiplier right - PP right
26 Example ( Implementation 3 ) Let ’ s do 0010 x 0110 (2 x 6), unsigned Iteration Implementation 3 StepMultiplierMultiplicandProduct|Multiplier 0 initial values : 0 -> no op : Multiplier shift right/ Product shift right × : 1 -> product = product + multiplicand × : Multiplier shift right/ Product shift right ×× : 1 -> product = product + multiplicand ×× : Multiplier shift right/ Product shift right ×× : 0 -> no op ××× : Multiplier shift right/ Product shift right ××××
27 Example (signed) Note: –Sign extension of partial product subtract –If most significant bit of multiplier is 1, then subtract → + + → + → → → + → → → → + → → →
28 Booth ’ s Encoding Three symbols to represent numbers: 1, 0, -1 -1 in 8 bits – (two ’ s complement) – (Booth ’ s encoding) 14 in 8 bits – (two ’ s complement) – (Booth ’ s encoding) Bit transitions show Booth ’ s encoding –0 to 0: 0 –0 to 1: -1 –1 to 1: 0 –1 to 0: 1 Partial results are obtained by –Adding multiplicand –Adding 0 –Subtracting multiplicand
29 Booth ’ s Algorithm Example Let ’ s do 0010 x 1101 (2 x -3) Iteration Implementation 3 StepMultiplicandProduct 0 initial values > product = product – multiplicand shift right > product = product + multiplicand shift right > product = product – multiplicand shift right > no op shift right
30 Why it works? b × (a 31 a 30 ….a 0 ) = a 0 × b × a 1 × b × … a 31 × b × 2 31 = ( 0 – a 0 ) × b × ( a 0 – a 1 ) × b × … (a 30 – a 31 ) × b × a 31 × b × 2 32 Booth's algorithm performs an addition when it encounters the first digit of a block of ones (0 1) and a subtraction when it encounters the end of the block (1 0). When the ones in a multiplier are grouped into long blocks, Booth's algorithm performs fewer additions and subtractions than the normal multiplication algorithm. ?
31 Why it works? Works for positive multiplier coz a 31 is 0 What happens for negative multipliers? a 31 is 1 –Remember that we are using 2’s complement binary b × (a 31 a 30 ….a 0 ) = b × (- a 31 × a 30 × ….a 0 ×2 0 ) Same derivation applies
32 Division
33 Integer Division Dividend = Quotient × Divisor + Remainder How to do it using paper and pencil? Q ?R ? ÷2 = 3 … 1
34 Implementation 1 64-bit wide 32-bit shift register
35 IterationStepQuotientDivisorRemainder 0 Initial values : Rem=Rem-Div b: Rem +Div, sll Q, Q 0 = : Shift Div right : Rem=Rem-Div b: Rem +Div, sll Q, Q 0 = : Shift Div right : Rem=Rem-Div b: Rem +Div, sll Q, Q 0 = : Shift Div right : Rem=Rem-Div a: Rem≥0=> sll Q, Q 0 = : Shift Div right : Rem=Rem-Div a: Rem≥0=> sll Q, Q 0 = : Shift Div right
36 Implementation 2 1. Place dividend in lower half 2. Do the division 3. Get remainder here 4. Get quotient here