Core.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. #if defined(JPH_COMPILER_GCC)
  185. // Prevents an issue with GCC attribute parsing (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69585)
  186. #define JPH_EXPORT_GCC_BUG_WORKAROUND [[gnu::visibility("default")]]
  187. #endif
  188. #endif
  189. #else
  190. // When linking against Jolt, we must import these symbols
  191. #ifdef JPH_PLATFORM_WINDOWS
  192. #define JPH_EXPORT __declspec(dllimport)
  193. #else
  194. #define JPH_EXPORT __attribute__ ((visibility ("default")))
  195. #if defined(JPH_COMPILER_GCC)
  196. // Prevents an issue with GCC attribute parsing (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69585)
  197. #define JPH_EXPORT_GCC_BUG_WORKAROUND [[gnu::visibility("default")]]
  198. #endif
  199. #endif
  200. #endif
  201. #else
  202. // If the define is not set, we use static linking and symbols don't need to be imported or exported
  203. #define JPH_EXPORT
  204. #endif
  205. #ifndef JPH_EXPORT_GCC_BUG_WORKAROUND
  206. #define JPH_EXPORT_GCC_BUG_WORKAROUND JPH_EXPORT
  207. #endif
  208. // Macro used by the RTTI macros to not export a function
  209. #define JPH_NO_EXPORT
  210. // Pragmas to store / restore the warning state and to disable individual warnings
  211. #ifdef JPH_COMPILER_CLANG
  212. #define JPH_PRAGMA(x) _Pragma(#x)
  213. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(clang diagnostic push)
  214. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(clang diagnostic pop)
  215. #define JPH_CLANG_SUPPRESS_WARNING(w) JPH_PRAGMA(clang diagnostic ignored w)
  216. #if __clang_major__ >= 13
  217. #define JPH_CLANG_13_PLUS_SUPPRESS_WARNING(w) JPH_CLANG_SUPPRESS_WARNING(w)
  218. #else
  219. #define JPH_CLANG_13_PLUS_SUPPRESS_WARNING(w)
  220. #endif
  221. #if __clang_major__ >= 16
  222. #define JPH_CLANG_16_PLUS_SUPPRESS_WARNING(w) JPH_CLANG_SUPPRESS_WARNING(w)
  223. #else
  224. #define JPH_CLANG_16_PLUS_SUPPRESS_WARNING(w)
  225. #endif
  226. #else
  227. #define JPH_CLANG_SUPPRESS_WARNING(w)
  228. #define JPH_CLANG_13_PLUS_SUPPRESS_WARNING(w)
  229. #define JPH_CLANG_16_PLUS_SUPPRESS_WARNING(w)
  230. #endif
  231. #ifdef JPH_COMPILER_GCC
  232. #define JPH_PRAGMA(x) _Pragma(#x)
  233. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(GCC diagnostic push)
  234. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(GCC diagnostic pop)
  235. #define JPH_GCC_SUPPRESS_WARNING(w) JPH_PRAGMA(GCC diagnostic ignored w)
  236. #else
  237. #define JPH_GCC_SUPPRESS_WARNING(w)
  238. #endif
  239. #ifdef JPH_COMPILER_MSVC
  240. #define JPH_PRAGMA(x) __pragma(x)
  241. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(warning (push))
  242. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(warning (pop))
  243. #define JPH_MSVC_SUPPRESS_WARNING(w) JPH_PRAGMA(warning (disable : w))
  244. #if _MSC_VER >= 1920 && _MSC_VER < 1930
  245. #define JPH_MSVC2019_SUPPRESS_WARNING(w) JPH_MSVC_SUPPRESS_WARNING(w)
  246. #else
  247. #define JPH_MSVC2019_SUPPRESS_WARNING(w)
  248. #endif
  249. #else
  250. #define JPH_MSVC_SUPPRESS_WARNING(w)
  251. #define JPH_MSVC2019_SUPPRESS_WARNING(w)
  252. #endif
  253. // Disable common warnings triggered by Jolt when compiling with -Wall
  254. #define JPH_SUPPRESS_WARNINGS \
  255. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat") \
  256. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") \
  257. JPH_CLANG_SUPPRESS_WARNING("-Wfloat-equal") \
  258. JPH_CLANG_SUPPRESS_WARNING("-Wsign-conversion") \
  259. JPH_CLANG_SUPPRESS_WARNING("-Wold-style-cast") \
  260. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-anonymous-struct") \
  261. JPH_CLANG_SUPPRESS_WARNING("-Wnested-anon-types") \
  262. JPH_CLANG_SUPPRESS_WARNING("-Wglobal-constructors") \
  263. JPH_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors") \
  264. JPH_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path") \
  265. JPH_CLANG_SUPPRESS_WARNING("-Wlanguage-extension-token") \
  266. JPH_CLANG_SUPPRESS_WARNING("-Wunused-parameter") \
  267. JPH_CLANG_SUPPRESS_WARNING("-Wformat-nonliteral") \
  268. JPH_CLANG_SUPPRESS_WARNING("-Wcovered-switch-default") \
  269. JPH_CLANG_SUPPRESS_WARNING("-Wcast-align") \
  270. JPH_CLANG_SUPPRESS_WARNING("-Winvalid-offsetof") \
  271. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-zero-variadic-macro-arguments") \
  272. JPH_CLANG_SUPPRESS_WARNING("-Wdocumentation-unknown-command") \
  273. JPH_CLANG_SUPPRESS_WARNING("-Wctad-maybe-unsupported") \
  274. JPH_CLANG_13_PLUS_SUPPRESS_WARNING("-Wdeprecated-copy") \
  275. JPH_CLANG_13_PLUS_SUPPRESS_WARNING("-Wdeprecated-copy-with-dtor") \
  276. JPH_CLANG_16_PLUS_SUPPRESS_WARNING("-Wunsafe-buffer-usage") \
  277. JPH_IF_NOT_ANDROID(JPH_CLANG_SUPPRESS_WARNING("-Wimplicit-int-float-conversion")) \
  278. \
  279. JPH_GCC_SUPPRESS_WARNING("-Wcomment") \
  280. JPH_GCC_SUPPRESS_WARNING("-Winvalid-offsetof") \
  281. JPH_GCC_SUPPRESS_WARNING("-Wclass-memaccess") \
  282. \
  283. JPH_MSVC_SUPPRESS_WARNING(4619) /* #pragma warning: there is no warning number 'XXXX' */ \
  284. JPH_MSVC_SUPPRESS_WARNING(4514) /* 'X' : unreferenced inline function has been removed */ \
  285. JPH_MSVC_SUPPRESS_WARNING(4710) /* 'X' : function not inlined */ \
  286. JPH_MSVC_SUPPRESS_WARNING(4711) /* function 'X' selected for automatic inline expansion */ \
  287. JPH_MSVC_SUPPRESS_WARNING(4820) /* 'X': 'Y' bytes padding added after data member 'Z' */ \
  288. JPH_MSVC_SUPPRESS_WARNING(4100) /* 'X' : unreferenced formal parameter */ \
  289. JPH_MSVC_SUPPRESS_WARNING(4626) /* 'X' : assignment operator was implicitly defined as deleted because a base class assignment operator is inaccessible or deleted */ \
  290. 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 */ \
  291. JPH_MSVC_SUPPRESS_WARNING(4365) /* 'argument' : conversion from 'X' to 'Y', signed / unsigned mismatch */ \
  292. JPH_MSVC_SUPPRESS_WARNING(4324) /* 'X' : structure was padded due to alignment specifier */ \
  293. JPH_MSVC_SUPPRESS_WARNING(4625) /* 'X' : copy constructor was implicitly defined as deleted because a base class copy constructor is inaccessible or deleted */ \
  294. JPH_MSVC_SUPPRESS_WARNING(5026) /* 'X': move constructor was implicitly defined as deleted because a base class move constructor is inaccessible or deleted */ \
  295. JPH_MSVC_SUPPRESS_WARNING(4623) /* 'X' : default constructor was implicitly defined as deleted */ \
  296. JPH_MSVC_SUPPRESS_WARNING(4201) /* nonstandard extension used: nameless struct/union */ \
  297. 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' */ \
  298. JPH_MSVC_SUPPRESS_WARNING(5045) /* Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified */ \
  299. JPH_MSVC_SUPPRESS_WARNING(4583) /* 'X': destructor is not implicitly called */ \
  300. JPH_MSVC_SUPPRESS_WARNING(4582) /* 'X': constructor is not implicitly called */ \
  301. JPH_MSVC_SUPPRESS_WARNING(5219) /* implicit conversion from 'X' to 'Y', possible loss of data */ \
  302. JPH_MSVC_SUPPRESS_WARNING(4826) /* Conversion from 'X *' to 'JPH::uint64' is sign-extended. This may cause unexpected runtime behavior. (32-bit) */ \
  303. JPH_MSVC_SUPPRESS_WARNING(5264) /* 'X': 'const' variable is not used */ \
  304. JPH_MSVC_SUPPRESS_WARNING(4251) /* class 'X' needs to have DLL-interface to be used by clients of class 'Y' */ \
  305. JPH_MSVC_SUPPRESS_WARNING(4738) /* storing 32-bit float result in memory, possible loss of performance */ \
  306. JPH_MSVC2019_SUPPRESS_WARNING(5246) /* the initialization of a subobject should be wrapped in braces */
  307. // OS-specific includes
  308. #if defined(JPH_PLATFORM_WINDOWS)
  309. #define JPH_BREAKPOINT __debugbreak()
  310. #elif defined(JPH_PLATFORM_BLUE)
  311. // Configuration for a popular game console.
  312. // This file is not distributed because it would violate an NDA.
  313. // Creating one should only be a couple of minutes of work if you have the documentation for the platform
  314. // (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).
  315. #include <Jolt/Core/PlatformBlue.h>
  316. #elif defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS)
  317. #if defined(JPH_CPU_X86)
  318. #define JPH_BREAKPOINT __asm volatile ("int $0x3")
  319. #elif defined(JPH_CPU_ARM)
  320. #define JPH_BREAKPOINT __builtin_trap()
  321. #endif
  322. #elif defined(JPH_PLATFORM_WASM)
  323. #define JPH_BREAKPOINT do { } while (false) // Not supported
  324. #else
  325. #error Unknown platform
  326. #endif
  327. // Crashes the application
  328. #define JPH_CRASH do { int *ptr = nullptr; *ptr = 0; } while (false)
  329. // Begin the JPH namespace
  330. #define JPH_NAMESPACE_BEGIN \
  331. JPH_SUPPRESS_WARNING_PUSH \
  332. JPH_SUPPRESS_WARNINGS \
  333. namespace JPH {
  334. // End the JPH namespace
  335. #define JPH_NAMESPACE_END \
  336. } \
  337. JPH_SUPPRESS_WARNING_POP
  338. // Suppress warnings generated by the standard template library
  339. #define JPH_SUPPRESS_WARNINGS_STD_BEGIN \
  340. JPH_SUPPRESS_WARNING_PUSH \
  341. JPH_MSVC_SUPPRESS_WARNING(4365) \
  342. JPH_MSVC_SUPPRESS_WARNING(4619) \
  343. JPH_MSVC_SUPPRESS_WARNING(4710) \
  344. JPH_MSVC_SUPPRESS_WARNING(4711) \
  345. JPH_MSVC_SUPPRESS_WARNING(4820) \
  346. JPH_MSVC_SUPPRESS_WARNING(4514) \
  347. JPH_MSVC_SUPPRESS_WARNING(5262) \
  348. JPH_MSVC_SUPPRESS_WARNING(5264) \
  349. JPH_MSVC_SUPPRESS_WARNING(4738)
  350. #define JPH_SUPPRESS_WARNINGS_STD_END \
  351. JPH_SUPPRESS_WARNING_POP
  352. // Standard C++ includes
  353. #include <float.h>
  354. #include <limits.h>
  355. #include <string.h>
  356. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  357. #include <vector>
  358. #include <utility>
  359. #include <cmath>
  360. #include <sstream>
  361. #include <functional>
  362. #include <algorithm>
  363. #include <cstdint>
  364. JPH_SUPPRESS_WARNINGS_STD_END
  365. #if defined(JPH_USE_SSE)
  366. #include <immintrin.h>
  367. #elif defined(JPH_USE_NEON)
  368. #ifdef JPH_COMPILER_MSVC
  369. #include <intrin.h>
  370. #include <arm64_neon.h>
  371. #else
  372. #include <arm_neon.h>
  373. #endif
  374. #endif
  375. JPH_NAMESPACE_BEGIN
  376. // Commonly used STL types
  377. using std::pair;
  378. using std::min;
  379. using std::max;
  380. using std::abs;
  381. using std::sqrt;
  382. using std::ceil;
  383. using std::floor;
  384. using std::trunc;
  385. using std::round;
  386. using std::fmod;
  387. using std::swap;
  388. using std::size;
  389. using std::string;
  390. using std::string_view;
  391. using std::function;
  392. using std::numeric_limits;
  393. using std::isfinite;
  394. using std::isnan;
  395. using std::is_trivial;
  396. using std::is_trivially_constructible;
  397. using std::is_trivially_destructible;
  398. using std::ostream;
  399. using std::istream;
  400. // Standard types
  401. using uint = unsigned int;
  402. using uint8 = std::uint8_t;
  403. using uint16 = std::uint16_t;
  404. using uint32 = std::uint32_t;
  405. using uint64 = std::uint64_t;
  406. // Assert sizes of types
  407. static_assert(sizeof(uint) >= 4, "Invalid size of uint");
  408. static_assert(sizeof(uint8) == 1, "Invalid size of uint8");
  409. static_assert(sizeof(uint16) == 2, "Invalid size of uint16");
  410. static_assert(sizeof(uint32) == 4, "Invalid size of uint32");
  411. static_assert(sizeof(uint64) == 8, "Invalid size of uint64");
  412. static_assert(sizeof(void *) == (JPH_CPU_ADDRESS_BITS == 64? 8 : 4), "Invalid size of pointer" );
  413. // Define inline macro
  414. #if defined(JPH_NO_FORCE_INLINE)
  415. #define JPH_INLINE inline
  416. #elif defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  417. #define JPH_INLINE __inline__ __attribute__((always_inline))
  418. #elif defined(JPH_COMPILER_MSVC)
  419. #define JPH_INLINE __forceinline
  420. #else
  421. #error Undefined
  422. #endif
  423. // Cache line size (used for aligning to cache line)
  424. #ifndef JPH_CACHE_LINE_SIZE
  425. #define JPH_CACHE_LINE_SIZE 64
  426. #endif
  427. // Define macro to get current function name
  428. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  429. #define JPH_FUNCTION_NAME __PRETTY_FUNCTION__
  430. #elif defined(JPH_COMPILER_MSVC)
  431. #define JPH_FUNCTION_NAME __FUNCTION__
  432. #else
  433. #error Undefined
  434. #endif
  435. // Stack allocation
  436. #define JPH_STACK_ALLOC(n) alloca(n)
  437. // Shorthand for #ifdef _DEBUG / #endif
  438. #ifdef _DEBUG
  439. #define JPH_IF_DEBUG(...) __VA_ARGS__
  440. #define JPH_IF_NOT_DEBUG(...)
  441. #else
  442. #define JPH_IF_DEBUG(...)
  443. #define JPH_IF_NOT_DEBUG(...) __VA_ARGS__
  444. #endif
  445. // Shorthand for #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED / #endif
  446. #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED
  447. #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...) __VA_ARGS__
  448. #else
  449. #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...)
  450. #endif
  451. // Helper macros to detect if we're running in single or double precision mode
  452. #ifdef JPH_DOUBLE_PRECISION
  453. #define JPH_IF_SINGLE_PRECISION(...)
  454. #define JPH_IF_SINGLE_PRECISION_ELSE(s, d) d
  455. #define JPH_IF_DOUBLE_PRECISION(...) __VA_ARGS__
  456. #else
  457. #define JPH_IF_SINGLE_PRECISION(...) __VA_ARGS__
  458. #define JPH_IF_SINGLE_PRECISION_ELSE(s, d) s
  459. #define JPH_IF_DOUBLE_PRECISION(...)
  460. #endif
  461. // Helper macro to detect if the debug renderer is active
  462. #ifdef JPH_DEBUG_RENDERER
  463. #define JPH_IF_DEBUG_RENDERER(...) __VA_ARGS__
  464. #define JPH_IF_NOT_DEBUG_RENDERER(...)
  465. #else
  466. #define JPH_IF_DEBUG_RENDERER(...)
  467. #define JPH_IF_NOT_DEBUG_RENDERER(...) __VA_ARGS__
  468. #endif
  469. // Macro to indicate that a parameter / variable is unused
  470. #define JPH_UNUSED(x) (void)x
  471. // Macro to enable floating point precise mode and to disable fused multiply add instructions
  472. #if defined(JPH_COMPILER_GCC) || defined(JPH_CROSS_PLATFORM_DETERMINISTIC)
  473. // We compile without -ffast-math and -ffp-contract=fast, so we don't need to disable anything
  474. #define JPH_PRECISE_MATH_ON
  475. #define JPH_PRECISE_MATH_OFF
  476. #elif defined(JPH_COMPILER_CLANG)
  477. // 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)
  478. // 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
  479. #if (defined(JPH_CPU_ARM) && !defined(JPH_PLATFORM_ANDROID) && __clang_major__ >= 16) || (defined(JPH_CPU_X86) && __clang_major__ >= 14)
  480. #define JPH_PRECISE_MATH_ON \
  481. _Pragma("float_control(precise, on, push)") \
  482. _Pragma("clang fp contract(off)")
  483. #define JPH_PRECISE_MATH_OFF \
  484. _Pragma("float_control(pop)")
  485. #elif __clang_major__ >= 14 && (defined(JPH_USE_FMADD) || defined(FP_FAST_FMA))
  486. #define JPH_PRECISE_MATH_ON \
  487. _Pragma("clang fp contract(off)")
  488. #define JPH_PRECISE_MATH_OFF \
  489. _Pragma("clang fp contract(on)")
  490. #else
  491. #define JPH_PRECISE_MATH_ON
  492. #define JPH_PRECISE_MATH_OFF
  493. #endif
  494. #elif defined(JPH_COMPILER_MSVC)
  495. // 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
  496. #define JPH_PRECISE_MATH_ON \
  497. __pragma(float_control(precise, on, push)) \
  498. __pragma(fp_contract(off))
  499. #define JPH_PRECISE_MATH_OFF \
  500. __pragma(fp_contract(on)) \
  501. __pragma(float_control(pop))
  502. #else
  503. #error Undefined
  504. #endif
  505. JPH_NAMESPACE_END