ua_architecture_definitions.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * Detect Endianness and IEEE 754 floating point
  130. * ---------------------------------------------
  131. * Integers and floating point numbers are transmitted in little-endian (IEEE
  132. * 754 for floating point) encoding. If the target architecture uses the same
  133. * format, numeral datatypes can be memcpy'd (overlayed) on the network buffer.
  134. * Otherwise, a slow default encoding routine is used that works for every
  135. * architecture.
  136. *
  137. * Integer Endianness
  138. * ^^^^^^^^^^^^^^^^^^
  139. * The definition ``UA_LITTLE_ENDIAN`` is true when the integer representation
  140. * of the target architecture is little-endian. */
  141. #if defined(_WIN32)
  142. # define UA_LITTLE_ENDIAN 1
  143. #elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
  144. # define UA_LITTLE_ENDIAN 1
  145. #elif (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  146. (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  147. # define UA_LITTLE_ENDIAN 1
  148. #elif defined(__linux__) /* Linux (including Android) */
  149. # include <endian.h>
  150. # if __BYTE_ORDER == __LITTLE_ENDIAN
  151. # define UA_LITTLE_ENDIAN 1
  152. # endif
  153. #elif defined(__OpenBSD__) /* OpenBSD */
  154. # include <sys/endian.h>
  155. # if BYTE_ORDER == LITTLE_ENDIAN
  156. # define UA_LITTLE_ENDIAN 1
  157. # endif
  158. #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) /* Other BSD */
  159. # include <sys/endian.h>
  160. # if _BYTE_ORDER == _LITTLE_ENDIAN
  161. # define UA_LITTLE_ENDIAN 1
  162. # endif
  163. #elif defined(__APPLE__) /* Apple (MacOS, iOS) */
  164. # include <libkern/OSByteOrder.h>
  165. # if defined(__LITTLE_ENDIAN__)
  166. # define UA_LITTLE_ENDIAN 1
  167. # endif
  168. #elif defined(__QNX__) || defined(__QNXNTO__) /* QNX */
  169. # include <gulliver.h>
  170. # if defined(__LITTLEENDIAN__)
  171. # define UA_LITTLE_ENDIAN 1
  172. # endif
  173. #elif defined(_OS9000) /* OS-9 */
  174. # if defined(_LIL_END)
  175. # define UA_LITTLE_ENDIAN 1
  176. # endif
  177. #endif
  178. #ifndef UA_LITTLE_ENDIAN
  179. # define UA_LITTLE_ENDIAN 0
  180. #endif
  181. /* Can the integers be memcpy'd onto the network buffer? Add additional checks
  182. * here. Some platforms (e.g. QNX) have sizeof(bool) > 1. Manually disable
  183. * overlayed integer encoding if that is the case. */
  184. #if (UA_LITTLE_ENDIAN == 1)
  185. UA_STATIC_ASSERT(sizeof(bool) == 1, cannot_overlay_integers_with_large_bool);
  186. # define UA_BINARY_OVERLAYABLE_INTEGER 1
  187. #else
  188. # define UA_BINARY_OVERLAYABLE_INTEGER 0
  189. #endif
  190. /**
  191. * Float Endianness
  192. * ^^^^^^^^^^^^^^^^
  193. * The definition ``UA_FLOAT_IEEE754`` is set to true when the floating point
  194. * number representation of the target architecture is IEEE 754. The definition
  195. * ``UA_FLOAT_LITTLE_ENDIAN`` is set to true when the floating point number
  196. * representation is in little-endian encoding. */
  197. #if defined(_WIN32)
  198. # define UA_FLOAT_IEEE754 1
  199. #elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
  200. defined(__ia64__) || defined(__powerpc__) || defined(__sparc__) || \
  201. defined(__arm__)
  202. # define UA_FLOAT_IEEE754 1
  203. #elif defined(__STDC_IEC_559__)
  204. # define UA_FLOAT_IEEE754 1
  205. #else
  206. # define UA_FLOAT_IEEE754 0
  207. #endif
  208. /* Wikipedia says (https://en.wikipedia.org/wiki/Endianness): Although the
  209. * ubiquitous x86 processors of today use little-endian storage for all types of
  210. * data (integer, floating point, BCD), there are a number of hardware
  211. * architectures where floating-point numbers are represented in big-endian form
  212. * while integers are represented in little-endian form. */
  213. #if defined(_WIN32)
  214. # define UA_FLOAT_LITTLE_ENDIAN 1
  215. #elif defined(__i386__) || defined(__x86_64__) || defined(__amd64__)
  216. # define UA_FLOAT_LITTLE_ENDIAN 1
  217. #elif defined(__FLOAT_WORD_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  218. (__FLOAT_WORD_ORDER__ == __ORDER_LITTLE_ENDIAN__) /* Defined only in GCC */
  219. # define UA_FLOAT_LITTLE_ENDIAN 1
  220. #elif defined(__FLOAT_WORD_ORDER) && defined(__LITTLE_ENDIAN) && \
  221. (__FLOAT_WORD_ORDER == __LITTLE_ENDIAN) /* Defined only in GCC */
  222. # define UA_FLOAT_LITTLE_ENDIAN 1
  223. #endif
  224. #ifndef UA_FLOAT_LITTLE_ENDIAN
  225. # define UA_FLOAT_LITTLE_ENDIAN 0
  226. #endif
  227. /* Only if the floating points are litle-endian **and** in IEEE 754 format can
  228. * we memcpy directly onto the network buffer. */
  229. #if (UA_FLOAT_IEEE754 == 1) && (UA_FLOAT_LITTLE_ENDIAN == 1)
  230. # define UA_BINARY_OVERLAYABLE_FLOAT 1
  231. #else
  232. # define UA_BINARY_OVERLAYABLE_FLOAT 0
  233. #endif
  234. #endif /* ARCH_UA_ARCHITECTURE_DEFINITIONS_H_ */