/* * 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 s = uf & (1 << 31); unsignedexp = (uf & 0x7f800000) >> 23; unsigned frac = uf & (~0xff800000);
if (exp == 0) return frac << 1 | s; if (exp == 255) return uf; exp++; if (exp == 255) return0x7f800000 | s; return s | (exp << 23) | frac; }
/* * 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 s = uf & (1 << 31); unsignedexp = (uf & 0x7f800000) >> 23; unsigned frac = uf & (~0xff800000);
int E = exp - 127; // 阶数 if (exp == 0Xff || E > 31) return0x80000000u; if (E < 0) return0; int Ori = frac | (0x800000); unsigned ret = (E > 23 ? Ori << (E - 23) : Ori >> (23 - E)); if (s) ret *= -1;
/* * 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) { int frac, exp; if (x >= 128) return0x7f800000; if (x >= -126) { return ((x + 127) << 23); } if (x >= -149) { return (1 << (x + 149)); } return0; }