Try   HackMD

Fast Inverse Square Root 手動推導過程紀錄

來源

https://www.youtube.com/watch?v=p8u_k2LIZyo

IEE 754

https://www.h-schmidt.net/FloatConverter/IEEE754.html

Mantissa is

M
Exponent is
E

223E+M

(1+M223)2E127
log2

log2((1+M223)2E127)

化簡
log2(1+M223)+log2(2E127)

log2(1+M223)+(E127)

quake 的作者利用數學上的技巧偷了一點近似值
當x 數字很小的時候就越接近x 自己,當x 在 0 到 1 之間
加上
μ
變數,可以調整這個範圍減少整體的平均面積(總之就是減少整體誤差)
log2(1+x)x+μ

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

繼續化簡

M223+μ+(E127)

變化一下

M223+E+μ127
M223+E223223+μ127

M+(E)223223+μ127

(M1)+(E1223)223+μ127

1223(M+E223)+μ127

所以跟上面的IEEE 754 找到相同的

223E+M
就是代表跟
log2(223E+M)(223E+M)

把IEEE 754 的float 數字當作long 來操作,但是不進行轉換

得到記憶體位置,然後把整體當作long 的樣子來操作,最後取值。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

介紹一下基本知識

x2=xxx1=1xx12=xx12=1xi=log(y)4=(8>>1)12x=(x>>1)log(1y0=log(y12))=12log(y)=(i>>1)

magic number 的主要計算過程

Γ=(1+MΓ223)2EΓ127
y=(1+My223)2Ey127

log(Γ)=12log(y)
1223(MΓ+EΓ223)+μ127=12((1+My223)2Ey127)

然後想要求解的是:

(MΓ+EΓ223)

(MΓ+EΓ223)=32223(127μ)12((My+Ey223))

當中魔法數字:

32223(127μ)
向右位移:
12((My+Ey223))=(i>>1)

輸入:
(My+Ey223)=i

牛頓接近法