//float /* * floatScale2 - Return bit-level equivalent of expression 2*f for * floating point argument f. * Both the argument and result are passed as unsigned int's, but * they are to be interpreted as the bit-level representation of * single-precision floating point values. * When argument is NaN, return argument * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ unsignedfloatScale2(unsigned uf){ unsigned sign = uf >> 31; int mMask = (1 << 23) - 1; unsigned m = uf & mMask; unsigned frac = (uf >> 23) & 0xff; if (frac == 0xff) { return uf; } if (frac == 0) { m <<= 1; } else { frac += 1; } return (sign << 31) + (frac << 23) + m; }
/* * floatFloat2Int - Return bit-level equivalent of expression (int) f * for floating point argument f. * Argument is passed as unsigned int, but * it is to be interpreted as the bit-level representation of a * single-precision floating point value. * Anything out of range (including NaN and infinity) should return * 0x80000000u. * Legal ops: Any integer/unsigned operations incl. ||, &&. also if, while * Max ops: 30 * Rating: 4 */ intfloatFloat2Int(unsigned uf){ unsigned sign = uf >> 31; int mMask = (1 << 23) - 1; unsigned m = uf & mMask; unsigned frac = (uf >> 23) & 0xff; intexp = frac - 127; if (frac == 0xff || exp >= 31) { return1 << 31; } if (exp < 0) { return0; } m += 1 << 23; if (exp <= 23) { m >>= 23 - exp; } else { m <<= exp - 23; } if (sign) { m = ~m + 1; } return m; }
/* * 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 */ unsignedfloatPower2(int x){ unsigned frac, m; if (x < -149) { return0; } if (x > 127) { return0xff << 23; } if (x < -126) { frac = 0; m = 1 << (149 + x); } else { frac = x + 127; m = 0; } return (frac << 23) + m; }