/*
* floatPower2 - Return bit-level equivalent of the expression 2.0^x
* (2.0 raised to the power x) for any 32-bit integer x.
*
* The unsigned value that is returned should have the identical bit
* representation as the single-precision floating-point number 2.0^x.
* If the result is too small to be represented as a denorm, return
* 0. If too large, return +INF.
*
* Legal ops: Any integer/unsigned operations incl. ||, &&. Also if, while
* Max ops: 30
* Rating: 4
*/
unsigned floatPower2(int x) {
if (x > 127) {
return 0x7F800000;
}
// norm
if (x >= -126) {
int E = x + 127;
return E << 23;
}
// denorm
if (x >= -149) {
int F = 1 << (x + 149);
return F;
}
return 0;
}
When running btest, it gets this:
ERROR: Test floatPower2 failed.
Timed out after 10 secs (probably infinite loop)
When compiling btest, it gets some warning, but the tests other than floatPower2 works well.
Warning as bellow:
$ make btest
gcc -O -Wall -m32 -lm -o btest bits.c btest.c decl.c tests.c
btest.c: In function ‘test_function’:
btest.c:334:23: warning: ‘arg_test_range’ may be used uninitialized [-Wmaybe-uninitialized]
334 | if (arg_test_range[2] < 1)
| ~~~~~~~~~~~~~~^~~
btest.c:299:9: note: ‘arg_test_range’ declared here
299 | int arg_test_range[3]; /* test range for each argument */
| ^~~~~~~~~~~~~~
I don’t know why it gets timed out.Is this warning the cause?
this lab can be downloaded here
I expected the program to test if my code is correct.