-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickmath.h
More file actions
2100 lines (1586 loc) · 50 KB
/
quickmath.h
File metadata and controls
2100 lines (1586 loc) · 50 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
/* ------------------------------------------------------------------------
*
* quickmath.h
* author: Daniel Elwell (2022)
* license: MIT
* description: a single-header library for common vector, matrix, and quaternion math
* functions designed for games and graphics programming.
*
* ------------------------------------------------------------------------
*
* to change or disable the function prefix (the default is "qm_"), you must
* "#define QM_FUNC_PREFIX(name) myprefix_##name" before including the library
*
* to define your own function attributes (like a __device__ decorator for CUDA), you
* must "#define QM_FUNC_ATTRIBS my_attribs" before including the library. Note that you
* almost always want to include "static" as an attrib
*
* to disable the need to link with the C runtime library, you must
* "#define QM_SQRTF(x) my_sqrtf(x)", "#define QM_SINF(x) my_sinf(x)", "#define QM_COSF(x) my_cosf(x)",
* "#define QM_TANF(x) my_tanf(x)", and "#define QM_ACOSF(x) my_acosf(x)" before
* including the library
*
* ------------------------------------------------------------------------
*
* the following functions are defined:
* (QMvecn means a vector of dimension, 2, 3, or 4, named QMvec2, QMvec3, and QMvec4)
* (QMmatn means a matrix of dimensions 3x3 or 4x4, named QMmat3 and QMmat4)
* (QMbboxn means a bounding box of dimensions 2 or 3)
*
* QMvecn qm_vecn_load (const float* in);
* void qm_vecn_store (QMvecn v, float* out);
* QMvecn qm_vecn_full (float val);
* QMvecn qm_vecn_add (QMvecn v1, QMvecn v2);
* QMvecn qm_vecn_sub (QMvecn v1, QMvecn v2);
* QMvecn qm_vecn_mult (QMvecn v1, QMvecn v2);
* QMvecn qm_vecn_div (QMvecn v1, QMvecn v2);
* QMvecn qm_vecn_scale (QMvecn v , float s );
* QMvecn qm_vecn_dot (QMvecn v1, QMvecn v2);
* QMvec3 qm_vec3_cross (QMvec3 v1, QMvec3 v2);
* float qm_vecn_length (QMvecn v);
* QMvecn qm_vecn_normalize (QMvecn v);
* float qm_vecn_distance (QMvecn v1, QMvecn v2);
* int qm_vecn_equals (QMvecn v1, QMvecn v2);
* QMvecn qm_vecn_min (QMvecn v1, QMvecn v2);
* QMvecn qm_vecn_max (QMvecn v1, QMvecn v2);
*
* QMmatn qm_matn_load (const float* in);
* QMmatn qm_matn_load_row_major (const float* in);
* void qm_matn_store (QMmatn m, float* out);
* void qm_matn_store_row_major (QMmatn m, float* out);
* QMmatn qm_matn_identity ();
* QMmatn qm_matn_add (QMmatn m1, QMmatn m2);
* QMmatn qm_matn_sub (QMmatn m1, QMmatn m2);
* QMmatn qm_matn_mult (QMmatn m1, QMmatn m2);
* QMvecn qm_matn_mult_vecn (QMmatn m , QMvecn v );
* QMvec3 qm_mat4_transform_vec3 (QMmat4 m , QMvec3 v );
* QMmatn qm_matn_transpose (QMmatn m);
* QMmatn qm_matn_inv (QMmatn m);
*
* QMmat3 qm_mat3_translate (QMvec2 t);
* QMmat4 qm_mat4_translate (QMvec3 t);
* QMmat3 qm_mat3_scale (QMvec2 s);
* QMmat4 qm_mat4_scale (QMvec3 s);
* QMmat3 qm_mat3_rotate (float angle);
* QMmat4 qm_mat4_rotate (QMvec3 axis, float angle);
* QMmat4 qm_mat4_rotate_euler (QMvec3 angles);
* QMmat3 qm_mat4_top_left (QMmat4 m);
*
* QMmat4 qm_mat4_prespective (float fov, float aspect, float near, float far);
* QMmat4 qm_mat4_orthographic (float left, float right, float bot, float top, float near, float far);
* QMmat4 qm_mat4_look (QMvec3 pos, QMvec3 dir , QMvec3 up);
* QMmat4 qm_mat4_lookat (QMvec3 pos, QMvec3 target, QMvec3 up);
*
* QMquaternion qm_quaternion_load (const float* in);
* void qm_quaternion_store (QMquaternion q, float* out);
* QMquaternion qm_quaternion_identity ();
* QMquaternion qm_quaternion_add (QMquaternion q1, QMquaternion q2);
* QMquaternion qm_quaternion_sub (QMquaternion q1, QMquaternion q2);
* QMquaternion qm_quaternion_mult (QMquaternion q1, QMquaternion q2);
* QMquaternion qm_quaternion_scale (QMquaternion q, float s);
* QMquaternion qm_quaternion_dot (QMquaternion q1, QMquaternion q2);
* float qm_quaternion_length (QMquaternion q);
* QMquaternion qm_quaternion_normalize (QMquaternion q);
* QMquaternion qm_quaternion_conjugate (QMquaternion q);
* QMquaternion qm_quaternion_inv (QMquaternion q);
* QMquaternion qm_quaternion_slerp (QMquaternion q1, QMquaternion q2, float a);
* QMquaternion qm_quaternion_from_axis_angle (QMvec3 axis, float angle);
* QMquaternion qm_quaternion_from_euler (QMvec3 angles);
* QMmat4 qm_quaternion_to_mat4 (QMquaternion q);
*
* QMbboxn qm_bboxn_load (const float* b);
* void qm_bboxn_store (QMbboxn b, float* out);
* QMbboxn qm_bboxn_initialized ();
* QMbboxn qm_bboxn_union (QMbboxn b1, QMbboxn b2);
* void qm_bboxn_union_inplace (QMbboxn* b1, QMbboxn b2);
* QMbboxn qm_bboxn_union_vecn (QMbboxn b, QMvecn v);
* void qm_bboxn_union_vecn_inplace (QMbboxn* b, QMvecn v);
* QMvecn qm_bboxn_extent (QMbboxn b);
* QMvecn qm_bboxn_centroid (QMbboxn b);
* QMvecn qm_bboxn_offset (QMbboxn b, QMvecn v);
*
* float qm_bbox2_perimeter (QMbbox2 b);
* float qm_bbox3_surface_area (QMbbox3 b);
*/
#ifndef QM_MATH_H
#define QM_MATH_H
#ifdef __cplusplus
extern "C"
{
#endif
//check for SSE support
#if defined(__SSE3__)
#include <xmmintrin.h>
#include <pmmintrin.h>
#define QM_USE_SSE 1
#else
#define QM_USE_SSE 0
#endif
//define customizeable function prefix
#ifndef QM_FUNC_PREFIX
#define QM_FUNC_PREFIX(name) qm_##name
#endif
#ifndef QM_FUNC_ATTRIBS
#define QM_FUNC_ATTRIBS static inline
#endif
//include crt math if needed
#if !defined(QM_SQRTF) || !defined(QM_SINF) || !defined(QM_COSF) || !defined(QM_TANF) || !defined(QM_ACOSF)
#include <math.h>
#define QM_SQRTF(x) sqrtf(x)
#define QM_SINF(x) sinf(x)
#define QM_COSF(x) cosf(x)
#define QM_TANF(x) tanf(x)
#define QM_ACOSF(x) acosf(x)
#endif
//remove troublesome win32 #defines
#ifdef _WIN32
#undef near
#undef far
#endif
//----------------------------------------------------------------------//
//STRUCT DEFINITIONS:
typedef int QMbool;
//a 2-dimensional vector of floats
typedef union
{
float v[2];
struct{ float x, y; };
struct{ float w, h; };
} QMvec2;
//a 3-dimensional vector of floats
typedef union
{
float v[3];
struct{ float x, y, z; };
struct{ float w, h, d; };
struct{ float r, g, b; };
} QMvec3;
//a 4-dimensional vector of floats
typedef union
{
float v[4];
struct{ float x, y, z, w; };
struct{ float r, g, b, a; };
#if QM_USE_SSE
__m128 packed;
#endif
} QMvec4;
//-----------------------------//
//matrices are column-major
//a 3x3 matrix of floats
typedef union
{
float m[3][3];
QMvec3 v[3];
} QMmat3;
//a 4x4 matrix of floats
typedef union
{
float m[4][4];
QMvec4 v[4];
#if QM_USE_SSE
__m128 packed[4]; //array of columns
#endif
} QMmat4;
//-----------------------------//
//a quaternion
typedef union
{
float q[4];
struct{ float x, y, z, w; };
#if QM_USE_SSE
__m128 packed;
#endif
} QMquaternion;
//-----------------------------//
//a 2-dimensional bounding box
typedef struct
{
QMvec2 min;
QMvec2 max;
} QMbbox2;
//a 3-dimensional bounding box
typedef struct
{
QMvec3 min;
QMvec3 max;
} QMbbox3;
//----------------------------------------------------------------------//
//HELPER FUNCS:
#define QM_MIN(x, y) ((x) < (y) ? (x) : (y))
#define QM_MAX(x, y) ((x) > (y) ? (x) : (y))
#define QM_ABS(x) ((x) > 0 ? (x) : -(x))
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(rad_to_deg)(float rad)
{
return rad * 57.2957795131f;
}
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(deg_to_rad)(float deg)
{
return deg * 0.01745329251f;
}
#if QM_USE_SSE
QM_FUNC_ATTRIBS __m128 QM_FUNC_PREFIX(mat4_mult_column_sse)(__m128 c1, QMmat4 m2)
{
__m128 result;
result = _mm_mul_ps(_mm_shuffle_ps(c1, c1, _MM_SHUFFLE(0, 0, 0, 0)), m2.packed[0]);
result = _mm_add_ps(result, _mm_mul_ps(_mm_shuffle_ps(c1, c1, _MM_SHUFFLE(1, 1, 1, 1)), m2.packed[1]));
result = _mm_add_ps(result, _mm_mul_ps(_mm_shuffle_ps(c1, c1, _MM_SHUFFLE(2, 2, 2, 2)), m2.packed[2]));
result = _mm_add_ps(result, _mm_mul_ps(_mm_shuffle_ps(c1, c1, _MM_SHUFFLE(3, 3, 3, 3)), m2.packed[3]));
return result;
}
#endif
//----------------------------------------------------------------------//
//VECTOR FUNCTIONS:
//loading:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_load)(const float* in)
{
QMvec2 result = { in[0], in[1] };
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_load)(const float* in)
{
QMvec3 result = { in[0], in[1], in[2] };
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_load)(const float* in)
{
QMvec4 result = { in[0], in[1], in[2], in[3] };
return result;
}
//storing:
QM_FUNC_ATTRIBS void QM_FUNC_PREFIX(vec2_store)(QMvec2 v, float* out)
{
out[0] = v.x;
out[1] = v.y;
}
QM_FUNC_ATTRIBS void QM_FUNC_PREFIX(vec3_store)(QMvec3 v, float* out)
{
out[0] = v.x;
out[1] = v.y;
out[2] = v.z;
}
QM_FUNC_ATTRIBS void QM_FUNC_PREFIX(vec4_store)(QMvec4 v, float* out)
{
out[0] = v.x;
out[1] = v.y;
out[2] = v.z;
out[3] = v.w;
}
//full:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_full)(float val)
{
QMvec2 result = { val, val };
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_full)(float val)
{
QMvec3 result = { val, val, val };
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_full)(float val)
{
QMvec4 result = { val, val, val, val };
return result;
}
//addition:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_add)(QMvec2 v1, QMvec2 v2)
{
QMvec2 result;
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_add)(QMvec3 v1, QMvec3 v2)
{
QMvec3 result;
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
result.z = v1.z + v2.z;
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_add)(QMvec4 v1, QMvec4 v2)
{
QMvec4 result;
#if QM_USE_SSE
result.packed = _mm_add_ps(v1.packed, v2.packed);
#else
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
result.z = v1.z + v2.z;
result.w = v1.w + v2.w;
#endif
return result;
}
//subtraction:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_sub)(QMvec2 v1, QMvec2 v2)
{
QMvec2 result;
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_sub)(QMvec3 v1, QMvec3 v2)
{
QMvec3 result;
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
result.z = v1.z - v2.z;
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_sub)(QMvec4 v1, QMvec4 v2)
{
QMvec4 result;
#if QM_USE_SSE
result.packed = _mm_sub_ps(v1.packed, v2.packed);
#else
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
result.z = v1.z - v2.z;
result.w = v1.w - v2.w;
#endif
return result;
}
//multiplication:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_mult)(QMvec2 v1, QMvec2 v2)
{
QMvec2 result;
result.x = v1.x * v2.x;
result.y = v1.y * v2.y;
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_mult)(QMvec3 v1, QMvec3 v2)
{
QMvec3 result;
result.x = v1.x * v2.x;
result.y = v1.y * v2.y;
result.z = v1.z * v2.z;
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_mult)(QMvec4 v1, QMvec4 v2)
{
QMvec4 result;
#if QM_USE_SSE
result.packed = _mm_mul_ps(v1.packed, v2.packed);
#else
result.x = v1.x * v2.x;
result.y = v1.y * v2.y;
result.z = v1.z * v2.z;
result.w = v1.w * v2.w;
#endif
return result;
}
//division:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_div)(QMvec2 v1, QMvec2 v2)
{
QMvec2 result;
result.x = v1.x / v2.x;
result.y = v1.y / v2.y;
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_div)(QMvec3 v1, QMvec3 v2)
{
QMvec3 result;
result.x = v1.x / v2.x;
result.y = v1.y / v2.y;
result.z = v1.z / v2.z;
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_div)(QMvec4 v1, QMvec4 v2)
{
QMvec4 result;
#if QM_USE_SSE
result.packed = _mm_div_ps(v1.packed, v2.packed);
#else
result.x = v1.x / v2.x;
result.y = v1.y / v2.y;
result.z = v1.z / v2.z;
result.w = v1.w / v2.w;
#endif
return result;
}
//scalar multiplication:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_scale)(QMvec2 v, float s)
{
QMvec2 result;
result.x = v.x * s;
result.y = v.y * s;
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_scale)(QMvec3 v, float s)
{
QMvec3 result;
result.x = v.x * s;
result.y = v.y * s;
result.z = v.z * s;
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_scale)(QMvec4 v, float s)
{
QMvec4 result;
#if QM_USE_SSE
__m128 scale = _mm_set1_ps(s);
result.packed = _mm_mul_ps(v.packed, scale);
#else
result.x = v.x * s;
result.y = v.y * s;
result.z = v.z * s;
result.w = v.w * s;
#endif
return result;
}
//dot product:
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(vec2_dot)(QMvec2 v1, QMvec2 v2)
{
float result;
result = v1.x * v2.x + v1.y * v2.y;
return result;
}
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(vec3_dot)(QMvec3 v1, QMvec3 v2)
{
float result;
result = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
return result;
}
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(vec4_dot)(QMvec4 v1, QMvec4 v2)
{
float result;
#if QM_USE_SSE
__m128 r = _mm_mul_ps(v1.packed, v2.packed);
r = _mm_hadd_ps(r, r);
r = _mm_hadd_ps(r, r);
_mm_store_ss(&result, r);
#else
result = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
#endif
return result;
}
//cross product
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_cross)(QMvec3 v1, QMvec3 v2)
{
QMvec3 result;
result.x = (v1.y * v2.z) - (v1.z * v2.y);
result.y = (v1.z * v2.x) - (v1.x * v2.z);
result.z = (v1.x * v2.y) - (v1.y * v2.x);
return result;
}
//length:
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(vec2_length)(QMvec2 v)
{
float result;
result = QM_SQRTF(QM_FUNC_PREFIX(vec2_dot)(v, v));
return result;
}
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(vec3_length)(QMvec3 v)
{
float result;
result = QM_SQRTF(QM_FUNC_PREFIX(vec3_dot)(v, v));
return result;
}
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(vec4_length)(QMvec4 v)
{
float result;
result = QM_SQRTF(QM_FUNC_PREFIX(vec4_dot)(v, v));
return result;
}
//normalize:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_normalize)(QMvec2 v)
{
QMvec2 result = {0};
float invLen = QM_FUNC_PREFIX(vec2_length)(v);
if(invLen != 0.0f)
{
invLen = 1.0f / invLen;
result.x = v.x * invLen;
result.y = v.y * invLen;
}
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_normalize)(QMvec3 v)
{
QMvec3 result = {0};
float invLen = QM_FUNC_PREFIX(vec3_length)(v);
if(invLen != 0.0f)
{
invLen = 1.0f / invLen;
result.x = v.x * invLen;
result.y = v.y * invLen;
result.z = v.z * invLen;
}
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_normalize)(QMvec4 v)
{
QMvec4 result = {0};
float len = QM_FUNC_PREFIX(vec4_length)(v);
if(len != 0.0f)
{
#if QM_USE_SSE
__m128 scale = _mm_set1_ps(len);
result.packed = _mm_div_ps(v.packed, scale);
#else
float invLen = 1.0f / len;
result.x = v.x * invLen;
result.y = v.y * invLen;
result.z = v.z * invLen;
result.w = v.w * invLen;
#endif
}
return result;
}
//distance:
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(vec2_distance)(QMvec2 v1, QMvec2 v2)
{
float result;
QMvec2 to = QM_FUNC_PREFIX(vec2_sub)(v1, v2);
result = QM_FUNC_PREFIX(vec2_length)(to);
return result;
}
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(vec3_distance)(QMvec3 v1, QMvec3 v2)
{
float result;
QMvec3 to = QM_FUNC_PREFIX(vec3_sub)(v1, v2);
result = QM_FUNC_PREFIX(vec3_length)(to);
return result;
}
QM_FUNC_ATTRIBS float QM_FUNC_PREFIX(vec4_distance)(QMvec4 v1, QMvec4 v2)
{
float result;
QMvec4 to = QM_FUNC_PREFIX(vec4_sub)(v1, v2);
result = QM_FUNC_PREFIX(vec4_length)(to);
return result;
}
//equality:
QM_FUNC_ATTRIBS QMbool QM_FUNC_PREFIX(vec2_equals)(QMvec2 v1, QMvec2 v2)
{
QMbool result;
result = (v1.x == v2.x) && (v1.y == v2.y);
return result;
}
QM_FUNC_ATTRIBS QMbool QM_FUNC_PREFIX(vec3_equals)(QMvec3 v1, QMvec3 v2)
{
QMbool result;
result = (v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z);
return result;
}
QM_FUNC_ATTRIBS QMbool QM_FUNC_PREFIX(vec4_equals)(QMvec4 v1, QMvec4 v2)
{
QMbool result;
//TODO: there are SIMD instructions for floating point equality, find a way to get a single bool from them
result = (v1.x == v2.x) && (v1.y == v2.y) && (v1.z == v2.z) && (v1.w == v2.w);
return result;
}
//min:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_min)(QMvec2 v1, QMvec2 v2)
{
QMvec2 result;
result.x = QM_MIN(v1.x, v2.x);
result.y = QM_MIN(v1.y, v2.y);
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_min)(QMvec3 v1, QMvec3 v2)
{
QMvec3 result;
result.x = QM_MIN(v1.x, v2.x);
result.y = QM_MIN(v1.y, v2.y);
result.z = QM_MIN(v1.z, v2.z);
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_min)(QMvec4 v1, QMvec4 v2)
{
QMvec4 result;
#if QM_USE_SSE
result.packed = _mm_min_ps(v1.packed, v2.packed);
#else
result.x = QM_MIN(v1.x, v2.x);
result.y = QM_MIN(v1.y, v2.y);
result.z = QM_MIN(v1.z, v2.z);
result.w = QM_MIN(v1.w, v2.w);
#endif
return result;
}
//max:
QM_FUNC_ATTRIBS QMvec2 QM_FUNC_PREFIX(vec2_max)(QMvec2 v1, QMvec2 v2)
{
QMvec2 result;
result.x = QM_MAX(v1.x, v2.x);
result.y = QM_MAX(v1.y, v2.y);
return result;
}
QM_FUNC_ATTRIBS QMvec3 QM_FUNC_PREFIX(vec3_max)(QMvec3 v1, QMvec3 v2)
{
QMvec3 result;
result.x = QM_MAX(v1.x, v2.x);
result.y = QM_MAX(v1.y, v2.y);
result.z = QM_MAX(v1.z, v2.z);
return result;
}
QM_FUNC_ATTRIBS QMvec4 QM_FUNC_PREFIX(vec4_max)(QMvec4 v1, QMvec4 v2)
{
QMvec4 result;
#if QM_USE_SSE
result.packed = _mm_max_ps(v1.packed, v2.packed);
#else
result.x = QM_MAX(v1.x, v2.x);
result.y = QM_MAX(v1.y, v2.y);
result.z = QM_MAX(v1.z, v2.z);
result.w = QM_MAX(v1.w, v2.w);
#endif
return result;
}
//----------------------------------------------------------------------//
//MATRIX FUNCTIONS:
//loading:
QM_FUNC_ATTRIBS QMmat3 QM_FUNC_PREFIX(mat3_load)(const float* in)
{
QMmat3 result = {
in[0], in[1], in[2],
in[3], in[4], in[5],
in[6], in[7], in[8]
};
return result;
}
QM_FUNC_ATTRIBS QMmat3 QM_FUNC_PREFIX(mat3_load_row_major)(const float* in)
{
QMmat3 result = {
in[0], in[3], in[6],
in[1], in[4], in[7],
in[2], in[5], in[8]
};
return result;
}
QM_FUNC_ATTRIBS QMmat4 QM_FUNC_PREFIX(mat4_load)(const float* in)
{
QMmat4 result = {
in[0 ], in[1 ], in[2 ], in[3 ],
in[4 ], in[5 ], in[6 ], in[7 ],
in[8 ], in[9 ], in[10], in[11],
in[12], in[13], in[14], in[15]
};
return result;
}
QM_FUNC_ATTRIBS QMmat4 QM_FUNC_PREFIX(mat4_load_row_major)(const float* in)
{
QMmat4 result = {
in[0], in[4], in[8 ], in[12],
in[1], in[5], in[9 ], in[13],
in[2], in[6], in[10], in[14],
in[3], in[7], in[11], in[15]
};
return result;
}
//storing:
QM_FUNC_ATTRIBS void QM_FUNC_PREFIX(mat3_store)(QMmat3 m, float* out)
{
out[0] = m.m[0][0];
out[1] = m.m[0][1];
out[2] = m.m[0][2];
out[3] = m.m[1][0];
out[4] = m.m[1][1];
out[5] = m.m[1][2];
out[6] = m.m[2][0];
out[7] = m.m[2][1];
out[8] = m.m[2][2];
}
QM_FUNC_ATTRIBS void QM_FUNC_PREFIX(mat3_store_row_major)(QMmat3 m, float* out)
{
out[0] = m.m[0][0];
out[1] = m.m[1][0];
out[2] = m.m[2][0];
out[3] = m.m[0][1];
out[4] = m.m[1][1];
out[5] = m.m[2][1];
out[6] = m.m[0][2];
out[7] = m.m[1][2];
out[8] = m.m[2][2];
}
QM_FUNC_ATTRIBS void QM_FUNC_PREFIX(mat4_store)(QMmat4 m, float* out)
{
out[0] = m.m[0][0];
out[1] = m.m[0][1];
out[2] = m.m[0][2];
out[3] = m.m[0][3];
out[4] = m.m[1][0];
out[5] = m.m[1][1];
out[6] = m.m[1][2];
out[7] = m.m[1][3];
out[ 8] = m.m[2][0];
out[ 9] = m.m[2][1];
out[10] = m.m[2][2];
out[11] = m.m[2][3];
out[12] = m.m[3][0];
out[13] = m.m[3][1];
out[14] = m.m[3][2];
out[15] = m.m[3][3];
}
QM_FUNC_ATTRIBS void QM_FUNC_PREFIX(mat4_store_row_major)(QMmat4 m, float* out)
{
out[0] = m.m[0][0];
out[1] = m.m[1][0];
out[2] = m.m[2][0];
out[3] = m.m[3][0];
out[4] = m.m[0][1];
out[5] = m.m[1][1];
out[6] = m.m[2][1];
out[7] = m.m[3][1];
out[ 8] = m.m[0][2];
out[ 9] = m.m[1][2];
out[10] = m.m[2][2];
out[11] = m.m[3][2];
out[12] = m.m[0][3];
out[13] = m.m[1][3];
out[14] = m.m[2][3];
out[15] = m.m[3][3];
}
//initialization:
QM_FUNC_ATTRIBS QMmat3 QM_FUNC_PREFIX(mat3_identity)()
{
QMmat3 result = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
return result;
}
QM_FUNC_ATTRIBS QMmat4 QM_FUNC_PREFIX(mat4_identity)()
{
QMmat4 result = {
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
};
return result;
}
//addition:
QM_FUNC_ATTRIBS QMmat3 QM_FUNC_PREFIX(mat3_add)(QMmat3 m1, QMmat3 m2)
{
QMmat3 result;
result.m[0][0] = m1.m[0][0] + m2.m[0][0];