06月 29th, 2008 0x5f3759df
Quake III的代码里面找出来的一段飞速计算平方根的倒数的代码。算法是牛顿迭代法的无限逼近,亮点在标题的这个0x5f3759df,被称为“Magic Number”,这个东东让计算平方根比一般的牛顿逼近快了4倍。又据说,0x5f375a86这个数比0x5f3759df效率更高。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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, this can be removed #ifndef Q3_VM #ifdef __linux__ assert( !isnan(y) ); // bk010122 - FPE? #endif #endif return y; } |
此文仅供存档之用。火星勿怪。
来源:
Matrix67 : http://www.matrix67.com/blog/archives/362
Code Maestro : http://www.codemaestro.com/reviews/9
欢迎光临Caspar Ant,如果您喜欢本站的内容,欢迎订阅我的RSS;如果您需要出于非商业性质的转载,请先阅读版权声明页面。
日志标签
相关日志
日志信息
本文的Trackback地址为:http://www.casparant.com/0x5f3759df.html/trackback,转载本站文章前请仔细阅读本站的版权声明页。您也可以选择点击下列按钮收藏本文到网络书签中:



















06月 29th, 2008 at 6:13 pm Quote
非常……非常……非常地火……
回复
06月 29th, 2008 at 7:30 pm Quote
@yegle
都说了我是存档用的,哼。
回复
06月 29th, 2008 at 7:37 pm Quote
zt
回复