



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS61C L16 Floating Point II ( 1 ) Mowery, Spring 2008 © UCB
UC Berkeley CS61C : Machine Structures Lecture 16 Floating Point II 2008-02- Electron Filmed for the First Time “ technique that will allow researchers to study the tiny particle's movementsScientists have filmed an electron in motion for the first time, using a new directly.” “ motion. A technology developed within the last few years can generateExtremely short flashes of light are necessary to capture an electron in short pulses of intense laser light, called done.” attosecond pulses, to get the job http://www.livescience.com/strangenews/080225-electron-movie.html CS61C L16 Floating Point II ( 2 ) Mowery, Spring 2008 © 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):
(-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 (2i) to count by (…, 1/4, 1/2, 1, 2, …) CS61C L16 Floating Point II ( 3 ) Mowery, Spring 2008 © UCB “Father” of the Floating point standard IEEE Standard 754 for Binary Floating-Point Arithmetic.
Prof. Kahan 1989 ACM Turing Award Winner! CS61C L16 Floating Point II ( 4 ) Mowery, Spring 2008 © 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). Representation for ±
OK to do further computations with ∞ E.g., X/0 > Y may be a valid comparison Ask math majors
Most positive exponent reserved for ∞ Significands all zeroes Representation for 0
exponent all zeroes significand all zeroes What about sign? Both cases valid. +0: 0 00000000 00000000000000000000000 -0: 1 00000000 00000000000000000000000
CS61C L16 Floating Point II ( 7 ) Mowery, Spring 2008 © UCB Special Numbers
Exponent Significand Object 0 0 0 0 nonzero ??? 1-254 anything +/- fl. pt. # 255 0 +/- ∞ 255 nonzero ???
We’ll talk about Exp=0,255 & Sig!=0 later CS61C L16 Floating Point II ( 8 ) Mowery, Spring 2008 © UCB Representation for Not a Number
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 ( 9 ) Mowery, Spring 2008 © UCB Representation for Denorms (1/2)
Smallest representable pos num: a = 1.0… 2 * 2-126^ = 2- Second smallest representable pos num: b = 1.000…… 1 2 * 2- = (1 + 0.00… 12 ) * 2- = (1 + 2-23) * 2- = 2-126^ + 2- a - 0 = 2-
- +
Normalization and implicit 1 is to blame! CS61C L16 Floating Point II ( 10 ) Mowery, Spring 2008 © UCB Representation for Denorm (2/2)
We still haven’t used Exponent=0, Significand nonzero Denormalized number: no (implied) leading 1, implicit exponent = - Smallest representable pos num: A = 2- Second smallest representable pos num: b = 2-
- + Special Numbers Summary Reserve exponents, significands: 255 nonzero NaN
1-254 Anything +/- fl. Pt # 0 nonzero Denorm
Exponent Significand Object Rounding
double to a single precision value, or floating point number to an integer
CS61C L16 Floating Point II ( 19 ) Mowery, Spring 2008 © UCB Peer Instruction Answer
1: f(1,2) < f(2,3) 2: f(1,2) = f(2,3)
- + CS61C L16 Floating Point II ( 20 ) Mowery, Spring 2008 © UCB “And in conclusion…”
255 nonzero NaN
1-254 Anything +/- fl. Pt # 0 nonzero Denorm
Exponent Significand Object CS61C L16 Floating Point II ( 21 ) Mowery, Spring 2008 © UCB 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 CS61C L16 Floating Point II ( 22 ) Mowery, Spring 2008 © UCB FP Addition
De-normalize to match exponents Add significands to get resulting one Keep the same exponent Normalize (possibly changing exponent)
MIPS Floating Point Architecture (1/4)
Single Precision: add.s, sub.s, mul.s, div.s Double Precision: add.d, sub.d, mul.d, div.d
MIPS Floating Point Architecture (2/4)
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 ( 25 ) Mowery, Spring 2008 © UCB MIPS Floating Point Architecture (3/4)
contains 32 32-bit registers: $f0 , $f1 , … most registers specified in .s and .d instruction refer to this set separate load and store: lwc1 and swc (“load word coprocessor 1”, “store …”) Double Precision: by convention, even/odd pair contain one DP FP number: $f0 / $f1 , $f2 / $f3 , … , $f30 / $f CS61C L16 Floating Point II ( 26 ) Mowery, Spring 2008 © UCB MIPS Floating Point Architecture (4/4)
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
mfc0 , mtc0 , mfc1 , mtc1 , etc.
CS61C L16 Floating Point II ( 27 ) Mowery, Spring 2008 © UCB Example: Representing 1/3 in MIPS 1/ = 0.33333… 10 = 0.25 + 0.0625 + 0.015625 + 0.00390625 + … = 1/4 + 1/16 + 1/64 + 1/256 + … = 2-2^ + 2-4^ + 2-6^ + 2-8^ + … = 0.0101010101… 2 * 2^0 = 1.0101010101… 2 * 2- Sign: 0 Exponent = -2 + 127 = 125 = 01111101 Significand = 0101010101…
CS61C L16 Floating Point II ( 28 ) Mowery, Spring 2008 © UCB Casting floats to ints and vice versa
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; int → float → int
if (i == (int)((float) i)) { printf(“true”); } float → int → float
if (f == (float)((int) f)) { printf(“true”); }