This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

speed up square root computation (approximation)

Hello,

I have to compute the square root of a floating point number.
A function sqrt() already exists in the math.h, but this function is too slow and the computation time depends on the input value (because of an iterative method used to compute the square root). The accuracy requirements in my case are not very high, so 2 digits after decimal point would be enough.

So I seached for an alternative method to compute a square root and found an interessting method, that uses an approximation to compute the square root (see below). But this only works on 32 bit CPUs (little endian), so I tried to port it to a ST10.

I thought this is not very difficult because the ST10 also uses little endian and is IEEE-754 compatible.

The function to approximate the square root:
float fastsqrt(float val) {
int tmp = *(int *)&val;
tmp -= 1<<23; /* Remove IEEE bias from exponent (-2^23) */
/* tmp is now an appoximation to logbase2(val) */
tmp = tmp >> 1; /* divide by 2 */
tmp += 1<<23; /* restore the IEEE bias from the exponent (+2^23) */
return *(float *)&tmp;
}

I replaced all int with long (because long is 32bit on ST10), but this doesn't work anyway.

It seems that the behavior of the shifts is different on the ST10 in combination with long numbers than on a 32-bit CPU.

Does anybody has same hints for me to make this function working?
Or has somebody an alternative function for approximate or compute a square root in a quick way on a ST10?

Thanks a lot...

Alexander

0