CS61C L16 Floating Point II (1) Garcia, Fall 2006 © UCB Prof Smoot given Nobel!!  Prof George Smoot joins the ranks of the most distinguished faculty.

Slides:



Advertisements
Similar presentations
Spring 2013 Advising Starts this week! CS2710 Computer Organization1.
Advertisements

Computer Organization CS224 Fall 2012 Lesson 19. Floating-Point Example  What number is represented by the single-precision float …00 
CSCE 212 Chapter 3: Arithmetic for Computers Instructor: Jason D. Bakos.
Fabián E. Bustamante, Spring 2007 Floating point Today IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.
Socratic Method, Understanding Floats. Float Cheat Sheet ExponentSignificandValue 000 0nonzeroDenorm 1~ 2 E - 2Anything+/- fl. Pt # 2 E - 1 (all 1s)0+/-
Lecture 16: Computer Arithmetic Today’s topic –Floating point numbers –IEEE 754 representations –FP arithmetic Reminder –HW 4 due Monday 1.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
1 CS61C L9 Fl. Pt. © UC Regents CS61C - Machine Structures Lecture 9 - Floating Point, Part I September 27, 2000 David Patterson
Integer Multiplication (1/3)
CS61C L11 Floating Point II (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Microprocessors The MIPS Architecture (Floating Point Instruction Set) Mar 26th, 2002.
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.
CS 61C L12 Pseudo (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #12: FP II & Pseudo Instructions.
1 CS61C L10 Fl. Pt. © UC Regents CS61C - Machine Structures Lecture 10 - Floating Point, Part II and Miscellaneous September 29, 2000 David Patterson
Inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #6 – Number Representation, IEEE FP Greet class Jeremy Huddleston.
CS 61C L16 : Floating Point II (1) Kusalo, Spring 2005 © UCB Lecturer NSOE Steven Kusalo inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture.
CS 61C L16 : Floating Point II (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
1 EECS Components and Design Techniques for Digital Systems Lec 19 – Fixed Point & Floating Point Arithmetic 10/23/2007 David Culler Electrical Engineering.
CS61C L14 MIPS Instruction Representation II (1) Garcia, Spring 2007 © UCB Google takes on Office!  Google Apps: premium “services” ( , instant messaging,
CS61C L15 Floating Point I (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS 61C L15 Floating Point I (1) Krause, Spring 2005 © UCB This day in history… TA Danny Krause inst.eecs.berkeley.edu/~cs61c-td inst.eecs.berkeley.edu/~cs61c.
CS61C L16 Floating Point II (1)Mowery, Spring 2008 © UCB TA Oddinaire Keaton Mowery inst.eecs.berkeley.edu/~cs61c UC Berkeley.
CS 61C L15 Floating Point I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
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.
CS61C L11 Floating Point II (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #11.
CS 61C L10 Floating Point (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #10: Floating Point
CS61C L15 Floating Point I (1) Jacobs, Spring 2008 © UCB TA Ordinaire Dave Jacobs inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS61C L15 Floating Point I (1) Garcia, Fall 2006 © UCB #16 Cal rolls over OSU  Behind the arm of Nate Longshore’s 341 yds passing & 4 TDs, the Bears roll.
CS 61C L16 : Floating Point II (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Systems Architecture Lecture 14: Floating Point Arithmetic
IT 251 Computer Organization and Architecture Introduction to Floating Point Numbers Chia-Chi Teng.
Csci 136 Computer Architecture II – Floating-Point Arithmetic
CEN 316 Computer Organization and Design Computer Arithmetic Floating Point Dr. Mansour AL Zuair.
CS61C L11 Floating Point (1) Pearce, Summer 2010 © UCB inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 11 Floating Point
Ellen Spertus MCS 111 October 11, 2001 Floating Point Arithmetic.
Computing Systems Basic arithmetic for computers.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /14/2013 Lecture 16: Floating Point Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
Computer Arithmetic II Instructor: Mozafar Bag-Mohammadi Spring 2006 University of Ilam.
Lecture 9: Floating Point
Computer Arithmetic II Instructor: Mozafar Bag-Mohammadi Ilam University.
CSC 221 Computer Organization and Assembly Language
Floating Point Representation for non-integral numbers – Including very small and very large numbers Like scientific notation – –2.34 × –
Computer Arithmetic Floating Point. We need a way to represent –numbers with fractions, e.g., –very small numbers, e.g., –very large.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
FLOATING POINT ARITHMETIC P&H Slides ECE 411 – Spring 2005.
CS 61C L3.2.1 Floating Point 1 (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture Floating Point Kurt Meinz inst.eecs.berkeley.edu/~cs61c.
CHAPTER 3 Floating Point.
CDA 3101 Fall 2013 Introduction to Computer Organization
10/7/2004Comp 120 Fall October 7 Read 5.1 through 5.3 Register! Questions? Chapter 4 – Floating Point.
Floating Point Representations
Floating Point Representations
Computer Architecture & Operations I
Topics IEEE Floating Point Standard Rounding Floating Point Operations
CS/COE0447 Computer Organization & Assembly Language
Floating Point Arithmetics
inst.eecs.berkeley.edu/~cs61c UC Berkeley CS61C : Machine Structures
CS 61C: Great Ideas in Computer Architecture Floating Point Arithmetic
CS 105 “Tour of the Black Holes of Computing!”
How to represent real numbers
Computer Arithmetic Multiplication, Floating Point
Hello to Robb Neuschwander listening from Louisville, KY!
CS 105 “Tour of the Black Holes of Computing!”
October 17 Chapter 4 – Floating Point Read 5.1 through 5.3 1/16/2019
cnn.com/2004/ALLPOLITICS/10/05/debate.main/
Floating Point Numbers
Lecturer PSOE Dan Garcia
Inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #11: FP II & Pseudo Instructions Andy Carle Greet class.
CS 105 “Tour of the Black Holes of Computing!”
CDA 3101 Spring 2016 Introduction to Computer Organization
Presentation transcript:

CS61C L16 Floating Point II (1) Garcia, Fall 2006 © UCB Prof Smoot given Nobel!!  Prof George Smoot joins the ranks of the most distinguished faculty in the world (Cal has 7 active winners) when he was awarded the Nobel Prize in Physics for images that confirmed the Big Bang Theory. Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c UC Berkeley CS61C : Machine Structures Lecture 16 Floating Point II

CS61C L16 Floating Point II (2) Garcia, Fall 2006 © UCB Review Floating Point lets us: Represent numbers containing both integer and fractional parts; makes efficient use of available bits. Store approximate values for very large and very small #s. IEEE 754 Floating Point Standard is most widely accepted attempt to standardize interpretation of such numbers (Every desktop or server computer sold since ~1997 follows these conventions) Summary (single precision): 0 31 SExponent Significand 1 bit8 bits23 bits (-1) S x (1 + Significand) x 2 (Exponent-127) Double precision identical, except with exponent bias of 1023 (half, quad similar) Exponent tells Significand how much (2 i ) to count by (…, 1/4, 1/2, 1, 2, …)

CS61C L16 Floating Point II (3) Garcia, Fall 2006 © UCB Precision and Accuracy Precision is a count of the number bits in a computer word used to represent a value. Accuracy is a measure of the difference between the actual value of a number and its computer representation. Don’t confuse these two terms! High precision permits high accuracy but doesn’t guarantee it. It is possible to have high precision but low accuracy. Example: float pi = 3.14; pi will be represented using all 24 bits of the significant (highly precise), but is only an approximation (not accurate).

CS61C L16 Floating Point II (4) Garcia, Fall 2006 © UCB Representation for ± ∞ In FP, divide by 0 should produce ± ∞, not overflow. Why? OK to do further computations with ∞ E.g., X/0 > Y may be a valid comparison Ask math majors IEEE 754 represents ± ∞ Most positive exponent reserved for ∞ Significands all zeroes

CS61C L16 Floating Point II (5) Garcia, Fall 2006 © UCB Representation for 0 Represent 0? exponent all zeroes significand all zeroes What about sign? Both cases valid. +0: :

CS61C L16 Floating Point II (6) Garcia, Fall 2006 © UCB Special Numbers What have we defined so far? (Single Precision) ExponentSignificandObject nonzero??? 1-254anything+/- fl. pt. # 2550+/- ∞ 255nonzero??? Professor Kahan had clever ideas; “Waste not, want not” We’ll talk about Exp=0,255 & Sig!=0 later

CS61C L16 Floating Point II (7) Garcia, Fall 2006 © UCB Representation for Not a Number What do I get if I calculate sqrt(-4.0) or 0/0 ? If ∞ not an error, these shouldn’t be either Called Not a Number (NaN) Exponent = 255, Significand nonzero Why is this useful? Hope NaNs help with debugging? They contaminate: op(NaN, X) = NaN

CS61C L16 Floating Point II (8) Garcia, Fall 2006 © UCB Representation for Denorms (1/2) Problem: There’s a gap among representable FP numbers around 0 Smallest representable pos num: a = 1.0… 2 * = Second smallest representable pos num: b = 1.000……1 2 * = a - 0 = b - a = b a Gaps! Normalization and implicit 1 is to blame!

CS61C L16 Floating Point II (9) Garcia, Fall 2006 © UCB Representation for Denorms (2/2) Solution: We still haven’t used Exponent = 0, Significand nonzero Denormalized number: no (implied) leading 1, implicit exponent = Smallest representable pos num: a = Second smallest representable pos num: b =

CS61C L16 Floating Point II (10) Garcia, Fall 2006 © UCB Special Numbers Summary Reserve exponents, significands: ExponentSignificandObject 000 0nonzeroDenorm 1-254anything+/- fl. pt. # 2550+/- ∞ 255nonzeroNaN

CS61C L16 Floating Point II (11) Garcia, Fall 2006 © UCB Administrivia HW4 due tonight Project 2 was up on Monday, due next Fri Pretend it is due on Wed so you have more time to study. We’ve made it easier than past project 2s… There are bugs on the Green sheet! Check the course web page for details

CS61C L16 Floating Point II (12) Garcia, Fall 2006 © UCB Rounding When we perform math on real numbers, we have to worry about rounding to fit the result in the significant field. The FP hardware carries two extra bits of precision, and then round to get the proper value Rounding also occurs when converting: double to a single precision value, or floating point number to an integer

CS61C L16 Floating Point II (13) Garcia, Fall 2006 © UCB IEEE FP Rounding Modes Round towards + ∞ ALWAYS round “up”:  3,  -2 Round towards - ∞ ALWAYS round “down”:  1,  -2 Truncate Just drop the last bits (round towards 0) Unbiased (default mode). Midway? Round to even Normal rounding, almost: 2.4  2, 2.6  3, 2.5  2, 3.5  4 Round like you learned in grade school (nearest int) Except if the value is right on the borderline, in which case we round to the nearest EVEN number Insures fairness on calculation This way, half the time we round up on tie, the other half time we round down. Tends to balance out inaccuracies Examples in decimal (but, of course, IEEE754 in binary)

CS61C L16 Floating Point II (14) Garcia, Fall 2006 © UCB Casting floats to ints and vice versa ( int) floating_point_expression Coerces and converts it to the nearest integer (C uses truncation) i = (int) ( * f); (float) integer_expression converts integer to nearest floating point f = f + (float) i;

CS61C L16 Floating Point II (15) Garcia, Fall 2006 © UCB FP Addition More difficult than with integers Can’t just add significands How do we do it? De-normalize to match exponents Add significands to get resulting one Keep the same exponent Normalize (possibly changing exponent) Note: If signs differ, just perform a subtract instead.

CS61C L16 Floating Point II (16) Garcia, Fall 2006 © UCB MIPS Floating Point Architecture (1/4) MIPS has special instructions for floating point operations: Single Precision: add.s, sub.s, mul.s, div.s Double Precision: add.d, sub.d, mul.d, div.d These instructions are far more complicated than their integer counterparts. They require special hardware and usually they can take much longer to compute.

CS61C L16 Floating Point II (17) Garcia, Fall 2006 © UCB MIPS Floating Point Architecture (2/4) Problems: It’s inefficient to have different instructions take vastly differing amounts of time. Generally, a particular piece of data will not change from FP to int, or vice versa, within a program. So only one type of instruction will be used on it. Some programs do no floating point calculations It takes lots of hardware relative to integers to do Floating Point fast

CS61C L16 Floating Point II (18) Garcia, Fall 2006 © UCB MIPS Floating Point Architecture (3/4) 1990 Solution: Make a completely separate chip that handles only FP. Coprocessor 1: FP chip contains bit registers: $f0, $f1, … most registers specified in.s and.d instruction refer to this set separate load and store: lwc1 and swc1 (“load word coprocessor 1”, “store …”) Double Precision: by convention, even/odd pair contain one DP FP number: $f0 / $f1, $f2 / $f3, …, $f30 / $f31

CS61C L16 Floating Point II (19) Garcia, Fall 2006 © UCB MIPS Floating Point Architecture (4/4) 1990 Computer actually contains multiple separate chips: Processor: handles all the normal stuff Coprocessor 1: handles FP and only FP; more coprocessors?… Yes, later Today, cheap chips may leave out FP HW Instructions to move data between main processor and coprocessors: mfc0, mtc0, mfc1, mtc1, etc. Appendix pages A-70 to A-74 contain many, many more FP operations.

CS61C L16 Floating Point II (20) Garcia, Fall 2006 © UCB 1: : : : -7 5: : -15 7: -7 * 2^129 8: -129 * 2^7 Peer Instruction What is the decimal equivalent of the floating pt # above?

CS61C L16 Floating Point II (21) Garcia, Fall 2006 © UCB Peer Instruction Answer What is the decimal equivalent of: SExponentSignificand (-1) S x (1 + Significand) x 2 (Exponent-127) (-1) 1 x ( ) x 2 ( ) -1 x (1.111) x 2 (2) 1: : : : -7 5: : -15 7: -7 * 2^129 8: -129 * 2^

CS61C L16 Floating Point II (22) Garcia, Fall 2006 © UCB Peer Instruction 1. Converting float -> int -> float produces same float number 2. Converting int -> float -> int produces same int number 3. FP add is associative: (x+y)+z = x+(y+z) ABC 1: FFF 2: FFT 3: FTF 4: FTT 5: TFF 6: TFT 7: TTF 8: TTT

CS61C L16 Floating Point II (23) Garcia, Fall 2006 © UCB Peer Instruction Answer 1. Converting a float -> int -> float produces same float number 2. Converting a int -> float -> int produces same int number 3. FP add is associative (x+y)+z = x+(y+z) > 3 -> bits for signed int, but 24 for FP mantissa? F A L S E 3. x = biggest pos #, y = -x, z = 1 (x != inf) 10 ABC 1: FFF 2: FFT 3: FTF 4: FTT 5: TFF 6: TFT 7: TTF 8: TTT

CS61C L16 Floating Point II (24) Garcia, Fall 2006 © UCB Peer Instruction Let f(1,2) = # of floats between 1 and 2 Let f(2,3) = # of floats between 2 and 3 1: f(1,2) < f(2,3) 2: f(1,2) = f(2,3) 3: f(1,2) > f(2,3)

CS61C L16 Floating Point II (25) Garcia, Fall 2006 © UCB Peer Instruction Answer 1: f(1,2) < f(2,3) 2: f(1,2) = f(2,3) 3: f(1,2) > f(2,3) Let f(1,2) = # of floats between 1 and 2 Let f(2,3) = # of floats between 2 and 3

CS61C L16 Floating Point II (26) Garcia, Fall 2006 © UCB “And in conclusion…” Reserve exponents, significands: ExponentSignificandObject 000 0nonzeroDenorm 1-254anything+/- fl. pt. # 2550+/- ∞ 255nonzeroNaN 4 rounding modes (default: unbiased) MIPS FL ops complicated, expensive

CS61C L16 Floating Point II (27) Garcia, Fall 2006 © UCB Example: Representing 1/3 in MIPS 1/3 = … 10 = … = 1/4 + 1/16 + 1/64 + 1/256 + … = … = … 2 * 2 0 = … 2 * 2 -2 Sign: 0 Exponent = = 125 = Significand = …

CS61C L16 Floating Point II (28) Garcia, Fall 2006 © UCB int  float  int Will not always print “true” Most large values of integers don’t have exact floating point representations! What about double ? if (i == (int)((float) i)) { printf(“true”); }

CS61C L16 Floating Point II (29) Garcia, Fall 2006 © UCB float  int  float Will not always print “true” Small floating point numbers (<1) don’t have integer representations For other numbers, rounding errors if (f == (float)((int) f)) { printf(“true”); }

CS61C L16 Floating Point II (30) Garcia, Fall 2006 © UCB Floating Point Fallacy FP add associative: FALSE! x = – 1.5 x 10 38, y = 1.5 x 10 38, and z = 1.0 x + (y + z)= –1.5x (1.5x ) = –1.5x (1.5x10 38 ) = 0.0 (x + y) + z= (–1.5x x10 38 ) = (0.0) = 1.0 Therefore, Floating Point add is not associative! Why? FP result approximates real result! This example: 1.5 x is so much larger than 1.0 that 1.5 x in floating point representation is still 1.5 x 10 38