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