defs.hpp 11 KB

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