-
Notifications
You must be signed in to change notification settings - Fork 0
/
rjd_math.h
1114 lines (1016 loc) · 49.4 KB
/
rjd_math.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
#pragma once
// SSE3 math library with some functions for normal floats
#define RJD_MATH_H 1
#define RJD_MATH_PI (3.141592653589793238462643f)
#define RJD_MATH_EPSILON (0.000001)
////////////////////////////////////////////////////////////////////////////////
// utils
static inline uint32_t rjd_math_next_pow2_u32(uint32_t v);
static inline int32_t rjd_math_pow_u32(int32_t v, uint32_t power);
#define RJD_MATH_DECLARE_SIGN_FUNC(name, type) static inline type name(type v);
#define RJD_MATH_DEFINE_SIGN_FUNC(name, type) static inline type name(type v) { return (v < (type)0) ? (type)-1 : (type)1; }
#define RJD_MATH_SIGN_FUNCS(xmacro) \
xmacro(rjd_math_sign_i64, int64_t) \
xmacro(rjd_math_sign_i32, int32_t) \
xmacro(rjd_math_sign_i16, int16_t) \
xmacro(rjd_math_sign_i8, int8_t) \
xmacro(rjd_math_sign_f64, double) \
xmacro(rjd_math_sign_f32, float)
RJD_MATH_SIGN_FUNCS(RJD_MATH_DECLARE_SIGN_FUNC)
#define rjd_math_sign(v) _Generic((v), \
int64_t: rjd_math_sign_i64, \
int32_t: rjd_math_sign_i32, \
int16_t: rjd_math_sign_i16, \
int8_t: rjd_math_sign_i8, \
double: rjd_math_sign_f64, \
float: rjd_math_sign_f32)(v)
#define RJD_MATH_DECLARE_ISEQUAL_FUNC(name, type) static inline bool name(type a, type b);
#define RJD_MATH_DEFINE_ISEQUAL_FUNC(name, type) static inline bool name(type a, type b) { return (type)fabs(a - b) < RJD_MATH_EPSILON; }
#define RJD_MATH_ISEQUAL_FUNCS(xmacro) \
xmacro(rjd_math_isequal_f64, double) \
xmacro(rjd_math_isequal_f32, float)
RJD_MATH_ISEQUAL_FUNCS(RJD_MATH_DECLARE_ISEQUAL_FUNC)
#define rjd_math_isequal(a, b) _Generic((a), \
double: rjd_math_isequal_f64, \
float: rjd_math_isequal_f32)((a), (b))
// size_t versions are needed on OSX due to them being defined as unsigned long, which is different than
#define RJD_MATH_DECLARE_MIN_FUNC(name, type) static inline type name(type a, type b);
#define RJD_MATH_DEFINE_MIN_FUNC(name, type) static inline type name(type a, type b) { return (a < b) ? a : b; }
#if RJD_PLATFORM_OSX
#define RJD_MATH_MIN_FUNCS(xmacro) \
xmacro(rjd_math_min_i64, int64_t) \
xmacro(rjd_math_min_i32, int32_t) \
xmacro(rjd_math_min_i16, int16_t) \
xmacro(rjd_math_min_i8, int8_t) \
xmacro(rjd_math_min_sizet, size_t) \
xmacro(rjd_math_min_u64, uint64_t) \
xmacro(rjd_math_min_u32, uint32_t) \
xmacro(rjd_math_min_u16, uint16_t) \
xmacro(rjd_math_min_u8, uint8_t) \
xmacro(rjd_math_min_f32, float) \
xmacro(rjd_math_min_f64, double)
#define rjd_math_min(a, b) _Generic((a), \
int64_t: rjd_math_min_i64, \
int32_t: rjd_math_min_i32, \
int16_t: rjd_math_min_i16, \
int8_t: rjd_math_min_i8, \
size_t: rjd_math_min_sizet, \
uint64_t: rjd_math_min_u64, \
uint32_t: rjd_math_min_u32, \
uint16_t: rjd_math_min_u16, \
uint8_t: rjd_math_min_u8, \
double: rjd_math_min_f64, \
float: rjd_math_min_f32)((a), (b))
#else
#define RJD_MATH_MIN_FUNCS(xmacro) \
xmacro(rjd_math_min_i64, int64_t) \
xmacro(rjd_math_min_i32, int32_t) \
xmacro(rjd_math_min_i16, int16_t) \
xmacro(rjd_math_min_i8, int8_t) \
xmacro(rjd_math_min_u64, uint64_t) \
xmacro(rjd_math_min_u32, uint32_t) \
xmacro(rjd_math_min_u16, uint16_t) \
xmacro(rjd_math_min_u8, uint8_t) \
xmacro(rjd_math_min_f32, float) \
xmacro(rjd_math_min_f64, double)
#define rjd_math_min(a, b) _Generic((a), \
int64_t: rjd_math_min_i64, \
int32_t: rjd_math_min_i32, \
int16_t: rjd_math_min_i16, \
int8_t: rjd_math_min_i8, \
uint64_t: rjd_math_min_u64, \
uint32_t: rjd_math_min_u32, \
uint16_t: rjd_math_min_u16, \
uint8_t: rjd_math_min_u8, \
double: rjd_math_min_f64, \
float: rjd_math_min_f32)((a), (b))
#endif
RJD_MATH_MIN_FUNCS(RJD_MATH_DECLARE_MIN_FUNC)
#define RJD_MATH_DECLARE_MAX_FUNC(name, type) static inline type name(type a, type b);
#define RJD_MATH_DEFINE_MAX_FUNC(name, type) static inline type name(type a, type b) { return (a < b) ? b : a; }
#if RJD_PLATFORM_OSX
#define RJD_MATH_MAX_FUNCS(xmacro) \
xmacro(rjd_math_max_i64, int64_t) \
xmacro(rjd_math_max_i32, int32_t) \
xmacro(rjd_math_max_i16, int16_t) \
xmacro(rjd_math_max_i8, int8_t) \
xmacro(rjd_math_max_sizet, size_t) \
xmacro(rjd_math_max_u64, uint64_t) \
xmacro(rjd_math_max_u32, uint32_t) \
xmacro(rjd_math_max_u16, uint16_t) \
xmacro(rjd_math_max_u8, uint8_t) \
xmacro(rjd_math_max_f64, double) \
xmacro(rjd_math_max_f32, float)
#define rjd_math_max(a, b) _Generic((a), \
int64_t: rjd_math_max_i64, \
int32_t: rjd_math_max_i32, \
int16_t: rjd_math_max_i16, \
int8_t: rjd_math_max_i8, \
size_t: rjd_math_max_sizet, \
uint64_t: rjd_math_max_u64, \
uint32_t: rjd_math_max_u32, \
uint16_t: rjd_math_max_u16, \
uint8_t: rjd_math_max_u8, \
double: rjd_math_max_f64, \
float: rjd_math_max_f32)((a), (b))
#else
#define RJD_MATH_MAX_FUNCS(xmacro) \
xmacro(rjd_math_max_i64, int64_t) \
xmacro(rjd_math_max_i32, int32_t) \
xmacro(rjd_math_max_i16, int16_t) \
xmacro(rjd_math_max_i8, int8_t) \
xmacro(rjd_math_max_u64, uint64_t) \
xmacro(rjd_math_max_u32, uint32_t) \
xmacro(rjd_math_max_u16, uint16_t) \
xmacro(rjd_math_max_u8, uint8_t) \
xmacro(rjd_math_max_f64, double) \
xmacro(rjd_math_max_f32, float)
#define rjd_math_max(a, b) _Generic((a), \
int64_t: rjd_math_max_i64, \
int32_t: rjd_math_max_i32, \
int16_t: rjd_math_max_i16, \
int8_t: rjd_math_max_i8, \
uint64_t: rjd_math_max_u64, \
uint32_t: rjd_math_max_u32, \
uint16_t: rjd_math_max_u16, \
uint8_t: rjd_math_max_u8, \
double: rjd_math_max_f64, \
float: rjd_math_max_f32)((a), (b))
#endif
RJD_MATH_MAX_FUNCS(RJD_MATH_DECLARE_MAX_FUNC)
#define RJD_MATH_DECLARE_CLAMP_FUNC(name, type) static inline type name(type v, type minv, type maxv);
#define RJD_MATH_DEFINE_CLAMP_FUNC(name, type) static inline type name(type v, type minv, type maxv) { return (v < minv) ? (minv) : (v > maxv ? maxv : v); }
#if RJD_PLATFORM_OSX
#define RJD_MATH_CLAMP_FUNCS(xmacro) \
xmacro(rjd_math_clamp_i64, int64_t) \
xmacro(rjd_math_clamp_i32, int32_t) \
xmacro(rjd_math_clamp_i16, int16_t) \
xmacro(rjd_math_clamp_i8, int8_t) \
xmacro(rjd_math_clamp_sizet,size_t) \
xmacro(rjd_math_clamp_u64, uint64_t) \
xmacro(rjd_math_clamp_u32, uint32_t) \
xmacro(rjd_math_clamp_u16, uint16_t) \
xmacro(rjd_math_clamp_u8, uint8_t) \
xmacro(rjd_math_clamp_f64, double) \
xmacro(rjd_math_clamp_f32, float)
#define rjd_math_clamp(v, min, max) _Generic((v), \
int64_t: rjd_math_clamp_i64, \
int32_t: rjd_math_clamp_i32, \
int16_t: rjd_math_clamp_i16, \
int8_t: rjd_math_clamp_i8, \
size_t: rjd_math_clamp_sizet, \
uint64_t: rjd_math_clamp_u64, \
uint32_t: rjd_math_clamp_u32, \
uint16_t: rjd_math_clamp_u16, \
uint8_t: rjd_math_clamp_u8, \
double: rjd_math_clamp_f64, \
float: rjd_math_clamp_f32)((v), (min), (max))
#else
#define RJD_MATH_CLAMP_FUNCS(xmacro) \
xmacro(rjd_math_clamp_i64, int64_t) \
xmacro(rjd_math_clamp_i32, int32_t) \
xmacro(rjd_math_clamp_i16, int16_t) \
xmacro(rjd_math_clamp_i8, int8_t) \
xmacro(rjd_math_clamp_u64, uint64_t) \
xmacro(rjd_math_clamp_u32, uint32_t) \
xmacro(rjd_math_clamp_u16, uint16_t) \
xmacro(rjd_math_clamp_u8, uint8_t) \
xmacro(rjd_math_clamp_f64, double) \
xmacro(rjd_math_clamp_f32, float)
#define rjd_math_clamp(v, min, max) _Generic((v), \
int64_t: rjd_math_clamp_i64, \
int32_t: rjd_math_clamp_i32, \
int16_t: rjd_math_clamp_i16, \
int8_t: rjd_math_clamp_i8, \
uint64_t: rjd_math_clamp_u64, \
uint32_t: rjd_math_clamp_u32, \
uint16_t: rjd_math_clamp_u16, \
uint8_t: rjd_math_clamp_u8, \
double: rjd_math_clamp_f64, \
float: rjd_math_clamp_f32)((v), (min), (max))
#endif
RJD_MATH_CLAMP_FUNCS(RJD_MATH_DECLARE_CLAMP_FUNC)
#define RJD_MATH_DECLARE_TRUNCATE_FUNC(name, bigtype, smalltype) static inline smalltype name(bigtype v);
#define RJD_MATH_DEFINE_TRUNCATE_FUNC(name, bigtype, smalltype) static inline smalltype name(bigtype v) { RJD_ASSERT(v <= (smalltype)-1); return (smalltype)v; }
#define RJD_MATH_TRUNCATE_FUNCS(xmacro) \
xmacro(rjd_math_truncate_u64_to_u32, uint64_t, uint32_t) \
xmacro(rjd_math_truncate_u64_to_u16, uint64_t, uint16_t) \
xmacro(rjd_math_truncate_u64_to_u8, uint64_t, uint8_t) \
xmacro(rjd_math_truncate_u64_to_sizet, uint64_t, size_t) \
xmacro(rjd_math_truncate_u32_to_u16, uint32_t, uint16_t) \
xmacro(rjd_math_truncate_u32_to_u8, uint32_t, uint8_t) \
xmacro(rjd_math_truncate_u16_to_u8, uint16_t, uint8_t)
RJD_MATH_TRUNCATE_FUNCS(RJD_MATH_DECLARE_TRUNCATE_FUNC)
#define RJD_MATH_DECLARE_REMAP_FUNC(name, type) static inline type name(type v, type oldmin, type oldmax, type newmin, type newmax);
#define RJD_MATH_DEFINE_REMAP_FUNC(name, type) static inline type name(type v, type oldmin, type oldmax, type newmin, type newmax) { type oldrange = oldmax - oldmin; type newrange = newmax - newmin; return ((v - oldmin) * newrange) / oldrange + newmin; }
#define RJD_MATH_REMAP_FUNCS(xmacro) \
xmacro(rjd_math_remap_f64, double) \
xmacro(rjd_math_remap_f32, float)
RJD_MATH_REMAP_FUNCS(RJD_MATH_DECLARE_REMAP_FUNC)
#define rjd_math_remap(v, oldmin, oldmax, newmin, newmax) _Generic((v), \
double: rjd_math_remap_f64, \
float: rjd_math_remap_f32)(v, oldmin, oldmax, newmin, newmax)
// vector structs
// NOTE: this is the one place we break the "no typedef" rule for convenience
RJD_FORCE_ALIGN(16, typedef struct rjd_math_vec3 {
__m128 v;
} rjd_math_vec3);
RJD_FORCE_ALIGN(16, typedef struct rjd_math_vec4 {
__m128 v;
} rjd_math_vec4);
// column-major 4x4 matrix
RJD_FORCE_ALIGN(16, typedef struct rjd_math_mat4 {
rjd_math_vec4 m[4];
} rjd_math_mat4);
// vec4
#define rjd_math_vec4_shuffle(v4, x, y, z, w) ((rjd_math_vec4){_mm_shuffle_ps((v4).v, (v4).v, _MM_SHUFFLE(w, z, y, x))})
static inline rjd_math_vec4 rjd_math_vec4_zero(void);
static inline rjd_math_vec4 rjd_math_vec4_xyzw(float x, float y, float z, float w);
static inline rjd_math_vec4 rjd_math_vec4_splat(float v);
static inline rjd_math_vec4 rjd_math_vec4_one(void);
static inline rjd_math_vec4 rjd_math_vec4_setx(rjd_math_vec4 v, float x);
static inline rjd_math_vec4 rjd_math_vec4_sety(rjd_math_vec4 v, float y);
static inline rjd_math_vec4 rjd_math_vec4_setz(rjd_math_vec4 v, float z);
static inline rjd_math_vec4 rjd_math_vec4_setw(rjd_math_vec4 v, float w);
static inline float rjd_math_vec4_x(rjd_math_vec4 v);
static inline float rjd_math_vec4_y(rjd_math_vec4 v);
static inline float rjd_math_vec4_z(rjd_math_vec4 v);
static inline float rjd_math_vec4_w(rjd_math_vec4 v);
static inline float rjd_math_vec4_sum(rjd_math_vec4 v);
static inline float rjd_math_vec4_dot(rjd_math_vec4 a, rjd_math_vec4 b);
static inline float rjd_math_vec4_lengthsq(rjd_math_vec4 v);
static inline float rjd_math_vec4_length(rjd_math_vec4 v);
static inline float rjd_math_vec4_i(rjd_math_vec4 v, size_t index);
static inline float rjd_math_vec4_hmin(rjd_math_vec4 v);
static inline float rjd_math_vec4_hmax(rjd_math_vec4 v);
static inline rjd_math_vec4 rjd_math_vec4_normalize(rjd_math_vec4 v);
static inline rjd_math_vec4 rjd_math_vec4_abs(rjd_math_vec4 v);
static inline rjd_math_vec4 rjd_math_vec4_neg(rjd_math_vec4 v);
static inline rjd_math_vec4 rjd_math_vec4_floor(rjd_math_vec4 v);
static inline rjd_math_vec4 rjd_math_vec4_ceil(rjd_math_vec4 v);
static inline rjd_math_vec4 rjd_math_vec4_round(rjd_math_vec4 v);
static inline rjd_math_vec4 rjd_math_vec4_scale(rjd_math_vec4 v, float s);
static inline rjd_math_vec4 rjd_math_vec4_add(rjd_math_vec4 a, rjd_math_vec4 b);
static inline rjd_math_vec4 rjd_math_vec4_sub(rjd_math_vec4 a, rjd_math_vec4 b);
static inline rjd_math_vec4 rjd_math_vec4_mul(rjd_math_vec4 a, rjd_math_vec4 b);
static inline rjd_math_vec4 rjd_math_vec4_div(rjd_math_vec4 a, rjd_math_vec4 b);
static inline rjd_math_vec4 rjd_math_vec4_min(rjd_math_vec4 a, rjd_math_vec4 b);
static inline rjd_math_vec4 rjd_math_vec4_max(rjd_math_vec4 a, rjd_math_vec4 b);
static inline rjd_math_vec4 rjd_math_vec4_project(rjd_math_vec4 a, rjd_math_vec4 b);
static inline rjd_math_vec4 rjd_math_vec4_lerp(rjd_math_vec4 a, rjd_math_vec4 b, float t);
static inline bool rjd_math_vec4_eq(rjd_math_vec4 a, rjd_math_vec4 b);
static inline bool rjd_math_vec4_ge(rjd_math_vec4 a, rjd_math_vec4 b);
static inline float* rjd_math_vec4_write(rjd_math_vec4 v, float* out);
// vec3
#define rjd_math_vec3_shuffle(v3, x, y, z) ((rjd_math_vec3){_mm_shuffle_ps((v3).v, (v3).v, _MM_SHUFFLE(3, z, y, x))})
static inline rjd_math_vec3 rjd_math_vec3_zero(void);
static inline rjd_math_vec3 rjd_math_vec3_xyz(float x, float y, float z);
static inline rjd_math_vec3 rjd_math_vec3_splat(float v);
static inline rjd_math_vec3 rjd_math_vec3_one(void);
static inline rjd_math_vec3 rjd_math_vec3_up(void);
static inline rjd_math_vec3 rjd_math_vec3_down(void);
static inline rjd_math_vec3 rjd_math_vec3_left(void);
static inline rjd_math_vec3 rjd_math_vec3_right(void);
static inline rjd_math_vec3 rjd_math_vec3_forward(void);
static inline rjd_math_vec3 rjd_math_vec3_back(void);
static inline rjd_math_vec3 rjd_math_vec3_yzx(rjd_math_vec3 v);
static inline rjd_math_vec3 rjd_math_vec3_zxy(rjd_math_vec3 v);
static inline rjd_math_vec3 rjd_math_vec3_setx(rjd_math_vec3 v, float x);
static inline rjd_math_vec3 rjd_math_vec3_sety(rjd_math_vec3 v, float y);
static inline rjd_math_vec3 rjd_math_vec3_setz(rjd_math_vec3 v, float z);
static inline float rjd_math_vec3_x(rjd_math_vec3 v);
static inline float rjd_math_vec3_y(rjd_math_vec3 v);
static inline float rjd_math_vec3_z(rjd_math_vec3 v);
static inline float rjd_math_vec3_sum(rjd_math_vec3 v);
static inline float rjd_math_vec3_dot(rjd_math_vec3 a, rjd_math_vec3 b);
static inline float rjd_math_vec3_angle(rjd_math_vec3 a, rjd_math_vec3 b);
static inline float rjd_math_vec3_lengthsq(rjd_math_vec3 v);
static inline float rjd_math_vec3_length(rjd_math_vec3 v);
static inline float rjd_math_vec3_hmin(rjd_math_vec3 v);
static inline float rjd_math_vec3_hmax(rjd_math_vec3 v);
static inline rjd_math_vec3 rjd_math_vec3_normalize(rjd_math_vec3 v);
static inline rjd_math_vec3 rjd_math_vec3_abs(rjd_math_vec3 v);
static inline rjd_math_vec3 rjd_math_vec3_neg(rjd_math_vec3 v);
static inline rjd_math_vec4 rjd_math_vec4_floor(rjd_math_vec4 v);
static inline rjd_math_vec4 rjd_math_vec4_ceil(rjd_math_vec4 v);
static inline rjd_math_vec4 rjd_math_vec4_round(rjd_math_vec4 v);
static inline rjd_math_vec3 rjd_math_vec3_scale(rjd_math_vec3 v, float s);
static inline rjd_math_vec3 rjd_math_vec3_add(rjd_math_vec3 a, rjd_math_vec3 b);
static inline rjd_math_vec3 rjd_math_vec3_sub(rjd_math_vec3 a, rjd_math_vec3 b);
static inline rjd_math_vec3 rjd_math_vec3_mul(rjd_math_vec3 a, rjd_math_vec3 b);
static inline rjd_math_vec3 rjd_math_vec3_div(rjd_math_vec3 a, rjd_math_vec3 b);
static inline rjd_math_vec3 rjd_math_vec3_cross(rjd_math_vec3 a, rjd_math_vec3 b);
static inline rjd_math_vec3 rjd_math_vec3_min(rjd_math_vec3 a, rjd_math_vec3 b);
static inline rjd_math_vec3 rjd_math_vec3_max(rjd_math_vec3 a, rjd_math_vec3 b);
static inline rjd_math_vec3 rjd_math_vec3_project(rjd_math_vec3 a, rjd_math_vec3 b);
static inline rjd_math_vec3 rjd_math_vec3_reflect(rjd_math_vec3 a, rjd_math_vec3 b);
static inline rjd_math_vec3 rjd_math_vec3_lerp(rjd_math_vec3 a, rjd_math_vec3 b, float t);
static inline bool rjd_math_vec3_eq(rjd_math_vec3 a, rjd_math_vec3 b);
static inline bool rjd_math_vec3_ge(rjd_math_vec3 a, rjd_math_vec3 b);
static inline float* rjd_math_vec3_write(rjd_math_vec3 v, float* out);
static inline float* rjd_math_vec3_writefast(rjd_math_vec3 v, float* out); // writes 4 floats to out
// mat4
static inline rjd_math_mat4 rjd_math_mat4_identity(void);
static inline rjd_math_mat4 rjd_math_mat4_translation(rjd_math_vec3 trans);
//static inline rjd_math_mat4 rjd_math_mat4_rotation(rjd_math_quat rot); // TODO quaternions
static inline rjd_math_mat4 rjd_math_mat4_angleaxis(float angle, rjd_math_vec3 axis);
static inline rjd_math_mat4 rjd_math_mat4_rotationx(float angle);
static inline rjd_math_mat4 rjd_math_mat4_rotationy(float angle);
static inline rjd_math_mat4 rjd_math_mat4_rotationz(float angle);
static inline rjd_math_mat4 rjd_math_mat4_rotationbasis(rjd_math_vec3 x, rjd_math_vec3 y, rjd_math_vec3 z);
static inline rjd_math_mat4 rjd_math_mat4_scaling(float scale);
static inline rjd_math_mat4 rjd_math_mat4_scaling_nonuniform(rjd_math_vec3 scale);
static inline rjd_math_mat4 rjd_math_mat4_add(rjd_math_mat4 a, rjd_math_mat4 b);
static inline rjd_math_mat4 rjd_math_mat4_mul(rjd_math_mat4 a, rjd_math_mat4 b);
static inline rjd_math_vec3 rjd_math_mat4_mulv3(rjd_math_mat4 m, rjd_math_vec3 v);
static inline rjd_math_vec4 rjd_math_mat4_mulv4(rjd_math_mat4 m, rjd_math_vec4 v);
static inline rjd_math_mat4 rjd_math_mat4_inv(rjd_math_mat4 m);
static inline rjd_math_mat4 rjd_math_mat4_transpose(rjd_math_mat4 m);
static inline rjd_math_mat4 rjd_math_mat4_frustum_righthanded(float left, float right, float top, float bot, float near_plane, float far_plane);
static inline rjd_math_mat4 rjd_math_mat4_ortho_righthanded(float left, float right, float top, float bot, float near_plane, float far_plane);
static inline rjd_math_mat4 rjd_math_mat4_ortho_lefthanded(float left, float right, float top, float bot, float near_plane, float far_plane);
static inline rjd_math_mat4 rjd_math_mat4_perspective_righthanded(float y_fov, float aspect, float near_plane, float far_plane);
static inline rjd_math_mat4 rjd_math_mat4_perspective_lefthanded(float y_fov, float aspect, float near_plane, float far_plane);
static inline rjd_math_mat4 rjd_math_mat4_lookat_righthanded(rjd_math_vec3 eye, rjd_math_vec3 target, rjd_math_vec3 up);
static inline rjd_math_mat4 rjd_math_mat4_lookat_lefthanded(rjd_math_vec3 eye, rjd_math_vec3 target, rjd_math_vec3 up);
static inline float* rjd_math_mat4_write_colmajor(rjd_math_mat4 m, float* out);
static inline float* rjd_math_mat4_write_rowmajor(rjd_math_mat4 m, float* out);
// implementation
RJD_MATH_SIGN_FUNCS(RJD_MATH_DEFINE_SIGN_FUNC)
RJD_MATH_ISEQUAL_FUNCS(RJD_MATH_DEFINE_ISEQUAL_FUNC)
RJD_MATH_MIN_FUNCS(RJD_MATH_DEFINE_MIN_FUNC)
RJD_MATH_MAX_FUNCS(RJD_MATH_DEFINE_MAX_FUNC)
RJD_MATH_CLAMP_FUNCS(RJD_MATH_DEFINE_CLAMP_FUNC)
RJD_MATH_TRUNCATE_FUNCS(RJD_MATH_DEFINE_TRUNCATE_FUNC)
RJD_MATH_REMAP_FUNCS(RJD_MATH_DEFINE_REMAP_FUNC)
static inline uint32_t rjd_math_next_pow2_u32(uint32_t v)
{
--v;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
static inline int32_t rjd_math_pow_u32(int32_t v, uint32_t power)
{
int32_t r = 1;
while (power) {
r *= v;
--power;
}
return r;
}
// vec3 <-> vec4 conversion helpers
static inline rjd_math_vec4 rjd_math_vec3to4(rjd_math_vec3 v3) {
rjd_math_vec4 v4 = { v3.v };
return v4;
}
static inline rjd_math_vec3 rjd_math_vec4to3(rjd_math_vec4 v4) {
rjd_math_vec3 v3 = { v4.v };
return v3;
}
static inline rjd_math_vec4 rjd_math_vec3to4w(rjd_math_vec3 v3, float w) {
rjd_math_vec4 v4 = rjd_math_vec3to4(v3);
return rjd_math_vec4_setw(v4, w);
}
static inline rjd_math_vec3 rjd_math_vec4to3w(rjd_math_vec4 v4) {
v4 = rjd_math_vec4_setw(v4, 0);
return rjd_math_vec4to3(v4);
}
// vec4
static inline rjd_math_vec4 rjd_math_vec4_zero(void) {
rjd_math_vec4 v = { _mm_setzero_ps() };
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_xyzw(float x, float y, float z, float w) {
rjd_math_vec4 v = { _mm_set_ps(w, z, y, x) };
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_splat(float f) {
rjd_math_vec4 v = { _mm_set1_ps(f) };
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_one(void) {
return rjd_math_vec4_splat(1);
}
static inline rjd_math_vec4 rjd_math_vec4_setx(rjd_math_vec4 v, float x) {
v.v = _mm_move_ss(v.v, rjd_math_vec4_splat(x).v);
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_sety(rjd_math_vec4 v, float y) {
v = rjd_math_vec4_shuffle(v, 1, 0, 2, 3);
v = rjd_math_vec4_setx(v, y);
v = rjd_math_vec4_shuffle(v, 1, 0, 2, 3);
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_setz(rjd_math_vec4 v, float z) {
v = rjd_math_vec4_shuffle(v, 2, 1, 0, 3);
v = rjd_math_vec4_setx(v, z);
v = rjd_math_vec4_shuffle(v, 2, 1, 0, 3);
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_setw(rjd_math_vec4 v, float w) {
v = rjd_math_vec4_shuffle(v, 3, 1, 2, 0);
v = rjd_math_vec4_setx(v, w);
v = rjd_math_vec4_shuffle(v, 3, 1, 2, 0);
return v;
}
static inline float rjd_math_vec4_x(rjd_math_vec4 v) {
return _mm_cvtss_f32(v.v);
}
static inline float rjd_math_vec4_y(rjd_math_vec4 v) {
return rjd_math_vec4_x(rjd_math_vec4_shuffle(v, 1, 0, 2, 3));
}
static inline float rjd_math_vec4_z(rjd_math_vec4 v) {
return rjd_math_vec4_x(rjd_math_vec4_shuffle(v, 2, 1, 0, 3));
}
static inline float rjd_math_vec4_w(rjd_math_vec4 v) {
return rjd_math_vec4_x(rjd_math_vec4_shuffle(v, 3, 1, 2, 0));
}
static inline float rjd_math_vec4_sum(rjd_math_vec4 v) {
v.v = _mm_hadd_ps(v.v, v.v);
v.v = _mm_hadd_ps(v.v, v.v);
return rjd_math_vec4_x(v);
}
static inline float rjd_math_vec4_dot(rjd_math_vec4 a, rjd_math_vec4 b) {
rjd_math_vec4 product = rjd_math_vec4_mul(a,b);
return rjd_math_vec4_sum(product);
}
static inline float rjd_math_vec4_lengthsq(rjd_math_vec4 v) {
return rjd_math_vec4_dot(v, v);
}
static inline float rjd_math_vec4_length(rjd_math_vec4 v) {
return sqrtf(rjd_math_vec4_lengthsq(v));
}
static inline float rjd_math_vec4_hmin(rjd_math_vec4 v) {
v = rjd_math_vec4_min(v, rjd_math_vec4_shuffle(v,1,1,2,3));
v = rjd_math_vec4_min(v, rjd_math_vec4_shuffle(v,2,1,2,3));
v = rjd_math_vec4_min(v, rjd_math_vec4_shuffle(v,3,1,2,3));
return rjd_math_vec4_x(v);
}
static inline float rjd_math_vec4_hmax(rjd_math_vec4 v) {
v = rjd_math_vec4_max(v, rjd_math_vec4_shuffle(v,1,1,2,3));
v = rjd_math_vec4_max(v, rjd_math_vec4_shuffle(v,2,1,2,3));
v = rjd_math_vec4_max(v, rjd_math_vec4_shuffle(v,3,1,2,3));
return rjd_math_vec4_x(v);
}
static inline float rjd_math_vec4_i(rjd_math_vec4 v, size_t index) {
switch(index) {
case 0: v = rjd_math_vec4_shuffle(v,0,0,0,0); break;
case 1: v = rjd_math_vec4_shuffle(v,1,1,1,1); break;
case 2: v = rjd_math_vec4_shuffle(v,2,2,2,2); break;
case 3: v = rjd_math_vec4_shuffle(v,3,3,3,3); break;
default:
RJD_ASSERTFAIL("index must be between 0 and 3");
break;
}
return rjd_math_vec4_x(v);
}
static inline rjd_math_vec4 rjd_math_vec4_normalize(rjd_math_vec4 v) {
float length = rjd_math_vec4_length(v);
RJD_ASSERT(length != 0);
rjd_math_vec4 l = rjd_math_vec4_splat(length);
return rjd_math_vec4_div(v, l);
}
static inline rjd_math_vec4 rjd_math_vec4_abs(rjd_math_vec4 v) {
__m128 signbits = _mm_set1_ps(-0.0f);
v.v = _mm_andnot_ps(signbits, v.v); // Remove the sign bit
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_neg(rjd_math_vec4 v) {
return rjd_math_vec4_scale(v, -1);
}
static inline rjd_math_vec4 rjd_math_vec4_floor(rjd_math_vec4 v) {
v.v = _mm_floor_ps(v.v);
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_ceil(rjd_math_vec4 v) {
v.v = _mm_ceil_ps(v.v);
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_round(rjd_math_vec4 v) {
v.v = _mm_round_ps(v.v, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC);
return v;
}
static inline rjd_math_vec4 rjd_math_vec4_scale(rjd_math_vec4 v, float s) {
rjd_math_vec4 scales = rjd_math_vec4_splat(s);
return rjd_math_vec4_mul(v, scales);
}
static inline rjd_math_vec4 rjd_math_vec4_add(rjd_math_vec4 a, rjd_math_vec4 b) {
a.v = _mm_add_ps(a.v, b.v);
return a;
}
static inline rjd_math_vec4 rjd_math_vec4_sub(rjd_math_vec4 a, rjd_math_vec4 b) {
a.v = _mm_sub_ps(a.v, b.v);
return a;
}
static inline rjd_math_vec4 rjd_math_vec4_mul(rjd_math_vec4 a, rjd_math_vec4 b) {
a.v = _mm_mul_ps(a.v, b.v);
return a;
}
static inline rjd_math_vec4 rjd_math_vec4_div(rjd_math_vec4 a, rjd_math_vec4 b) {
a.v = _mm_div_ps(a.v, b.v);
return a;
}
static inline rjd_math_vec4 rjd_math_vec4_min(rjd_math_vec4 a, rjd_math_vec4 b) {
a.v = _mm_min_ps(a.v, b.v);
return a;
}
static inline rjd_math_vec4 rjd_math_vec4_max(rjd_math_vec4 a, rjd_math_vec4 b) {
a.v = _mm_max_ps(a.v, b.v);
return a;
}
static inline rjd_math_vec4 rjd_math_vec4_project(rjd_math_vec4 a, rjd_math_vec4 b) {
float dot = rjd_math_vec4_dot(a, b);
float lb = rjd_math_vec4_length(b);
return rjd_math_vec4_scale(b, dot / lb);
}
static inline rjd_math_vec4 rjd_math_vec4_lerp(rjd_math_vec4 a, rjd_math_vec4 b, float t) {
rjd_math_vec4 v = rjd_math_vec4_sub(b, a);
v = rjd_math_vec4_scale(v, t);
v = rjd_math_vec4_add(v, a);
return v;
}
static inline bool rjd_math_vec4_eq(rjd_math_vec4 a, rjd_math_vec4 b) {
return (_mm_movemask_ps(_mm_cmpeq_ps(a.v, b.v)) & 0xF) == 0xF;
}
static inline bool rjd_math_vec4_ge(rjd_math_vec4 a, rjd_math_vec4 b) {
return (_mm_movemask_ps(_mm_cmpge_ps(a.v, b.v)) & 0xF) == 0xF;
}
static inline float* rjd_math_vec4_write(rjd_math_vec4 v, float* out) {
RJD_ASSERT(RJD_MEM_ISALIGNED(out, 16));
_mm_stream_ps(out, v.v);
return out + 4;
}
// vec3
static inline rjd_math_vec3 rjd_math_vec3_zero(void) {
return rjd_math_vec4to3(rjd_math_vec4_zero());
}
static inline rjd_math_vec3 rjd_math_vec3_xyz(float x, float y, float z) {
return rjd_math_vec4to3(rjd_math_vec4_xyzw(x,y,z,0));
}
static inline rjd_math_vec3 rjd_math_vec3_splat(float v) { return rjd_math_vec3_xyz(v,v,v); }
static inline rjd_math_vec3 rjd_math_vec3_one(void) { return rjd_math_vec3_xyz(1,1,1); }
static inline rjd_math_vec3 rjd_math_vec3_up(void) { return rjd_math_vec3_xyz(0,1,0); }
static inline rjd_math_vec3 rjd_math_vec3_down(void) { return rjd_math_vec3_xyz(0,-1,0); }
static inline rjd_math_vec3 rjd_math_vec3_left(void) { return rjd_math_vec3_xyz(-1,0,0); }
static inline rjd_math_vec3 rjd_math_vec3_right(void) { return rjd_math_vec3_xyz(1,0,0); }
static inline rjd_math_vec3 rjd_math_vec3_forward(void) { return rjd_math_vec3_xyz(0,0,1); }
static inline rjd_math_vec3 rjd_math_vec3_back(void) { return rjd_math_vec3_xyz(0,0,-1); }
static inline rjd_math_vec3 rjd_math_vec3_yzx(rjd_math_vec3 v) {
return rjd_math_vec3_shuffle(v, 1, 2, 0);
}
static inline rjd_math_vec3 rjd_math_vec3_zxy(rjd_math_vec3 v) {
return rjd_math_vec3_shuffle(v, 2, 0, 1);
}
static inline rjd_math_vec3 rjd_math_vec3_setx(rjd_math_vec3 v, float x) {
v.v = _mm_move_ss(v.v, rjd_math_vec3_splat(x).v);
return v;
}
static inline rjd_math_vec3 rjd_math_vec3_sety(rjd_math_vec3 v, float y) {
return rjd_math_vec4to3(rjd_math_vec4_sety(rjd_math_vec3to4(v), y));
}
static inline rjd_math_vec3 rjd_math_vec3_setz(rjd_math_vec3 v, float z) {
return rjd_math_vec4to3(rjd_math_vec4_setz(rjd_math_vec3to4(v), z));
}
static inline float rjd_math_vec3_x(rjd_math_vec3 v) {
return rjd_math_vec4_x(rjd_math_vec3to4(v));
}
static inline float rjd_math_vec3_y(rjd_math_vec3 v) {
return rjd_math_vec4_y(rjd_math_vec3to4(v));
}
static inline float rjd_math_vec3_z(rjd_math_vec3 v) {
return rjd_math_vec4_z(rjd_math_vec3to4(v));
}
static inline float rjd_math_vec3_sum(rjd_math_vec3 v) {
return rjd_math_vec4_sum(rjd_math_vec3to4(v));
}
static inline float rjd_math_vec3_dot(rjd_math_vec3 a, rjd_math_vec3 b) {
return rjd_math_vec4_dot(rjd_math_vec3to4(a), rjd_math_vec3to4(b));
}
static inline float rjd_math_vec3_angle(rjd_math_vec3 a, rjd_math_vec3 b) {
float dot = rjd_math_vec3_dot(a, b);
float la = rjd_math_vec3_length(a);
float lb = rjd_math_vec3_length(b);
return acosf(dot / (la * lb));
}
static inline float rjd_math_vec3_lengthsq(rjd_math_vec3 v) {
return rjd_math_vec4_lengthsq(rjd_math_vec3to4(v));
}
static inline float rjd_math_vec3_length(rjd_math_vec3 v) {
return rjd_math_vec4_length(rjd_math_vec3to4(v));
}
static inline float rjd_math_vec3_hmin(rjd_math_vec3 v) {
v = rjd_math_vec3_min(v, rjd_math_vec3_shuffle(v,1,1,2));
v = rjd_math_vec3_min(v, rjd_math_vec3_shuffle(v,2,1,2));
return rjd_math_vec3_x(v);
}
static inline float rjd_math_vec3_hmax(rjd_math_vec3 v) {
v = rjd_math_vec3_max(v, rjd_math_vec3_shuffle(v,1,1,2));
v = rjd_math_vec3_max(v, rjd_math_vec3_shuffle(v,2,1,2));
return rjd_math_vec3_x(v);
}
static inline rjd_math_vec3 rjd_math_vec3_normalize(rjd_math_vec3 v) {
return rjd_math_vec4to3(rjd_math_vec4_normalize(rjd_math_vec3to4(v)));
}
static inline rjd_math_vec3 rjd_math_vec3_abs(rjd_math_vec3 v) {
return rjd_math_vec4to3(rjd_math_vec4_abs(rjd_math_vec3to4(v)));
}
static inline rjd_math_vec3 rjd_math_vec3_neg(rjd_math_vec3 v) {
return rjd_math_vec4to3(rjd_math_vec4_neg(rjd_math_vec3to4(v)));
}
static inline rjd_math_vec3 rjd_math_vec3_floor(rjd_math_vec3 v) {
return rjd_math_vec4to3(rjd_math_vec4_floor(rjd_math_vec3to4(v)));
}
static inline rjd_math_vec3 rjd_math_vec3_ceil(rjd_math_vec3 v) {
return rjd_math_vec4to3(rjd_math_vec4_ceil(rjd_math_vec3to4(v)));
}
static inline rjd_math_vec3 rjd_math_vec3_round(rjd_math_vec3 v) {
return rjd_math_vec4to3(rjd_math_vec4_abs(rjd_math_vec3to4(v)));
}
static inline rjd_math_vec3 rjd_math_vec3_scale(rjd_math_vec3 v, float s) {
return rjd_math_vec4to3(rjd_math_vec4_scale(rjd_math_vec3to4(v), s));
}
static inline rjd_math_vec3 rjd_math_vec3_add(rjd_math_vec3 a, rjd_math_vec3 b) {
return rjd_math_vec4to3(rjd_math_vec4_add(rjd_math_vec3to4(a), rjd_math_vec3to4(b)));
}
static inline rjd_math_vec3 rjd_math_vec3_sub(rjd_math_vec3 a, rjd_math_vec3 b) {
return rjd_math_vec4to3(rjd_math_vec4_sub(rjd_math_vec3to4(a), rjd_math_vec3to4(b)));
}
static inline rjd_math_vec3 rjd_math_vec3_mul(rjd_math_vec3 a, rjd_math_vec3 b) {
return rjd_math_vec4to3(rjd_math_vec4_mul(rjd_math_vec3to4(a), rjd_math_vec3to4(b)));
}
static inline rjd_math_vec3 rjd_math_vec3_div(rjd_math_vec3 a, rjd_math_vec3 b) {
return rjd_math_vec4to3(rjd_math_vec4_div(rjd_math_vec3to4(a), rjd_math_vec3to4(b)));
}
static inline rjd_math_vec3 rjd_math_vec3_cross(rjd_math_vec3 a, rjd_math_vec3 b) {
rjd_math_vec3 ap = rjd_math_vec3_mul(rjd_math_vec3_yzx(a), rjd_math_vec3_zxy(b));
rjd_math_vec3 bp = rjd_math_vec3_mul(rjd_math_vec3_zxy(a), rjd_math_vec3_yzx(b));
return rjd_math_vec3_sub(ap, bp);
}
static inline rjd_math_vec3 rjd_math_vec3_min(rjd_math_vec3 a, rjd_math_vec3 b) {
return rjd_math_vec4to3(rjd_math_vec4_min(rjd_math_vec3to4(a), rjd_math_vec3to4(b)));
}
static inline rjd_math_vec3 rjd_math_vec3_max(rjd_math_vec3 a, rjd_math_vec3 b) {
return rjd_math_vec4to3(rjd_math_vec4_max(rjd_math_vec3to4(a), rjd_math_vec3to4(b)));
}
static inline rjd_math_vec3 rjd_math_vec3_project(rjd_math_vec3 a, rjd_math_vec3 b) {
return rjd_math_vec4to3(rjd_math_vec4_project(rjd_math_vec3to4(a), rjd_math_vec3to4(b)));
}
static inline rjd_math_vec3 rjd_math_vec3_reflect(rjd_math_vec3 v, rjd_math_vec3 n) {
RJD_ASSERT(rjd_math_vec3_eq(n, rjd_math_vec3_normalize(n)));
rjd_math_vec3 projected = rjd_math_vec3_scale(n, 2 * rjd_math_vec3_dot(v, n));
return rjd_math_vec3_sub(v, projected);
}
static inline rjd_math_vec3 rjd_math_vec3_lerp(rjd_math_vec3 a, rjd_math_vec3 b, float t) {
return rjd_math_vec4to3(rjd_math_vec4_lerp(rjd_math_vec3to4(a), rjd_math_vec3to4(b), t));
}
static inline bool rjd_math_vec3_eq(rjd_math_vec3 a, rjd_math_vec3 b) {
return (_mm_movemask_ps(_mm_cmpeq_ps(a.v, b.v)) & 7) == 7; // 7 is the platform-independent version of 0b111
}
static inline bool rjd_math_vec3_ge(rjd_math_vec3 a, rjd_math_vec3 b) {
return (_mm_movemask_ps(_mm_cmpge_ps(a.v, b.v)) & 7) == 7;
}
static inline float* rjd_math_vec3_write(rjd_math_vec3 v, float* out) {
RJD_FORCE_ALIGN(16, float) tmp[4];
_mm_stream_ps(tmp, v.v);
memcpy(out, tmp, sizeof(float) * 3);
return out + 3;
}
static inline float* rjd_math_vec3_writefast(rjd_math_vec3 v, float* out) {
RJD_ASSERT(RJD_MEM_ISALIGNED(out, 16));
_mm_stream_ps(out, v.v);
return out + 3;
}
// mat4
static inline rjd_math_mat4 rjd_math_mat4_identity(void) {
rjd_math_mat4 m;
m.m[0] = rjd_math_vec4_xyzw(1,0,0,0);
m.m[1] = rjd_math_vec4_xyzw(0,1,0,0);
m.m[2] = rjd_math_vec4_xyzw(0,0,1,0);
m.m[3] = rjd_math_vec4_xyzw(0,0,0,1);
return m;
}
static inline rjd_math_mat4 rjd_math_mat4_translation(rjd_math_vec3 trans) {
rjd_math_mat4 m;
m.m[0] = rjd_math_vec4_xyzw(1,0,0,0);
m.m[1] = rjd_math_vec4_xyzw(0,1,0,0);
m.m[2] = rjd_math_vec4_xyzw(0,0,1,0);
m.m[3] = rjd_math_vec3to4w(trans, 1);
return m;
}
static inline rjd_math_mat4 rjd_math_mat4_angleaxis(float angle, rjd_math_vec3 axis) {
rjd_math_mat4 m;
// TODO optimize
float c = cosf(angle);
float k = 1 - c;
float s = sinf(angle);
float x = rjd_math_vec3_x(axis);
float y = rjd_math_vec3_y(axis);
float z = rjd_math_vec3_z(axis);
//rjd_math_vec4 axis4 = rjd_math_vec3to4w(axis,1); // x,y,z,1
//rjd_math_vec4 tmp1 = rjd_math_vec4_mul(axis4, rjd_math_vec4_xyzw(s,s,s,c)); // x*s, y*s, z*s, c
//rjd_math_vec4 vk = rjd_math_vec4_splat(k);
//rjd_math_vec4 v0 = rjd_math_vec4_mul(axis4, rjd_math_vec4_shuffle(axis4,0,0,0,3));
//v0 = rjd_math_vec4_mul(v0, vk);
//v0 = rjd_math_vec4_add(v0, rjd_math_vec4_shuffle(tmp1,3,2,1));
//m.m[0] =
//vec3 diagonal = rjd_math_vec3_mul(axis, axis);
//diagonal = rjd_math_vec3_mul(diagonal, veck);
//diagonal = rjd_math_vec3_add(diagonal, rjd_math_vec3_splat(c));
float m00 = k*x*x + c;
float m10 = k*x*y + z*s;
float m20 = k*x*z - y*s;
float m01 = k*x*y - z*s;
float m11 = k*y*y + c;
float m21 = k*y*z + x*s;
float m02 = k*x*z + y*s;
float m12 = k*y*z - x*s;
float m22 = k*z*z + c;
m.m[0] = rjd_math_vec4_xyzw(m00, m10, m20, 0);
m.m[1] = rjd_math_vec4_xyzw(m01, m11, m21, 0);
m.m[2] = rjd_math_vec4_xyzw(m02, m12, m22, 0);
m.m[3] = rjd_math_vec4_xyzw( 0, 0, 0, 1);
return m;
}
static inline rjd_math_mat4 rjd_math_mat4_rotationx(float angle) {
return rjd_math_mat4_angleaxis(angle, rjd_math_vec3_right());
}
static inline rjd_math_mat4 rjd_math_mat4_rotationy(float angle) {
return rjd_math_mat4_angleaxis(angle, rjd_math_vec3_up());
}
static inline rjd_math_mat4 rjd_math_mat4_rotationz(float angle) {
return rjd_math_mat4_angleaxis(angle, rjd_math_vec3_forward());
}
static inline rjd_math_mat4 rjd_math_mat4_rotationbasis(rjd_math_vec3 x, rjd_math_vec3 y, rjd_math_vec3 z) {
rjd_math_vec4 xx = rjd_math_vec3to4(x);
rjd_math_vec4 yy = rjd_math_vec3to4(y);
rjd_math_vec4 zz = rjd_math_vec3to4(z);
rjd_math_mat4 m = { { xx, yy, zz, rjd_math_vec4_xyzw(0,0,0,1) } };
return m;
}
static inline rjd_math_mat4 rjd_math_mat4_scaling(float s) {
rjd_math_mat4 m;
m.m[0] = rjd_math_vec4_xyzw(s,0,0,0);
m.m[1] = rjd_math_vec4_xyzw(0,s,0,0);
m.m[2] = rjd_math_vec4_xyzw(0,0,s,0);
m.m[3] = rjd_math_vec4_xyzw(0,0,0,1);
return m;
}
static inline rjd_math_mat4 rjd_math_mat4_scaling_nonuniform(rjd_math_vec3 scale) {
float x = rjd_math_vec3_x(scale);
float y = rjd_math_vec3_y(scale);
float z = rjd_math_vec3_z(scale);
rjd_math_mat4 m;
m.m[0] = rjd_math_vec4_xyzw(x,0,0,0);
m.m[1] = rjd_math_vec4_xyzw(0,y,0,0);
m.m[2] = rjd_math_vec4_xyzw(0,0,z,0);
m.m[3] = rjd_math_vec4_xyzw(0,0,0,1);
return m;
}
static inline rjd_math_mat4 rjd_math_mat4_add(rjd_math_mat4 a, rjd_math_mat4 b) {
rjd_math_mat4 m = {0};
for (size_t i = 0; i < rjd_countof(m.m); ++i) {
m.m[i] = rjd_math_vec4_add(a.m[i], b.m[i]);
}
return m;
}
static inline rjd_math_mat4 rjd_math_mat4_mul(rjd_math_mat4 a, rjd_math_mat4 b) {
rjd_math_mat4 m = {0};
for (size_t i = 0; i < rjd_countof(m.m); ++i) {
m.m[i] = rjd_math_mat4_mulv4(a, b.m[i]);
}
return m;
}
static inline rjd_math_vec3 rjd_math_mat4_mulv3(rjd_math_mat4 m, rjd_math_vec3 v) {
return rjd_math_vec4to3(rjd_math_mat4_mulv4(m, rjd_math_vec3to4w(v, 1)));
}
static inline rjd_math_vec4 rjd_math_mat4_mulv4(rjd_math_mat4 m, rjd_math_vec4 v) {
// TODO optimize
rjd_math_mat4 trans = rjd_math_mat4_transpose(m);
float x = rjd_math_vec4_dot(trans.m[0], v);
float y = rjd_math_vec4_dot(trans.m[1], v);
float z = rjd_math_vec4_dot(trans.m[2], v);
float w = rjd_math_vec4_dot(trans.m[3], v);
return rjd_math_vec4_xyzw(x, y, z, w);
}
static inline rjd_math_mat4 rjd_math_mat4_inv(rjd_math_mat4 m) {
rjd_math_mat4 t = rjd_math_mat4_transpose(m);
rjd_math_vec4 t0 = t.m[0];
rjd_math_vec4 t1 = t.m[1];
rjd_math_vec4 t2 = t.m[2];
rjd_math_vec4 t3 = t.m[3];
rjd_math_mat4 inv;
rjd_math_vec4 term;
// first column
//inv0.x = m11m22m33 + m12m23m31 + m13m21m32 - m11m23m32 - m12m21m33 - m13m22m31;
//inv0.y = m10m23m32 + m12m20m33 + m13m22m30 - m10m22m33 - m12m23m30 - m13m20m32;
//inv0.z = m10m21m31 + m11m23m30 + m13m20m31 - m10m23m30 - m11m20m33 - m13m21m30;
//inv0.w = m10m22m31 + m11m20m32 + m12m21m30 - m10m21m32 - m11m22m30 - m12m20m31;
// x y z w
term = rjd_math_vec4_shuffle(t1,1,0,0,0); // m11, m10, m10, m10
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,2,3,1,2)); // m11m22, m10m23, m10m21, m10m22
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,3,2,1,1)); // m11m22m33,m10m23m32,m10m21m31,m10m22m31
inv.m[0] = term;
term = rjd_math_vec4_shuffle(t1,2,2,1,1); // m12, m12, m11, m11
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,3,0,3,0)); // m12m23, m12m20, m11m23, m11m20
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,1,3,0,2)); // m12m23m31,m12m20m33,m11m23m30,m11m20m32
inv.m[0] = rjd_math_vec4_add(inv.m[0], term);
term = rjd_math_vec4_shuffle(t1,3,3,3,2); // m13, m13, m13, m12
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,2,3,1,2)); // m13m21, m13m22, m13m20, m12m21
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,3,2,1,1)); // m13m21m32,m13m22m30,m13m20m31,m12m21m30
inv.m[0] = rjd_math_vec4_add(inv.m[0], term);
term = rjd_math_vec4_shuffle(t1,1,0,0,0); // m11, m10, m10, m10
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,3,2,3,1)); // m11m23, m10m22, m10m23, m10m21
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,2,3,0,2)); // m11m23m32,m10m22m33,m10m23m30,m10m21m32
inv.m[0] = rjd_math_vec4_sub(inv.m[0], term);
term = rjd_math_vec4_shuffle(t1,2,2,1,1); // m12, m12, m11, m11
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,1,3,0,2)); // m12m21, m12m23, m11m20, m11m22
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,3,0,3,0)); // m12m21m33,m12m23m30,m11m20m33,m11m22m30
inv.m[0] = rjd_math_vec4_sub(inv.m[0], term);
term = rjd_math_vec4_shuffle(t1,3,3,3,2); // m13, m13, m13, m12
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,2,0,1,0)); // m13m22, m13m20, m13m21, m12m20
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,1,2,0,1)); // m13m22m31,m13m20m32,m13m21m30,m12m20m31
inv.m[0] = rjd_math_vec4_sub(inv.m[0], term);
// second column
// inv.m[1].x = m01m23m32 + m02m21m33 + m03m22m31 - m01m22m33 - m02m23m31 - m03m21m32
// inv.m[1].y = m00m22m33 + m02m23m30 + m03m20m30 - m00m23m32 - m02m20m33 - m03m22m30
// inv.m[1].z = m00m23m31 + m01m20m33 + m03m21m30 - m00m21m33 - m01m23m30 - m03m20m31
// inv.m[1].w = m00m21m32 + m01m22m30 + m02m20m31 - m00m22m31 - m01m20m32 - m02m21m30
term = rjd_math_vec4_shuffle(t0,1,0,0,0); // m01, m00, m00, m00
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,3,2,3,1)); // m01m23, m00m22, m00m23, m00m21
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,2,3,1,2)); // m01m23m32,m00m22m33,m00m23m31,m00m21m32
inv.m[1] = term;
term = rjd_math_vec4_shuffle(t0,2,2,1,1); // m02, m02, m01, m01
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,1,3,0,2)); // m02m21, m02m23, m01m20, m01m22
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,3,0,3,0)); // m02m21m33,m02m23m30,m01m20m33,m01m22m30
inv.m[1] = rjd_math_vec4_add(inv.m[1], term);
term = rjd_math_vec4_shuffle(t0,3,3,3,2); // m03, m03, m03, m02
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,2,0,1,0)); // m03m22, m03m20, m03m21, m02m20
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,1,0,0,1)); // m03m22m31,m03m20m30,m03m21m30,m02m20m31
inv.m[1] = rjd_math_vec4_add(inv.m[1], term);
term = rjd_math_vec4_shuffle(t0,1,0,0,0); // m01, m00, m00, m00
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,2,3,1,2)); // m01m22, m00m23, m00m21, m00m22
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,3,2,3,1)); // m01m22m33,m00m23m32,m00m21m33,m00m22m31
inv.m[1] = rjd_math_vec4_sub(inv.m[1], term);
term = rjd_math_vec4_shuffle(t0,2,2,1,1); // m02, m02, m01, m01
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,3,0,3,0)); // m02m23, m02m20, m01m23, m01m20
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,1,3,0,2)); // m02m23m31,m02m20m33,m01m23m30,m01m20m32
inv.m[1] = rjd_math_vec4_sub(inv.m[1], term);
term = rjd_math_vec4_shuffle(t0,3,3,3,2); // m03, m03, m03, m02
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,1,2,0,1)); // m03m21, m03m22, m03m20, m02m21
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,2,0,1,0)); // m03m21m32,m03m22m30,m03m20m31,m02m21m30
inv.m[1] = rjd_math_vec4_sub(inv.m[1], term);
// third column
// inv.m[2].x = m01m12m33 + m02m13m31 + m03m11m32 - m01m13m32 - m02m11m33 - m03m12m31
// inv.m[2].y = m00m13m32 + m02m10m33 + m03m12m30 - m00m12m33 - m02m13m30 - m03m10m32
// inv.m[2].z = m00m11m33 + m01m13m30 + m03m10m31 - m00m13m31 - m01m10m33 - m01m11m30
// inv.m[2].w = m00m12m31 + m01m10m32 + m02m11m30 - m00m11m32 - m01m12m30 - m02m10m31
term = rjd_math_vec4_shuffle(t0,1,0,0,0); // m01 m00 m00 m00
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,2,3,1,2)); // m01m12 m00m13 m00m11 m00m12
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,3,2,3,1)); // m01m12m33 m00m13m32 m00m11m33 m00m12m31
inv.m[2] = term;
term = rjd_math_vec4_shuffle(t0,2,2,1,1); // m02 m02 m01 m01
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,3,0,3,0)); // m02m13 m02m10 m01m13 m01m10
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,1,3,0,2)); // m02m13m31 m02m10m33 m01m13m30 m01m10m32
inv.m[2] = rjd_math_vec4_add(inv.m[2], term);
term = rjd_math_vec4_shuffle(t0,3,3,3,2); // m03 m03 m03 m02
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,1,2,0,1)); // m03m11 m03m12 m03m10 m02m11
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,2,0,1,0)); // m03m11m32 m03m12m30 m03m10m31 m02m11m30
inv.m[2] = rjd_math_vec4_add(inv.m[2], term);
term = rjd_math_vec4_shuffle(t0,1,0,0,0); // m01 m00 m00 m00
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,3,2,3,1)); // m01m13 m00m12 m00m13 m00m11
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,2,3,1,2)); // m01m13m32 m00m12m33 m00m13m31 m00m11m32
inv.m[2] = rjd_math_vec4_sub(inv.m[2], term);
term = rjd_math_vec4_shuffle(t0,2,2,1,1); // m02 m02 m01 m01
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,1,3,0,2)); // m02m11 m02m13 m01m10 m01m12
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,3,0,3,0)); // m02m11m33 m02m13m30 m01m10m33 m01m12m30
inv.m[2] = rjd_math_vec4_sub(inv.m[2], term);
term = rjd_math_vec4_shuffle(t0,3,3,1,2); // m03 m03 m01 m02
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,2,0,1,0)); // m03m12 m03m10 m01m11 m02m10
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t3,1,2,0,1)); // m03m12m31 m03m10m32 m01m11m30 m02m10m31
inv.m[2] = rjd_math_vec4_sub(inv.m[2], term);
// fourth column
// inv.m[3].x = m01m13m22 + m02m11m23 + m03m12m21 - m01m12m23 - m02m13m21 - m03m11m22
// inv.m[3].y = m00m12m23 + m02m13m20 + m03m10m22 - m00m13m22 - m02m10m23 - m03m12m20
// inv.m[3].z = m00m13m21 + m01m13m23 + m03m11m20 - m00m11m23 - m01m13m20 - m03m10m21
// inv.m[3].w = m00m11m22 + m01m12m20 + m02m10m21 - m00m12m21 - m01m10m22 - m02m11m20
term = rjd_math_vec4_shuffle(t0,1,0,0,0); // m01 m00 m00 m00
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,3,2,3,1)); // m01m13 m00m12 m00m13 m00m11
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,2,3,1,2)); // m01m13m22 m00m12m23 m00m13m21 m00m11m22
inv.m[3] = term;
term = rjd_math_vec4_shuffle(t0,2,2,1,1); // m02 m02 m01 m01
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,1,3,3,2)); // m02m11 m02m13 m01m13 m01m12
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,3,0,3,0)); // m02m11m23 m02m13m20 m01m13m23 m01m12m20
inv.m[3] = rjd_math_vec4_add(inv.m[3], term);
term = rjd_math_vec4_shuffle(t0,3,3,3,2); // m03 m03 m03 m02
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,2,0,1,0)); // m03m12 m03m10 m03m11 m02m10
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,1,2,0,1)); // m03m12m21 m03m10m22 m03m11m20 m02m10m21
inv.m[3] = rjd_math_vec4_add(inv.m[3], term);
term = rjd_math_vec4_shuffle(t0,1,0,0,0); // m01 m00 m00 m00
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t1,2,3,1,2)); // m01m12 m00m13 m00m11 m00m12
term = rjd_math_vec4_mul(term, rjd_math_vec4_shuffle(t2,3,2,3,1)); // m01m12m23 m00m13m22 m00m11m23 m00m12m21