CS61C Machine Structures Lecture 10 Floating Point Part II and Miscellaneous September 29 2000 David Patterson http www inst eecs berkeley edu cs61c CS61C L10 F 1 Review Floating Point numbers approximate values that we want to use IEEE 754 Floating Point Standard is most widely accepted attempt to standardize interpretation 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 bits have no meaning unless given in context2 CS61C L10 F Overvie w 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 3 CS61C L10 F Special Numbers What have we defined so far Single Precision Exponent 0 0 Significand Object 0 0 nonzero 1 254 anything fl pt 255 0 255 nonzero infinity Professor Kahan had clever ideas Waste not want not CS61C L10 F 4 Representation for Not a Number What do I get if I calculate sqrt 4 0 or 0 0 If infinity is 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 OK if calculate but don t use it Ask math majors CS61C L10 F 5 Special Numbers cont d What have we defined so far Single Precision Exponent Significand Object 0 0 0 0 nonzero 1 254 anything fl pt 255 0 infinity 255 nonzero NaN CS61C L10 F 6 Representation for Denorms 1 2 Problem There s a gap among representable 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 150 Gap Gap b 0 a CS61C L10 F 7 Representation 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 149 CS61C L10 F 0 8 Roundin g When we perform math on real numbers we have to worry about rounding The actual math carries two extra bits of precision and then round to get the proper value Rounding also occurs when converting a double to a single precision value or converting a floating point number to an integer CS61C L10 F 9 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 Just drop the last bits round towards 0 Round to nearest even Normal rounding almost CS61C L10 F 10 Round to Even Round like you learned in grade school Except if the value is right on the borderline in which case we round to the nearest 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 This is the default rounding mode CS61C L10 F 11 Casting floats to ints and vice versa int exp Coerces and converts it to the nearest integer affected by rounding modes i int 3 14159 f float exp converts integer to nearest floating point f f float i CS61C L10 F 12 int float int if i int float i printf true Will not always work Large values of integers don t have exact floating point representations Similarly we may round to the wrong value CS61C L10 F 13 float int float if f float int f printf true Will not always work Small values of floating point don t have good integer representations Also rounding errors CS61C L10 F 14 Floating 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 subtract are not associative Why FP result approximates real result This exampe 1 5 x 1038 is so much larger than 1 0 that 1 5 x 1038 1 0 in floating 38 point representation is still 1 5 x 10 15 CS61C L10 F Administrivia Need to catchup with Homework Reading assignment Reading 4 8 CS61C L10 F 16 J Format Instructions 1 5 For branches we assumed that we won t want to branch too far so we can specify change in PC For general jumps j and jal we may jump to anywhere in memory Ideally we could specify a 32 bit memory address to jump to Unfortunately we can t fit both a 6 bit opcode and a 32 bit address into a single 32 bit word so we compromise CS61C L10 F 17 J Format Instructions 2 5 Define fields of the following number of bits each 6 bits 26 bits As usual each field has a name opcode target address Key Concepts Keep opcode field identical to R format and I format for consistency Combine all other fields to make room for target address CS61C L10 F 18 J Format Instructions 3 5 For now we can specify 26 bits of the 32 bit bit address Optimization Note that just like with branches jumps will only jump to word aligned addresses so last two bits are always 00 in binary So let s just take this for granted and not even specify them CS61C L10 F 19 J Format Instructions 4 5 For now we can specify 28 bits of the 32 bit address Where do we get the other 4 bits By definition take the 4 highest order bits from the PC Technically this means that we cannot jump to anywhere in memory but it s adequate 99 9999 of the time since programs aren t that long If we absolutely need to specify a 32 bit address we can always put it in a register and use the jr instruction CS61C L10 F 20 J Format Instructions 5 5 Summary New PC PC 31 28 address 26 bits 00 target Note II means concatenation 4 bits 26 bits 2 bits 32 bit address Understand where each part came from CS61C L10 F 21 Decoding Machine Language How do we convert 1s and 0s to C code For each 32 bits Look at opcode 0 means R Format 2 or 3 mean J Format otherwise I Format Use instruction type to determine which fields exist and convert each field into the decimal equivalent Once we have decimal values write out MIPS assembly code Logically convert this MIPS code into valid C code CS61C L10 F 22 Decoding Example 1 6 Here are six machine language instructions in …
View Full Document
Unlocking...