Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Core.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7// Jolt library version
8#define JPH_VERSION_MAJOR 5
9#define JPH_VERSION_MINOR 6
10#define JPH_VERSION_PATCH 1
11
12// Determine if we want extra debugging code to be active
13#if !defined(NDEBUG) && !defined(JPH_NO_DEBUG)
14 #define JPH_DEBUG
15#endif
16
17// Always turn on asserts in Debug mode
18#if defined(JPH_DEBUG) && !defined(JPH_ENABLE_ASSERTS)
19 #define JPH_ENABLE_ASSERTS
20#endif
21
22// Determine which features the library was compiled with
23#ifdef JPH_DOUBLE_PRECISION
24 #define JPH_VERSION_FEATURE_BIT_1 1
25#else
26 #define JPH_VERSION_FEATURE_BIT_1 0
27#endif
28#ifdef JPH_CROSS_PLATFORM_DETERMINISTIC
29 #define JPH_VERSION_FEATURE_BIT_2 1
30#else
31 #define JPH_VERSION_FEATURE_BIT_2 0
32#endif
33#ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED
34 #define JPH_VERSION_FEATURE_BIT_3 1
35#else
36 #define JPH_VERSION_FEATURE_BIT_3 0
37#endif
38#ifdef JPH_PROFILE_ENABLED
39 #define JPH_VERSION_FEATURE_BIT_4 1
40#else
41 #define JPH_VERSION_FEATURE_BIT_4 0
42#endif
43#ifdef JPH_EXTERNAL_PROFILE
44 #define JPH_VERSION_FEATURE_BIT_5 1
45#else
46 #define JPH_VERSION_FEATURE_BIT_5 0
47#endif
48#ifdef JPH_DEBUG_RENDERER
49 #define JPH_VERSION_FEATURE_BIT_6 1
50#else
51 #define JPH_VERSION_FEATURE_BIT_6 0
52#endif
53#ifdef JPH_DISABLE_TEMP_ALLOCATOR
54 #define JPH_VERSION_FEATURE_BIT_7 1
55#else
56 #define JPH_VERSION_FEATURE_BIT_7 0
57#endif
58#ifdef JPH_DISABLE_CUSTOM_ALLOCATOR
59 #define JPH_VERSION_FEATURE_BIT_8 1
60#else
61 #define JPH_VERSION_FEATURE_BIT_8 0
62#endif
63#if defined(JPH_OBJECT_LAYER_BITS) && JPH_OBJECT_LAYER_BITS == 32
64 #define JPH_VERSION_FEATURE_BIT_9 1
65#else
66 #define JPH_VERSION_FEATURE_BIT_9 0
67#endif
68#ifdef JPH_ENABLE_ASSERTS
69 #define JPH_VERSION_FEATURE_BIT_10 1
70#else
71 #define JPH_VERSION_FEATURE_BIT_10 0
72#endif
73#ifdef JPH_OBJECT_STREAM
74 #define JPH_VERSION_FEATURE_BIT_11 1
75#else
76 #define JPH_VERSION_FEATURE_BIT_11 0
77#endif
78#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) | (JPH_VERSION_FEATURE_BIT_11 << 10))
79
80// Combine the version and features in a single ID
81#define JPH_VERSION_ID ((JPH_VERSION_FEATURES << 24) | (JPH_VERSION_MAJOR << 16) | (JPH_VERSION_MINOR << 8) | JPH_VERSION_PATCH)
82
83// Determine platform
84#if defined(JPH_PLATFORM_BLUE)
85 // Correct define already defined, this overrides everything else
86#elif defined(_WIN32) || defined(_WIN64)
87 #include <winapifamily.h>
88 #if WINAPI_FAMILY == WINAPI_FAMILY_APP
89 #define JPH_PLATFORM_WINDOWS_UWP // Building for Universal Windows Platform
90 #endif
91 #define JPH_PLATFORM_WINDOWS
92#elif defined(__ANDROID__) // Android is linux too, so that's why we check it first
93 #define JPH_PLATFORM_ANDROID
94#elif defined(__linux__)
95 #define JPH_PLATFORM_LINUX
96#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
97 #define JPH_PLATFORM_BSD
98#elif defined(__APPLE__)
99 #include <TargetConditionals.h>
100 #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE
101 #define JPH_PLATFORM_MACOS
102 #else
103 #define JPH_PLATFORM_IOS
104 #endif
105#elif defined(__EMSCRIPTEN__)
106 #define JPH_PLATFORM_WASM
107#endif
108
109// Platform helper macros
110#ifdef JPH_PLATFORM_ANDROID
111 #define JPH_IF_NOT_ANDROID(x)
112#else
113 #define JPH_IF_NOT_ANDROID(x) x
114#endif
115
116// Determine compiler
117#if defined(__clang__)
118 #define JPH_COMPILER_CLANG
119#elif defined(__GNUC__)
120 #define JPH_COMPILER_GCC
121#elif defined(_MSC_VER)
122 #define JPH_COMPILER_MSVC
123#endif
124
125#if defined(__MINGW64__) || defined (__MINGW32__)
126 #define JPH_COMPILER_MINGW
127#endif
128
129// Detect CPU architecture
130#if defined(__aarch64__) || defined(_M_ARM64) || defined(__arm__) || defined(_M_ARM) || defined(_M_ARM64EC)
131 // ARM CPU architecture
132 #define JPH_CPU_ARM
133 #if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
134 #define JPH_CPU_ARCH_BITS 64
135 #define JPH_USE_NEON
136 #define JPH_VECTOR_ALIGNMENT 16
137 #define JPH_DVECTOR_ALIGNMENT 32
138 #else
139 #define JPH_CPU_ARCH_BITS 32
140 #define JPH_VECTOR_ALIGNMENT 8 // 32-bit ARM does not support aligning on the stack on 16 byte boundaries
141 #define JPH_DVECTOR_ALIGNMENT 8
142 #endif
143 #ifndef JPH_CROSS_PLATFORM_DETERMINISTIC // FMA is not compatible with cross platform determinism
144 #if defined(__ARM_FEATURE_FMA) && !defined(JPH_USE_FMADD)
145 #define JPH_USE_FMADD
146 #endif
147 #endif
148#elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
149 // X86 CPU architecture
150 #define JPH_CPU_X86
151 #if defined(__x86_64__) || defined(_M_X64)
152 #define JPH_CPU_ARCH_BITS 64
153 #else
154 #define JPH_CPU_ARCH_BITS 32
155 #endif
156 #define JPH_USE_SSE
157 #define JPH_VECTOR_ALIGNMENT 16
158 #define JPH_DVECTOR_ALIGNMENT 32
159
160 // Detect enabled instruction sets
161 #if defined(__AVX512F__) && defined(__AVX512VL__) && defined(__AVX512DQ__) && !defined(JPH_USE_AVX512)
162 #define JPH_USE_AVX512
163 #endif
164 #if (defined(__AVX2__) || defined(JPH_USE_AVX512)) && !defined(JPH_USE_AVX2)
165 #define JPH_USE_AVX2
166 #endif
167 #if (defined(__AVX__) || defined(JPH_USE_AVX2)) && !defined(JPH_USE_AVX)
168 #define JPH_USE_AVX
169 #endif
170 #if (defined(__SSE4_2__) || defined(JPH_USE_AVX)) && !defined(JPH_USE_SSE4_2)
171 #define JPH_USE_SSE4_2
172 #endif
173 #if (defined(__SSE4_1__) || defined(JPH_USE_SSE4_2)) && !defined(JPH_USE_SSE4_1)
174 #define JPH_USE_SSE4_1
175 #endif
176 #if (defined(__F16C__) || defined(JPH_USE_AVX2)) && !defined(JPH_USE_F16C)
177 #define JPH_USE_F16C
178 #endif
179 #if (defined(__LZCNT__) || defined(JPH_USE_AVX2)) && !defined(JPH_USE_LZCNT)
180 #define JPH_USE_LZCNT
181 #endif
182 #if (defined(__BMI__) || defined(JPH_USE_AVX2)) && !defined(JPH_USE_TZCNT)
183 #define JPH_USE_TZCNT
184 #endif
185 #ifndef JPH_CROSS_PLATFORM_DETERMINISTIC // FMA is not compatible with cross platform determinism
186 #if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
187 #if defined(__FMA__) && !defined(JPH_USE_FMADD)
188 #define JPH_USE_FMADD
189 #endif
190 #elif defined(JPH_COMPILER_MSVC)
191 #if defined(__AVX2__) && !defined(JPH_USE_FMADD) // AVX2 also enables fused multiply add
192 #define JPH_USE_FMADD
193 #endif
194 #else
195 #error Undefined compiler
196 #endif
197 #endif
198#elif defined(__riscv)
199 // RISC-V CPU architecture
200 #define JPH_CPU_RISCV
201 #if __riscv_xlen == 64
202 #define JPH_CPU_ARCH_BITS 64
203 #define JPH_VECTOR_ALIGNMENT 16
204 #define JPH_DVECTOR_ALIGNMENT 32
205 #else
206 #define JPH_CPU_ARCH_BITS 32
207 #define JPH_VECTOR_ALIGNMENT 16
208 #define JPH_DVECTOR_ALIGNMENT 8
209 #endif
210 #if defined(__riscv_vector)
211 #define JPH_USE_RVV
212 #endif
213#elif defined(JPH_PLATFORM_WASM)
214 // WebAssembly CPU architecture
215 #define JPH_CPU_WASM
216 #if defined(__wasm64__)
217 #define JPH_CPU_ARCH_BITS 64
218 #else
219 #define JPH_CPU_ARCH_BITS 32
220 #endif
221 #define JPH_VECTOR_ALIGNMENT 16
222 #define JPH_DVECTOR_ALIGNMENT 32
223 #ifdef __wasm_simd128__
224 #define JPH_USE_SSE
225 #define JPH_USE_SSE4_1
226 #define JPH_USE_SSE4_2
227 #endif
228#elif defined(__powerpc__) || defined(__powerpc64__)
229 // PowerPC CPU architecture
230 #define JPH_CPU_PPC
231 #if defined(__powerpc64__)
232 #define JPH_CPU_ARCH_BITS 64
233 #else
234 #define JPH_CPU_ARCH_BITS 32
235 #endif
236 #ifdef _BIG_ENDIAN
237 #define JPH_CPU_BIG_ENDIAN
238 #endif
239 #define JPH_VECTOR_ALIGNMENT 16
240 #define JPH_DVECTOR_ALIGNMENT 8
241#elif defined(__loongarch__)
242 // LoongArch CPU architecture
243 #define JPH_CPU_LOONGARCH
244 #if defined(__loongarch64)
245 #define JPH_CPU_ARCH_BITS 64
246 #else
247 #define JPH_CPU_ARCH_BITS 32
248 #endif
249 #define JPH_VECTOR_ALIGNMENT 16
250 #define JPH_DVECTOR_ALIGNMENT 8
251#elif defined(__e2k__)
252 // E2K CPU architecture (MCST Elbrus 2000)
253 #define JPH_CPU_E2K
254 #define JPH_CPU_ARCH_BITS 64
255 #define JPH_VECTOR_ALIGNMENT 16
256 #define JPH_DVECTOR_ALIGNMENT 32
257
258 // Compiler flags on e2k arch determine CPU features
259 #if defined(__SSE__) && !defined(JPH_USE_SSE)
260 #define JPH_USE_SSE
261 #endif
262#else
263 #error Unsupported CPU architecture
264#endif
265
266// If this define is set, Jolt is compiled as a shared library
267#ifdef JPH_SHARED_LIBRARY
268 #ifdef JPH_BUILD_SHARED_LIBRARY
269 // While building the shared library, we must export these symbols
270 #if defined(JPH_PLATFORM_WINDOWS) && !defined(JPH_COMPILER_MINGW)
271 #define JPH_EXPORT __declspec(dllexport)
272 #else
273 #define JPH_EXPORT __attribute__ ((visibility ("default")))
274 #if defined(JPH_COMPILER_GCC)
275 // Prevents an issue with GCC attribute parsing (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69585)
276 #define JPH_EXPORT_GCC_BUG_WORKAROUND [[gnu::visibility("default")]]
277 #endif
278 #endif
279 #else
280 // When linking against Jolt, we must import these symbols
281 #if defined(JPH_PLATFORM_WINDOWS) && !defined(JPH_COMPILER_MINGW)
282 #define JPH_EXPORT __declspec(dllimport)
283 #else
284 #define JPH_EXPORT __attribute__ ((visibility ("default")))
285 #if defined(JPH_COMPILER_GCC)
286 // Prevents an issue with GCC attribute parsing (see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69585)
287 #define JPH_EXPORT_GCC_BUG_WORKAROUND [[gnu::visibility("default")]]
288 #endif
289 #endif
290 #endif
291
292 #define JPH_IF_SHARED_LIBRARY(x) x
293 #define JPH_IF_NOT_SHARED_LIBRARY(x)
294#else
295 // If the define is not set, we use static linking and symbols don't need to be imported or exported
296 #define JPH_EXPORT
297
298 #define JPH_IF_SHARED_LIBRARY(x)
299 #define JPH_IF_NOT_SHARED_LIBRARY(x) x
300#endif
301
302#ifndef JPH_EXPORT_GCC_BUG_WORKAROUND
303 #define JPH_EXPORT_GCC_BUG_WORKAROUND JPH_EXPORT
304#endif
305
306// Macro used by the RTTI macros to not export a function
307#define JPH_NO_EXPORT
308
309// Pragmas to store / restore the warning state and to disable individual warnings
310#ifdef JPH_COMPILER_CLANG
311 #define JPH_PRAGMA(x) _Pragma(#x)
312 #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(clang diagnostic push)
313 #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(clang diagnostic pop)
314 #define JPH_CLANG_SUPPRESS_WARNING(w) JPH_PRAGMA(clang diagnostic ignored w)
315 #if __has_warning("-Wdeprecated-copy")
316 #define JPH_CLANG_SUPPRESS_DEPRECATED_COPY_WARNING JPH_CLANG_SUPPRESS_WARNING("-Wdeprecated-copy")
317 #endif
318 #if __has_warning("-Wdeprecated-copy-with-dtor")
319 #define JPH_CLANG_SUPPRESS_DEPRECATED_COPY_WITH_DTOR_WARNING JPH_CLANG_SUPPRESS_WARNING("-Wdeprecated-copy-with-dtor")
320 #endif
321 #if __has_warning("-Wunsafe-buffer-usage")
322 #define JPH_CLANG_SUPPRESS_UNSAFE_BUFFER_USAGE_WARNING JPH_CLANG_SUPPRESS_WARNING("-Wunsafe-buffer-usage")
323 #endif
324 #if __has_warning("-Wimplicit-int-float-conversion")
325 #define JPH_CLANG_SUPPRESS_IMPLICIT_INT_FLOAT_CONVERSION_WARNING JPH_CLANG_SUPPRESS_WARNING("-Wimplicit-int-float-conversion")
326 #endif
327 #if __has_warning("-Wunique-object-duplication")
328 #define JPH_CLANG_SUPPRESS_UNIQUE_OBJECT_DUPLICATION_WARNING JPH_CLANG_SUPPRESS_WARNING("-Wunique-object-duplication")
329 #endif
330 #if __has_warning("-Wnrvo")
331 #define JPH_CLANG_SUPPRESS_NRVO_WARNING JPH_CLANG_SUPPRESS_WARNING("-Wnrvo")
332 #endif
333#else
334 #define JPH_CLANG_SUPPRESS_WARNING(w)
335#endif
336#ifndef JPH_CLANG_SUPPRESS_DEPRECATED_COPY_WARNING
337 #define JPH_CLANG_SUPPRESS_DEPRECATED_COPY_WARNING
338#endif
339#ifndef JPH_CLANG_SUPPRESS_DEPRECATED_COPY_WITH_DTOR_WARNING
340 #define JPH_CLANG_SUPPRESS_DEPRECATED_COPY_WITH_DTOR_WARNING
341#endif
342#ifndef JPH_CLANG_SUPPRESS_UNSAFE_BUFFER_USAGE_WARNING
343 #define JPH_CLANG_SUPPRESS_UNSAFE_BUFFER_USAGE_WARNING
344#endif
345#ifndef JPH_CLANG_SUPPRESS_IMPLICIT_INT_FLOAT_CONVERSION_WARNING
346 #define JPH_CLANG_SUPPRESS_IMPLICIT_INT_FLOAT_CONVERSION_WARNING
347#endif
348#ifndef JPH_CLANG_SUPPRESS_UNIQUE_OBJECT_DUPLICATION_WARNING
349 #define JPH_CLANG_SUPPRESS_UNIQUE_OBJECT_DUPLICATION_WARNING
350#endif
351#ifndef JPH_CLANG_SUPPRESS_NRVO_WARNING
352 #define JPH_CLANG_SUPPRESS_NRVO_WARNING
353#endif
354
355#ifdef JPH_COMPILER_GCC
356 #define JPH_PRAGMA(x) _Pragma(#x)
357 #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(GCC diagnostic push)
358 #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(GCC diagnostic pop)
359 #define JPH_GCC_SUPPRESS_WARNING(w) JPH_PRAGMA(GCC diagnostic ignored w)
360#else
361 #define JPH_GCC_SUPPRESS_WARNING(w)
362#endif
363
364#ifdef JPH_COMPILER_MSVC
365 #define JPH_PRAGMA(x) __pragma(x)
366 #define JPH_SUPPRESS_WARNING_PUSH JPH_PRAGMA(warning (push))
367 #define JPH_SUPPRESS_WARNING_POP JPH_PRAGMA(warning (pop))
368 #define JPH_MSVC_SUPPRESS_WARNING(w) JPH_PRAGMA(warning (disable : w))
369 #if _MSC_VER >= 1920 && _MSC_VER < 1930
370 #define JPH_MSVC2019_SUPPRESS_WARNING(w) JPH_MSVC_SUPPRESS_WARNING(w)
371 #else
372 #define JPH_MSVC2019_SUPPRESS_WARNING(w)
373 #endif
374 #if _MSC_VER >= 1950
375 #define JPH_MSVC2026_PLUS_SUPPRESS_WARNING(w) JPH_MSVC_SUPPRESS_WARNING(w)
376 #else
377 #define JPH_MSVC2026_PLUS_SUPPRESS_WARNING(w)
378 #endif
379#else
380 #define JPH_MSVC_SUPPRESS_WARNING(w)
381 #define JPH_MSVC2019_SUPPRESS_WARNING(w)
382 #define JPH_MSVC2026_PLUS_SUPPRESS_WARNING(w)
383#endif
384
385// Disable common warnings triggered by Jolt when compiling with -Wall
386#define JPH_SUPPRESS_WARNINGS \
387 JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat") \
388 JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic") \
389 JPH_CLANG_SUPPRESS_WARNING("-Wfloat-equal") \
390 JPH_CLANG_SUPPRESS_WARNING("-Wsign-conversion") \
391 JPH_CLANG_SUPPRESS_WARNING("-Wold-style-cast") \
392 JPH_CLANG_SUPPRESS_WARNING("-Wgnu-anonymous-struct") \
393 JPH_CLANG_SUPPRESS_WARNING("-Wnested-anon-types") \
394 JPH_CLANG_SUPPRESS_WARNING("-Wglobal-constructors") \
395 JPH_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors") \
396 JPH_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path") \
397 JPH_CLANG_SUPPRESS_WARNING("-Wlanguage-extension-token") \
398 JPH_CLANG_SUPPRESS_WARNING("-Wunused-parameter") \
399 JPH_CLANG_SUPPRESS_WARNING("-Wformat-nonliteral") \
400 JPH_CLANG_SUPPRESS_WARNING("-Wcovered-switch-default") \
401 JPH_CLANG_SUPPRESS_WARNING("-Wcast-align") \
402 JPH_CLANG_SUPPRESS_WARNING("-Winvalid-offsetof") \
403 JPH_CLANG_SUPPRESS_WARNING("-Wgnu-zero-variadic-macro-arguments") \
404 JPH_CLANG_SUPPRESS_WARNING("-Wdocumentation-unknown-command") \
405 JPH_CLANG_SUPPRESS_WARNING("-Wctad-maybe-unsupported") \
406 JPH_CLANG_SUPPRESS_WARNING("-Wswitch-default") \
407 JPH_CLANG_SUPPRESS_DEPRECATED_COPY_WARNING \
408 JPH_CLANG_SUPPRESS_DEPRECATED_COPY_WITH_DTOR_WARNING \
409 JPH_CLANG_SUPPRESS_UNSAFE_BUFFER_USAGE_WARNING \
410 JPH_CLANG_SUPPRESS_IMPLICIT_INT_FLOAT_CONVERSION_WARNING \
411 JPH_CLANG_SUPPRESS_WARNING("-Wpadded") \
412 JPH_IF_NOT_SHARED_LIBRARY(JPH_CLANG_SUPPRESS_UNIQUE_OBJECT_DUPLICATION_WARNING) \
413 JPH_CLANG_SUPPRESS_NRVO_WARNING \
414 \
415 JPH_GCC_SUPPRESS_WARNING("-Wcomment") \
416 JPH_GCC_SUPPRESS_WARNING("-Winvalid-offsetof") \
417 JPH_GCC_SUPPRESS_WARNING("-Wclass-memaccess") \
418 JPH_GCC_SUPPRESS_WARNING("-Wpedantic") \
419 JPH_GCC_SUPPRESS_WARNING("-Wunused-parameter") \
420 JPH_GCC_SUPPRESS_WARNING("-Wmaybe-uninitialized") \
421 \
422 JPH_MSVC_SUPPRESS_WARNING(4619) /* #pragma warning: there is no warning number 'XXXX' */ \
423 JPH_MSVC_SUPPRESS_WARNING(4514) /* 'X' : unreferenced inline function has been removed */ \
424 JPH_MSVC_SUPPRESS_WARNING(4710) /* 'X' : function not inlined */ \
425 JPH_MSVC_SUPPRESS_WARNING(4711) /* function 'X' selected for automatic inline expansion */ \
426 JPH_MSVC_SUPPRESS_WARNING(4714) /* function 'X' marked as __forceinline not inlined */ \
427 JPH_MSVC_SUPPRESS_WARNING(4820) /* 'X': 'Y' bytes padding added after data member 'Z' */ \
428 JPH_MSVC_SUPPRESS_WARNING(4100) /* 'X' : unreferenced formal parameter */ \
429 JPH_MSVC_SUPPRESS_WARNING(4626) /* 'X' : assignment operator was implicitly defined as deleted because a base class assignment operator is inaccessible or deleted */ \
430 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 */ \
431 JPH_MSVC_SUPPRESS_WARNING(4365) /* 'argument' : conversion from 'X' to 'Y', signed / unsigned mismatch */ \
432 JPH_MSVC_SUPPRESS_WARNING(4324) /* 'X' : structure was padded due to alignment specifier */ \
433 JPH_MSVC_SUPPRESS_WARNING(4625) /* 'X' : copy constructor was implicitly defined as deleted because a base class copy constructor is inaccessible or deleted */ \
434 JPH_MSVC_SUPPRESS_WARNING(5026) /* 'X': move constructor was implicitly defined as deleted because a base class move constructor is inaccessible or deleted */ \
435 JPH_MSVC_SUPPRESS_WARNING(4623) /* 'X' : default constructor was implicitly defined as deleted */ \
436 JPH_MSVC_SUPPRESS_WARNING(4201) /* nonstandard extension used: nameless struct/union */ \
437 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' */ \
438 JPH_MSVC_SUPPRESS_WARNING(5045) /* Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified */ \
439 JPH_MSVC_SUPPRESS_WARNING(4583) /* 'X': destructor is not implicitly called */ \
440 JPH_MSVC_SUPPRESS_WARNING(4582) /* 'X': constructor is not implicitly called */ \
441 JPH_MSVC_SUPPRESS_WARNING(5219) /* implicit conversion from 'X' to 'Y', possible loss of data */ \
442 JPH_MSVC_SUPPRESS_WARNING(4826) /* Conversion from 'X *' to 'JPH::uint64' is sign-extended. This may cause unexpected runtime behavior. (32-bit) */ \
443 JPH_MSVC_SUPPRESS_WARNING(5264) /* 'X': 'const' variable is not used */ \
444 JPH_MSVC_SUPPRESS_WARNING(4251) /* class 'X' needs to have DLL-interface to be used by clients of class 'Y' */ \
445 JPH_MSVC_SUPPRESS_WARNING(4738) /* storing 32-bit float result in memory, possible loss of performance */ \
446 JPH_MSVC2019_SUPPRESS_WARNING(5246) /* the initialization of a subobject should be wrapped in braces */ \
447 JPH_MSVC2026_PLUS_SUPPRESS_WARNING(5291) /* 'X': deriving from the base class 'Y' can cause potential runtime issues due to an ABI bug. Recommend adding a 4-byte data member to the base class for the padding at the end of it to work around this bug. */
448
449// OS-specific includes
450#if defined(JPH_PLATFORM_WINDOWS)
451 #define JPH_BREAKPOINT __debugbreak()
452#elif defined(JPH_PLATFORM_BLUE)
453 // Configuration for a popular game console.
454 // This file is not distributed because it would violate an NDA.
455 // Creating one should only be a couple of minutes of work if you have the documentation for the platform
456 // (you only need to define JPH_BREAKPOINT, JPH_PLATFORM_BLUE_GET_TICKS, JPH_PLATFORM_BLUE_MUTEX*, JPH_PLATFORM_BLUE_RWLOCK*, JPH_PLATFORM_BLUE_SEMAPHORE* and include the right header).
457 #include <Jolt/Core/PlatformBlue.h>
458#elif defined(JPH_PLATFORM_LINUX) || defined(JPH_PLATFORM_ANDROID) || defined(JPH_PLATFORM_MACOS) || defined(JPH_PLATFORM_IOS) || defined(JPH_PLATFORM_BSD)
459 #if defined(JPH_CPU_X86)
460 #define JPH_BREAKPOINT __asm volatile ("int $0x3")
461 #elif defined(JPH_CPU_ARM) || defined(JPH_CPU_RISCV) || defined(JPH_CPU_E2K) || defined(JPH_CPU_PPC) || defined(JPH_CPU_LOONGARCH)
462 #define JPH_BREAKPOINT __builtin_trap()
463 #else
464 #error Unknown CPU architecture
465 #endif
466#elif defined(JPH_PLATFORM_WASM)
467 #define JPH_BREAKPOINT do { } while (false) // Not supported
468#else
469 #error Unknown platform
470#endif
471
472// Begin the JPH namespace
473#define JPH_NAMESPACE_BEGIN \
474 JPH_SUPPRESS_WARNING_PUSH \
475 JPH_SUPPRESS_WARNINGS \
476 namespace JPH {
477
478// End the JPH namespace
479#define JPH_NAMESPACE_END \
480 } \
481 JPH_SUPPRESS_WARNING_POP
482
483// Suppress warnings generated by the standard template library
484#define JPH_SUPPRESS_WARNINGS_STD_BEGIN \
485 JPH_SUPPRESS_WARNING_PUSH \
486 JPH_CLANG_SUPPRESS_WARNING("-Wpadded") \
487 JPH_MSVC_SUPPRESS_WARNING(4365) \
488 JPH_MSVC_SUPPRESS_WARNING(4619) \
489 JPH_MSVC_SUPPRESS_WARNING(4710) \
490 JPH_MSVC_SUPPRESS_WARNING(4711) \
491 JPH_MSVC_SUPPRESS_WARNING(4820) \
492 JPH_MSVC_SUPPRESS_WARNING(4514) \
493 JPH_MSVC_SUPPRESS_WARNING(5262) \
494 JPH_MSVC_SUPPRESS_WARNING(5264) \
495 JPH_MSVC_SUPPRESS_WARNING(4738) \
496 JPH_MSVC_SUPPRESS_WARNING(5045)
497
498#define JPH_SUPPRESS_WARNINGS_STD_END \
499 JPH_SUPPRESS_WARNING_POP
500
501// MSVC STL requires _HAS_EXCEPTIONS=0 if exceptions are turned off
502#if defined(JPH_COMPILER_MSVC) && (!defined(__cpp_exceptions) || !__cpp_exceptions) && !defined(_HAS_EXCEPTIONS)
503 #define _HAS_EXCEPTIONS 0
504#endif
505
506// Standard C++ includes
508#include <float.h>
509#include <limits.h>
510#include <string.h>
511#include <new>
512#include <utility>
513#include <cmath>
514#include <sstream>
515#include <functional>
516#include <algorithm>
517#include <cstdint>
518#include <type_traits>
519#if defined(JPH_COMPILER_MSVC) || (defined(JPH_COMPILER_CLANG) && defined(_MSC_VER)) // MSVC or clang-cl
520 #include <malloc.h> // for alloca
521#endif
522#if defined(JPH_USE_SSE)
523 #include <immintrin.h>
524#elif defined(JPH_USE_NEON)
525 #ifdef JPH_COMPILER_MSVC
526 #include <intrin.h>
527 #include <arm64_neon.h>
528 #else
529 #include <arm_neon.h>
530 #endif
531#elif defined(JPH_USE_RVV)
532 #include <riscv_vector.h>
533#endif
535
537
538// Commonly used STL types
539using std::min;
540using std::max;
541using std::abs;
542using std::ceil;
543using std::floor;
544using std::trunc;
545using std::round;
546using std::fmod;
547using std::string_view;
548using std::function;
549using std::numeric_limits;
550using std::isfinite;
551using std::isnan;
552using std::ostream;
553using std::istream;
554
555// Standard types
556using uint = unsigned int;
557using uint8 = std::uint8_t;
558using uint16 = std::uint16_t;
559using uint32 = std::uint32_t;
560using int32 = std::int32_t;
561using uint64 = std::uint64_t;
562using int64 = std::int64_t;
563
564// Assert sizes of types
565static_assert(sizeof(uint) >= 4, "Invalid size of uint");
566static_assert(sizeof(uint8) == 1, "Invalid size of uint8");
567static_assert(sizeof(uint16) == 2, "Invalid size of uint16");
568static_assert(sizeof(uint32) == 4, "Invalid size of uint32");
569static_assert(sizeof(uint64) == 8, "Invalid size of uint64");
570
571// Define inline macro
572#if defined(JPH_NO_FORCE_INLINE)
573 #define JPH_INLINE inline
574#elif defined(JPH_COMPILER_CLANG)
575 #define JPH_INLINE __inline__ __attribute__((always_inline))
576#elif defined(JPH_COMPILER_GCC)
577 // On gcc 14 using always_inline in debug mode causes error: "inlining failed in call to 'always_inline' 'XXX': function not considered for inlining"
578 // See: https://github.com/jrouwe/JoltPhysics/issues/1096
579 #if __GNUC__ >= 14 && defined(JPH_DEBUG)
580 #define JPH_INLINE inline
581 #else
582 #define JPH_INLINE __inline__ __attribute__((always_inline))
583 #endif
584#elif defined(JPH_COMPILER_MSVC)
585 #define JPH_INLINE __forceinline
586#else
587 #error Undefined
588#endif
589
590// Default memory allocation alignment.
591// This define can be overridden in case the user provides an Allocate function that has a different alignment than the platform default.
592#ifndef JPH_DEFAULT_ALLOCATE_ALIGNMENT
593 #if defined(JPH_COMPILER_MINGW) && JPH_CPU_ARCH_BITS == 32
594 // 32-bit MinGW g++ lies that __STDCPP_DEFAULT_NEW_ALIGNMENT__ is 16, but it is actually 8.
595 // See: https://github.com/godotengine/godot/issues/121608#issuecomment-5142806330
596 // Note: See similar fix for the definition of JPH_INTERNAL_DEFAULT_ALLOCATE.
597 #define JPH_DEFAULT_ALLOCATE_ALIGNMENT 8
598 #else
599 #define JPH_DEFAULT_ALLOCATE_ALIGNMENT __STDCPP_DEFAULT_NEW_ALIGNMENT__
600 #endif
601#endif
602
603// Cache line size (used for aligning to cache line)
604#ifndef JPH_CACHE_LINE_SIZE
605 #define JPH_CACHE_LINE_SIZE 64
606#endif
607
608// Define macro to get current function name
609#if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
610 #define JPH_FUNCTION_NAME __PRETTY_FUNCTION__
611#elif defined(JPH_COMPILER_MSVC)
612 #define JPH_FUNCTION_NAME __FUNCTION__
613#else
614 #error Undefined
615#endif
616
617// Stack allocation
618#define JPH_STACK_ALLOC(n) alloca(n)
619
620// Shorthand for #ifdef JPH_DEBUG / #endif
621#ifdef JPH_DEBUG
622 #define JPH_IF_DEBUG(...) __VA_ARGS__
623 #define JPH_IF_NOT_DEBUG(...)
624#else
625 #define JPH_IF_DEBUG(...)
626 #define JPH_IF_NOT_DEBUG(...) __VA_ARGS__
627#endif
628
629// Shorthand for #ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED / #endif
630#ifdef JPH_FLOATING_POINT_EXCEPTIONS_ENABLED
631 #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...) __VA_ARGS__
632#else
633 #define JPH_IF_FLOATING_POINT_EXCEPTIONS_ENABLED(...)
634#endif
635
636// Helper macros to detect if we're running in single or double precision mode
637#ifdef JPH_DOUBLE_PRECISION
638 #define JPH_IF_SINGLE_PRECISION(...)
639 #define JPH_IF_SINGLE_PRECISION_ELSE(s, d) d
640 #define JPH_IF_DOUBLE_PRECISION(...) __VA_ARGS__
641#else
642 #define JPH_IF_SINGLE_PRECISION(...) __VA_ARGS__
643 #define JPH_IF_SINGLE_PRECISION_ELSE(s, d) s
644 #define JPH_IF_DOUBLE_PRECISION(...)
645#endif
646
647// Helper macro to detect if the debug renderer is active
648#ifdef JPH_DEBUG_RENDERER
649 #define JPH_IF_DEBUG_RENDERER(...) __VA_ARGS__
650 #define JPH_IF_NOT_DEBUG_RENDERER(...)
651#else
652 #define JPH_IF_DEBUG_RENDERER(...)
653 #define JPH_IF_NOT_DEBUG_RENDERER(...) __VA_ARGS__
654#endif
655
656// Macro to indicate that a parameter / variable is unused
657#define JPH_UNUSED(x) (void)x
658
659// Macro to enable floating point precise mode
660#if defined(JPH_COMPILER_CLANG)
661 #define JPH_PRECISE_MATH_ON \
662 _Pragma("clang diagnostic push") \
663 _Pragma("clang diagnostic ignored \"-Wignored-pragmas\"") \
664 _Pragma("float_control(precise, on, push)") \
665 _Pragma("clang diagnostic pop")
666 #define JPH_PRECISE_MATH_OFF \
667 _Pragma("clang diagnostic push") \
668 _Pragma("clang diagnostic ignored \"-Wignored-pragmas\"") \
669 _Pragma("float_control(pop)") \
670 _Pragma("clang diagnostic pop")
671#elif defined(JPH_COMPILER_MSVC)
672 #define JPH_PRECISE_MATH_ON \
673 __pragma(float_control(precise, on, push))
674 #define JPH_PRECISE_MATH_OFF \
675 __pragma(float_control(pop))
676#else
677 #define JPH_PRECISE_MATH_ON
678 #define JPH_PRECISE_MATH_OFF
679#endif
680
681// Check if Thread Sanitizer is enabled
682#ifdef __has_feature
683 #if __has_feature(thread_sanitizer)
684 #define JPH_TSAN_ENABLED
685 #endif
686#else
687 #ifdef __SANITIZE_THREAD__
688 #define JPH_TSAN_ENABLED
689 #endif
690#endif
691
692// Attribute to disable Thread Sanitizer for a particular function
693#ifdef JPH_TSAN_ENABLED
694 #define JPH_TSAN_NO_SANITIZE __attribute__((no_sanitize("thread")))
695#else
696 #define JPH_TSAN_NO_SANITIZE
697#endif
698
699// Check if Address Sanitizer is enabled
700#ifdef __has_feature
701 #if __has_feature(address_sanitizer)
702 #define JPH_ASAN_ENABLED
703 #endif
704#else
705 #ifdef __SANITIZE_ADDRESS__
706 #define JPH_ASAN_ENABLED
707 #endif
708#endif
709
710// DirectX 12 is only supported on Windows
711#if defined(JPH_USE_DX12) && !defined(JPH_PLATFORM_WINDOWS)
712 #undef JPH_USE_DX12
713#endif // JPH_PLATFORM_WINDOWS
714
715// Metal is only supported on Apple platforms
716#if defined(JPH_USE_MTL) && !defined(JPH_PLATFORM_MACOS) && !defined(JPH_PLATFORM_IOS)
717 #undef JPH_USE_MTL
718#endif // !JPH_PLATFORM_MACOS && !JPH_PLATFORM_IOS
719
std::uint8_t uint8
Definition Core.h:557
std::int32_t int32
Definition Core.h:560
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition Core.h:484
std::int64_t int64
Definition Core.h:562
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition Core.h:498
std::uint64_t uint64
Definition Core.h:561
unsigned int uint
Definition Core.h:556
#define JPH_NAMESPACE_END
Definition Core.h:479
std::uint32_t uint32
Definition Core.h:559
#define JPH_NAMESPACE_BEGIN
Definition Core.h:473
std::uint16_t uint16
Definition Core.h:558