-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastMath.h
More file actions
4672 lines (4485 loc) · 161 KB
/
FastMath.h
File metadata and controls
4672 lines (4485 loc) · 161 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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
FAST MATH
C++ single header math libary
https://github.com/Czapa10/Fast-Math
Made by Grzegorz "Czapa" Bednorz
YOU HAVE TO
#define FM_IMPLEMENTATION
in one of C++ files that include this header, BEFORE the include, like this:
#define FM_IMPLEMENTATION
#include <FastMath.h>
*/
#ifndef FAST_MATH_H
#define FAST_MATH_H
#include <stdint.h>
#include <cmath>
#ifndef FM_USE_SSE2_INSTEAD_OF_SSE4
#include <smmintrin.h>
#else
#include <emmintrin.h>
#endif
#ifdef _MSC_VER
__pragma(warning(push))
__pragma(warning(disable : 4201))
__pragma(warning(disable : 4146))
#endif
// TODO: make FM_SINL work on all compilers
#define FM_INL __forceinline
#define FM_SINL static __forceinline
#define FM_CALL __vectorcall
#define FM_FUN auto
#define FM_FUN_T template<t> auto
#define FM_FUN_I FM_INL auto
#define FM_FUN_C auto FM_CALL
#define FM_FUN_TI template<class t> FM_INL auto
#define FM_FUN_IC FM_INL auto FM_CALL
#define FM_FUN_SI static FM_INL auto
#define FM_FUN_TSI template<class t> static FM_INL auto
#define FM_FUN_2T template<class t> template<class u> auto
#define FM_FUN_SIC static FM_INL auto FM_CALL
#define FM_ArrayCount(_Arr) (sizeof(_Arr)/sizeof((_Arr)[0]))
#define FM_GENERIC_FUNCTION(Macro) \
Macro(, float) Macro(d, double) \
Macro(i, int32_t) Macro(u, uint32_t) \
Macro(i64, int64_t) Macro(u64, uint64_t) \
Macro(i16, int16_t) Macro(u16, uint16_t) \
Macro(i8, int8_t) Macro(u8, uint8_t)
#ifdef NDEBUG
#ifdef _MSC_VER
#define FM_ASSERT(expression) if(!expression)__debugbreak()
#define FM_ERROR() __debugbreak()
#else
#define FM_ASSERT(expression) if(!expression){*(int32_t*)0 = 0}
#define FM_ERROR() {*(int32_t*)0 = 0}
#endif
#else
#define FM_ASSERT(expression)
#define FM_ERROR()
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4715)
#endif
namespace fm {
//////////////////////
// type definitions //
//////////////////////
template<class t> concept v2_concept = requires(t Object)
{
{Object.IsFastMathV2()};
};
#define FM_generic_v2 fm::v2_concept auto
template<class t>
union v2_base
{
struct { t X, Y; };
struct { t U, V; };
struct { t Width, Height; };
struct { t W, H; };
struct { t Left, Right; };
t Elements[2];
v2_base(t X, t Y) :X(X), Y(Y) {}
explicit v2_base(t XY) :X(XY), Y(XY) {}
explicit v2_base(const t* Mem) :X(Mem[0]), Y(Mem[1]) {}
v2_base() = default;
template<class u> explicit v2_base(v2_base<u> V)
:X((t)V.X), Y((t)V.Y) {}
FM_FUN_I operator[](uint32_t Index) -> t&;
constexpr void IsFastMathV2() {}
};
using v2 = v2_base<float>;
using v2d = v2_base<double>;
using v2i = v2_base<int32_t>;
using v2u = v2_base<uint32_t>;
using v2i64 = v2_base<int64_t>;
using v2u64 = v2_base<uint64_t>;
using v2i16 = v2_base<int16_t>;
using v2u16 = v2_base<uint16_t>;
using v2i8 = v2_base<int8_t>;
using v2u8 = v2_base<uint8_t>;
template<class t> concept v3_concept = requires(t Object)
{
{Object.IsFastMathV3()};
};
#define FM_generic_v3 fm::v3_concept auto
template<class t>
union v3_base
{
struct { t X, Y, Z; };
struct { t U, V, W; };
struct { t R, G, B; };
struct { t Width, Height, Depth; };
struct { v2_base<t> XY; };
struct { t Placeholder1_; v2_base<t> YZ; };
struct { v2_base<t> UV; };
struct { t Placeholder2_; v2_base<t> VW; };
struct { v2_base<t> RG; };
struct { t Placeholder3_; v2_base<t> GB; };
t Elements[3];
v3_base(t X, t Y, t Z) :X(X), Y(Y), Z(Z) {}
v3_base(v2_base<t> XY_, t Z_ = 0){ XY = XY_; Z = Z_; }
v3_base(t X, v2_base<t> YZ) :X(X), YZ(YZ) {}
explicit v3_base(t XYZ) :X(XYZ), Y(XYZ), Z(XYZ) {}
explicit v3_base(const t* Mem) :X(Mem[0]), Y(Mem[1]), Z(Mem[2]) {}
v3_base() = default;
template<class u> explicit v3_base(v3_base<u> V)
:X((t)V.X), Y((t)V.Y), Z((t)V.Z) {}
FM_FUN_I operator[](uint32_t Index) -> t&;
FM_FUN_I ZXY() -> v3_base<t>;
constexpr void IsFastMathV3() {}
};
using v3 = v3_base<float>;
using v3d = v3_base<double>;
using v3i = v3_base<int32_t>;
using v3u = v3_base<uint32_t>;
using v3i64 = v3_base<int64_t>;
using v3u64 = v3_base<uint64_t>;
using v3i16 = v3_base<int16_t>;
using v3u16 = v3_base<uint16_t>;
using v3i8 = v3_base<int8_t>;
using v3u8 = v3_base<uint8_t>;
template<class t> concept v4_concept = requires(t Object)
{
{Object.IsFastMathV4()};
};
#define FM_generic_v4 fm::v4_concept auto
template<class t> union v4_base
{
struct { t X, Y, Z, W; };
struct { t R, G, B, A; };
v3_base<t> XYZ;
v3_base<t> RGB;
struct
{
v2_base<t> XY;
v2_base<t> ZW;
};
struct
{
t Placeholder1_;
v2_base<t> YZ;
};
struct
{
v2_base<t> RG;
v2_base<t> BA;
};
struct
{
t Placeholder2_;
v2_base<t> GB;
};
t Elements[4];
v4_base(t X, t Y, t Z, t W) :X(X), Y(Y), Z(Z), W(W) {}
v4_base(v2_base<t> XY, v2_base<t> ZW = {}) :XY(XY), ZW(ZW) {}
v4_base(v2_base<t> XY_, t Z_ = 0, t W_ = 0) { XY = XY_; Z = Z_; W = W_; }
v4_base(v3_base<t> XYZ_, t W_ = 0) { XYZ = XYZ_; W = W_; }
v4_base(t X, v3_base<t> YZW) :X(X), YZ(YZW.XY), W(YZW.Z) {}
explicit v4_base(t XYZW) :X(XYZW), Y(XYZW), Z(XYZW), W(XYZW) {}
explicit v4_base(const t* Mem) :X(Mem[0]), Y(Mem[1]), Z(Mem[2]), W(Mem[3]) {}
v4_base() = default;
template<class u> explicit v4_base(v4_base<u> V)
:X((t)V.X), Y((t)V.Y), Z((t)V.Z), W((t)V.W) {}
FM_FUN_I operator[](uint32_t Index) -> t&;
constexpr void IsFastMathV4() {}
};
using v4 = v4_base<float>;
using v4d = v4_base<double>;
using v4i = v4_base<int32_t>;
using v4u = v4_base<uint32_t>;
using v4i64 = v4_base<int64_t>;
using v4u64 = v4_base<uint64_t>;
using v4i16 = v4_base<int16_t>;
using v4u16 = v4_base<uint16_t>;
using v4i8 = v4_base<int8_t>;
using v4u8 = v4_base<uint8_t>;
struct alignas(16) vec2
{
__m128 M;
FM_INL void FM_CALL SetX(float);
FM_INL void FM_CALL SetY(float);
FM_INL float FM_CALL X() const;
FM_INL float FM_CALL U() const { return X(); }
FM_INL float FM_CALL Left() const { return X(); }
FM_INL float FM_CALL Width() const { return X(); }
FM_INL float FM_CALL Y() const;
FM_INL float FM_CALL V() const { return Y(); }
FM_INL float FM_CALL Top() const { return Y(); }
FM_INL float FM_CALL Height() const { return Y(); }
FM_INL vec2 FM_CALL YX() const;
FM_INL vec2 FM_CALL XX() const;
FM_INL vec2 FM_CALL YY() const;
FM_INL void FM_CALL AddX(float);
FM_INL void FM_CALL AddY(float);
FM_INL void FM_CALL SubX(float);
FM_INL void FM_CALL SubY(float);
FM_INL void FM_CALL MulX(float);
FM_INL void FM_CALL MulY(float);
FM_INL void FM_CALL DivX(float);
FM_INL void FM_CALL DivY(float);
FM_INL float& operator[](uint32_t Index);
};
struct alignas(16) vec2d
{
__m128d M;
FM_INL void FM_CALL SetX(double X);
FM_INL void FM_CALL SetY(double Y);
FM_INL double FM_CALL X() const;
FM_INL double FM_CALL U() const { return X(); }
FM_INL double FM_CALL Left() const { return X(); }
FM_INL double FM_CALL Width() const { return X(); }
FM_INL double FM_CALL Y() const;
FM_INL double FM_CALL V() const { return Y(); }
FM_INL double FM_CALL Height() const { return Y(); }
FM_INL double FM_CALL Top() const { return Y(); }
FM_INL vec2d FM_CALL YX() const;
FM_INL vec2d FM_CALL XX() const;
FM_INL vec2d FM_CALL YY() const;
FM_INL vec2d FM_CALL VU() const { return YX(); }
FM_INL vec2d FM_CALL UU() const { return XX(); }
FM_INL vec2d FM_CALL VV() const { return YY(); }
FM_INL void FM_CALL AddX(double);
FM_INL void FM_CALL AddY(double);
FM_INL void FM_CALL SubX(double);
FM_INL void FM_CALL SubY(double);
FM_INL void FM_CALL MulX(double);
FM_INL void FM_CALL MulY(double);
FM_INL void FM_CALL DivX(double);
FM_INL void FM_CALL DivY(double);
FM_INL double& operator[](uint32_t Index);
// TODO: Copy constructor and assign operator
};
struct alignas(16) vec2i
{
__m128i M;
FM_INL void FM_CALL SetX(int32_t);
FM_INL void FM_CALL SetY(int32_t);
FM_INL int32_t FM_CALL X() const;
FM_INL int32_t FM_CALL U() const { return X(); }
FM_INL int32_t FM_CALL Left() const { return X(); }
FM_INL int32_t FM_CALL Width() const { return X(); }
FM_INL int32_t FM_CALL Y() const;
FM_INL int32_t FM_CALL V() const { return Y(); }
FM_INL int32_t FM_CALL Top() const { return Y(); }
FM_INL int32_t FM_CALL Height() const { return Y(); }
FM_INL vec2i FM_CALL YX() const;
FM_INL vec2i FM_CALL XX() const;
FM_INL vec2i FM_CALL YY() const;
FM_INL vec2i FM_CALL VU() const { return YX(); }
FM_INL vec2i FM_CALL UU() const { return XX(); }
FM_INL vec2i FM_CALL VV() const { return YY(); }
FM_INL void FM_CALL AddX(int32_t);
FM_INL void FM_CALL AddY(int32_t);
FM_INL void FM_CALL SubX(int32_t);
FM_INL void FM_CALL SubY(int32_t);
FM_INL void FM_CALL MulX(int32_t);
FM_INL void FM_CALL MulY(int32_t);
FM_INL void FM_CALL DivX(int32_t);
FM_INL void FM_CALL DivY(int32_t);
FM_INL int32_t& operator[](uint32_t Index);
// TODO: Copy constructor and assign operator
};
struct alignas(16) vec2u
{
__m128i M;
FM_INL void FM_CALL SetX(uint32_t);
FM_INL void FM_CALL SetY(uint32_t);
FM_INL uint32_t FM_CALL X() const;
FM_INL uint32_t FM_CALL U() const { return X(); }
FM_INL uint32_t FM_CALL Left() const { return X(); }
FM_INL uint32_t FM_CALL Width() const { return X(); }
FM_INL uint32_t FM_CALL Y() const;
FM_INL uint32_t FM_CALL V() const { return Y(); }
FM_INL uint32_t FM_CALL Top() const { return Y(); }
FM_INL uint32_t FM_CALL Height() const { return Y(); }
FM_INL vec2u FM_CALL YX() const;
FM_INL vec2u FM_CALL XX() const;
FM_INL vec2u FM_CALL YY() const;
FM_INL vec2u FM_CALL VU() const { return YX(); }
FM_INL vec2u FM_CALL UU() const { return XX(); }
FM_INL vec2u FM_CALL VV() const { return YY(); }
FM_INL void FM_CALL AddX(uint32_t);
FM_INL void FM_CALL AddY(uint32_t);
FM_INL void FM_CALL SubX(uint32_t);
FM_INL void FM_CALL SubY(uint32_t);
FM_INL void FM_CALL MulX(uint32_t);
FM_INL void FM_CALL MulY(uint32_t);
FM_INL void FM_CALL DivX(uint32_t);
FM_INL void FM_CALL DivY(uint32_t);
FM_INL uint32_t& operator[](uint32_t Index);
// TODO: Copy constructor and assign operator
};
struct alignas(16) vec3
{
__m128 M;
FM_INL void FM_CALL SetX(float X);
FM_INL void FM_CALL SetY(float Y);
FM_INL void FM_CALL SetZ(float Z);
FM_INL void FM_CALL AddX(float X);
FM_INL void FM_CALL AddY(float Y);
FM_INL void FM_CALL AddZ(float Z);
FM_INL void FM_CALL SubX(float X);
FM_INL void FM_CALL SubY(float Y);
FM_INL void FM_CALL SubZ(float Z);
FM_INL void FM_CALL MulX(float X);
FM_INL void FM_CALL MulY(float Y);
FM_INL void FM_CALL MulZ(float Z);
FM_INL void FM_CALL DivX(float X);
FM_INL void FM_CALL DivY(float Y);
FM_INL void FM_CALL DivZ(float Z);
FM_INL float FM_CALL X() const;
FM_INL float FM_CALL U() const { return X(); }
FM_INL float FM_CALL R() const { return X(); }
FM_INL float FM_CALL Y() const;
FM_INL float FM_CALL V() const { return Y(); }
FM_INL float FM_CALL G() const { return Y(); }
FM_INL float FM_CALL Z() const;
FM_INL float FM_CALL W() const { return Z(); }
FM_INL float FM_CALL B() const { return Z(); }
FM_INL vec3 FM_CALL ZXY() const;
FM_INL float& operator[](uint32_t Index);
};
struct alignas(16) vec4
{
__m128 M;
FM_INL void FM_CALL SetX(float);
FM_INL void FM_CALL SetY(float);
FM_INL void FM_CALL SetZ(float);
FM_INL void FM_CALL SetW(float);
FM_INL void FM_CALL AddX(float);
FM_INL void FM_CALL AddY(float);
FM_INL void FM_CALL AddZ(float);
FM_INL void FM_CALL AddW(float);
FM_INL void FM_CALL SubX(float);
FM_INL void FM_CALL SubY(float);
FM_INL void FM_CALL SubZ(float);
FM_INL void FM_CALL SubW(float);
FM_INL void FM_CALL MulX(float);
FM_INL void FM_CALL MulY(float);
FM_INL void FM_CALL MulZ(float);
FM_INL void FM_CALL MulW(float);
FM_INL void FM_CALL DivX(float);
FM_INL void FM_CALL DivY(float);
FM_INL void FM_CALL DivZ(float);
FM_INL void FM_CALL DivW(float);
FM_INL float FM_CALL X() const;
FM_INL float FM_CALL Y() const;
FM_INL float FM_CALL Z() const;
FM_INL float FM_CALL W() const;
FM_INL float FM_CALL R() const { return X(); }
FM_INL float FM_CALL G() const { return Y(); }
FM_INL float FM_CALL B() const { return Z(); }
FM_INL float FM_CALL A() const { return W(); }
FM_INL float& operator[](uint32_t Index);
};
template<class t> concept rect2_concept = requires(t Object)
{
{Object.IsFastMathRect2()};
};
#define FM_generic_rect2 fm::rect2_concept auto
template<class t>
struct rect2_base
{
v2_base<t> Min, Max;
rect2_base() = default;
template<class u> rect2_base(rect2_base<u>);
constexpr void IsFastMathRect2() {}
};
using rect2 = rect2_base<float>;
using rect2d = rect2_base<double>;
using rect2i = rect2_base<int32_t>;
using rect2u = rect2_base<uint32_t>;
using rect2i64 = rect2_base<int64_t>;
using rect2u64 = rect2_base<uint64_t>;
using rect2i16 = rect2_base<int16_t>;
using rect2u16 = rect2_base<uint16_t>;
using rect2i8 = rect2_base<int8_t>;
using rect2u8 = rect2_base<uint8_t>;
struct alignas(16) mat4
{
__m128 Columns[4];
FM_FUN_IC GetColumnV4(uint32_t Index) -> v4;
FM_FUN_IC GetColumnVec4(uint32_t Index) -> vec4;
FM_FUN_IC SetColumn(uint32_t Index, v4 Col) -> void;
FM_FUN_IC SetColumn(uint32_t Index, vec4 Col) -> void;
FM_FUN_IC SetColumn(uint32_t Index, float X, float Y, float Z, float W) -> void;
FM_FUN_IC SwapColumns(uint32_t Col1Index, uint32_t Col2Index) -> void;
FM_FUN_C GetRowV4(uint32_t Index) -> v4;
FM_FUN_C GetRowVec4(uint32_t Index) -> vec4;
FM_FUN_C SetRow(uint32_t Index, v4 Row) -> void;
FM_FUN_C SetRow(uint32_t Index, vec4 Row) -> void;
FM_FUN_IC SetRow(uint32_t Index, float X, float Y, float Z, float W) -> void;
FM_FUN_IC SwapRows(uint32_t Row1Index, uint32_t Row2Index) -> void;
FM_FUN_IC GetMainDiagonalV4() -> v4;
FM_FUN_IC GetMainDiagonalVec4() -> vec4;
FM_FUN_IC SetMainDiagonal(float X, float Y, float Z, float W) -> void;
FM_FUN_IC SetMainDiagonal(v4) -> void;
FM_FUN_IC SetMainDiagonal(vec4) -> void;
FM_FUN_I operator[](uint32_t Index) -> float&;
};
///////////////
// constants //
///////////////
static constexpr uint8_t MaxU8 = 255;
static constexpr uint16_t MaxU16 = 65535;
static constexpr uint32_t MaxU32 = 4294967295;
static constexpr uint64_t MaxU64 = 18446744073709551615;
static constexpr int8_t MinI8 = -128;
static constexpr int8_t MaxI8 = 127;
static constexpr int16_t MinI16 = -32768;
static constexpr int16_t MaxI16 = 32767;
static constexpr int32_t MinI32 = -2147483648;
static constexpr int32_t MaxI32 = 2147483647;
static constexpr int64_t MinI64 = -9223372036854775808;
static constexpr int64_t MaxI64 = 9223372036854775807;
static constexpr float MinPositiveF32 = 1.401298464e-45f;
static constexpr float MaxF32 = 3.402823466e+38f;
static constexpr float MinF32 = -MaxF32;
static constexpr double MinPositiveF64 = 4.9406564584124654e-324;
static constexpr double MaxF64 = 1.7976931348623158e+308;
static constexpr double MinF64 = -MaxF64;
static constexpr float EpsilonF32 = 1.19209290E-07f;
static constexpr double EpsilonF64 = 2.2204460492503131e-16;
static constexpr float Pi32 = 3.14159265359f;
static constexpr double Pi64 = 3.14159265358979323846;
static constexpr float Tau32 = Pi32 * 2;
static constexpr double Tau64 = Pi64 * 2;
template<class t> class constants
{
static constexpr bool IsSpecialized = false;
static constexpr t Epsilon() { return {}; }
static constexpr t Min() { return {}; }
static constexpr t Max() { return {}; }
static constexpr t Pi() { return {}; }
static constexpr t Tau() { return {}; }
static constexpr t MinPositive() { return {}; }
};
template<> class constants<float>
{
static constexpr bool IsSpecialized = true;
static constexpr float Epsilon() { return EpsilonF32; }
static constexpr float Min() { return MinF32; }
static constexpr float Max() { return MaxF32; }
static constexpr float Pi() { return Pi32; }
static constexpr float Tau() { return Tau32; }
static constexpr float MinPositive() { return MinPositiveF32; }
};
template<> class constants<double>
{
static constexpr bool IsSpecialized = true;
static constexpr double Epsilon() { return EpsilonF64; }
static constexpr double Min() { return MinF64; }
static constexpr double Max() { return MaxF64; }
static constexpr double Pi() { return Pi64; }
static constexpr double Tau() { return Tau64; }
static constexpr double MinPositive() { return MinPositiveF64; }
};
template<> class constants<uint8_t>
{
static constexpr bool IsSpecialized = true;
static constexpr uint8_t Epsilon() { return 0; }
static constexpr uint8_t Min() { return 0; }
static constexpr uint8_t Max() { return MaxU8; }
static constexpr uint8_t Pi() { return 0; }
static constexpr uint8_t Tau() { return 0; }
static constexpr uint8_t MinPositive() { return 1; }
};
template<> class constants<int8_t>
{
static constexpr bool IsSpecialized = true;
static constexpr int8_t Epsilon() { return 0; }
static constexpr int8_t Min() { return MinI8; }
static constexpr int8_t Max() { return MaxI8; }
static constexpr int8_t Pi() { return 0; }
static constexpr int8_t Tau() { return 0; }
static constexpr int8_t MinPositive() { return 1; }
};
template<> class constants<uint16_t>
{
static constexpr bool IsSpecialized = true;
static constexpr uint16_t Epsilon() { return 0; }
static constexpr uint16_t Min() { return 0; }
static constexpr uint16_t Max() { return MaxU16; }
static constexpr uint16_t Pi() { return 0; }
static constexpr uint16_t Tau() { return 0; }
static constexpr uint16_t MinPositive() { return 1; }
};
template<> class constants<int16_t>
{
static constexpr bool IsSpecialized = true;
static constexpr int16_t Epsilon() { return 0; }
static constexpr int16_t Min() { return MinI16; }
static constexpr int16_t Max() { return MaxI16; }
static constexpr int16_t Pi() { return 0; }
static constexpr int16_t Tau() { return 0; }
static constexpr int16_t MinPositive() { return 1; }
};
template<> class constants<uint32_t>
{
static constexpr bool IsSpecialized = true;
static constexpr uint32_t Epsilon() { return 0; }
static constexpr uint32_t Min() { return 0; }
static constexpr uint32_t Max() { return MaxU32; }
static constexpr uint32_t Pi() { return 0; }
static constexpr uint32_t Tau() { return 0; }
static constexpr uint32_t MinPositive() { return 1; }
};
template<> class constants<int32_t>
{
static constexpr bool IsSpecialized = true;
static constexpr int32_t Epsilon() { return 0; }
static constexpr int32_t Min() { return MinI32; }
static constexpr int32_t Max() { return MaxI32; }
static constexpr int32_t Pi() { return 0; }
static constexpr int32_t Tau() { return 0; }
static constexpr int32_t MinPositive() { return 1; }
};
template<> class constants<uint64_t>
{
static constexpr bool IsSpecialized = true;
static constexpr uint64_t Epsilon() { return 0; }
static constexpr uint64_t Min() { return 0; }
static constexpr uint64_t Max() { return MaxU64; }
static constexpr uint64_t Pi() { return 0; }
static constexpr uint64_t Tau() { return 0; }
static constexpr uint64_t MinPositive() { return 1; }
};
template<> class constants<int64_t>
{
static constexpr bool IsSpecialized = true;
static constexpr int64_t Epsilon() { return 0; }
static constexpr int64_t Min() { return MinI64; }
static constexpr int64_t Max() { return MaxI64; }
static constexpr int64_t Pi() { return 0; }
static constexpr int64_t Tau() { return 0; }
static constexpr int64_t MinPositive() { return 1; }
};
////////////////////////////////////
// invalid values - simple types //
////////////////////////////////////
static constexpr uint16_t InvalidU16 = MaxU16;
static constexpr uint32_t InvalidU32 = MaxU32;
static constexpr uint64_t InvalidU64 = MaxU64;
static constexpr int16_t InvalidI16 = MinI16;
static constexpr int32_t InvalidI32 = MinI32;
static constexpr int64_t InvalidI64 = MinI64;
static constexpr float InvalidF32 = MinF32;
static constexpr double InvalidF64 = MinF64;
///////////////////////////////////
// forward function declarations //
///////////////////////////////////
FM_SINL void FM_CALL Store(float* Mem, vec2 V);
FM_SINL void FM_CALL Store(float* Mem, vec3 V);
FM_SINL void FM_CALL Store(float* Mem, vec4 V);
FM_SINL vec2 FM_CALL Vec2(v2 V);
FM_SINL vec3 FM_CALL Vec3(v3 V);
FM_SINL vec4 FM_CALL Vec4(v4 V);
///////////////////////
// utility functions //
///////////////////////
FM_FUN_TSI Negate(t* A) -> void {
*A = -(*A);
}
FM_FUN_TSI Min(t A, t B) -> t {
return A < B ? A : B;
}
FM_FUN_TSI Max(t A, t B) -> t {
return A > B ? A : B;
}
FM_FUN_TSI Abs(t A) -> t {
return A < 0 ? -A : A;
}
FM_FUN_TSI Abs(t* A) -> void {
*A = *A < 0 ? -(*A) : *A;
}
FM_FUN_TSI Square(t A) -> t {
return A * A;
}
FM_FUN_TSI Square(t* A) -> void {
*A = (*A) * (*A);
}
FM_FUN_TSI IsWithinRange(t Min, t A, t Max) -> bool {
return A >= Min && A <= Max;
}
FM_FUN_TSI Clamp(t Min, t Value, t Max) -> t {
if(Value < Min)
Value = Min;
else if(Value > Max)
Value = Max;
return Value;
}
FM_FUN_TSI Clamp01(t Value) -> t {
return Clamp((t)0, Value, (t)1);
}
FM_FUN_TSI ClampAboveZero(t Value) -> t {
return Value < (t)0 ? (t)0 : Value;
}
FM_FUN_TSI Clamp(t Min, t* Value, t Max) -> void {
*Value = Clamp(Min, *Value, Max);
}
FM_FUN_TSI Clamp01(t* Value) -> void {
*Value = Clamp01(*Value);
}
FM_FUN_TSI ClampAboveZero(t* Value) -> void {
*Value = ClampAboveZero(*Value);
}
FM_FUN_SI RadiansToDegrees(float Radians) -> float {
return Radians * 180.f / Pi32;
}
FM_FUN_SI RadiansToDegrees(double Radians) -> double {
return Radians * 180.0 / Pi64;
}
FM_FUN_SI DegreesToRadians(float Degrees) -> float {
return Degrees * Pi32 / 180.f;
}
FM_FUN_SI DegreesToRadians(double Degrees) -> double {
return Degrees * Pi64 / 180.0;
}
FM_FUN_SI PeriodicClampRotationDegrees(float Degrees) -> float {
if(Degrees > 360)
Degrees -= 360;
else if(Degrees < 0)
Degrees += 360;
return Degrees;
}
FM_FUN_SI PeriodicClampRotationDegrees(double Degrees) -> double {
if(Degrees > 360)
Degrees -= 360;
else if(Degrees < 0)
Degrees += 360;
return Degrees;
}
FM_FUN_SI PeriodicClampRotationDegrees(float* Degrees) -> void {
*Degrees = PeriodicClampRotationDegrees(*Degrees);
}
FM_FUN_SI PeriodicClampRotationDegrees(double* Degrees) -> void {
*Degrees = PeriodicClampRotationDegrees(*Degrees);
}
FM_FUN_SI DifferenceBetweenDegrees(float A, float B) -> float {
FM_ASSERT(A >= 0 && B >= 0);
float Diff = Abs(B - A);
if(Diff > 180)
Diff = 360 - Diff;
return Diff;
}
FM_FUN_TSI SafeDivN(t Numerator, t Divisor, t N) -> t {
if(Divisor != 0)
return Numerator / Divisor;
return N;
}
FM_FUN_TSI SafeDiv1(t Numerator, t Divisor) -> t {
return SafeDivN<t>(Numerator, Divisor, 1);
}
FM_FUN_TSI SafeDiv0(t Numerator, t Divisor) -> t {
return SafeDivN<t>(Numerator, Divisor, 0);
}
FM_FUN_SI Lerp(float Source, float Dest, float T) -> float {
return ((1.f - T) * Source) + (T * Dest);
}
FM_FUN_SI Lerp(double Source, double Dest, double T) -> double {
return ((1.0 - T) * Source) + (T * Dest);
}
FM_FUN_SI FastLerp(float Source, float Dest, float T) -> float {
return Source + (Dest - Source) * T;
}
FM_FUN_SI FastLerp(double Source, double Dest, double T) -> double {
return Source + (Dest - Source) * T;
}
FM_FUN_SI Equal(float A, float B, float Epsilon = 0.0001f) -> bool {
float Difference = Abs(A - B);
return Difference < Epsilon;
}
FM_FUN_SI Equal(double A, double B, double Epsilon = 0.0001) -> bool {
double Difference = Abs(A - B);
return Difference < Epsilon;
}
/////////////////////////////////////////
// fast math internal helper functions //
/////////////////////////////////////////
namespace priv {
FM_SINL __m128 FM_CALL SetX(__m128 m, float x) {
return _mm_move_ss(m, _mm_set_ss(x));
}
FM_SINL __m128 FM_CALL SetY(__m128 m, float y) {
__m128 temp = _mm_move_ss(m, _mm_set_ss(y));
temp = _mm_shuffle_ps(temp, temp, _MM_SHUFFLE(3, 2, 0, 0));
return _mm_move_ss(temp, m);
}
FM_SINL __m128 FM_CALL SetZ(__m128 m, float z) {
__m128 temp = _mm_move_ss(m, _mm_set_ss(z));
temp = _mm_shuffle_ps(temp, temp, _MM_SHUFFLE(3, 0, 1, 0));
return _mm_move_ss(temp, m);
}
FM_SINL __m128 FM_CALL SetW(__m128 m, float w) {
__m128 temp = _mm_move_ss(m, _mm_set_ss(w));
temp = _mm_shuffle_ps(temp, temp, _MM_SHUFFLE(0, 2, 1, 0));
return _mm_move_ss(temp, m);
}
FM_SINL float FM_CALL GetX(__m128 m) {
return _mm_cvtss_f32(m);
}
FM_SINL float FM_CALL GetY(__m128 m) {
return _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(3, 2, 1, 1)));
}
FM_SINL float FM_CALL GetZ(__m128 m) {
return _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(3, 2, 1, 2)));
}
FM_SINL float FM_CALL GetW(__m128 m) {
return _mm_cvtss_f32(_mm_shuffle_ps(m, m, _MM_SHUFFLE(3, 2, 1, 3)));
}
FM_SINL float FM_CALL SumOfElements(__m128 m) {
return GetX(m) + GetY(m) + GetZ(m) + GetW(m);
}
}
//////////////////
// v2 functions //
//////////////////
FM_FUN_TI v2_base<t>::operator[](uint32_t Index) -> t& {
FM_ASSERT(Index == 0 || Index == 1);
return Elements[Index];
}
FM_FUN_TSI Ptr(v2_base<t>& V) -> t* {
return &V.X;
}
FM_FUN_TSI PtrY(v2_base<t>& V) -> t* {
return &V.Y;
}
FM_FUN_TSI Store(t* Mem, v2_base<t> V) -> void {
Mem[0] = V.X;
Mem[1] = V.Y;
}
FM_FUN_TSI operator+(v2_base<t> A, v2_base<t> B) -> v2_base<t> {
v2_base<t> R;
R.X = A.X + B.X;
R.Y = A.Y + B.Y;
return R;
}
FM_FUN_TSI operator-(v2_base<t> A, v2_base<t> B) -> v2_base<t> {
v2_base<t> R;
R.X = A.X - B.X;
R.Y = A.Y - B.Y;
return R;
}
FM_FUN_TSI operator+=(v2_base<t>& A, v2_base<t> B) -> v2_base<t> {
A.X += B.X;
A.Y += B.Y;
return A;
}
FM_FUN_TSI operator-=(v2_base<t>& A, v2_base<t> B) -> v2_base<t> {
A.X -= B.X;
A.Y -= B.Y;
return A;
}
FM_FUN_TSI AddX(v2_base<t> A, t X) -> v2_base<t> {
A.X += X;
return A;
}
FM_FUN_TSI AddY(v2_base<t> A, t Y) -> v2_base<t> {
A.Y += Y;
return A;
}
FM_FUN_TSI MulX(v2_base<t> A, t X) -> v2_base<t> {
A.X *= X;
return A;
}
FM_FUN_TSI MulY(v2_base<t> A, t Y) -> v2_base<t> {
A.Y *= Y;
return A;
}
FM_FUN_TSI DivX(v2_base<t> A, t X) -> v2_base<t> {
A.X /= X;
return A;
}
FM_FUN_TSI DivY(v2_base<t> A, t Y) -> v2_base<t> {
A.Y /= Y;
return A;
}
FM_FUN_TSI HadamardMul(v2_base<t> A, v2_base<t> B) -> v2_base<t> {
v2_base<t> R;
R.X = A.X * B.X;
R.Y = A.Y * B.Y;
return R;
}
FM_FUN_TSI HadamardDiv(v2_base<t> A, v2_base<t> B) -> v2_base<t> {
v2_base<t> R;
R.X = A.X / B.X;
R.Y = A.Y / B.Y;
return R;
}
FM_FUN_TSI SafeHadamardDivN(v2_base<t> A, v2_base<t> B, v2_base<t> N) -> v2_base<t> {
v2_base<t> R;
R.X = B.X != 0 ? A.X / B.X : N.X;
R.Y = B.Y != 0 ? A.Y / B.Y : N.Y;
return R;
}
FM_FUN_TSI SafeHadamardDiv1(v2_base<t> A, v2_base<t> B) -> v2_base<t> {
return SafeHadamardDivN<t>(A, B, v2_base<t>((t)1));
}
FM_FUN_TSI SafeHadamardDiv0(v2_base<t> A, v2_base<t> B) -> v2_base<t> {
return SafeHadamardDivN<t>(A, B, v2_base<t>((t)0));
}
FM_FUN_TSI operator*(v2_base<t> V, t Scalar) -> v2_base<t> {
V.X *= Scalar;
V.Y *= Scalar;
return V;
}
FM_FUN_TSI operator*(t Scalar, v2_base<t> V) -> v2_base<t> {
return V * Scalar;
}
FM_FUN_TSI operator*=(v2_base<t>& V, t Scalar) -> v2_base<t>& {
V = V * Scalar;
return V;
}
FM_FUN_TSI operator/(v2_base<t> V, t Scalar) -> v2_base<t> {
V.X /= Scalar;
V.Y /= Scalar;
return V;
}
FM_FUN_TSI operator/=(v2_base<t>& V, t Scalar) -> v2_base<t>& {
V = V / Scalar;
return V;
}
FM_FUN_TSI operator-(v2_base<t> V) -> v2_base<t> {
V.X = -V.X;
V.Y = -V.Y;
return V;
}
FM_FUN_TSI Dot(v2_base<t> A, v2_base<t> B) -> t {
return A.X * B.X + A.Y * B.Y;
}
FM_FUN_TSI Min(v2_base<t> A, v2_base<t> B) -> v2_base<t> {
v2_base<t> R;
R.X = Min(A.X, B.X);
R.Y = Min(A.Y, B.Y);
return R;
}
FM_FUN_TSI Max(v2_base<t> A, v2_base<t> B) -> v2_base<t> {
v2_base<t> R;
R.X = Max(A.X, B.X);
R.Y = Max(A.Y, B.Y);
return R;
}
FM_FUN_TSI Abs(v2_base<t> V) -> v2_base<t> {
v2_base<t> R;
R.X = Abs(V.X);
R.Y = Abs(V.Y);
return R;
}
FM_FUN_TSI SumOfElements(v2_base<t> V) -> t {