home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / graphics / fixedtri.c < prev    next >
Text File  |  1992-04-09  |  2KB  |  72 lines

  1. /* 
  2. Fixed-Point Trigonometry with CORDIC Iterations
  3. by Ken Turkowski
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. #define COSCALE 0x22c2dd1c /* 0.271572 */
  8. #define QUARTER ((int)(3.141592654 / 2.0 * (1 << 28)))
  9. static long arctantab[32] = {  /* MS 4 integral bits for radians */
  10.     297197971, 210828714, 124459457, 65760959, 33381290, 16755422,     8385879, 4193963, 2097109, 1048571, 524287, 262144, 131072,
  11.     65536, 32768, 16384, 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32,     16, 8, 4, 2, 1, 0, 0,
  12. };
  13.  
  14. CordicRotate(px, py, theta)
  15. long *px, *py;
  16. register long theta;    /* Assume that abs(theta) <= pi */
  17. {
  18.     register int i;
  19.     register long x = *px, y = *py, xtemp;
  20.     register long *arctanptr = arctantab;
  21.  
  22.     /* The -1 may need to be pulled out and done as a left shift */
  23.     for (i = -1; i <= 28; i++) {
  24.         if (theta < 0) {
  25.             xtemp = x + (y >> i);
  26.             y     = y - (x >> i);
  27.             x = xtemp;
  28.             theta += *arctanptr++;
  29.         } else {
  30.             xtemp = x - (y >> i);    
  31.             y     = y + (x >> i);
  32.             x = xtemp;    
  33.             theta -= *arctanptr++;
  34.         }
  35.     }
  36.  
  37.     *px = frmul(x, COSCALE); /* Compensate for CORDIC enlargement */
  38.     *py = frmul(y, COSCALE); /* frmul(a,b)=(a*b)>>31, high part  */
  39.                           /* of 64-bit product */
  40. }
  41.  
  42.  
  43.  
  44.  
  45. CordicPolarize(argx, argy)
  46. long *argx, *argy;    /* We assume these are already in the */
  47.                     /*  right half plane */
  48. {
  49.     register long theta, yi, i;
  50.     register long x = *argx, y = *argy;
  51.     register long *arctanptr = arctantab;
  52.     for (i = -1; i <= 28; i++) {
  53.         if (y < 0) {        /* Rotate positive */
  54.             yi = y + (x >> i);
  55.             x  = x - (y >> i);
  56.             y  = yi;
  57.             theta -= *arctanptr++;
  58.         } else {         /* Rotate negative */
  59.             yi = y - (x >> i);
  60.             x  = x + (y >> i);
  61.             y  = yi;
  62.             theta += *arctanptr++;
  63.         }
  64.     }
  65.  
  66.     *argx = frmul(x, COSCALE);
  67.     *argy = theta;
  68. }
  69.  
  70.  
  71.  
  72.