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

如果您喜欢本站的内容,欢迎订阅我的RSS以获取本站最新资讯。

RSS地址1:http://feed.casparant.com/

RSS地址2:http://feeds.casparant.com/casparant

标签:, ,

Related Posts

---------- COPYRIGHT (C) CasparAnt.COM 2008 ----------

    本站所有文章均遵循“创作共用条款(CC)3.0版本”, 允许转载演绎本站文章,仅需遵循以下原则:保留文章出处(URL及站名Caspar Ant), 并且给我一个 引用通告(trackback)。如果您觉得本站的文章很好,欢迎选择下面的网络书签收藏本文; 如果您觉得本站值得浏览,欢迎点击侧边栏进行订阅;欢迎您对文章发表评论,您的留言是对我最好的鼓励!
    This entry is under CREATIVE COMMON ATTRIBUTION 3.0 LICENSE. Please remain "Caspar Ant" and the URL stay in your site when you share or remix this entry. It's necessary to give me a trackback from your own site. If you think this entry is good enough, welcome to put it to your own web bookmark. You can select from the bookmark sites at follows:

Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网

3 Responses to “0x5f3759df”

Leave a Reply


Powered by Wordpress © 2008 - Caspar Ant | iKon Theme by TextNData | Admin | | 本站正在备案中