我在http://en.wikipedia.org/wiki/Fast_inverse_square_root上的net Fast Inverse Square Root中找到了.它在x64上是否正常工作?有没有人使用和认真测试?
Lyt*_*yth 19
最初快速反向平方根是为32位浮点编写的,因此只要您使用IEEE-754浮点表示,x64架构就不会影响结果.
请注意,对于"双精度"浮点(64位),您应该使用另一个常量:
...... 64位IEEE754大小型双...的"幻数"显示为正好是0x5fe6eb50c7b537a9
这是双精度浮点数的实现:
#include <cstdint>
double invsqrtQuake( double number )
{
double y = number;
double x2 = y * 0.5;
std::int64_t i = *(std::int64_t *) &y;
// The magic number is for doubles is from https://cs.uwaterloo.ca/~m32rober/rsqrt.pdf
i = 0x5fe6eb50c7b537a9 - (i >> 1);
y = *(double *) &i;
y = y * (1.5 - (x2 * y * y)); // 1st iteration
// y = y * ( 1.5 - ( x2 * y * y ) ); // 2nd iteration, this can be removed
return y;
}
Run Code Online (Sandbox Code Playgroud)
我做了一些测试,看来效果很好