defs.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**************************************************************************/
  2. /* defs.hpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include <cstddef>
  32. #include <cstdint>
  33. #include <type_traits>
  34. #include <utility>
  35. namespace godot {
  36. #if !defined(GDE_EXPORT)
  37. #if defined(_WIN32)
  38. #define GDE_EXPORT __declspec(dllexport)
  39. #elif defined(__GNUC__)
  40. #define GDE_EXPORT __attribute__((visibility("default")))
  41. #else
  42. #define GDE_EXPORT
  43. #endif
  44. #endif
  45. // Turn argument to string constant:
  46. // https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html#Stringizing
  47. #ifndef _STR
  48. #define _STR(m_x) #m_x
  49. #define _MKSTR(m_x) _STR(m_x)
  50. #endif
  51. // Should always inline no matter what.
  52. #ifndef _ALWAYS_INLINE_
  53. #if defined(__GNUC__)
  54. #define _ALWAYS_INLINE_ __attribute__((always_inline)) inline
  55. #elif defined(_MSC_VER)
  56. #define _ALWAYS_INLINE_ __forceinline
  57. #else
  58. #define _ALWAYS_INLINE_ inline
  59. #endif
  60. #endif
  61. // Should always inline, except in dev builds because it makes debugging harder,
  62. // or `size_enabled` builds where inlining is actively avoided.
  63. #ifndef _FORCE_INLINE_
  64. #if defined(DEV_ENABLED) || defined(SIZE_EXTRA)
  65. #define _FORCE_INLINE_ inline
  66. #else
  67. #define _FORCE_INLINE_ _ALWAYS_INLINE_
  68. #endif
  69. #endif
  70. // Should never inline.
  71. #ifndef _NO_INLINE_
  72. #if defined(__GNUC__)
  73. #define _NO_INLINE_ __attribute__((noinline))
  74. #elif defined(_MSC_VER)
  75. #define _NO_INLINE_ __declspec(noinline)
  76. #else
  77. #define _NO_INLINE_
  78. #endif
  79. #endif
  80. // In some cases [[nodiscard]] will get false positives,
  81. // we can prevent the warning in specific cases by preceding the call with a cast.
  82. #ifndef _ALLOW_DISCARD_
  83. #define _ALLOW_DISCARD_ (void)
  84. #endif
  85. // Windows badly defines a lot of stuff we'll never use. Undefine it.
  86. #ifdef _WIN32
  87. #undef min // override standard definition
  88. #undef max // override standard definition
  89. #undef ERROR // override (really stupid) wingdi.h standard definition
  90. #undef DELETE // override (another really stupid) winnt.h standard definition
  91. #undef MessageBox // override winuser.h standard definition
  92. #undef Error
  93. #undef OK
  94. #undef CONNECT_DEFERRED // override from Windows SDK, clashes with Object enum
  95. #undef MemoryBarrier
  96. #undef MONO_FONT
  97. #endif
  98. // Make room for our constexpr's below by overriding potential system-specific macros.
  99. #undef SIGN
  100. #undef MIN
  101. #undef MAX
  102. #undef CLAMP
  103. template <typename T>
  104. constexpr const T SIGN(const T m_v) {
  105. return m_v > 0 ? +1.0f : (m_v < 0 ? -1.0f : 0.0f);
  106. }
  107. template <typename T, typename T2>
  108. constexpr auto MIN(const T m_a, const T2 m_b) {
  109. return m_a < m_b ? m_a : m_b;
  110. }
  111. template <typename T, typename T2>
  112. constexpr auto MAX(const T m_a, const T2 m_b) {
  113. return m_a > m_b ? m_a : m_b;
  114. }
  115. template <typename T, typename T2, typename T3>
  116. constexpr auto CLAMP(const T m_a, const T2 m_min, const T3 m_max) {
  117. return m_a < m_min ? m_min : (m_a > m_max ? m_max : m_a);
  118. }
  119. // Generic swap template.
  120. #ifndef SWAP
  121. #define SWAP(m_x, m_y) std::swap((m_x), (m_y))
  122. #endif // SWAP
  123. /* Functions to handle powers of 2 and shifting. */
  124. // Returns `true` if a positive integer is a power of 2, `false` otherwise.
  125. template <typename T>
  126. inline bool is_power_of_2(const T x) {
  127. return x && ((x & (x - 1)) == 0);
  128. }
  129. // Function to find the next power of 2 to an integer.
  130. static _FORCE_INLINE_ unsigned int next_power_of_2(unsigned int x) {
  131. if (x == 0) {
  132. return 0;
  133. }
  134. --x;
  135. x |= x >> 1;
  136. x |= x >> 2;
  137. x |= x >> 4;
  138. x |= x >> 8;
  139. x |= x >> 16;
  140. return ++x;
  141. }
  142. // Function to find the previous power of 2 to an integer.
  143. static _FORCE_INLINE_ unsigned int previous_power_of_2(unsigned int x) {
  144. x |= x >> 1;
  145. x |= x >> 2;
  146. x |= x >> 4;
  147. x |= x >> 8;
  148. x |= x >> 16;
  149. return x - (x >> 1);
  150. }
  151. // Function to find the closest power of 2 to an integer.
  152. static _FORCE_INLINE_ unsigned int closest_power_of_2(unsigned int x) {
  153. unsigned int nx = next_power_of_2(x);
  154. unsigned int px = previous_power_of_2(x);
  155. return (nx - x) > (x - px) ? px : nx;
  156. }
  157. // Get a shift value from a power of 2.
  158. static inline int get_shift_from_power_of_2(unsigned int p_bits) {
  159. for (unsigned int i = 0; i < 32; i++) {
  160. if (p_bits == (unsigned int)(1 << i)) {
  161. return i;
  162. }
  163. }
  164. return -1;
  165. }
  166. template <typename T>
  167. static _FORCE_INLINE_ T nearest_power_of_2_templated(T x) {
  168. --x;
  169. // The number of operations on x is the base two logarithm
  170. // of the number of bits in the type. Add three to account
  171. // for sizeof(T) being in bytes.
  172. size_t num = get_shift_from_power_of_2(sizeof(T)) + 3;
  173. // If the compiler is smart, it unrolls this loop.
  174. // If it's dumb, this is a bit slow.
  175. for (size_t i = 0; i < num; i++) {
  176. x |= x >> (1 << i);
  177. }
  178. return ++x;
  179. }
  180. // Function to find the nearest (bigger) power of 2 to an integer.
  181. static inline unsigned int nearest_shift(unsigned int p_number) {
  182. for (int i = 30; i >= 0; i--) {
  183. if (p_number & (1 << i)) {
  184. return i + 1;
  185. }
  186. }
  187. return 0;
  188. }
  189. // constexpr function to find the floored log2 of a number
  190. template <typename T>
  191. constexpr T floor_log2(T x) {
  192. return x < 2 ? x : 1 + floor_log2(x >> 1);
  193. }
  194. // Get the number of bits needed to represent the number.
  195. // IE, if you pass in 8, you will get 4.
  196. // If you want to know how many bits are needed to store 8 values however, pass in (8 - 1).
  197. template <typename T>
  198. constexpr T get_num_bits(T x) {
  199. return floor_log2(x);
  200. }
  201. // Swap 16, 32 and 64 bits value for endianness.
  202. #if defined(__GNUC__)
  203. #define BSWAP16(x) __builtin_bswap16(x)
  204. #define BSWAP32(x) __builtin_bswap32(x)
  205. #define BSWAP64(x) __builtin_bswap64(x)
  206. #elif defined(_MSC_VER)
  207. #define BSWAP16(x) _byteswap_ushort(x)
  208. #define BSWAP32(x) _byteswap_ulong(x)
  209. #define BSWAP64(x) _byteswap_uint64(x)
  210. #else
  211. static inline uint16_t BSWAP16(uint16_t x) {
  212. return (x >> 8) | (x << 8);
  213. }
  214. static inline uint32_t BSWAP32(uint32_t x) {
  215. return ((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24));
  216. }
  217. static inline uint64_t BSWAP64(uint64_t x) {
  218. x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
  219. x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
  220. x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
  221. return x;
  222. }
  223. #endif
  224. // Generic comparator used in Map, List, etc.
  225. template <typename T>
  226. struct Comparator {
  227. _ALWAYS_INLINE_ bool operator()(const T &p_a, const T &p_b) const { return (p_a < p_b); }
  228. };
  229. // Global lock macro, relies on the static Mutex::_global_mutex.
  230. void _global_lock();
  231. void _global_unlock();
  232. struct _GlobalLock {
  233. _GlobalLock() { _global_lock(); }
  234. ~_GlobalLock() { _global_unlock(); }
  235. };
  236. #define GLOBAL_LOCK_FUNCTION _GlobalLock _global_lock_;
  237. #if defined(__GNUC__)
  238. #define likely(x) __builtin_expect(!!(x), 1)
  239. #define unlikely(x) __builtin_expect(!!(x), 0)
  240. #else
  241. #define likely(x) x
  242. #define unlikely(x) x
  243. #endif
  244. #if defined(__GNUC__)
  245. #define _PRINTF_FORMAT_ATTRIBUTE_2_0 __attribute__((format(printf, 2, 0)))
  246. #define _PRINTF_FORMAT_ATTRIBUTE_2_3 __attribute__((format(printf, 2, 3)))
  247. #else
  248. #define _PRINTF_FORMAT_ATTRIBUTE_2_0
  249. #define _PRINTF_FORMAT_ATTRIBUTE_2_3
  250. #endif
  251. // This is needed due to a strange OpenGL API that expects a pointer
  252. // type for an argument that is actually an offset.
  253. #define CAST_INT_TO_UCHAR_PTR(ptr) ((uint8_t *)(uintptr_t)(ptr))
  254. // Home-made index sequence trick, so it can be used everywhere without the costly include of std::tuple.
  255. // https://stackoverflow.com/questions/15014096/c-index-of-type-during-variadic-template-expansion
  256. template <size_t... Is>
  257. struct IndexSequence {};
  258. template <size_t N, size_t... Is>
  259. struct BuildIndexSequence : BuildIndexSequence<N - 1, N - 1, Is...> {};
  260. template <size_t... Is>
  261. struct BuildIndexSequence<0, Is...> : IndexSequence<Is...> {};
  262. // Limit the depth of recursive algorithms when dealing with Array/Dictionary
  263. #define MAX_RECURSION 100
  264. #ifdef DEBUG_ENABLED
  265. #define DEBUG_METHODS_ENABLED
  266. #endif
  267. // Macro GD_IS_DEFINED() allows to check if a macro is defined. It needs to be defined to anything (say 1) to work.
  268. #define __GDARG_PLACEHOLDER_1 false,
  269. #define __gd_take_second_arg(__ignored, val, ...) val
  270. #define ____gd_is_defined(arg1_or_junk) __gd_take_second_arg(arg1_or_junk true, false)
  271. #define ___gd_is_defined(val) ____gd_is_defined(__GDARG_PLACEHOLDER_##val)
  272. #define GD_IS_DEFINED(x) ___gd_is_defined(x)
  273. // Whether the default value of a type is just all-0 bytes.
  274. // This can most commonly be exploited by using memset for these types instead of loop-construct.
  275. // Trivially constructible types are also zero-constructible.
  276. template <typename T>
  277. struct is_zero_constructible : std::is_trivially_constructible<T> {};
  278. template <typename T>
  279. struct is_zero_constructible<const T> : is_zero_constructible<T> {};
  280. template <typename T>
  281. struct is_zero_constructible<volatile T> : is_zero_constructible<T> {};
  282. template <typename T>
  283. struct is_zero_constructible<const volatile T> : is_zero_constructible<T> {};
  284. template <typename T>
  285. inline constexpr bool is_zero_constructible_v = is_zero_constructible<T>::value;
  286. } //namespace godot