CS61C Finishing the Datapath: Subtract, Multiple, Divide Lecture 21

Slides:



Advertisements
Similar presentations
Morgan Kaufmann Publishers Arithmetic for Computers
Advertisements

Multiplication and Division
Arithmetic in Computers Chapter 4 Arithmetic in Computers2 Outline Data representation integers Unsigned integers Signed integers Floating-points.
Arithmetic for Computers
Datorteknik IntegerMulDiv bild 1 MIPS mul/div instructions Multiply: mult $2,$3Hi, Lo = $2 x $3;64-bit signed product Multiply unsigned: multu$2,$3Hi,
CMPE 325 Computer Architecture II
Computer Architecture ECE 361 Lecture 6: ALU Design
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE Computer Organization Lecture 8 - Multiplication.
Division CPSC 321 Computer Architecture Andreas Klappenecker.
Lecture 15: Computer Arithmetic Today’s topic –Division 1.
CMPT 334 Computer Organization Chapter 3 Arithmetic for Computers [Adapted from Computer Organization and Design 5 th Edition, Patterson & Hennessy, ©
Lecture Objectives: 1)Perform binary division of two numbers. 2)Define dividend, divisor, quotient, and remainder. 3)Explain how division is accomplished.
Integer Multiplication and DivisionICS 233 – KFUPM © Muhamed Mudawar slide 1 Multiplicand and HI are sign-extended  Sign is the sign of the result Signed.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
361 div.1 Computer Architecture ECE 361 Lecture 7: ALU Design : Division.
Lecture 9 Sept 28 Chapter 3 Arithmetic for Computers.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 4: Arithmetic / Data Transfer Instructions Partially adapted from Computer Organization.
Arithmetic IV CPSC 321 Andreas Klappenecker. Any Questions?
Integer Multiplication and Division ICS 233 Computer Architecture and Assembly Language Dr. Aiman El-Maleh College of Computer Sciences and Engineering.
Computer Organization Multiplication and Division Feb 2005 Reading: Portions of these slides are derived from: Textbook figures © 1998 Morgan Kaufmann.
Chap 3.3~3.5 Construction an Arithmetic Logic Unit (ALU) Jen-Chang Liu, Spring 2006.
Integer Multiplication and Division
1 Lecture 8: Binary Multiplication & Division Today’s topics:  Addition/Subtraction  Multiplication  Division Reminder: get started early on assignment.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
ECE 232 L9.Mult.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 9 Computer Arithmetic.
Lecture Objectives: 1)Explain the relationship between addition and subtraction with twos complement numbering systems 2)Explain the concept of numeric.
Multiplication CPSC 252 Computer Organization Ellen Walker, Hiram College.
ECE232: Hardware Organization and Design
Copyright 1995 by Coherence LTD., all rights reserved (Revised: Oct 97 by Rafi Lohev, Oct 99 by Yair Wiseman, Sep 04 Oren Kapah) IBM י ב מ 10-1 The ALU.
Lec 13Systems Architecture1 Systems Architecture Lecture 13: Integer Multiplication and Division Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan.
07/19/2005 Arithmetic / Logic Unit – ALU Design Presentation F CSE : Introduction to Computer Architecture Slides by Gojko Babić.
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department.
Multiplication of signed-operands
July 2005Computer Architecture, The Arithmetic/Logic UnitSlide 1 Part III The Arithmetic/Logic Unit.
Lecture 6: Multiply, Shift, and Divide
Chapter 3 Arithmetic for Computers (Integers). Florida A & M University - Department of Computer and Information Sciences Arithmetic for Computers Operations.
Cs 152 l6 Multiply 1 DAP Fa 97 © U.C.B. ECE Computer Architecture Lecture Notes Multiply, Shift, Divide Shantanu Dutt Univ. of Illinois at.
05/03/2009CA&O Lecture 8,9,10 By Engr. Umbreen sabir1 Computer Arithmetic Computer Engineering Department.
Arithmetic Logic Unit (ALU) Anna Kurek CS 147 Spring 2008.
Integer Multiplication and Division
Cs 61C L20 datapath.1 Patterson Spring 99 ©UCB CS61C Virtual Memory Wrap-Up + Processor Datapath Lecture 20 April 9, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)
Integer Multiplication and Division ICS 233 Computer Architecture and Assembly Language Dr. Aiman El-Maleh College of Computer Sciences and Engineering.
Csci 136 Computer Architecture II – Multiplication and Division
Mohamed Younis CMCS 411, Computer Architecture 1 CMSC Computer Architecture Lecture 11 Performing Division March 5,
Orange Coast College Business Division Computer Science Department CS 116- Computer Architecture Arithmetic: Part II.
EI 209 Chapter 3.1CSE, 2015 EI 209 Computer Organization Fall 2015 Chapter 3: Arithmetic for Computers Haojin Zhu ( )
Division Check for 0 divisor Long division approach – If divisor ≤ dividend bits 1 bit in quotient, subtract – Otherwise 0 bit in quotient, bring down.
CDA 3101 Spring 2016 Introduction to Computer Organization
Division Quotient Divisor Dividend – – Remainder.
Integer Multiplication and Division COE 301 Computer Organization Dr. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University.
Integer Operations Computer Organization and Assembly Language: Module 5.
By Wannarat Computer System Design Lecture 3 Wannarat Suntiamorntut.
Integer Multiplication and Division ICS 233 Computer Architecture & Assembly Language Prof. Muhamed Mudawar College of Computer Sciences and Engineering.
1 Lecture 5Multiplication and Division ECE 0142 Computer Organization.
Computer System Design Lecture 3
Computer Architecture & Operations I
Integer Multiplication and Division
MIPS mul/div instructions
Part II : Lecture III By Wannarat.
CS 314 Computer Organization Fall Chapter 3: Arithmetic for Computers
Morgan Kaufmann Publishers
Lecture 8: Binary Multiplication & Division
Multiplication & Division
CDA 3101 Summer 2007 Introduction to Computer Organization
CS352H: Computer Systems Architecture
Lecture 8: Addition, Multiplication & Division
Lecture 8: Addition, Multiplication & Division
Topic 3c Integer Multiply and Divide
Computer Architecture EECS 361 Lecture 6: ALU Design
1 Lecture 5Multiplication and Division ECE 0142 Computer Organization.
Presentation transcript:

CS61C Finishing the Datapath: Subtract, Multiple, Divide Lecture 21 April 14, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson) www-inst.eecs.berkeley.edu/~cs61c/schedule.html Give qualifications of instructors: DAP teaching computer architecture at Berkeley since 1977 Co-author of textbook used in class Best known for being one of pioneers of RISC and RAID Member of NAE

Outline Review Datapath, ALU Including subtract in a 32-bit ALU Review Binary Multiplication Administrivia, “What’s this Stuff Good For” Multiplier Review Binary Division Divider Conclusion

Review 1/5: Steps in Executing MIPS Subset 1) Fetch Instruction and Increment PC 2) Read 1 or 2 Registers 3) Mem-ref: Calculate Address Arith-log: Perform Operation Branch: Compare if operands == 4) Load: Read Data from Memory Store: Write Data to Memory Arith-og: Write Result to Register Branch: if operands ==, Change PC 5) Load: Write Data to Register

Review 2/5 : A Datapath for MIPS Address Data Out Data In Data Out Address Data Out Data In (Step 4,5) PC Instruction Cache Registers ALU Data Cache Step 1 Step 2 Step 3 (Step 4)

Review 3/5: Hardware Building Blocks AND Gate OR Gate Symbol Definition Symbol Definition A A C C B B Inverter Multiplexor Symbol Definition Symbol Definition D A C A C B 1

Review 4/5: Add 1-bit Adder to 1-bit ALU CarryIn Op A B C 1 2 + Definition CarryOut Now connect 32 1-bit ALUs together

Connect CarryOuti to CarryIni+1 Connect 32 1-bit ALUs together Review 5/5: 32-bit ALU Op A0 B0 C0 1 2 + Connect CarryOuti to CarryIni+1 Connect 32 1-bit ALUs together Connect Op to all 32 bits of ALU A1 B1 C1 1 2 + ... A31 B31 C31 1 2 + Does 32-bit And, Or, Add What about subtract?

2’s comp. shortcut: Negation (Lecture 7) Invert every 0 to 1 and every 1 to 0, then add 1 to the result Sum of number and its inverted rep. (“one’s complement”) must be 111...111two 111...111two= -1ten Let x mean the inverted representation of x Then x + x = -1  x + x + 1 = 0  x + 1 = -x Example: -4 to +4 to -4 x : 1111 1111 1111 1111 1111 1111 1111 1100two x : 0000 0000 0000 0000 0000 0000 0000 0011two +1: 0000 0000 0000 0000 0000 0000 0000 0100two () : 1111 1111 1111 1111 1111 1111 1111 1011two +1: 1111 1111 1111 1111 1111 1111 1111 1100two

Suppose added input to 1-bit ALU that gave the one’s complement of B How Do Subtract? Suppose added input to 1-bit ALU that gave the one’s complement of B What happens if set CarryIn0 to 1 in 32-bit ALU? 32-bit Sum = A + B + 1 Then if select one’s complement of B (B), Sum is A + B + 1 = A + (B + 1) = A + (-B) = A - B If modify 1-bit ALU, can do Subtract as well as And, Or, Add

1-bit ALU with Subtract Support CarryIn Binvert Op A 1 B C 1 + 2 Definition CarryOut

32-bit ALU made from AND gates, OR gates, Inverters, Multiplexors Binvert CarryIn Op A0 B0 C0 1 2 + 32-bit ALU made from AND gates, OR gates, Inverters, Multiplexors Performs 32-bit AND, OR,Add, Sub (2’s complement) Op, Binvert, CarryIn are control lines; control datapath A1 B1 C1 1 2 + ... ... A31 1 C31 B31 1 + 2

MULTIPLY: From Lecture 9 Paper and pencil example: Multiplicand 1000 Multiplier 1001 1000 0000 0000 1000 Product 01001000 m bits x n bits = m+n bit product MIPS: mul, mulu puts product in pair of new registers hi, lo; copy by mfhi, mflo 32-bit integer result in lo

Binary multiplication is easy: MULTIPLY Binary multiplication is easy: 0  place 0 ( 0 x multiplicand) 1  place a copy ( 1 x multiplicand) Shift the multiplicand left before adding to product 3 versions of multiply hardware & algorithm: Successive refinement to get to real version Go into first version in detail, give idea of others

Shift-add multiplier (version 1) 1000 1001 1000 0000 0000 1000 01001000 64-bit Multiplicand reg, 64-bit ALU, 64-bit Product reg, 32-bit Multiplier reg Shift Left Multiplicand 64 bits Shift Right Multiplier 64-bit ALU 32 bits Product Write Control 64 bits Multiplier = datapath + control

Shift-Add Multiply Algorithm V. 1 Multiplier0 = 1 Multiplier0 = 0 1. Test Multiplier0 1a. Add multiplicand to product & place the result in Product register 2. Shift the M’cand register left 1 bit Product M’plier M’cand 0000 0000 0011 0000 0010 0000 0010 0001 0000 0100 0000 0110 0000 0000 1000 0000 0110 0000 0001 0000 0000 0110 M’ier: 0011 M’and: 0000 0010 P: 0000 0000 1a. 1=>P=P+Mcand M’ier: 0011 Mcand: 0000 0010 P: 0000 0010 2. Shl Mcand M’ier: 0011 Mcand: 0000 0100 P: 0000 0010 3. Shr M’ier M’ier: 0001 Mcand: 0000 0100 P: 0000 0010 1a. 1=>P=P+Mcand M’ier: 0001 Mcand: 0000 0100 P: 0000 0110 2. Shl Mcand M’ier: 0001 Mcand: 0000 1000 P: 0000 0110 3. Shr M’ier M’ier: 0000 Mcand: 0000 1000 P: 0000 0110 1. 0=>nop M’ier: 0000 Mcand: 0000 1000 P: 0000 0110 2. Shl Mcand M’ier: 0000 Mcand: 0001 0000 P: 0000 0110 3. Shr M’ier M’ier: 0000 Mcand: 0001 0000 P: 0000 0110 1. 0=>nop M’ier: 0000 Mcand: 0001 0000 P: 0000 0110 2. Shl Mcand M’ier: 0000 Mcand: 0010 0000 P: 0000 0110 3. Shr M’ier M’ier: 0000 Mcand: 0010 0000 P: 0000 0110 3. Shift the M’plier register right 1 bit 31nd repetition? No: < 32 repetitions Yes: 32 repetitions Done

9th homework: Due Friday (Ex. 7.35, 4.24) Administrivia Project 5: Due today Next Readings: 3.12, 4.9 Optional: Appendix D “An Alternative to RISC: the Intel 80x86,” Computer Architecture, A Quantitative Approach, 2/e Optional: RISC Survey (including HP ISA) ftp://mkp.com/COD2e/Web_Extensions/survey.htm 9th homework: Due Friday (Ex. 7.35, 4.24) 10th homework: Due Wednesday 4/21 7PM Exercises 4.43, 3.17 (assume each instruction takes 1 clock cycle, performance = no. instructions executed * clock cycle time, ignore CPI comment) This is the 1st slide of the “Course Structure and Course Philosophy” section of the lecture. Need to emphasis that for exams, our goal is the test your knowledge. It is not our goal to test how well your perform under time pressure.

Administrivia: Rest of 61C F 4/16 Intel x86, HP instruction sets; 3.12, 4.9 W 4/21 Performance; Reading sections 2.1-2.5 F 4/23 Review: Procedures, Variable Args (Due: x86/HP ISA lab, homework 10) W 4/28 Processor Pipelining; section 6.1 F 4/30 Review: Caches/TLB/VM; section 7.5 (Due: Project 6-sprintf in MIPS, homework 11) M 5/3 Deadline to correct your grade record W 5/5 Review: Interrupts/Polling F 5/7 61C Summary / Your Cal heritage Sun 5/9 Final Review starting 2PM (1 Pimintel) W 5/12 Final (5PM 1 Pimintel); mds@cory

“What’s This Stuff Good For?” Aquanex system Swimmers looking for an edge over the competition can now use tiny half-ounce sensors that give them instant feedback on their power and their stroke rate, length and velocity, shown as real-time performance graphs generated by the PC. Unlike most, this swimmer generates more arm power in his butterfly than in his freestyle. "If he learns to apply that strength to his freestyle, he'll almost certainly decrease his time." One Digital Day, 1998 www.intel.com/onedigitalday Will computers help us utilize untapped physical potential as well as intellectual potential?

Observations on Shift-Add Multiply V.1 1/2 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?

Shift-add Multiplier Version 2 (v. slide 14) 32-bit Multiplicand reg, 32-bit ALU, 64-bit Product reg, 32-bit Multiplier reg Multiplicand 32 bits Shift Right Multiplier 32-bit ALU 32 bits Write Product Control 64 bits Shift Right Multiplier = datapath + control

Shift-Add Multiplier Version 3 Product reg. wastes space that exactly matches size of Multiplier reg  combine Multiplier reg and Product reg  “0-bit” Multiplier reg Multiplicand 32 bits 32-bit ALU 0 bits Write Product (M’plier) Control Shift Right 64 bits Multiplier = datapath + control

Dividend = Quotient x Divisor + Remainder Divide: From Lecture 9 1001 Quotient Divisor 1000 1001010 Dividend –1000 10 101 1010 –1000 10 Remainder (or Modulo result) Dividend = Quotient x Divisor + Remainder See how big a number can be subtracted, creating quotient bit on each step Binary  1 * divisor or 0 * divisor

DIVIDE HARDWARE Version 1 1001 1000 1001010 –1000 10 101 1010 –1000 64-bit Divisor reg, 64-bit ALU, 64-bit Remainder reg, 32-bit Quotient reg Shift Right Divisor 64 bits Quotient Shift Left 64-bit ALU 32 bits Write Remainder Control 64 bits Put Dividend into Remainder to start, collect Quotient 1 bit at a time

Divide Algorithm V. 1 Start: Place Dividend in Remainder 1. Subtract the Divisor register from the Remainder register, and place the result in the Remainder register. Remainder ≥ 0 Remainder < 0 Test Remainder 2a. Shift the Quotient register to the left setting the new rightmost bit to 1 2b. Restore the original value by adding the Divisor register to the Remainder register, and place the sum in the Remainder register. Also shift the Quotient register to the left, setting the new least significant bit to 0 Q: 0000 D: 0010 0000 R: 0000 0111 –D = 1110 0000 1: R = R–D Q: 0000 D: 0010 0000 R: 1110 0111 2b: +D, sl Q, 0 Q: 0000 D: 0010 0000 R: 0000 0111 3: Shr D Q: 0000 D: 0001 0000 R: 0000 0111 –D = 1111 0000 1: R = R–D Q: 0000 D: 0001 0000 R: 1111 0111 2b: +D, sl Q, 0 Q: 0000 D: 0001 0000 R: 0000 0111 3: Shr D Q: 0000 D: 0000 1000 R: 0000 0111 –D = 1111 1000 1: R = R–D Q: 0000 D: 0000 1000 R: 1111 1111 2b: +D, sl Q, 0 Q: 0000 D: 0000 1000 R: 0000 0111 3: Shr D Q: 0000 D: 0000 0100 R: 0000 0111 –D = 1111 1100 1: R = R–D Q: 0000 D: 0000 0100 R: 0000 0011 2a: sl Q, 1 Q: 0001 D: 0000 0100 R: 0000 0011 3: Shr D Q: 0000 D: 0000 0010 R: 0000 0011 –D = 1111 1110 1: R = R–D Q: 0000 D: 0000 0010 R: 0000 0001 2a: sl Q, 1 Q: 0011 D: 0000 0010 R: 0000 0001 3: Shr D Q: 0011 D: 0000 0001 R: 0000 0001 Recommend show 2’s comp of divisor, show lines for subtract divisor and restore remainder 3. Shift the Divisor register right1 bit n+1 repetition? No: < n+1 repetitions Yes: n+1 repetitions (n = 4 here) Done

Example Divide Algorithm V. 1 Takes n+1 steps for n-bit Quotient & Rem. Remainder Quotient Divisor (-Divisor) 0000 0111 0000 0010 0000 (1110 0000) 1 1110 0111 2b 0000 3 0000 0111 0001 0000 (1111 0000) 1 1111 0111 2b 0000 3 0000 0111 0000 1000 (1111 1000) 1 1111 1111 2b 0000 3 0000 0111 0000 0100 (1111 1100) Q: 0000 D: 0010 0000 R: 0000 0111 –D = 1110 0000 1: R = R–D Q: 0000 D: 0010 0000 R: 1110 0111 2b: +D, sl Q, 0 Q: 0000 D: 0010 0000 R: 0000 0111 3: Shr D Q: 0000 D: 0001 0000 R: 0000 0111 –D = 1111 0000 1: R = R–D Q: 0000 D: 0001 0000 R: 1111 0111 2b: +D, sl Q, 0 Q: 0000 D: 0001 0000 R: 0000 0111 3: Shr D Q: 0000 D: 0000 1000 R: 0000 0111 –D = 1111 1000 1: R = R–D Q: 0000 D: 0000 1000 R: 1111 1111 2b: +D, sl Q, 0 Q: 0000 D: 0000 1000 R: 0000 0111 3: Shr D Q: 0000 D: 0000 0100 R: 0000 0111 –D = 1111 1100 1: R = R–D Q: 0000 D: 0000 0100 R: 0000 0011 2a: sl Q, 1 Q: 0001 D: 0000 0100 R: 0000 0011 3: Shr D Q: 0000 D: 0000 0010 R: 0000 0011 –D = 1111 1110 1: R = R–D Q: 0000 D: 0000 0010 R: 0000 0001 2a: sl Q, 1 Q: 0011 D: 0000 0010 R: 0000 0001 3: Shr D Q: 0011 D: 0000 0001 R: 0000 0001 Recommend show 2’s comp of divisor, show lines for subtract divisor and restore remainder

Example Divide Algorithm V. 1 (cont’d) Remainder Quotient Divisor (-Divisor) 0000 0111 0000 0000 0100 (1111 1100) 1 0000 0011 2a 0001 3 0000 0011 0000 0010 (1111 1110) 1 0000 0001 2a 0011 3 0000 0001 0000 0001 (1111 1111) Original division of 0111two (7) by 0010two (2) gives a quotient of 0011two (3) plus a remainder of 0001two (1) Q: 0000 D: 0010 0000 R: 0000 0111 –D = 1110 0000 1: R = R–D Q: 0000 D: 0010 0000 R: 1110 0111 2b: +D, sl Q, 0 Q: 0000 D: 0010 0000 R: 0000 0111 3: Shr D Q: 0000 D: 0001 0000 R: 0000 0111 –D = 1111 0000 1: R = R–D Q: 0000 D: 0001 0000 R: 1111 0111 2b: +D, sl Q, 0 Q: 0000 D: 0001 0000 R: 0000 0111 3: Shr D Q: 0000 D: 0000 1000 R: 0000 0111 –D = 1111 1000 1: R = R–D Q: 0000 D: 0000 1000 R: 1111 1111 2b: +D, sl Q, 0 Q: 0000 D: 0000 1000 R: 0000 0111 3: Shr D Q: 0000 D: 0000 0100 R: 0000 0111 –D = 1111 1100 1: R = R–D Q: 0000 D: 0000 0100 R: 0000 0011 2a: sl Q, 1 Q: 0001 D: 0000 0100 R: 0000 0011 3: Shr D Q: 0000 D: 0000 0010 R: 0000 0011 –D = 1111 1110 1: R = R–D Q: 0000 D: 0000 0010 R: 0000 0001 2a: sl Q, 1 Q: 0011 D: 0000 0010 R: 0000 0001 3: Shr D Q: 0011 D: 0000 0001 R: 0000 0001 Recommend show 2’s comp of divisor, show lines for subtract divisor and restore remainder

Observations on Divide Version 1 1/2 bits in divisor always 0 => 1/2 of 64-bit adder is wasted => 1/2 of divisor is wasted Instead of shifting Divisor to right, shift Remainder to left?

DIVIDE HARDWARE Version 2 (v. slide 28) 32-bit Divisor reg, 32-bit ALU, 64-bit Remainder reg, 32-bit Quotient reg Divisor 32 bits Quotient Shift Left 32-bit ALU Write Remainder Control Shift Left 64 bits

DIVIDE HARDWARE Version 3 Eliminate Quotient register by combining with Remainder as shifted left 32-bit “0-bit” Quotient reg Divisor 32 bits 32-bit ALU 0-bits Write Remainder (Quot.) Control Shift Left 64 bits

Multiply, Divide Algorithms Explain MIPS Multiplicand/Divisor 32 bits 32-bit ALU Hi Lo Write Product/ Remainder Product/ Quotient Control Shift 64 bits mul puts product in pair of regs hi, lo; 32-bit integer result in lo MIPS: div, divu puts Remainer into hi, puts Quotient into lo

A Revised Datapath for MIPS (Step 4, 5) PC Instruction Cache Registers ALU Data Cache Step 1 Step 2 Step 3 (Step 4) X, / Hi, Lo registers Steps 3-19

“And in Conclusion..” 1/1 Subtract included to ALU by adding one’s complement of B Multiple by shift and add Divide by shift and subtract, then restore by add if didn’t fit Can Multiply, Divide simply by adding 64-bit shift register to ALU MIPS allows multiply, divide in parallel with ALU operations Next: Intel and HP Instruction Set Architectures