Question: Bit-Level Floating-Point Coding Rules In the following problems, you will write code to implement floating-point functions, operating directly on bit-level representations of floating-point numbers. Your

Bit-Level Floating-Point Coding Rules

In the following problems, you will write code to implement floating-point functions, operating

directly on bit-level representations of floating-point numbers. Your code should exactly replicate

the conventions for IEEE floating-point operations, including using round-to-even mode when

rounding is required. To this end, we define data type

float_bits

to be equivalent to

unsigned

:

/* Access bit-level representation floating-point number */

typedef unsigned float_bits;

Rather than using data type float in your code, you will use

float_bits

. You may use both int

and unsigned data types, including unsigned and integer constants and operations.

You may

not use any unions, structs, or arrays. Most significantly, you may not use any

floating-point data types, operations, or constants.

Instead, your code should perform the bit

manipulations that implement the specified floating-point operations.

The following function illustrates the use of these coding rules. For argument f, it returns +/- 0 if f

is denormalized (preserving the sign of f), and returns f otherwise.

/* If f is denorm, return 0. Otherwise, return f */

float_bits float_denorm_zero(float_bits f) {

/* Decompose bit representation into parts */

unsigned sign = f >> 31;

unsigned exp = f >> 23 & 0xFF;

unsigned frac = f & 0x7FFFFF;

if (exp == 0) {

/* Denormalized. Set fraction to 0 */

frac = 0;

}

/* Reassemble bits */

return (sign<<31) | (exp << 23) | frac;

}

Homework problems 2.97

Following the bit level floating-point coding rules, implement the function with the following

prototype:

/* Compute (float) i */

float_bits float_i2f(int i);

For argument

i

, this function computes the bit level representation of

(float) i

. Your function

will be tested by evaluating it for all 2

32

values of argument

f

and comparing the result to what

would be obtained using your machine's floating-point operations.

Testing your program

Download the tarfile hw3handout.tar from Canvas. Use scp to copy it to W204 machines into

your 311 folder like hw2. Untar the file. You only need to edit one file

float_i2f.c

. To test your

program, use

make

to compile your code and create an executable called

test

. We included the

test.c file in your tar file this time, which means you could also try to do your homework on your

own machine that meet the following condition: Its int in C is 32bits integer. Its 32bit single point

precision floating number meets IEEE standard and uses round to even. And when you finished

your work, please make sure you verify your implementation can pass the test on W204

machines before submission.

The program

test

can be used in two different ways. You can run this program by passing it an

argument that is an integer representation (could be decimal, hexadecimal or octal like you write

constant in C), and it will only test if your function works on this given integer. You can also run

this program on its own, and it will test your function on all 2

32

integer values until you either

pass all the tests, or fail at least 10 tests. This test will run for quite a while because 2

32

is a

large number. And it will print out some status showing the test is on going for every 2

28

test we

do.

Here is some sample output on a correctly implemented float_i2f function.

[vagrant@localhost hw3]$ ./test

Testing numbers between 0x00000000 to 0x0FFFFFFF

Testing numbers between 0x10000000 to 0x1FFFFFFF

Testing numbers between 0x20000000 to 0x2FFFFFFF

Testing numbers between 0x30000000 to 0x3FFFFFFF

Testing numbers between 0x40000000 to 0x4FFFFFFF

Testing numbers between 0x50000000 to 0x5FFFFFFF

Testing numbers between 0x60000000 to 0x6FFFFFFF

Testing numbers between 0x70000000 to 0x7FFFFFFF

Testing numbers between 0x80000000 to 0x8FFFFFFF

Testing numbers between 0x90000000 to 0x9FFFFFFF

Testing numbers between 0xA0000000 to 0xAFFFFFFF

Testing numbers between 0xB0000000 to 0xBFFFFFFF

Testing numbers between 0xC0000000 to 0xCFFFFFFF

Testing numbers between 0xD0000000 to 0xDFFFFFFF

Testing numbers between 0xE0000000 to 0xEFFFFFFF

Testing numbers between 0xF0000000 to 0xFFFFFFFF

Passed all tests for i2f.

[vagrant@localhost hw3]$ ./test 0

i = 0x00000000, answer = 0x00000000, your solution = 0x00000000

answer:

sign = 0, exp = 0x0, frac = 0x0

Passed one test for number 0.

[vagrant@localhost hw3]$ ./test 64

i = 0x00000040, answer = 0x42800000, your solution = 0x42800000

answer:

sign = 0, exp = 0x85, frac = 0x0

Passed one test for number 64.

[vagrant@localhost hw3]$ ./test 0xabcd130

i = 0x0ABCD130, answer = 0x4D2BCD13, your solution = 0x4D2BCD13

answer:

sign = 0, exp = 0x9A, frac = 0x2BCD13

Passed one test for number 180146480.

Here is a sample output on the given incomplete float_i2f function implementation.

[vagrant@localhost hw3handout]$ ./test

Testing numbers between 0x00000000 to 0x0FFFFFFF

i = 0x00000001, answer = 0x3F800000, your solution = 0x00000000

i = 0x00000002, answer = 0x40000000, your solution = 0x00000000

i = 0x00000003, answer = 0x40400000, your solution = 0x00000000

i = 0x00000004, answer = 0x40800000, your solution = 0x00000000

i = 0x00000005, answer = 0x40A00000, your solution = 0x00000000

i = 0x00000006, answer = 0x40C00000, your solution = 0x00000000

i = 0x00000007, answer = 0x40E00000, your solution = 0x00000000

i = 0x00000008, answer = 0x41000000, your solution = 0x00000000

i = 0x00000009, answer = 0x41100000, your solution = 0x00000000

i = 0x0000000A, answer = 0x41200000, your solution = 0x00000000

Failed 10 tests. Aborting test.

[vagrant@localhost hw3handout]$ ./test 0

i = 0x00000000, answer = 0x00000000, your solution = 0x00000000

answer:

sign = 0, exp = 0x0, frac = 0x0

Passed one test for number 0.

[vagrant@localhost hw3handout]$ ./test 64

i = 0x00000040, answer = 0x42800000, your solution = 0x00000000

answer:

sign = 0, exp = 0x85, frac = 0x0

f:

sign = 0, exp = 0x0, frac = 0x0

Failed one test.

[vagrant@localhost hw3handout]$ ./test 0xabcd130

i = 0x0ABCD130, answer = 0x4D2BCD13, your solution = 0x00000000

answer:

sign = 0, exp = 0x9A, frac = 0x2BCD13

f:

sign = 0, exp = 0x0, frac = 0x0

Failed one test.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!