Homework 2

CMSC 411 / Olano, Fall 2013

Updates

21 September 2012

  1. 2's Complement

    What is the 8-bit 2's complement encoding (in binary and hexidecimal) for the following numbers? 0, 1, 2, -1, -2, 127, -127, -128

  2. Floating Point

    Write a C or C++ program that takes two single-precision floating point numbers on the command line and prints their product. However, the only place you can use an actual floating point variable or computation is to print the result. You will find a file named 'product.c' in your hw2 directory that does this using floating point operations. You must replace the marked section of this file with something that does the same thing using only integer operations. This includes generating the floating point encoding for the operands from the string input, performing the multiply, and generating the encoded result.

    int result;
    /* ... your code to compute the result ... */
    printf("  x = %g (encoding 0x%08x)\n", *(float*)&x, x);
    printf("  y = %g (encoding 0x%08x)\n", *(float*)&y, y);
    printf("  result = %g (encoding 0x%08x)\n", *(float*)&result, result);

    You should take scientific notation input as strings on argv[1] and argv[2] and encode them using the 32-bit IEEE floating point format. You do not need to support the full floating point text representation, but should handle:

    1. The exact strings 'nan' = not a number or 'inf' = positive infinity, or '-inf' = negative infinity
    2. A signed base-10 integer followed by the letter 'e' then a signed base-10 integer exponent. Some examples:
      0e0 = 0
      1e0 = 1
      10e0 = 1e1 = 10
      5e-1 = 0.5
      -5e-1 = -0.5
    3. Note that you do NOT need to support decimal notation (no 0.5, use 5e-1 instead)
    4. Note also that you can assume that the exponent is ALWAYS given. If you don't need the exponent (for integers), use e0.

    You SHOULD handle NaN, infinity, and denormalized floats.

    For reference, wikpedia page for IEEE single precion float has a pretty good summary of the format.

    Some examples:

    product 1e0 -1e0
    x = 1 (encoding 0x3f800000)
    y = -1 (encoding 0xbf800000)
    result = -1 (encoding 0xbf800000) product 5e-1 1e1 x = 0.5 (encoding 0x3f000000)
    y = 10 (encoding 0x41200000)
    result = 5 (encoding 0x40a00000) product inf 1e0 x = inf (encoding 0x7f800000)
    y = 1 (encoding 0x3f800000)
    result = inf (encoding 0x7f800000) product inf 0e0 x = inf (encoding 0x7f800000)
    y = 0 (encoding 0x00000000)
    result = nan (encoding 0xffc00000) product nan 2e10
    x = nan (encoding 0x7fc00000)
    y = 2e+10 (encoding 0x509502f9)
    result = nan (encoding 0x7fc00000)

Submitting

Submit your answer to part 1 in a ascii text file named "hw1.txt". Submit your solution to the part 2 as a single C or C++ file named 'product.c' or 'product.cxx'.

All electronic submissions in this class will be done using the CVS version control system. You should look at the class CVS instructions before you start work. To get full credit for your submission, you should (1) check out a copy of the empty hw1 directory before you start, (2) do your work in that checked out copy, (3) submit several intermediate checkins with short but useful messages, and (4) check in your final submission before class starts on the day of the deadline