Homework 5

Computer Organization
Spring, 2013
Due:  April 28th, 8 pm via handin - turn in as assignment 5

For this problem you will implement the IEEE Floating point standard either as a Java class, C++ class, or as a struct in a C program.  Some of the code is provided in the file FloatingPoint.zip; you are required to write methods to perform addition and multiplication on 32-bit IEEE-format floating point numbers.

For C programmers, the jar file contains a file xfloat.c which contains conversion and output functions for your use in completing the assignment.  The file contains dummy implementations of xadd and xmult.  You need to write your own versions of these which perform addition and multiplication correctly.

For Java programmers, the file Xfloat.java contains an abstract base class Xfloat with three fields:

  byte sign;
  byte exp;
  int man;
These fields correspond to the sign, exponent and mantissa fields of a floating point number.  The abstract methods are xadd and xmult. The class also contains methods for converting between actual floating point and Xfloat.

MyXfloat.java contains the definition of a subclass of Xfloat.  It contains a main method that reads 2 floating point numbers from the command line (called x and y) and prints out their Xfloat fields.  For example,

> java MyXfloat 2.5 7.25
x:   40200000 (0 10000000 01000000000000000000000) s: 0 e: 80 m: 200000 2.5
y:   40e80000 (0 10000001 11010000000000000000000) s: 0 e: 81 m: 680000 7.25
x*y: 0 (0 00000000 00000000000000000000000) s: 0 e: 0 m: 0 0.0
x+y: 0 (0 00000000 00000000000000000000000) s: 0 e: 0 m: 0 0.0
The first 8 hex-digit number corresponds to the bits of the actual IEEE floating point number. The binary representation follows, separated into sign, exponent and mantissa fields. Next are the corresponding Xfloat fields in hex. Note that the mantissa field requires only 6 hex digits since only 23 of the 32 bits are actually used.

MyXfloat.java includes only dummy implementations for xadd and xmult, which return objects of type MyXfloat with the value 0.  Your assignment is to write proper implementations for these methods.  For the C version, you are to write 2 procedures: xmult(XFLOAT*, XFLOAT*, XFLOAT*) and xadd(XFLOAT*, XFLOAT*, XFLOAT*), which respectively add and multiply their first 2 arguments and store the result in the third.  Note that the programs are set up to print the product and sum, currently using dummy versions of xmult and xadd.

I've also provided a correct version of the program (XfloatAns.class) in the jar file.  You can run it with the command "java XfloatAns ...". (You must have already compiled Xfloat.java and have Xfloat.class visible on your classpath in order for XfloatAns to run).

Notes:

In the Java Virtual Machine, floating point values are always converted to double precision before any computation takes place.  Mantissa multiplication should proceed as follows:

  1. Use Xfloat.BMASK to return the implicit 1 in the mantissa
  2. Convert the mantissas to 64 bit long integers by type casting;
  3. Multiply the 64 bit long integers;
  4. Shift the result appropriately (making necessary adjustments to the exponent).
  5. Cast the mantissa back to 32 bits and remove the implicit 1.
The various static MASK constants defined in Xfloat should be helpful in this process.

Note that in Java, the unsigned right shift is represented by the operator >>>.

You can ignore denormalized values, but be sure to properly handle the special case of 0.