Quake3引擎里用了这么一种火星算法:
//
// 计算参数x的平方根的倒数
//
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;
}
个人觉得 set /a 支持位运算,应该可以实现开方倒数的
In the Quake3 engine, there is such a "mars algorithm":
//
// Calculate the reciprocal of the square root of parameter x
//
float InvSqrt (float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i >> 1); // Calculate the first approximate root
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x); // Newton's iteration method
return x;
}
Personally, I think that set /a supports bit operations, and it should be possible to implement the reciprocal of the square root.