Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Multipliers and Dividers

Sachin Gajjar
sachin.gajjar@nirmauni.ac.in
Reading Material
• Chapter 11 Multipliers and Dividers
• Book - Computer Architecture from Microprocessor to Super
Computer by Behrooz Parahami, Oxford Publications
Use of Multipliers and Dividers
• Multiplication and division are used in:
• Numerical Computation
• Data encryption for privacy/ security
• Image compression methods
• Graphic rendering etc.
• Multipliers and Dividers are essential for most of the processors
Pencil and Paper multiplication

(Partial Product)

(Partial Product Shifted (in computer terminology) and Added)

(Partial Product Shifted and Added)

(Partial Product Shifted and Added)


Pencil and Paper multiplication
• Becomes much simpler for binary numbers

(Partial Product)
(Partial Product Shifted and Added)
(Partial Product Shifted and Added)
(Partial Product Shifted and Added)
Shift-Add Multiplication
• Simplest machine multipliers are
designed to follow a variant of
pencil-and-paper multiplication
algorithm Multiplicand x
Multiplier y
• Each row of dots in partial
products bit-matrix is either all 0s y0 x 20
Partial y x 21
(if the corresponding yi = 0) or the products 1
y2 x 22
same as x (if yi = 1) bit-matrix
y3 x 23
• For k x k multiplication manually, Product z
we form all of k partial products
Multiplication of 4-bit numbers in dot notation.
and add the resulting k numbers to
obtain the product p
Shift-Add Multiplication
• For machine execution:
• a cumulative partial product is initialized to z(0) = 0, (Accumulator)
• each row of bit-matrix added to it as the corresponding term (partial product of each stage) is generated,
• result of addition shifted to right by one bit to achieve proper alignment with the next term as machine
adds two numbers at a time
• the recurrence equation describing the process is:
z(j+1) = (z(j) + yj x 2k) 2–1 with z(0) = 0 and z(k) = z
|––– add –––|
|–– shift right ––|

Multiplicand x
Multiplier y
y0 x 20
Partial y x 21
products 1
y2 x 22
bit-matrix
y3 x 23
Product z
Decimal Multiplication – When variant of Pen and Paper method is implemented in machine

Position 7 6 5 4 3 2 1 0 Position 7 6 5 4 3 2 1 0
========================= =========================
x2 4 1 x0 Multiplier
1 0 x104 3 5 2 8 SL by 4
Multiplicand
y 0 0 1 1 y 4 0 6 7
3528 x 4067
========================= =========================
z (0) 0 0 0 0 z (0) 0 0 0 0 Partial Product is initialized to 0
+y 4
x2 x 4 digit
1 0 1 0 +y0x10 4 2 4 6 9 6
4 0digit i.e. 7 x 3528
–––––––––––––––––––––––––– ––––––––––––––––––––––––––
2z (1) 0 1 0 1 0 10z (1) 2 4 6 9 6 z0+y0x104
zResultant product
(1) 0 1 0 1will0 be 8 z (1) 0 2 4 6 9 6 Shift right by 1 (i.e. 10z1/10)
+y 4 +y1x104 2 1 1 6 8
1 x2
digits 1 0 1 0 i.e. 6 x 3528
–––––––––––––––––––––––––– ––––––––––––––––––––––––––
2z (2) 0 1 1 1 1 0 10z (2) 2 3 6 3 7 6
z (2) 0 1 1 1 1 0 z (2) 2 3 6 3 7 6 Shift right by 1 (i.e. 10z2/10)
+y2x2 4 0 0 0 0 +y2x104 0 0 0 0 0 i.e. 0 x 3528
–––––––––––––––––––––––––– ––––––––––––––––––––––––––
2z (3) 0 0 1 1 1 1 0 10z (3) 0 2 3 6 3 7 6
z (3) 0 0 1 1 1 1 0 z (3) 0 2 3 6 3 7 6
+y3x2 4 0 0 0 0 +y3x10 4 1 4 1 1 2
–––––––––––––––––––––––––– ––––––––––––––––––––––––––
2z (4) 0 0 0 1 1 1 1 0 10z (4) 1 4 3 4 8 3 7 6
z (4) 0 0 0 1 1 1 1 0 z (4) 1 4 3 4 8 3 7 6
========================= =========================
(a) Binary (b) Decimal
Binary Multiplication – When variant of Pen and Paper method is implemented in machine
Position 7 6 5 4 3 2 1 0 Position 7 6 5 4 3 2 1 0
========================= =========================
x24 1 0 1 0 SL 4by 4
x10 3 5 2 8
Multiplicand x Multiplier y 0 0 1 1 y 4 0 6 7
1010 x 0011 = 11110 ========================= =========================
z (0) 0 0 0 0 Partial Product is initialized
z (0) to 0 0 0 0 0
10 D x 3 D = 30 D +y0x2 4 1 0 1 0 i.e. 1 x +y 0x10
1010 4 2 4 6 9 6
4 bit x 4 bit –––––––––––––––––––––––––– ––––––––––––––––––––––––––
0+y x24 , extra bit 0 for carry generated through addition
2z (1) 0 1 0 1 0 z 0 10z (1) 2 4 6 9 6
Resultant product will be (1) (1)1/2)
z 0 1 0 1 0 Shift right by 1 (i.e.z2z 0 2 4 6 9 6
8 bits +y1x2 4 1 0 1 0 i.e. 1 x+y 4
1 x10
1010 2 1 1 6 8
–––––––––––––––––––––––––– ––––––––––––––––––––––––––
2z (2) 0 1 1 1 1 0 10z (2) 2 3 6 3 7 6
z (2) 0 1 1 1 1 0 z (2) 2 3 6 3 7 6
+y2x2 4 0 0 0 0 +y2x10 4 0 0 0 0 0
–––––––––––––––––––––––––– ––––––––––––––––––––––––––
2z (3) 0 0 1 1 1 1 0 10z (3) 0 2 3 6 3 7 6
z (3) 0 0 1 1 1 1 0 z (3) 0 2 3 6 3 7 6
+y3x2 4 0 0 0 0 +y3x10 4 1 4 1 1 2
–––––––––––––––––––––––––– ––––––––––––––––––––––––––
2z (4) 0 0 0 1 1 1 1 0 10z (4) 1 4 3 4 8 3 7 6
z (4) 0 0 0 1 1 1 1 0 z (4) 1 4 3 4 8 3 7 6
========================= =========================
Shift – Add Multiplication – the actual implementation
Shift-Add
Operation
Shift-Add Operation for signed numbers
• Signed-magnitude numbers can be multiplied by using the shift-add
algorithm to multiply their magnitudes
• and separately deriving sign of product as XOR of multiplicand and
multiplier sign bits (If they are alike, sign of product is positive else
negative)
Hardware
Implementation
Flowchart
Shift-Add Operation
Shift-Add Operation through programming

• On machines that do not have a hardware-supported multiply


instruction, multiplication can be performed in software by means of
the shift-add algorithm
• Such a program can be written for MiniMIPS
shamu: multiply_inc:
lui $s0, 211 # multiplicand M sll $s0, $s0, 1
lui $s1, 211 # multiplier Q addi $t1, $t1, 1 # incrementing the
lui $s2, 0 # to store result counter for cycles of operation
lui $s3, 1 # Mask for extracting bit of Q j multiply
lui $t1, 0 # Counter for keeping track of cycles of
operation exit:
add $a0, $s2, $zero # put result in
multiply: accumulator
beq $t1, 31, exit # cycles in operation = 32 jr ra
and $t0, $s1, $s3 # checking Q0 bit
sll $s3, $s3, 1 # for checking the next bit of Q
beq $t0, 0, multiply_inc # only shift to be
performed if q bit is zero
add $s2, $s2, $s0 # q bit is one, so add and then
shift
Multiplication with constant
(when a multiplicand x is to be multiplied by a constant multiplier a)
• Multiplication by a power of 2 can be done through shifting, which is faster
and more efficient than a full-blown multiply instruction
• Multiplication by other small constants can also be performed through shift
and add instructions
• For example, to compute 5x, one can form 4x by a left-shift instruction and
then add x to the result (uses a shifter and an adder)
• On many machines, one shift and one add instruction take less time than a multiply
instruction
• Here instead of running multiply algorithm/using hardware multiplier operation is
performed using a shifter and an adder
• Most modern compilers are smart enough to avoid spewing multiply
instructions when a short sequence of add/shift instructions can accomplish
the same goal
Z=
X=
=Y
=S
Shift-Subtract Division
• Dividers are designed as variant of
pencil and-paper division algorithm y Quotient
x Divisor
• For 2k/k division manually, z Dividend
• Initially Partial remainder = Dividend Z y 3 x 23
• form subtracted terms one at a time by Subtracted y 2 x 22
finding value of next quotient digit, bit-matrix y 1 x 21
y 0 x 20
• subtract appropriate term (0 or a
suitably shifted version of x) from partial s Remainder
remainder, and proceed until all k bits of
quotient y have been determined Division of an 8-bit number by a 4-bit number in dot
• Each row of dots in subtracted bit matrix notation.
(partial remainder) is all 0s (if
corresponding yi = 0) or x (if yi = 1) z(j) = 2z(j-1) - yk-j x 2k with z(0) = z and z(k) = 2k s
• At this time, the partial remainder | shift|
becomes final remainder s |–– subtract ––|
Z=Dividend = 0111 0101 (117D) Z/Y->Y, S
X=Divisor = 1010 (10D)
Y=Quotient 2 z(j) has one extra digit at the left end (position 8) to avoid
S=Remainder losing the additional digit (which may be 1) created by
doubling (shifting left)
Position 7 6 5 4 3 2 1 0 Position –1 –2 –3 –4 –5 –6 –7 –8
========================= ==========================
z 0 1 1 1 0 1 0 1 z .1 4 3 5 1 5 0 2
x2 4 1 0 1 0 (X is SLx by 4) .4 0 6 7
========================= ==========================
z (0) 0 1 1 1 0 1 0 1 0 z (0) .1 4 3 5 1 5 0 2
(0) (2*Z i.e Z is(0)SL by 1)
2z 0 1 1 1 0 1 0 1 10z 1 . 4 3 5 1 5 0 2
–y3x2 4 1 0 1 0 y3=1 (01110>1010, –y–1xhence y31=1) .2 2 0 1 y–1=3
–––––––––––––––––––––––––– –––––––––––––––––––––––––––
3 4
z (1) 0 1 0 0 1 0 1 (2z-y x2 ) z (1) .2 1 5 0 5 0 2
2z (1) 0 1 0 0 1 0 1 (Z is SL by 1) 10z (1) 2.1 5 0 5 0 2
–y2x2 4 0 0 0 0 y2=0 –y–2 x y2=0) 2.0 3 3 5 y–2=5
–––––––––––––––––––––––––– (01001<1010, hence
–––––––––––––––––––––––––––
z (2) 1 0 0 1 0 1 z (2) .1 1 7 0 0 2
2z (2) 1 0 0 1 0 1 (Z is SL by 1) 10z (2) 1.1 7 0 0 2
–y1x2 4 1 0 1 0 y1=1 –y–3x
(10010>1010, hence 0y3.=1)
8 1 3 4 y–3=2
–––––––––––––––––––––––––– –––––––––––––––––––––––––––
z (3) 1 0 0 0 1 z (3) .3 5 6 6 2
2z (3) 1 0 0 0 1 10z (3) 3.5 6 6 2 z(j) = 2z(j-1) - yk-j x 2k with z(0) = z and z(k) = 2k s
–y0x24 1 0 1 0 y0=1 –y–4x 3.2 5 3 6 y–4=8 | shift|
–––––––––––––––––––––––––– –––––––––––––––––––––––––––
z (4) 0 1 1 1 z (4) .3 1 2 6 |–– subtract ––|
s 0 1 1 1 s .0 0 0 0 3 1 2 6
y 1 0 1 1 y .3 5 2 8
========================= ==========================
Division of signed numbers
• When dividing signed numbers, as per recurrence equation remainder s is defined
to have same sign as dividend z and a magnitude that is less than lx|
• Consider following examples of integer division with all possible combinations of
signs for z and x:
Dividend Divisor Quotient Remainder
z=5 x=3  y=1 s=2
z=5 x = –3  y = –1 s=2
z = –5 x=3  y = –1 s = –2
z = –5 x = –3  y=1 s = –2
• From above examples it can be seen that:
• Magnitudes of quotient y and remainder s are unaffected by the input signs
• Signs of y and s are easily derivable from the signs of 2 and x
• Thus for signed division
• use an indirect algorithm that converts operands into unsigned values and, at the end,
accounts for the signs by adjusting the sign bits
Restoring Division
Algorithm
Hardware Implementation
Shift-Subtract Operation through programming

• On machines that do not have a hardware-supported division


instruction, division can be performed in software by means of the
shift-subtract algorithm
• Such a program can be written for MiniMIPS
Thank You

You might also like