Project #1: Floating Point Representation Solution




This is to be an individual effort. No partners.

No late work allowed after 48 hours; each day late uses up one of your tokens.

In class, we talked about the IEEE standard for floating point representation and did examples using different sizes for exponent and fraction fields so that you could learn how to do the conversions. For this assignment, you are going to write code to do this, allowing you to store these smaller floating point numbers in a 32-bit integer!

INPUT: For this assignment, you will read in a ‘program’ and call your functions to implement these programs. An example of one possible program might be:

x = 18.113

print x

y = 4.5

a = x + y

print a

z = x * y

print z

OUTPUT: The output will be the current values of the given variables at the print statements. For the above program, output would be:

x = 18.0625000000

a = 22.5625000000

z = 81.2500000000

Some of this task is already done for you. I will provide a program that reads in the given programs, saves the variable values and calls the functions (described next) that you will be implementing.

You are going to implement a 15 bit floating point representation, where 6 bits are for the exponent and 8 are for the fraction. Using bit level operators, you will write functions (shown below) to help implement the program statements:

Assignment statement (variable = value) – your function computeFP() will be

called that will take the input floating point value and convert it to our 15 bit

representation, returning this as an integer. This integer will be saved as the value of the given variable.

int computeFP(float val) { }

  • input: float value to be represented

  • output: integer version in our representation

Given the number of bits, the rounding you will have to do for this representation is pretty substantial. For this assignment, we are always going to take the easy way and truncate the fraction (i.e. round down). For example, the closest representable value for 18.113 (rounding down) is 18.0625, as can be seen in the program output.

Print statement (print variable) – for this statement, the value of the variable in our representation will be converted back to a C floating point using your

function and this will be printed.

float getFP(int val) { }

    • Using the defined representation, compute the

    • and return the floating point value

Multiply statement – for this statement, you are going to take two values in our representation and use the same technique as described in class to multiply these values and return the result in our representation.

int multVals(int source1, int source2) {}

Add statement – for this statement, you are going to take two values in our representation and use the technique as described in class to add these values and return the result in our representation. DO NOT convert them back to float,

add them, then convert to the new representation.

int addVals(int source1, int source2) {}

To make your life a little easier, we are going to make the following assumptions:

No negative numbers. The sign bit can be ignored.

No denormalized (or special) numbers. If the given number is too small to be represented as a normalized number, you can return 0. If the number is too large, return -1

Getting Started

First, get the starting code (lab1_handout.tar) from the same place you got this handout. Once you un-tar the handout on zeus (using “tar xvf lab1_handout.tar”), you will have the following files:

fp_functs.c – This is the file you will be modifying (and submitting). There are stubs in this file for the functions that will be called by the rest of the framework. Feel free to define more functions if you like but put all of your code in this file!

Makefile – to build the assignment (and clean up).

fp_program.c – This is the main program for the assignment. You should not change it. It implements a recursive descent parser to read in the program files, determine what each line is supposed to do, and call your functions to convert, add and multiply.

print_all_values – This is a program I wrote to make debugging easier for me. It prints out all legal values in our representation! This will help you determine what values you should be seeing. For example, in the above program I assign 18.113 to x. This number is not in the output for this program. The closest smaller number is 18.0 – when I see this output, I know that value is being rounded and stored correctly.

program1, program2 – some sample input files. Note that the comments give information about the expected outputs.

fp.h and fp.l – You can ignore these files – They are the Lex specification which tokenizes input and sends it to the recursive descent parser in the main program.

Implementation Notes

Program Files – The accepted syntax is very simplistic and it should be easy to write your own programs to test your code (which I strongly encourage). Variable names are single lower case letters. Comments start with the character ‘#” and go to the end of the line. There are 4 different statement types:





print x – where x is a variable.

  1. = value – for some floating point value. This statement has the obvious meaning.

  1. = y + z – for any legal variable names

  1. = y * z – for any legal variable names

Submitting & Grading

Submit this assignment electronically on blackboard. In addition, bring a hard copy of this file to the first class after the due date (Oct 1). Note that the file that get submitted is fp_functs.c

Your grade will be determined as follows:

30 points – code & comments. Be sure to document your design clearly in your

code comments. This score will be based on reading your source code (you will lose these points if you do not turn in a hardcopy).

70 points – correctness. We will be building your code using the fp_functs.c code you submit along with our code (which will be the framework with some extensions to make our grading easier). If you program does not compile, we cannot grade it. If your program compiles but does not run, we cannot grade it. I will give partial credit for incomplete submissions. You will not get credit for a particular part of the assignment (multiplication for example), if you do not use the required techniques, even if your program performs correctly on the test cases for this part.

error: Content is protected !!