Searching \ for 'SQRT (OT)' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=sqrt
Search entire site for: 'SQRT (OT)'.

Truncated match.
PICList Thread
'SQRT (OT)'
1998\02\10@222320 by Walter Banks

picon face
Sean Breheny wrote:
>
> BTW, I'm just curious, what method do most sqrt routenes use?

There is a guy on comp.arch.arithmetic "Dann Corbit"
<spam_OUTdcorbitTakeThisOuTspamsolutionsiq.com> that has posted many
sqrt routines including the following one that
uses no mult's or divides suitable for processors
used in embedded system applications. If you are looking
for speed unfold the for loop.

unsigned long   msqrt(unsigned long input)
{
   unsigned long   s;
   unsigned long   s21;
   long            j;
   s = 0;
   for (j = 15; j >= 0; --j) {
       s21 = (s << (j + 1)) + (1 << (j + j));
       if (s21 <= input) {
           s += (1 << j);
           input -= s21;
       }
   }
   return s;
}

Byte Craft uses a variety of different SQRT routines depending
on the target processor. For integer and fixed point data
types on processors with a hardware multiply we are using
a SQRT routine that Gord Hayward and I wrote that is based on
a forcing function around a multiply. It returns a number the
same size as the original with half the bits the integer
and half the bits a fraction part.  The code for this
follows.


long SQRT (long l)
/*
   Square root V1.0.
   Hayward/Banks approximation.

   This routine generates a square root to eight bit
   resolution taking a 16 bit argument normalizing the
   argument and returning a 16 bit real number that
   consists of 8 bits interger and 8 bit fractional parts.
   The MS8bits are the interger part.

     i i i i  i i i i . f f f f  f f f f
     | | | |  | | | |   | | | |  | | | |
   128 |32 |  8 4 2 1  .5 | | |  | | |  .00390625
      64  16           .25  | |  | |  --.0078125
                       .125-  |  |  ----.015625
                       .0625--    ------.03125


  These routines may be adapted for any purpose when
  used with one of Byte Craft's Code Development systems.
  No warranty is implied or given as to their usability
  for any purpose.

       (c) Byte Craft Limited
       Waterloo, Ontario
       Canada N2J 4E4
       (519) 888-6911

       Walter Banks/Gordon Hayward
*/

 {
   unsigned int n,j,k;
   unsigned long t @ 0x18;  // Multiply result
   n = 0;
   while( (l & 0xc000) == 0)
    {
      n++;
      l <<= 2;
    }
   for (j = 0, k = 0x80; k != 0; k >>=1)
    {
      j = j | k;
      t = j * j;
      if (t > l) j = j & (~k);
    }
   t = j;
   return (t << (8-n));
 }

unsigned long result;
unsigned int int_part, frac_part;
void main(void)
 {
   result = SQRT(6000);
   int_part  = result >> 8;
   frac_part = result & 0xff;
 }

We are using a Cordic function for SQRT on compilers
that have floating point support.

Walter Banks

More... (looser matching)
- Last day of these posts
- In 1998 , 1999 only
- Today
- New search...