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