OpenBarnyard
 
Loading...
Searching...
No Matches
TMathInline.h
Go to the documentation of this file.
1#pragma once
2#include "Toshi/Typedefs.h"
3#include "Toshi/TDebug.h"
4
5#include <cstdint>
6#include <cfloat>
7#include <cmath>
8
10
11namespace TMath
12{
13
14constexpr TINT8 TINT8_MAX = INT8_MAX;
15constexpr TINT8 TINT8_MIN = INT8_MIN;
16constexpr TUINT8 TUINT8_MAX = UINT8_MAX;
17
18constexpr TUINT TINT16_MAX = INT16_MAX;
19constexpr TUINT TINT16_MIN = INT16_MIN;
20constexpr TUINT TUINT16_MAX = UINT16_MAX;
21
22constexpr TINT TINT32_MAX = INT32_MAX;
23constexpr TINT TINT32_MIN = INT32_MIN;
24constexpr TUINT TUINT32_MAX = UINT32_MAX;
25
26constexpr TUINT16 MAXWCHAR = WCHAR_MAX;
27
28constexpr TUINTPTR MAXPTR = UINTPTR_MAX;
29
30constexpr TFLOAT MAXFLOAT = FLT_MAX;
31constexpr TFLOAT MINFLOAT = FLT_MIN;
32constexpr TFLOAT TFLOAT_EPSILON = FLT_EPSILON; // Renamed according to de Blob
33constexpr TFLOAT FLOATEPSILON = FLT_EPSILON; // Both exist for some reason
34
35constexpr TFLOAT PI = 3.1415927f;
36constexpr TFLOAT TWO_PI = PI * 2;
37constexpr TFLOAT HALF_PI = PI / 2;
38constexpr TFLOAT ONEOVER_SQRT_TWO = 0.70710677f;
39constexpr TFLOAT ONEOVERTWO_PI = 1 / TWO_PI;
40
41TFORCEINLINE TBOOL IsFinite( TFLOAT fVal ) { return _finite( fVal ) != 0; }
42TFORCEINLINE TFLOAT Sin( TFLOAT fVal ) { return sinf( fVal ); }
43TFORCEINLINE TFLOAT Cos( TFLOAT fVal ) { return cosf( fVal ); }
44TFORCEINLINE TFLOAT Tan( TFLOAT fVal ) { return tanf( fVal ); }
45
46// $Barnyard: FUNCTION 006b69f0
47TFORCEINLINE TFLOAT ASin( TFLOAT fVal ) { return asinf( fVal ); }
48
49// $Barnyard: FUNCTION 006b6a20
50TFORCEINLINE TFLOAT ACos( TFLOAT fVal ) { return acosf( fVal ); }
52{
53 TFLOAT fRes = atanf( fVal );
54 TASSERT( TMath::IsFinite( fRes ) );
55 return fRes;
56}
58{
59 TFLOAT fRes = atan2f( fVal1, fVal2 );
60 TASSERT( TMath::IsFinite( fRes ) );
61 return fRes;
62}
63TFORCEINLINE TFLOAT Abs( TFLOAT fVal ) { return (TFLOAT)fabs( fVal ); }
64TFORCEINLINE constexpr TFLOAT DegToRad( TFLOAT fDeg ) { return ( fDeg / 180.0f ) * PI; }
65
66TFORCEINLINE constexpr TUINT IntLog2( TUINT32 a_uiValue )
67{
68 return std::bit_width( a_uiValue ) - 1;
69}
70
71inline constexpr TUINT RoundToNextPowerOfTwo( TUINT a_uiValue )
72{
73 a_uiValue--;
74 a_uiValue |= a_uiValue >> 1;
75 a_uiValue |= a_uiValue >> 2;
76 a_uiValue |= a_uiValue >> 4;
77 a_uiValue |= a_uiValue >> 8;
78 a_uiValue |= a_uiValue >> 16;
79 a_uiValue++;
80
81 return a_uiValue;
82}
83
85{
86 TASSERT( a_fX != 0.0f );
87 TFLOAT fVal = sqrtf( a_fX );
88 TASSERT( IsFinite( fVal ) );
89 return fVal;
90}
91
93{
94 return 1.0f / Sqrt( a_fX );
95}
96
97TFORCEINLINE TINT Round( TFLOAT a_fVal ) { return (TINT)roundf( a_fVal ); }
98
99// $Barnyard: FUNCTION 006b69d0
100TFORCEINLINE TINT CeilToInt( TFLOAT a_fVal ) { return TINT( a_fVal ) - TUINT32( 0 < TUINT32( a_fVal - TUINT32( a_fVal ) ) ); }
101
102// $Barnyard: FUNCTION 006b69b0
103TFORCEINLINE TINT FloorToInt( TFLOAT a_fVal ) { return TINT( a_fVal ) - TUINT32( 0x80000000 < TUINT32( a_fVal - TUINT32( a_fVal ) ) ); }
104
105TFORCEINLINE TBOOL IsNaN( TFLOAT fVal ) { return isnan( fVal ); }
106
107TFORCEINLINE TINT FastMod( TINT a_iNum, TINT a_iModulus )
108{
109 TASSERT( a_iNum >= 0 );
110 TASSERT( a_iModulus > 0 );
111 TASSERT( 0 == ( a_iModulus & ( a_iModulus - 1 ) ) );
112 return a_iNum & ( a_iModulus - 1 );
113}
114
115// $Barnyard: FUNCTION 006b6af0
117{
118 return powf( a_fValue, a_fPow );
119}
120
121// $Barnyard: FUNCTION 006b7370
122TFORCEINLINE void SinCos( TFLOAT fVal, TFLOAT& a_rSin, TFLOAT& a_rCos )
123{
124 a_rSin = sinf( fVal );
125 a_rCos = cosf( fVal );
126}
127
128// $Barnyard: FUNCTION 006b6640
129TFORCEINLINE TFLOAT LERP( TFLOAT a, TFLOAT b, TFLOAT t ) { return a + t * ( b - a ); }
130
131// $Barnyard: FUNCTION 006b6660
133{
134 TFLOAT fResult = LERP( a, b, t );
135
136 if ( ( a < b && b < fResult ) || ( b < a && fResult < b ) )
137 return b;
138
139 return fResult;
140}
141
142// $Barnyard: FUNCTION 006b6810
144{
145 if ( PI < a_rfValue )
146 a_rfValue = fmodf( a_rfValue + PI, PI * 2 ) - PI;
147 else if ( a_rfValue < -PI )
148 a_rfValue = fmodf( a_rfValue - PI, PI * 2 ) + PI;
149}
150
151// $Barnyard: FUNCTION 006b6870
152// $Barnyard: FUNCTION 006b68a0
153template <typename T>
154TFORCEINLINE void Clip( T& rVal, const T& Min, const T& Max )
155{
156 if ( Max < rVal ) { rVal = Max; }
157 if ( rVal < Min ) { rVal = Min; }
158}
159
160template <typename T>
161TFORCEINLINE const T& Min( const T& a, const T& b )
162{
163 return ( a < b ) ? a : b;
164}
165
166template <typename T>
167TFORCEINLINE const T& Max( const T& a, const T& b )
168{
169 return ( a < b ) ? b : a;
170}
171
172} // namespace TMath
173
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TFORCEINLINE
Definition Defines.h:74
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
uint16_t TUINT16
Definition Typedefs.h:15
unsigned int TUINT
Definition Typedefs.h:8
uintptr_t TUINTPTR
Definition Typedefs.h:18
uint8_t TUINT8
Definition Typedefs.h:17
float TFLOAT
Definition Typedefs.h:4
uint32_t TUINT32
Definition Typedefs.h:13
int TINT
Definition Typedefs.h:7
bool TBOOL
Definition Typedefs.h:6
int8_t TINT8
Definition Typedefs.h:16
Definition TMath.h:12
constexpr TFLOAT MAXFLOAT
Definition TMathInline.h:30
TFORCEINLINE void SinCos(TFLOAT fVal, TFLOAT &a_rSin, TFLOAT &a_rCos)
TFORCEINLINE TFLOAT ATan(TFLOAT fVal)
Definition TMathInline.h:51
constexpr TUINT TINT16_MIN
Definition TMathInline.h:19
TFORCEINLINE TFLOAT Abs(TFLOAT fVal)
Definition TMathInline.h:63
TFORCEINLINE TFLOAT ATan2(TFLOAT fVal1, TFLOAT fVal2)
Definition TMathInline.h:57
constexpr TUINT8 TUINT8_MAX
Definition TMathInline.h:16
constexpr TINT TINT32_MIN
Definition TMathInline.h:23
constexpr TINT8 TINT8_MAX
Definition TMathInline.h:14
TFORCEINLINE TFLOAT Tan(TFLOAT fVal)
Definition TMathInline.h:44
constexpr TFLOAT ONEOVER_SQRT_TWO
Definition TMathInline.h:38
TFORCEINLINE TFLOAT Pow(TFLOAT a_fValue, TFLOAT a_fPow)
constexpr TFLOAT ONEOVERTWO_PI
Definition TMathInline.h:39
TFORCEINLINE void Clip(T &rVal, const T &Min, const T &Max)
TFORCEINLINE TBOOL IsFinite(TFLOAT fVal)
Definition TMathInline.h:41
constexpr TFLOAT MINFLOAT
Definition TMathInline.h:31
constexpr TFLOAT TFLOAT_EPSILON
Definition TMathInline.h:32
TFORCEINLINE TINT CeilToInt(TFLOAT a_fVal)
constexpr TINT8 TINT8_MIN
Definition TMathInline.h:15
TFORCEINLINE TINT FastMod(TINT a_iNum, TINT a_iModulus)
constexpr TUINT RoundToNextPowerOfTwo(TUINT a_uiValue)
Definition TMathInline.h:71
TFORCEINLINE void NormaliseAngle(TFLOAT &a_rfValue)
TFORCEINLINE TBOOL IsNaN(TFLOAT fVal)
TFORCEINLINE const T & Max(const T &a, const T &b)
TFORCEINLINE TINT Round(TFLOAT a_fVal)
Definition TMathInline.h:97
constexpr TFLOAT HALF_PI
Definition TMathInline.h:37
constexpr TUINT TINT16_MAX
Definition TMathInline.h:18
TFORCEINLINE constexpr TFLOAT DegToRad(TFLOAT fDeg)
Definition TMathInline.h:64
constexpr TINT TINT32_MAX
Definition TMathInline.h:22
TFORCEINLINE const T & Min(const T &a, const T &b)
constexpr TFLOAT FLOATEPSILON
Definition TMathInline.h:33
TFORCEINLINE TFLOAT Cos(TFLOAT fVal)
Definition TMathInline.h:43
TFORCEINLINE TINT FloorToInt(TFLOAT a_fVal)
constexpr TUINT TUINT32_MAX
Definition TMathInline.h:24
TFORCEINLINE constexpr TUINT IntLog2(TUINT32 a_uiValue)
Definition TMathInline.h:66
TFORCEINLINE TFLOAT OneOverSqrt(TFLOAT a_fX)
Definition TMathInline.h:92
constexpr TFLOAT PI
Definition TMathInline.h:35
constexpr TFLOAT TWO_PI
Definition TMathInline.h:36
constexpr TUINT TUINT16_MAX
Definition TMathInline.h:20
TFORCEINLINE TFLOAT Sqrt(TFLOAT a_fX)
Definition TMathInline.h:84
TFORCEINLINE TFLOAT LERP(TFLOAT a, TFLOAT b, TFLOAT t)
constexpr TUINT16 MAXWCHAR
Definition TMathInline.h:26
TFORCEINLINE TFLOAT ASin(TFLOAT fVal)
Definition TMathInline.h:47
TFORCEINLINE TFLOAT Sin(TFLOAT fVal)
Definition TMathInline.h:42
TFORCEINLINE TFLOAT LERPClamped(TFLOAT a, TFLOAT b, TFLOAT t)
TFORCEINLINE TFLOAT ACos(TFLOAT fVal)
Definition TMathInline.h:50
constexpr TUINTPTR MAXPTR
Definition TMathInline.h:28