源自 Quake3 的快速求 InvSqrt() 函数

ID Software 的 Quake3 代码里面有这么一段,可以快速求解 1/sqrt(x):


float InvSqrt(float x){
float xhalf=0.5f*x;
long i=*(long*)&x;
i=0x5f3759df - (i>>1);
x=*(float *)&i;
x=x*(1.5f-xhalf*x*x);
return x;
} float InvSqrt(float x){
float xhalf=0.5f*x;
long i=*(long*)&x;
i=0x5f3759df - (i>>1);
x=*(float *)&i;
x=x*(1.5f-xhalf*x*x);
return x;
}

Beyond3D.com 的 Ryszard Sommefeldt 一直在到底是哪个家伙写了这些神奇的代码?2003年Chris Lomont还写了一篇文章对这些代码进行了分析。

对于编译,OS 这些我还有很多要看,CSDN 上有篇帖子对上面的问题进行了讨论。



Comments

  1. Quote
    zoro said 2006-12-07, 13:25:

    这段代码应该不是id的人原创吧?

  2. Quote

    好像有人考究了不是,不过具体是哪里还不清楚

Leave a Comment

(required)

(required)

Formatting Your Comment

The following XHTML tags are available for use:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

URLs are automatically converted to hyperlinks.