int * const exp_ptr = reinterpret_cast<int *>(&val); int &x = *exp_ptr; //const int log_2 = ((x >> 23) & 255) - 128; const int log_2 = (x >> 23) - 128; // (1) : "& 255" is not necessary as log of -0 (val < 0) is illegal anyway
x &= ~(255 << 23); x += 127 << 23; //*exp_ptr = x; val = ((-1.0f/3) * val + 2) * val - 2.0f/3; //(2)
return (val + log_2); }
Comments:
1) The line "const int log_2 = ((x >> 23) & 255) - 128" is commented out and replaced with a simplified one, because we do not need the bit mask given the requirement that val > 0.
2) The line marked with (2) computes 1 + log2(m), m ranging from 1 to 2. The proposed formula is a 3rd degree polynomial keeping first derivate continuity. Higher degree could be used for more accuracy. For faster results, one can remove this line, if accuracy is not the matter (it gives some linear interpolation between powers of 2).