Core.h 21 KB

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