2
0

Core.h 20 KB

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