## 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
7
8 (c)2012/2013 rudi from CHDK[-DE] forum
10
11 trigonometric functions
12     angle as DEG:
13     sind, cosd, tand, asind, acosd, atand, recd, pold
15     sinr, cosr, tanr, asinr, acosr, atanr, recr, polr
16
18     integer calculation (a * b / c):
19     fixed muldivScaled(fixed a , fixed b, fixed c)
20
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
40 enum {ROTATE, VECTOR};
41 typedef int fcordic;
43 typedef int tangle;
44
45 enum {
46     FRACTIONBITS = 17,
47     N = 17,
48     M = 9,
49     CORDIC_SCALE = 1 << FRACTIONBITS,
50     CORDIC_INTEGER = ~(CORDIC_SCALE - 1),
51     INT_SCALE = 1000,
52 };
53
54 #include "limits.h"
55
56 #define FIXED(X)        (floatToFixed((X)))
57 #define FLOAT(X)        ((X) / (double)CORDIC_SCALE)
58 #define INT2FIXED(X)    (intToFixed((X), 0))
59 #define INT2FIXEDR(X)   (intToFixed((X), 1))
60 #define FIXED2INT(X)    (fixedToInt((X), 0))
61 #define FIXED2INTR(X)   (fixedToInt((X), 1))
62
63 // extern
64 // PI/2, PI, 2*PI
65 LUAI_DATA fixed FULL_CIRCLE[];
66 LUAI_DATA fixed HALF_CIRCLE[];
67 LUAI_DATA fixed QUART_CIRCLE[];
68 //used by macro
69 LUALIB_API fixed floatToFixed(double a);
70 LUALIB_API fixed intToFixed(int4b a, int round);
71 LUALIB_API int4b fixedToInt(fixed a, int round);
73 LUALIB_API fixed muldivScaled(fixed a, fixed b, fixed c);
74 // DEG
75 LUALIB_API fixed sind(fixed phi);
76 LUALIB_API fixed cosd(fixed phi);
77 LUALIB_API fixed tand(fixed phi);
78 LUALIB_API void recd(fixed r, fixed theta, fixed *px, fixed *py);
79 LUALIB_API fixed asind(fixed x);
80 LUALIB_API fixed acosd(fixed x);
81 LUALIB_API fixed atand(fixed x);
82 LUALIB_API void pold(fixed px, fixed py, fixed *r, fixed *theta);
84 LUALIB_API fixed sinr(fixed phi);
85 LUALIB_API fixed cosr(fixed phi);
86 LUALIB_API fixed tanr(fixed phi);
87 LUALIB_API void recr(fixed r, fixed theta, fixed *px, fixed *py);
88 LUALIB_API fixed asinr(fixed x);
89 LUALIB_API fixed acosr(fixed x);
90 LUALIB_API fixed atanr(fixed x);
91 LUALIB_API void polr(fixed px, fixed py, fixed *r, fixed *theta);