CS520
Spring 2017
Laboratory 3
Friday February 10
The lab must be submitted prior to noon on Sunday February 12.


Copy the file FloatAdd.c from ~cs520/public/prog2 and edit it to accomplish the following:

  1. (20 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. However, of course, do not insert the one bit if the value is zero, denormalized, infinity or NaN.) Insert debugging code to display these three components in hex to stderr.

  2. (20 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.

  3. (20 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.

  4. (20 points) Handle infinity inputs to FloatAdd. Basically, if one of the inputs is infinity, return infinity. But handling signed infinities will require some investigation.

  5. (20 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.

Use #if C preprocessor commands to control whether the debugging prints are enabled. (See the Program 2 specification for more information about how to do this.) Leave the debugging prints enabled for your submission of this lab.

To test your function, you will need to supply a main function. Put this function in another file! For example, you might construct a main function like this one, which will both test your function and display the correct answer for comparison:

#include <stdio.h>

int FloatAdd(int, int);

int main(void)
{
  int x = 0x80000000;
  int y = 0x00000000;

  int iresult = FloatAdd(x, y);
  float fresult = (*(float *) &x) + (*(float *) &y);;

  printf("%08x + %08x --> %08x (should be %08x)\n", x, y, iresult,
    *(int *)&fresult);

  return 0;
}

If the main function is in the file main.c and your implementation of FloatAdd is in the file FloatAdd.c, then to compile and run the program:

gcc -Wall -std=c99 FloatAdd.c main.c -o lab3
./lab3

Turn in this laboratory, by typing:

% ~cs520/bin/submit lab3 FloatAdd.c

Submissions can be checked by typing:

% ~cs520/bin/scheck lab3

Remember that there are no late submissions of labs. Submit what you have prior to noon on Sunday February 12.


Last modified on February 7, 2017.

Comments and questions should be directed to pjh@cs.unh.edu