-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm_common.h
332 lines (283 loc) · 6.88 KB
/
mm_common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#ifndef mm_common_h
#define mm_common_h
#include <cmath>
#include <algorithm>
#include <assert.h>
//#define MYMATH_USE_SSE2
//#define MYMATH_USE_FMA
//#define MYMATH_FORCE_INLINE
//TODO comment this out when compilers can handle it...
#define MYMATH_FAST_COMPILE
#ifdef MYMATH_USE_SSE2
#include "x86intrin.h"
#endif
#ifdef MYMATH_FORCE_INLINE
#ifdef _MSC_VER //msvc++
#define MYMATH_INLINE __forceinline
#else //g++ and clang
#define MYMATH_INLINE __attribute__((always_inline))
#endif
#else
#define MYMATH_INLINE inline
#endif
//align variables to X bytes
#ifdef _MSC_VER //msvc++
#define MYMATH_ALIGNED(x) __declspec(align(x))
#else
#define MYMATH_ALIGNED(x) alignas(x)
#endif
#ifdef MYMATH_USE_SSE2
#define MYMATH_SHUFFLE(x, y, z, w) (_MM_SHUFFLE(w, z, y, x))
#ifndef MYMATH_FAST_COMPILE
#define MM_SHUFFLE_SWIZZLE_HELPER(x,y,z,w) ((1<<(y<<1)) | (2<<(z<<1)) | (3<<(w<<1)))
#endif
#endif
#define MYMATH_GPU_ALIGNMENT 16
//align variables to 16 bytes (GPU friendly)
#define MM_16_BYTE_ALIGNED MYMATH_ALIGNED(MYMATH_GPU_ALIGNMENT)
//makes sure only explicit cast is available between vecn and float/double etc.
#define MYMATH_STRICT_GLSL 0
#define MYMATH_DOUBLE_PRECISION 0
#ifdef _WIN32
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
#ifdef near
#undef near
#endif
#ifdef far
#undef far
#endif
#endif
namespace mymath
{
static const float epsilon = 0.0001f; //should be 0.00001f
static const double depsilon = 0.00000001;
namespace impl
{
typedef int post; //this serves as a flag that an increment is a post increment
template< class t >
static bool is_eq( t a, t b )
{
return a == b;
}
static bool is_eq( float a, float b )
{
return std::abs( a - b ) < epsilon;
}
static bool is_eq( double a, double b )
{
return std::abs( a - b ) < depsilon;
}
}
static const float pi = 3.1415926535897932384626433832795f;
static const float two_pi = 2.0f * pi;
static const float pi_div_180 = pi / 180.0f;
static const float inv_pi_div_180 = 180.0f / pi;
#define MYMATH_INVERSESQRT( t ) \
MYMATH_INLINE t inversesqrt( const t& a ) \
{ \
return 1 / std::sqrt( a ); \
}
#define MYMATH_STEP( t ) \
MYMATH_INLINE t step( const t& a, const t& b ) \
{ \
return b < a ? 0 : 1; \
}
#define MYMATH_MIX( t ) \
MYMATH_INLINE t mix( const t& a, const t& b, const t& c ) \
{ \
return a * ( 1 - c ) + b * c; \
}
#define MYMATH_FRACT( t ) \
MYMATH_INLINE t fract( const t& a ) \
{ \
return a - trunc( a ); \
}
#define MYMATH_ATAN( t ) \
MYMATH_INLINE t atan( const t& a, const t& b ) \
{ \
return std::atan( b / a ); \
}
#define MYMATH_CLAMP( t ) \
MYMATH_INLINE t clamp( const t& a, const t& b, const t& c ) \
{ \
return std::min( std::max( a, b ), c ); \
}
#define MYMATH_SMOOTHSTEP( t ) \
MYMATH_INLINE t smoothstep( const t& a, const t& b, const t& c ) \
{ \
float u = ( c - a ) / ( b - a ); \
u = clamp( u, 0, 1 ); \
return u * u * ( 3 - 2 * u ); \
}
#define MYMATH_FMA( t ) \
MYMATH_INLINE t fma( const t& a, const t& b, const t& c ) \
{ \
return a * b + c; \
}
MYMATH_INLINE float radians( const float& degrees )
{
return degrees * pi_div_180;
}
MYMATH_INLINE float degrees( const float& radians )
{
return radians * inv_pi_div_180;
}
#define MYMATH_SIGN( t ) \
MYMATH_INLINE t sign( const t& num ) \
{ \
if( num > 0 ) \
{ \
return 1; \
} \
else if( num < 0 ) \
{ \
return -1; \
} \
else \
{ \
return num; \
} \
}
#define MYMATH_ASINH( t ) \
MYMATH_INLINE t asinh( const t& num ) \
{ \
return std::log( num + std::sqrt( num * num + 1 ) ); \
}
#define MYMATH_ACOSH( t ) \
MYMATH_INLINE t acosh( const t& num ) \
{ \
return std::log( num + std::sqrt( num * num - 1 ) ); \
}
#define MYMATH_ATANH( t ) \
MYMATH_INLINE t atanh( const t& num ) \
{ \
return std::log( ( 1 + num ) / ( 1 - num ) ) / 2; \
}
#define MYMATH_LOG2( t ) \
MYMATH_INLINE t log2( const t& num ) \
{ \
return std::log( num ) / std::log( 2 ); \
}
#define MYMATH_TRUNC( t ) \
MYMATH_INLINE t trunc( const t& num ) \
{ \
return num < 0 ? -floor( -num ) : floor( num ); \
}
#define MYMATH_ROUND( t ) \
MYMATH_INLINE t round( const t& num ) \
{ \
if( num < 0 ) \
{ \
return t( int( num - 0.5 ) ); \
} \
else \
{ \
return t( int( num + 0.5 ) ); \
} \
}
MYMATH_INLINE bool isnan( const float& num )
{
#ifdef _WIN32
return _isnan( num ) != 0;
#else
return std::isnan( num );
#endif
}
MYMATH_INLINE bool isinf( const float& num )
{
#ifdef _WIN32
return _fpclass( num ) == _FPCLASS_NINF || _fpclass( num ) == _FPCLASS_PINF;
#else
return std::isinf( num );
#endif
}
#define MYMATH_MIN( t ) \
MYMATH_INLINE t min( const t& a, const t& b ) \
{ \
return std::min( a, b ); \
}
#define MYMATH_MAX( t ) \
MYMATH_INLINE t max( const t& a, const t& b ) \
{ \
return std::max( a, b ); \
}
#ifdef _WIN32
#pragma warning( push )
#pragma warning( disable : 4244 )
#endif
MYMATH_STEP( float )
MYMATH_LOG2( float )
#ifdef _WIN32
#pragma warning( pop )
#pragma warning( disable : 4244 )
#endif
MYMATH_INVERSESQRT( float )
MYMATH_MIX( float )
MYMATH_FRACT( float )
MYMATH_ATAN( float )
MYMATH_CLAMP( float )
MYMATH_SMOOTHSTEP( float )
MYMATH_FMA( float )
MYMATH_SIGN( float )
MYMATH_ASINH( float )
MYMATH_ACOSH( float )
MYMATH_ATANH( float )
MYMATH_TRUNC( float )
MYMATH_ROUND( float )
MYMATH_MIN( float )
MYMATH_MAX( float )
#if MYMATH_DOUBLE_PRECISION == 1
static const double dpi = 3.1415926535897932384626433832795;
static const double dtwo_pi = 2.0 * dpi;
static const double dpi_div_180 = dpi / 180.0;
static const double dinv_pi_div_180 = 180.0 / dpi;
MYMATH_INLINE double radians( const double& degrees )
{
return degrees * dpi_div_180;
}
MYMATH_INLINE double degrees( const double& radians )
{
return radians * dinv_pi_div_180;
}
MYMATH_INLINE bool isnan( const double& num )
{
#ifdef _WIN32
return _isnan( num ) != 0;
#else
return std::isnan( num );
#endif
}
MYMATH_INLINE bool isinf( const double& num )
{
#ifdef _WIN32
return _fpclass( num ) == _FPCLASS_NINF || _fpclass( num ) == _FPCLASS_PINF;
#else
return std::isinf( num );
#endif
}
MYMATH_INVERSESQRT( double )
MYMATH_STEP( double )
MYMATH_MIX( double )
MYMATH_FRACT( double )
MYMATH_ATAN( double )
MYMATH_CLAMP( double )
MYMATH_SMOOTHSTEP( double )
MYMATH_FMA( double )
MYMATH_SIGN( double )
MYMATH_ASINH( double )
MYMATH_ACOSH( double )
MYMATH_ATANH( double )
MYMATH_LOG2( double )
MYMATH_TRUNC( double )
MYMATH_ROUND( double )
MYMATH_MIN( double )
MYMATH_MAX( double )
#endif
}
namespace mm = mymath;
#endif