home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / graphics / 14240 < prev    next >
Encoding:
Text File  |  1993-01-27  |  3.2 KB  |  140 lines

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