maug
Quick and dirty C mini-augmentation library.
Loading...
Searching...
No Matches
retrofp.h
Go to the documentation of this file.
1
2#ifndef RETROFP_H
3#define RETROFP_H
4
17#define RETROFP_PI (3141)
18
24 const int16_t* SEG_MCONST lut, int16_t num, int16_t mult );
25
26#define retrofp_sin( num, mult ) retrofp_lut( g_retrofp_sin, num, mult )
27
28#define retrofp_cos( num, mult ) retrofp_lut( g_retrofp_cos, num, mult )
29
30#ifdef RETROFP_C
31
32MAUG_CONST int16_t SEG_MCONST g_retrofp_cos[] = {
33 1000,
34 995,
35 980,
36 955,
37 921,
38 877,
39 825,
40 764,
41 696,
42 621,
43 540,
44 453,
45 362,
46 267,
47 169,
48 70,
49 -29,
50 -128,
51 -227,
52 -323,
53 -416,
54 -504,
55 -588,
56 -666,
57 -737,
58 -801,
59 -856,
60 -904,
61 -942,
62 -970,
63 -989,
64 -999,
65 -998,
66 -987,
67 -966,
68 -936,
69 -896,
70 -848,
71 -790,
72 -725,
73 -653,
74 -574,
75 -490,
76 -400,
77 -307,
78 -210,
79 -112,
80 -12,
81 87,
82 186,
83 283,
84 377,
85 468,
86 554,
87 634,
88 708,
89 775,
90 834,
91 885,
92 927,
93 960,
94 983,
95 996,
96};
97
98MAUG_CONST int16_t SEG_MCONST g_retrofp_sin[] = {
99 0,
100 99,
101 198,
102 295,
103 389,
104 479,
105 564,
106 644,
107 717,
108 783,
109 841,
110 891,
111 932,
112 963,
113 985,
114 997,
115 999,
116 991,
117 973,
118 946,
119 909,
120 863,
121 808,
122 745,
123 675,
124 598,
125 515,
126 427,
127 334,
128 239,
129 141,
130 41,
131 -58,
132 -157,
133 -255,
134 -350,
135 -442,
136 -529,
137 -611,
138 -687,
139 -756,
140 -818,
141 -871,
142 -916,
143 -951,
144 -977,
145 -993,
146 -999,
147 -996,
148 -982,
149 -958,
150 -925,
151 -883,
152 -832,
153 -772,
154 -705,
155 -631,
156 -550,
157 -464,
158 -373,
159 -279,
160 -182,
161 -83,
162};
163
164int16_t retrofp_lut(
165 const int16_t* SEG_MCONST lut, int16_t num, int16_t mult
166) {
167 int16_t cos_out;
168 uint8_t neg = 0;
169
170 /* Can't take an index of a negative number, so hold on to neg for later. */
171 if( num < 0 ) {
172 neg = 1;
173 num *= -1;
174 }
175
176 /* cos/sin repeat after every 2PI. */
177 if( num >= (2 * RETROFP_PI) ) {
178 num -= (2 * RETROFP_PI);
179 }
180
181 /* Remove num precision to get index. */
182 num /= 100;
183 cos_out = lut[num];
184
185 /* Multiply by multiplier before removing precision. */
186 cos_out *= mult;
187 cos_out /= 1000;
188
189 /* Restore neg taken earlier. */
190 if( neg ) {
191 cos_out *= -1;
192 }
193
194 return (int16_t)cos_out;
195}
196
197#else
198
199extern MAUG_CONST int16_t SEG_MCONST g_retrofp_cos[];
200extern MAUG_CONST int16_t SEG_MCONST g_retrofp_sin[];
201
202#endif /* RETROFP_C */
203
204 /* maug_retrofp */
205
206#endif /* !RETROFP_H */
207
#define RETROFP_PI
Fixed-point representation of Pi (3.141).
Definition retrofp.h:17
int16_t retrofp_lut(const int16_t *SEG_MCONST lut, int16_t num, int16_t mult)
Given a lookup table, return the corresponding value after trimming precision and making sure number ...