MiniGUI API Reference (MiniGUI-Threads)  v5.0.6
A mature and proven cross-platform GUI system for embedded and smart IoT devices
fixedmath.h
Go to the documentation of this file.
1 //
3 // IMPORTANT NOTICE
4 //
5 // The following open source license statement does not apply to any
6 // entity in the Exception List published by FMSoft.
7 //
8 // For more information, please visit:
9 //
10 // https://www.fmsoft.cn/exception-list
11 //
13 
57 /*
58  * $Id: fixedmath.h 12154 2009-10-15 07:21:34Z weiym $
59  *
60  * MiniGUI for Linux/uClinux, eCos, uC/OS-II, VxWorks,
61  * pSOS, ThreadX, NuCleus, OSE, and Win32.
62  *
63  * Fixed-point math routins come from Allegro (a gift software)
64  * by Shawn Hargreaves and others.
65  */
66 
67 #ifndef _MGUI_MGHAVE_FIXED_MATH_H
68 #define _MGUI_MGHAVE_FIXED_MATH_H
69 
70 
71 #include <errno.h>
72 #include <math.h>
73 #include <stdlib.h>
74 
75 /* Set up for C function definitions, even when using C++ */
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 
80 #ifdef _MGHAVE_FIXED_MATH
81 
122 MG_EXPORT fixed fixsqrt (fixed x);
123 
134 MG_EXPORT fixed fixhypot (fixed x, fixed y);
135 
148 MG_EXPORT fixed fixatan (fixed x);
149 
164 MG_EXPORT fixed fixatan2 (fixed y, fixed x);
165 
166 extern MG_EXPORT const fixed __mg_cos_tbl[];
167 extern MG_EXPORT const fixed __mg_tan_tbl[];
168 extern MG_EXPORT const fixed __mg_acos_tbl[];
169 
170 /************************** inline fixed point math functions *****************/
171 /* ftofix and fixtof are used in generic C versions of fixmul and fixdiv */
172 
185 static inline fixed ftofix (double x)
186 {
187  if (x > 32767.0) {
188  errno = ERANGE;
189  return 0x7FFFFFFF;
190  }
191 
192  if (x < -32767.0) {
193  errno = ERANGE;
194  return -0x7FFFFFFF;
195  }
196 
197  return (long)(x * 65536.0 + (x < 0 ? -0.5 : 0.5));
198 }
199 
209 static inline double fixtof (fixed x)
210 {
211  return (double)x / 65536.0;
212 }
213 
214 
229 static inline fixed fixadd (fixed x, fixed y)
230 {
231  fixed result = x + y;
232 
233  if (result >= 0) {
234  if ((x < 0) && (y < 0)) {
235  errno = ERANGE;
236  return -0x7FFFFFFF;
237  }
238  else
239  return result;
240  }
241  else {
242  if ((x > 0) && (y > 0)) {
243  errno = ERANGE;
244  return 0x7FFFFFFF;
245  }
246  else
247  return result;
248  }
249 }
250 
266 static inline fixed fixsub (fixed x, fixed y)
267 {
268  fixed result = x - y;
269 
270  if (result >= 0) {
271  if ((x < 0) && (y > 0)) {
272  errno = ERANGE;
273  return -0x7FFFFFFF;
274  }
275  else
276  return result;
277  }
278  else {
279  if ((x > 0) && (y < 0)) {
280  errno = ERANGE;
281  return 0x7FFFFFFF;
282  }
283  else
284  return result;
285  }
286 }
287 
301 MG_EXPORT fixed fixmul (fixed x, fixed y);
302 
316 MG_EXPORT fixed fixdiv (fixed x, fixed y);
317 
328 static inline int fixceil (fixed x)
329 {
330  if (x > 0x7FFF0000) {
331  errno = ERANGE;
332  return 0x7FFF;
333  }
334 
335  x += 0xFFFF;
336  if (x > 0)
337  return (x >> 16);
338  else
339  return ~((~x) >> 16);
340 }
341 
350 static inline fixed itofix (int x)
351 {
352  return x << 16;
353 }
354 
363 static inline int fixtoi (fixed x)
364 {
365  return (x >> 16) + ((x & 0x8000) >> 15);
366 }
367 
377 static inline fixed fixcos (fixed x)
378 {
379  return __mg_cos_tbl[((x + 0x4000) >> 15) & 0x1FF];
380 }
381 
391 static inline fixed fixsin (fixed x)
392 {
393  return __mg_cos_tbl[((x - 0x400000 + 0x4000) >> 15) & 0x1FF];
394 }
395 
405 static inline fixed fixtan (fixed x)
406 {
407  return __mg_tan_tbl[((x + 0x4000) >> 15) & 0xFF];
408 }
409 
423 static inline fixed fixacos (fixed x)
424 {
425  if ((x < -65536) || (x > 65536)) {
426  errno = EDOM;
427  return 0;
428  }
429 
430  return __mg_acos_tbl[(x+65536+127)>>8];
431 }
432 
446 static inline fixed fixasin (fixed x)
447 {
448  if ((x < -65536) || (x > 65536)) {
449  errno = EDOM;
450  return 0;
451  }
452 
453  return 0x00400000 - __mg_acos_tbl[(x+65536+127)>>8];
454 }
455 
463 #endif /* _MGHAVE_FIXED_MATH */
464 
465 /* Ends C function definitions when using C++ */
466 #ifdef __cplusplus
467 }
468 #endif
469 
470 #endif /* _MGUI_MGHAVE_FIXED_MATH_H */
471 
fixed
long fixed
Data type of fixed point.
Definition: common.h:1117
fixdiv
MG_EXPORT fixed fixdiv(fixed x, fixed y)
Returns the quotient of two fixed point values.
fixtoi
static int fixtoi(fixed x)
Converts an fixed point value to an integer.
Definition: fixedmath.h:363
fixatan
MG_EXPORT fixed fixatan(fixed x)
Calculates the arc tangent of a fixed point value.
fixcos
static fixed fixcos(fixed x)
Returns the cosine of a fixed point.
Definition: fixedmath.h:377
fixsin
static fixed fixsin(fixed x)
Returns the sine of a fixed point.
Definition: fixedmath.h:391
fixtof
static double fixtof(fixed x)
Converts a fixed point value to a float point value.
Definition: fixedmath.h:209
itofix
static fixed itofix(int x)
Converts an integer to a fixed point value.
Definition: fixedmath.h:350
fixadd
static fixed fixadd(fixed x, fixed y)
Returns the sum of two fixed point values.
Definition: fixedmath.h:229
fixmul
MG_EXPORT fixed fixmul(fixed x, fixed y)
Returns the product of two fixed point values.
fixacos
static fixed fixacos(fixed x)
Calculates and returns the arc cosine of a fixed point.
Definition: fixedmath.h:423
fixsub
static fixed fixsub(fixed x, fixed y)
Subtract a fixed point value from another.
Definition: fixedmath.h:266
fixatan2
MG_EXPORT fixed fixatan2(fixed y, fixed x)
Calculates the arc tangent of two fixed point variables.
fixasin
static fixed fixasin(fixed x)
Calculates and returns the arc sine of a fixed point.
Definition: fixedmath.h:446
fixhypot
MG_EXPORT fixed fixhypot(fixed x, fixed y)
Returns the Euclidean distance from the origin.
fixsqrt
MG_EXPORT fixed fixsqrt(fixed x)
Returns the non-negative square root of a fixed point value.
fixceil
static int fixceil(fixed x)
Rounds a fixed point value to the nearest integer.
Definition: fixedmath.h:328
ftofix
static fixed ftofix(double x)
Converts a float point value to a fixed point value.
Definition: fixedmath.h:185
fixtan
static fixed fixtan(fixed x)
Returns the tangent of a fixed point.
Definition: fixedmath.h:405