-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgamemath.h
557 lines (472 loc) · 12.6 KB
/
gamemath.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/**
* @file
*
* @author Tiberian Technologies
* @author OmniBlade
*
* @brief Floating point math functions. Based on Tiberian Technologies Renegade Scripts wwmath class.
*
* @copyright Thyme is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version
* 2 of the License, or (at your option) any later version.
* A full copy of the GNU General Public License can be found in
* LICENSE
*/
#pragma once
#include "always.h"
#include "array.h"
#include <captainslog.h>
#include <cfloat>
#include <cstdlib>
#ifdef PROCESSOR_X86
#include <xmmintrin.h>
#endif
#ifdef BUILD_WITH_GAMEMATH
#include <gmath.h>
#else
#include <cmath>
#endif
#define GAMEMATH_EPSILON 0.0001f
#define GAMEMATH_EPSILON2 GAMEMATH_EPSILON *GAMEMATH_EPSILON
#define GAMEMATH_PI 3.141592654f
#define GAMEMATH_PI2 6.2831855f
#define GAMEMATH_PI_DBL 3.141592741012573
#define GAMEMATH_FLOAT_MAX (FLT_MAX)
#define GAMEMATH_FLOAT_MIN (FLT_MIN)
#define GAMEMATH_FLOAT_TINY (1.0e-37f)
#define GAMEMATH_SQRT2 1.414213562f
#define GAMEMATH_SQRT3 1.732050808f
#define GAMEMATH_OOSQRT2 0.707106781f
#define GAMEMATH_OOSQRT3 0.577350269f
#define GAMEMATH_TIGHT_CORNER_RADIUS 0.5f
#ifndef RAD_TO_DEG
#define RAD_TO_DEG(x) (((double)x) * 180.0 / GAMEMATH_PI_DBL)
#endif
#ifndef DEG_TO_RAD
#define DEG_TO_RAD(x) (((double)x) * GAMEMATH_PI_DBL / 180.0)
#endif
#ifndef RAD_TO_DEGF
#define RAD_TO_DEGF(x) (((float)x) * 180.0f / GAMEMATH_PI)
#endif
#ifndef DEG_TO_RADF
#define DEG_TO_RADF(x) (((float)x) * GAMEMATH_PI / 180.0f)
#endif
const int ARC_TABLE_SIZE = 1024;
const int SIN_TABLE_SIZE = 1024;
extern const Array<float, ARC_TABLE_SIZE> _FastAcosTable;
extern const Array<float, ARC_TABLE_SIZE> _FastAsinTable;
extern const Array<float, SIN_TABLE_SIZE> _FastSinTable;
extern const Array<float, SIN_TABLE_SIZE> _FastInvSinTable;
inline float Normalize_Angle(float angle)
{
#ifdef BUILD_WITH_GAMEMATH
captainslog_dbgassert(!gm_isnanf(angle), "Angle is NAN in normalizeAngle!\n");
if (gm_isnanf(angle)) {
return 0.0f;
}
#else
captainslog_dbgassert(!isnanf(angle), "Angle is NAN in normalizeAngle!\n");
if (isnanf(angle)) {
return 0.0f;
}
#endif
while (angle > GAMEMATH_PI) {
angle = angle - GAMEMATH_PI * 2;
}
while (angle <= -GAMEMATH_PI) {
angle = angle + GAMEMATH_PI * 2;
}
return angle;
}
namespace GameMath
{
void Init();
void Shutdown();
inline float Max(float v1, float v2)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_fmaxf(v1, v2);
#else
return fmaxf(v1, v2);
#endif
}
inline float Min(float v1, float v2)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_fminf(v1, v2);
#else
return fminf(v1, v2);
#endif
}
inline float Pow(float val, float power)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_powf(val, power);
#else
return powf(val, power);
#endif
}
inline float Square(float val)
{
return float(val * val);
}
inline float Fabs(float val)
{
float tmp = val;
uint32_t value = *reinterpret_cast<uint32_a *>(&tmp);
value &= 0x7fffffff;
return float(*reinterpret_cast<float_a *>(&value));
}
inline int Float_To_Int_Chop(const float &f)
{
int32_t a = *reinterpret_cast<const int32_a *>(&f);
int32_t sign = (a >> 31);
int32_t mantissa = (a & ((1 << 23) - 1)) | (1 << 23);
int32_t exponent = ((a & 0x7fffffff) >> 23) - 127;
int32_t r = ((uint32_t)(mantissa) << 8) >> (31 - exponent);
return ((r ^ (sign)) - sign) & ~(exponent >> 31);
}
inline int Float_To_Int_Floor(const float &f)
{
int32_t a = *reinterpret_cast<const int32_a *>(&f);
int32_t sign = (a >> 31);
a &= 0x7fffffff;
int32_t exponent = (a >> 23) - 127;
int32_t expsign = ~(exponent >> 31);
int32_t imask = ((1 << (31 - (exponent)))) - 1;
int32_t mantissa = (a & ((1 << 23) - 1));
int32_t r = ((uint32_t)(mantissa | (1 << 23)) << 8) >> (31 - exponent);
r = ((r & expsign) ^ (sign)) + (((!((mantissa << 8) & imask)) & (expsign ^ ((a - 1) >> 31))) & sign);
return r;
}
inline float Cos(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_cosf(val);
#else
return float(cosf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Sin(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_sinf(val);
#else
return float(sinf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Inv_Sin(float val)
{
return (val > 0.0f) ? 1.0f / Sin(val) : GAMEMATH_FLOAT_MAX;
}
inline float Sqrt(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_sqrtf(val);
#else
return float(sqrtf(val)); // IEEE standard says this is predictable for all conforming implementations.
#endif
}
inline float Inv_Sqrt(float val)
{
return float(1.0f / Sqrt(val));
}
inline float Acos(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_acosf(val);
#else
return float(acosf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Asin(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_asinf(val);
#else
return float(asinf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Atan(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_atanf(val);
#else
return float(atanf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Atan2(float y, float x)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_atan2f(y, x);
#else
return float(atan2f(y, x)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Tan(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_tanf(val);
#else
return float(tanf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Fast_Sin(float val)
{
val *= float(SIN_TABLE_SIZE) / (2.0f * GAMEMATH_PI);
int idx0 = Float_To_Int_Floor(val);
int idx1 = idx0 + 1;
float frac = val - float(idx0);
idx0 = (unsigned(idx0)) & (SIN_TABLE_SIZE - 1);
idx1 = (unsigned(idx1)) & (SIN_TABLE_SIZE - 1);
return (1.0f - frac) * _FastSinTable[idx0] + frac * _FastSinTable[idx1];
}
inline float Fast_Inv_Sin(float val)
{
return float(1.0f / Fast_Sin(val));
}
inline float Fast_Cos(float val)
{
val += (GAMEMATH_PI * 0.5f);
val *= float(SIN_TABLE_SIZE) / (2.0f * GAMEMATH_PI);
int idx0 = Float_To_Int_Floor(val);
int idx1 = idx0 + 1;
float frac = val - float(idx0);
idx0 = (unsigned(idx0)) & (SIN_TABLE_SIZE - 1);
idx1 = (unsigned(idx1)) & (SIN_TABLE_SIZE - 1);
return (1.0f - frac) * _FastSinTable[idx0] + frac * _FastSinTable[idx1];
}
inline float Fast_Inv_Cos(float val)
{
return float(1.0f / Fast_Cos(val));
}
inline float Fast_Acos(float val)
{
// Presumably the table method isn't accurate enough in this range.
if (Fabs(val) > 0.975f) {
return Acos(val);
}
val *= float(ARC_TABLE_SIZE / 2);
int idx0 = Float_To_Int_Floor(val);
int idx1 = idx0 + 1;
float frac = val - float(idx0);
idx0 += ARC_TABLE_SIZE / 2;
idx1 += ARC_TABLE_SIZE / 2;
captainslog_dbgassert((idx0 >= 0) && (idx0 < ARC_TABLE_SIZE), "Index out of table range");
captainslog_dbgassert((idx1 >= 0) && (idx1 < ARC_TABLE_SIZE), "Index out of table range");
return (1.0f - frac) * _FastAcosTable[idx0] + frac * _FastAcosTable[idx1];
}
inline float Fast_Asin(float val)
{
// Presumably the table method isn't accurate enough in this range.
if (Fabs(val) > 0.975f) {
return Asin(val);
}
val *= float(ARC_TABLE_SIZE / 2);
int idx0 = Float_To_Int_Floor(val);
int idx1 = idx0 + 1;
float frac = val - float(idx0);
idx0 += ARC_TABLE_SIZE / 2;
idx1 += ARC_TABLE_SIZE / 2;
captainslog_dbgassert((idx0 >= 0) && (idx0 < ARC_TABLE_SIZE), "Index out of table range");
captainslog_dbgassert((idx1 >= 0) && (idx1 < ARC_TABLE_SIZE), "Index out of table range");
return (1.0f - frac) * _FastAsinTable[idx0] + frac * _FastAsinTable[idx1];
}
inline float Sign(float val)
{
if (val > 0.0f) {
return +1.0f;
}
if (val < 0.0f) {
return -1.0f;
}
return 0.0f;
}
inline float Ceil(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_ceilf(val);
#else
return float(ceilf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Floor(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_floorf(val);
#else
return float(floorf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline bool Fast_Is_Float_Positive(const float &val)
{
return ((*reinterpret_cast<uint32_a const *>(&val)) & 0x80000000) == 0;
}
inline int Fast_To_Int_Floor(float val)
{
static const float _almost_one = 0.99999994f;
if (!Fast_Is_Float_Positive(val)) {
val -= _almost_one;
}
#ifdef BUILD_WITH_GAMEMATH
return gm_lrintf(gm_truncf(val));
#else
return lrintf(truncf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline int Fast_To_Int_Ceil(float val)
{
static const float _almost_one = 0.99999994f;
if (Fast_Is_Float_Positive(val)) {
val += _almost_one;
}
#ifdef BUILD_WITH_GAMEMATH
return gm_lrintf(gm_truncf(val));
#else
return lrintf(truncf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline int Fast_To_Int_Truncate(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_lrintf(gm_truncf(val));
#else
return lrintf(truncf(val)); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Random_Float()
{
return float((float(rand() & 0xFFF)) / float(0xFFF));
}
inline float Random_Float(float min, float max)
{
return float(Random_Float() * (max - min) + min);
}
inline float Wrap(float val, float min = 0.0f, float max = 1.0f)
{
if (val >= max) {
val -= (max - min);
}
if (val < min) {
val += (max - min);
}
if (val < min) {
val = min;
}
if (val > max) {
val = max;
}
return float(val);
}
// inline double Wrap(double val, double min = 0.0f, double max = 1.0f)
//{
// if ( val >= max ) {
// val -= (max - min);
// }
//
// if ( val < min ) {
// val += (max - min);
// }
//
// if ( val < min ) {
// val = min;
// }
//
// if ( val > max ) {
// val = max;
// }
//
// return static_cast<double>(val);
//}
inline float Lerp(float a, float b, float lerp)
{
return float(a + (b - a) * lerp);
}
// Do we want any double math?
// inline double Lerp(double a, double b, float lerp)
//{
// return (a + (b - a) * lerp);
//}
inline int Lerp(int a, int b, float lerp)
{
return (a + int((b - a) * lerp));
}
inline int Float_To_Long(float f)
{
#ifdef PROCESSOR_X86
return _mm_cvtt_ss2si(_mm_load_ss(&f));
#else
return int(f);
#endif
}
// Do we want any double math?
// inline long Float_To_Long(double f)
//{
//#ifdef PROCESSOR_X86
// return _mm_cvttsd_si32(_mm_load_pd(&f));
//#else
// return (int)(f);
//#endif
//}
inline uint8_t Unit_Float_To_Byte(float f)
{
return uint8_t(f * 255.0f);
}
inline float Byte_To_Unit_Float(unsigned char byte)
{
return float(byte) / 255.0f;
}
inline bool Is_Valid_Float(float x)
{
uint32_a *plong = reinterpret_cast<uint32_a *>(&x);
uint32_t exponent = ((*plong) & 0x7F800000) >> (32 - 9);
if (exponent == 0xFF) {
return false;
}
return true;
}
inline float Fast_Float_Floor(float val)
{
static const float _almost_one = 0.99999994f;
if (!Fast_Is_Float_Positive(val)) {
val -= _almost_one;
}
#ifdef BUILD_WITH_GAMEMATH
return gm_truncf(val);
#else
return truncf(val); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline float Fast_Float_Ceil(float val)
{
static const float _almost_one = 0.99999994f;
if (Fast_Is_Float_Positive(val)) {
val += _almost_one;
}
#ifdef BUILD_WITH_GAMEMATH
return gm_truncf(val);
#else
return truncf(val); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
inline int Lrintf(float val)
{
#ifdef BUILD_WITH_GAMEMATH
return gm_lrintf(val);
#else
return lrintf(val); // TODO reimplement based on fdlibm for cross platform reproducibility.
#endif
}
// inline bool Is_Valid_Double(double x)
//{
// uint32_a *plong = reinterpret_cast<uint32_a*>(&x) + 1;
// uint32_t exponent = ((*plong) & 0x7FF00000) >> (32 - 12);
//
// if ( exponent == 0x7FF ) {
// return false;
// }
//
// return true;
//}
} // namespace GameMath