Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/17/06 21:04
Read: times


 
#122505 - I'll optimize it tomorrow.
Responding to: ???'s previous message
no, it sounds reasonable

Maybe. Doing anything with long ints eats cycles like crazy on the '51 architecture, especially bit-shifts. I'll optimize it tomorrow to make better use of the '51.

Here's what the function looks like (from the top of my head, don't have it on my computer at home):

unsigned int sqrt(unsigned long input)
{
  unsigned char i;
  unsigned long remainder = 0;
  unsigned long divisor = 0;

  for(i = 16; i > 0; i--)
  {
    divisor <<= 1;
    remainder = (remainder << 2) + (input >> 30);
    input <<= 2;
    if(divisor < remainder)
    {
      divisor++;
      remainder -= divisor;
      divisor++;
    }
  }
  divisor >>= 1;
  return divisor;
}


On anything that can natively shift 32 bit integers, this should beat the table-lookup-plus-interpolation route easily unless accuracy requirements (table size) is small.

On my '51, some more work might be required. Unrolling the loop and avoiding the shift of input in favor of looking at each byte of input individually should help (the 30 right shifts probably eat cycles like crazy, too). Additionally, divisor will, at most, have 18 significant bits, so for the first 14 iterations only the LSW needs to be shifted (for the first 6, actually, only the LSB).

Of course, doing it in assembly would be a lot faster, since you can do the "shift bit out of A and into B" instead of "shift B left and add A shifted right by 30".

List of 21 messages in thread
TopicAuthorDate
Things you find ...            01/01/70 00:00      
   10 lines PLUS a whole bunch of 'lines'            01/01/70 00:00      
      I forgot to mention ...            01/01/70 00:00      
         sounds reasonable            01/01/70 00:00      
            I'll optimize it tomorrow.            01/01/70 00:00      
               haven't heard of that one            01/01/70 00:00      
                  Do it using the RLC instruction.            01/01/70 00:00      
               Optimized results:            01/01/70 00:00      
                  how does the lookup table approach time            01/01/70 00:00      
                     Comparison:            01/01/70 00:00      
                        Table error            01/01/70 00:00      
                           No error.            01/01/70 00:00      
                           So how did you calculate/measure the average?            01/01/70 00:00      
                              Right below the table:            01/01/70 00:00      
   I do the same thing            01/01/70 00:00      
      Lookup table will win hands down            01/01/70 00:00      
         For floats, yes. For long ints ... not so sure.            01/01/70 00:00      
   one more thing            01/01/70 00:00      
   if the precision is not 'critical'            01/01/70 00:00      
      Another source            01/01/70 00:00      
         What about a Hardware Solution?            01/01/70 00:00      

Back to Subject List