·

Ciência da Computação ·

Arquitetura de Computadores

Send your question to AI and receive an answer instantly

Ask Question

Recommended for you

Preview text

MIPS floatingpoint arithmetic f a I hr fh A l UEEEMW 74 N oom Floatingpoint computations are vital for many applications but correct implementation of floatingpoint hardware and software is very tricky Today well study the IEEE 754 standard for floatingpoint arithmetic Floatingpoint number representations are complex but limited Addition and multiplication operations require several steps The MIPS architecture includes support for floatingpoint arithmetic Machine Problem 2 will include some floatingpoint programming in MIPS Sections this week will review the last three lectures on arithmetic February 26 2003 20012003 Howard Huang 1 Floatingpoint representation IEEE numbers are stored using a kind of scientific notation mantissa x 2Pore We can represent floatingpoint numbers with three binary fields a sign bit s an exponent field e and a fraction field f spo The IEEE 754 standard defines several different precisions Single precision numbers include an 8bit exponent field and a 23bit fraction for a total of 32 bits Double precision numbers have an 11bit exponent field and a 52bit fraction for a total of 64 bits There are also various extended precision formats For example Intel processors use an 80bit format internally February 26 2003 MIPS floatingpoint arithmetic 2 Sign ggg spe The sign bit is O for positive numbers and 1 for negative numbers But unlike integers IEEE values are stored in signed magnitude format February 26 2003 MIPS floatingpoint arithmetic 3 Mantissa spo The field f contains a binary fraction The actual mantissa of the floatingpoint value is 1 f In other words there is an implicit 1 to the left of the binary point For example if f is 01101 the mantissa would be 1011071 There are many ways to write a number in scientific notation but there is always a unique normalized representation with exactly one nonzero digit to the left of the point 0232 x 10 232 x 10 232 x 107 Aside effect is that we get a little more precision there are 24 bits in the mantissa but we only need to store 23 of them February 26 2003 MIPS floatingpoint arithmetic 4 Exponent The e field represents the exponent as a biased number It contains the actual exponent plus 127 for single precision or the actual exponent plus 1023 in double precision This converts all singleprecision exponents from 127 to 127 into unsigned numbers from 0 to 255 and all doubleprecision exponents from 1024 to 1023 into unsigned numbers from 0 to 2047 Two examples with singleprecision numbers are shown below If the exponent is 4 the e field will be 4 127 131 10000011 If e contains 01011101 93 the actual exponent is 93 127 34 Storing a biased exponent before a normalized mantissa means we can compare IEEE values as if they were signed integers February 26 2003 MIPS floatingpoint arithmetic 5 Converting an IEEE 754 number to decimal spo The decimal value of an IEEE number is given by the formula 1 2s x 1 f x 2 Here thes f and e fields are assumed to be in decimal 1 2s is 1 or 1 depending on whether the sign bit is O or 1 We add an implicit 1 to the fraction field f as mentioned earlier Again the bias is either 127 or 1023 for single or double precision February 26 2003 MIPS floatingpoint arithmetic 6 Example IEEEdecimal conversion Lets find the decimal value of the following IEEE number 1 01111100 11000000000000000000000 First convert each individual field to decimal The sign bit s is 1 The e field contains 01111100 1245 The mantissa is 011000 07545 Then just plug these decimal values of s e and f into our formula 1 2s x 1 f x 208 This gives us 1 2 x 1 075 x 21417 175 x 2 021875 February 26 2003 MIPS floatingpoint arithmetic 7 Converting a decimal number to IEEE 754 What is the singleprecision representation of 347625 1 First convert the number to binary 347625 101011011101 2 Normalize the number by shifting the binary point until there is a single 1 to the left 101011011101 x 2 101011011101 x 2 3 The bits to the right of the binary point 01011011101 comprise the fractional field f 4 The number of times you shifted gives the exponent In this case the field e should contain 8 127 135 10000111 5 The number is positive so the sign bit is 0 The final result is 0 10000111 01011011101000000000000 February 26 2003 MIPS floatingpoint arithmetic 8 Special values The smallest and largest possible exponents e00000000 and e11111111 and their double precision counterparts are reserved for special values lf the mantissa is always 1 f then how is 0 represented The fraction field f should be 00000000 The exponent field e contains the value 00000000 With signed magnitude there are two zeroes 00 and 00 There are representations of positive and negative infinity which might sometimes help with instances of overflow The fraction f is O0000000 The exponent field e is set to 11111111 Finally there is a special not a number value which can handle some cases of errors or invalid operations such as 0000 The fraction field f is set to any nonzero value The exponent e will contain 11111111 February 26 2003 MIPS floatingpoint arithmetic 9 Range of singleprecision numbers 1 2s x 1 f x 217 The largest possible normal number is 2 23 x 2127 2128 21 The largest possible e is 11111110 254 The largest possible f is 11111111111111111111111 1 2 And the smallest positive nonzero number is 1 x 217 27 The smallest e is O0000001 1 The smallest f is OOOOOOOOODD0O000000000000 0 In comparison the smallest and largest possible 32bit integers in twos complement are only 2 and 2 1 How can we represent so many more values in the IEEE 754 format even though we use the same number of bits as regular integers f i j m vf February 26 2003 MIPS floatingpoint arithmetic 10 Finiteness sn There arent more IEEE numbers With 32 bits there are 21 or about 4 billion different bit patterns These can represent 4 billion integers or 4 billion reals But there are an infinite number of reals and the IEEE format can only represent some of the ones from about 28 to 2178 This causes enormous headaches in doing floatingpoint arithmetic Not all values between 228 to 228 can be represented Small roundoff errors can quickly accumulate with multiplications or exponentiations resulting in big errors Rounding errors can invalidate many basic arithmetic principles such as the associative law xy ZxXyZ The IEEE 754 standard guarantees that all machines will produce the same resultsbut those results may not be mathematically correct February 26 2003 MIPS floatingpoint arithmetic 11 Limits of the IEEE representation Even some integers cannot be represented in the IEEE format int x 33554431 Float y 33554431 printfC d x printfC f y Some simple decimal numbers cannot be represented exactly in binary to begin with 010 00001100110011 February 26 2003 MIPS floatingpoint arithmetic 12 010 During the Gulf War in 1991 a US Patriot missile failed to intercept an Iraqi Scud missile and 28 Americans were killed A later study determined that the problem was caused by the inaccuracy of the binary representation of 010 The Patriot incremented a counter once every 010 seconds It multiplied the counter value by 010 to compute the actual time However the 24bit binary representation of 010 actually corresponds to 0099999904632568359375 which is off by 0000000095367431640625 This doesnt seem like much but after 100 hours the time ends up being off by 034 secondsenough time for a Scud to travel 500 meters Professor Skeel wrote a short article about this Roundoff Error and the Patriot Missile SIAM News 25411 July 1992 eS February 26 2003 MIPS floatingpoint arithmetic 13 Floatingpoint addition example To get a feel for floatingpoint operations well do an addition example To keep it simple well use base 10 scientific notation Assume the mantissa has four digits and the exponent has one digit The text shows an example for the addition 9999 0161 100151 As normalized numbers the operands would be written as 9999 x 10 1610 x 10 February 26 2003 MIPS floatingpoint arithmetic 14 February 26 2003 MIPS floatingpoint arithmetic 15 Steps 12 the actual addition 1 Equalize the exponents The operand with the smaller exponent should be rewritten by increasing its exponent and shifting the point leftwards 1610 101 00161 101 With four significant digits this gets rounded to 0016 101 This can result in a loss of least significant digitsthe rightmost 1 in this case But rewriting the number with the larger exponent could result in loss of the most significant digits which is much worse 2 Add the mantissas 101 10015 0016 101 9999 101 February 26 2003 MIPS floatingpoint arithmetic 16 Steps 35 representing the result 3 Normalize the result if necessary 10015 101 10015 102 This step may cause the point to shift either left or right and the exponent to either increase or decrease 4 Round the number if needed 10015 102 gets rounded to 1002 102 5 Repeat Step 3 if the result is no longer normalized We dont need this in our example but its possible for rounding to add digitsfor example rounding 99995 yields 10000 Our result is 1002 102 or 1002 The correct answer is 100151 so we have the right answer to four significant digits but theres a small error already Extreme errors As we saw rounding errors in addition can occur if one argument is much smaller than the other since we need to match the exponents An extreme example with 32bit IEEE values is the following 15 x 1038 10 x 10 15 x 108 The number 10 x 10 is much smaller than 15 x 1038 and it basically gets rounded out of existence This has some nasty implications The order in which you do additions can affect the result so x y zis not always the same as x y z Float x 15e38 Float y 15438 printfC f Cx y 10 printfC f x Cy 10 February 26 2003 MIPS floatingpoint arithmetic 17 Multiplication To multiply two floatingpoint values first multiply their magnitudes and add their exponents 9999 x 10 x 1610 x 10 16098 x 10 You can then round and normalize the result yielding 1610 x 10 The sign of the product is the exclusiveor of the signs of the operands If two numbers have the same sign their product is positive If two numbers have different signs the product is negative 000 011 1801 1010 This is one of the main advantages of using signed magnitude February 26 2003 MIPS floatingpoint arithmetic 18 The history of floatingpoint computation Inthe past each machine had its own implementation of floatingpoint arithmetic hardware andor software It was impossible to write portable programs that would produce the same results on different systems Many strange tricks were needed to get correct answers out of some machines such as Crays or the IBM System 370 It wasnt until 1985 that the IEEE 754 standard was adopted The standard is very complex and difficult to implement efficiently But having a standard at least ensures that all compliant machines will produce the same outputs for the same program IN February 26 2003 MIPS floatingpoint arithmetic 19 Floatingpoint hardware Intel introduced the 8087 coprocessor around 1981 The main CPU would call the 8087 for floatingpoint operations The 8087 had eight separate 80bit floatingpoint registers that could be accessed in a stacklike fashion Some of the IEEE standard is based on the 8087 Intels 80486 introduced in 1989 included floatingpoint support in the main processor itself The MIPS floatingpoint architecture and instruction set still reflect the old coprocessor days with separate floatingpoint registers and special instructions for accessing those registers WWwrmundopcnet February 26 2003 MIPS floatingpoint arithmetic 20 MIPS floatingpoint architecture MIPS includes a separate set of 32 floatingpoint registers Sf0Sf31 Each register is 32 bits long and can hold a singleprecision value Two registers can be combined to store a doubleprecision number You can have up to 16 doubleprecision values in registers Sf0Sf1 Sf2Sf3 f30f31 f0 is not hardwired to the value 00 There are also separate instructions for floatingpoint arithmetic The operands must be floatingpoint registers and not immediate values adds f1 f2 f3 Singleprecision f1 f2 3 addd f2 f4 f6 Doubleprecision f2 f4 f6 There are other basic operations as you would expect subs and subd for subtraction muls and muld for multiplication divs and divd for division February 26 2003 MIPS floatingpoint arithmetic 21 Floatingpoint register transfers movs and movd copy data between floatingpoint registers Use mtc1 and mfc1 to transfer data between the integer registers 031 and the floatingpoint registers Sf0f31 These are raw data transfers that do not convert between integer and floatingpoint representations Be careful with the order of the operands in these instructions mtcl t0 f0 f0 t0 mfcl t0 f0 tO f0 There are also special loads and stores for transferring data between the floatingpoint registers and memory The base address is still given in an integer register Iwcl 2 OCa0 f2 Ma0 swol f4 4Csp Msp4 f4 The c1 in the instruction names stands for coprocessor 1 February 26 2003 MIPS floatingpoint arithmetic 22 Floatingpoint comparisons We also need special instructions for comparing floatingpoint values since slt and sltu only apply to signed and unsigned integers cles 2 f4 ceqs f2 f4 clts 2 4 The comparison result is stored in a special coprocessor register You can then branch based on whether this register contains 1 or 0 bc1lt Label branch if true bc1f Label branch if false Here is how you can branch to the label Exit if f2 f4 ceqs f2 f4 bc1t Exit February 26 2003 MIPS floatingpoint arithmetic 23 Floatingpoint functions There are conventions for passing data to and from functions Floatingpoint arguments are placed in f12f15 Floatingpoint return values go into f0Sf1 We also split the registersaving chores just like earlier f0f19 are callersaved f20f31 are calleesaved These are the same basic ideas as before because we still have the same problems to solvenow its just with different registers February 26 2003 MIPS floatingpoint arithmetic 24 Floatingpoint constants ggg MIPS does not support immediate floatingpoint arithmetic instructions so you must load constant values into a floatingpoint register first One solution is to store floatingpoint constants in the data segment and to load them with a ls or d pseudoinstruction data alpha float 055555 50 90 text 1s f6 alpha f6 055555 Newer versions of SPIM also support the lis and lid pseudoinstructions which make life much easier lis 6 055555 f6 055555 February 26 2003 MIPS floatingpoint arithmetic 25 Type conversions You can also cast integers to floatingpoint values using the MIPS type conversion instructions Type to Floatingpoint convert to cvtsw f4 f2 Type to Floatingpoint convert from source register Possible types for conversions are integers w singleprecision s and doubleprecision d floatingpoint li t0 32 tO 32 mtcl tO f2 f2 32 cvtsw f4 f2 fF4 320 February 26 2003 MIPS floatingpoint arithmetic 26 A complete example Here is a slightly different version of the textbook example of converting singleprecision temperatures from Fahrenheit to Celsius celsius fahrenheit 320 x 50 7 90 celsius li tO 32 mtcl tO f4 cvtsw f4 f4 f4 320 11s f6 055555 f6 50 90 subs f0 f12 f4 f0 f12 320 muls f0 f0 f6 f0 f0 5090 jr ra This example demonstrates a couple of things The argument is passed in f12 and the return value is placed in f0 We use two different ways of loading floatingpoint constants We used only callersaved floatingpoint registers February 26 2003 MIPS floatingpoint arithmetic 27 Summary sn The IEEE 754 standard defines number representations and operations for floatingpoint arithmetic Having a finite number of bits means we cant represent all possible real numbers and errors will occur from approximations MIPS processors implement the IEEE 754 standard There is a separate set of floatingpoint registers Sf0Sf31 New instructions handle basic floatingpoint operations comparisons and branches There is also support for transferring data between the floatingpoint registers main memory and the integer registers We still have to deal with issues of argument and result passing and register saving and restoring in function calls February 26 2003 MIPS floatingpoint arithmetic 28