中国开发网: 论坛: 程序员情感CBD: 贴子 301536
Water-E
说点别的吧,看看Carmack大仙的代码,取rsqrt的函数,比直接调用汇编代码快4倍。
关键就在这个常数0x5f3759df 上

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;
}



Not only does it work, on some CPU's Carmack's Q_rsqrt runs up to 4 times faster than (float)(1.0/sqrt(x), eventhough sqrt() is usually implemented using the FSQRT assembley instruction!


http://www.codemaestro.com/reviews/review00000105.html
嘿嘿

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录