问题描述
我想知道如果任何人都可以向我提供的浮点平方根算法的一些例子,可以利用硬件除法。
I am wondering if anybody can provide me with some examples of floating point square root algorithms which can utilize a hardware divider.
多余的细节:我有一个浮点单元我开发了具有硬件浮点IEEE-754 32位乘法器,加法器,和分频器。我已经实现用牛顿迭代法只用乘法和加法/减法平方根,但现在我想,如果我有一个硬件除法提供给我来比较平方根的吞吐量。
Extra details:I have a floating point unit I am developing which has a hardware floating-point IEEE-754 32-bit multiplier, adder, and divider. I already implemented square root using the Newton-Raphson method using only multiplication and addition/subtraction, but now I want to compare the throughput of square root if I have a hardware divider available to me.
1特定输入是难以计算准确是0x7F7FFFFF的平方根(3.4028234663852886E38)。
1 particular input that is difficult to compute accurately is the square root of 0x7F7FFFFF (3.4028234663852886E38).
推荐答案
解决方案通过@tmyklebu提供肯定会出现,以满足您的要求。
Solution provide by @tmyklebu certainly appears to meet your requirements.
r = input value
s(0) = initial estimate of sqrt(r). Example: r with its exponent halved.
s(n) = sqrt(r)
S&其中; - (S + R / S)/ 2
s <- (s + r/s)/2
据二次收敛,执行请求的鸿沟。 N = 3或4应该这样做了32位浮点。
It has quadratic convergence, performs the requested divide. N = 3 or 4 should do it for 32 bit float.
[每OP要求编辑]
// Initial estimate
static double S0(double R) {
double OneOverRoot2 = 0.70710678118654752440084436210485;
double Root2 = 1.4142135623730950488016887242097;
int Expo;
// Break R into mantissa and exponent parts.
double Mantissa = frexp(R, &Expo);
int j;
printf("S0 %le %d %le\n", Mantissa, Expo, frexp(sqrt(R), &j));
// If exponent is odd ...
if (Expo & 1) {
// Pretend the mantissa [0.5 ... 1.0) is multiplied by 2 as Expo is odd,
// so it now has the value [1.0 ... 2.0)
// Estimate the sqrt(mantissa) as [1.0 ... sqrt(2))
// IOW: linearly map (0.5 ... 1.0) to (1.0 ... sqrt(2))
Mantissa = (Root2 - 1.0)/(1.0 - 0.5)*(Mantissa - 0.5) + 1.0;
}
else {
// The mantissa is in range [0.5 ... 1.0)
// Estimate the sqrt(mantissa) as [1/sqrt(2) ... 1.0)
// IOW: linearly map (0.5 ... 1.0) to (1/sqrt(2) ... 1.0)
Mantissa = (1.0 - OneOverRoot2)/(1.0 - 0.5)*(Mantissa - 0.5) + OneOverRoot2;
}
// Form initial estimate by using the above mantissa estimate and exponent/2
return ldexp(Mantissa, Expo/2);
}
// S = (S + R/S)/2 method
double Sqrt(double R) {
double S = S0(R);
int i = 5; // May be reduced to 3 or 4 for double and 2 for float
do {
printf("S %u %le %le\n", 5-i, S, (S-sqrt(R))/sqrt(R));
S = (S + R/S)/2;
} while (--i);
return S;
}
void STest(double x) {
printf("T %le %le %le\n", x, Sqrt(x), sqrt(x));
}
int main(void) {
STest(612000000000.0);
return 0;
}
经过3次迭代的双重收敛。
Converges after 3 iterations for double.
S0 5.566108e-01 40 7.460635e-01
秒0 7.762279e + 05 -7.767318e-03
第1条7.823281e + 05 3.040175e-05
S 2 7.823043e + 05 4.621193e-10
第3 7.823043e + 05 0.000000e + 00
S 4 7.823043e + 05 0.000000e + 00
牛逼6.120000e + 11 7.823043e + 05 7.823043e + 05
S0 5.566108e-01 40 7.460635e-01
S 0 7.762279e+05 -7.767318e-03
S 1 7.823281e+05 3.040175e-05
S 2 7.823043e+05 4.621193e-10
S 3 7.823043e+05 0.000000e+00
S 4 7.823043e+05 0.000000e+00
T 6.120000e+11 7.823043e+05 7.823043e+05
这篇关于浮点平方根算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!