r/ProgrammerTIL • u/mikaey00 • May 16 '19
Other TIL learned how floating-point numbers are represented in binary form.
I'm 37 now, and I've been doing C since I was maybe 14. I never quite understood the binary format of floating point numbers, so finally I sat down and managed to find something that explained it to me. With that, I was able to write the following pseudocode to decode a floating-point number (the example below is for a 32-bit float):
Sign = FloatVal >> 31; // Bit 0
Exponent = ( FloatVal >> 23 ) & 0x7f; // Bits 1-8
Mantissa = FloatVal & 0x7fffff; // Bits 9-31
if( Exponent == 255 ) {
if( Mantissa == 0 ) {
return ( Sign == 1 ? -Infinity : Infinity );
} else {
return ( Sign == 1 ? -NaN : NaN );
}
} else {
if( Exponent != 0 ) {
return ( Sign == 1 ? -1 : 1 ) * ( 1 + ( Mantissa / 0x800000 ) ) * 2^( Exponent - 127 );
} else {
return ( Sign == 1 ? -1 : 1 ) * ( Mantissa / 0x800000 ) * 2^-126;
}
}Thank you to Bruce Dawson's blog that explained this nicely!
164
Upvotes
View all comments
Show parent comment
11
u/CptCap May 17 '19 edited May 17 '19
Hi, I compiled a small benchmark, but to my surprise the fast inverse is faster.
I do not have time to investigate this further right now, but here are a few speculations:
1.0f/sqrtf(x)and I am a moron. (Although they might exists better and faster approximations).-march=native -ffast-mathmight give better result.[edit] This quora answer suggests that better approximations do exists.
[edit] g++ generates very different code for
1.0f / sqrtf(x)with-ffast-math[edit] I think I understand what happens, compilers might not emit the
rsqrtssinstruction due to it having slightly different behavior than1.0f / sqrtf(x)in some weird cases. This means that they have to actually do the division and that might be enough to make it slower.If you are in a situation where you might think about using the fast inverse, you are probably already compiling with
-ffast-math, in which casersqrtsswill be used by the compiler.