11. Floating Point
Part of
22C:60, Computer Organization Notes
|
The Hawk architecture includes two opcodes reserved for communication with coprocessors. The term coprocessor refers to a special purpose processor that operates in conjunction with the central processor. Some coprocessors are physically separate from the central processor, for example, on a separate chip, but the logical separation is essential. Coprocessors are frequently used to perform floating point operations, but they have also been used for graphics, cryptography, and other specialized computations.
The Hawk coprocessor instructions, COSET and COGET allow
data to be transferred between the Hawk general purpose registers and the
specialized registers inside one or more coprocessors. In this chapter,
we will only discuss coprocessor number one, the floating point coprocessor.
|
|
In these instructions, the dst field always refers to a CPU register, while the x field refers to one of the registeres in the currently active coprocessor. Coprocessor register zero, the coprocessor status register, COSTAT is used to select the active coprocessor. This register has several fields, the details of which can be found in the Hawk manual. What matters, for our purposes, is that the following instruction sequence enables the floating point coprocessor to handle short (32-bit) floating point operands:
LIL R1, FPENAB + FPSEL COSET R1, COSTAT |
Once the floating point coprocessor is enabled, addressing coprocessor registers 1 to 15 refers specifically to registers inside the floating point unit. When operating in short format, there are only two useful registers in the floating-point coprocessor, floating-point accumulators zero and one, FPA0 and FPA1, which corespond to register numbers 2 and 3. Register 1 will be ignored, for now. It is used for access to the least significant halfword of long (64-bit) floating point operands.
Coprocessor registers 4 to 15 are not, in reality, registers. Rather, operations that would appear to set these registers actually initiate operations on the floating point accumulators. The available operations include the obvious ones, floating point add, subtract, multiply and divide, as well as square root and conversion from integer to floating. Setting even registers in the range from 4 to 15 causes operations on FPA0 and setting odd registers in this range operates on FPA1. For example, setting coprocessor register number 5 converts an integer operand from a general purpose register into a floating point value in FPA1. The complete set of short (32-bit) floating point operations on floating point accumulator zero is illustrated below; the same operations are available on coprocessor register 1.
COSET R1, FPA0 ; 2 FPA0 = R1 COSET R1, FPINT+FPA0 ; 4 FPA0 = (float) R1 COSET R1, FPSQRT+FPA0 ; 6 FPA0 = sqrt( R1 ) COSET R1, FPADD+FPA0 ; 8 FPA0 = FPA0 + R1 COSET R1, FPSUB+FPA0 ; 10 FPA0 = FPA0 - R1 COSET R1, FPMUL+FPA0 ; 12 FPA0 = FPA0 * R1 COSET R1, FPDIV+FPA0 ; 14 FPA0 = FPA0 / R1 |
Unlike integer operations, floating point operations do not directly set the condition codes. When the coprocessor get instruction COGET is used to get the contents of either floating point accumulator, it sets the N and Z condition codes to indicate whether the floating point value is negative or zero. In addition, the C condition code is used to report floating point values that are infinite or non numeric. This is possible because the floating point representation includes representations for infinite values.
Exercises
a) Give appropriate defines for the symbols FPA0, FPA1, FPSQRT, FPADD, FPSUB, FPMUL and FPDIV that are used as operands on the COSET instruction.
b) Given 32-bit floating point values x in R4 and y in R5, give Hawk code to enable the floating point coprocessor, compute sqrt(x2 + y2), place the result in R3 and then disable all coprocessors.
It is not sufficient to say that we have a floating point coprocessor that supports 32-bit floating point values. We must also define the data format used by this processor. Most modern computers support the same floating point format, a format defined by the Institute for Electrical and Electronic Engineers, the IEEE. The Hawk is no exception. This format follows a general outline that is very similar to most floating point formats supported by floating point hardware since the early 1960's, but it does have some eccentric and occasionally difficult to explain features.
The format of the binary floating point numbers used in computers is closely related to the format of decimal numbers expressed in scientific notation. Consider the number 6.02×1023, known as Avagadro's number. This number is composed of a mantissa, 6.02, and an exponent, 23. The number base used in the mantissa is the same as the value to which the exponent is applied. Scientific notation is more complex than this, however, because we have normalization rules. Consider the following expressions of the same number:
60221419.9 | × | 1016 | \ | ||
60221.4199 | × | 1019 | not normalized | ||
60.2214199 | × | 1022 | / | ||
6.02214199 | × | 1023 | normalized | ||
0.602214199 | × | 1024 | not normalized |
Of these, only 6.02... × 1023 is considered to be properly in scientific notation. In scientific notation, the mantissa is always represented as a fixed-point decimal number between 1.000 and 9.999... The only exception is zero. When we find a number that has a mantissa that does not satisfy this rule, we normalize it by moving the point (and adjusting the exponent accordingly) until it satisfies this rule.
The IEEE standard includes both 32 and 64-bit floating point numbers. In this discussion, we will ignore the latter and focus the one-word 32-bit format. As with numbers in scientific notation, these have an exponent and a mantissa field, but these are binary representations, so the mantissa field is in base two and the exponent field is a power of two, not a power of ten.
31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 09 | 08 | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00 |
S | exponent | mantissa |
In the IEEE floating point formats, like most others hardware floating point formats, the most significant bit holds the sign of the mantissa, with zero meaning positive. The mantissa is stored in signed magnitude form. The magnitude of the mantissa of a 32-bit floating-point number is given to 24 bits of precision, while the exponent is stored in the 8 remaining bits. Notice that this adds up to 33 bits of sign, exponent and mantissa. This is because of some exceptionally tricky details of the IEEE floating point representation. IEEE double-precision numbers differ from the above in that each number is 64 bits. This allows 11-bits for the exponent instead of an 8 bits, and 53 bits for the mantissa, including one extra bit obtained from the same trickery that got an extra bit for the 32-bit format.
The way the IEEE format gets an extra bit for the mantissa stems from a consequence of the normalization rule used for the mantissa. The mantissa in an IEEE format number is represented as a binary fixed point number with one place to the left of the point. With this representation, the normalization rule is very similar to that used for scientific notation. The smallest normalized mantissa value is 1.0, while the largest normalized value is 1.1111...2. This means that, for normalized mantissas, the most significant bit of the mantissa is always one. In general, if a bit is always the same, there is no point in storing it, we can take the constant value for granted. We call this bit that we do not store the hidden bit. Consider the following IEEE floating point value represented as 1234567816:
|
|
| The IEEE format supports non-normalized representations only in the case of the smallest possible exponents. The representation of zero falls into this category, a value with the smallest possible exponent and a mantissa of zero.
The Biased ExponentThe second odd feature of the IEEE format is that the exponent is given as a biased signed integer with the eccentric bias of 127. The normal range of exponents runs from 000000012, meaning -126, to 111111102, meaning +127. The exponent represented as 000000002 is reserved for unnormalized (extraordinarily small) values and for zero. In this case, the exponent is still interpreted as having the representation -126. The hidden bit is zero for unnormalized values. The exponent 111111112 is reserved for infinity (with a mantissa of zero) and for values that the IEEE calls NaNs, where NaN stands for not a number. The mantissa field of a NaN may be put to a variety of uses by software, but this is rarely done. Because of the odd bias of 127 for exponents, the exponent one is represented as 100000002, zero is 011111112, and negative one is 011111102. There is a competing but equivalent presentation of the IEEE format that presents the bias as 128 and places the point in the mantissa differently relative to the hidden bit. The different presentations of the IEEE system make no difference in the number representations, but they can be confusing when comparing presentations of the number system from different sources. The following table shows IEEE floating-point numbers, given in binary, along with their interpretations.
Software Floating PointSome versions of the Hawk emulator do not have a floating point coprocessor. On such machines, we must implement floating point operations entirely with software. This forces us to examine the algorithms that underly floating point arithmetic, and it faces us with an extended example of a software module that implements a complex class. In the following presentation, we will not bother to maintain any compatibility with the Hawk floating point coprocessor or with IEEE floating point format. Instead, we will focus on clear code and straightforward data representation. Transformation between this package and IEEE format will be left for last. The interface specification for a class should list all of the operations applicable to objects of that class, the methods, and for each method, it should specify the parameters expected, constraints on those parameters, and the nature of the result. The implementation of the class must then give the details of the object representation and the specific algorithms used to implement each method. It is good practice to add documentation to the interface specification, so that it serves as a manual for users of the class as well as a formal interface. For our floating-point class, the set of operations is fairly obvious. We want operators that add, subtract, multiply and divide floating-point numbers, and we also want operators that return the integer part of a number, and that convert integers to floating-point form. We probably want other operations, but we will forgo those for now. In most object-oriented programming languages, a strong effort is made to avoid copying objects from place to place. Instead, objects sit in memory and object handles are used to refer to them. The handle for an object is actually just a pointer to that object, that is, a word holding the address of the object. Therefore, our floating point operators will take, as parameters, the addresses of their operands, not the values. Finally, the interface specificaiton for a class must indicate how to allocate storage for an element of that class. The only thing a user of the object needs to know is the size of the object, not the internal details of its representation. The following interface specification for our Hawk floating point package assumes that each floating point number is stored in two words of memory, enough for an exponent and a mantissa of one word each, although the user need not know how the words are used.
A floating point representationThe simplest way to represent a floating point number for a software implementation of floating point operations is as a pair of words, one holding the exponent and another holding the mantissa, but this is not enough detail. Which word is which? We need to specify the interpretation of the bits of each of these words. What is the range of exponent values? How do we represent the sign of the exponent? How is the mantissa normalized? How do we represent non-normalized values such as zero? On a computer that supports two's complement integers, it makes sense to represent the exponent and mantissa as two's complement values. We can represent zero using a mantissa of zero; technically, when the mantissa is zero, the exponent does not matter, but we will always set the exponent to the smallest (most negative) possible value. The more difficult question is, where is the point in our two's complement mantissa? We could put the point anywhere and make it work, but the two obvious choices are to use an integer mantissa or to put the point immediately to the right of the sign bit. Here, we opt for the latter, and we will normalize the mantissa so that the bit immediately to the right of the point is always a one. This is equivalent to changing the normalization rules for decimal scientific notation so that 0.602×1024 is considered to be normalized. The following examples illustrate this number format.
Normalizing a floating point numberMany operations on floating point numbers produce results that are unnormalized, and these must be normalized before performing additional operations on them. If this is not done, there will be a loss of precision in the results. Classical scientific notation is always presented in normalized form for the same reason. To normalize a floating point number, we must distinguish some special cases: First, is the number zero? Zero cannot be normalized! Second, is the number negative? Because we have opted to represent our mantissa in two's complement form, negative numbers are slightly more difficult to normalize; this is why many hardware floating-point systems use signed magnitude for their floating point numbers. The normalize subroutine is not part of the public interface to our floating point package, but rather, it a private component, used as the final step of just about every floating point operation. Therefore, we can write it with the assumption that operands are passed in registers instead of using pointers to memory locations. We will code this here using registers 3 and 4 to hold the exponent and mantissa of the number to be normalized, and we will use this convention both on entrance and exit.
There are two tricks in this code worth mention. First, this code uses the BITTST instruction to test bit 30 of the mantissa. This instruction moves the indicated bit to the C condition code; in fact, the assembler converts this instruction to either a left or a right shift to move the indicated bit into the carry bit while discarding the shifted result using R0. In C, C++ or Java, in contrast, inspection of one bit of a word is most easily expressed by anding that word with a constant with just that bit set. The second trick involves normalizing negative numbers. In the example values presented above, note that the representation of -0.5 has bit 30 set to 1, while -0.75 has it set to zero. By subtracting one from the least significant bit of each negative value, we can convert to one's complement, allowing us to take advantage of the fact that bit 30 of the one's complement representation of normalized mantissas is always zero.
Integer to Floating ConversionConversion from integer to floating point is remarkably simple. All that needs to be done is to adjust the exponent field to 31 and set the mantissa field to the desired integer, and then normalize the result. This is because the fixed point fractions we are using to represent the mantissa can be viewed as integer counts in units of 2-31. As a result, our code simply moves the data into place for a call to normalize and then stores the results in the indicated memory location.
Floating to Integer ConversionConversion of floating-point numbers to integer is a bit more complex, but only because we have no pre-written denormalize routine that will set the exponent field to 31. Instead, we need to write this ourselves. Where the normalize routine shifted the mantissa left and decremented the exponent until the number was normalized, the floating to integer conversion routine will have to shift the mantissa right and increment the exponent until the exponent has the value 31. This leaves open the question of what happens if the initial value of the exponent was greater than 31. The answer is, in that case, the integer part of the number is too large to represent in 32 bits. In this case, we should raise an exception, or, lacking a model of how to write exception handlers, we could set the overflow condition code. Here, this is left as an exercise for the reader.
Floating Point AdditionWe are now ready to explore the implementation of some of the floating point operations. These follow quite naturally from the standard rules for working with numbers in scientific notation. Consider the problem of adding 9.92×103 to 9.25×101. We begin by denormalizing the numbers so that they have the same exponents; this allows us to add the mantissas, after which we renormalize the result and round it to the appropriate number of decimal places:
The final rounding step is one many students forget, particularly in this era of scientific calculators. For numbers given in scientific notation, we have the convention that the number of digits given is an indication of the precision of the measurements from which the numbers were taken. As a result, if two numbers are given in scientific notation and then added or subtracted, the result should not be expressed to greater precision than the least precise of the operands! When throwing away the less significant digits of the result, we always round in order to minimise the loss of information and introduction of systematic error that would result from truncation. An important question arises here: Which number do we denormalize prior to adding? The the answer is, we never want to lose the most significant digits of the sum, so we always increase the smaller of the two exponents while shifting the corresponding mantissa to the right. In addition, we are seriously concerned with preventing a carry out of the high digit of the result; this caused no problem with pencil and paper, but if we do this in software, we must be prepared to recover from overflow in the sum! This problem is solved in the following floating point add subroutine for the Hawk:
Most of this code follows simply from the logic of adding that we demonstrated with the addition of two numbers using scientific notation. There are some points, however, that are worthy of note. First, about 1/3 of the way down, this code exchanges the two numbers; this involves exchanging two pairs of registers. There are many ways to do this; the approach used here is the simplest to understand, setting the value in one of the registers aside, moving the other register, and then moving the set-aside value into its final resting place. This takes three move instructions and a spare register. There are other ways to do this that are just as fast but do not require a spare register, but these are harder to understand. The most famous and cryptic of these uses the exclusive or operator: a=a⊕b;b=a⊕b;a=a⊕b. Because this routine uses registers 1 to 7 and makes calls to another routine, it needs to use its activation record; here, we have constructed an activation record with two fields, one for saving register 1 to allow the call to NORMALIZE, and one for saving register 8, freeing it for local use. While FLTADD uses its activation record, NORMALIZE does not. Therefore, this code does not need to adjust the stack pointer, register 2, before or after the call to normalize. Finally, there is the issue of dealing with overflow during addition. After addition, when the sign is wrong, interpreted as a sign bit, it does have the correct value as the most significant bit of the magnitude, as if there were an invisible sign bit to the left of it. Therefore, after a signed right shift to make space for the new sign bit (incrementing the exponent to compensate for this) we can complement the sign by adding one to it, for example, using the ADJUST instruction.
Floating Point MultiplicationStarting with a working integer multiply routine, floating point multiplication is simpler than floating point addition. This simplicity is apparent in the algorithm for multiplying in scientific notation: Add the exponents, multiply the mantissas and normalize the result, as illustrated below:
Unlike addition, we need not denormalize anything before the operation. The one new issue we face is the matter of precision. Multiplying two 32-bit mantissas gives a 64-bit result. We will assume a signed multiply routine that delivers this result, with the following calling sequence:
If the multiplier and multiplicand have 31 places after the point in each, then the 64-bit product has 62 places after the point. Therefore, to normalize the result, we will always shift it one place. If the multiplier and multiplicand are normalized to have minimum absolute values of 0.5, the product will have a minimum absolute value of 0.25. Normalizing such a small product will require an additional shift, but never more than one. We must use 64-bit shifts for thiese normalize steps in order to avoid loss of precision, so we cannot use the normalize code we used with addition, subtraction and conversion from binary to floating point.
Most of the above code is involved with normalizing the result. This code is oversimplified! What if the product is zero? Our normalization rule is that a product of zero should have the most negative possible value. This code does not test for overflow or underflow, that is, no test for exponent out of bounds.
Other OperationsMultiply and divide routines do not finish the story. Our commitment to strong abstraction means that users of our floating point numbers may not examine their representations. The designers of floating point hardware do not face this constraint. They advertise the exact format they use and users are free to use this information. If we do not disclose such detail, we must provide tools for comparing numbers, for testing the sign of numbers, for testing for zero, and other operations that might otherwise be trivial. Another issue we face is the import and export of floating point numbers. We need tools to convert numbers to and from textual and IEEE standard format. The routine to convert from our eccentric format to IEEE format begins by dealing with the range of exponent values. Our 32-bit exponent field has an extraordinary range. Second, it converts the exponent and mantissa to the appropriate form, and finally, it packs the pieces must be packed together. The following somewhat oversimplified code does this:
Note in the above code that the advertised bias of the IEEE format is 127, yet we used a bias of 126! This is because we also subtracted one from the original exponent to account for the fact that our numbers were normalized in the range 0.5 to 1.0, while IEEE numbers are normalized in the range 1.0 to 2.0. This is also why we compared with 128 and -125 instead of 127 and -126 when checking for the maximum and minimum legal exponents in the IEEE format. We have omitted one significant detail in the above! All underflows were simply forced to zero when some of them ought to have resulted in denormalized numbers.
Conversion from IEEE format to our eccentric software format is fairly easy because our exponent and mantissa fields are larger than those of the single-precision IEEE format. Thus, we can convert with no loss of precision. This code presented above ignores the possibility that the value might be a NaN or infinity. This code makes extensive use of shifting to clear fields within the number. Thus, instead of writing n&0xFFFFFF00, we write (n>>8)<<8. This trick is useful on many machines where loading a large constant is significantly slower than a shift instruction. By doing this, we avoid both loading a long constant into a register and using an extra register to hold it. We used a related trick to set the implicit one bit, using a subtract instruction to set the carry bit and then adding this bit into the number using an adjust instruction.
Conversion to DecimalA well designed floating point package will include a complete set of tools for conversion to and from decimal textual representations, but our purpose here is to use the conversion problem to illustrate the use of our floating point package, so we will write our conversion code as user-level code, making no use of any details of the floating point abstraction that are not described in the header file for the package. First, consider the problem of printing a floating point number using only the operations we have defined, ignoring the complexity of assembly language and focusing on the algorithm. We can begin by taking the integer part of the number and printing that, followed by a point, but the question is, how do we continue from there, printing the digits after the point?
To print the fractional part of a number, the above C code takes the integer part of the number and subtract it from the number, leaving just the fractional part. Multiplying the fractional part by ten brings one decimal digit of the fraction above the point. Print that digit, and then repeat this process for each following digit. This is not particularly efficient, since it keeps converting back and forth between floating and integer representations, but it works.
We face a few problems here, and it is best to tackle these incrementally.
First, in order to allow code to be written with no knowledge of the structure
of floating point numbers, we must pass pointers to numbers, not the numbers
themselves, because passing the numbers themselves will require that the
assembly language programmer know how manyu registers it takes to hold each
number. Second, we have used arithmetic operators above that involve
calls to routines in the floating point package. We will tackle these
problems as the high-level before trying to deal with them in assembly
language.
The above code shows some of the problems we forced on ourselves by insisting on having no knowledge of the representation of floating point numbers when we write our print routine. Where a C or Java programmer would write 10.0, relying on the compiler to translate this into floating point representation, and put it in memory, we have been forced to use the integer constant 10 and then call the float() routine to convert it to its internal representation. This is a common consequence of strict object oriented encapsulation, although loose encapsulation schemes, for example, those that export compile or assembly time macros to process constants into their internal representation can get around this. The next problem we face is that, at the time we write this code, we are denying ourselves knowledge of the size of the representation of floating point numbers. As a result, we cannot allocate space in our activation records taking advantage of a known size. Our solution to this problem rests on two elements. First, we will rely on the fact that the interface definition for the floating point package float.h provides us with the size of a floating point number in the constant FLOATSIZE; in fact, we have adopted the general convention that, for each object, record or structure, we always have a symbol defined to hold its size. Second, we can use the assembler itself to sum up the sizes of the fields of the activation record instead of adding them up by hand, as we have in our previous examples. To do this, we begin with an activation record size of zero, define each field in terms of the previous activation record size, and then add the field size to compute the new activation record size. We could, of course, have defined all of the easy to define fields first using the old method, but to be consistant, we have defined all of the fields this way in the following:
In the above, had we allowed ourselves to use knowledge about the size of a floating point number, we could have defined NUM=4, TMP=12 and TEN=20, but then, any change in the floating point package would have required us to rewrite this code. The macro LOCAL allows us to write local variable declarations compactly; without this macro, each of our local variables would have required two lines of code. For example, the declaration of the local variable NUM would begin with NUM=ARSIZE, and then it would add to the activation record size with ARSIZE=ARSIZE+FLOATSIZE. The local variables for saving registers 8 and 9 were allocated so that the integer variables in our code can use these registers over and over again instead of being loaded and stored in order to survive each call to a routine in the floating point package. Of course, if those routines need registers 8 and 9, they will be saved and restored anyway, but we leave that to them. The following code contains one significant optimization. With all of the subroutine calls, we could have incremented and decremented the stack pointer many times. Instead, we increment it just once at the start of the print routine and decrement it just once at the end; in between, we always subtract ARSIZE from every displacement into the activation record in order to correct for this.
|