|
[x]
Posted via EE Mobile
|
||
Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
||
| Question |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: |
/*
* CS:APP Data Lab
*
* fp-add.c - Source file for the floating-point rounding Lab.
* You will hand in "fp-round.c"
*/
#if 0
/*
* Read the following instructions carefully.
*/
You will provide your solution to this lab by editing the file "fp-round.c".
Each "Expr" is an expression using ONLY the following:
1. Integer constants 0 through 255 (0xFF), inclusive. You are
not allowed to use big constants such as 0xffffffff.
2. Function arguments and local variables (no global variables).
3. Unary integer operations ! ~
4. Binary integer operations & ^ | + << >>
Some of the problems restrict the set of allowed operators even further.
Each "Expr" may consist of multiple operators. You are not restricted to
one operator per line.
You are expressly forbidden to:
1. Use any control constructs such as do, while, for, switch, etc.
2. Define or use any macros.
3. Define any additional functions in this file.
4. Call any functions.
5. Use any other operations, such as &&, ||, -, or ?:
6. Use any form of casting.
However, you may use one-level-deep IF statements. That is, you
may write
if( test ) {
varQ = ExprQ;
varR = ExprR;
...
}
You may assume that your machine:
1. Uses 2s complement, 32-bit representations of integers.
2. Performs right shifts arithmetically.
3. Has unpredictable behavior when shifting an integer by more
than the word size.
#endif
#include "fp.h"
int print_flg = 0;
i32 fp_add( i32 x, i32 y )
{
i32 t;
// Set X to be input with equal or larger exponent.
if ( f32_exp(x) < f32_exp(y) ) {t = y; y = x; x = t;}
i32 x_exp = f32_exp(x);
i32 y_exp = f32_exp(y);
i32 x_dnrm = ! x_exp;
i32 y_dnrm = ! y_exp;
i32 ans_exp = x_exp;
if ( x_dnrm ) x_exp = 1;
if ( y_dnrm ) y_exp = 1;
i32 exp_diff = x_exp - y_exp;
i32 x_sgn = f32_sgn(x);
i32 x_man = f32_man(x);
if ( !x_dnrm ) {x_man |= (1 << 23);}
i32 y_sgn = f32_sgn(y);
i32 y_man = f32_man(y);
if ( !y_dnrm ) {y_man |= (1 << 23);}
// Example debugging statements...
if ( print_flg ) {
printf( "x_sgn: %d, x_dnrm: %d, x_exp: %02x, x_man: 0x:%08x\n",
x_sgn, x_dnrm, x_exp, x_man );
printf( "y_sgn: %d, y_dnrm: %d, y_exp: %02x, y_man: 0x:%08x\n",
y_sgn, y_dnrm, y_exp, y_man );
}
exp_diff = ( exp_diff > 26 ) ? 26 : exp_diff;
i32 ans_sgn = x_sgn; // Just positive numbers.
i32 ans_man = x_man + ( y_man >> exp_diff );
i32 ans_rnd = y_man & (~(-1 << exp_diff));
// Put your round-to-even solution in the file "round.c".
// The contents of the file "fp-round.c" will be inserted
// here by the "#include" directive.
#include "fp-round.c"
i32 ans_all = f32_sem( ans_sgn, ans_exp, ans_man );
return( ans_all );
}
i32 main (int argc, char *argv[], char *env[] )
{
if32 a, b, f_ans, i_ans;
i32 i;
i32 correct_count = 0;
i32 incorrect_count = 0;
i32 number_of_tests = 100; // Number of test cases to try.
for( i = 0; i < number_of_tests; i++ )
{
print_flg = 0;
// Generate suitable test inputs
do{ a.ival = random(); }
while( f32_exp(a.ival) == 255);
do{ b.ival = random(); }
while( f32_exp(b.ival) == 255);
// Perform the addition, using our integer-based solution.
i_ans.ival = fp_add( a.ival, b.ival );
// Perform the addition, using the floating-point add instruction.
f_ans.fval = a.fval + b.fval;
// If different, rerun with the print_flg set so debugging messages
// can be observed.
if ( i_ans.ival != f_ans.ival )
{
incorrect_count++;
print_flg = 1;
fp_add( a.ival, b.ival );
printf( "i: %5d, ", i );
printf( "a: 0x:%08x, b: 0x:%08x, i_ans: 0x:%08x, f_ans: 0x:%08x.\n\n",
a.ival, b.ival, i_ans.ival, f_ans.ival);
print_flg = 0;
}
else
correct_count++;
}
printf( "tests correct: %d\n", correct_count);
printf( "tests incorrect: %d\n", incorrect_count );
return( !!incorrect_count );
}
|
Advertisement
| Hall of Fame |