1CS61 C L1 0 F l . Pt . © UC Regent sCS61C - Machine StructuresLecture 10 - Floating Point, Part II andMiscellaneousSeptember 29, 2000David Pattersonhttp://www-inst.eecs.berkeley.edu/~cs61c/2CS61 C L1 0 F l . Pt . © UC Regent sReview° Floating Point numbers approximatevalues that we want to use.° IEEE 754 Floating Point Standard is mostwidely accepted attempt to standardizeinterpretation of such numbers ($1T)° New MIPS registers($f0-$f31), instruct.:• Single Precision (32 bits, 2x10-38… 2x1038):add.s, sub.s, mul.s, div.s• Double Precision (64 bits , 2x10-308…2x10308):add.d, sub.d, mul.d, div.d° Type is not associated with data, bitshave no meaning unless given in context3CS61 C L1 0 F l . Pt . © UC Regent sOverview° Special Floating Point Numbers: NaN,Denorms° IEEE Rounding modes° Floating Point fallacies, hacks° Catchup topics:• Representation of jump, jump and link• Reverse time travel:MIPS machine language-> MIPS assembly language-> C code• Logical, shift instructions (time permiting)4CS61 C L1 0 F l . Pt . © UC Regent sMIPS Floating Point Architecture (1/2)° 1990 Solution: Make a completelyseparate chip that handles only FP.° Coprocessor 1: FP chip• contains 32 32-bit registers: $f0, $f1, …• most registers specified in .s and .dinstruction 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/$f315CS61 C L1 0 F l . Pt . © UC Regent sMIPS Floating Point Architecture (2/2)° 1990 Computer actually containsmultiple 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 betweenmain processor and coprocessors:¥mfc0, mtc0, mfc1, mtc1, etc.° Appendix pages A-70 to A-74 containmany, many more FP operations.6CS61 C L1 0 F l . Pt . © UC Regent sSpecial Numbers° What have we defined so far? (Single Precision)Exponent Significand Object00 00nonzero ???1-254 anything +/- fl. pt. #255 0 +/- infinity255nonzero ???° Professor Kahan had clever ideas;“Waste not, want not”7CS61 C L1 0 F l . Pt . © UC Regent sRepresentation for Not a Number° What do I get if I calculatesqrt(-4.0)or 0/0?• If infinity is not an error, it may be usefulnot to crash program for these too.• Called Not a Number (NaN)• Exponent = 255, Significand nonzero° Why is this useful?• Hope NaNs help with debugging• They contaminate: op(NaN,X) = NaN• OK if calculate but don’t use it• Ask math majors8CS61 C L1 0 F l . Pt . © UC Regent sSpecial Numbers (cont’d)° What have we defined so far?(Single Precision)?Exponent Significand Object00 00 nonzero ???1-254 anything +/- fl. pt. #255 0 +/- infinity255 nonzero NaN9CS61 C L1 0 F l . Pt . © UC Regent sRepresentation for Denorms (1/2)° Problem: There’s a gap amongrepresentable FP numbers around 0• Smallest representable pos num:- a = 1.0… 2 * 2-127 = 2-127• Second smallest representable pos num:- b = 1.000……1 2 * 2-127 = 2-127 + 2-150• a - 0 = 2-127• b - a = 2-150ba0+-Gap!Gap!10CS61 C L1 0 F l . Pt . © UC Regent sRepresentation for Denorms (2/2)° Solution:• We still haven’t used Exponent = 0,Significand nonzero• Denormalized number: no leading 1• Smallest representable pos num:- a = 2-150• Second smallest representable pos num:- b = 2-1490+-11CS61 C L1 0 F l . Pt . © UC Regent sRounding° When we perform math on realnumbers, we have to worry aboutrounding° The actual math carries two extra bitsof precision, and then round to get theproper value° Rounding also occurs whenconverting a double to a singleprecision value, or converting afloating point number to an integer12CS61 C L1 0 F l . Pt . © UC Regent s4 IEEE Rounding Modes° Round towards +infinity• ALWAYS round “up”: 2.001 -> 3• -2.001 -> -2° Round towards -infinity• ALWAYS round “down”: 1.999 -> 1,• -1.999 -> -2° Truncate: 2.001 -> 2, -2.001 -> -2• Just drop the last bits (round towards 0)° Round to (nearest) even• Normal rounding, almost13CS61 C L1 0 F l . Pt . © UC Regent sRound to Even° Round like you learned in grade school° Except if the value is right on theborderline, in which case we round to thenearest EVEN number• 2.5 -> 2• 3.5 -> 4° Insures fairness on calculation• This way, half the time we round up on tie,the other half time we round down• Ask statistics majors° Default C rounding mode; only Java mode14CS61 C L1 0 F l . Pt . © UC Regent sFloating Point Fallacy° FP Add, subtract associative: FALSE!• x = – 1.5 x 1038, y = 1.5 x 1038, and z = 1.0• x + (y + z) = –1.5x1038 + (1.5x1038 + 1.0)= –1.5x1038 + (1.5x1038) = 0.0• (x + y) + z = (–1.5x1038 + 1.5x1038) + 1.0= (0.0) + 1.0 = 1.0° Therefore, Floating Point add, subtractare not associative!• Why? FP result approximates real result!• This exampe: 1.5 x 1038 is so much largerthan 1.0 that 1.5 x 1038 + 1.0 in floatingpoint representation is still 1.5 x 103815CS61 C L1 0 F l . Pt . © UC Regent sCasting floats to ints and vice versa¡(int) exp• Coerces and converts it to the nearestinteger• affected by rounding modes¥i = (int) (3.14159 * f);¡(float) exp• converts integer to nearest floating point¥f = f + (float) i;16CS61 C L1 0 F l . Pt . © UC Regent sint -> float -> int° Will not always work° Large values of integers don’t haveexact floating point representations° Similarly, we may round to the wrongvalueif (i == (int)((float) i)) { printf( true );}17CS61 C L1 0 F l . Pt . © UC Regent sfloat -> int -> float° Will not always work° Small values of floating point don’t have good integer representations° Also rounding errorsif (f == (float)((int) f)) { printf( true );}18CS61 C L1 0 F l . Pt . © UC Regent sAdministrivia° Need to catchup with Homework° Reading assignment: Reading 4.819CS61 C L1 0 F l . Pt . © UC Regent sJ-Format Instructions (1/5)° For branches, we assumed that wewon’t want to branch too far, so wecan specify change in PC.° For general jumps (j and jal), we mayjump to anywhere in memory.° Ideally, we could specify a 32-bitmemory address to jump to.° Unfortunately, we can’t fit both a 6-bitopcode and a 32-bit address into asingle 32-bit word, so we compromise.20CS61 C L1 0 F l . Pt . © UC Regent sJ-Format Instructions (2/5)° Define “fields” of the
View Full Document