Core.h 15 KB

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