CS520
Spring 2014
Laboratory 3
Monday February 10
The lab must be submitted prior to 8am on Tuesday February 11.
Copy the file FloatAdd.c from ~cs520/public/prog2
and edit it to accomplish the following:
- (10 points)
Write code to break out the three components of each input
to FloatAdd: sign, exponent and significand.
Remember to insert the hidden one bit at the left end of the significand.
(This is the one bit immediately to the left of the radix point that
must always be present because the value is normalized, but is not
actually stored in the representation.)
Insert debugging code to display these three components in hex to stderr.
- (10 points)
Write code to categorize each input to FloatAdd as either
zero, infinity, NaN, denormalized value, or a "regular" normalized value.
Insert debugging code to display this categorization to stderr.
- (10 points)
Handle zero inputs to FloatAdd.
Basically, if one of the inputs is zero, return the other input.
However, remember that IEEE floating-point zeros are signed.
Figuring out the sign of the result when at least one of the inputs
is negative zero will require some investigation.
Remember: your goal is to match exactly what the Intel hardware does.
- (10 points)
Handle infinity inputs to FloatAdd.
Bascially, if one of the inputs is infinity, return infinity.
But handling signed infinities will require some investigation.
- (10 points)
Handle NaN inputs to FloatAdd.
Basically, if one of the inputs is NaN, return NaN.
But handling signed NaNs will require some investigation.
One complication is that there are actually two kinds of NaNs:
quiet NaNs and signalling NaNs.
Wikipedia has a good entry
on NaNs that you should consult.
You can investigate how the Intel hardware works by using a program
like this:
#include <stdio.h>
int main(void)
{
int x = 0x80000000;
int y = 0x00000000;
float result;
result = (*(float *) &x) + (*(float *) &y);
printf("%08x + %08x --> %08x\n", x, y, *(int *)&result);
return 0;
}
In this example, I am investigating what happens when you add
a positive zero and a negative zero.
You should use #if C preprocessor commands to
allow your debugging prints to be easily turned on or off.
For example,
#define DO_DEBUGGING_PRINTS 1
#if DO_DEBUGGING_PRINTS
fprintf(stderr, "this is debugging output\n");
#endif
By simply changing the 1 to a 0 in the definition of
DO_DEBUGGING_PRINTS,
the debugging output can be disabled.
Leave the debugging prints enabled for your submission of this lab.
You will have to construct a main program to test your code.
But do not submit this main program!
Put it in a separate file.
Note: you are only submitting the one file, FloatAdd.c, so
do not put any of your code in any other file.
In particular, FloatAdd.c should not include any files that
you created.
It should only include standard system header files.
To turn in this laboratory, type:
~cs520/bin/submit lab3 FloatAdd.c
Submissions can be checked by typing:
~cs520/bin/scheck lab3
Last modified on February 2, 2014.
Comments and questions should be directed to
hatcher@unh.edu