English: Aliasing a positive IEEE-754floating point number x to an integer Ix (like in C: float x = ...; int32_t i = * (int32_t *) &x;) yields a scaled and shifted approximation of its base-2 logarithm. This is the basis of the fast inverse square root algorithm.
Français : Si un nombre positif x représenté en virgule flottante au format IEEE 754 est interprété comme un entier Ix (comme en C : float x = ...; int32_t i = * (int32_t *) &x;) cela produit, à une transformation linéaire près, une approximation de son logarithme en base 2. Cette approximation est le fondement de l'algorithme de racine carrée inverse rapide.
This W3C-unspecified plot was created with Gnuplot.
Gnuplot source
Warning: This source contains non-ASCII characters in label 3, in the title of the first curve and in comments.
#!/usr/bin/gnuplot# Aliasing a float32 to an int32 yields an approximation of its base-2# logarithm.# Parameters for IEEE-754 single-precision floatsL=2**23# mantissa scaling factorB=127# exponent bias# For m ∈ [0, 1), log₂(1 + m) ≈ 1 + σsigma=0.0430357# Assume x is a positive normal float32.# Compute I(x): the int32 aliased to x.log_of_2=log(2)log2(x)=log(x)/log_of_2round(x)=floor(x+0.5)e(x)=floor(log2(x))# exponentm(x, e_x)=x/2**e_x-1# mantissa, without the leading 1M(x, e_x)=round(m(x,e_x)*L)# scaled and rounded mantissaI2(x, e_x)=(e_x+B)*L+M(x,e_x)I(x)=I2(x,e(x))# Best fit to a logarithmscaled_log(x)=L*log2(x)+L*(B-sigma)# Do the plotsetterminalsvgsize640,480enhancedsetoutput'Log_by_aliasing_to_int.svg'setxrange[0:10]setyrange[0x3d800000:]setformaty"0x%x"setyticsLsetkeytopleftLeftreverseinvertspacing1.4setsamples200setstyleline1lw3lcrgb"gray80"setstyleline2lw1lcrgb"blue"setstyleincrementusersetlabel1"L = 2^{23}"at6,0x3fc00000setlabel2"B = 127"at6,0x3f600000setlabel3"σ = 0.0430357"at6,0x3f000000plotscaled_log(x)title"L log_2(x) + L (B − σ)",\
I(x)title'I_x'lt3
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
to share – to copy, distribute and transmit the work
to remix – to adapt the work
Under the following conditions:
attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.