Core.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. // Determine platform
  5. #if defined(JPH_PLATFORM_BLUE)
  6. // Correct define already defined, this overrides everything else
  7. #elif defined(_WIN32) || defined(_WIN64)
  8. #include <winapifamily.h>
  9. #if WINAPI_FAMILY == WINAPI_FAMILY_APP
  10. #define JPH_PLATFORM_WINDOWS_UWP // Building for Universal Windows Platform
  11. #endif
  12. #define JPH_PLATFORM_WINDOWS
  13. #elif defined(__ANDROID__) // Android is linux too, so that's why we check it first
  14. #define JPH_PLATFORM_ANDROID
  15. #elif defined(__linux__)
  16. #define JPH_PLATFORM_LINUX
  17. #elif defined(__APPLE__)
  18. #include <TargetConditionals.h>
  19. #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE
  20. #define JPH_PLATFORM_MACOS
  21. #else
  22. #define JPH_PLATFORM_IOS
  23. #endif
  24. #endif
  25. // Platform helper macros
  26. #ifdef JPH_PLATFORM_ANDROID
  27. #define JPH_IF_NOT_ANDROID(x)
  28. #else
  29. #define JPH_IF_NOT_ANDROID(x) x
  30. #endif
  31. // Determine compiler
  32. #if defined(__clang__)
  33. #define JPH_COMPILER_CLANG
  34. #elif defined(__GNUC__)
  35. #define JPH_COMPILER_GCC
  36. #elif defined(_MSC_VER)
  37. #define JPH_COMPILER_MSVC
  38. #endif
  39. // Detect CPU architecture
  40. #if defined(__x86_64__) || defined(_M_X64)
  41. // X86 CPU architecture
  42. #define JPH_CPU_X64
  43. #define JPH_USE_SSE
  44. // Detect enabled instruction sets
  45. #if (defined(__F16C__) || defined(__AVX2__)) && !defined(JPH_USE_F16C)
  46. #define JPH_USE_F16C
  47. #endif
  48. #if (defined(__LZCNT__) || defined(__AVX2__)) && !defined(JPH_USE_LZCNT)
  49. #define JPH_USE_LZCNT
  50. #endif
  51. #if (defined(__BMI__) || defined(__AVX2__)) && !defined(JPH_USE_TZCNT)
  52. #define JPH_USE_TZCNT
  53. #endif
  54. #if (defined(__SSE4_1__) || defined(__AVX__)) && !defined(JPH_USE_SSE4_1)
  55. #define JPH_USE_SSE4_1
  56. #endif
  57. #if (defined(__SSE4_2__) || defined(__AVX__)) && !defined(JPH_USE_SSE4_2)
  58. #define JPH_USE_SSE4_2
  59. #endif
  60. #if defined(__AVX__) && !defined(JPH_USE_AVX)
  61. #define JPH_USE_AVX
  62. #endif
  63. #if defined(__AVX2__) && !defined(JPH_USE_AVX2)
  64. #define JPH_USE_AVX2
  65. #endif
  66. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  67. #if defined(__FMA__) && !defined(JPH_USE_FMADD)
  68. #define JPH_USE_FMADD
  69. #endif
  70. #elif defined(JPH_COMPILER_MSVC)
  71. #if defined(__AVX2__) && !defined(JPH_USE_FMADD) // AVX2 also enables fused multiply add
  72. #define JPH_USE_FMADD
  73. #endif
  74. #else
  75. #error Undefined compiler
  76. #endif
  77. #elif defined(__aarch64__) || defined(_M_ARM64)
  78. // ARM64 CPU architecture
  79. #define JPH_CPU_ARM64
  80. #define JPH_USE_NEON
  81. #else
  82. #error Unsupported CPU architecture
  83. #endif
  84. // Pragmas to store / restore the warning state and to disable individual warnings
  85. #ifdef JPH_COMPILER_CLANG
  86. #define JPH_PRAGMA(x) _Pragma(#x)
  87. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(clang diagnostic push)
  88. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(clang diagnostic pop)
  89. #define JPH_CLANG_SUPPRESS_WARNING(w) JPH_PRAGMA(clang diagnostic ignored w)
  90. #else
  91. #define JPH_CLANG_SUPPRESS_WARNING(w)
  92. #endif
  93. #ifdef JPH_COMPILER_GCC
  94. #define JPH_PRAGMA(x) _Pragma(#x)
  95. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(GCC diagnostic push)
  96. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(GCC diagnostic pop)
  97. #define JPH_GCC_SUPPRESS_WARNING(w) JPH_PRAGMA(GCC diagnostic ignored w)
  98. #else
  99. #define JPH_GCC_SUPPRESS_WARNING(w)
  100. #endif
  101. #ifdef JPH_COMPILER_MSVC
  102. #define JPH_PRAGMA(x) __pragma(x)
  103. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(warning (push))
  104. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(warning (pop))
  105. #define JPH_MSVC_SUPPRESS_WARNING(w) JPH_PRAGMA(warning (disable : w))
  106. #else
  107. #define JPH_MSVC_SUPPRESS_WARNING(w)
  108. #endif
  109. // Disable common warnings triggered by Jolt when compiling with -Wall
  110. #define JPH_SUPPRESS_WARNINGS \
  111. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat") \
  112. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") \
  113. JPH_CLANG_SUPPRESS_WARNING("-Wfloat-equal") \
  114. JPH_CLANG_SUPPRESS_WARNING("-Wsign-conversion") \
  115. JPH_CLANG_SUPPRESS_WARNING("-Wold-style-cast") \
  116. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-anonymous-struct") \
  117. JPH_CLANG_SUPPRESS_WARNING("-Wnested-anon-types") \
  118. JPH_CLANG_SUPPRESS_WARNING("-Wglobal-constructors") \
  119. JPH_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors") \
  120. JPH_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path") \
  121. JPH_CLANG_SUPPRESS_WARNING("-Wlanguage-extension-token") \
  122. JPH_CLANG_SUPPRESS_WARNING("-Wunused-parameter") \
  123. JPH_CLANG_SUPPRESS_WARNING("-Wformat-nonliteral") \
  124. JPH_CLANG_SUPPRESS_WARNING("-Wcovered-switch-default") \
  125. JPH_CLANG_SUPPRESS_WARNING("-Wcast-align") \
  126. JPH_CLANG_SUPPRESS_WARNING("-Winvalid-offsetof") \
  127. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-zero-variadic-macro-arguments") \
  128. JPH_CLANG_SUPPRESS_WARNING("-Wdocumentation-unknown-command") \
  129. JPH_CLANG_SUPPRESS_WARNING("-Wctad-maybe-unsupported") \
  130. JPH_IF_NOT_ANDROID(JPH_CLANG_SUPPRESS_WARNING("-Wimplicit-int-float-conversion")) \
  131. \
  132. JPH_GCC_SUPPRESS_WARNING("-Wcomment") \
  133. JPH_GCC_SUPPRESS_WARNING("-Winvalid-offsetof") \
  134. JPH_GCC_SUPPRESS_WARNING("-Wclass-memaccess") \
  135. \
  136. JPH_MSVC_SUPPRESS_WARNING(4514) /* 'X' : unreferenced inline function has been removed */ \
  137. JPH_MSVC_SUPPRESS_WARNING(4710) /* 'X' : function not inlined */ \
  138. JPH_MSVC_SUPPRESS_WARNING(4711) /* function 'X' selected for automatic inline expansion */ \
  139. JPH_MSVC_SUPPRESS_WARNING(4820) /* 'X': 'Y' bytes padding added after data member 'Z' */ \
  140. JPH_MSVC_SUPPRESS_WARNING(4100) /* 'X' : unreferenced formal parameter */ \
  141. JPH_MSVC_SUPPRESS_WARNING(4626) /* 'X' : assignment operator was implicitly defined as deleted because a base class assignment operator is inaccessible or deleted */ \
  142. JPH_MSVC_SUPPRESS_WARNING(5027) /* 'X' : move assignment operator was implicitly defined as deleted because a base class move assignment operator is inaccessible or deleted */ \
  143. JPH_MSVC_SUPPRESS_WARNING(4365) /* 'argument' : conversion from 'X' to 'Y', signed / unsigned mismatch */ \
  144. JPH_MSVC_SUPPRESS_WARNING(4324) /* 'X' : structure was padded due to alignment specifier */ \
  145. JPH_MSVC_SUPPRESS_WARNING(4625) /* 'X' : copy constructor was implicitly defined as deleted because a base class copy constructor is inaccessible or deleted */ \
  146. JPH_MSVC_SUPPRESS_WARNING(5026) /* 'X': move constructor was implicitly defined as deleted because a base class move constructor is inaccessible or deleted */ \
  147. JPH_MSVC_SUPPRESS_WARNING(4623) /* 'X' : default constructor was implicitly defined as deleted */ \
  148. JPH_MSVC_SUPPRESS_WARNING(4201) /* nonstandard extension used: nameless struct/union */ \
  149. JPH_MSVC_SUPPRESS_WARNING(4371) /* 'X': layout of class may have changed from a previous version of the compiler due to better packing of member 'Y' */ \
  150. JPH_MSVC_SUPPRESS_WARNING(5045) /* Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified */ \
  151. JPH_MSVC_SUPPRESS_WARNING(4583) /* 'X': destructor is not implicitly called */ \
  152. JPH_MSVC_SUPPRESS_WARNING(4582) /* 'X': constructor is not implicitly called */ \
  153. JPH_MSVC_SUPPRESS_WARNING(5219) /* implicit conversion from 'X' to 'Y', possible loss of data */
  154. // OS-specific includes
  155. #if defined(JPH_PLATFORM_WINDOWS)
  156. #define JPH_BREAKPOINT __debugbreak()
  157. #elif defined(JPH_PLATFORM_BLUE)
  158. // Configuration for a popular game console.
  159. // This file is not distributed because it would violate an NDA.
  160. // Creating one should only be a couple of minutes of work if you have the documentation for the platform
  161. // (you only need to define JPH_BREAKPOINT, JPH_PLATFORM_BLUE_GET_TICKS and JPH_PLATFORM_BLUE_GET_TICK_FREQUENCY and include the right header).
  162. #include <Jolt/Core/PlatformBlue.h>
  163. #elif defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS)
  164. #include <float.h>
  165. #include <limits.h>
  166. #include <string.h>
  167. #if defined(JPH_CPU_X64)
  168. #define JPH_BREAKPOINT __asm volatile ("int $0x3")
  169. #elif defined(JPH_CPU_ARM64)
  170. #define JPH_BREAKPOINT __builtin_trap()
  171. #endif
  172. #else
  173. #error Unknown platform
  174. #endif
  175. // Crashes the application
  176. #define JPH_CRASH do { int *ptr = nullptr; *ptr = 0; } while (false)
  177. // Begin the JPH namespace
  178. #define JPH_NAMESPACE_BEGIN \
  179. JPH_SUPPRESS_WARNING_PUSH \
  180. JPH_SUPPRESS_WARNINGS \
  181. namespace JPH {
  182. // End the JPH namespace
  183. #define JPH_NAMESPACE_END \
  184. } \
  185. JPH_SUPPRESS_WARNING_POP
  186. // On MSVC the std library generates warnings, use these macros to disable them
  187. #define JPH_SUPPRESS_WARNINGS_STD_BEGIN \
  188. JPH_SUPPRESS_WARNING_PUSH \
  189. JPH_MSVC_SUPPRESS_WARNING(4710) \
  190. JPH_MSVC_SUPPRESS_WARNING(4711) \
  191. JPH_MSVC_SUPPRESS_WARNING(4820) \
  192. JPH_MSVC_SUPPRESS_WARNING(4514)
  193. #define JPH_SUPPRESS_WARNINGS_STD_END \
  194. JPH_SUPPRESS_WARNING_POP
  195. // Standard C++ includes
  196. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  197. #include <vector>
  198. #include <algorithm>
  199. #include <utility>
  200. #include <cmath>
  201. #include <sstream>
  202. #include <functional>
  203. JPH_SUPPRESS_WARNINGS_STD_END
  204. #if defined(JPH_USE_SSE)
  205. #include <immintrin.h>
  206. #elif defined(JPH_USE_NEON)
  207. #include <arm_neon.h>
  208. #endif
  209. JPH_NAMESPACE_BEGIN
  210. using namespace std;
  211. // Standard types
  212. using uint = unsigned int;
  213. using uint8 = uint8_t;
  214. using uint16 = uint16_t;
  215. using uint32 = uint32_t;
  216. using uint64 = uint64_t;
  217. // Assert sizes of types
  218. static_assert(sizeof(uint) >= 4, "Invalid size of uint");
  219. static_assert(sizeof(uint8) == 1, "Invalid size of uint8");
  220. static_assert(sizeof(uint16) == 2, "Invalid size of uint16");
  221. static_assert(sizeof(uint32) == 4, "Invalid size of uint32");
  222. static_assert(sizeof(uint64) == 8, "Invalid size of uint64");
  223. static_assert(sizeof(void *) == 8, "Invalid size of pointer");
  224. // Define inline macro
  225. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  226. #define JPH_INLINE __inline__ __attribute__((always_inline))
  227. #elif defined(JPH_COMPILER_MSVC)
  228. #define JPH_INLINE __forceinline
  229. #else
  230. #error Undefined
  231. #endif
  232. // Cache line size (used for aligning to cache line)
  233. #ifndef JPH_CACHE_LINE_SIZE
  234. #define JPH_CACHE_LINE_SIZE 64
  235. #endif
  236. // Define macro to get current function name
  237. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  238. #define JPH_FUNCTION_NAME __PRETTY_FUNCTION__
  239. #elif defined(JPH_COMPILER_MSVC)
  240. #define JPH_FUNCTION_NAME __FUNCTION__
  241. #else
  242. #error Undefined
  243. #endif
  244. // Stack allocation
  245. #define JPH_STACK_ALLOC(n) alloca(n)
  246. // Shorthand for #ifdef _DEBUG / #endif
  247. #ifdef _DEBUG
  248. #define JPH_IF_DEBUG(...) __VA_ARGS__
  249. #else
  250. #define JPH_IF_DEBUG(...)
  251. #endif
  252. // Macro to indicate that a parameter / variable is unused
  253. #define JPH_UNUSED(x) (void)x
  254. // Macro to enable floating point precise mode and to disable fused multiply add instructions
  255. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  256. // In clang it appears you cannot turn off -ffast-math and -ffp-contract=fast for a code block
  257. // There is #pragma clang fp contract (off) but that doesn't seem to work under clang 9 & 10 when -ffast-math is specified on the commandline (you override it to turn it on, but not off)
  258. // There is #pragma float_control(precise, on) but that doesn't work under clang 9.
  259. // So for now we compile clang without -ffast-math so the macros are empty
  260. #define JPH_PRECISE_MATH_ON
  261. #define JPH_PRECISE_MATH_OFF
  262. #elif defined(JPH_COMPILER_MSVC)
  263. // Unfortunately there is no way to push the state of fp_contract, so we have to assume it was turned on before JPH_PRECISE_MATH_ON
  264. #define JPH_PRECISE_MATH_ON \
  265. __pragma(float_control(precise, on, push)) \
  266. __pragma(fp_contract(off))
  267. #define JPH_PRECISE_MATH_OFF \
  268. __pragma(fp_contract(on)) \
  269. __pragma(float_control(pop))
  270. #else
  271. #error Undefined
  272. #endif
  273. JPH_NAMESPACE_END