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