/*
* float_i2f - Return bit-level equivalent of expression (float) x
* Result is returned as unsigned int, but
* it is to be interpreted as the bit-level representation of a
* single-precision floating point values.
* Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
* Max ops: 30
* Rating: 4
*/
unsigned float_i2f(int x){
unsigned leftshift =0;
unsigned frac32 ;
unsigned frac23;
unsigned guard;
unsigned anssign;
unsigned exponent;
unsigned answer;
int temp;
int help;
int tmin = 0x80000000;
// Determine the signed bit
if (x<0)
anssign = tmin;
else
anssign =0;
// case for zero
if (!x)
return 0;
if (anssign)
// If -ve make +ve
{
x = -x ;
}
while (!((tmin) & (x)))
// shift it to the left till u get to leading 1
{
// x = x*2;
x = x<<1;
leftshift = leftshift +1;
}
// fraction in 32 bits
frac32 = x<<1;
// determining exponent
exponent = 158 - (leftshift) ;
//guard bit
guard = (frac32 & (0x200))>>9;
//round + stick bits
help = frac32 & (0x000001ff);
frac23 = frac32>>9;
answer = anssign + (exponent <<23) + frac23;
if (help > 0x00000100 || ((help==0x00000100) && (guard)) )
// rounding condition
{
// to ensure that overflow goes on to exponent we do rounding on exponent + frac
temp = (exponent<<23) + frac23;
// rounding up
return anssign + (temp+1);
}
return answer;
}