ua_architecture_definitions.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  7. * Copyright 2018 (c) Jose Cabral, fortiss GmbH
  8. */
  9. #ifndef ARCH_UA_ARCHITECTURE_DEFINITIONS_H_
  10. #define ARCH_UA_ARCHITECTURE_DEFINITIONS_H_
  11. /**
  12. * C99 Definitions
  13. * --------------- */
  14. #include <string.h>
  15. #include <stddef.h>
  16. /* Include stdint.h and stdbool.h or workaround for older Visual Studios */
  17. #if !defined(_MSC_VER) || _MSC_VER >= 1600
  18. # include <stdint.h>
  19. # include <stdbool.h> /* C99 Boolean */
  20. #else
  21. # include "ms_stdint.h"
  22. # if !defined(__bool_true_false_are_defined)
  23. # define bool unsigned char
  24. # define true 1
  25. # define false 0
  26. # define __bool_true_false_are_defined
  27. # endif
  28. #endif
  29. /**
  30. * Assertions
  31. * ----------
  32. * The assert macro is disabled by defining NDEBUG. It is often forgotten to
  33. * include -DNDEBUG in the compiler flags when using the single-file release. So
  34. * we make assertions dependent on the UA_DEBUG definition handled by CMake. */
  35. #ifdef UA_DEBUG
  36. # include <assert.h>
  37. # define UA_assert(ignore) assert(ignore)
  38. #else
  39. # define UA_assert(ignore)
  40. #endif
  41. /* Outputs an error message at compile time if the assert fails.
  42. * Example usage:
  43. * UA_STATIC_ASSERT(sizeof(long)==7, use_another_compiler_luke)
  44. * See: https://stackoverflow.com/a/4815532/869402 */
  45. #if defined(__cplusplus) && __cplusplus >= 201103L /* C++11 or above */
  46. # define UA_STATIC_ASSERT(cond,msg) static_assert(cond, #msg)
  47. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L /* C11 or above */
  48. # define UA_STATIC_ASSERT(cond,msg) _Static_assert(cond, #msg)
  49. #elif defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER) /* GCC, Clang, MSC */
  50. # define UA_CTASTR2(pre,post) pre ## post
  51. # define UA_CTASTR(pre,post) UA_CTASTR2(pre,post)
  52. # ifndef __COUNTER__ /* PPC GCC fix */
  53. # define __COUNTER__ __LINE__
  54. # endif
  55. # define UA_STATIC_ASSERT(cond,msg) \
  56. typedef struct { \
  57. int UA_CTASTR(static_assertion_failed_,msg) : !!(cond); \
  58. } UA_CTASTR(static_assertion_failed_,__COUNTER__)
  59. #else /* Everybody else */
  60. # define UA_STATIC_ASSERT(cond,msg) typedef char static_assertion_##msg[(cond)?1:-1]
  61. #endif
  62. #if defined(_WIN32) && defined(UA_DYNAMIC_LINKING)
  63. # ifdef UA_DYNAMIC_LINKING_EXPORT /* export dll */
  64. # ifdef __GNUC__
  65. # define UA_EXPORT __attribute__ ((dllexport))
  66. # else
  67. # define UA_EXPORT __declspec(dllexport)
  68. # endif
  69. # else /* import dll */
  70. # ifdef __GNUC__
  71. # define UA_EXPORT __attribute__ ((dllimport))
  72. # else
  73. # define UA_EXPORT __declspec(dllimport)
  74. # endif
  75. # endif
  76. #else /* non win32 */
  77. # if __GNUC__ || __clang__
  78. # define UA_EXPORT __attribute__ ((visibility ("default")))
  79. # endif
  80. #endif
  81. #ifndef UA_EXPORT
  82. # define UA_EXPORT /* fallback to default */
  83. #endif
  84. /**
  85. * Inline Functions
  86. * ---------------- */
  87. #ifdef _MSC_VER
  88. # define UA_INLINE __inline
  89. #else
  90. # define UA_INLINE inline
  91. #endif
  92. /**
  93. * Non-aliasing pointers
  94. * -------------------- */
  95. #ifdef _MSC_VER
  96. # define UA_RESTRICT __restrict
  97. #elif defined(__GNUC__)
  98. # define UA_RESTRICT __restrict__
  99. #else
  100. # define UA_RESTRICT restrict
  101. #endif
  102. /**
  103. * Function attributes
  104. * ------------------- */
  105. #if defined(__GNUC__) || defined(__clang__)
  106. # define UA_FUNC_ATTR_MALLOC __attribute__((malloc))
  107. # define UA_FUNC_ATTR_PURE __attribute__ ((pure))
  108. # define UA_FUNC_ATTR_CONST __attribute__((const))
  109. # define UA_FUNC_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
  110. # define UA_FORMAT(X,Y) __attribute__ ((format (printf, X, Y)))
  111. #else
  112. # define UA_FUNC_ATTR_MALLOC
  113. # define UA_FUNC_ATTR_PURE
  114. # define UA_FUNC_ATTR_CONST
  115. # define UA_FUNC_ATTR_WARN_UNUSED_RESULT
  116. # define UA_FORMAT(X,Y)
  117. #endif
  118. #if defined(__GNUC__) || defined(__clang__)
  119. # define UA_DEPRECATED __attribute__((deprecated))
  120. #elif defined(_MSC_VER)
  121. # define UA_DEPRECATED __declspec(deprecated)
  122. #else
  123. # define UA_DEPRECATED
  124. #endif
  125. /**
  126. * Detect Endianness and IEEE 754 floating point
  127. * ---------------------------------------------
  128. * Integers and floating point numbers are transmitted in little-endian (IEEE
  129. * 754 for floating point) encoding. If the target architecture uses the same
  130. * format, numeral datatypes can be memcpy'd (overlayed) on the network buffer.
  131. * Otherwise, a slow default encoding routine is used that works for every
  132. * architecture.
  133. *
  134. * Integer Endianness
  135. * ^^^^^^^^^^^^^^^^^^
  136. * The definition ``UA_LITTLE_ENDIAN`` is true when the integer representation
  137. * of the target architecture is little-endian. */
  138. #if defined(_WIN32)
  139. # define UA_LITTLE_ENDIAN 1
  140. #elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
  141. # define UA_LITTLE_ENDIAN 1
  142. #elif (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  143. (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  144. # define UA_LITTLE_ENDIAN 1
  145. #elif defined(__linux__) /* Linux (including Android) */
  146. # include <endian.h>
  147. # if __BYTE_ORDER == __LITTLE_ENDIAN
  148. # define UA_LITTLE_ENDIAN 1
  149. # endif
  150. #elif defined(__OpenBSD__) /* OpenBSD */
  151. # include <sys/endian.h>
  152. # if BYTE_ORDER == LITTLE_ENDIAN
  153. # define UA_LITTLE_ENDIAN 1
  154. # endif
  155. #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) /* Other BSD */
  156. # include <sys/endian.h>
  157. # if _BYTE_ORDER == _LITTLE_ENDIAN
  158. # define UA_LITTLE_ENDIAN 1
  159. # endif
  160. #elif defined(__APPLE__) /* Apple (MacOS, iOS) */
  161. # include <libkern/OSByteOrder.h>
  162. # if defined(__LITTLE_ENDIAN__)
  163. # define UA_LITTLE_ENDIAN 1
  164. # endif
  165. #elif defined(__QNX__) || defined(__QNXNTO__) /* QNX */
  166. # include <gulliver.h>
  167. # if defined(__LITTLEENDIAN__)
  168. # define UA_LITTLE_ENDIAN 1
  169. # endif
  170. #elif defined(_OS9000) /* OS-9 */
  171. # if defined(_LIL_END)
  172. # define UA_LITTLE_ENDIAN 1
  173. # endif
  174. #endif
  175. #ifndef UA_LITTLE_ENDIAN
  176. # define UA_LITTLE_ENDIAN 0
  177. #endif
  178. /* Can the integers be memcpy'd onto the network buffer? Add additional checks
  179. * here. Some platforms (e.g. QNX) have sizeof(bool) > 1. Manually disable
  180. * overlayed integer encoding if that is the case. */
  181. #if (UA_LITTLE_ENDIAN == 1)
  182. UA_STATIC_ASSERT(sizeof(bool) == 1, cannot_overlay_integers_with_large_bool);
  183. # define UA_BINARY_OVERLAYABLE_INTEGER 1
  184. #else
  185. # define UA_BINARY_OVERLAYABLE_INTEGER 0
  186. #endif
  187. /**
  188. * Float Endianness
  189. * ^^^^^^^^^^^^^^^^
  190. * The definition ``UA_FLOAT_IEEE754`` is set to true when the floating point
  191. * number representation of the target architecture is IEEE 754. The definition
  192. * ``UA_FLOAT_LITTLE_ENDIAN`` is set to true when the floating point number
  193. * representation is in little-endian encoding. */
  194. #if defined(_WIN32)
  195. # define UA_FLOAT_IEEE754 1
  196. #elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
  197. defined(__ia64__) || defined(__powerpc__) || defined(__sparc__) || \
  198. defined(__arm__)
  199. # define UA_FLOAT_IEEE754 1
  200. #elif defined(__STDC_IEC_559__)
  201. # define UA_FLOAT_IEEE754 1
  202. #else
  203. # define UA_FLOAT_IEEE754 0
  204. #endif
  205. /* Wikipedia says (https://en.wikipedia.org/wiki/Endianness): Although the
  206. * ubiquitous x86 processors of today use little-endian storage for all types of
  207. * data (integer, floating point, BCD), there are a number of hardware
  208. * architectures where floating-point numbers are represented in big-endian form
  209. * while integers are represented in little-endian form. */
  210. #if defined(_WIN32)
  211. # define UA_FLOAT_LITTLE_ENDIAN 1
  212. #elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
  213. # define UA_FLOAT_LITTLE_ENDIAN 1
  214. #elif defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  215. (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__) /* Defined only in GCC */
  216. # define UA_FLOAT_LITTLE_ENDIAN 1
  217. #elif defined(__FLOAT_WORD_ORDER) && defined(__LITTLE_ENDIAN) && \
  218. (__FLOAT_WORD_ORDER == __LITTLE_ENDIAN) /* Defined only in GCC */
  219. # define UA_FLOAT_LITTLE_ENDIAN 1
  220. #endif
  221. #ifndef UA_FLOAT_LITTLE_ENDIAN
  222. # define UA_FLOAT_LITTLE_ENDIAN 0
  223. #endif
  224. /* Only if the floating points are litle-endian **and** in IEEE 754 format can
  225. * we memcpy directly onto the network buffer. */
  226. #if (UA_FLOAT_IEEE754 == 1) && (UA_FLOAT_LITTLE_ENDIAN == 1)
  227. # define UA_BINARY_OVERLAYABLE_FLOAT 1
  228. #else
  229. # define UA_BINARY_OVERLAYABLE_FLOAT 0
  230. #endif
  231. #endif /* ARCH_UA_ARCHITECTURE_DEFINITIONS_H_ */