Nebula
Loading...
Searching...
No Matches
scalar.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
20namespace Math
21{
22#ifndef PI
23#define PI (3.1415926535897932384626433832795028841971693993751)
24#endif
25
26// the half circle
27#ifndef N_PI
28#define N_PI (Math::scalar(PI))
29#endif
30// the whole circle
31#ifndef N_PI_DOUBLE
32#define N_PI_DOUBLE (Math::scalar(6.283185307179586476925286766559))
33#endif
34// a quarter circle
35#ifndef N_PI_HALF
36#define N_PI_HALF (Math::scalar(1.5707963267948966192313216916398f))
37#endif
38// ---HOTFIXED
39
40#ifndef TINY
41#define TINY (0.0000001f)
42#endif
43#define N_TINY TINY
44
45typedef float scalar;
46typedef float float32;
47typedef double float64;
48
49#ifndef PI
50#define PI (3.1415926535897932384626433832795028841971693993751)
51#endif
52// the half circle
53#ifndef N_PI
54#define N_PI (Math::scalar(3.1415926535897932384626433832795028841971693993751))
55#endif
56
57struct float2
58{
59 union
60 {
61 struct { scalar x, y; };
63 };
64};
65
66struct float3
67{
68 union
69 {
70 struct { scalar x, y, z; };
72 };
73};
74
75struct float4
76{
77 union
78 {
79 struct { scalar x, y, z, w; };
81 };
82};
83
84struct int2
85{
86 union
87 {
88 struct { int x, y; };
89 int v[2];
90 };
91};
92
93struct int3
94{
95 union
96 {
97 struct { int x, y, z; };
98 int v[3];
99 };
100};
101
102struct int4
103{
104 union
105 {
106 struct { int x, y, z, w; };
107 int v[4];
108 };
109};
110
111struct uint2
112{
113 union
114 {
115 struct { unsigned int x, y; };
116 unsigned int v[2];
117 };
118};
119
120struct uint3
121{
122 union
123 {
124 struct { unsigned int x, y, z; };
125 unsigned int v[3];
126 };
127};
128
129struct uint4
130{
131 union
132 {
133 struct { unsigned int x, y, z, w; };
134 unsigned int v[4];
135 };
136};
137
138struct byte4u
139{
140 union
141 {
142 struct { ubyte x, y, z, w; };
143 unsigned int v;
144 };
145};
146
147struct byte4
148{
149 union
150 {
151 struct { byte x, y, z, w; };
152 unsigned int v;
153 };
154};
155
156//------------------------------------------------------------------------------
160__forceinline scalar
162{
163 return scalar(::rand()) / scalar(RAND_MAX);
164}
165
166//------------------------------------------------------------------------------
170__forceinline scalar
172{
173 scalar unit = scalar(::rand()) / RAND_MAX;
174 scalar diff = max - min;
175 return min + unit * diff;
176}
177
178//------------------------------------------------------------------------------
181__forceinline scalar
183{
184 return sinf(x);
185}
186
187//------------------------------------------------------------------------------
190__forceinline scalar
192{
193 return cosf(x);
194}
195
196//------------------------------------------------------------------------------
199__forceinline scalar
201{
202 return tanf(x);
203}
204
205//------------------------------------------------------------------------------
208__forceinline scalar
210{
211 return asinf(x);
212}
213
214//------------------------------------------------------------------------------
217__forceinline scalar
219{
220 return acosf(x);
221}
222
223//------------------------------------------------------------------------------
226__forceinline scalar
228{
229 return atanf(x);
230}
231
232//------------------------------------------------------------------------------
235__forceinline scalar
237{
238 return sqrtf(x);
239}
240
241//------------------------------------------------------------------------------
245__forceinline int
247{
249 return int(f);
250}
251
252//------------------------------------------------------------------------------
255__forceinline scalar
257{
258 return fmodf(x, y);
259}
260
261//------------------------------------------------------------------------------
267__forceinline scalar
269{
270#if 0
271 static const scalar rev = scalar(6.283185307179586476925286766559);
272 return n_fmod(a,rev);
273#else
274 static const scalar REVOLUTION = scalar(6.283185307179586476925286766559);
275 a += N_PI;
276 scalar temp = fabsf(a);
277 temp = temp - floorf(temp/REVOLUTION) * REVOLUTION;
278 temp -= N_PI;
279 temp = a<0 ? -temp:temp;
280 if(temp < scalar(-N_PI)) temp += REVOLUTION;
281 if(temp >= scalar(N_PI)) temp -= REVOLUTION;
282 return temp;
283#endif
284}
285
286//------------------------------------------------------------------------------
290__forceinline scalar
292{
293 return log2f(f);
294}
295
296//------------------------------------------------------------------------------
299__forceinline scalar
301{
302 return exp2f(x);
303}
304
305//------------------------------------------------------------------------------
309__forceinline scalar
311{
312 return logf(x);
313}
314
315//------------------------------------------------------------------------------
318__forceinline scalar
320{
321 return expf(x);
322}
323
324//------------------------------------------------------------------------------
328__forceinline int
330{
331 return fchop(floorf(f + 0.5f));
332}
333
334//------------------------------------------------------------------------------
337__forceinline scalar
339{
340 return powf(x, y);
341}
342
343//------------------------------------------------------------------------------
347__forceinline bool
349{
350 scalar f = f0 - f1;
351 return ((f > (-tol)) && (f < tol));
352}
353
354//------------------------------------------------------------------------------
357template<typename TYPE>
358__forceinline TYPE
359max(TYPE a, TYPE b)
360{
361 return (a > b) ? a : b;
362}
363
364//------------------------------------------------------------------------------
367template<typename TYPE0, typename ...TYPEN>
368__forceinline TYPE0
369max(TYPE0 first, TYPE0 second, TYPEN... rest)
370{
371 return first > second ? max(first, std::forward<TYPEN>(rest)...) : max(second, std::forward<TYPEN>(rest)...);
372}
373
374//------------------------------------------------------------------------------
378template<>
379__forceinline unsigned int
380max(unsigned int a, unsigned int b)
381{
382 return a ^ ((a ^ b) & -(a < b));
383}
384
385//------------------------------------------------------------------------------
388template<typename TYPE>
389__forceinline TYPE
390min(TYPE a, TYPE b)
391{
392 return (a < b) ? a : b;
393}
394
395//------------------------------------------------------------------------------
399template<>
400__forceinline unsigned int
401min(unsigned int a, unsigned int b)
402{
403 return b ^ ((a ^ b) & -(a < b));
404}
405
406//------------------------------------------------------------------------------
410template<>
411__forceinline uint16_t
412min(uint16_t a, uint16_t b)
413{
414 return b ^ ((a ^ b) & -(a < b));
415}
416
417//------------------------------------------------------------------------------
420template<typename TYPE0, typename ...TYPEN>
421__forceinline TYPE0
422min(TYPE0 first, TYPE0 second, TYPEN... rest)
423{
424 return first < second ? min(first, std::forward<TYPEN>(rest)...) : min(second, std::forward<TYPEN>(rest)...);
425}
426
427//------------------------------------------------------------------------------
431__forceinline scalar
433{
434 return (a < 0.0f) ? -a : a;
435}
436
437//------------------------------------------------------------------------------
440__forceinline double
441abs(double a)
442{
443 return (a < 0.0) ? -a : a;
444}
445
446//------------------------------------------------------------------------------
449__forceinline int
450abs(int a)
451{
452 return (a < 0) ? -a : a;
453}
454
455//------------------------------------------------------------------------------
458__forceinline scalar
460{
461 return (a < 0.0f) ? -1.0f : 1.0f;
462}
463
464//------------------------------------------------------------------------------
467__forceinline constexpr scalar
469{
470 return (scalar)((d * N_PI) / 180.0f);
471}
472
473//------------------------------------------------------------------------------
476__forceinline constexpr scalar
478{
479 return (scalar)((r * 180.0f) / N_PI);
480}
481
482//------------------------------------------------------------------------------
486__forceinline float
487clamp(float val, float minVal, float maxVal)
488{
489 if (val < minVal) return minVal;
490 else if (val > maxVal) return maxVal;
491 else return val;
492}
493
494//------------------------------------------------------------------------------
498__forceinline int64_t
499clamp(int64_t val, int64_t minVal, int64_t maxVal)
500{
501 if (val < minVal) return minVal;
502 else if (val > maxVal) return maxVal;
503 else return val;
504}
505
506//------------------------------------------------------------------------------
510__forceinline int32_t
511clamp(int32_t val, int32_t minVal, int32_t maxVal)
512{
513 if (val < minVal) return minVal;
514 else if (val > maxVal) return maxVal;
515 else return val;
516}
517
518//------------------------------------------------------------------------------
522__forceinline float
523ceil(float val)
524{
525 return ceilf(val);
526}
527
528//------------------------------------------------------------------------------
532__forceinline float
533floor(float val)
534{
535 return floorf(val);
536}
537
538//------------------------------------------------------------------------------
542__forceinline float
543round(float val)
544{
545 return roundf(val);
546}
547
548//------------------------------------------------------------------------------
552__forceinline bool
554{
555 return ((f0 - f1) < tol);
556}
557
558//------------------------------------------------------------------------------
562__forceinline bool
564{
565 return ((f0 - f1) > tol);
566}
567
568//------------------------------------------------------------------------------
572__forceinline scalar
574{
575 if (val < 0.0f) return 0.0f;
576 else if (val > 1.0f) return 1.0f;
577 else return val;
578}
579
580//------------------------------------------------------------------------------
584__forceinline double
585saturate(double val)
586{
587 if (val < 0.0) return 0.0;
588 else if (val > 1.0) return 1.0;
589 else return val;
590}
591
592//------------------------------------------------------------------------------
596__forceinline float
597lerp(float x, float y, float l)
598{
599 return x + l * (y - x);
600}
601
602//------------------------------------------------------------------------------
606__forceinline scalar
608{
609 scalar normFrom = modangle(from);
610 scalar normTo = modangle(to);
611 scalar dist = normTo - normFrom;
612 if (dist < deg2rad(-180.0f))
613 {
614 dist += deg2rad(360.0f);
615 }
616 else if (dist > deg2rad(180.0f))
617 {
618 dist -= deg2rad(360.0f);
619 }
620 return dist;
621}
622
623//------------------------------------------------------------------------------
627__forceinline bool
629{
630#if __GNUC__
631 union { scalar s; unsigned int u; } pun;
632 pun.s = s;
633 return ((pun.u&0x7f800000)==0);
634#else
635 return (((*(unsigned int*)&s)&0x7f800000)==0);
636#endif
637}
638
639//------------------------------------------------------------------------------
643__forceinline float
645{
646 if (isdenormal(s))
647 {
648 return 0.0f;
649 }
650 else
651 {
652 return s;
653 }
654}
655
656//------------------------------------------------------------------------------
660__forceinline bool
662{
663 return abs(a - b) <= epsilon;
664}
665
666//------------------------------------------------------------------------------
669__forceinline scalar
671{
672 return scalar(1.0) / tan(x);
673}
674
675//------------------------------------------------------------------------------
678__forceinline scalar
680{
681 // Scale, bias and saturate x to 0..1 range
682 x = saturate((x - edge0) / (edge1 - edge0));
683 // Evaluate polynomial
684 return x*x*(3-2*x);
685}
686
687//------------------------------------------------------------------------------
691__forceinline int
692irand(int min, int max)
693{
694 int range = max - min + 1;
695 int unit = ::rand() % range;
696 return min + unit;
697}
698
699//------------------------------------------------------------------------------
703__forceinline int
704mostsignificant(unsigned int val)
705{
706#ifdef WIN32
707 unsigned long ret;
708 bool res = _BitScanReverse(&ret, val);
709 ret = res ? ret : 0;
710 return ret + 1;
711#else
712 unsigned long ret;
713 ret = __builtin_clz(val);
714 return ret + 1;
715#endif
716}
717
718//------------------------------------------------------------------------------
721__forceinline unsigned int
722align(unsigned int alignant, unsigned int alignment)
723{
724 return (alignant + alignment - 1) & ~(alignment - 1);
725}
726
727//------------------------------------------------------------------------------
730__forceinline uintptr_t
731alignptr(uintptr_t alignant, uintptr_t alignment)
732{
733 return (alignant + alignment - 1) & ~(alignment - 1);
734}
735
736//------------------------------------------------------------------------------
739__forceinline unsigned int
740align_down(unsigned int alignant, unsigned int alignment)
741{
742 return (alignant / alignment * alignment);
743}
744
745//------------------------------------------------------------------------------
748__forceinline uintptr_t
749align_downptr(uintptr_t alignant, uintptr_t alignment)
750{
751 return (alignant / alignment * alignment);
752}
753
754//------------------------------------------------------------------------------
758__forceinline unsigned int
759divandroundup(unsigned int dividend, unsigned int divider)
760{
761 return (dividend % divider != 0) ? (dividend / divider + 1) : (dividend / divider);
762}
763
764//------------------------------------------------------------------------------
768__forceinline unsigned int
769roundtopow2(unsigned int val)
770{
771 val--;
772 val |= val >> 1;
773 val |= val >> 2;
774 val |= val >> 4;
775 val |= val >> 8;
776 val |= val >> 16;
777 val++;
778 return val;
779}
780
781//------------------------------------------------------------------------------
786{
788};
789
790__forceinline uint64
792{
793 uint64_t t = state.x[0];
794 uint64_t const s = state.x[1];
795 state.x[0] = s;
796 t ^= t << 23; // a
797 t ^= t >> 18; // b -- Again, the shifts and the multipliers are tunable
798 t ^= s ^ (s >> 5); // c
799 state.x[1] = t;
800 return t + s;
801}
802
803//------------------------------------------------------------------------------
807__forceinline uint32_t
808pointerhash(void* ptr)
809{
810 return (uint32_t) (((uintptr_t)(void *)ptr) % 4294967291u);
811}
812
813
814//------------------------------------------------------------------------------
818inline float
819normtofloat(uint16_t value)
820{
821 return (float)value / 65535.0f;
822}
823
824//------------------------------------------------------------------------------
828inline float
829normtofloat(int16_t value)
830{
831 return Math::max((float)value / 32767.0f, -1.0f);
832}
833
834//------------------------------------------------------------------------------
838inline float
839normtofloat(uint8_t value)
840{
841 return (float)value / 255.0f;
842}
843
844//------------------------------------------------------------------------------
848inline float
849normtofloat(int8_t value)
850{
851 return Math::max((float)value / 127.0f, -1.0f);
852}
853
854} // namespace Math
855
856//------------------------------------------------------------------------------
860inline long double
861operator"" _rad(long double deg)
862{
863 return Math::deg2rad(deg);
864}
865
866//------------------------------------------------------------------------------
869inline long double
870operator"" _rad(unsigned long long deg)
871{
872 return Math::deg2rad(deg);
873}
874
875//------------------------------------------------------------------------------
879inline long double
880operator"" _deg(long double rad)
881{
882 return Math::rad2deg(rad);
883}
884
885//------------------------------------------------------------------------------
888inline long double
889operator"" _deg(unsigned long long deg)
890{
891 return Math::rad2deg(deg);
892}
893
894//------------------------------------------------------------------------------
Different curves.
Definition angularpfeedbackloop.h:17
__forceinline uintptr_t alignptr(uintptr_t alignant, uintptr_t alignment)
Definition scalar.h:731
__forceinline float undenormalize(scalar s)
Returns 0 if scalar is denormal.
Definition scalar.h:644
__forceinline constexpr scalar rad2deg(scalar r)
Definition scalar.h:477
__forceinline int mostsignificant(unsigned int val)
Returns the position of the most significant bit of the number.
Definition scalar.h:704
__forceinline bool fequal(scalar f0, scalar f1, scalar tol)
A fuzzy floating point equality check.
Definition scalar.h:348
__forceinline scalar cot(scalar x)
Definition scalar.h:670
__forceinline scalar sqrt(scalar x)
Definition scalar.h:236
__forceinline uint64 randxor(randxorstate &state)
Definition scalar.h:791
__forceinline scalar log(scalar x)
get logarithm of x
Definition scalar.h:310
__forceinline unsigned int align_down(unsigned int alignant, unsigned int alignment)
Definition scalar.h:740
__forceinline constexpr scalar deg2rad(scalar d)
Definition scalar.h:468
__forceinline scalar asin(scalar x)
Definition scalar.h:209
__forceinline scalar saturate(scalar val)
Saturate a value (clamps between 0.0f and 1.0f)
Definition scalar.h:573
__forceinline uint32_t pointerhash(void *ptr)
cheap pointer hash using modulo with a mersenne prime
Definition scalar.h:808
__forceinline scalar log2(scalar f)
log2() function.
Definition scalar.h:291
__forceinline int fchop(scalar f)
Chop float to int.
Definition scalar.h:246
float normtofloat(uint16_t value)
convert normalized uint16_t to float
Definition scalar.h:819
__forceinline int frnd(scalar f)
Round float to integer.
Definition scalar.h:329
__forceinline float round(float val)
Floating point rounding.
Definition scalar.h:543
__forceinline float ceil(float val)
Floating point ceiling.
Definition scalar.h:523
__forceinline float lerp(float x, float y, float l)
Linearly interpolate between 2 values: ret = x + l * (y - x)
Definition scalar.h:597
__forceinline scalar modangle(scalar a)
Normalize an angular value into the range rad(0) to rad(360).
Definition scalar.h:268
__forceinline int irand(int min, int max)
Return a pseudo integer random number between min and max.
Definition scalar.h:692
__forceinline scalar pow(scalar x, scalar y)
Definition scalar.h:338
__forceinline float clamp(float val, float minVal, float maxVal)
Float clamping.
Definition scalar.h:487
__forceinline unsigned int align(unsigned int alignant, unsigned int alignment)
Definition scalar.h:722
__forceinline scalar rand()
Return a pseudo random number between 0 and 1.
Definition scalar.h:161
__forceinline TYPE min(TYPE a, TYPE b)
Definition scalar.h:390
__forceinline bool isdenormal(scalar s)
Returns true if the input scalar is denormalized (#DEN)
Definition scalar.h:628
double float64
Definition scalar.h:47
__forceinline scalar smoothstep(scalar edge0, scalar edge1, scalar x)
Definition scalar.h:679
__forceinline scalar angulardistance(scalar from, scalar to)
Get angular distance.
Definition scalar.h:607
float scalar
Definition scalar.h:45
__forceinline uintptr_t align_downptr(uintptr_t alignant, uintptr_t alignment)
Definition scalar.h:749
__forceinline scalar exp(scalar x)
Definition scalar.h:319
__forceinline scalar exp2(scalar x)
Definition scalar.h:300
__forceinline TYPE max(TYPE a, TYPE b)
Definition scalar.h:359
__forceinline scalar sgn(scalar a)
Definition scalar.h:459
__forceinline scalar abs(scalar a)
Definition scalar.h:432
__forceinline scalar cos(scalar x)
Definition scalar.h:191
__forceinline unsigned int divandroundup(unsigned int dividend, unsigned int divider)
Integer division with rounding.
Definition scalar.h:759
__forceinline scalar acos(scalar x)
Definition scalar.h:218
__forceinline bool nearequal(const point &v0, const point &v1, float epsilon)
Definition point.h:485
__forceinline scalar sin(scalar x)
Definition scalar.h:182
__forceinline bool fless(scalar f0, scalar f1, scalar tol)
A fuzzy floating point less-then check.
Definition scalar.h:553
__forceinline bool fgreater(scalar f0, scalar f1, scalar tol)
A fuzzy floating point greater-then check.
Definition scalar.h:563
__forceinline unsigned int roundtopow2(unsigned int val)
Rounds up to next power of 2.
Definition scalar.h:769
__forceinline scalar atan(scalar x)
Definition scalar.h:227
__forceinline float floor(float val)
Floating point flooring.
Definition scalar.h:533
__forceinline scalar tan(scalar x)
Definition scalar.h:200
float float32
Definition scalar.h:46
__forceinline scalar fmod(scalar x, scalar y)
Definition scalar.h:256
#define N_PI
Definition scalar.h:28
Definition scalar.h:148
byte y
Definition scalar.h:151
byte w
Definition scalar.h:151
byte z
Definition scalar.h:151
unsigned int v
Definition scalar.h:152
byte x
Definition scalar.h:151
Definition scalar.h:139
ubyte x
Definition scalar.h:142
unsigned int v
Definition scalar.h:143
ubyte z
Definition scalar.h:142
ubyte w
Definition scalar.h:142
ubyte y
Definition scalar.h:142
Definition scalar.h:58
scalar x
Definition scalar.h:61
scalar v[2]
Definition scalar.h:62
scalar y
Definition scalar.h:61
Definition scalar.h:67
scalar x
Definition scalar.h:70
scalar y
Definition scalar.h:70
scalar v[3]
Definition scalar.h:71
scalar z
Definition scalar.h:70
Definition scalar.h:76
scalar x
Definition scalar.h:79
scalar v[4]
Definition scalar.h:80
scalar y
Definition scalar.h:79
scalar w
Definition scalar.h:79
scalar z
Definition scalar.h:79
Definition scalar.h:85
int v[2]
Definition scalar.h:89
int y
Definition scalar.h:88
int x
Definition scalar.h:88
Definition scalar.h:94
int x
Definition scalar.h:97
int z
Definition scalar.h:97
int v[3]
Definition scalar.h:98
int y
Definition scalar.h:97
Definition scalar.h:103
int z
Definition scalar.h:106
int w
Definition scalar.h:106
int x
Definition scalar.h:106
int y
Definition scalar.h:106
int v[4]
Definition scalar.h:107
fast random generator based on xorshift+
Definition scalar.h:786
uint64 x[2]
Definition scalar.h:787
Definition scalar.h:112
unsigned int x
Definition scalar.h:115
unsigned int v[2]
Definition scalar.h:116
unsigned int y
Definition scalar.h:115
Definition scalar.h:121
unsigned int z
Definition scalar.h:124
unsigned int y
Definition scalar.h:124
unsigned int v[3]
Definition scalar.h:125
unsigned int x
Definition scalar.h:124
Definition scalar.h:130
unsigned int w
Definition scalar.h:133
unsigned int z
Definition scalar.h:133
unsigned int x
Definition scalar.h:133
unsigned int y
Definition scalar.h:133
unsigned int v[4]
Definition scalar.h:134
unsigned char ubyte
Definition types.h:34
uint64_t uint64
Definition types.h:36