Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 31st, 2013  |  syntax: None  |  size: 1.43 KB  |  views: 99  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * float_i2f - Return bit-level equivalent of expression (float) x
  3.  *   Result is returned as unsigned int, but
  4.  *   it is to be interpreted as the bit-level representation of a
  5.  *   single-precision floating point values.
  6.  *   Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while
  7.  *   Max ops: 30
  8.  *   Rating: 4
  9.  */
  10. unsigned float_i2f(int x){
  11.         unsigned leftshift =0;
  12.         unsigned frac32 ;
  13.         unsigned frac23;
  14.         unsigned guard;
  15.         unsigned anssign;
  16.         unsigned exponent;
  17.         unsigned answer;
  18.         int  temp;
  19.         int  help;
  20.         int  tmin = 0x80000000;
  21.         // Determine the signed bit
  22.         if (x<0)
  23.                 anssign = tmin;
  24.         else
  25.                 anssign =0;
  26.         // case for zero
  27.         if (!x)
  28.                 return  0;
  29.         if (anssign)
  30.                 // If -ve make +ve
  31.                 {
  32.                         x = -x ;
  33.                 }
  34.         while (!((tmin) & (x)))
  35.                 // shift it to the left till u get to leading 1
  36.                 {
  37.                         //       x = x*2;
  38.                         x = x<<1;
  39.                         leftshift = leftshift +1;
  40.                 }
  41.         // fraction in 32 bits
  42.         frac32 = x<<1;
  43.         // determining exponent
  44.         exponent = 158 - (leftshift)  ;
  45.         //guard bit
  46.         guard = (frac32 & (0x200))>>9;
  47.         //round + stick bits
  48.         help = frac32 & (0x000001ff);
  49.         frac23 = frac32>>9;
  50.         answer = anssign + (exponent <<23) + frac23;
  51.         if (help > 0x00000100 || ((help==0x00000100) && (guard)) )
  52.                 // rounding condition
  53.                 {
  54.                         // to ensure that overflow goes on to exponent we do rounding on exponent + frac
  55.                         temp = (exponent<<23) + frac23;
  56.                         // rounding up
  57.                         return anssign + (temp+1);
  58.                 }
  59.         return answer;
  60. }