Core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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(__MINGW64__) || defined (__MINGW32__)
  37. #define JPH_COMPILER_GCC
  38. #define JPH_COMPILER_MINGW
  39. #elif defined(__GNUC__)
  40. #define JPH_COMPILER_GCC
  41. #elif defined(_MSC_VER)
  42. #define JPH_COMPILER_MSVC
  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. // Detect enabled instruction sets
  55. #if (defined(__F16C__) || defined(__AVX2__)) && !defined(JPH_USE_F16C)
  56. #define JPH_USE_F16C
  57. #endif
  58. #if (defined(__LZCNT__) || defined(__AVX2__)) && !defined(JPH_USE_LZCNT)
  59. #define JPH_USE_LZCNT
  60. #endif
  61. #if (defined(__BMI__) || defined(__AVX2__)) && !defined(JPH_USE_TZCNT)
  62. #define JPH_USE_TZCNT
  63. #endif
  64. #if (defined(__SSE4_1__) || defined(__AVX__)) && !defined(JPH_USE_SSE4_1)
  65. #define JPH_USE_SSE4_1
  66. #endif
  67. #if (defined(__SSE4_2__) || defined(__AVX__)) && !defined(JPH_USE_SSE4_2)
  68. #define JPH_USE_SSE4_2
  69. #endif
  70. #if defined(__AVX__) && !defined(JPH_USE_AVX)
  71. #define JPH_USE_AVX
  72. #endif
  73. #if defined(__AVX2__) && !defined(JPH_USE_AVX2)
  74. #define JPH_USE_AVX2
  75. #endif
  76. #if defined(__AVX512F__) && defined(__AVX512VL__) && defined(__AVX512DQ__) && !defined(JPH_USE_AVX512)
  77. #define JPH_USE_AVX512
  78. #endif
  79. #ifndef JPH_CROSS_PLATFORM_DETERMINISTIC // FMA is not compatible with cross platform determinism
  80. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  81. #if defined(__FMA__) && !defined(JPH_USE_FMADD)
  82. #define JPH_USE_FMADD
  83. #endif
  84. #elif defined(JPH_COMPILER_MSVC)
  85. #if defined(__AVX2__) && !defined(JPH_USE_FMADD) // AVX2 also enables fused multiply add
  86. #define JPH_USE_FMADD
  87. #endif
  88. #else
  89. #error Undefined compiler
  90. #endif
  91. #endif
  92. #elif defined(__aarch64__) || defined(_M_ARM64)
  93. // ARM64 CPU architecture
  94. #define JPH_CPU_ARM64
  95. #define JPH_USE_NEON
  96. #define JPH_CPU_ADDRESS_BITS 64
  97. #elif defined(JPH_PLATFORM_WASM)
  98. // WebAssembly CPU architecture
  99. #define JPH_CPU_WASM
  100. #define JPH_CPU_ADDRESS_BITS 32
  101. #define JPH_DISABLE_CUSTOM_ALLOCATOR
  102. #else
  103. #error Unsupported CPU architecture
  104. #endif
  105. // Pragmas to store / restore the warning state and to disable individual warnings
  106. #ifdef JPH_COMPILER_CLANG
  107. #define JPH_PRAGMA(x) _Pragma(#x)
  108. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(clang diagnostic push)
  109. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(clang diagnostic pop)
  110. #define JPH_CLANG_SUPPRESS_WARNING(w) JPH_PRAGMA(clang diagnostic ignored w)
  111. #if __clang_major__ >= 15
  112. #define JPH_CLANG15_SUPPRESS_WARNING(w) JPH_PRAGMA(clang diagnostic ignored w)
  113. #else
  114. #define JPH_CLANG15_SUPPRESS_WARNING(w)
  115. #endif
  116. #else
  117. #define JPH_CLANG_SUPPRESS_WARNING(w)
  118. #define JPH_CLANG15_SUPPRESS_WARNING(w)
  119. #endif
  120. #ifdef JPH_COMPILER_GCC
  121. #define JPH_PRAGMA(x) _Pragma(#x)
  122. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(GCC diagnostic push)
  123. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(GCC diagnostic pop)
  124. #define JPH_GCC_SUPPRESS_WARNING(w) JPH_PRAGMA(GCC diagnostic ignored w)
  125. #else
  126. #define JPH_GCC_SUPPRESS_WARNING(w)
  127. #endif
  128. #ifdef JPH_COMPILER_MSVC
  129. #define JPH_PRAGMA(x) __pragma(x)
  130. #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(warning (push))
  131. #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(warning (pop))
  132. #define JPH_MSVC_SUPPRESS_WARNING(w) JPH_PRAGMA(warning (disable : w))
  133. #else
  134. #define JPH_MSVC_SUPPRESS_WARNING(w)
  135. #endif
  136. // Disable common warnings triggered by Jolt when compiling with -Wall
  137. #define JPH_SUPPRESS_WARNINGS \
  138. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat") \
  139. JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") \
  140. JPH_CLANG_SUPPRESS_WARNING("-Wfloat-equal") \
  141. JPH_CLANG_SUPPRESS_WARNING("-Wsign-conversion") \
  142. JPH_CLANG_SUPPRESS_WARNING("-Wold-style-cast") \
  143. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-anonymous-struct") \
  144. JPH_CLANG_SUPPRESS_WARNING("-Wnested-anon-types") \
  145. JPH_CLANG_SUPPRESS_WARNING("-Wglobal-constructors") \
  146. JPH_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors") \
  147. JPH_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path") \
  148. JPH_CLANG_SUPPRESS_WARNING("-Wlanguage-extension-token") \
  149. JPH_CLANG_SUPPRESS_WARNING("-Wunused-parameter") \
  150. JPH_CLANG_SUPPRESS_WARNING("-Wformat-nonliteral") \
  151. JPH_CLANG_SUPPRESS_WARNING("-Wcovered-switch-default") \
  152. JPH_CLANG_SUPPRESS_WARNING("-Wcast-align") \
  153. JPH_CLANG_SUPPRESS_WARNING("-Winvalid-offsetof") \
  154. JPH_CLANG_SUPPRESS_WARNING("-Wgnu-zero-variadic-macro-arguments") \
  155. JPH_CLANG_SUPPRESS_WARNING("-Wdocumentation-unknown-command") \
  156. JPH_CLANG_SUPPRESS_WARNING("-Wctad-maybe-unsupported") \
  157. JPH_CLANG_SUPPRESS_WARNING("-Wdeprecated-copy") \
  158. JPH_CLANG15_SUPPRESS_WARNING("-Wunqualified-std-cast-call") \
  159. JPH_IF_NOT_ANDROID(JPH_CLANG_SUPPRESS_WARNING("-Wimplicit-int-float-conversion")) \
  160. \
  161. JPH_GCC_SUPPRESS_WARNING("-Wcomment") \
  162. JPH_GCC_SUPPRESS_WARNING("-Winvalid-offsetof") \
  163. JPH_GCC_SUPPRESS_WARNING("-Wclass-memaccess") \
  164. \
  165. JPH_MSVC_SUPPRESS_WARNING(4514) /* 'X' : unreferenced inline function has been removed */ \
  166. JPH_MSVC_SUPPRESS_WARNING(4710) /* 'X' : function not inlined */ \
  167. JPH_MSVC_SUPPRESS_WARNING(4711) /* function 'X' selected for automatic inline expansion */ \
  168. JPH_MSVC_SUPPRESS_WARNING(4820) /* 'X': 'Y' bytes padding added after data member 'Z' */ \
  169. JPH_MSVC_SUPPRESS_WARNING(4100) /* 'X' : unreferenced formal parameter */ \
  170. JPH_MSVC_SUPPRESS_WARNING(4626) /* 'X' : assignment operator was implicitly defined as deleted because a base class assignment operator is inaccessible or deleted */ \
  171. 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 */ \
  172. JPH_MSVC_SUPPRESS_WARNING(4365) /* 'argument' : conversion from 'X' to 'Y', signed / unsigned mismatch */ \
  173. JPH_MSVC_SUPPRESS_WARNING(4324) /* 'X' : structure was padded due to alignment specifier */ \
  174. JPH_MSVC_SUPPRESS_WARNING(4625) /* 'X' : copy constructor was implicitly defined as deleted because a base class copy constructor is inaccessible or deleted */ \
  175. JPH_MSVC_SUPPRESS_WARNING(5026) /* 'X': move constructor was implicitly defined as deleted because a base class move constructor is inaccessible or deleted */ \
  176. JPH_MSVC_SUPPRESS_WARNING(4623) /* 'X' : default constructor was implicitly defined as deleted */ \
  177. JPH_MSVC_SUPPRESS_WARNING(4201) /* nonstandard extension used: nameless struct/union */ \
  178. 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' */ \
  179. JPH_MSVC_SUPPRESS_WARNING(5045) /* Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified */ \
  180. JPH_MSVC_SUPPRESS_WARNING(4583) /* 'X': destructor is not implicitly called */ \
  181. JPH_MSVC_SUPPRESS_WARNING(4582) /* 'X': constructor is not implicitly called */ \
  182. JPH_MSVC_SUPPRESS_WARNING(5219) /* implicit conversion from 'X' to 'Y', possible loss of data */ \
  183. JPH_MSVC_SUPPRESS_WARNING(4826) /* Conversion from 'X *' to 'JPH::uint64' is sign-extended. This may cause unexpected runtime behavior. (32-bit) */
  184. // OS-specific includes
  185. #if defined(JPH_PLATFORM_WINDOWS)
  186. #define JPH_BREAKPOINT __debugbreak()
  187. #elif defined(JPH_PLATFORM_BLUE)
  188. // Configuration for a popular game console.
  189. // This file is not distributed because it would violate an NDA.
  190. // Creating one should only be a couple of minutes of work if you have the documentation for the platform
  191. // (you only need to define JPH_BREAKPOINT, JPH_PLATFORM_BLUE_GET_TICKS and JPH_PLATFORM_BLUE_GET_TICK_FREQUENCY and include the right header).
  192. #include <Jolt/Core/PlatformBlue.h>
  193. #elif defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS)
  194. #if defined(JPH_CPU_X86)
  195. #define JPH_BREAKPOINT __asm volatile ("int $0x3")
  196. #elif defined(JPH_CPU_ARM64)
  197. #define JPH_BREAKPOINT __builtin_trap()
  198. #endif
  199. #elif defined(JPH_PLATFORM_WASM)
  200. #define JPH_BREAKPOINT do { } while (false) // Not supported
  201. #else
  202. #error Unknown platform
  203. #endif
  204. // Crashes the application
  205. #define JPH_CRASH do { int *ptr = nullptr; *ptr = 0; } while (false)
  206. // Begin the JPH namespace
  207. #define JPH_NAMESPACE_BEGIN \
  208. JPH_SUPPRESS_WARNING_PUSH \
  209. JPH_SUPPRESS_WARNINGS \
  210. namespace JPH {
  211. // End the JPH namespace
  212. #define JPH_NAMESPACE_END \
  213. } \
  214. JPH_SUPPRESS_WARNING_POP
  215. // Suppress warnings generated by the standard template library
  216. #define JPH_SUPPRESS_WARNINGS_STD_BEGIN \
  217. JPH_SUPPRESS_WARNING_PUSH \
  218. JPH_MSVC_SUPPRESS_WARNING(4710) \
  219. JPH_MSVC_SUPPRESS_WARNING(4711) \
  220. JPH_MSVC_SUPPRESS_WARNING(4820) \
  221. JPH_MSVC_SUPPRESS_WARNING(4514) \
  222. \
  223. JPH_GCC_SUPPRESS_WARNING("-Wstringop-overflow=")
  224. #define JPH_SUPPRESS_WARNINGS_STD_END \
  225. JPH_SUPPRESS_WARNING_POP
  226. // Standard C++ includes
  227. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  228. #include <vector>
  229. #include <utility>
  230. #include <cmath>
  231. #include <sstream>
  232. #include <functional>
  233. #include <algorithm>
  234. JPH_SUPPRESS_WARNINGS_STD_END
  235. #include <limits.h>
  236. #include <float.h>
  237. #include <string.h>
  238. #if defined(JPH_USE_SSE)
  239. #include <immintrin.h>
  240. #elif defined(JPH_USE_NEON)
  241. #include <arm_neon.h>
  242. #endif
  243. JPH_NAMESPACE_BEGIN
  244. using namespace std;
  245. // Standard types
  246. using uint = unsigned int;
  247. using uint8 = uint8_t;
  248. using uint16 = uint16_t;
  249. using uint32 = uint32_t;
  250. using uint64 = uint64_t;
  251. // Assert sizes of types
  252. static_assert(sizeof(uint) >= 4, "Invalid size of uint");
  253. static_assert(sizeof(uint8) == 1, "Invalid size of uint8");
  254. static_assert(sizeof(uint16) == 2, "Invalid size of uint16");
  255. static_assert(sizeof(uint32) == 4, "Invalid size of uint32");
  256. static_assert(sizeof(uint64) == 8, "Invalid size of uint64");
  257. static_assert(sizeof(void *) == (JPH_CPU_ADDRESS_BITS == 64? 8 : 4), "Invalid size of pointer" );
  258. // Define inline macro
  259. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  260. #define JPH_INLINE __inline__ __attribute__((always_inline))
  261. #elif defined(JPH_COMPILER_MSVC)
  262. #define JPH_INLINE __forceinline
  263. #else
  264. #error Undefined
  265. #endif
  266. // Cache line size (used for aligning to cache line)
  267. #ifndef JPH_CACHE_LINE_SIZE
  268. #define JPH_CACHE_LINE_SIZE 64
  269. #endif
  270. // Define macro to get current function name
  271. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
  272. #define JPH_FUNCTION_NAME __PRETTY_FUNCTION__
  273. #elif defined(JPH_COMPILER_MSVC)
  274. #define JPH_FUNCTION_NAME __FUNCTION__
  275. #else
  276. #error Undefined
  277. #endif
  278. // Stack allocation
  279. #define JPH_STACK_ALLOC(n) alloca(n)
  280. // Shorthand for #ifdef _DEBUG / #endif
  281. #ifdef _DEBUG
  282. #define JPH_IF_DEBUG(...) __VA_ARGS__
  283. #define JPH_IF_NOT_DEBUG(...)
  284. #else
  285. #define JPH_IF_DEBUG(...)
  286. #define JPH_IF_NOT_DEBUG(...) __VA_ARGS__
  287. #endif
  288. // Shorthand for #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED / #endif
  289. #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED
  290. #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...) __VA_ARGS__
  291. #else
  292. #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...)
  293. #endif
  294. // Macro to indicate that a parameter / variable is unused
  295. #define JPH_UNUSED(x) (void)x
  296. // Macro to enable floating point precise mode and to disable fused multiply add instructions
  297. #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC) || defined(JPH_CROSS_PLATFORM_DETERMINISTIC)
  298. // In clang it appears you cannot turn off -ffast-math and -ffp-contract=fast for a code block
  299. // There is #pragma clang fp contract (off) but that doesn't seem to work under clang 9 & 10 when -ffast-math is specified on the commandline (you override it to turn it on, but not off)
  300. // There is #pragma float_control(precise, on) but that doesn't work under clang 9.
  301. // So for now we compile clang without -ffast-math so the macros are empty
  302. #define JPH_PRECISE_MATH_ON
  303. #define JPH_PRECISE_MATH_OFF
  304. #elif defined(JPH_COMPILER_MSVC)
  305. // 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
  306. #define JPH_PRECISE_MATH_ON \
  307. __pragma(float_control(precise, on, push)) \
  308. __pragma(fp_contract(off))
  309. #define JPH_PRECISE_MATH_OFF \
  310. __pragma(fp_contract(on)) \
  311. __pragma(float_control(pop))
  312. #else
  313. #error Undefined
  314. #endif
  315. JPH_NAMESPACE_END