sbgECom Library  4.0.1987-stable
C library to interface SBG Systems IMU/AHRS/INS
sbgDefines.h
Go to the documentation of this file.
1 
33 #ifndef SBG_DEFINES_H
34 #define SBG_DEFINES_H
35 
36 // Standard headers
37 #include <assert.h>
38 #include <errno.h>
39 #include <float.h>
40 #include <inttypes.h>
41 #include <limits.h>
42 #include <math.h>
43 #include <stdarg.h>
44 #include <stdbool.h>
45 #include <stddef.h>
46 #include <stdlib.h>
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <time.h>
51 
52 // Local headers
53 #include "sbgConfig.h"
54 
55 //
56 // XXX If NDEBUG is defined, most libraries define assert() as ((void)0), which may
57 // cause "defined but not used" warnings. Redefine assert() in a way that safely
58 // prevents this warning, i.e. without triggering the expression side effects.
59 //
60 #ifdef NDEBUG
61 #undef assert
62 #define assert(expression) ((void)sizeof(expression))
63 #endif // NDEBUG
64 
68 #ifdef _MSC_VER
69  #ifdef SBG_COMMON_STATIC_USE
70  #define SBG_COMMON_LIB_API
71  #else
72  #ifdef SBG_COMMON_LIB_API_EXPORT
73  #define SBG_COMMON_LIB_API __declspec(dllexport)
74  #else
75  #define SBG_COMMON_LIB_API __declspec(dllimport)
76  #endif
77  #endif
78 #else
79  #define SBG_COMMON_LIB_API
80 #endif
81 
82 //----------------------------------------------------------------------//
83 //- Global definitions -//
84 //----------------------------------------------------------------------//
85 #ifndef SBG_DISABLE
86  #define SBG_DISABLE (0)
87 #endif
88 
89 #ifndef SBG_ENABLE
90  #define SBG_ENABLE (1)
91 #endif
92 
93 #ifndef FALSE
94  #define FALSE (0)
95 #endif
96 
97 #ifndef TRUE
98  #define TRUE (1)
99 #endif
100 
101 #ifndef NULL
102  #define NULL (0)
103 #endif
104 
105 #ifndef SBG_INVALID_HANDLE
106  #define SBG_INVALID_HANDLE (NULL)
107 #endif
108 
109 #ifndef SBG_ARRAY_SIZE
110  #define SBG_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
111 #endif
112 
113 #ifndef SBG_STRLEN
114  #define SBG_STRLEN(s) (sizeof(s) - 1)
115 #endif
116 
117 #ifndef SBG_QUOTE_NX
118  #define SBG_QUOTE_NX(x) #x
119 #endif
120 
121 #ifndef SBG_QUOTE
122  #define SBG_QUOTE(x) SBG_QUOTE_NX(x)
123 #endif
124 
125 #ifndef SBG_CONCAT_NX
126  #define SBG_CONCAT_NX(a, b) a ## b
127 #endif
128 
129 #ifndef SBG_CONCAT
130  #define SBG_CONCAT(a, b) SBG_CONCAT_NX(a, b)
131 #endif
132 
133 #ifndef SBG_UNPACK
134  #define SBG_UNPACK(...) __VA_ARGS__
135 #endif
136 
137 #ifndef SBG_CONTAINER_OF
138  #define SBG_CONTAINER_OF(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
139 #endif
140 
141 #ifndef SBG_LIKELY
142  #if defined(__GNUC__) || defined(__clang__)
143  #define SBG_LIKELY(expr) __builtin_expect((bool)(expr), true)
144  #else
145  #define SBG_LIKELY(expr) (expr)
146  #endif
147 #endif
148 
149 #ifndef SBG_UNLIKELY
150  #if defined(__GNUC__) || defined(__clang__)
151  #define SBG_UNLIKELY(expr) __builtin_expect((bool)(expr), false)
152  #else
153  #define SBG_UNLIKELY(expr) (expr)
154  #endif
155 #endif
156 
157 #ifndef SBG_CHECK_FORMAT
158  #if defined(__GNUC__) || defined(__clang__)
159  #define SBG_CHECK_FORMAT(style, format_index, va_args) __attribute__((format(style, format_index, va_args)))
160  #else
161  #define SBG_CHECK_FORMAT(style, format_index, va_args)
162  #endif
163 #endif
164 
171 #ifndef __SBG_TYPEOF
172  #ifdef __cplusplus
173  #define __SBG_TYPEOF(x) decltype(x)
174  #elif defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__)
175  #define __SBG_TYPEOF(x) typeof(x)
176  #endif
177 #endif
178 
179 #ifndef SBG_CONST_CAST_AA
180  #ifdef __SBG_TYPEOF
181  #define SBG_CONST_CAST_AA(x) ((const __SBG_TYPEOF((x)[0][0])(*)[SBG_ARRAY_SIZE((x)[0])])(x))
182  #else
183  #define SBG_CONST_CAST_AA(x) x
184  #endif
185 #endif
186 
187 #ifndef SBG_CONST_CAST_PP
188  #ifdef __SBG_TYPEOF
189  #define SBG_CONST_CAST_PP(x) ((const __SBG_TYPEOF(**(x))**)(x))
190  #else
191  #define SBG_CONST_CAST_PP(x) x
192  #endif
193 #endif
194 
198 #if !defined(__GNUC__) && !defined(__clang__)
199 #ifndef __BASE_FILE__
200  #define __BASE_FILE__ __FILE__
201 #endif
202 #endif
203 
204 #ifdef __cplusplus
205  #define SBG_DELETE(p) if (p){delete (p); (p) = NULL;}
206  #define SBG_DELETE_ARRAY(p) if (p){delete[] (p); (p) = NULL;}
207  #define SBG_FREE(p) if (p){free(p); (p) = NULL;}
208  #define SBG_FREE_ARRAY(p) if (p){free(p); (p) = NULL;}
209 #else
210  #define SBG_DELETE if (p){free(p); (p) = NULL;}
211  #define SBG_DELETE_ARRAY if (p){free(p); (p) = NULL;}
212  #define SBG_FREE(p) if (p){free(p); (p) = NULL;}
213  #define SBG_FREE_ARRAY(p) if (p){free(p); (p) = NULL;}
214 #endif
215 
216 //----------------------------------------------------------------------//
217 //- Compiler definitions -//
218 //----------------------------------------------------------------------//
219 
223 #ifndef SBG_INLINE
224  #if defined(_MSC_VER)
225  #define SBG_INLINE __inline
226  #else
227  #define SBG_INLINE static inline
228  #endif
229 #endif
230 
234 #ifndef SBG_UNUSED_PARAMETER
235  #define SBG_UNUSED_PARAMETER(x) (void)(x)
236 #endif
237 
244 #ifndef SBG_FALLTHROUGH
245  #if __cplusplus >= 201703L
246  #define SBG_FALLTHROUGH [[fallthrough]] /* introduced in C++ 17 */
247  #elif defined(__GNUC__) || defined(__clang__)
248  #if (__GNUC__ >= 7) || defined(__clang__)
249  #define SBG_FALLTHROUGH __attribute__((fallthrough))
250  #else
251  #define SBG_FALLTHROUGH ((void)0)
252  #endif
253  #else
254  #define SBG_FALLTHROUGH
255  #endif
256 #endif
257 
258 //----------------------------------------------------------------------//
259 //- Macro used to defined packed structures -//
260 //----------------------------------------------------------------------//
261 
268 #if defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__)
269  #define SBG_BEGIN_PACKED()
270 #elif defined(_MSC_VER)
271  #define SBG_BEGIN_PACKED() __pragma(pack(push, 1))
272 #else
273  #error you must byte-align these structures with the appropriate compiler directives
274 #endif
275 
281 #if defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__)
282  #define SBG_PACKED __attribute__((packed))
283 #elif defined(_MSC_VER)
284  #define SBG_PACKED
285 #else
286  #error you must byte-align these structures with the appropriate compiler directives
287 #endif
288 
294 #if defined(__GNUC__) || defined(__clang__) || defined(__TI_COMPILER_VERSION__)
295  #define SBG_END_PACKED()
296 #elif defined(_MSC_VER)
297  #define SBG_END_PACKED() __pragma(pack(pop))
298 #else
299  #error you must byte-align these structures with the appropriate compiler directives
300 #endif
301 
302 //----------------------------------------------------------------------//
303 //- Deprecation definitions -//
304 //----------------------------------------------------------------------//
305 
309 #if defined(__GNUC__) || defined(__clang__)
310  #define SBG_DEPRECATED(func) __attribute__((deprecated)) func
311 #elif defined(__TI_COMPILER_VERSION__)
312  #define SBG_DEPRECATED(func) func __attribute__((deprecated))
313 #elif defined(_MSC_VER)
314  #define SBG_DEPRECATED(func) __declspec(deprecated) func
315 #else
316  #define SBG_DEPRECATED(func) func
317 #endif
318 
322 #if defined(__GNUC__) || defined(__clang__)
323  #define SBG_DEPRECATED_MACRO(func) __pragma(deprecated(func))
324 #elif defined(_MSC_VER)
325 #define SBG_DEPRECATED_MACRO(func) __pragma(deprecated(func))
326 #else
327  #define SBG_DEPRECATED_MACRO(func) func
328 #endif
329 
333 #ifndef SBG_CONFIG_WARN_ABOUT_DEPRECATED_TYPES
334 #define SBG_CONFIG_WARN_ABOUT_DEPRECATED_TYPES (1)
335 #endif
336 
343 #if SBG_CONFIG_WARN_ABOUT_DEPRECATED_TYPES != 0
344  #if defined(__GNUC__) || defined(__clang__)
345  #define SBG_DEPRECATED_TYPEDEF(decl) decl __attribute__((deprecated))
346  #elif defined(_MSC_VER)
347  #define SBG_DEPRECATED_TYPEDEF(decl) __declspec(deprecated) decl
348  #else
349  #define SBG_DEPRECATED_TYPEDEF(decl) decl
350  #endif
351 #else
352  #define SBG_DEPRECATED_TYPEDEF(decl) decl
353 #endif
354 
355 //----------------------------------------------------------------------//
356 //- Basic maths definitions -//
357 //----------------------------------------------------------------------//
358 #ifndef SBG_PI
359  #define SBG_PI 3.14159265358979323846
360 #endif
361 
362 #ifndef SBG_PI_F
363  #define SBG_PI_F 3.14159265358979323846f
364 #endif
365 
372 #ifndef sbgAbs
373  #define sbgAbs(x) (((x) < 0) ? -(x) : (x))
374 #endif
375 
383 #ifndef sbgMax
384  #define sbgMax(a,b) (((a) > (b)) ? (a) : (b))
385 #endif
386 
394 #ifndef sbgMin
395  #define sbgMin(a,b) (((a) < (b)) ? (a) : (b))
396 #endif
397 
406 #ifndef sbgClamp
407  #define sbgClamp(value, minValue, maxValue) (((value) < (minValue))?(minValue): ((value) > (maxValue)?maxValue:value))
408 #endif
409 
417 #ifndef sbgDivCeil
418  #define sbgDivCeil(n, d) (((n) + (d) - 1) / (d))
419 #endif
420 
427 SBG_INLINE double sbgRadToDegd(double angle)
428 {
429  return angle * 180.0 / SBG_PI;
430 }
431 
438 SBG_INLINE double sbgDegToRadd(double angle)
439 {
440  return angle * SBG_PI / 180.0;
441 }
442 
449 SBG_INLINE float sbgRadToDegf(float angle)
450 {
451  return angle * 180.0f / SBG_PI_F;
452 }
453 
460 SBG_INLINE float sbgDegToRadf(float angle)
461 {
462  return angle * SBG_PI_F / 180.0f;
463 }
464 
472 SBG_INLINE bool sbgAlmostEqualsFloat(float leftValue, float rightValue)
473 {
474  //
475  // The IEEE standard says that any comparison operation involving a NAN must return false.
476  //
477  if (isnan(leftValue) || isnan(rightValue))
478  {
479  return false;
480  }
481 
482  //
483  // This method is not good enough and should be updated using DistanceBetweenSignAndMagnitudeNumbers methods
484  //
485  if (fabsf(leftValue - rightValue) < FLT_EPSILON)
486  {
487  return true;
488  }
489  else
490  {
491  return false;
492  }
493 }
494 
502 SBG_INLINE bool sbgAlmostEqualsDouble(double leftValue, double rightValue)
503 {
504  //
505  // The IEEE standard says that any comparison operation involving a NAN must return false.
506  //
507  if (isnan(leftValue) || isnan(rightValue))
508  {
509  return false;
510  }
511 
512  //
513  // This method is not good enough and should be updated using DistanceBetweenSignAndMagnitudeNumbers methods
514  //
515  if (fabs(leftValue - rightValue) < DBL_EPSILON)
516  {
517  return true;
518  }
519  else
520  {
521  return false;
522  }
523 }
524 
525 #endif // SBG_DEFINES_H
Header file used to configure the framework.
SBG_INLINE float sbgRadToDegf(float angle)
Definition: sbgDefines.h:449
#define SBG_INLINE
Definition: sbgDefines.h:227
SBG_INLINE double sbgDegToRadd(double angle)
Definition: sbgDefines.h:438
SBG_INLINE double sbgRadToDegd(double angle)
Definition: sbgDefines.h:427
SBG_INLINE float sbgDegToRadf(float angle)
Definition: sbgDefines.h:460
SBG_INLINE bool sbgAlmostEqualsFloat(float leftValue, float rightValue)
Definition: sbgDefines.h:472
SBG_INLINE bool sbgAlmostEqualsDouble(double leftValue, double rightValue)
Definition: sbgDefines.h:502