arithmetic and logic unit second part

38
Laboratorio de Tecnologías de Información Arquitectura de Computadoras ALU2- 1 Arithmetic and Logic Unit Arithmetic and Logic Unit Second Part Second Part Arquitectura de Computadoras Arquitectura de Computadoras Arturo D Arturo D í í az P az P é é rez rez Centro de Investigaci Centro de Investigaci ó ó n y de Estudios Avanzados del IPN n y de Estudios Avanzados del IPN Laboratorio de Tecnolog Laboratorio de Tecnolog í í as de Informaci as de Informaci ó ó n n [email protected] [email protected]

Upload: others

Post on 14-Apr-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 1

Arithmetic and Logic UnitArithmetic and Logic Unit Second PartSecond Part

Arquitectura de ComputadorasArquitectura de ComputadorasArturo DArturo Dííaz Paz Péérezrez

Centro de InvestigaciCentro de Investigacióón y de Estudios Avanzados del IPNn y de Estudios Avanzados del IPNLaboratorio de TecnologLaboratorio de Tecnologíías de Informacias de Informacióónn

[email protected]@cinvestav.mx

Page 2: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 2

MultiplicationMultiplication

Multiplication can’t be that hard!■

It’s just repeated addition.

If we have adders, we can do multiplication also.♦

Remember that the AND operation is equivalent to multiplication on two bits:

a b ab0 0 00 1 01 0 01 1 1

a b a×b0 0 00 1 01 0 01 1 1

Page 3: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 3

Binary multiplication exampleBinary multiplication example

Since we always multiply by either 0 or 1, the partial products

are always either 0000

or the multiplicand (1101

in this example).

There are four partial products which are added to form the result.■

We can add them in pairs, using three adders.■

Even though the product has up to 8 bits, we can use 4-bit adders if we “stagger”

them leftwards, like the partial products themselves.

1 1 0 1 Multiplicandx

0

1

1

0

Multiplier

0 0 0 0 Partial products1 1 0 1

1 1 0 1+

0 0 0 0

1 0 0 1 1 1 0 Product

Page 4: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 4

A 2x2 binary multiplierA 2x2 binary multiplier

The AND gates produce the partial products.

For a 2-bit by 2-bit multiplier, we can just use two half adders to sum the partial products. In general, though, we’ll need full adders.

Here C3

-C0

are the product, not carries!

B1 B0

x A1 A0

A0B1 A0B0

+ A1B1 A1B0

C3 C2 C1 C0

Page 5: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 5

A 4x4 multiplier circuitA 4x4 multiplier circuit

Page 6: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 6

More on multipliersMore on multipliers

Notice that this 4-bit multiplier produces an 8-bit result■

We could just keep all 8 bits

Or, if we needed a 4-bit result, we could ignore C4-C7, and consider it an overflow condition if the result is longer than 4

bits

Multipliers are very complex circuits. ■

In general, when multiplying an m-bit number by an n-bit number:» There are n partial products, one for each bit of the multiplier» This requires n-1 adders, each of which can add m bits (the size of

the multiplicand)■

The circuit for 32-bit or 64-bit multiplication would be huge!

Page 7: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 7

Multiplication: a special caseMultiplication: a special case

In decimal, an easy way to multiply by 10 is to shift all the digits to the left, and tack a 0 to the right end.

128

x 10 = 1280

We can do the same thing in binary. Shifting left is equivalent to multiplying by 2:

11

x 10 = 110

(in decimal, 3 x 2 = 6)

Shifting left twice is equivalent to multiplying by 4:

11

x 100 = 1100

(in decimal, 3 x 4 = 12)

As an aside, shifting to the right is equivalent to dividing by 2.

110

÷

10 = 11

(in decimal, 6 ÷

2 = 3)

Page 8: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 8

Addition and multiplication summaryAddition and multiplication summary

Adder and multiplier circuits mimic human algorithms for addition and multiplication

Adders and multipliers are built hierarchically■

We start with half adders or full adders and work our way up

Building these functions from scratch with truth tables and K-maps would be pretty difficult

The arithmetic circuits impose a limit on the number of bits that can be added. Exceeding this limit results in overflow

There is a tradeoff between simple but slow circuits (ripple carry adders) and complex but fast circuits (carry lookahead adders)

Multiplication and division by powers of 2 can be handled with simple shifting

Page 9: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 9

Unsigned Combinational MultiplierUnsigned Combinational Multiplier

Stage i accumulates A * 2 i

if Bi

== 1♦

Q: How much hardware for 32 bit multiplier? Critical path?

B0

A0A1A2A3

A0A1A2A3

A0A1A2A3

A0A1A2A3

B1

B2

B3

P0P1P2P3P4P5P6P7

0 0 0 0

Page 10: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 10

How does it work?How 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

B0A0A1A2A3

A0A1A2A3

A0A1A2A3

A0A1A2A3

B1

B2

B3

P0P1P2P3P4P5P6P7

0 0 0 00 0 0

Page 11: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 11

Unsigned shiftUnsigned shift--add multiplier (version 1)add multiplier (version 1)

64-bit Multiplicand reg, 64-bit ALU, 64-bit Product reg, 32-bit multiplier reg

Product

Multiplier

Multiplicand

64-bit ALU

Shift Left

Shift Right

WriteControl

32 bits

64 bits

64 bits

Multiplier = datapath + control

Page 12: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 12

Multiply Algorithm Version 1Multiply Algorithm Version 1

Product Multiplier Multiplicand 0000 0000 0011 0000 00100000 0010 0001 0000 01000000 0110 0000 0000 10000000 0110

3. Shift the Multiplier register right 1 bit.

DoneYes: 32 repetitions

2. Shift the Multiplicand register left 1 bit.

No: < 32 repetitions

1. TestMultiplier0

Multiplier0 = 0Multiplier0 = 1

1a. Add multiplicand to product & place the result in Product register

32nd repetition?

Start

Page 13: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 13

Observations on Multiply Version 1Observations on Multiply Version 1

1 clock per cycle => ≈

100 clocks per multiply■

Ratio of multiply to add 5:1 to 100:1

Half of bits in multiplicand always 0 => 64-bit adder is wasted

0’s inserted in left of multiplicand as shifted => least significant bits of product never changed once

formed♦

Instead of shifting multiplicand to left, shift product to right?

Page 14: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 14

MULTIPLY HARDWARE Version 2MULTIPLY HARDWARE Version 2

32-bit Multiplicand reg, 32

-bit ALU, 64-bit Product reg, 32-bit Multiplier reg

Product

Multiplier

Multiplicand

32-bit ALUShift Right

WriteControl

32 bits

32 bits

64 bits

Shift Right

Page 15: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 15

How to think of this? How to think of this?

B0

A0A1A2A3

A0A1A2A3

A0A1A2A3

A0A1A2A3

B1

B2

B3

P0P1P2P3P4P5P6P7

0 0 0 0

Remember original combinational multiplier:

Page 16: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 16

Simply warp to let product move Simply warp to let product move right...right...

Multiplicand stay’s still and product moves right

B0

B1

B2

B3

P0P1P2P3P4P5P6P7

0 0 0 0

A0A1A2A3

A0A1A2A3

A0A1A2A3

A0A1A2A3

Page 17: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 17

Multiply Algorithm Multiply Algorithm Version 2Version 2

3. Shift the Multiplier register right 1 bit.

DoneYes: 32 repetitions

2. Shift the Product register right 1 bit.

No: < 32 repetitions

1. TestMultiplier0

Multiplier0 = 0Multiplier0 = 1

1a. Add multiplicand to the left half of product & place the result in the left half of Product register

32nd repetition?

Start

0000 0000 0011 00101: 0010 0000 0011 00102: 0001 0000 0011 00103: 0001 0000 0001 00101: 0011 0000 0001 00102: 0001 1000 0001 00103: 0001 1000 0000 00101: 0001 1000 0000 00102: 0000 1100 0000 00103: 0000 1100 0000 00101: 0000 1100 0000 00102: 0000 0110 0000 00103: 0000 0110 0000 0010

0000 0110 0000 0010

Product Multiplier Multiplicand

Page 18: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 18

Still more wasted space!Still more wasted space!

Product Multiplier Multiplicand

3. Shift the Multiplier register right 1 bit.

DoneYes: 32 repetitions

2. Shift the Product register right 1 bit.

No: < 32 repetitions

1. TestMultiplier0

Multiplier0 = 0Multiplier0 = 1

1a. Add multiplicand to the left half of product & place the result in the left half of Product register

32nd repetition?

Start

0000 0000 0011 00101a: 0010 0000 0011 00102: 0001 0000 0011 00103: 0001 0000 0001 00101a: 0011 0000 0001 00102: 0001 1000 0001 00103: 0001 1000 0000 00101: 0001 1000 0000 00102: 0000 1100 0000 00103: 0000 1100 0000 00101: 0000 1100 0000 00102: 0000 0110 0000 00103: 0000 0110 0000 0010

0000 0110 0000 0010

Page 19: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 19

Observations on Multiply Version 2Observations on Multiply Version 2

Product register wastes space that exactly matches size of multiplier

=> combine Multiplier register and Product register

Page 20: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 20

MULTIPLY HARDWARE Version 3MULTIPLY HARDWARE Version 3

32-bit Multiplicand reg, 32 -bit ALU, 64-bit Product reg, (0-bit Multiplier reg)

Product (Multiplier)

Multiplicand

32-bit ALU

WriteControl

32 bits

64 bits

Shift Right

Page 21: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 21

Multiply Algorithm Multiply Algorithm Version 3Version 3

Multiplicand Product 0010 0000 0011

DoneYes: 32 repetitions

2. Shift the Product register right 1 bit.

No: < 32 repetitions

1. TestProduct0

Product0 = 0Product0 = 1

1a. Add multiplicand to the left half of product & place the result in the left half of Product register

32nd repetition?

Start

Page 22: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 22

Observations on Multiply Version 3Observations on Multiply Version 3

2 steps per bit because Multiplier & Product combined♦

MIPS registers Hi and Lo are left and right half of Product

Gives us MIPS instruction MultU♦

How can you make it faster?

What about signed multiplication?■

easiest solution is to make both positive & remember whether to

complement product when done (leave out the sign bit, run for 31 steps)

apply definition of 2’s complement » need to sign-extend partial products and subtract at the end

Booth’s Algorithm is elegant way to multiply signed numbers using same hardware as before and save cycles

» can handle multiple bits at a time

Page 23: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 23

FastFast MultiplicationMultiplication HardwareHardwareMplier1●Mcand Mplier0●Mcand

Product0

Mplier2●Mcand32

32

1-bit

Product1

1-bit

32

1-bit

Product2

32

Mplier3●Mcand

Product63..32

Mplier31●Mcand32

32

Product31

1-bit

Page 24: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 24

Divide: Paper & PencilDivide: Paper & Pencil1001 Quotient

Divisor 1000 1001010 Dividend–1000

10 101 1010 –1000

10 Remainder (or Modulo result)

See how big a number can be subtracted, creating quotient bit on each step

Binary => 1 * divisor or 0 * divisor

Dividend = Quotient x Divisor + Remainder => | Dividend | = | Quotient | + | Divisor |

3 versions of divide, successive refinement

Page 25: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 25

DIVIDE HARDWARE Version 1DIVIDE HARDWARE Version 1

64-bit Divisor reg, 64-bit ALU, 64-bit Remainder reg, 32-bit Quotient reg

Remainder

Quotient

Divisor

64-bit ALU

Shift Right

Shift Left

Write Control

32 bits

64 bits

64 bits test

Page 26: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 26

2b. Restore the original value by adding the Divisor register to the Remainder register, &place the sum in the Remainder register. Alsoshift the Quotient register to the left, setting the new least significant bit to 0.

Divide Algorithm Divide Algorithm Version 1Version 1

Test Remainder

Remainder < 0Remainder ≥

0

1. Subtract the Divisor register from the Remainder register, and place the result in the Remainder register.

2a. Shift the Quotient registerto the left setting the new rightmostbit to 1.

3. Shift the Divisor register right 1 bit.

Done

Yes: n+1 repetitions (n = 4 here)

Start: Place Dividend in Remainder

n+1repetition?

No: < n+1 repetitions

Page 27: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 27

Divide Algorithm Divide Algorithm Version 1Version 1

Quotient Divisor Remainder 0: Initial Values 0000 0010 0000 0000 01111: Rem = Rem-Div 0000 0010 0000 1110 01112b:Rem<0; +Div, sll Q<-0 0000 0010 0000 0000 01113: Shift Div Right 0000 0001 0000 0000 01111: Rem = Rem-Div 0000 0001 0000 1111 01112b:Rem<0; +Div, sll Q<-0 0000 0001 0000 0000 01113: Shift Div Right 0000 0000 1000 0000 01111: Rem = Rem-Div 0000 0000 1000 1111 11112b:Rem<0; +Div, sll Q<-0 0000 0000 1000 0000 01113: Shift Div Right 0000 0000 0100 0000 01111: Rem = Rem-Div 0000 0000 0100 0000 00112a:Rem>=0; sll Q <-1 0001 0000 0100 0000 00113: Shift Div Right 0001 0000 0010 0000 00111: Rem = Rem-Div 0001 0000 0010 0000 00012a:Rem>=0; sll Q <-1 0011 0000 0010 0000 00013: Shift Div Right 0011 0000 0001 0000 0001

10 111

Page 28: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 28

Observations on Divide Version 1Observations on Divide Version 1

Half of bits in divisor always 0 => half of 64-bit adder is wasted

=> half of divisor is wasted♦

Instead of shifting divisor to right, shift remainder to left?

1st step cannot produce a 1 in quotient bit (otherwise too big)

=> switch order to shift first and then subtract, can save 1 iteration

Page 29: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 29

DIVIDE HARDWARE Version 2DIVIDE HARDWARE Version 2

32-bit Divisor reg, 32-bit ALU, 64-bit Remainder reg, 32-bit Quotient reg

Remainder

Quotient

Divisor

32-bit ALUShift Left

WriteControl

32 bits

32 bits

64 bits

Shift Left

Page 30: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 30

Divide Algorithm Divide Algorithm Version 2Version 2

3b. Restore the original value by adding the Divisor register to the left half of the Remainder register, &place the sum in the left half of the Remainderregister. Also shift the Quotient register to the left, setting the new least significant bit to 0.

Test Remainder

Remainder < 0Remainder ≥

0

2. Subtract the Divisor register from the left half of the Remainder register, & place the result in the left half of the Remainder register.

3a. Shift the Quotient register to the left setting the new rightmostbit to 1.

1. Shift the Remainder register left 1 bit.

Done

Yes: n repetitions (n = 4 here)

nthrepetition?

No: < n repetitions

Start: Place Dividend in Remainder

Page 31: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 31

Divide Algorithm Divide Algorithm Version 2Version 2

Quotient Divisor Remainder 0: Initial Values 0000 0010 0000 01111: sll Rem<-0 0000 0010 0000 11102: Rem = Rem-Div; 0000 0010 1110 11103b:Rem<0; Rem+=Div;sll Q<-0 0000 0010 0000 11101: sll Rem<-0 0000 0010 0001 11002: Rem = Rem-Div; 0000 0010 1111 11003b:Rem<0; Rem+=Div;sll Q<-0 0000 0010 0001 11001: sll Rem<-0 0000 0010 0011 10002: Rem = Rem-Div; 0000 0010 0001 10003a:Rem>0; sll Q<-1 0001 0010 0001 10001: sll Rem<-0 0001 0010 0011 00002: Rem = Rem–Div; 0001 0010 0001 00003a:Rem>0; sll Q<-1 0011 0010 0001 0000

Page 32: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 32

Observations on Divide Version 2Observations on Divide Version 2

Eliminate Quotient register by combining with Remainder as shifted left■

Start by shifting the Remainder left as before.

Thereafter loop contains only two steps because the shifting of the Remainder register shifts both the remainder in the left half and the quotient in the right half

The consequence of combining the two registers together and the new order of the operations in the loop is that the remainder will shifted left one time too many.

Thus the final correction step must shift back only the remainder in the left half of the register

Page 33: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 33

DIVIDE HARDWARE Version 3DIVIDE HARDWARE Version 3

32-bit Divisor reg, 32 -bit ALU, 64-bit Remainder reg, (0-bit Quotient reg)

Remainder (Quotient)

Divisor

32-bit ALU

WriteControl

32 bits

64 bits

Shift Left“HI” “LO”

Page 34: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 34

Divide Algorithm Divide Algorithm Version 3Version 3

3b. Restore the original value by adding the Divisor register to the left half of the Remainder register, &place the sum in the left half of the Remainder register. Also shift the Remainder register to the left, setting the new least significant bit to 0.

Test Remainder

Remainder < 0Remainder >= 0

2. Subtract the Divisor register from the left half of the Remainder register, & place the result in the left half of the Remainder register.

3a. Shift the Remainder register to the left setting the new rightmostbit to 1.

1. Shift the Remainder register left 1 bit.

Done. Shift left half of Remainder right 1 bit.Yes: n repetitions (n = 4 here)

nthrepetition?

No: < n repetitions

Start: Place Dividend in Remainder

Page 35: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 35

Divide Algorithm Divide Algorithm Version 3Version 3

Divisor Remainder 0: Initial Values 0010 0000 01111: sll Rem<-0 0010 0000 11102: Rem = Rem-Div; 0010 1110 11103b:Rem<0; Rem+=Div;sll Rem<-0 0010 0000 11101: sll Rem<-0 0010 0001 11002: Rem = Rem-Div; 0010 1111 11003b:Rem<0; Rem+=Div;sll Rem<-0 0010 0001 11001: sll Rem<-0 0010 0011 10002: Rem = Rem-Div; 0010 0001 10003a:Rem>0; sll Rem<-1 0010 0001 10011: sll Rem<-0 0010 0011 00102: Rem = Rem–Div; 0010 0001 00103a:Rem>0; sll Rem<-1 0010 0001 0011

Page 36: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 36

Observations on Divide Version 3Observations on Divide Version 3

Same Hardware as Multiply: just need ALU to add or subtract, and 63-bit register to shift left or shift right

Hi and Lo registers in MIPS combine to act as 64-bit register for multiply and divide

Signed Divides: Simplest is to remember signs, make positive, and complement quotient and remainder if necessary■

Note: Dividend and Remainder must have same sign

Note: Quotient negated if Divisor sign & Dividend sign disagree e.g., –7 ÷

2 = –3, remainder = –1

Possible for quotient to be too large: if divide 64-bit integer by 1, quotient is 64 bits (“called saturation”)

Page 37: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 37

SummarySummary

Multiply: successive refinement to see final design ■

32-bit Adder, 64-bit shift register, 32-bit Multiplicand Register

Division: successive refinement to see final design ■

32-bit Adder, 64-bit shift register, 32-bit Divisor Register

Page 38: Arithmetic and Logic Unit Second Part

Laboratorio deTecnologías de Información

Arquitectura de Computadoras ALU2- 38

HwHw55: : AssignmentsAssignments

Prepare a ppt presentation explaining

the

design

and send it by e- mail

Due date:

October 13th, 2007.

1 Claudia Méndez Garza

Design an 8-bit carry look ahead adderDevelop equationsDraw a schematicIndicate delays

2 José

Ramírez Uresti Design a signed

8x8 multiplier+ times +, -

times –+ times –, -

times +

3 Víctor Echeverría Ríos Design a signed

8x8 divider + times +, -

times –+ times –, -

times +