Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Math.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
8
10static constexpr float JPH_PI = 3.14159265358979323846f;
11
13JPH_INLINE constexpr float DegreesToRadians(float inV)
14{
15 return inV * (JPH_PI / 180.0f);
16}
17
19JPH_INLINE constexpr float RadiansToDegrees(float inV)
20{
21 return inV * (180.0f / JPH_PI);
22}
23
25inline float CenterAngleAroundZero(float inV)
26{
27 if (inV < -JPH_PI)
28 {
29 do
30 inV += 2.0f * JPH_PI;
31 while (inV < -JPH_PI);
32 }
33 else if (inV > JPH_PI)
34 {
35 do
36 inV -= 2.0f * JPH_PI;
37 while (inV > JPH_PI);
38 }
39 JPH_ASSERT(inV >= -JPH_PI && inV <= JPH_PI);
40 return inV;
41}
42
44template <typename T>
45JPH_INLINE constexpr T Clamp(T inV, T inMin, T inMax)
46{
47 return min(max(inV, inMin), inMax);
48}
49
51template <typename T>
52JPH_INLINE constexpr T Square(T inV)
53{
54 return inV * inV;
55}
56
58template <typename T>
59JPH_INLINE constexpr T Cubed(T inV)
60{
61 return inV * inV * inV;
62}
63
65template <typename T>
66JPH_INLINE constexpr T Sign(T inV)
67{
68 return inV < 0? T(-1) : T(1);
69}
70
72template <typename T>
73constexpr bool IsPowerOf2(T inV)
74{
75 return (inV & (inV - 1)) == 0;
76}
77
79template <typename T>
80inline T AlignUp(T inV, uint64 inAlignment)
81{
82 JPH_ASSERT(IsPowerOf2(inAlignment));
83 return T((uint64(inV) + inAlignment - 1) & ~(inAlignment - 1));
84}
85
87template <typename T>
88inline bool IsAligned(T inV, uint64 inAlignment)
89{
90 JPH_ASSERT(IsPowerOf2(inAlignment));
91 return (uint64(inV) & (inAlignment - 1)) == 0;
92}
93
96{
97#if defined(JPH_CPU_X86) || defined(JPH_CPU_WASM)
98 #if defined(JPH_USE_TZCNT)
99 return _tzcnt_u32(inValue);
100 #elif defined(JPH_COMPILER_MSVC)
101 if (inValue == 0)
102 return 32;
103 unsigned long result;
104 _BitScanForward(&result, inValue);
105 return result;
106 #else
107 if (inValue == 0)
108 return 32;
109 return __builtin_ctz(inValue);
110 #endif
111#elif defined(JPH_CPU_ARM)
112 #if defined(JPH_COMPILER_MSVC)
113 if (inValue == 0)
114 return 32;
115 unsigned long result;
116 _BitScanForward(&result, inValue);
117 return result;
118 #else
119 if (inValue == 0)
120 return 32;
121 return __builtin_ctz(inValue);
122 #endif
123#elif defined(JPH_CPU_E2K)
124 return inValue ? __builtin_ctz(inValue) : 32;
125#else
126 #error Undefined
127#endif
128}
129
132{
133#if defined(JPH_CPU_X86) || defined(JPH_CPU_WASM)
134 #if defined(JPH_USE_LZCNT)
135 return _lzcnt_u32(inValue);
136 #elif defined(JPH_COMPILER_MSVC)
137 if (inValue == 0)
138 return 32;
139 unsigned long result;
140 _BitScanReverse(&result, inValue);
141 return 31 - result;
142 #else
143 if (inValue == 0)
144 return 32;
145 return __builtin_clz(inValue);
146 #endif
147#elif defined(JPH_CPU_ARM)
148 #if defined(JPH_COMPILER_MSVC)
149 return _CountLeadingZeros(inValue);
150 #else
151 return __builtin_clz(inValue);
152 #endif
153#elif defined(JPH_CPU_E2K)
154 return inValue ? __builtin_clz(inValue) : 32;
155#else
156 #error Undefined
157#endif
158}
159
161inline uint CountBits(uint32 inValue)
162{
163#if defined(JPH_COMPILER_CLANG) || defined(JPH_COMPILER_GCC)
164 return __builtin_popcount(inValue);
165#elif defined(JPH_COMPILER_MSVC)
166 #if defined(JPH_USE_SSE4_2)
167 return _mm_popcnt_u32(inValue);
168 #elif defined(JPH_USE_NEON) && (_MSC_VER >= 1930) // _CountOneBits not available on MSVC2019
169 return _CountOneBits(inValue);
170 #else
171 inValue = inValue - ((inValue >> 1) & 0x55555555);
172 inValue = (inValue & 0x33333333) + ((inValue >> 2) & 0x33333333);
173 inValue = (inValue + (inValue >> 4)) & 0x0F0F0F0F;
174 return (inValue * 0x01010101) >> 24;
175 #endif
176#else
177 #error Undefined
178#endif
179}
180
183{
184 return inValue <= 1? uint32(1) : uint32(1) << (32 - CountLeadingZeros(inValue - 1));
185}
186
187// Simple implementation of C++20 std::bit_cast (unfortunately not constexpr)
188template <class To, class From>
189JPH_INLINE To BitCast(const From &inValue)
190{
191 static_assert(std::is_trivially_constructible_v<To>);
192 static_assert(sizeof(From) == sizeof(To));
193
194 union FromTo
195 {
196 To mTo;
197 From mFrom;
198 };
199
200 FromTo convert;
201 convert.mFrom = inValue;
202 return convert.mTo;
203}
204
std::uint64_t uint64
Definition: Core.h:456
unsigned int uint
Definition: Core.h:452
#define JPH_NAMESPACE_END
Definition: Core.h:378
std::uint32_t uint32
Definition: Core.h:455
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
JPH_INLINE constexpr T Clamp(T inV, T inMin, T inMax)
Clamp a value between two values.
Definition: Math.h:45
JPH_INLINE constexpr T Sign(T inV)
Get the sign of a value.
Definition: Math.h:66
float CenterAngleAroundZero(float inV)
Convert angle in radians to the range .
Definition: Math.h:25
constexpr bool IsPowerOf2(T inV)
Check if inV is a power of 2.
Definition: Math.h:73
uint CountBits(uint32 inValue)
Count the number of 1 bits in a value.
Definition: Math.h:161
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition: Math.h:52
JPH_INLINE constexpr float DegreesToRadians(float inV)
Convert a value from degrees to radians.
Definition: Math.h:13
JPH_INLINE constexpr T Cubed(T inV)
Returns .
Definition: Math.h:59
JPH_INLINE constexpr float RadiansToDegrees(float inV)
Convert a value from radians to degrees.
Definition: Math.h:19
bool IsAligned(T inV, uint64 inAlignment)
Check if inV is inAlignment aligned.
Definition: Math.h:88
uint CountTrailingZeros(uint32 inValue)
Compute number of trailing zero bits (how many low bits are zero)
Definition: Math.h:95
JPH_INLINE To BitCast(const From &inValue)
Definition: Math.h:189
uint CountLeadingZeros(uint32 inValue)
Compute the number of leading zero bits (how many high bits are zero)
Definition: Math.h:131
uint32 GetNextPowerOf2(uint32 inValue)
Get the next higher power of 2 of a value, or the value itself if the value is already a power of 2.
Definition: Math.h:182
T AlignUp(T inV, uint64 inAlignment)
Align inV up to the next inAlignment bytes.
Definition: Math.h:80