ua_architecture_definitions.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /* Atomic Operations
  251. * -----------------
  252. * Atomic operations that synchronize across processor cores (for
  253. * multithreading). Only the inline-functions defined next are used. Replace
  254. * with architecture-specific operations if necessary. */
  255. #ifndef UA_ENABLE_MULTITHREADING
  256. # define UA_atomic_sync()
  257. #else
  258. # ifdef _MSC_VER /* Visual Studio */
  259. # define UA_atomic_sync() _ReadWriteBarrier()
  260. # else /* GCC/Clang */
  261. # define UA_atomic_sync() __sync_synchronize()
  262. # endif
  263. #endif
  264. static UA_INLINE void *
  265. UA_atomic_xchg(void * volatile * addr, void *newptr) {
  266. #ifndef UA_ENABLE_MULTITHREADING
  267. void *old = *addr;
  268. *addr = newptr;
  269. return old;
  270. #else
  271. # ifdef _MSC_VER /* Visual Studio */
  272. return _InterlockedExchangePointer(addr, newptr);
  273. # else /* GCC/Clang */
  274. return __sync_lock_test_and_set(addr, newptr);
  275. # endif
  276. #endif
  277. }
  278. static UA_INLINE void *
  279. UA_atomic_cmpxchg(void * volatile * addr, void *expected, void *newptr) {
  280. #ifndef UA_ENABLE_MULTITHREADING
  281. void *old = *addr;
  282. if(old == expected) {
  283. *addr = newptr;
  284. }
  285. return old;
  286. #else
  287. # ifdef _MSC_VER /* Visual Studio */
  288. return _InterlockedCompareExchangePointer(addr, expected, newptr);
  289. # else /* GCC/Clang */
  290. return __sync_val_compare_and_swap(addr, expected, newptr);
  291. # endif
  292. #endif
  293. }
  294. static UA_INLINE uint32_t
  295. UA_atomic_addUInt32(volatile uint32_t *addr, uint32_t increase) {
  296. #ifndef UA_ENABLE_MULTITHREADING
  297. *addr += increase;
  298. return *addr;
  299. #else
  300. # ifdef _MSC_VER /* Visual Studio */
  301. return _InterlockedExchangeAdd(addr, increase) + increase;
  302. # else /* GCC/Clang */
  303. return __sync_add_and_fetch(addr, increase);
  304. # endif
  305. #endif
  306. }
  307. static UA_INLINE size_t
  308. UA_atomic_addSize(volatile size_t *addr, size_t increase) {
  309. #ifndef UA_ENABLE_MULTITHREADING
  310. *addr += increase;
  311. return *addr;
  312. #else
  313. # ifdef _MSC_VER /* Visual Studio */
  314. return _InterlockedExchangeAdd(addr, increase) + increase;
  315. # else /* GCC/Clang */
  316. return __sync_add_and_fetch(addr, increase);
  317. # endif
  318. #endif
  319. }
  320. static UA_INLINE uint32_t
  321. UA_atomic_subUInt32(volatile uint32_t *addr, uint32_t decrease) {
  322. #ifndef UA_ENABLE_MULTITHREADING
  323. *addr -= decrease;
  324. return *addr;
  325. #else
  326. # ifdef _MSC_VER /* Visual Studio */
  327. return _InterlockedExchangeSub(addr, decrease) - decrease;
  328. # else /* GCC/Clang */
  329. return __sync_sub_and_fetch(addr, decrease);
  330. # endif
  331. #endif
  332. }
  333. static UA_INLINE size_t
  334. UA_atomic_subSize(volatile size_t *addr, size_t decrease) {
  335. #ifndef UA_ENABLE_MULTITHREADING
  336. *addr -= decrease;
  337. return *addr;
  338. #else
  339. # ifdef _MSC_VER /* Visual Studio */
  340. return _InterlockedExchangeSub(addr, decrease) - decrease;
  341. # else /* GCC/Clang */
  342. return __sync_sub_and_fetch(addr, decrease);
  343. # endif
  344. #endif
  345. }
  346. #endif /* ARCH_UA_ARCHITECTURE_DEFINITIONS_H_ */