Core.h 19 KB

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