IEEE Standard 754 for Binary Floating-Point Arithmetic.

Similar documents
UC Berkeley CS61C : Machine Structures

Precision and Accuracy

UC Berkeley CS61C : Machine Structures

IEEE Standard 754 for Binary Floating-Point Arithmetic.

CS61C : Machine Structures

95% of the folks out there are completely clueless about floating-point.

UCB CS61C : Machine Structures

CS61C : Machine Structures

Lecture 13: (Integer Multiplication and Division) FLOATING POINT NUMBERS

xx.yyyy Lecture #11 Floating Point II Summary (single precision): Precision and Accuracy Fractional Powers of 2 Representation of Fractions

Floating Point Numbers. Lecture 9 CAP

3.5 Floating Point: Overview

CS 61C: Great Ideas in Computer Architecture Floating Point Arithmetic

ecture 25 Floating Point Friedland and Weaver Computer Science 61C Spring 2017 March 17th, 2017

CS61C : Machine Structures

September, 2003 Saeid Nooshabadi

Written Homework 3. Floating-Point Example (1/2)

CS61C : Machine Structures

September, Saeid Nooshabadi. Overview IEEE 754 Standard COMP Microprocessors and Embedded Systems

Computer Architecture and IC Design Lab. Chapter 3 Part 2 Arithmetic for Computers Floating Point

CS61C Floating Point Operations & Multiply/Divide. Lecture 9. February 17, 1999 Dave Patterson (http.cs.berkeley.edu/~patterson)

CSCI 402: Computer Architectures. Arithmetic for Computers (4) Fengguang Song Department of Computer & Information Science IUPUI.

Review 1/2 Big Idea: Instructions determine meaning of data; nothing inherent inside the data Characters: ASCII takes one byte

CS61C : Machine Structures

95% of the folks out there are completely clueless about floating-point.

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture Performance and Floating Point Arithmetic

Outline. What is Performance? Restating Performance Equation Time = Seconds. CPU Performance Factors

CS 61C: Great Ideas in Computer Architecture Performance and Floating-Point Arithmetic

Floating Point COE 308. Computer Architecture Prof. Muhamed Mudawar. Computer Engineering Department King Fahd University of Petroleum and Minerals

CS61C : Machine Structures

Floating Point Arithmetic

Arithmetic for Computers. Hwansoo Han

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture Performance and Floating-Point Arithmetic

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Chapter 3 Arithmetic for Computers

Floating Point Arithmetic. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS61C : Machine Structures

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Lecture 10: Floating Point, Digital Design

Floating Point January 24, 2008

System Programming CISC 360. Floating Point September 16, 2008

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

CS 61C: Great Ideas in Computer Architecture Lecture 17: Performance and Floa.ng Point Arithme.c

Chapter 3 Arithmetic for Computers (Part 2)

UC Berkeley CS61C : Machine Structures

Floating Point Numbers

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Giving credit where credit is due

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Giving credit where credit is due

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Floating Point Numbers

CS61C : Machine Structures

Floating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon

CS61C L10 MIPS Instruction Representation II, Floating Point I (6)

Brought to you by CalLUG (UC Berkeley GNU/Linux User Group). Tuesday, September 20, 6-8 PM in 100 GPB.

Lecture #6 Intro MIPS; Load & Store Assembly Variables: Registers (1/4) Review. Unlike HLL like C or Java, assembly cannot use variables

CO Computer Architecture and Programming Languages CAPL. Lecture 15

Floating-Point Arithmetic

Floating Point Arithmetic

Floating-Point Arithmetic

Chapter 3. Arithmetic Text: P&H rev

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CS61C : Machine Structures

Thomas Polzer Institut für Technische Informatik

UC Berkeley CS61C : Machine Structures

EE 109 Unit 19. IEEE 754 Floating Point Representation Floating Point Arithmetic

COMPUTER ORGANIZATION AND DESIGN

Lecture 33 Caches III What to do on a write hit? Block Size Tradeoff (1/3) Benefits of Larger Block Size

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output }

Representing and Manipulating Floating Points

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

Floating Point Numbers

Floating Point Numbers

CS61C : Machine Structures

CS 6210 Fall 2016 Bei Wang. Lecture 4 Floating Point Systems Continued

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

CS61c Midterm Review (fa06) Number representation and Floating points From your friendly reader

Representing and Manipulating Floating Points

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

Foundations of Computer Systems

Computer Organization: A Programmer's Perspective

9 Floating-Point. 9.1 Objectives. 9.2 Floating-Point Number Representation. Value = ± (1.F) 2 2 E Bias

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

Computer Systems C S Cynthia Lee

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

CS61C : Machine Structures

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

Representing and Manipulating Floating Points. Jo, Heeseung

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CS61C : Machine Structures

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng

Representing and Manipulating Floating Points

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

Data Representation Floating Point

Data Representation Floating Point

CSE 2021 Computer Organization. Hugh Chesser, CSEB 1012U W4-W

Transcription:

CS61C L14 MIPS Instruction Representation II (1) inst.eecs.berkeley.edu/~cs61c UC Berkeley CS61C : Machine Structures Lecture 16 Floating Point II 27-2-23 Lecturer SOE Dan Garcia As Pink Floyd crooned: Is anybody out there? www.cs.berkeley.edu/~ddgarcia Google takes on Office! Google Apps: premium services (email, instant vs messaging, calendar, web creation, word processing, spreadsheets). Data is there. www.nytimes.com/27/2/22/technology/22google.html 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): 31 3 23 22 S Exponent Significand 1 bit 8 bits 23 bits (-1) S x (1 + Significand) x 2 (Exponent-127) Double precision identical, except with exponent bias of 123 (half, quad similar) CS61C L14 MIPS Instruction Representation II (2) Exponent tells Significand how much (2 i ) to count by (, 1/4, 1/2, 1, 2, ) Father of the Floating point standard IEEE Standard 754 for Binary Floating-Point Arithmetic. 1989 ACM Turing Award Winner! Prof. Kahan www.cs.berkeley.edu/~wkahan/ /ieee754status/754story.html Precision and Accuracy Don t confuse these two terms! 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. 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 L14 MIPS Instruction Representation II (3) CS61C L14 MIPS Instruction Representation II (4) Representation for ± In FP, divide by should produce ±, not overflow. Why? OK to do further computations with E.g., X/ > Y may be a valid comparison Ask math majors IEEE 754 represents ± Most positive exponent reserved for Significands all zeroes Representation for Represent? exponent all zeroes significand all zeroes What about sign? Both cases valid. +: -: 1 CS61C L14 MIPS Instruction Representation II (5) CS61C L14 MIPS Instruction Representation II (6)

CS61C L14 MIPS Instruction Representation II (7) Special Numbers What have we defined so far? (Single Precision) nonzero??? 255 +/- 255 nonzero??? Professor Kahan had clever ideas; Waste not, want not We ll talk about Exp=,255 & Sig!= later Representation for Not a Number What do I get if I calculate sqrt(-4.)or /? 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 L14 MIPS Instruction Representation II (8) Representation for Denorms (1/2) Problem: There s a gap among representable FP numbers around Smallest representable pos num: a = 1. 2 * 2-126 = 2-126 Second smallest representable pos num: b = 1. 1 2 * 2-126 = (1 +. 1 2 ) * 2-126 = (1 + 2-23 ) * 2-126 = 2-126 + 2-149 a - = 2-126 b - a = 2-149 - Gaps! b a Normalization and implicit 1 is to blame! + Representation for Denorms (2/2) Solution: We still haven t used Exponent =, Significand nonzero Denormalized number: no (implied) leading 1, implicit exponent = -126. Smallest representable pos num: a = 2-149 Second smallest representable pos num: b = 2-148 - + CS61C L14 MIPS Instruction Representation II (9) CS61C L14 MIPS Instruction Representation II (1) Special Numbers Summary Reserve exponents, significands: nonzero Denorm 255 +/- 255 nonzero NaN CS61C L14 MIPS Instruction Representation II (11) Administrivia Project 2 up on Thurs, due next next Fri After Midterm, just as you wanted There are bugs on the Green sheet! Check the course web page for details If you didn t attend Stallman s talk, you need to re-assess your priorities! He s talking AGAIN today (5-6:3pm) in 36 Soda The Free Software Movement and the GNU/Linux Operating System Richard Stallman launched the development of the GNU operating system (see www.gnu.org) in 1984. GNU is free software: everyone has the freedom to copy it and redistribute it, as well as to make changes either large or small. The GNU/Linux system, basically the GNU operating system with Linux added, is used on tens of millions of computers today. CS61C L14 MIPS Instruction Representation II (12)

CS61C L14 MIPS Instruction Representation II (13) 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 IEEE FP Rounding Modes Examples in decimal (but, of course, IEEE754 in binary) Round towards + ALWAYS round up : 2.1 3, -2.1-2 Round towards - ALWAYS round down : 1.999 1, -1.999-2 Truncate Just drop the last bits (round towards ) 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 CS61C L14 MIPS Instruction Representation II (14) 1 1 1 111 What is the decimal equivalent of the floating pt # above? 1: -1.75 2: -3.5 3: -3.75 4: -7 5: -7.5 6: -15 7: -7 * 2^129 8: -129 * 2^7 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 L14 MIPS Instruction Representation II (15) CS61C L14 MIPS Instruction Representation II (17) 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) And in conclusion Reserve exponents, significands: nonzero Denorm 255 +/- 255 nonzero NaN 4 rounding modes (default: unbiased) MIPS FL ops complicated, expensive CS61C L14 MIPS Instruction Representation II (19) CS61C L14 MIPS Instruction Representation II (21)

CS61C L14 MIPS Instruction Representation II (22) Bonus slides These are extra slides that used to be included in lecture notes, but have been moved to this, the bonus area to serve as a supplement. The slides will appear in the order they would have in the normal presentation 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 L14 MIPS Instruction Representation II (23) 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. 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 L14 MIPS Instruction Representation II (24) CS61C L14 MIPS Instruction Representation II (25) MIPS Floating Point Architecture (3/4) 199 Solution: Make a completely separate chip that handles only FP. Coprocessor 1: FP chip contains 32 32-bit registers: $f, $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: $f/$f1, $f2/$f3,, $f3/$f31 MIPS Floating Point Architecture (4/4) 199 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: mfc, mtc, mfc1, mtc1, etc. Appendix pages A-7 to A-74 contain many, many more FP operations. CS61C L14 MIPS Instruction Representation II (26) CS61C L14 MIPS Instruction Representation II (27)

CS61C L14 MIPS Instruction Representation II (28) Example: Representing 1/3 in MIPS 1/3 =.33333 1 =.25 +.625 +.15625 +.39625 + = 1/4 + 1/16 + 1/64 + 1/256 + = 2-2 + 2-4 + 2-6 + 2-8 + =.11111 2 * 2 = 1.11111 2 * 2-2 Sign: Exponent = -2 + 127 = 125 = 111111 Significand = 11111 Casting floats to ints and vice versa (int) floating_point_expression Coerces and converts it to the nearest integer (C uses truncation) i = (int) (3.14159 * f); (float) integer_expression converts integer to nearest floating point f = f + (float) i; 111 111 11 11 11 11 11 1 CS61C L14 MIPS Instruction Representation II (29) int float int float int float if (i == (int)((float) i)) { } printf( true ); Will not always print true Most large values of integers don t have exact floating point representations! What about double? if (f == (float)((int) f)) { } printf( true ); Will not always print true Small floating point numbers (<1) don t have integer representations For other numbers, rounding errors CS61C L14 MIPS Instruction Representation II (3) CS61C L14 MIPS Instruction Representation II (31) Floating Point Fallacy FP add associative: FALSE! x = 1.5 x 1 38, y = 1.5 x 1 38, and z = 1. x + (y + z) = 1.5x1 38 + (1.5x1 38 + 1.) = 1.5x1 38 + (1.5x1 38 ) =. (x + y) + z = ( 1.5x1 38 + 1.5x1 38 ) + 1. = (.) + 1. = 1. Therefore, Floating Point add is not associative! Why? FP result approximates real result! This example: 1.5 x 1 38 is so much larger than 1. that 1.5 x 1 38 + 1. in floating point representation is still 1.5 x 1 38 CS61C L14 MIPS Instruction Representation II (32)