2012-12-03 12:32 AM
Hello,
I'm looking for a very fast SQRT.c function (32bit Int to 16bit Int) that works on STM32f100. I already have one but it's slow. thank you, #sqrt-stm32f100 #integer-square-root2012-12-03 01:08 AM
Piecewise linear interpolation in a precalculated table.
That's the way most integer fft algorithms doing it with sin/cos.2012-12-03 03:05 AM
Google ''Crenshaw Integer Square Root'' or buy Jack Crenshaw's book.
2012-12-03 06:56 AM
/**
* @brief Integer square root for RMS
* @param sqrtAvg (sum(x squared) / count)
* @retval approximate square root
*
* Approximate integer square root, used for RMS calculations.
*/
static unsigned int sqrtI( unsigned long sqrtArg )
{
unsigned int answer, x;
unsigned long temp;
if ( sqrtArg == 0 ) return 0; // undefined result
if ( sqrtArg == 1 ) return 1; // identity
answer = 0; // integer square root
for( x=0x8000; x>0; x=x>>1 )
{ // 16 bit shift
answer |= x; // possible bit in root
temp = __builtin_muluu(answer, answer); // fast unsigned multiply
if (temp == sqrtArg) break; // exact, found it
if (temp > sqrtArg) answer ^= x; // too large, reverse bit
}
return answer; // approximate root
}
Here's a simple bitwise successive approximation routine I use in motor controllers. It depends on a relatively fast multiply. The muluu routine is a PIC33 DSP hardware instruction for unsigned multiply.
On average theiterations to derive a sqrt is log2 of the number, assuming an even distribution.
Jack Peacock
2012-12-03 07:16 AM
LUT and one or two Newton iterations may be accurate enough and fast.