home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20342 < prev    next >
Encoding:
Text File  |  1993-01-28  |  3.0 KB  |  139 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!rpi!ghost.dsi.unimi.it!univ-lyon1.fr!scsing.switch.ch!dxcern!marmac2.in2p3.fr!user
  3. From: Meessen@marvax.IN2P3.fr (Meessen Christophe)
  4. Subject: Fixed point sqrt
  5. Message-ID: <Meessen-280193094030@marmac2.in2p3.fr>
  6. Followup-To: comp.lang.c
  7. Sender: news@dxcern.cern.ch (USENET News System)
  8. Organization: Centre de Physique des Particules de Marseille
  9. Date: Thu, 28 Jan 1993 08:35:23 GMT
  10. Lines: 127
  11.  
  12. In article <VJ.93Jan19115116@dlsi.dl.ac.uk>, vj@gserv1.dl.ac.uk (Dave
  13. Hines) wrote:
  14. > > Well? Does anyone know of a quick and dirty way to get a reasonably accurate
  15. > > square root of a fixed point integer number?
  16. > Hmmm - I have a long integer square root routine. 
  17.  
  18. Dave Hines proposed a sqrt function receiving an unsigned long (X) and
  19. returning
  20. an unsigned long. The remainder, an unsigned long, is accessible as
  21. 'result2'.
  22. The algorithm uses 4 unsigned long variables. 
  23. Thus it's a long -> long function.
  24.  
  25.  
  26. I suggest two algorithms using the same variables. The first one will
  27. compute a sqrt of a long, returning a fixed point value. The second will
  28. compute the sqrt of a fixed point value, returning a fixed point value.
  29. Thus it's a long -> fixed or fixed -> fixed function.
  30.  
  31. We will suppose a fixed is equivalent to a long, and longs are 32 bits
  32. wide.
  33.  
  34. /*
  35.  * fixed sqrtL2F( long X );
  36.  *
  37.  * Long to fixed point square roots. 
  38.  * RETURNS the fixed point square root of X (long).
  39.  * REQUIRES X is positive.
  40.  *
  41.  * Christophe MEESSEN, 1993.
  42.  */
  43.  
  44. fixed sqrtL2F( long X ) {
  45.  
  46.   unsigned long t, q, b, r;
  47.  
  48.   r = X;
  49.   b = 0x40000000;
  50.   q = 0;
  51.  
  52.   while( b > 0 ) {
  53.     t = q + b;
  54.     if( r >= t ) {
  55.       r = r - t;
  56.       q = t + b;
  57.     }
  58.     r = r * 2;     /* shift left  1 bit */
  59.     b = b / 2;     /* shift right 1 bit */
  60.   }
  61.   if( r >= q )
  62.     q = q + 1;
  63.  
  64.   return( q );
  65. }
  66.  
  67.  
  68. /*
  69.  * fixed sqrtF2F( fixed X );
  70.  *
  71.  * Fixed to fixed point square roots. 
  72.  * RETURNS the fixed point square root of X (fixed).
  73.  * REQUIRES X is positive.
  74.  *
  75.  * Christophe MEESSEN, 1993.
  76.  */
  77.  
  78. fixed sqrtF2F ( fixed X ) {
  79.  
  80.   unsigned long t, q, b, r;
  81.  
  82.   r = X;
  83.   b = 0x40000000;
  84.   q = 0;
  85.  
  86.   while( b >= 256 ) {
  87.     t = q + b;
  88.     if( r >= t ) {
  89.       r = r - t;
  90.       q = t + b;
  91.     }
  92.     r = r * 2;     /* shift left  1 bit */
  93.     b = b / 2;     /* shift right 1 bit */
  94.   }
  95.   q = q / 256;     /* shift right 8 bits */
  96.  
  97.   return( q );
  98. }
  99.  
  100. for memory I just rewrote the long to long sqrt function.
  101.  
  102. /*
  103.  * long sqrtL2L( long X );
  104.  *
  105.  * Long to long point square roots. 
  106.  * RETURNS the integer square root of X (long).
  107.  * REMAINDER is in the local variable r of type long on return.  
  108.  * REQUIRES X is positive.
  109.  *
  110.  * Christophe MEESSEN, 1993.
  111.  */
  112.  
  113. long sqrtL2L( long X ) {
  114.  
  115.   unsigned long t, q, b, r;
  116.  
  117.   r = X;
  118.   b = 0x40000000;
  119.   q = 0;
  120.  
  121.   while( b >= 256 ) {
  122.     t = q + b;
  123.     q = q / 2;     /* shift right 1 bit */
  124.     if( r >= t ) {
  125.       r = r - t;
  126.       q = q + b;
  127.     }
  128.     b = b / 4;     /* shift right 2 bits */
  129.   }
  130.  
  131.   return( q );
  132. }
  133.  
  134. This functions have been tested. But I'll be pleased to receive any
  135. comments.
  136. Please respond through mail: Meessen@marvax.in2p3.fr
  137.