diff --git a/_posts/2024-04-23-Fast-inverse-square-root.md b/_posts/2024-04-23-Fast-inverse-square-root.md new file mode 100644 index 0000000..ee6e8b6 --- /dev/null +++ b/_posts/2024-04-23-Fast-inverse-square-root.md @@ -0,0 +1,119 @@ +--- +layout: post +title: 快速平方根倒数 +date: 2024-04-23 13:00:00 +0800 +categories: algorithm +tags: bit-hack +published: true +--- + +* content +{:toc} + +## 雷神之锤3源码 + +```C +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 + + return y; +} +``` + +## 牛顿迭代 + + + +## IEEE 754 + +科学计数法 + +normalised numbers +denormalised numbers +NaN +0 & -0 + +## 推导 + +x 为 float,令 y 为 x 的平方根倒数: +$$ +\begin{aligned} +& \quad\,y=\frac{1}{\sqrt{x}}={x^{-s\frac{1}{2}}}\\ +&\quad\,y=\frac{1}{\sqrt{x}}={x^{-s\frac{1}{2}}}\\ +&\Rightarrow\log_2 (y)=-\frac{1}{2}\log_2 (x)\\ +\Rightarrow\log_2 (y)=-\frac{1}{2}\log_2 (x)\\ +$$ + +代入 x 和 y float 的 long representation + +$$ +&\rightarrow \log_2 (2^(E_y-127)*(1.0+\frac{M_y}{2^23}))=-\frac{1}{2}\log_2 (2^(E_x-127)*(1.0+\frac{M_x}{2^23}))\\ +&⇒=\sum_{i=1}^{n-k}{i^2} + (2n-k+1)\sum_{i=n-k+1}^{n}{i} - \sum_{i=n-k+1}^{n}{i^2}\\ +&=\sum_{i=1}^{n-k}{i^2} + (2n-k+1)(\sum_{i=1}^{n}{i}-\sum_{i=1}^{n-k}{i}) - (\sum_{i=1}^{n}{i^2}-\sum_{i=1}^{n-k}{i^2})\\ +&=2\sum_{i=1}^{n-k}{i^2} - \sum_{i=1}^{n}{i^2} + (2n-k+1)(\sum_{i=1}^{n}{i}-\sum_{i=1}^{n-k}{i})\\ +&=2\frac{(n-k)[(n-k)+1][2(n-k)+1]}{6} - \frac{n(n+1)(2n+1)}{6} + (2n-k+1)(\frac{n(n+1)}{2}-\frac{(n-k)(n-k+1)}{2})\\ +&=\frac{2n^3+3n^2+n-k^3+k}{6} +\end{aligned} +$$ + +$\log_2 10$ + +$$\lg_2 10$$ + +$$\lg 10$$ + +$$lg 10$$ + +$$\ln 10$$ + +$$ln 10$$ + +$$\log_2 10$$ + +$$log_2 10$$ + +$\sqrt{3x-1}+(1+x)^2$ + +$$\sqrt3$$ + +$$\sqrt{3}$$ + +$$\sqrt{3x-1}$$ + +$$\sqrt{3x-1}+(1+x)^2$$ + +$10^{10}$ + +$\frac{4n^3-6n^2-10n-2k^3-3k^2+8k+12kn+c}{12}$ + +$$\frac{4n^3-6n^2-10n-2k^3-3k^2+8k+12kn+c}{12}$$ + +$$\frac{4}{12}$$ + +$$\tfrac{4}{12}$$ + +$$\dfrac{4}{12}$$ + +$$\cfrac{4}{12}$$ + +## 总结 + +现在已经有了平方根运算器,已经不需要这么写了。 + + + + + + + \ No newline at end of file