-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathMathFunc.cpp
More file actions
756 lines (663 loc) · 15.6 KB
/
MathFunc.cpp
File metadata and controls
756 lines (663 loc) · 15.6 KB
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/* Copyright Jukka Jylänki
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
/** @file MathFunc.cpp
@author Jukka Jylänki
@brief Common mathematical functions. */
#include "MathFunc.h"
#include "Swap.h"
#include "SSEMath.h"
#ifdef MATH_ENABLE_STL_SUPPORT
#include <utility>
#include <algorithm>
#endif
#include "myassert.h"
#include "float2.h"
#include "float4.h"
#ifdef MATH_WITH_GRISU3
#include "grisu3.h"
#endif
#ifdef WIN32
#include "../Math/InclWindows.h"
#endif
#ifdef MATH_SSE2
#include "sse_mathfun.h"
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/math.h>
#endif
MATH_BEGIN_NAMESPACE
bool mathBreakOnAssume =
#ifdef MATH_STARTUP_BREAK_ON_ASSUME
true;
#else
false;
#endif
void SetMathBreakOnAssume(bool isEnabled)
{
mathBreakOnAssume = isEnabled;
}
/// Returns the current state of the math break-on-assume flag.
bool MathBreakOnAssume()
{
return mathBreakOnAssume;
}
bool AssumeFailed()
{
#ifndef OPTIMIZED_RELEASE
if (mathBreakOnAssume)
{
#if defined(WIN32) && !defined(WIN8RT) // Win8 metro apps don't have DebugBreak.
DebugBreak();
#elif defined(__EMSCRIPTEN__)
emscripten_debugger();
// TODO: Test locally on Linux GCC and enable
//#elif defined(__clang__) && __has_builtin(__builtin_debugtrap)
// __builtin_debugtrap();
#endif
}
#endif
// If your debugger is breaking in this function, it means that an assume() failure has occurred,
// or a LOGE()/LOGW() failure has occurred, and building with trap-to-debugger enabled. Navigate
// up the callstack to find the offending code that raised the error.
return mathBreakOnAssume;
}
#if defined(__EMSCRIPTEN__) && !defined(MATH_USE_SINCOS_LOOKUPTABLE)
// On Emscripten using lookup tables has been profiled to be significantly faster.
#define MATH_USE_SINCOS_LOOKUPTABLE
#endif
#define MAX_CIRCLE_ANGLE 65536
#define HALF_MAX_CIRCLE_ANGLE (MAX_CIRCLE_ANGLE/2)
#define QUARTER_MAX_CIRCLE_ANGLE (MAX_CIRCLE_ANGLE/4)
#define MASK_MAX_CIRCLE_ANGLE (MAX_CIRCLE_ANGLE - 1)
#define PI 3.14159265358979323846f
#ifdef MATH_USE_SINCOS_LOOKUPTABLE
// A lookup table implementation adapted from http://www.flipcode.com/archives/Fast_Trigonometry_Functions_Using_Lookup_Tables.shtml
static float fast_cossin_table[MAX_CIRCLE_ANGLE]; // Declare table of fast cosinus and sinus
class Init_fast_cossin_table
{
public:
Init_fast_cossin_table()
{
// Build cossin table
for(int i = 0; i < MAX_CIRCLE_ANGLE; i++)
#ifdef __EMSCRIPTEN__
fast_cossin_table[i] = (float)emscripten_math_sin((double)i * PI / HALF_MAX_CIRCLE_ANGLE);
#else
fast_cossin_table[i] = (float)sin((double)i * PI / HALF_MAX_CIRCLE_ANGLE);
#endif
}
};
Init_fast_cossin_table static_initializer;
static inline float sin_lookuptable(float n)
{
int i = (int)(n * (HALF_MAX_CIRCLE_ANGLE / PI));
if (i < 0) return -fast_cossin_table[(-i) & MASK_MAX_CIRCLE_ANGLE];
else return fast_cossin_table[i & MASK_MAX_CIRCLE_ANGLE];
}
static inline float cos_lookuptable(float n)
{
int i = (int)(n * (HALF_MAX_CIRCLE_ANGLE / PI));
if (i < 0) return fast_cossin_table[(QUARTER_MAX_CIRCLE_ANGLE - i) & MASK_MAX_CIRCLE_ANGLE];
else return fast_cossin_table[(QUARTER_MAX_CIRCLE_ANGLE + i) & MASK_MAX_CIRCLE_ANGLE];
}
static inline void sincos_lookuptable(float n, float &sinOut, float &cosOut)
{
int i = (int)(n * (HALF_MAX_CIRCLE_ANGLE / PI));
i = (i >= 0) ? (i & MASK_MAX_CIRCLE_ANGLE) : (MAX_CIRCLE_ANGLE - ((-i) & MASK_MAX_CIRCLE_ANGLE));
sinOut = fast_cossin_table[i];
cosOut = fast_cossin_table[(QUARTER_MAX_CIRCLE_ANGLE + i) & MASK_MAX_CIRCLE_ANGLE];
}
static inline void sincos_lookuptable_u16ScaledRadians(u16 u16ScaledRadians, float &sinOut, float &cosOut)
{
sinOut = fast_cossin_table[u16ScaledRadians];
cosOut = fast_cossin_table[(u16)(QUARTER_MAX_CIRCLE_ANGLE + u16ScaledRadians)];
}
#endif
#ifdef MATH_SSE2
static const __m128 pi2 = _mm_set1_ps(2.f*pi);
#endif
float Sin(float angleRadians)
{
#ifdef MATH_USE_SINCOS_LOOKUPTABLE
return sin_lookuptable(angleRadians);
#elif defined(MATH_SSE2)
// Do range reduction by 2pi before calling sin - this enchances precision of sin_ps a lot
return s4f_x(sin_ps(modf_ps(setx_ps(angleRadians), pi2)));
#else
return sinf(angleRadians);
#endif
}
float Cos(float angleRadians)
{
#ifdef MATH_USE_SINCOS_LOOKUPTABLE
return cos_lookuptable(angleRadians);
#elif defined(MATH_SSE2)
// Do range reduction by 2pi before calling cos - this enchances precision of cos_ps a lot
return s4f_x(cos_ps(modf_ps(setx_ps(angleRadians), pi2)));
#else
return cosf(angleRadians);
#endif
}
float Tan(float angleRadians)
{
#ifdef __EMSCRIPTEN__
// Use Math.tan() for minimal code size
return emscripten_math_tan(angleRadians);
#else
return tanf(angleRadians);
#endif
}
void SinCos(float angleRadians, float &outSin, float &outCos)
{
#ifdef MATH_USE_SINCOS_LOOKUPTABLE
return sincos_lookuptable(angleRadians, outSin, outCos);
#elif defined(MATH_SSE2)
__m128 angle = modf_ps(setx_ps(angleRadians), pi2);
__m128 sin, cos;
sincos_ps(angle, &sin, &cos);
outSin = s4f_x(sin);
outCos = s4f_x(cos);
#else
outSin = Sin(angleRadians);
outCos = Cos(angleRadians);
#endif
}
void SinCosU16ScaledRadians(u16 u16ScaledRadians, float &outSin, float &outCos)
{
#ifdef MATH_USE_SINCOS_LOOKUPTABLE
return sincos_lookuptable_u16ScaledRadians(u16ScaledRadians, outSin, outCos);
#elif defined(MATH_SSE2)
float angleRadians = u16ScaledRadians * (PI / HALF_MAX_CIRCLE_ANGLE);
__m128 angle = modf_ps(setx_ps(angleRadians), pi2);
__m128 sin, cos;
sincos_ps(angle, &sin, &cos);
outSin = s4f_x(sin);
outCos = s4f_x(cos);
#else
float angleRadians = u16ScaledRadians * (PI / HALF_MAX_CIRCLE_ANGLE);
outSin = Sin(angleRadians);
outCos = Cos(angleRadians);
#endif
}
void SinCos2(const float4 &angleRadians, float4 &outSin, float4 &outCos)
{
#ifdef MATH_SSE2
__m128 angle = modf_ps(angleRadians.v, pi2);
sincos_ps(angle, &outSin.v, &outCos.v);
#else
SinCos(angleRadians.x, outSin.x, outCos.x);
SinCos(angleRadians.y, outSin.y, outCos.y);
#endif
}
void SinCos3(const float4 &angleRadians, float4 &outSin, float4 &outCos)
{
#ifdef MATH_SSE2
__m128 angle = modf_ps(angleRadians.v, pi2);
sincos_ps(angle, &outSin.v, &outCos.v);
#else
SinCos(angleRadians.x, outSin.x, outCos.x);
SinCos(angleRadians.y, outSin.y, outCos.y);
SinCos(angleRadians.z, outSin.z, outCos.z);
#endif
}
void SinCos4(const float4 &angleRadians, float4 &outSin, float4 &outCos)
{
#ifdef MATH_SSE2
__m128 angle = modf_ps(angleRadians.v, pi2);
sincos_ps(angle, &outSin.v, &outCos.v);
#else
SinCos(angleRadians.x, outSin.x, outCos.x);
SinCos(angleRadians.y, outSin.y, outCos.y);
SinCos(angleRadians.z, outSin.z, outCos.z);
SinCos(angleRadians.w, outSin.w, outCos.w);
#endif
}
float Asin(float x)
{
#ifdef __EMSCRIPTEN__
// Use Math.asin() for minimal code size
return emscripten_math_asin(x);
#else
return asinf(x);
#endif
}
float Acos(float x)
{
#ifdef __EMSCRIPTEN__
// Use Math.acos() for minimal code size
return emscripten_math_acos(x);
#else
return acosf(x);
#endif
}
float Atan(float x)
{
#ifdef __EMSCRIPTEN__
// Use Math.atan() for minimal code size
return emscripten_math_atan(x);
#else
return atanf(x);
#endif
}
float Atan2(float y, float x)
{
#ifdef __EMSCRIPTEN__
// Use Math.atan2() for minimal code size
return emscripten_math_atan2(y, x);
#else
return atan2f(y, x);
#endif
}
float Sinh(float x)
{
#ifdef __EMSCRIPTEN__
// Use Math.sinh() for minimal code size
return emscripten_math_sinh(x);
#else
return sinhf(x);
#endif
}
float Cosh(float x)
{
#ifdef __EMSCRIPTEN__
// Use Math.atan() for minimal code size
return emscripten_math_cosh(x);
#else
return coshf(x);
#endif
}
float Tanh(float x)
{
#ifdef __EMSCRIPTEN__
// Use Math.tanh() for minimal code size
return emscripten_math_tanh(x);
#else
return tanhf(x);
#endif
}
bool IsPow2(u32 number)
{
return (number & (number-1)) == 0;
}
bool IsPow2(u64 number)
{
return (number & (number-1)) == 0;
}
u32 RoundUpPow2(u32 x)
{
assert(sizeof(u32) == 4);
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
++x;
return x;
}
u64 RoundUpPow2(u64 x)
{
assert(sizeof(u64) == 8);
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
++x;
return x;
}
u32 RoundDownPow2(u32 x)
{
assert(sizeof(u32) == 4);
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x - (x >> 1);
}
u64 RoundDownPow2(u64 x)
{
assert(sizeof(u64) == 8);
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
return x - (x >> 1);
}
int RoundIntUpToMultipleOfPow2(int x, int n)
{
assert(IsPow2(n));
return (x + n-1) & ~(n-1);
}
s64 RoundIntUpToMultipleOfPow2(s64 x, s64 n)
{
assert(IsPow2(n));
return (x + n-1) & ~(n-1);
}
float Pow(float base, float exponent)
{
return pow(base, exponent);
}
float Exp(float exponent)
{
return exp(exponent);
}
float Log(float base, float value)
{
return log(value) / log(base);
}
float Log2(float value)
{
return Log(2.f, value);
}
float Ln(float value)
{
return log(value);
}
float Log10(float value)
{
return Log(10.f, value);
}
float Ceil(float x)
{
return ceilf(x);
}
int CeilInt(float x)
{
return (int)ceilf(x);
}
float Floor(float x)
{
return floorf(x);
}
int FloorInt(float x)
{
return (int)floorf(x);
}
float Round(float x)
{
return Floor(x+0.5f);
}
int RoundInt(float x)
{
return (int)Round(x);
}
float Sign(float x)
{
return x >= 0.f ? 1.f : -1.f;
}
float SignOrZero(float x, float epsilon)
{
return Abs(x) <= epsilon ? 0.f : Sign(x);
}
float Lerp(float a, float b, float t)
{
return a + t * (b-a);
}
float LerpMod(float a, float b, float mod, float t)
{
a = ModPos(a, mod);
b = ModPos(b, mod);
if (Abs(b-a) * 2.f <= mod)
return Lerp(a, b, t);
else
{
if (a < b)
return ModPos(Lerp(a + mod, b, t), mod);
else
return ModPos(Lerp(a, b + mod, t), mod);
}
}
float InvLerp(float a, float b, float x)
{
assume(Abs(b-a) > 1e-5f);
return (x - a) / (b - a);
}
float Step(float y, float x)
{
return (x >= y) ? 1.f : 0.f;
}
float Ramp(float min, float max, float x)
{
return x <= min ? 0.f : (x >= max ? 1.f : (x - min) / (max - min));
}
float PingPongMod(float x, float mod)
{
x = Mod(x, mod * 2.f);
return x >= mod ? (2.f * mod - x) : x;
}
float Mod(float x, float mod)
{
return fmod(x, mod);
}
float Mod(float x, int mod)
{
///@todo Optimize.
return fmod(x, (float)mod);
}
float ModPos(float x, float mod)
{
float m = fmod(x, mod);
return m >= 0.f ? m : (m + mod);
}
float ModPos(float x, int mod)
{
///@todo Optimize.
return ModPos(x, (float)mod);
}
float Frac(float x)
{
return x - Floor(x);
}
/** Uses a recursive approach, not the fastest/brightest method.
Note that 13! = 6227020800 overflows already.
@return n! = n * (n-1) * (n-2) * ... * 1. */
int Factorial(int n)
{
int result = 1;
for(int i = 2; i <= n; i++)
result *= i;
return result;
}
/** @return Binomial coefficients with recursion, i.e. n choose k, C(n,k) or nCk. */
int CombinatorialRec(int n, int k)
{
/* We could do:
return factorial(n)/(factorial(n-k)*factorial(k));
But prefer the recursive approach instead, because it's not so prone
to numerical overflow. This approach uses the idea of the Pascal triangle. */
if (k <= 0 || k >= n)
return 1;
else
return CombinatorialRec(n-1,k-1) + CombinatorialRec(n-1,k);
}
/** @return Binomial coefficients by tabulation, i.e. n choose k, C(n,k) or nCk. */
int CombinatorialTab(int n, int k)
{
if (k == 0 || k == n)
return 1;
if (k < 0 || k > n)
return 0;
// We use two auxiliary tables, one of size n-2 and one of size n-3.
int *table = new int[2*(k+1)]; ///@todo We can lower this size.
for(int i = 0; i < 2*(k+1); ++i)
table[i] = 1;
int *t1 = &table[0];
int *t2 = &table[k+1];
// Iteratively fill the tables.
for(int i = 2; i <= n; ++i)
{
for(int j = Max(1, i-n+k); j <= Min(k,i-1); ++j)
t1[j] = t2[j] + t2[j-1];
Swap(t1, t2);
}
int c = t2[k];
delete[] table;
return c;
}
float PowUInt(float base, u32 exponent)
{
// 'Fast Exponentiation': We interpret exponent in base two and calculate the power by
// squaring and multiplying by base.
// Find the highest bit that is set.
u32 e = 0x80000000;
while((exponent & e) == 0 && e > 0)
e >>= 1;
float val = 1.f;
do
{
val *= val; // Shifts the exponent one place left
val *= (exponent & e) != 0 ? base : 1.f; // Adds a 1 as the LSB of the exponent
e >>= 1;
} while(e > 0);
return val;
}
/** @param base Exponent base value.
@param exponent Integer exponent to raise base to.
@return pow(base,exponent) but optimized because we only use integer exponent. */
float PowInt(float base, int exponent)
{
if (exponent == 0)
return 1.f;
else if (exponent < 0)
return 1.f / PowUInt(base, (u32)-exponent);
else
return PowUInt(base, (u32)exponent);
}
char *SerializeFloat(float f, char *dstStr)
{
if (!IsNan(f))
{
#ifdef MATH_WITH_GRISU3
int numChars = dtoa_grisu3((double)f, dstStr);
return dstStr + numChars;
#else
return dstStr + sprintf(dstStr, "%.17g", f);
#endif
}
else
{
u32 u = ReinterpretAsU32(f);
int numChars = sprintf(dstStr, "NaN(%8X)", (unsigned int)u);
return dstStr + numChars;
}
}
float DeserializeFloat(const char *str, const char **outEndStr)
{
if (!str)
return FLOAT_NAN;
while(*str > 0 && *str <= ' ')
++str;
if (*str == 0)
return FLOAT_NAN;
if (MATH_NEXT_WORD_IS(str, "NaN("))
{
str += strlen("NaN("); //MATH_SKIP_WORD(str, "NaN(");
u32 x;
int n = sscanf(str, "%X", (unsigned int *)&x);
if (n != 1)
return FLOAT_NAN;
while(*str != 0)
{
++str;
if (*str == ')')
{
++str;
break;
}
}
if (outEndStr)
*outEndStr = str;
return ReinterpretAsFloat(x);
}
float f;
if (!strncmp(str, "-inf", 4)) { f = -FLOAT_INF; str += 4; }
else if (!strncmp(str, "inf", 3)) { f = FLOAT_INF; str += 3; }
else f = (float)strtod(str, const_cast<char**>(&str));
while(*str > 0 && *str <= ' ')
++str;
if (*str == ',' || *str == ';')
++str;
while(*str > 0 && *str <= ' ')
++str;
if (outEndStr)
*outEndStr = str;
return f;
}
int hexstr_to_u64(const char *str, uint64_t *u)
{
assert(u);
*u = 0;
const char *s = str;
for(int i = 0; i <= 16; ++i)
{
char ch = *s;
if (ch >= '0' && ch <= '9')
*u = 16 * *u + ch - '0';
else if (ch >= 'a' && ch <= 'f')
*u = 16 * *u + 10 + ch - 'a';
else if (ch >= 'A' && ch <= 'F')
*u = 16 * *u + 10 + ch - 'A';
else
break;
++s;
}
return (int)(s - str);
}
double DeserializeDouble(const char *str, const char **outEndStr)
{
if (!str)
return (double)FLOAT_NAN;
while(*str > 0 && *str <= ' ')
++str;
if (*str == 0)
return (double)FLOAT_NAN;
if (MATH_NEXT_WORD_IS(str, "NaN("))
{
str += strlen("NaN("); //MATH_SKIP_WORD(str, "NaN(");
// Read 64-bit unsigned hex representation of the NaN. TODO: Make this more efficient without using sscanf.
uint64_t u;
int nChars = hexstr_to_u64(str, &u);
str += nChars;
while(*str != 0)
{
++str;
if (*str == ')')
{
++str;
break;
}
}
if (outEndStr)
*outEndStr = str;
return ReinterpretAsDouble(u);
}
double f;
if (!strncmp(str, "-inf", 4)) { f = (double)-FLOAT_INF; str += 4; }
else if (!strncmp(str, "inf", 3)) { f = (double)FLOAT_INF; str += 3; }
else f = strtod(str, const_cast<char**>(&str));
while(*str > 0 && *str <= ' ')
++str;
if (*str == ',' || *str == ';')
++str;
while(*str > 0 && *str <= ' ')
++str;
if (outEndStr)
*outEndStr = str;
return f;
}
MATH_END_NAMESPACE