-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsylvester.h
3331 lines (2924 loc) · 93.9 KB
/
sylvester.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
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
// Sylvester - optimized linear math library
// https://github.com/hsnovel/Sylvester
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
//
// MIT License
// Copyright (c) Çağan Korkmaz <cagankorkmaz35@gmail.com>
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/* Sylvester
*
* ===========================================================================
*
* IMPORTANT
*
* DO NOT ACCESS VARIABLE OR FUNCTION THAT START WITH UNDERSCORE. THEY ARE
* INTERNAL ONLY AND CHANGING IT MIGHT BREAK THE LIBRARY OR CAUSE UNEXPECTED
* BEHAVIOURS !
*
* ===========================================================================
*
*/
#ifndef SYLVESTER_H
#define SYLVESTER_H
#if defined(SYL_ENABLE_AVX)
#include <immintrin.h>
#elif defined(SYL_ENABLE_SSE4)
#include <smmintrin.h>
#endif
#include <string.h>
#include <math.h>
#include <stdbool.h>
#if defined(__clang__)
# define _SYL_SET_SPEC_ALIGN(x) __attribute__((aligned(x)))
#elif defined(__GNUC__) || defined(__GNUG__)
# define _SYL_SET_SPEC_ALIGN(x) __attribute__((aligned(x)))
#elif defined(_MSC_VER)
# define _SYL_SET_SPEC_ALIGN(x) __declspec(align(x))
#else
# define _SYL_SET_SPEC_ALIGN(x)
#endif
#define SYL_INLINE
/* Doing it the normal way fucks up emacs indentation*/
#define _SYL_CPP_EXTER_START extern "C" {
#define _SYL_CPP_EXTERN_END }
#ifdef __cplusplus
_SYL_CPP_EXTER_START
#endif
typedef union svec2
{
struct { float x, y; };
struct { float s; float t; };
float e[2];
#if defined(SYL_ENABLE_SSE4)
__m64 v;
#endif
} _SYL_SET_SPEC_ALIGN(8) svec2;
typedef union svec3
{
struct { float x; float y; float z; };
struct { float r; float g; float b; };
struct { float s; float t; float p; };
float e[3];
} svec3;
typedef union svec4
{
struct { float x; float y; float z; float w; };
struct { float r; float g; float b; float a; };
struct { float s; float t; float m; float q; };
float e[4];
#if defined(SYL_ENABLE_SSE4)
__m128 v;
#endif
} _SYL_SET_SPEC_ALIGN(16) svec4;
/* We use column-major matricies */
typedef union smat4
{
struct
{
float m00, m01, m02, m03;
float m10, m11, m12, m13;
float m20, m21, m22, m23;
float m30, m31, m32, m33;
};
float e[16];
float e2[4][4];
svec4 v4d[4];
#if defined(SYL_ENABLE_SSE4)
__m128 v[4];
#endif
#if defined(SYL_ENABLE_AVX)
__m256 v2[2];
#endif
} _SYL_SET_SPEC_ALIGN(16) smat4;
SYL_INLINE float s_radian_to_degree(float Radian);
SYL_INLINE float s_degree_to_radian(float Degree);
SYL_INLINE float s_roundf(float A);
SYL_INLINE double s_roundd(double A);
SYL_INLINE float s_ceilf(float A);
SYL_INLINE double s_ceild(double A);
SYL_INLINE float s_floorf(float A);
SYL_INLINE double s_floord(double A);
SYL_INLINE svec4 s_bgra_unpack(int Color);
SYL_INLINE unsigned int s_bgra_pack(svec4 Color);
SYL_INLINE svec4 s_rgba_unpack(unsigned int Color);
SYL_INLINE unsigned int s_rgba_pack(svec4 Color);
SYL_INLINE float s_clampf(float Value, float Min, float Max);
SYL_INLINE float s_clampd(float Value, float Min, float Max);
SYL_INLINE int s_clampi(int Value, int Min, int Max);
SYL_INLINE float s_clamp01f(float Value);
SYL_INLINE double s_clamp01d(double Value);
SYL_INLINE float s_clamp_above_zero(float Value);
SYL_INLINE float s_clamp_below_zero(float Value);
SYL_INLINE bool s_is_in_range(float Value, float Min, float Max);
SYL_INLINE float s_lerp(float A, float t, float B);
SYL_INLINE float s_square(float x);
SYL_INLINE float s_abs(float x);
SYL_INLINE float s_pythagorean(float x, float y);
SYL_INLINE float s_maxf(float x, float y);
SYL_INLINE int s_maxi(int x, int y);
SYL_INLINE int s_mini(int x, int y);
SYL_INLINE float s_minf(float x, float y);
SYL_INLINE float s_mod(float x, float y);
SYL_INLINE float s_pow(float Value, float Times);
SYL_INLINE float s_truncatef(float Value, float Remain);
SYL_INLINE double s_truncated(double Value, double Places);
SYL_INLINE float s_normalize(float Value, float Min, float Max);
SYL_INLINE float s_map(float Value, float SourceMin, float SourceMax, float DestMin, float DestMax);
SYL_INLINE svec3 s_rgb_to_hsv(svec3 RGB);
SYL_INLINE svec2 SVEC2F(float a, float b);
SYL_INLINE svec2 SVEC2A(float* a);
SYL_INLINE void s_vec2_zero(svec2* Vector);
SYL_INLINE bool s_vec2_equal(svec2 vec1, svec2 Vec2);
SYL_INLINE bool s_vec2_equal_scalar(svec2 vec1, float Value);
SYL_INLINE bool s_vec2_not_equal(svec2 vec1, svec2 Vec2);
SYL_INLINE bool s_vec2_not_equal_scalar(svec2 vec1, float Value);
SYL_INLINE bool s_vec2_greater(svec2 vec1, svec2 Vec2);
SYL_INLINE bool s_vec2_greater_scalar(svec2 vec1, float Value);
SYL_INLINE bool s_vec2_greater_equal(svec2 vec1, svec2 Vec2);
SYL_INLINE bool s_vec2_greater_equal_scalar(svec2 vec1, float Value);
SYL_INLINE bool s_vec2_less(svec2 vec1, svec2 Vec2);
SYL_INLINE bool s_vec2_less_scalar(svec2 vec1, float Value);
SYL_INLINE bool s_vec2_less_equal(svec2 vec1, svec2 Vec2);
SYL_INLINE bool s_vec2_less_equal_scalar(svec2 vec1, float Value);
SYL_INLINE svec2 s_vec2_add(svec2 vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2p_add(svec2* vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2_add_scalar(svec2 vec1, float Value);
SYL_INLINE svec2 s_vec2p_add_scalar(svec2* vec1, float Value);
SYL_INLINE svec2 s_vec2_sub(svec2 vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2p_sub(svec2* vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2_sub_scalar(svec2 vec1, float Value);
SYL_INLINE svec2 s_scalar_sub_vec2(float Value, svec2 vec1);
SYL_INLINE svec2 s_vec2p_sub_scalar(svec2* vec1, float Value);
SYL_INLINE svec2 s_scalar_sub_vec2p(float Value, svec2* vec1);
SYL_INLINE svec2 s_vec2_mul(svec2 vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2p_mul(svec2* vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2_mul_scalar(svec2 vec1, float Value);
SYL_INLINE svec2 s_vec2p_mul_scalar(svec2* vec1, float Value);
SYL_INLINE svec2 s_vec2_div(svec2 vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2p_div(svec2* vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2_div_scalar(svec2 vec1, float Value);
SYL_INLINE svec2 s_scalar_div_vec2(float Value, svec2 vec1);
SYL_INLINE svec2 s_vec2p_div_scalar(svec2* vec1, float Value);
SYL_INLINE svec2 s_scalar_div_vec2p(float Value, svec2* vec1);
SYL_INLINE svec2 s_vec2_negate(svec2 a);
SYL_INLINE svec2 s_vec2_floor(svec2 A);
SYL_INLINE svec2 s_vec2_round(svec2 A);
SYL_INLINE float s_vec2_dot(svec2 vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2_hadamard(svec2 vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2_lerp(svec2 vec1, svec2 Vec2, float t);
SYL_INLINE svec2 s_vec2_clamp(svec2 Value, svec2 Min, svec2 Max);
SYL_INLINE float s_vec2_length(svec2 vec1);
SYL_INLINE float s_vec2_distance(svec2 vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2_normalize(svec2 a);
SYL_INLINE svec2 s_vec2_reflect(svec2 Pos, svec2 N);
SYL_INLINE svec2 s_vec2_project(svec2 VectorToProject, svec2 ProjectionVector);
SYL_INLINE svec2 s_vec2_max_vector(svec2 vec1, svec2 Vec2);
SYL_INLINE svec2 s_vec2_min_vector(svec2 vec1, svec2 Vec2);
SYL_INLINE float s_vec2_max(svec2 A);
SYL_INLINE float s_vec2_min(svec2 A);
SYL_INLINE float s_vec2_sum(svec2 vec1);
SYL_INLINE float s_triangle_area(svec2 vec1, svec2 Vec2, svec2 Vec3);
SYL_INLINE svec3 SVEC3(float a, float b, float c);
SYL_INLINE svec3 SVEC3A(float* a);
SYL_INLINE void s_vec3_zero(svec3* Vector);
SYL_INLINE bool s_vec3_equal(svec3 vec1, svec3 Vec2);
SYL_INLINE bool s_vec3_equal_scalar(svec3 vec1, float Value);
SYL_INLINE bool s_vec3_not_equal(svec3 vec1, svec3 Vec2);
SYL_INLINE bool s_vec3_not_equal_scalar(svec3 vec1, float Value);
SYL_INLINE bool s_vec3_greater(svec3 vec1, svec3 Vec2);
SYL_INLINE bool s_vec3_less(svec3 vec1, svec3 Vec2);
SYL_INLINE bool s_vec3_less_scalar(svec3 vec1, float Value);
SYL_INLINE bool s_vec3_greater_equal(svec3 vec1, svec3 Vec2);
SYL_INLINE bool s_vec3_greater_equal_scalar(svec3 vec1, float Value);
SYL_INLINE bool s_vec3_less_equal(svec3 vec1, svec3 Vec2);
SYL_INLINE bool s_vec3_less_equal_scalar(svec3 vec1, float Value);
SYL_INLINE svec3 s_vec3_add(svec3 vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3p_add(svec3* vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3_add_scalar(svec3 vec1, float Value);
SYL_INLINE svec3 s_vec3p_add_scalar(svec3* vec1, float Value);
SYL_INLINE svec3 s_vec3_sub(svec3 vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3p_sub(svec3* vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3_sub_scalar(svec3 vec1, float Value);
SYL_INLINE svec3 s_vec3p_sub_saclar(svec3* vec1, float Value);
SYL_INLINE svec3 s_scalar_sub_vec3(float value, svec3 vec1);
SYL_INLINE svec3 s_scalar_sub_vec3p(float value, svec3* vec1);
SYL_INLINE svec3 s_vec3_mul(svec3 vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3p_mul(svec3* vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3_mul_scalar(svec3 vec1, float Value);
SYL_INLINE svec3 s_vec3p_mul_scalar(svec3* vec1, float Value);
SYL_INLINE svec3 s_vec3_div(svec3 vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3p_div(svec3* vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3_div_scalar(svec3 vec1, float Value);
SYL_INLINE svec3 s_vec3p_div_scalar(svec3* vec1, float Value);
SYL_INLINE svec3 s_scalar_div_vec3(float Value, svec3 vec1);
SYL_INLINE svec3 s_scalar_div_vec3p(float Value, svec3* vec1);
SYL_INLINE svec3 s_vec3_floor(svec3 A);
SYL_INLINE svec3 s_vec3_round(svec3 A);
SYL_INLINE svec3 s_vec3_negate(svec3 a);
SYL_INLINE float s_vec3_dot(svec3 vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3_hadamard(svec3 vec1, svec3 Vec2);
SYL_INLINE float s_vec3_length(svec3 vec1);
SYL_INLINE float s_vec3_distance(svec3 vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3_normalize(svec3 a);
SYL_INLINE float s_vec3_max(svec3 A);
SYL_INLINE float s_vec3_min_value(svec3 A);
SYL_INLINE svec3 s_vec3_max_vector(svec3 vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3_min_vector(svec3 vec1, svec3 Vec2);
SYL_INLINE svec3 s_vec3_clamp(svec3 Value, svec3 Min, svec3 Max);
SYL_INLINE svec3 s_vec3_lerp(svec3 vec1, svec3 Vec2, float t);
SYL_INLINE svec3 s_vec3_project(svec3 VectorToProject, svec3 ProjectionVector);
SYL_INLINE svec3 s_vec3_cross(svec3 vec1, svec3 Vec2);
SYL_INLINE float Slope(svec3 PointA, svec3 PointB);
SYL_INLINE svec4 SVEC4(float a, float b, float c, float d);
SYL_INLINE svec4 SVEC4A(float* a);
SYL_INLINE svec4 SVEC4VF(svec3 Vector, float Value);
SYL_INLINE void s_vector4_zero(svec4* Vector);
SYL_INLINE bool s_vec4_equal(svec4 vec1, svec4 Vec2);
SYL_INLINE bool s_vec4_equal_scalar(svec4 vec1, float Value);
SYL_INLINE bool s_vec4_not_equal(svec4 vec1, svec4 Vec2);
SYL_INLINE bool s_vec4_not_equal_scalar(svec4 vec1, float Value);
SYL_INLINE bool s_vec4_greater(svec4 vec1, svec4 Vec2);
SYL_INLINE bool s_vec4_less(svec4 vec1, svec4 Vec2);
SYL_INLINE bool s_vec4_less_scalar(svec4 vec1, float Value);
SYL_INLINE bool s_vec4_greater_equal(svec4 vec1, svec4 Vec2);
SYL_INLINE bool s_vec4_greater_equal_scalar(svec4 vec1, float Value);
SYL_INLINE bool s_vec4_less_equal(svec4 vec1, svec4 Vec2);
SYL_INLINE bool s_vec4_less_equal_scalar(svec4 vec1, float Value);
SYL_INLINE svec4 s_vec4_add(svec4 vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4p_add(svec4* vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4_add_scalar(svec4 vec1, float Value);
SYL_INLINE svec4 s_vec4p_add_scalar(svec4* vec1, float Value);
SYL_INLINE svec4 s_vec4_sub(svec4 vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4p_sub(svec4* vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4_sub_scalar(svec4 vec1, float Value);
SYL_INLINE svec4 s_vec4p_sub_scalar(svec4* vec1, float Value);
SYL_INLINE svec4 s_scalar_vec4_sub(float value, svec4 vec1);
SYL_INLINE svec4 s_scalar_sub_vec4p(float value, svec4* vec1);
SYL_INLINE svec4 s_vec4_mul(svec4 vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4p_mul(svec4* vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4_mul_scalar(svec4 vec1, float Value);
SYL_INLINE svec4 s_vec4p_mul_scalar(svec4* vec1, float Value);
SYL_INLINE svec4 s_vec4_div(svec4 vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4p_div(svec4* vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4_div_scalar(svec4 vec1, float Value);
SYL_INLINE svec4 s_vec4p_div_scalar(svec4* vec1, float Value);
SYL_INLINE svec4 s_vec4_floor(svec4 A);
SYL_INLINE svec4 s_vec4_round(svec4 A);
SYL_INLINE svec4 s_vec4_negate(svec4 a);
SYL_INLINE float s_vec4_dot(svec4 vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4_hadamard(svec4 vec1, svec4 Vec2);
SYL_INLINE float s_vec4_length(svec4 vec1);
SYL_INLINE float s_vec4_distance(svec4 vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4_normalize(svec4 a);
SYL_INLINE svec4 s_vec4_lerp(svec4 vec1, svec4 Vec2, float t);
SYL_INLINE svec4 s_vec4_cross(svec4 vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4_project(svec4 VectorToProject, svec4 ProjectionVector);
SYL_INLINE svec4 s_vec4_max_vector(svec4 vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4_min_vector(svec4 vec1, svec4 Vec2);
SYL_INLINE svec4 s_vec4_clamp(svec4 Value, svec4 Min, svec4 Max);
SYL_INLINE float s_vec4_max(svec4 A);
SYL_INLINE float s_vec4_min(svec4 A);
SYL_INLINE float s_vec4_sum(svec4 vec1);
SYL_INLINE smat4 SMAT4(float value);
SYL_INLINE smat4 SMAT4V(float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33);
SYL_INLINE smat4 SMAT4A(float* a);
SYL_INLINE void s_mat4_zero(smat4* Matrix);
SYL_INLINE smat4 s_mat4_identity();
SYL_INLINE void s_mat4_identityp(smat4 *ptr);
SYL_INLINE bool s_mat4_is_identity(smat4 Mat);
SYL_INLINE smat4 s_mat4_mul(smat4 Matrix1, smat4 Matrix2);
SYL_INLINE smat4 s_mat4_transpose(smat4 Mat);
SYL_INLINE smat4 s_mat4_inverse_noscale(smat4 Matrix);
SYL_INLINE svec4 s_mat4_transform(smat4 Matrix, svec4 Vector);
SYL_INLINE svec4 s_mat4_mul_vec4(smat4 Matrix1, svec4 Vector);
SYL_INLINE svec3 s_mat4_mul_vec3(smat4 Matrix1, svec3 Vector);
SYL_INLINE smat4 s_mat4_translate(smat4 matrix, svec3 vec);
SYL_INLINE smat4 s_mat4_scale(smat4 matrix, svec3 vec);
smat4 s_mat4_rotate(smat4 *matrix, float angle, svec3 vec);
SYL_INLINE smat4 s_mat4_xrotation(float Angle);
SYL_INLINE smat4 s_mat4_yrotation(float Angle);
SYL_INLINE smat4 s_mat4_zrotation(float Angle);
SYL_INLINE smat4 s_mat4_translation(svec3 Vector);
SYL_INLINE smat4 s_mat4_perspective_projection_rh(float Fov, float AspectRatio, float NearClipPlane, float FarClipPlane);
SYL_INLINE smat4 s_mat4_ortho_rh(float left, float right, float bottom, float top, float znear, float zfar);
#endif // SYLVESTER_H
#ifdef SYL_IMPLEMENTATION
#define SYL_PI 3.14159265359f
#define _SYL_SHUFFLE(a,b,c,d) (((a) << 6) | ((b) << 4) | \
((c) << 2) | ((d)))
#define _SYL_PERMUTE_PS( v, c ) _mm_shuffle_ps((v), (v), c )
#define _SYL_ADD_PS( a, b, c ) _mm_sub_ps((c), _mm_mul_ps((a), (b)))
#define _SYL_LOAD(a) _mm_load_ps((a))
#define _SYL_LOADV2(a) _mm_loadl_pi(_mm_setzero_ps(), (__m64*) & (a))
#define _SYL_MAKE_SHUFFLE_MASK(x,y,z,w) (x | (y<<2) | (z<<4) | (w<<6))
#define _SYL_VEC_SWIZZLE_MASK(vec, mask) _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(vec), mask))
#define _SYL_VEC_SWIZZLE(vec, x, y, z, w) _SYL_VEC_SWIZZLE_MASK(vec, _SYL_MAKE_SHUFFLE_MASK(x,y,z,w))
#define _SYL_VEC_SWIZZLE1(vec, x) _SYL_VEC_SWIZZLE_MASK(vec, _SYL_MAKE_SHUFFLE_MASK(x,x,x,x))
#define _SYL_VEC_SWIZZLE_0022(vec) _mm_moveldup_ps(vec)
#define _SYL_VEC_SWIZZLE_1133(vec) _mm_movehdup_ps(vec)
// return (vec1[x], vec1[y], vec2[z], vec2[w])
#define _SYL_VEC_SHUFFLE(vec1, vec2, x,y,z,w) _mm_shuffle_ps(vec1, vec2, _SYL_MAKE_SHUFFLE_MASK(x,y,z,w))
// special shuffle
#define _SYL_VEC_SHUFFLE_0101(vec1, vec2) _mm_movelh_ps(vec1, vec2)
#define _SYL_VEC_SHUFFLE_2323(vec1, vec2) _mm_movehl_ps(vec2, vec1)
#define _SYL_SMALL_NUMBER (1.e-8f)
#ifdef SYL_GENERIC_FUNCTIONS
#define syl_add(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_add, \
float: s_vec2_add_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_add, \
float: s_vec3_add_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_add, \
float: s_vec4_add_scalar \
) \
)(v1, v2)
#define syl_sub(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_sub, \
float: s_vec2_sub_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_sub, \
float: s_vec3_sub_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_sub, \
float: s_vec4_sub_scalar \
) \
)(v1, v2)
#define syl_mul(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_mul, \
float: s_vec2_mul_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_mul, \
float: s_vec3_mul_scalar, \
), \
svec4: _Generic((v2), \
default: s_vec4_mul, \
float: s_vec4_mul_scalar \
), \
smat4: _Generic((v2), \
default: s_mat4_mul, \
svec4: s_mat4_mul_vec4, \
svec3: s_mat4_mul_vec3 \
) \
)(v1, v2)
#define syl_div(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_div, \
float: s_vec2_div_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_div, \
float: s_vec3_div_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_div, \
float: s_vec4_div_scalar \
) \
)(v1, v2)
#define syl_equal(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_equal, \
float: s_vec2_equal_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_equal, \
float: s_vec3_equal_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_equal, \
float: s_vec4_equal_scalar \
) \
)(v1, v2)
#define syl_not_equal(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_not_equal, \
float: s_vec2_not_equal_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_not_equal, \
float: s_vec3_not_equal_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_not_equal, \
float: s_vec4_not_equal_scalar \
) \
)(v1, v2)
#define syl_greater(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_greater, \
float: s_vec2_greater_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_greater, \
float: s_vec3_greater_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_greater, \
float: s_vec4_greater_scalar \
) \
)(v1, v2)
#define syl_greater_scalar(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_greater_scalar, \
float: s_vec2_greater_scalar_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_greater_scalar, \
float: s_vec3_greater_scalar_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_greater_scalar, \
float: s_vec4_greater_scalar_scalar \
) \
)(v1, v2)
#define syl_greater_equal(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_greater_equal, \
float: s_vec2_greater_equal_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_greater_equal, \
float: s_vec3_greater_equal_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_greater_equal, \
float: s_vec4_greater_equal_scalar \
) \
)(v1, v2)
#define syl_greater_equal_scalar(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_greater_equal_scalar, \
float: s_vec2_greater_equal_scalar_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_greater_equal_scalar, \
float: s_vec3_greater_equal_scalar_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_greater_equal_scalar, \
float: s_vec4_greater_equal_scalar_scalar \
) \
)(v1, v2)
#define syl_less(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_less, \
float: s_vec2_less_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_less, \
float: s_vec3_less_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_less, \
float: s_vec4_less_scalar \
) \
)(v1, v2)
#define syl_less_scalar(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_less_scalar, \
float: s_vec2_less_scalar_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_less_scalar, \
float: s_vec3_less_scalar_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_less_scalar, \
float: s_vec4_less_scalar_scalar \
) \
)(v1, v2)
#define syl_less_equal(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_less_equal, \
float: s_vec2_less_equal_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_less_equal, \
float: s_vec3_less_equal_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_less_equal, \
float: s_vec4_less_equal_scalar \
) \
)(v1, v2)
#define syl_less_equal_scalar(v1, v2) _Generic((v1), \
svec2: _Generic((v2), \
default: s_vec2_less_equal_scalar, \
float: s_vec2_less_equal_scalar_scalar \
), \
svec3: _Generic((v2), \
default: s_vec3_less_equal_scalar, \
float: s_vec3_less_equal_scalar_scalar \
), \
svec4: _Generic((v2), \
default: s_vec4_less_equal_scalar, \
float: s_vec4_less_equal_scalar_scalar \
) \
)(v1, v2)
#define syl_length(vec) _Generic((vec), svec2: s_vec2_length, svec3: s_vec3_length)(vec)
#define syl_lerp(v1, v2, v3) _Generic((v1), float: s_lerp, svec2: s_vec2_lerp, svec3: s_vec3_length, svec4: s_vec4_lerp)(v1, v2, v3)
#define syl_clamp(v1, v2, v3) _Generic((v1), float: s_clampf, double: s_clampd, int: s_clampi, svec2: s_vec2_clamp, svec3: s_vec3_clamp, svec4: s_vec4_clamp)(v1, v2, v3)
#define syl_max2(v1, v2) _Generic((v1), float: s_maxf, int: s_maxi, svec2: s_vec2_max_vector, svec3: s_vec3_max_vector, svec4: s_vec4_max_vector)(v1, v2)
#define syl_max_value(v1) _Generic((v1), svec2: s_vec2_max, svec3: s_vec3_max, svec4: s_vec4_max)(v1)
#define syl_min2(v1, v2) _Generic((v1), float: s_minf, int: s_mini, svec2: s_vec2_min_vector, svec3: s_vec3_min_vector, svec4: s_vec4_min_vector)(v1, v2)
#define syl_min_value(v1) _Generic((v1), svec2: s_vec2_min, svec3: s_vec3_min, svec4: s_vec4_min)(v1)
#define syl_normalize(v1) _Generic((v1), float: s_normalize, svec2: s_vec2_normalize, svec3: s_vec3_normalize, svec4: s_vec4_normalize)(v1)
#define syl_hadamard(v1, v2) _Generic((v1), svec2: s_vec2_hadamard, svec3: s_vec3_hadamard, svec4: s_vec4_hadamard)(v1, v2)
#define syl_dot(v1, v2) _Generic((v1), svec2: s_vec2_dot, svec3: s_vec3_dot, svec4: s_vec4_dot)(v1, v2)
// floor is already reserved for c function
#define syl_floor(v1) _Generic((v1), float: s_floorf, double: s_floord, svec2: s_vec2_floor, svec3: s_vec3_floor, svec4: s_vec4_floor)(v1)
#define syl_round(v1) _Generic((v1), float: s_roundf, double: s_roundd, svec2: s_vec2_round, svec3: s_vec3_round, svec4: s_vec4_round)(v1)
// ceil is already reserved for c function
#define syl_ceil(v1) _Generic((v1), float: s_ceilf, double: s_ceild)(v1)
#define syl_project(v1, v2, v3) _Generic((v1), svec2: s_vec2_project, svec3: s_vec3_project, svec4: s_vec4_project)(v1, v2, v3)
#define syl_sum(v1) _Generic((v1), svec2: s_vec2_sum, svec3: s_vec3_sum, svec4: s_vec4_sum)(v1)
#define syl_negate(v1) _Generic((v1), svec2: s_vec2_negate, svec3: s_vec3_negate, svec4: s_vec4_negate)(v1)
#ifdef SYL_GENERIC_FUNCTIONS_NO_PREFIX_ALIASES
#define add syl_add
#define sub syl_sub
#define mul syl_mul
#define div syl_div
#define equal syl_equal
#define not_equal syl_not_equal
#define greater syl_greater
#define greater_scalar syl_greater_scalar
#define greater_equal syl_greater_equal
#define greater_equal_scalar syl_greater_equal_scalar
#define less syl_less
#define less_scalar syl_less_scalar
#define less_equal syl_less_equal
#define less_equal_scalar syl_less_equal_scalar
#define length syl_length
#define lerp syl_lerp
#define clamp syl_clamp
#define max2 syl_max2
#define max_value syl_max_value
#define min2 syl_min2
#define min_value syl_min_value
#define normalize syl_normalize
#define hadamard syl_hadamard
#define dot syl_dot
#define round syl_round
#define project syl_project
#define sum syl_sum
#define negate syl_negate
#endif
#endif
const smat4 _S_IDENT4X4 = { {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
} };
svec2 _SVEC2_ZERO = { { 0.0f, 0.0f } };
#if defined(SYL_ENABLE_SSE4)
const __m128 _S_XMM_ZERO = { 0.0f, 0.0f, 0.0f, 0.0f };
const __m128 _S_IDENT4x4R0 = { 1.0f, 0.0f, 0.0f, 0.0f };
const __m128 _S_IDENT4x4R1 = { 0.0f, 1.0f, 0.0f, 0.0f };
const __m128 _S_IDENT4x4R2 = { 0.0f, 0.0f, 1.0f, 0.0f };
const __m128 _S_IDENT4x4R3 = { 0.0f, 0.0f, 0.0f, 1.0f };
const __m128 _S_XMM_MASK_3 = { (float)0xFFFFFFFF, (float)0xFFFFFFFF, (float)0xFFFFFFFF, (float)0x00000000 };
const __m128 _S_XMM_MASK_Y = { 0x00000000, (float)0xFFFFFFFF, 0x00000000, 0x00000000 };
#endif
#if defined(SYL_ENABLE_AVX)
const __m256 _S_YMM_ZERO = { { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f } };
#endif
/*********************************************
* Utility *
*********************************************/
SYL_INLINE float s_radian_to_degree(float Radian)
{
return(Radian * (180 / SYL_PI));
}
SYL_INLINE float s_degree_to_radian(float Degree)
{
return (Degree * (SYL_PI / 180));
}
SYL_INLINE float s_roundf(float A)
{
return((int)(A + 0.5f));
}
SYL_INLINE double s_roundd(double A)
{
return((int)(A + 0.5f));
}
SYL_INLINE float s_ceilf(float A)
{
return((int)(A + 1.0f));
}
SYL_INLINE double s_ceild(double A)
{
return((int)(A + 1.0f));
}
SYL_INLINE float s_floorf(float A)
{
return((int)A);
}
SYL_INLINE double s_floord(double A)
{
return((int)A);
}
/* Unpack four 8-bit BGRA values into vec4 */
SYL_INLINE svec4 s_bgra_unpack(int Color)
{
svec4 Result = { {
(float)((Color >> 16) & 0xFF),
(float)((Color >> 8) & 0xFF),
(float)((Color >> 0) & 0xFF),
(float)((Color >> 24) & 0xFF)
} };
return(Result);
}
/* Pack four 8-bit RGB values */
SYL_INLINE unsigned int s_bgra_pack(svec4 Color)
{
unsigned int Result =
((unsigned int)(Color.a) << 24) |
((unsigned int)(Color.r) << 16) |
((unsigned int)(Color.g) << 8) |
((unsigned int)(Color.b) << 0);
return(Result);
}
/* Unpack four 8-bit RGBA values into vec4 */
SYL_INLINE svec4 s_rgba_unpack(unsigned int Color)
{
svec4 Result = { {
(float)((Color >> 24) & 0xFF),
(float)((Color >> 16) & 0xFF),
(float)((Color >> 8) & 0xFF),
(float)((Color >> 0) & 0xFF)
} };
return(Result);
}
/* Pack four 8-bit RGBA values */
SYL_INLINE unsigned int s_rgba_pack(svec4 Color)
{
unsigned int Result =
((unsigned int)(Color.a) << 24) |
((unsigned int)(Color.b) << 16) |
((unsigned int)(Color.g) << 8) |
((unsigned int)(Color.r) << 0);
return(Result);
}
SYL_INLINE float s_clampf(float Value, float Min, float Max)
{
float Result = Value;
if (Result < Min)
Result = Min;
else if (Result > Max)
Result = Max;
return(Result);
}
SYL_INLINE float s_clampd(float Value, float Min, float Max)
{
float Result = Value;
if (Result < Min)
Result = Min;
else if (Result > Max)
Result = Max;
return(Result);
}
SYL_INLINE int s_clampi(int Value, int Min, int Max)
{
int Result = Value;
if (Result < Min)
Result = Min;
else if (Result > Max)
Result = Max;
return(Result);
}
SYL_INLINE float s_clamp01f(float Value)
{
return(s_clampf(0.0f, Value, 1.0f));
}
SYL_INLINE double s_clamp01d(double Value)
{
return(s_clampf(0.0f, Value, 1.0f));
}
SYL_INLINE float s_clamp_above_zero(float Value)
{
return (Value < 0) ? 0.0f : Value;
}
SYL_INLINE float s_clamp_below_zero(float Value)
{
return (Value > 0) ? 0.0f : Value;
}
SYL_INLINE bool s_is_in_range(float Value, float Min, float Max)
{
return(((Min <= Value) && (Value <= Max)));
}
SYL_INLINE float s_lerp(float A, float t, float B)
{
return (1.0f - t) * A + t * B;
}
/* It is handy to type square if the expression happens to be very long */
SYL_INLINE float s_square(float x)
{
return(x * x);
}
/* Absoule value */
SYL_INLINE float s_abs(float x)
{
return *((unsigned int*)(&x)) &= 0xffffffff >> 1;
}
/* Find the hypotenuse of a triangle given two other sides */
SYL_INLINE float s_pythagorean(float x, float y)
{
return sqrt(x * x + y * y);
}
/* Maximum of two values */
SYL_INLINE float s_maxf(float x, float y)
{
if (x > y)
return(x);
return(y);
}
SYL_INLINE int s_maxi(int x, int y)
{
if (x > y)
return(x);
return(y);
}
SYL_INLINE int s_mini(int x, int y)
{
if (x < y)
return(x);
return(y);
}
SYL_INLINE float s_minf(float x, float y)
{
if (x < y)
return(x);
return(y);
}
SYL_INLINE float s_mod(float x, float y)
{
return(x - (s_roundf(x / y) * y));
}
SYL_INLINE float s_pow(float Value, float Times)
{
float pow = 1;
for (int i = 0; i < Times; i++) {
pow = pow * Value;
}
return(pow);
}
/* Places -> How many digits you want to keep remained */
SYL_INLINE float s_truncatef(float Value, float Remain)
{
int Remove = s_pow(10, Remain);
return(s_roundf(Value * Remove) / Remove);
}
SYL_INLINE double s_truncated(double Value, double Places)
{
int Remove = pow(10, Places);
return(s_roundf(Value * Remove) / Remove);
}
SYL_INLINE float s_normalize(float Value, float Min, float Max)
{
return (Value - Min) / (Max - Min);
}
SYL_INLINE float s_map(float Value, float SourceMin, float SourceMax, float DestMin, float DestMax)
{
return s_lerp(s_normalize(Value, SourceMin, SourceMax), DestMin, DestMax);
}
SYL_INLINE svec3 s_rgb_to_hsv(svec3 RGB)
{
/* Range the values between 1 and 0*/
RGB.r = RGB.r / 255.0;
RGB.g = RGB.g / 255.0;
RGB.b = RGB.b / 255.0;
float MaxValue = s_maxf(RGB.r, s_maxf(RGB.g, RGB.b));
float MinValue = s_maxf(RGB.r, s_minf(RGB.g, RGB.b));
float Dif = MaxValue - MinValue;
svec3 Result;
Result.x = -1, Result.y = -1;
if (MaxValue == MinValue)
Result.x = 0;
else if (MaxValue == RGB.r)
Result.x = s_mod(60 * ((RGB.g - RGB.b) / Dif) + 360, 360);
else if (MaxValue == RGB.g)
Result.x = s_mod(60 * ((RGB.b - RGB.r) / Dif) + 120, 360);
else if (MaxValue == RGB.b)
Result.x = s_mod(60 * ((RGB.r - RGB.g) / Dif) + 240, 360);
if (MaxValue == 0)
Result.y = 0;
else
Result.y = (Dif / MaxValue) * 100;
Result.z = MaxValue * 100;
return(Result);
}
/*********************************************
* VECTOR 2D *
*********************************************/
SYL_INLINE svec2 SVEC2(float a, float b) // From individiaul valeus
{
svec2 r = { { a, b } };
return(r);
}
SYL_INLINE svec2 SVEC2A(float* a) // From array
{
svec2 r = { { a[0], a[1] } };
return(r);
}
SYL_INLINE void s_vec2_zero(svec2* vector)
{
vector->x = 0;
vector->y = 0;
}
SYL_INLINE bool s_vec2_equal(svec2 vec1, svec2 vec2)
{
bool result = false;
if (vec1.x == vec2.x && vec1.y == vec2.y)
result = true;
return(result);
}
SYL_INLINE bool s_vec2_equal_scalar(svec2 vec1, float Value)
{
bool result = false;