Beyond Belief

December 4, 2006

Fast InvSqrt()

Filed under: 科技, 網路

寫程式的人喜歡看別人的 code,一來增進功力,二來也可以收為己用
當見到短小精幹,卻有妙用無窮的碼,除了歡喜讚嘆,也有很大的好奇心想知道出自於何人之手,期見大師之風範

Fast InvSqrt() 正是讓眾高手驚嘆的一段程式

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

比用 (float)(1.0/sqrt(x)) 快了四倍以上,同時又有很高的精確度

對於像我這般的低段末手,聼高手闡釋其中精妙,只能恭誦附和,因爲數學底子太差,完全無法頓悟

Rys Sommefeldt 為了追尋作者,一個追一個,不過還是沒有找到始作俑者

在資訊的洪流沖刷下,bits and bytes 很快又將歷史掩埋

Comments »

The URI to TrackBack this entry is: http://bbelief.blogsome.com/2006/12/04/fast-invsqrt/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>























Get free blog up and running in minutes with Blogsome | Theme designs available here