MiniGUI API Reference (MiniGUI-Processes)  v3.2.0
A mature and proven cross-platform GUI system for embedded and smart IoT devices
fixedmath.h
Go to the documentation of this file.
1 
45 /*
46  * $Id: fixedmath.h 12154 2009-10-15 07:21:34Z weiym $
47  *
48  * MiniGUI for Linux/uClinux, eCos, uC/OS-II, VxWorks,
49  * pSOS, ThreadX, NuCleus, OSE, and Win32.
50  *
51  * Fixed-point math routins come from Allegro (a gift software)
52  * by Shawn Hargreaves and others.
53  */
54 
55 #ifndef _MGUI_MGHAVE_FIXED_MATH_H
56 #define _MGUI_MGHAVE_FIXED_MATH_H
57 
58 
59 #include <errno.h>
60 #include <math.h>
61 #include <stdlib.h>
62 
63 /* Set up for C function definitions, even when using C++ */
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 #ifdef _MGHAVE_FIXED_MATH
69 
110 MG_EXPORT fixed fixsqrt (fixed x);
111 
122 MG_EXPORT fixed fixhypot (fixed x, fixed y);
123 
136 MG_EXPORT fixed fixatan (fixed x);
137 
152 MG_EXPORT fixed fixatan2 (fixed y, fixed x);
153 
154 extern MG_EXPORT fixed _cos_tbl[];
155 extern MG_EXPORT fixed _tan_tbl[];
156 extern MG_EXPORT fixed _acos_tbl[];
157 
158 /************************** inline fixed point math functions *****************/
159 /* ftofix and fixtof are used in generic C versions of fixmul and fixdiv */
160 
173 static inline fixed ftofix (double x)
174 {
175  if (x > 32767.0) {
176  errno = ERANGE;
177  return 0x7FFFFFFF;
178  }
179 
180  if (x < -32767.0) {
181  errno = ERANGE;
182  return -0x7FFFFFFF;
183  }
184 
185  return (long)(x * 65536.0 + (x < 0 ? -0.5 : 0.5));
186 }
187 
197 static inline double fixtof (fixed x)
198 {
199  return (double)x / 65536.0;
200 }
201 
202 
217 static inline fixed fixadd (fixed x, fixed y)
218 {
219  fixed result = x + y;
220 
221  if (result >= 0) {
222  if ((x < 0) && (y < 0)) {
223  errno = ERANGE;
224  return -0x7FFFFFFF;
225  }
226  else
227  return result;
228  }
229  else {
230  if ((x > 0) && (y > 0)) {
231  errno = ERANGE;
232  return 0x7FFFFFFF;
233  }
234  else
235  return result;
236  }
237 }
238 
254 static inline fixed fixsub (fixed x, fixed y)
255 {
256  fixed result = x - y;
257 
258  if (result >= 0) {
259  if ((x < 0) && (y > 0)) {
260  errno = ERANGE;
261  return -0x7FFFFFFF;
262  }
263  else
264  return result;
265  }
266  else {
267  if ((x > 0) && (y < 0)) {
268  errno = ERANGE;
269  return 0x7FFFFFFF;
270  }
271  else
272  return result;
273  }
274 }
275 
289 MG_EXPORT fixed fixmul (fixed x, fixed y);
290 
304 MG_EXPORT fixed fixdiv (fixed x, fixed y);
305 
316 static inline int fixceil (fixed x)
317 {
318  if (x > 0x7FFF0000) {
319  errno = ERANGE;
320  return 0x7FFF;
321  }
322 
323  x += 0xFFFF;
324  if (x > 0)
325  return (x >> 16);
326  else
327  return ~((~x) >> 16);
328 }
329 
338 static inline fixed itofix (int x)
339 {
340  return x << 16;
341 }
342 
351 static inline int fixtoi (fixed x)
352 {
353  return (x >> 16) + ((x & 0x8000) >> 15);
354 }
355 
365 static inline fixed fixcos (fixed x)
366 {
367  return _cos_tbl[((x + 0x4000) >> 15) & 0x1FF];
368 }
369 
379 static inline fixed fixsin (fixed x)
380 {
381  return _cos_tbl[((x - 0x400000 + 0x4000) >> 15) & 0x1FF];
382 }
383 
393 static inline fixed fixtan (fixed x)
394 {
395  return _tan_tbl[((x + 0x4000) >> 15) & 0xFF];
396 }
397 
411 static inline fixed fixacos (fixed x)
412 {
413  if ((x < -65536) || (x > 65536)) {
414  errno = EDOM;
415  return 0;
416  }
417 
418  return _acos_tbl[(x+65536+127)>>8];
419 }
420 
434 static inline fixed fixasin (fixed x)
435 {
436  if ((x < -65536) || (x > 65536)) {
437  errno = EDOM;
438  return 0;
439  }
440 
441  return 0x00400000 - _acos_tbl[(x+65536+127)>>8];
442 }
443 
451 #endif /* _MGHAVE_FIXED_MATH */
452 
453 /* Ends C function definitions when using C++ */
454 #ifdef __cplusplus
455 }
456 #endif
457 
458 #endif /* _MGUI_MGHAVE_FIXED_MATH_H */
459 
static fixed fixsin(fixed x)
Returns the sine of a fixed point.
Definition: fixedmath.h:379
MG_EXPORT fixed fixmul(fixed x, fixed y)
Returns the product of two fixed point values.
static fixed fixcos(fixed x)
Returns the cosine of a fixed point.
Definition: fixedmath.h:365
static fixed fixasin(fixed x)
Calculates and returns the arc sine of a fixed point.
Definition: fixedmath.h:434
MG_EXPORT fixed fixatan(fixed x)
Calculates the arc tangent of a fixed point value.
static fixed fixadd(fixed x, fixed y)
Returns the sum of two fixed point values.
Definition: fixedmath.h:217
static int fixtoi(fixed x)
Converts an fixed point value to an integer.
Definition: fixedmath.h:351
static double fixtof(fixed x)
Converts a fixed point value to a float point value.
Definition: fixedmath.h:197
MG_EXPORT fixed fixdiv(fixed x, fixed y)
Returns the quotient of two fixed point values.
static fixed ftofix(double x)
Converts a float point value to a fixed point value.
Definition: fixedmath.h:173
static fixed fixacos(fixed x)
Calculates and returns the arc cosine of a fixed point.
Definition: fixedmath.h:411
static fixed itofix(int x)
Converts an integer to a fixed point value.
Definition: fixedmath.h:338
long fixed
Data type of fixed point.
Definition: common.h:1025
static fixed fixsub(fixed x, fixed y)
Subtract a fixed point value from another.
Definition: fixedmath.h:254
static int fixceil(fixed x)
Rounds a fixed point value to the nearest integer.
Definition: fixedmath.h:316
MG_EXPORT fixed fixhypot(fixed x, fixed y)
Returns the Euclidean distance from the origin.
static fixed fixtan(fixed x)
Returns the tangent of a fixed point.
Definition: fixedmath.h:393
MG_EXPORT fixed fixatan2(fixed y, fixed x)
Calculates the arc tangent of two fixed point variables.
MG_EXPORT fixed fixsqrt(fixed x)
Returns the non-negative square root of a fixed point value.