Core.h 22 KB

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