ua_architecture_definitions.h 8.9 KB

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