Core.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 3
  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__) || defined(__OpenBSD__) || defined(__NetBSD__)
  82. #define JPH_PLATFORM_BSD
  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(__riscv)
  174. // RISC-V CPU architecture
  175. #define JPH_CPU_RISCV
  176. #if __riscv_xlen == 64
  177. #define JPH_CPU_ADDRESS_BITS 64
  178. #define JPH_VECTOR_ALIGNMENT 16
  179. #define JPH_DVECTOR_ALIGNMENT 32
  180. #else
  181. #define JPH_CPU_ADDRESS_BITS 32
  182. #define JPH_VECTOR_ALIGNMENT 16
  183. #define JPH_DVECTOR_ALIGNMENT 8
  184. #endif
  185. #elif defined(JPH_PLATFORM_WASM)
  186. // WebAssembly CPU architecture
  187. #define JPH_CPU_WASM
  188. #if defined(__wasm64__)
  189. #define JPH_CPU_ADDRESS_BITS 64
  190. #else
  191. #define JPH_CPU_ADDRESS_BITS 32
  192. #endif
  193. #define JPH_VECTOR_ALIGNMENT 16
  194. #define JPH_DVECTOR_ALIGNMENT 32
  195. #ifdef __wasm_simd128__
  196. #define JPH_USE_SSE
  197. #define JPH_USE_SSE4_1
  198. #define JPH_USE_SSE4_2
  199. #endif
  200. #elif defined(__powerpc__) || defined(__powerpc64__)
  201. // PowerPC CPU architecture
  202. #define JPH_CPU_PPC
  203. #if defined(__powerpc64__)
  204. #define JPH_CPU_ADDRESS_BITS 64
  205. #else
  206. #define JPH_CPU_ADDRESS_BITS 32
  207. #endif
  208. #ifdef _BIG_ENDIAN
  209. #define JPH_CPU_BIG_ENDIAN
  210. #endif
  211. #define JPH_VECTOR_ALIGNMENT 16
  212. #define JPH_DVECTOR_ALIGNMENT 8
  213. #elif defined(__loongarch__)
  214. // LoongArch CPU architecture
  215. #define JPH_CPU_LOONGARCH
  216. #if defined(__loongarch64)
  217. #define JPH_CPU_ADDRESS_BITS 64
  218. #else
  219. #define JPH_CPU_ADDRESS_BITS 32
  220. #endif
  221. #define JPH_VECTOR_ALIGNMENT 16
  222. #define JPH_DVECTOR_ALIGNMENT 8
  223. #elif defined(__e2k__)
  224. // E2K CPU architecture (MCST Elbrus 2000)
  225. #define JPH_CPU_E2K
  226. #define JPH_CPU_ADDRESS_BITS 64
  227. #define JPH_VECTOR_ALIGNMENT 16
  228. #define JPH_DVECTOR_ALIGNMENT 32
  229. // Compiler flags on e2k arch determine CPU features
  230. #if defined(__SSE__) && !defined(JPH_USE_SSE)
  231. #define JPH_USE_SSE
  232. #endif
  233. #else
  234. #error Unsupported CPU architecture
  235. #endif
  236. // If this define is set, Jolt is compiled as a shared library
  237. #ifdef JPH_SHARED_LIBRARY
  238. #ifdef JPH_BUILD_SHARED_LIBRARY
  239. // While building the shared library, we must export these symbols
  240. #if defined(JPH_PLATFORM_WINDOWS) && !defined(JPH_COMPILER_MINGW)
  241. #define JPH_EXPORT __declspec(dllexport)
  242. #else
  243. #define JPH_EXPORT __attribute__ ((visibility ("default")))
  244. #if defined(JPH_COMPILER_GCC)
  245. // Prevents an issue with GCC attribute parsing (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69585)
  246. #define JPH_EXPORT_GCC_BUG_WORKAROUND [[gnu::visibility("default")]]
  247. #endif
  248. #endif
  249. #else
  250. // When linking against Jolt, we must import these symbols
  251. #if defined(JPH_PLATFORM_WINDOWS) && !defined(JPH_COMPILER_MINGW)
  252. #define JPH_EXPORT __declspec(dllimport)
  253. #else
  254. #define JPH_EXPORT __attribute__ ((visibility ("default")))
  255. #if defined(JPH_COMPILER_GCC)
  256. // Prevents an issue with GCC attribute parsing (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69585)
  257. #define JPH_EXPORT_GCC_BUG_WORKAROUND [[gnu::visibility("default")]]
  258. #endif
  259. #endif
  260. #endif
  261. #else
  262. // If the define is not set, we use static linking and symbols don't need to be imported or exported
  263. #define JPH_EXPORT
  264. #endif
  265. #ifndef JPH_EXPORT_GCC_BUG_WORKAROUND
  266. #define JPH_EXPORT_GCC_BUG_WORKAROUND JPH_EXPORT
  267. #endif
  268. // Macro used by the RTTI macros to not export a function
  269. #define JPH_NO_EXPORT
  270. // Pragmas to store / restore the warning state and to disable individual warnings
  271. #ifdef JPH_COMPILER_CLANG
  272. #define JPH_PRAGMA(x) _Pragma(#x)
  273. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(clang diagnostic push)
  274. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(clang diagnostic pop)
  275. #define JPH_CLANG_SUPPRESS_WARNING(w) JPH_PRAGMA(clang diagnostic ignored w)
  276. #if __clang_major__ >= 13
  277. #define JPH_CLANG_13_PLUS_SUPPRESS_WARNING(w) JPH_CLANG_SUPPRESS_WARNING(w)
  278. #else
  279. #define JPH_CLANG_13_PLUS_SUPPRESS_WARNING(w)
  280. #endif
  281. #if __clang_major__ >= 16
  282. #define JPH_CLANG_16_PLUS_SUPPRESS_WARNING(w) JPH_CLANG_SUPPRESS_WARNING(w)
  283. #else
  284. #define JPH_CLANG_16_PLUS_SUPPRESS_WARNING(w)
  285. #endif
  286. #else
  287. #define JPH_CLANG_SUPPRESS_WARNING(w)
  288. #define JPH_CLANG_13_PLUS_SUPPRESS_WARNING(w)
  289. #define JPH_CLANG_16_PLUS_SUPPRESS_WARNING(w)
  290. #endif
  291. #ifdef JPH_COMPILER_GCC
  292. #define JPH_PRAGMA(x) _Pragma(#x)
  293. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(GCC diagnostic push)
  294. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(GCC diagnostic pop)
  295. #define JPH_GCC_SUPPRESS_WARNING(w) JPH_PRAGMA(GCC diagnostic ignored w)
  296. #else
  297. #define JPH_GCC_SUPPRESS_WARNING(w)
  298. #endif
  299. #ifdef JPH_COMPILER_MSVC
  300. #define JPH_PRAGMA(x) __pragma(x)
  301. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(warning (push))
  302. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(warning (pop))
  303. #define JPH_MSVC_SUPPRESS_WARNING(w) JPH_PRAGMA(warning (disable : w))
  304. #if _MSC_VER >= 1920 && _MSC_VER < 1930
  305. #define JPH_MSVC2019_SUPPRESS_WARNING(w) JPH_MSVC_SUPPRESS_WARNING(w)
  306. #else
  307. #define JPH_MSVC2019_SUPPRESS_WARNING(w)
  308. #endif
  309. #else
  310. #define JPH_MSVC_SUPPRESS_WARNING(w)
  311. #define JPH_MSVC2019_SUPPRESS_WARNING(w)
  312. #endif
  313. // Disable common warnings triggered by Jolt when compiling with -Wall
  314. #define JPH_SUPPRESS_WARNINGS \
  315. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat") \
  316. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") \
  317. JPH_CLANG_SUPPRESS_WARNING("-Wfloat-equal") \
  318. JPH_CLANG_SUPPRESS_WARNING("-Wsign-conversion") \
  319. JPH_CLANG_SUPPRESS_WARNING("-Wold-style-cast") \
  320. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-anonymous-struct") \
  321. JPH_CLANG_SUPPRESS_WARNING("-Wnested-anon-types") \
  322. JPH_CLANG_SUPPRESS_WARNING("-Wglobal-constructors") \
  323. JPH_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors") \
  324. JPH_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path") \
  325. JPH_CLANG_SUPPRESS_WARNING("-Wlanguage-extension-token") \
  326. JPH_CLANG_SUPPRESS_WARNING("-Wunused-parameter") \
  327. JPH_CLANG_SUPPRESS_WARNING("-Wformat-nonliteral") \
  328. JPH_CLANG_SUPPRESS_WARNING("-Wcovered-switch-default") \
  329. JPH_CLANG_SUPPRESS_WARNING("-Wcast-align") \
  330. JPH_CLANG_SUPPRESS_WARNING("-Winvalid-offsetof") \
  331. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-zero-variadic-macro-arguments") \
  332. JPH_CLANG_SUPPRESS_WARNING("-Wdocumentation-unknown-command") \
  333. JPH_CLANG_SUPPRESS_WARNING("-Wctad-maybe-unsupported") \
  334. JPH_CLANG_SUPPRESS_WARNING("-Wswitch-default") \
  335. JPH_CLANG_13_PLUS_SUPPRESS_WARNING("-Wdeprecated-copy") \
  336. JPH_CLANG_13_PLUS_SUPPRESS_WARNING("-Wdeprecated-copy-with-dtor") \
  337. JPH_CLANG_16_PLUS_SUPPRESS_WARNING("-Wunsafe-buffer-usage") \
  338. JPH_IF_NOT_ANDROID(JPH_CLANG_SUPPRESS_WARNING("-Wimplicit-int-float-conversion")) \
  339. \
  340. JPH_GCC_SUPPRESS_WARNING("-Wcomment") \
  341. JPH_GCC_SUPPRESS_WARNING("-Winvalid-offsetof") \
  342. JPH_GCC_SUPPRESS_WARNING("-Wclass-memaccess") \
  343. JPH_GCC_SUPPRESS_WARNING("-Wpedantic") \
  344. JPH_GCC_SUPPRESS_WARNING("-Wunused-parameter") \
  345. JPH_GCC_SUPPRESS_WARNING("-Wmaybe-uninitialized") \
  346. \
  347. JPH_MSVC_SUPPRESS_WARNING(4619) /* #pragma warning: there is no warning number 'XXXX' */ \
  348. JPH_MSVC_SUPPRESS_WARNING(4514) /* 'X' : unreferenced inline function has been removed */ \
  349. JPH_MSVC_SUPPRESS_WARNING(4710) /* 'X' : function not inlined */ \
  350. JPH_MSVC_SUPPRESS_WARNING(4711) /* function 'X' selected for automatic inline expansion */ \
  351. JPH_MSVC_SUPPRESS_WARNING(4714) /* function 'X' marked as __forceinline not inlined */ \
  352. JPH_MSVC_SUPPRESS_WARNING(4820) /* 'X': 'Y' bytes padding added after data member 'Z' */ \
  353. JPH_MSVC_SUPPRESS_WARNING(4100) /* 'X' : unreferenced formal parameter */ \
  354. JPH_MSVC_SUPPRESS_WARNING(4626) /* 'X' : assignment operator was implicitly defined as deleted because a base class assignment operator is inaccessible or deleted */ \
  355. 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 */ \
  356. JPH_MSVC_SUPPRESS_WARNING(4365) /* 'argument' : conversion from 'X' to 'Y', signed / unsigned mismatch */ \
  357. JPH_MSVC_SUPPRESS_WARNING(4324) /* 'X' : structure was padded due to alignment specifier */ \
  358. JPH_MSVC_SUPPRESS_WARNING(4625) /* 'X' : copy constructor was implicitly defined as deleted because a base class copy constructor is inaccessible or deleted */ \
  359. JPH_MSVC_SUPPRESS_WARNING(5026) /* 'X': move constructor was implicitly defined as deleted because a base class move constructor is inaccessible or deleted */ \
  360. JPH_MSVC_SUPPRESS_WARNING(4623) /* 'X' : default constructor was implicitly defined as deleted */ \
  361. JPH_MSVC_SUPPRESS_WARNING(4201) /* nonstandard extension used: nameless struct/union */ \
  362. 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' */ \
  363. JPH_MSVC_SUPPRESS_WARNING(5045) /* Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified */ \
  364. JPH_MSVC_SUPPRESS_WARNING(4583) /* 'X': destructor is not implicitly called */ \
  365. JPH_MSVC_SUPPRESS_WARNING(4582) /* 'X': constructor is not implicitly called */ \
  366. JPH_MSVC_SUPPRESS_WARNING(5219) /* implicit conversion from 'X' to 'Y', possible loss of data */ \
  367. JPH_MSVC_SUPPRESS_WARNING(4826) /* Conversion from 'X *' to 'JPH::uint64' is sign-extended. This may cause unexpected runtime behavior. (32-bit) */ \
  368. JPH_MSVC_SUPPRESS_WARNING(5264) /* 'X': 'const' variable is not used */ \
  369. JPH_MSVC_SUPPRESS_WARNING(4251) /* class 'X' needs to have DLL-interface to be used by clients of class 'Y' */ \
  370. JPH_MSVC_SUPPRESS_WARNING(4738) /* storing 32-bit float result in memory, possible loss of performance */ \
  371. JPH_MSVC2019_SUPPRESS_WARNING(5246) /* the initialization of a subobject should be wrapped in braces */
  372. // OS-specific includes
  373. #if defined(JPH_PLATFORM_WINDOWS)
  374. #define JPH_BREAKPOINT __debugbreak()
  375. #elif defined(JPH_PLATFORM_BLUE)
  376. // Configuration for a popular game console.
  377. // This file is not distributed because it would violate an NDA.
  378. // Creating one should only be a couple of minutes of work if you have the documentation for the platform
  379. // (you only need to define JPH_BREAKPOINT, JPH_PLATFORM_BLUE_GET_TICKS, JPH_PLATFORM_BLUE_MUTEX*, JPH_PLATFORM_BLUE_RWLOCK*, JPH_PLATFORM_BLUE_SEMAPHORE* and include the right header).
  380. #include <Jolt/Core/PlatformBlue.h>
  381. #elif defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS) || defined(JPH_PLATFORM_BSD)
  382. #if defined(JPH_CPU_X86)
  383. #define JPH_BREAKPOINT __asm volatile ("int $0x3")
  384. #elif defined(JPH_CPU_ARM) || defined(JPH_CPU_RISCV) || defined(JPH_CPU_E2K) || defined(JPH_CPU_PPC) || defined(JPH_CPU_LOONGARCH)
  385. #define JPH_BREAKPOINT __builtin_trap()
  386. #else
  387. #error Unknown CPU architecture
  388. #endif
  389. #elif defined(JPH_PLATFORM_WASM)
  390. #define JPH_BREAKPOINT do { } while (false) // Not supported
  391. #else
  392. #error Unknown platform
  393. #endif
  394. // Begin the JPH namespace
  395. #define JPH_NAMESPACE_BEGIN \
  396. JPH_SUPPRESS_WARNING_PUSH \
  397. JPH_SUPPRESS_WARNINGS \
  398. namespace JPH {
  399. // End the JPH namespace
  400. #define JPH_NAMESPACE_END \
  401. } \
  402. JPH_SUPPRESS_WARNING_POP
  403. // Suppress warnings generated by the standard template library
  404. #define JPH_SUPPRESS_WARNINGS_STD_BEGIN \
  405. JPH_SUPPRESS_WARNING_PUSH \
  406. JPH_MSVC_SUPPRESS_WARNING(4365) \
  407. JPH_MSVC_SUPPRESS_WARNING(4619) \
  408. JPH_MSVC_SUPPRESS_WARNING(4710) \
  409. JPH_MSVC_SUPPRESS_WARNING(4711) \
  410. JPH_MSVC_SUPPRESS_WARNING(4820) \
  411. JPH_MSVC_SUPPRESS_WARNING(4514) \
  412. JPH_MSVC_SUPPRESS_WARNING(5262) \
  413. JPH_MSVC_SUPPRESS_WARNING(5264) \
  414. JPH_MSVC_SUPPRESS_WARNING(4738) \
  415. JPH_MSVC_SUPPRESS_WARNING(5045)
  416. #define JPH_SUPPRESS_WARNINGS_STD_END \
  417. JPH_SUPPRESS_WARNING_POP
  418. // Standard C++ includes
  419. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  420. #include <float.h>
  421. #include <limits.h>
  422. #include <string.h>
  423. #include <utility>
  424. #include <cmath>
  425. #include <sstream>
  426. #include <functional>
  427. #include <algorithm>
  428. #include <cstdint>
  429. #ifdef JPH_COMPILER_MSVC
  430. #include <malloc.h> // for alloca
  431. #endif
  432. #if defined(JPH_USE_SSE)
  433. #include <immintrin.h>
  434. #elif defined(JPH_USE_NEON)
  435. #ifdef JPH_COMPILER_MSVC
  436. #include <intrin.h>
  437. #include <arm64_neon.h>
  438. #else
  439. #include <arm_neon.h>
  440. #endif
  441. #endif
  442. JPH_SUPPRESS_WARNINGS_STD_END
  443. JPH_NAMESPACE_BEGIN
  444. // Commonly used STL types
  445. using std::min;
  446. using std::max;
  447. using std::abs;
  448. using std::sqrt;
  449. using std::ceil;
  450. using std::floor;
  451. using std::trunc;
  452. using std::round;
  453. using std::fmod;
  454. using std::string_view;
  455. using std::function;
  456. using std::numeric_limits;
  457. using std::isfinite;
  458. using std::isnan;
  459. using std::ostream;
  460. using std::istream;
  461. // Standard types
  462. using uint = unsigned int;
  463. using uint8 = std::uint8_t;
  464. using uint16 = std::uint16_t;
  465. using uint32 = std::uint32_t;
  466. using uint64 = std::uint64_t;
  467. // Assert sizes of types
  468. static_assert(sizeof(uint) >= 4, "Invalid size of uint");
  469. static_assert(sizeof(uint8) == 1, "Invalid size of uint8");
  470. static_assert(sizeof(uint16) == 2, "Invalid size of uint16");
  471. static_assert(sizeof(uint32) == 4, "Invalid size of uint32");
  472. static_assert(sizeof(uint64) == 8, "Invalid size of uint64");
  473. static_assert(sizeof(void *) == (JPH_CPU_ADDRESS_BITS == 64? 8 : 4), "Invalid size of pointer" );
  474. // Determine if we want extra debugging code to be active
  475. #if !defined(NDEBUG) && !defined(JPH_NO_DEBUG)
  476. #define JPH_DEBUG
  477. #endif
  478. // Define inline macro
  479. #if defined(JPH_NO_FORCE_INLINE)
  480. #define JPH_INLINE inline
  481. #elif defined(JPH_COMPILER_CLANG)
  482. #define JPH_INLINE __inline__ __attribute__((always_inline))
  483. #elif defined(JPH_COMPILER_GCC)
  484. // On gcc 14 using always_inline in debug mode causes error: "inlining failed in call to 'always_inline' 'XXX': function not considered for inlining"
  485. // See: https://github.com/jrouwe/JoltPhysics/issues/1096
  486. #if __GNUC__ >= 14 && defined(JPH_DEBUG)
  487. #define JPH_INLINE inline
  488. #else
  489. #define JPH_INLINE __inline__ __attribute__((always_inline))
  490. #endif
  491. #elif defined(JPH_COMPILER_MSVC)
  492. #define JPH_INLINE __forceinline
  493. #else
  494. #error Undefined
  495. #endif
  496. // Cache line size (used for aligning to cache line)
  497. #ifndef JPH_CACHE_LINE_SIZE
  498. #define JPH_CACHE_LINE_SIZE 64
  499. #endif
  500. // Define macro to get current function name
  501. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  502. #define JPH_FUNCTION_NAME __PRETTY_FUNCTION__
  503. #elif defined(JPH_COMPILER_MSVC)
  504. #define JPH_FUNCTION_NAME __FUNCTION__
  505. #else
  506. #error Undefined
  507. #endif
  508. // Stack allocation
  509. #define JPH_STACK_ALLOC(n) alloca(n)
  510. // Shorthand for #ifdef JPH_DEBUG / #endif
  511. #ifdef JPH_DEBUG
  512. #define JPH_IF_DEBUG(...) __VA_ARGS__
  513. #define JPH_IF_NOT_DEBUG(...)
  514. #else
  515. #define JPH_IF_DEBUG(...)
  516. #define JPH_IF_NOT_DEBUG(...) __VA_ARGS__
  517. #endif
  518. // Shorthand for #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED / #endif
  519. #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED
  520. #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...) __VA_ARGS__
  521. #else
  522. #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...)
  523. #endif
  524. // Helper macros to detect if we're running in single or double precision mode
  525. #ifdef JPH_DOUBLE_PRECISION
  526. #define JPH_IF_SINGLE_PRECISION(...)
  527. #define JPH_IF_SINGLE_PRECISION_ELSE(s, d) d
  528. #define JPH_IF_DOUBLE_PRECISION(...) __VA_ARGS__
  529. #else
  530. #define JPH_IF_SINGLE_PRECISION(...) __VA_ARGS__
  531. #define JPH_IF_SINGLE_PRECISION_ELSE(s, d) s
  532. #define JPH_IF_DOUBLE_PRECISION(...)
  533. #endif
  534. // Helper macro to detect if the debug renderer is active
  535. #ifdef JPH_DEBUG_RENDERER
  536. #define JPH_IF_DEBUG_RENDERER(...) __VA_ARGS__
  537. #define JPH_IF_NOT_DEBUG_RENDERER(...)
  538. #else
  539. #define JPH_IF_DEBUG_RENDERER(...)
  540. #define JPH_IF_NOT_DEBUG_RENDERER(...) __VA_ARGS__
  541. #endif
  542. // Macro to indicate that a parameter / variable is unused
  543. #define JPH_UNUSED(x) (void)x
  544. // Macro to enable floating point precise mode and to disable fused multiply add instructions
  545. #if defined(JPH_COMPILER_GCC) || defined(JPH_CROSS_PLATFORM_DETERMINISTIC)
  546. // We compile without -ffast-math and -ffp-contract=fast, so we don't need to disable anything
  547. #define JPH_PRECISE_MATH_ON
  548. #define JPH_PRECISE_MATH_OFF
  549. #elif defined(JPH_COMPILER_CLANG)
  550. // 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)
  551. // 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
  552. #if (defined(JPH_CPU_ARM) && !defined(JPH_PLATFORM_ANDROID) && __clang_major__ >= 16) || (defined(JPH_CPU_X86) && __clang_major__ >= 14)
  553. #define JPH_PRECISE_MATH_ON \
  554. _Pragma("float_control(precise, on, push)") \
  555. _Pragma("clang fp contract(off)")
  556. #define JPH_PRECISE_MATH_OFF \
  557. _Pragma("float_control(pop)")
  558. #elif __clang_major__ >= 14 && (defined(JPH_USE_FMADD) || defined(FP_FAST_FMA))
  559. #define JPH_PRECISE_MATH_ON \
  560. _Pragma("clang fp contract(off)")
  561. #define JPH_PRECISE_MATH_OFF \
  562. _Pragma("clang fp contract(on)")
  563. #else
  564. #define JPH_PRECISE_MATH_ON
  565. #define JPH_PRECISE_MATH_OFF
  566. #endif
  567. #elif defined(JPH_COMPILER_MSVC)
  568. // 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
  569. #define JPH_PRECISE_MATH_ON \
  570. __pragma(float_control(precise, on, push)) \
  571. __pragma(fp_contract(off))
  572. #define JPH_PRECISE_MATH_OFF \
  573. __pragma(fp_contract(on)) \
  574. __pragma(float_control(pop))
  575. #else
  576. #error Undefined
  577. #endif
  578. // Check if Thread Sanitizer is enabled
  579. #ifdef __has_feature
  580. #if __has_feature(thread_sanitizer)
  581. #define JPH_TSAN_ENABLED
  582. #endif
  583. #else
  584. #ifdef __SANITIZE_THREAD__
  585. #define JPH_TSAN_ENABLED
  586. #endif
  587. #endif
  588. // Attribute to disable Thread Sanitizer for a particular function
  589. #ifdef JPH_TSAN_ENABLED
  590. #define JPH_TSAN_NO_SANITIZE __attribute__((no_sanitize("thread")))
  591. #else
  592. #define JPH_TSAN_NO_SANITIZE
  593. #endif
  594. JPH_NAMESPACE_END