Computer Organization and Design More Arithmetic: Multiplication, Division & Floating-Point Montek Singh Mon, Nov 5, 2012 Lecture 13.

Slides:



Advertisements
Similar presentations
Fixed Point Numbers The binary integer arithmetic you are used to is known by the more general term of Fixed Point arithmetic. Fixed Point means that we.
Advertisements

Arithmetic in Computers Chapter 4 Arithmetic in Computers2 Outline Data representation integers Unsigned integers Signed integers Floating-points.
CSCE 212 Chapter 3: Arithmetic for Computers Instructor: Jason D. Bakos.
1 CONSTRUCTING AN ARITHMETIC LOGIC UNIT CHAPTER 4: PART II.
2-1 Chapter 2 - Data Representation Computer Architecture and Organization by M. Murdocca and V. Heuring © 2007 M. Murdocca and V. Heuring Computer Architecture.
Lecture 16: Computer Arithmetic Today’s topic –Floating point numbers –IEEE 754 representations –FP arithmetic Reminder –HW 4 due Monday 1.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3: IT Students.
Floating Point Numbers
1 Lecture 9: Floating Point Today’s topics:  Division  IEEE 754 representations  FP arithmetic Reminder: assignment 4 will be posted later today.
Chapter 3 Arithmetic for Computers. Multiplication More complicated than addition accomplished via shifting and addition More time and more area Let's.
L11 – Floating Point 1 Comp 411 – Fall /21/2009 Floating-Point Arithmetic Reading: Study Chapter 3.5 Skim if ((A + A) - A == A) { SelfDestruct()
1  2004 Morgan Kaufmann Publishers Chapter Three.
L10 – Multiplication Division 1 Comp 411 – Fall /19/2009 Binary Multipliers ×
CSE 378 Floating-point1 How to represent real numbers In decimal scientific notation –sign –fraction –base (i.e., 10) to some power Most of the time, usual.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Hao Ji.
ECEN 248 Integer Multiplication, Number Format Adopted from Copyright 2002 David H. Albonesi and the University of Rochester.
Computer ArchitectureFall 2008 © August 27, CS 447 – Computer Architecture Lecture 4 Computer Arithmetic (2)
February 26, 2003MIPS floating-point arithmetic1 Question  Which of the following are represented by the hexadecimal number 0x ? —the integer.
Computer Organization and Design Floating-Point Arithmetic
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Computer Arithmetic.
CEN 316 Computer Organization and Design Computer Arithmetic Floating Point Dr. Mansour AL Zuair.
Number Systems So far we have studied the following integer number systems in computer Unsigned numbers Sign/magnitude numbers Two’s complement numbers.
Computing Systems Basic arithmetic for computers.
ECE232: Hardware Organization and Design
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /14/2013 Lecture 16: Floating Point Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Lecture 9: Floating Point
CSC 221 Computer Organization and Assembly Language
Conversion to Larger Number of Bits Ex: Immediate Field (signed 16 bit) to 32 bit Positive numbers have implied 0’s to the left. So, put 16 bit number.
Princess Sumaya Univ. Computer Engineering Dept. Chapter 3:
Lecture notes Reading: Section 3.4, 3.5, 3.6 Multiplication
Computer Arithmetic See Stallings Chapter 9 Sep 10, 2009
CDA 3101 Fall 2013 Introduction to Computer Organization
Chapter 3 Arithmetic for Computers. Chapter 3 — Arithmetic for Computers — 2 Arithmetic for Computers Operations on integers Addition and subtraction.
순천향대학교 정보기술공학부 이 상 정 1 3. Arithmetic for Computers.
Numbers in Computers.
10/7/2004Comp 120 Fall October 7 Read 5.1 through 5.3 Register! Questions? Chapter 4 – Floating Point.
King Fahd University of Petroleum and Minerals King Fahd University of Petroleum and Minerals Computer Engineering Department Computer Engineering Department.
Montek Singh Apr 8, 2016 Lecture 12
William Stallings Computer Organization and Architecture 8th Edition
Computer Architecture & Operations I
Lecture 9: Floating Point
Floating-Point Arithmetic
Floating-Point Arithmetic
CS/COE0447 Computer Organization & Assembly Language
Montek Singh Nov 8, 2017 Lecture 12
Chapter 6 Floating Point
How to represent real numbers
Computer Arithmetic Multiplication, Floating Point
ECEG-3202 Computer Architecture and Organization
October 17 Chapter 4 – Floating Point Read 5.1 through 5.3 1/16/2019
Montek Singh Mon, Mar 28, 2011 Lecture 11
More Arithmetic: Multiplication, Division, and Floating-Point
Presentation transcript:

Computer Organization and Design More Arithmetic: Multiplication, Division & Floating-Point Montek Singh Mon, Nov 5, 2012 Lecture 13

Topics  Brief overview of: integer multiplication integer multiplication integer division integer division floating-point numbers and operations floating-point numbers and operations

Binary Multipliers × Reading: Study Chapter × You’ve got to be kidding… It can’t be that easy! The key trick of multiplication is memorizing a digit-to-digit table… Everything else is just adding

Binary Multiplication A0A0 A1A1 A2A2 A3A3 B0B0 B1B1 B2B2 B3B3 A0B0A0B0 A1B0A1B0 A2B0A2B0 A3B0A3B0 A0B1A0B1 A1B1A1B1 A2B1A2B1 A3B1A3B1 A0B2A0B2 A1B2A1B2 A2B2A2B2 A3B2A3B2 A0B3A0B3 A1B3A1B3 A2B3A2B3 A3B3A3B3 x + A j B i is a “partial product” Multiplying N-digit number by M-digit number gives (N+M)-digit result Easy part: forming partial products (just an AND gate since B I is either 0 or 1) Hard part: adding M, N-bit partial products X The “Binary” Multiplication Table Hey, that looks like an AND gate Binary multiplication is implemented using the same basic longhand algorithm that you learned in grade school.

Multiplication: Implementation t 32nd repetition? Start Multiplier0 = 0Multiplier0 = 1 No: < 32 repetitions Yes: 32 repetitions Flow Chart Hardware Implementation

Second Version Multiplier Shift right Write 32 bits 64 bits 32 bits Shift right Multiplicand 32-bit ALU ProductControl test ift the Multiplier register right 1 bit 32nd repetition? Start Multiplier0 = 0Multiplier0 = 1 No: < 32 repetitions Yes: 32 repetitions More Efficient Hardware Implementation Flow Chart

Example for second version Test true shift right Test false shift right Test true shift right Test true shift right Initial0 ProductMultiplicandMultiplierStepIteration

Final Version Done 1. Test Product0 1a. Add multiplicand to the left half of the product and place the result in the left half of the Product register 2. Shift the Product register right 1 bit 32nd repetition? Start Product0 = 0Product0 = 1 No: < 32 repetitions Yes: 32 repetitions The trick is to use the lower half of the product to hold the multiplier during the operation. Even More Efficient Hardware Implementation!

What about the sign?  Positive numbers are easy  How about negative numbers? Please read signed multiplication in textbook (Ch 3.3) Please read signed multiplication in textbook (Ch 3.3)

Faster Multiply A1 & BA0 & B A2 & B A3 & B A31 & B P1P2P0 P31 P32-P63

Simple Combinational Multiplier t PD = 10 * t PD,FA not 16 NB: this circuit only works for nonnegative operands Components used: N 2 full adders (FA) N 2 AND gates t PD = (2*(N-1) + N) * t PD,FA

Even Faster Multiply  Even faster designs for multiplication e.g., “Carry-Save Multiplier” e.g., “Carry-Save Multiplier”  covered in advanced courses

Division Flow Chart Hardware Implementation See example in textbook (Fig 3.11)

Floating-Point Numbers & Arithmetic

Floating-Point Arithmetic Reading: Study Chapter 3.5 Skim 3.6 and 3.8 if ((A + A) - A == A) { SelfDestruct() }

Why do we need floating point?  Several reasons: Many numeric applications need numbers over a huge range Many numeric applications need numbers over a huge range  e.g., nanoseconds to centuries Most scientific applications require real numbers (e.g.  ) Most scientific applications require real numbers (e.g.  )  But so far we only have integers. What do we do? We could implement the fractions explicitly We could implement the fractions explicitly  e.g.: ½, 1023/ We could use bigger integers We could use bigger integers  e.g.: 64-bit integers Floating-point representation is often better Floating-point representation is often better  has some drawbacks too!

Recall Scientific Notation  Recall scientific notation from high school Numbers represented in parts: Numbers represented in parts:  42 = x 10 1  1024 = x 10 3  = x  Arithmetic is done in pieces x x x x 10 3 = x 10 3 = x 10 2 Before adding, we must match the exponents, effectively “denormalizing” the smaller magnitude number We then “normalize” the final result so there is one digit to the left of the decimal point and adjust the exponent accordingly. Significant Digits Exponent

Multiplication in Scientific Notation  Is straightforward: Multiply together the significant parts Multiply together the significant parts Add the exponents Add the exponents Normalize if required Normalize if required  Examples: x x 10 3 x x x x = x 10 1 = x x x 10 1 x x x x = x = x = x 10 0 (Normalized) In multiplication, how far is the most you will ever normalize? In addition?

Binary Floating-Point Notation  IEEE single precision floating-point format Example: (0x in hexadecimal) Example: (0x in hexadecimal)  Three fields: Sign bit (S) Sign bit (S) Exponent (E): Unsigned “Bias 127” 8-bit integer Exponent (E): Unsigned “Bias 127” 8-bit integer  E = Exponent  Exponent = (132) – 127 = 5 Significand (F): Unsigned fixed-point with “hidden 1” Significand (F): Unsigned fixed-point with “hidden 1”  Significand = “1” = Final value: N = -1 S (1+F) x 2 E-127 = -1 0 (1.3125) x 2 5 = 42 Final value: N = -1 S (1+F) x 2 E-127 = -1 0 (1.3125) x 2 5 = “F” Significand (Mantissa) - 1 “E” Exponent “S” Sign Bit

Example Numbers  One Sign = +, Exponent = 0, Significand = 1.0 Sign = +, Exponent = 0, Significand = 1.0  1 = -1 0 (1.0) x 2 0  S = 0, E = , F = 1.0 – ‘1’  = 0x3f  One-half Sign = +, Exponent = -1, Significand = 1.0 Sign = +, Exponent = -1, Significand = 1.0  ½ = -1 0 (1.0) x 2 -1  S = 0, E = , F = 1.0 – ‘1’  = 0x3f  Minus Two Sign = -, Exponent = 1, Significand = 1.0 Sign = -, Exponent = 1, Significand = 1.0  -2 = -1 1 (1.0) x 2 1  = 0xc

Zeros  How do you represent 0? Sign = ?, Exponent = ?, Significand = ? Sign = ?, Exponent = ?, Significand = ?  Here’s where the hidden “1” comes back to bite you  Hint: Zero is small. What’s the smallest number you can generate? –Exponent = -127, Signficand = 1.0 –-1 0 (1.0) x = x  IEEE Convention When E = 0 (Exponent = -127), we’ll interpret numbers differently… When E = 0 (Exponent = -127), we’ll interpret numbers differently…  = 0 not 1.0 x  = -0 not -1.0 x Yes, there are “2” zeros. Setting E=0 is also used to represent a few other small numbers besides 0. In all of these numbers there is no “hidden” one assumed in F, and they are called the “unnormalized numbers”. WARNING: If you rely these values you are skating on thin ice!

Infinities  IEEE floating point also reserves the largest possible exponent to represent “unrepresentable” large numbers Positive Infinity: S = 0, E = 255, F = 0 Positive Infinity: S = 0, E = 255, F = 0  = +∞  0x7f Negative Infinity: S = 1, E = 255, F = 0 Negative Infinity: S = 1, E = 255, F = 0  = -∞  0xff Other numbers with E = 255 (F ≠ 0) are used to represent exceptions or Not-A-Number (NAN) Other numbers with E = 255 (F ≠ 0) are used to represent exceptions or Not-A-Number (NAN)  √-1, -∞ x 42, 0/0, ∞/∞, log(-5) It does, however, attempt to handle a few special cases: It does, however, attempt to handle a few special cases:  1/0 = + ∞, -1/0 = - ∞, log(0) = - ∞

denorm gap Low-End of the IEEE Spectrum  “Denormalized Gap” The gap between 0 and the next representable normalized number is much larger than the gaps between nearby representable numbers The gap between 0 and the next representable normalized number is much larger than the gaps between nearby representable numbers IEEE standard uses denormalized numbers to fill in the gap, making the distances between numbers near 0 more alike IEEE standard uses denormalized numbers to fill in the gap, making the distances between numbers near 0 more alike  Denormalized numbers have a hidden “0” and…  … a fixed exponent of -126  X = -1 S (0.F) –Zero is represented using 0 for the exponent and 0 for the mantissa. Either, +0 or -0 can be represented, based on the sign bit bias 2 1-bias 2 2-bias normal numbers with hidden bit

Floating point AIN’T NATURAL  It is CRUCIAL for computer scientists to know that Floating Point arithmetic is NOT the arithmetic you learned since childhood  1.0 is NOT EQUAL to 10*0.1 (Why?) 1.0 * 10.0 == * 10.0 == * 10.0 != * 10.0 != decimal == 1/16 + 1/32 + 1/ / / … == 0.1 decimal == 1/16 + 1/32 + 1/ / / … ==  … In decimal 1/3 is a repeating fraction … In decimal 1/3 is a repeating fraction … If you quit at some fixed number of digits, then 3 * 1/3 != 1 If you quit at some fixed number of digits, then 3 * 1/3 != 1  Floating Point arithmetic IS NOT associative x + (y + z) is not necessarily equal to (x + y) + z x + (y + z) is not necessarily equal to (x + y) + z  Addition may not even result in a change (x + 1) MAY == x (x + 1) MAY == x

Floating Point Disasters  Scud Missiles get through, 28 die In 1991, during the 1st Gulf War, a Patriot missile defense system let a Scud get through, hit a barracks, and kill 28 people. The problem was due to a floating-point error when taking the difference of a converted & scaled integer. (Source: Robert Skeel, "Round-off error cripples Patriot Missile", SIAM News, July 1992.) In 1991, during the 1st Gulf War, a Patriot missile defense system let a Scud get through, hit a barracks, and kill 28 people. The problem was due to a floating-point error when taking the difference of a converted & scaled integer. (Source: Robert Skeel, "Round-off error cripples Patriot Missile", SIAM News, July 1992.)  $7B Rocket crashes (Ariane 5) When the first ESA Ariane 5 was launched on June 4, 1996, it lasted only 39 seconds, then the rocket veered off course and self-destructed. An inertial system, produced a floating-point exception while trying to convert a 64-bit floating-point number to an integer. Ironically, the same code was used in the Ariane 4, but the larger values were never generated ( When the first ESA Ariane 5 was launched on June 4, 1996, it lasted only 39 seconds, then the rocket veered off course and self-destructed. An inertial system, produced a floating-point exception while trying to convert a 64-bit floating-point number to an integer. Ironically, the same code was used in the Ariane 4, but the larger values were never generated (  Intel Ships and Denies Bugs In 1994, Intel shipped its first Pentium processors with a floating-point divide bug. The bug was due to bad look-up tables used to speed up quotient calculations. After months of denials, Intel adopted a no-questions replacement policy, costing $300M. ( In 1994, Intel shipped its first Pentium processors with a floating-point divide bug. The bug was due to bad look-up tables used to speed up quotient calculations. After months of denials, Intel adopted a no-questions replacement policy, costing $300M. (

Floating-Point Multiplication SEFSEF × 24 by 24 round Small ADDER Mux (Shift Right by 1) Control Subtract 127 Add 1 SEF Step 1: Multiply significands Add exponents E R = E 1 + E (do not need twice the bias) Step 2: Normalize result (Result of [1,2) *[1.2) = [1,4) at most we shift right one bit, and fix exponent

Floating-Point Addition

MIPS Floating Point  Floating point “Co-processor” 32 Floating point registers 32 Floating point registers  separate from 32 general purpose registers  32 bits wide each  use an even-odd pair for double precision  Instructions: add.d fd, fs, ft # fd = fs + ft in double precision add.d fd, fs, ft # fd = fs + ft in double precision add.s fd, fs, ft# fd = fs + ft in single precision add.s fd, fs, ft# fd = fs + ft in single precision sub.d, sub.s, mul.d, mul.s, div.d, div.s, abs.d, abs.s sub.d, sub.s, mul.d, mul.s, div.d, div.s, abs.d, abs.s l.d fd, address# load a double from address l.d fd, address# load a double from address l.s, s.d, s.s l.s, s.d, s.s Conversion instructions Conversion instructions Compare instructions Compare instructions Branch (bc1t, bc1f) Branch (bc1t, bc1f)

Chapter Three Summary  From bits to numbers: Computer arithmetic is constrained by limited precision Computer arithmetic is constrained by limited precision Bit patterns have no inherent meaning but standards do exist Bit patterns have no inherent meaning but standards do exist  two’s complement  IEEE 754 floating point Instructions determine “meaning” of the bit patterns Instructions determine “meaning” of the bit patterns  Performance and accuracy … are important so there are many complexities in real machines (i.e., algorithms and implementation). … are important so there are many complexities in real machines (i.e., algorithms and implementation). Accurate numerical computing requires methods quite different from those of the math you learned in grade school. Accurate numerical computing requires methods quite different from those of the math you learned in grade school.