Core.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. // Determine platform
  6. #if defined(JPH_PLATFORM_BLUE)
  7. // Correct define already defined, this overrides everything else
  8. #elif defined(_WIN32) || defined(_WIN64)
  9. #include <winapifamily.h>
  10. #if WINAPI_FAMILY == WINAPI_FAMILY_APP
  11. #define JPH_PLATFORM_WINDOWS_UWP // Building for Universal Windows Platform
  12. #endif
  13. #define JPH_PLATFORM_WINDOWS
  14. #elif defined(__ANDROID__) // Android is linux too, so that's why we check it first
  15. #define JPH_PLATFORM_ANDROID
  16. #elif defined(__linux__)
  17. #define JPH_PLATFORM_LINUX
  18. #elif defined(__APPLE__)
  19. #include <TargetConditionals.h>
  20. #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE
  21. #define JPH_PLATFORM_MACOS
  22. #else
  23. #define JPH_PLATFORM_IOS
  24. #endif
  25. #elif defined(__EMSCRIPTEN__)
  26. #define JPH_PLATFORM_WASM
  27. #endif
  28. // Platform helper macros
  29. #ifdef JPH_PLATFORM_ANDROID
  30. #define JPH_IF_NOT_ANDROID(x)
  31. #else
  32. #define JPH_IF_NOT_ANDROID(x) x
  33. #endif
  34. // Determine compiler
  35. #if defined(__clang__)
  36. #define JPH_COMPILER_CLANG
  37. #elif defined(__GNUC__)
  38. #define JPH_COMPILER_GCC
  39. #elif defined(_MSC_VER)
  40. #define JPH_COMPILER_MSVC
  41. #endif
  42. #if defined(__MINGW64__) || defined (__MINGW32__)
  43. #define JPH_COMPILER_MINGW
  44. #endif
  45. // Detect CPU architecture
  46. #if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
  47. // X86 CPU architecture
  48. #define JPH_CPU_X86
  49. #if defined(__x86_64__) || defined(_M_X64)
  50. #define JPH_CPU_ADDRESS_BITS 64
  51. #else
  52. #define JPH_CPU_ADDRESS_BITS 32
  53. #endif
  54. #define JPH_USE_SSE
  55. #define JPH_VECTOR_ALIGNMENT 16
  56. #define JPH_DVECTOR_ALIGNMENT 32
  57. // Detect enabled instruction sets
  58. #if defined(__AVX512F__) && defined(__AVX512VL__) && defined(__AVX512DQ__) && !defined(JPH_USE_AVX512)
  59. #define JPH_USE_AVX512
  60. #endif
  61. #if (defined(__AVX2__) || defined(JPH_USE_AVX512)) && !defined(JPH_USE_AVX2)
  62. #define JPH_USE_AVX2
  63. #endif
  64. #if (defined(__AVX__) || defined(JPH_USE_AVX2)) && !defined(JPH_USE_AVX)
  65. #define JPH_USE_AVX
  66. #endif
  67. #if (defined(__SSE4_2__) || defined(JPH_USE_AVX)) && !defined(JPH_USE_SSE4_2)
  68. #define JPH_USE_SSE4_2
  69. #endif
  70. #if (defined(__SSE4_1__) || defined(JPH_USE_SSE4_2)) && !defined(JPH_USE_SSE4_1)
  71. #define JPH_USE_SSE4_1
  72. #endif
  73. #if (defined(__F16C__) || defined(JPH_USE_AVX2)) && !defined(JPH_USE_F16C)
  74. #define JPH_USE_F16C
  75. #endif
  76. #if (defined(__LZCNT__) || defined(JPH_USE_AVX2)) && !defined(JPH_USE_LZCNT)
  77. #define JPH_USE_LZCNT
  78. #endif
  79. #if (defined(__BMI__) || defined(JPH_USE_AVX2)) && !defined(JPH_USE_TZCNT)
  80. #define JPH_USE_TZCNT
  81. #endif
  82. #ifndef JPH_CROSS_PLATFORM_DETERMINISTIC // FMA is not compatible with cross platform determinism
  83. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  84. #if defined(__FMA__) && !defined(JPH_USE_FMADD)
  85. #define JPH_USE_FMADD
  86. #endif
  87. #elif defined(JPH_COMPILER_MSVC)
  88. #if defined(__AVX2__) && !defined(JPH_USE_FMADD) // AVX2 also enables fused multiply add
  89. #define JPH_USE_FMADD
  90. #endif
  91. #else
  92. #error Undefined compiler
  93. #endif
  94. #endif
  95. #elif defined(__aarch64__) || defined(_M_ARM64) || defined(__arm__) || defined(_M_ARM)
  96. // ARM CPU architecture
  97. #define JPH_CPU_ARM
  98. #if defined(__aarch64__) || defined(_M_ARM64)
  99. #define JPH_CPU_ADDRESS_BITS 64
  100. #define JPH_USE_NEON
  101. #define JPH_VECTOR_ALIGNMENT 16
  102. #define JPH_DVECTOR_ALIGNMENT 32
  103. #else
  104. #define JPH_CPU_ADDRESS_BITS 32
  105. #define JPH_VECTOR_ALIGNMENT 8 // 32-bit ARM does not support aligning on the stack on 16 byte boundaries
  106. #define JPH_DVECTOR_ALIGNMENT 8
  107. #endif
  108. #elif defined(JPH_PLATFORM_WASM)
  109. // WebAssembly CPU architecture
  110. #define JPH_CPU_WASM
  111. #define JPH_CPU_ADDRESS_BITS 32
  112. #define JPH_VECTOR_ALIGNMENT 16
  113. #define JPH_DVECTOR_ALIGNMENT 32
  114. #define JPH_DISABLE_CUSTOM_ALLOCATOR
  115. #else
  116. #error Unsupported CPU architecture
  117. #endif
  118. // Pragmas to store / restore the warning state and to disable individual warnings
  119. #ifdef JPH_COMPILER_CLANG
  120. #define JPH_PRAGMA(x) _Pragma(#x)
  121. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(clang diagnostic push)
  122. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(clang diagnostic pop)
  123. #define JPH_CLANG_SUPPRESS_WARNING(w) JPH_PRAGMA(clang diagnostic ignored w)
  124. #else
  125. #define JPH_CLANG_SUPPRESS_WARNING(w)
  126. #endif
  127. #ifdef JPH_COMPILER_GCC
  128. #define JPH_PRAGMA(x) _Pragma(#x)
  129. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(GCC diagnostic push)
  130. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(GCC diagnostic pop)
  131. #define JPH_GCC_SUPPRESS_WARNING(w) JPH_PRAGMA(GCC diagnostic ignored w)
  132. #else
  133. #define JPH_GCC_SUPPRESS_WARNING(w)
  134. #endif
  135. #ifdef JPH_COMPILER_MSVC
  136. #define JPH_PRAGMA(x) __pragma(x)
  137. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(warning (push))
  138. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(warning (pop))
  139. #define JPH_MSVC_SUPPRESS_WARNING(w) JPH_PRAGMA(warning (disable : w))
  140. #if _MSC_VER >= 1920 && _MSC_VER < 1930
  141. #define JPH_MSVC2019_SUPPRESS_WARNING(w) JPH_MSVC_SUPPRESS_WARNING(w)
  142. #else
  143. #define JPH_MSVC2019_SUPPRESS_WARNING(w)
  144. #endif
  145. #else
  146. #define JPH_MSVC_SUPPRESS_WARNING(w)
  147. #define JPH_MSVC2019_SUPPRESS_WARNING(w)
  148. #endif
  149. // Disable common warnings triggered by Jolt when compiling with -Wall
  150. #define JPH_SUPPRESS_WARNINGS \
  151. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat") \
  152. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") \
  153. JPH_CLANG_SUPPRESS_WARNING("-Wfloat-equal") \
  154. JPH_CLANG_SUPPRESS_WARNING("-Wsign-conversion") \
  155. JPH_CLANG_SUPPRESS_WARNING("-Wold-style-cast") \
  156. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-anonymous-struct") \
  157. JPH_CLANG_SUPPRESS_WARNING("-Wnested-anon-types") \
  158. JPH_CLANG_SUPPRESS_WARNING("-Wglobal-constructors") \
  159. JPH_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors") \
  160. JPH_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path") \
  161. JPH_CLANG_SUPPRESS_WARNING("-Wlanguage-extension-token") \
  162. JPH_CLANG_SUPPRESS_WARNING("-Wunused-parameter") \
  163. JPH_CLANG_SUPPRESS_WARNING("-Wformat-nonliteral") \
  164. JPH_CLANG_SUPPRESS_WARNING("-Wcovered-switch-default") \
  165. JPH_CLANG_SUPPRESS_WARNING("-Wcast-align") \
  166. JPH_CLANG_SUPPRESS_WARNING("-Winvalid-offsetof") \
  167. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-zero-variadic-macro-arguments") \
  168. JPH_CLANG_SUPPRESS_WARNING("-Wdocumentation-unknown-command") \
  169. JPH_CLANG_SUPPRESS_WARNING("-Wctad-maybe-unsupported") \
  170. JPH_CLANG_SUPPRESS_WARNING("-Wdeprecated-copy") \
  171. JPH_IF_NOT_ANDROID(JPH_CLANG_SUPPRESS_WARNING("-Wimplicit-int-float-conversion")) \
  172. \
  173. JPH_GCC_SUPPRESS_WARNING("-Wcomment") \
  174. JPH_GCC_SUPPRESS_WARNING("-Winvalid-offsetof") \
  175. JPH_GCC_SUPPRESS_WARNING("-Wclass-memaccess") \
  176. \
  177. JPH_MSVC_SUPPRESS_WARNING(4619) /* #pragma warning: there is no warning number 'XXXX' */ \
  178. JPH_MSVC_SUPPRESS_WARNING(4514) /* 'X' : unreferenced inline function has been removed */ \
  179. JPH_MSVC_SUPPRESS_WARNING(4710) /* 'X' : function not inlined */ \
  180. JPH_MSVC_SUPPRESS_WARNING(4711) /* function 'X' selected for automatic inline expansion */ \
  181. JPH_MSVC_SUPPRESS_WARNING(4820) /* 'X': 'Y' bytes padding added after data member 'Z' */ \
  182. JPH_MSVC_SUPPRESS_WARNING(4100) /* 'X' : unreferenced formal parameter */ \
  183. JPH_MSVC_SUPPRESS_WARNING(4626) /* 'X' : assignment operator was implicitly defined as deleted because a base class assignment operator is inaccessible or deleted */ \
  184. 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 */ \
  185. JPH_MSVC_SUPPRESS_WARNING(4365) /* 'argument' : conversion from 'X' to 'Y', signed / unsigned mismatch */ \
  186. JPH_MSVC_SUPPRESS_WARNING(4324) /* 'X' : structure was padded due to alignment specifier */ \
  187. JPH_MSVC_SUPPRESS_WARNING(4625) /* 'X' : copy constructor was implicitly defined as deleted because a base class copy constructor is inaccessible or deleted */ \
  188. JPH_MSVC_SUPPRESS_WARNING(5026) /* 'X': move constructor was implicitly defined as deleted because a base class move constructor is inaccessible or deleted */ \
  189. JPH_MSVC_SUPPRESS_WARNING(4623) /* 'X' : default constructor was implicitly defined as deleted */ \
  190. JPH_MSVC_SUPPRESS_WARNING(4201) /* nonstandard extension used: nameless struct/union */ \
  191. 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' */ \
  192. JPH_MSVC_SUPPRESS_WARNING(5045) /* Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified */ \
  193. JPH_MSVC_SUPPRESS_WARNING(4583) /* 'X': destructor is not implicitly called */ \
  194. JPH_MSVC_SUPPRESS_WARNING(4582) /* 'X': constructor is not implicitly called */ \
  195. JPH_MSVC_SUPPRESS_WARNING(5219) /* implicit conversion from 'X' to 'Y', possible loss of data */ \
  196. JPH_MSVC_SUPPRESS_WARNING(4826) /* Conversion from 'X *' to 'JPH::uint64' is sign-extended. This may cause unexpected runtime behavior. (32-bit) */ \
  197. JPH_MSVC_SUPPRESS_WARNING(5264) /* 'X': 'const' variable is not used */ \
  198. JPH_MSVC2019_SUPPRESS_WARNING(5246) /* the initialization of a subobject should be wrapped in braces */
  199. // OS-specific includes
  200. #if defined(JPH_PLATFORM_WINDOWS)
  201. #define JPH_BREAKPOINT __debugbreak()
  202. #elif defined(JPH_PLATFORM_BLUE)
  203. // Configuration for a popular game console.
  204. // This file is not distributed because it would violate an NDA.
  205. // Creating one should only be a couple of minutes of work if you have the documentation for the platform
  206. // (you only need to define JPH_BREAKPOINT, JPH_PLATFORM_BLUE_GET_TICKS and JPH_PLATFORM_BLUE_GET_TICK_FREQUENCY and include the right header).
  207. #include <Jolt/Core/PlatformBlue.h>
  208. #elif defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS)
  209. #if defined(JPH_CPU_X86)
  210. #define JPH_BREAKPOINT __asm volatile ("int $0x3")
  211. #elif defined(JPH_CPU_ARM)
  212. #define JPH_BREAKPOINT __builtin_trap()
  213. #endif
  214. #elif defined(JPH_PLATFORM_WASM)
  215. #define JPH_BREAKPOINT do { } while (false) // Not supported
  216. #else
  217. #error Unknown platform
  218. #endif
  219. // Crashes the application
  220. #define JPH_CRASH do { int *ptr = nullptr; *ptr = 0; } while (false)
  221. // Begin the JPH namespace
  222. #define JPH_NAMESPACE_BEGIN \
  223. JPH_SUPPRESS_WARNING_PUSH \
  224. JPH_SUPPRESS_WARNINGS \
  225. namespace JPH {
  226. // End the JPH namespace
  227. #define JPH_NAMESPACE_END \
  228. } \
  229. JPH_SUPPRESS_WARNING_POP
  230. // Suppress warnings generated by the standard template library
  231. #define JPH_SUPPRESS_WARNINGS_STD_BEGIN \
  232. JPH_SUPPRESS_WARNING_PUSH \
  233. JPH_MSVC_SUPPRESS_WARNING(4619) \
  234. JPH_MSVC_SUPPRESS_WARNING(4710) \
  235. JPH_MSVC_SUPPRESS_WARNING(4711) \
  236. JPH_MSVC_SUPPRESS_WARNING(4820) \
  237. JPH_MSVC_SUPPRESS_WARNING(4514) \
  238. JPH_MSVC_SUPPRESS_WARNING(5262) \
  239. JPH_MSVC_SUPPRESS_WARNING(5264)
  240. #define JPH_SUPPRESS_WARNINGS_STD_END \
  241. JPH_SUPPRESS_WARNING_POP
  242. // Standard C++ includes
  243. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  244. #include <vector>
  245. #include <utility>
  246. #include <cmath>
  247. #include <sstream>
  248. #include <functional>
  249. #include <algorithm>
  250. JPH_SUPPRESS_WARNINGS_STD_END
  251. #include <limits.h>
  252. #include <float.h>
  253. #include <string.h>
  254. #if defined(JPH_USE_SSE)
  255. #include <immintrin.h>
  256. #elif defined(JPH_USE_NEON)
  257. #ifdef JPH_COMPILER_MSVC
  258. #include <intrin.h>
  259. #include <arm64_neon.h>
  260. #else
  261. #include <arm_neon.h>
  262. #endif
  263. #endif
  264. JPH_NAMESPACE_BEGIN
  265. // Commonly used STL types
  266. using std::pair;
  267. using std::min;
  268. using std::max;
  269. using std::abs;
  270. using std::sqrt;
  271. using std::ceil;
  272. using std::floor;
  273. using std::trunc;
  274. using std::round;
  275. using std::fmod;
  276. using std::swap;
  277. using std::size;
  278. using std::string;
  279. using std::string_view;
  280. using std::function;
  281. using std::numeric_limits;
  282. using std::isfinite;
  283. using std::isnan;
  284. using std::is_trivial;
  285. using std::is_trivially_constructible;
  286. using std::is_trivially_destructible;
  287. using std::ostream;
  288. using std::istream;
  289. // Standard types
  290. using uint = unsigned int;
  291. using uint8 = uint8_t;
  292. using uint16 = uint16_t;
  293. using uint32 = uint32_t;
  294. using uint64 = uint64_t;
  295. // Assert sizes of types
  296. static_assert(sizeof(uint) >= 4, "Invalid size of uint");
  297. static_assert(sizeof(uint8) == 1, "Invalid size of uint8");
  298. static_assert(sizeof(uint16) == 2, "Invalid size of uint16");
  299. static_assert(sizeof(uint32) == 4, "Invalid size of uint32");
  300. static_assert(sizeof(uint64) == 8, "Invalid size of uint64");
  301. static_assert(sizeof(void *) == (JPH_CPU_ADDRESS_BITS == 64? 8 : 4), "Invalid size of pointer" );
  302. // Define inline macro
  303. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  304. #define JPH_INLINE __inline__ __attribute__((always_inline))
  305. #elif defined(JPH_COMPILER_MSVC)
  306. #define JPH_INLINE __forceinline
  307. #else
  308. #error Undefined
  309. #endif
  310. // Cache line size (used for aligning to cache line)
  311. #ifndef JPH_CACHE_LINE_SIZE
  312. #define JPH_CACHE_LINE_SIZE 64
  313. #endif
  314. // Define macro to get current function name
  315. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  316. #define JPH_FUNCTION_NAME __PRETTY_FUNCTION__
  317. #elif defined(JPH_COMPILER_MSVC)
  318. #define JPH_FUNCTION_NAME __FUNCTION__
  319. #else
  320. #error Undefined
  321. #endif
  322. // Stack allocation
  323. #define JPH_STACK_ALLOC(n) alloca(n)
  324. // Shorthand for #ifdef _DEBUG / #endif
  325. #ifdef _DEBUG
  326. #define JPH_IF_DEBUG(...) __VA_ARGS__
  327. #define JPH_IF_NOT_DEBUG(...)
  328. #else
  329. #define JPH_IF_DEBUG(...)
  330. #define JPH_IF_NOT_DEBUG(...) __VA_ARGS__
  331. #endif
  332. // Shorthand for #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED / #endif
  333. #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED
  334. #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...) __VA_ARGS__
  335. #else
  336. #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...)
  337. #endif
  338. // Helper macros to detect if we're running in single or double precision mode
  339. #ifdef JPH_DOUBLE_PRECISION
  340. #define JPH_IF_SINGLE_PRECISION(...)
  341. #define JPH_IF_SINGLE_PRECISION_ELSE(s, d) d
  342. #define JPH_IF_DOUBLE_PRECISION(...) __VA_ARGS__
  343. #else
  344. #define JPH_IF_SINGLE_PRECISION(...) __VA_ARGS__
  345. #define JPH_IF_SINGLE_PRECISION_ELSE(s, d) s
  346. #define JPH_IF_DOUBLE_PRECISION(...)
  347. #endif
  348. // Helper macro to detect if the debug renderer is active
  349. #ifdef JPH_DEBUG_RENDERER
  350. #define JPH_IF_DEBUG_RENDERER(...) __VA_ARGS__
  351. #define JPH_IF_NOT_DEBUG_RENDERER(...)
  352. #else
  353. #define JPH_IF_DEBUG_RENDERER(...)
  354. #define JPH_IF_NOT_DEBUG_RENDERER(...) __VA_ARGS__
  355. #endif
  356. // Macro to indicate that a parameter / variable is unused
  357. #define JPH_UNUSED(x) (void)x
  358. // Macro to enable floating point precise mode and to disable fused multiply add instructions
  359. #if defined(JPH_COMPILER_GCC) || defined(JPH_CROSS_PLATFORM_DETERMINISTIC)
  360. // We compile without -ffast-math and -ffp-contract=fast, so we don't need to disable anything
  361. #define JPH_PRECISE_MATH_ON
  362. #define JPH_PRECISE_MATH_OFF
  363. #elif defined(JPH_COMPILER_CLANG)
  364. // We compile without -ffast-math because it cannot be turned off for a single compilation unit
  365. // On clang 14 and later we can turn off float contraction through a pragma, so if FMA is on we can disable it through this macro
  366. #if __clang_major__ >= 14 && defined(JPH_USE_FMADD)
  367. #define JPH_PRECISE_MATH_ON \
  368. _Pragma("clang fp contract(off)")
  369. #define JPH_PRECISE_MATH_OFF \
  370. _Pragma("clang fp contract(on)")
  371. #else
  372. #define JPH_PRECISE_MATH_ON
  373. #define JPH_PRECISE_MATH_OFF
  374. #endif
  375. #elif defined(JPH_COMPILER_MSVC)
  376. // 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
  377. #define JPH_PRECISE_MATH_ON \
  378. __pragma(float_control(precise, on, push)) \
  379. __pragma(fp_contract(off))
  380. #define JPH_PRECISE_MATH_OFF \
  381. __pragma(fp_contract(on)) \
  382. __pragma(float_control(pop))
  383. #else
  384. #error Undefined
  385. #endif
  386. JPH_NAMESPACE_END