首页 > 代码库 > 一些函数

一些函数

ref: http://www.guokr.com/post/90718/

开平方根

 1 // 平方根倒数 1/x
 2 float Q_rsqrt(float x)
 3 {
 4     float x2 = x * 0.5F;
 5     int i  = *(int*)&x; // evil floating point bit level hacking
 6     //i  = 0x5f3759df - (i >> 1);     // what the fuck?
 7     i  = 0x5f375a86 - (i >> 1);     // what the fuck?
 8     x  = *(float*)&i;
 9     x  = x * (1.5F - (x2 * x * x)); // 1st iteration
10     return x;
11 }
12 // 平方根
13 float SquareRootFloat(float number) {
14     long i;
15     float x, y;
16     const float f = 1.5F;
17 
18     x = number * 0.5F;
19     y  = number;
20     i  = * ( long * ) &y; 
21     i  = 0x5f3759df - ( i >> 1 );
22     y  = * ( float * ) &i; 
23 
24     y  = y * ( f - ( x * y * y ) );
25     y  = y * ( f - ( x * y * y ) );
26     return number * y;
27 }
28   

 

一些函数