Download presentation
Presentation is loading. Please wait.
Published byShawn Timblin Modified over 9 years ago
1
The Mathematics Behind the Fast Inverse Square Root Function Code
0x5f3759df The Mathematics Behind the Fast Inverse Square Root Function Code McEniry, Charles 2007 November 2014
2
Fast Inverse Square Root
Problem Application Compute Why normalization is called billions of times for lighting and reflection tasks
3
Q_rsqrt: Quake3 source code
float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration return y; }
4
2) Newtonβs method: with initial solution
y= 1 βπ₯ the function to minimize: f . =π¦β 1 βπ₯ π¦ π+1 = π¦ π β π( π¦ π ) πβ²( π¦ π ) Plugging π(π¦ π ), f β² π¦ π = 1 π¦ 2 βπ₯,β 2 π¦ 3 gives π¦(3βπ₯ π¦ 2 ) 2 =π¦(1.5β π₯ π¦ 2 2 ) const float threehalfs = 1.5F; x2 = number * 0.5F; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
5
1) Approximation of log_2(x) using Int(x)
1.1 Floating Point Representation 1.2 Float Perspective: 1.3 Integer Perspective 1.4 Finding int(y) from int(x) i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df - ( i >> 1 ); // what the fuck? y = * ( float * ) &i;
6
0x5f3759df McEniry, Charles (August 2007).Β "The Mathematics Behind the Fast Inverse Square Root Function Code
7
0x5f3759df The integer aliased to a floating point number (in blue), compared to a scaled and shifted logarithm (in gray).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.