root/modules/cordic_math.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


   1 /*
   2 CORDIC LIBRARY
   3 integer based trigonometric mathematics
   4 all values scaled by 100000 (CORDIC_SCALE)
   5 
   6 based on http://www.andreadrian.de/c-workshop/index.html
   7 
   8 (c)2012/2013 rudi from CHDK[-DE] forum
   9 License GPL 2.0
  10 
  11 trigonometric functions
  12     angle as DEG:
  13     sind, cosd, tand, asind, acosd, atand, recd, pold
  14     angle as RAD:
  15     sinr, cosr, tanr, asinr, acosr, atanr, recr, polr
  16 
  17 additional functions
  18     integer calculation (a * b / c):
  19     fixed muldivScaled(fixed a , fixed b, fixed c)
  20 
  21 additional math functions
  22     fixed fint(a)   get integer
  23     fixed fceil(a)  get largest previous integer
  24     fixed ffloor(a) get smallest following integer
  25     fixed fround(a) get nearest integer
  26 
  27 all values in CORDIC routines are from type 'fixed' (4 byte integer)
  28     makros to convert values:
  29     FIXED(X) ...... convert double to cordic fixed
  30     FLOAT(X) ...... convert cordic fixed to double
  31     INT2FIXED(X) .. convert integer (value *1000) to cordic fixed
  32     INT2FIXEDR(X) . equal to INT2FIXED(X) with round
  33     FIXED2INT(X) .. convert cordic fixed to integer (value * 1000)
  34     FIXED2INTR(X) . equal to FIXED2INT(X) with round
  35 */
  36 
  37 typedef long fixed;
  38 typedef int int4b; //4 byte integer
  39 enum fcordic {ROTATE, VECTOR};
  40 typedef int fcordic;
  41 enum tangle {RAD, DEG};
  42 typedef int tangle;
  43 
  44 enum {
  45     FRACTIONBITS = 17,
  46     N = 17,
  47     M = 9,
  48     CORDIC_SCALE = 1 << FRACTIONBITS,
  49     CORDIC_INTEGER = ~(CORDIC_SCALE - 1),
  50     INT_SCALE = 1000,
  51 };
  52 
  53 #include "limits.h"
  54 
  55 #define FIXED(X)        (floatToFixed((X)))
  56 #define FLOAT(X)        ((X) / (double)CORDIC_SCALE)
  57 #define INT2FIXED(X)    (intToFixed((X), 0))
  58 #define INT2FIXEDR(X)   (intToFixed((X), 1))
  59 #define FIXED2INT(X)    (fixedToInt((X), 0))
  60 #define FIXED2INTR(X)   (fixedToInt((X), 1))
  61 
  62 //intern used functions
  63 LUALIB_API void cordic(tangle t, fcordic f, fixed *x, fixed *y, fixed *z);
  64 LUALIB_API fixed mulScaled(fixed a, fixed b);
  65 LUALIB_API fixed divScaled(fixed a, fixed b);
  66 LUALIB_API int convertToQ1(fixed *x, fixed *y);
  67 LUALIB_API void convertFromQ1(fixed *x, fixed *y, int q);
  68 LUALIB_API int rotateToQ1(tangle t, fixed *phi);
  69 LUALIB_API void rotateFromQ1(tangle t, fixed *phi, int q);
  70 LUALIB_API fixed cathetus(fixed x);
  71 /* base CIRCULAR mode, ROTATE */
  72 LUALIB_API void sincosCordic(tangle t, fixed phi, fixed *sinphi, fixed *cosphi);
  73 /* base CIRCULAR mode, VECTOR */
  74 LUALIB_API void atanhypCordic(tangle t, fixed px, fixed py, fixed *phi, fixed *hyp);
  75 /* functions CIRCULAR mode, ROTATE */
  76 LUALIB_API fixed sinCordic(tangle t, fixed phi);
  77 LUALIB_API fixed cosCordic(tangle t, fixed phi);
  78 LUALIB_API fixed tanCordic(tangle t, fixed phi);
  79 LUALIB_API void recCordic(tangle t, fixed r, fixed theta, fixed *px, fixed *py);
  80 /* functions CIRCULAR mode, VECTOR */
  81 LUALIB_API fixed asinCordic(tangle t, fixed x);
  82 LUALIB_API fixed acosCordic(tangle t, fixed x);
  83 LUALIB_API fixed atanCordic(tangle t, fixed x);
  84 LUALIB_API void polCordic(tangle t, fixed px, fixed py, fixed *r, fixed *theta);
  85 
  86 // extern
  87 // PI/2, PI, 2*PI
  88 LUAI_DATA fixed FULL_CIRCLE[];
  89 LUAI_DATA fixed HALF_CIRCLE[];
  90 LUAI_DATA fixed QUART_CIRCLE[];
  91 //used by macro
  92 LUALIB_API fixed floatToFixed(double a);
  93 LUALIB_API fixed intToFixed(int4b a, int round);
  94 LUALIB_API int4b fixedToInt(fixed a, int round);
  95 //additional
  96 LUALIB_API fixed muldivScaled(fixed a, fixed b, fixed c);
  97 // DEG
  98 LUALIB_API fixed sind(fixed phi);
  99 LUALIB_API fixed cosd(fixed phi);
 100 LUALIB_API fixed tand(fixed phi);
 101 LUALIB_API void recd(fixed r, fixed theta, fixed *px, fixed *py);
 102 LUALIB_API fixed asind(fixed x);
 103 LUALIB_API fixed acosd(fixed x);
 104 LUALIB_API fixed atand(fixed x);
 105 LUALIB_API void pold(fixed px, fixed py, fixed *r, fixed *theta);
 106 // RAD
 107 LUALIB_API fixed sinr(fixed phi);
 108 LUALIB_API fixed cosr(fixed phi);
 109 LUALIB_API fixed tanr(fixed phi);
 110 LUALIB_API void recr(fixed r, fixed theta, fixed *px, fixed *py);
 111 LUALIB_API fixed asinr(fixed x);
 112 LUALIB_API fixed acosr(fixed x);
 113 LUALIB_API fixed atanr(fixed x);
 114 LUALIB_API void polr(fixed px, fixed py, fixed *r, fixed *theta);
 115 //additional math
 116 LUALIB_API fixed fint(fixed a);
 117 LUALIB_API fixed fceil(fixed a);
 118 LUALIB_API fixed ffloor(fixed a);
 119 LUALIB_API fixed fround(fixed a);

/* [<][>][^][v][top][bottom][index][help] */