-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloat_math.h
More file actions
2654 lines (2193 loc) · 90.1 KB
/
float_math.h
File metadata and controls
2654 lines (2193 loc) · 90.1 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
// float_math_amalgamated.h - Single-file distribution
// Generated automatically - do not edit
//
// This is an amalgamated version of float_math containing all implementations
// in a single header file for easy distribution.
//
// Usage: Just include this file. Platform detection is automatic.
//
// To force a specific implementation, define before including:
// #define FLOAT_MATH_FORCE_FALLBACK 1 // Force scalar implementation
// #define FLOAT_MATH_FORCE_SSE 1 // Force SSE implementation
// #define FLOAT_MATH_FORCE_NEON 1 // Force NEON implementation
#ifndef FLOAT_MATH_AMALGAMATED_H
#define FLOAT_MATH_AMALGAMATED_H
// Platform detection and implementation selection
#if defined(FLOAT_MATH_FORCE_FALLBACK)
#define FLOAT_MATH_USE_FALLBACK 1
#elif defined(FLOAT_MATH_FORCE_SSE)
#define FLOAT_MATH_USE_SSE 1
#elif defined(FLOAT_MATH_FORCE_NEON)
#define FLOAT_MATH_USE_NEON 1
#elif defined(__SSE4_1__) || (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86)))
#define FLOAT_MATH_USE_SSE 1
#elif defined(__ARM_NEON) || defined(__aarch64__)
#define FLOAT_MATH_USE_NEON 1
#else
#define FLOAT_MATH_USE_FALLBACK 1
#endif
// ============================================================================
// Fallback (scalar) implementation
// ============================================================================
#if defined(FLOAT_MATH_USE_FALLBACK)
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#ifdef __cplusplus
extern "C" {
#endif
// ============================================================================
// Vector types (scalar fallback - no SIMD)
// ============================================================================
typedef struct { float x, y; } float2;
typedef struct { float x, y, z; } float3;
typedef struct { float x, y, z; } float3s; // Same as float3 in fallback (no SIMD padding)
typedef struct { float x, y, z, w; } float4;
// Matrix type (row-major for direct shader compatibility)
typedef struct {
float m[16]; // Row-major: [m00, m01, m02, m03, m10, m11, ...]
} float4x4;
// Conversions between float3 and float3s (no-op in fallback)
static inline float3 float3s_to_float3(float3s v) { return (float3){v.x, v.y, v.z}; }
static inline float3s float3_to_float3s(float3 v) { return (float3s){v.x, v.y, v.z}; }
// ============================================================================
// Float2 operations
// ============================================================================
static inline float2 float2_add (float2 a, float2 b) { return (float2){a.x + b.x, a.y + b.y}; }
static inline float2 float2_add_s(float2 a, float s) { return (float2){a.x + s, a.y + s}; }
static inline float2 float2_sub (float2 a, float2 b) { return (float2){a.x - b.x, a.y - b.y}; }
static inline float2 float2_sub_s(float2 a, float s) { return (float2){a.x - s, a.y - s}; }
static inline float2 float2_mul (float2 a, float2 b) { return (float2){a.x * b.x, a.y * b.y}; }
static inline float2 float2_mul_s(float2 a, float s) { return (float2){a.x * s, a.y * s}; }
static inline float2 float2_div (float2 a, float2 b) { return (float2){a.x / b.x, a.y / b.y}; }
static inline float2 float2_div_s(float2 a, float s) { return (float2){a.x / s, a.y / s}; }
static inline float float2_dot (float2 a, float2 b) { return a.x * b.x + a.y * b.y; }
static inline float float2_mag2 (float2 v) { return float2_dot(v, v); }
static inline float float2_dist2(float2 a, float2 b) { return float2_mag2(float2_sub(a, b)); }
static inline float2 float2_frac(float2 v) {
return (float2){v.x - floorf(v.x), v.y - floorf(v.y)};
}
static inline float2 float2_floor(float2 v) {
return (float2){floorf(v.x), floorf(v.y)};
}
static inline float2 float2_ceil(float2 v) {
return (float2){ceilf(v.x), ceilf(v.y)};
}
static inline float2 float2_abs(float2 v) {
return (float2){fabsf(v.x), fabsf(v.y)};
}
static inline float2 float2_min(float2 a, float2 b) {
return (float2){fminf(a.x, b.x), fminf(a.y, b.y)};
}
static inline float2 float2_max(float2 a, float2 b) {
return (float2){fmaxf(a.x, b.x), fmaxf(a.y, b.y)};
}
static inline float float2_mag(float2 v) {
return sqrtf(float2_mag2(v));
}
static inline float float2_dist(float2 a, float2 b) {
return sqrtf(float2_dist2(a, b));
}
static inline float2 float2_norm(float2 v) {
float mag = float2_mag(v);
if (mag == 0.0f) {
return (float2){0.0f, 0.0f};
}
return float2_div_s(v, mag);
}
// ============================================================================
// Float3 operations
// ============================================================================
static inline float3 float3_add (float3 a, float3 b) { return (float3){a.x + b.x, a.y + b.y, a.z + b.z}; }
static inline float3 float3_add_s(float3 a, float s) { return (float3){a.x + s, a.y + s, a.z + s}; }
static inline float3 float3_sub (float3 a, float3 b) { return (float3){a.x - b.x, a.y - b.y, a.z - b.z}; }
static inline float3 float3_sub_s(float3 a, float s) { return (float3){a.x - s, a.y - s, a.z - s}; }
static inline float3 float3_mul (float3 a, float3 b) { return (float3){a.x * b.x, a.y * b.y, a.z * b.z}; }
static inline float3 float3_mul_s(float3 a, float s) { return (float3){a.x * s, a.y * s, a.z * s}; }
static inline float3 float3_div (float3 a, float3 b) { return (float3){a.x / b.x, a.y / b.y, a.z / b.z}; }
static inline float3 float3_div_s(float3 a, float s) { return (float3){a.x / s, a.y / s, a.z / s}; }
static inline float float3_dot (float3 a, float3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
static inline float float3_mag2 (float3 v) { return float3_dot(v, v); }
static inline float float3_dist2(float3 a, float3 b) { return float3_mag2(float3_sub(a, b)); }
static inline float3 float3_cross(float3 a, float3 b) {
return (float3){
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
};
}
static inline float3 float3_frac(float3 v) {
return (float3){v.x - floorf(v.x), v.y - floorf(v.y), v.z - floorf(v.z)};
}
static inline float3 float3_floor(float3 v) {
return (float3){floorf(v.x), floorf(v.y), floorf(v.z)};
}
static inline float3 float3_ceil(float3 v) {
return (float3){ceilf(v.x), ceilf(v.y), ceilf(v.z)};
}
static inline float3 float3_abs(float3 v) {
return (float3){fabsf(v.x), fabsf(v.y), fabsf(v.z)};
}
static inline float3 float3_min(float3 a, float3 b) {
return (float3){fminf(a.x, b.x), fminf(a.y, b.y), fminf(a.z, b.z)};
}
static inline float3 float3_max(float3 a, float3 b) {
return (float3){fmaxf(a.x, b.x), fmaxf(a.y, b.y), fmaxf(a.z, b.z)};
}
static inline float float3_mag(float3 v) {
return sqrtf(float3_mag2(v));
}
static inline float float3_dist(float3 a, float3 b) {
return sqrtf(float3_dist2(a, b));
}
static inline float3 float3_norm(float3 v) {
float mag = float3_mag(v);
if (mag == 0.0f) {
return (float3){0.0f, 0.0f, 0.0f};
}
return float3_div_s(v, mag);
}
// ============================================================================
// Float3s operations (same as float3 in fallback - no SIMD benefit)
// ============================================================================
static inline float3s float3s_add (float3s a, float3s b) { return (float3s){a.x + b.x, a.y + b.y, a.z + b.z}; }
static inline float3s float3s_add_s(float3s a, float s) { return (float3s){a.x + s, a.y + s, a.z + s}; }
static inline float3s float3s_sub (float3s a, float3s b) { return (float3s){a.x - b.x, a.y - b.y, a.z - b.z}; }
static inline float3s float3s_sub_s(float3s a, float s) { return (float3s){a.x - s, a.y - s, a.z - s}; }
static inline float3s float3s_mul (float3s a, float3s b) { return (float3s){a.x * b.x, a.y * b.y, a.z * b.z}; }
static inline float3s float3s_mul_s(float3s a, float s) { return (float3s){a.x * s, a.y * s, a.z * s}; }
static inline float3s float3s_div (float3s a, float3s b) { return (float3s){a.x / b.x, a.y / b.y, a.z / b.z}; }
static inline float3s float3s_div_s(float3s a, float s) { return (float3s){a.x / s, a.y / s, a.z / s}; }
static inline float float3s_dot (float3s a, float3s b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
static inline float float3s_mag2 (float3s v) { return float3s_dot(v, v); }
static inline float float3s_dist2(float3s a, float3s b) { return float3s_mag2(float3s_sub(a, b)); }
static inline float3s float3s_cross(float3s a, float3s b) {
return (float3s){
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
};
}
static inline float3s float3s_frac(float3s v) {
return (float3s){v.x - floorf(v.x), v.y - floorf(v.y), v.z - floorf(v.z)};
}
static inline float3s float3s_floor(float3s v) {
return (float3s){floorf(v.x), floorf(v.y), floorf(v.z)};
}
static inline float3s float3s_ceil(float3s v) {
return (float3s){ceilf(v.x), ceilf(v.y), ceilf(v.z)};
}
static inline float3s float3s_abs(float3s v) {
return (float3s){fabsf(v.x), fabsf(v.y), fabsf(v.z)};
}
static inline float3s float3s_min(float3s a, float3s b) {
return (float3s){fminf(a.x, b.x), fminf(a.y, b.y), fminf(a.z, b.z)};
}
static inline float3s float3s_max(float3s a, float3s b) {
return (float3s){fmaxf(a.x, b.x), fmaxf(a.y, b.y), fmaxf(a.z, b.z)};
}
static inline float float3s_mag(float3s v) {
return sqrtf(float3s_mag2(v));
}
static inline float float3s_dist(float3s a, float3s b) {
return sqrtf(float3s_dist2(a, b));
}
static inline float3s float3s_norm(float3s v) {
float mag = float3s_mag(v);
if (mag == 0.0f) {
return (float3s){0.0f, 0.0f, 0.0f};
}
return float3s_div_s(v, mag);
}
// ============================================================================
// Float4 operations
// ============================================================================
static inline float4 float4_add (float4 a, float4 b) { return (float4){a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; }
static inline float4 float4_add_s(float4 a, float s) { return (float4){a.x + s, a.y + s, a.z + s, a.w + s}; }
static inline float4 float4_sub (float4 a, float4 b) { return (float4){a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; }
static inline float4 float4_sub_s(float4 a, float s) { return (float4){a.x - s, a.y - s, a.z - s, a.w - s}; }
static inline float4 float4_mul (float4 a, float4 b) { return (float4){a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w}; }
static inline float4 float4_mul_s(float4 a, float s) { return (float4){a.x * s, a.y * s, a.z * s, a.w * s}; }
static inline float4 float4_div (float4 a, float4 b) { return (float4){a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w}; }
static inline float4 float4_div_s(float4 a, float s) { return (float4){a.x / s, a.y / s, a.z / s, a.w / s}; }
static inline float float4_dot (float4 a, float4 b) { return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; }
static inline float float4_mag2 (float4 v) { return float4_dot(v, v); }
static inline float float4_dist2(float4 a, float4 b) { return float4_mag2(float4_sub(a, b)); }
static inline float4 float4_frac(float4 v) {
return (float4){v.x - floorf(v.x), v.y - floorf(v.y), v.z - floorf(v.z), v.w - floorf(v.w)};
}
static inline float4 float4_floor(float4 v) {
return (float4){floorf(v.x), floorf(v.y), floorf(v.z), floorf(v.w)};
}
static inline float4 float4_ceil(float4 v) {
return (float4){ceilf(v.x), ceilf(v.y), ceilf(v.z), ceilf(v.w)};
}
static inline float4 float4_abs(float4 v) {
return (float4){fabsf(v.x), fabsf(v.y), fabsf(v.z), fabsf(v.w)};
}
static inline float4 float4_min(float4 a, float4 b) {
return (float4){fminf(a.x, b.x), fminf(a.y, b.y), fminf(a.z, b.z), fminf(a.w, b.w)};
}
static inline float4 float4_max(float4 a, float4 b) {
return (float4){fmaxf(a.x, b.x), fmaxf(a.y, b.y), fmaxf(a.z, b.z), fmaxf(a.w, b.w)};
}
static inline float float4_mag(float4 v) {
return sqrtf(float4_mag2(v));
}
static inline float float4_dist(float4 a, float4 b) {
return sqrtf(float4_dist2(a, b));
}
static inline float4 float4_norm(float4 v) {
float mag = float4_mag(v);
if (mag == 0.0f) {
return (float4){0.0f, 0.0f, 0.0f, 0.0f};
}
return float4_div_s(v, mag);
}
// ============================================================================
// Quaternion operations (using float4)
// ============================================================================
// Conjugate (inverse for unit quaternions)
static inline float4 float4_quat_conjugate(float4 q) {
return (float4){-q.x, -q.y, -q.z, q.w};
}
static inline float4 float4_quat_from_euler(float3 euler_xyz) {
// Half angles
float cx = cosf(euler_xyz.x * 0.5f);
float sx = sinf(euler_xyz.x * 0.5f);
float cy = cosf(euler_xyz.y * 0.5f);
float sy = sinf(euler_xyz.y * 0.5f);
float cz = cosf(euler_xyz.z * 0.5f);
float sz = sinf(euler_xyz.z * 0.5f);
// XYZ order
return (float4){
sx * cy * cz - cx * sy * sz,
cx * sy * cz + sx * cy * sz,
cx * cy * sz - sx * sy * cz,
cx * cy * cz + sx * sy * sz
};
}
static inline float4 float4_quat_from_axis_angle(float3 axis, float angle) {
float half_angle = angle * 0.5f;
float s = sinf(half_angle);
return (float4){
axis.x * s,
axis.y * s,
axis.z * s,
cosf(half_angle)
};
}
static inline float4 float4_quat_mul(float4 a, float4 b) {
return (float4){
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
};
}
static inline float3 float4_quat_rotate(float4 q, float3 v) {
// v' = q * v * q^-1
float3 qv = (float3){q.x, q.y, q.z};
float3 uv = float3_cross(qv, v);
float3 uuv = float3_cross(qv, uv);
uv = float3_mul_s(uv, 2.0f * q.w);
uuv = float3_mul_s(uuv, 2.0f);
return float3_add(float3_add(v, uv), uuv);
}
// ============================================================================
// Matrix operations
// ============================================================================
static inline float4x4 float4x4_identity(void) {
return (float4x4){{
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
}};
}
static inline float4x4 float4x4_mul(float4x4 a, float4x4 b) {
float4x4 result;
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
float sum = 0.0f;
for (int k = 0; k < 4; k++) {
sum += a.m[row * 4 + k] * b.m[k * 4 + col];
}
result.m[row * 4 + col] = sum;
}
}
return result;
}
static inline float4x4 float4x4_transpose(float4x4 m) {
return (float4x4){{
m.m[0], m.m[4], m.m[8], m.m[12],
m.m[1], m.m[5], m.m[9], m.m[13],
m.m[2], m.m[6], m.m[10], m.m[14],
m.m[3], m.m[7], m.m[11], m.m[15]
}};
}
static inline float3 float4x4_transform_pt(float4x4 m, float3 pt) {
float x = m.m[0] * pt.x + m.m[1] * pt.y + m.m[2] * pt.z + m.m[3];
float y = m.m[4] * pt.x + m.m[5] * pt.y + m.m[6] * pt.z + m.m[7];
float z = m.m[8] * pt.x + m.m[9] * pt.y + m.m[10] * pt.z + m.m[11];
return (float3){x, y, z};
}
static inline float3 float4x4_transform_dir(float4x4 m, float3 dir) {
float x = m.m[0] * dir.x + m.m[1] * dir.y + m.m[2] * dir.z;
float y = m.m[4] * dir.x + m.m[5] * dir.y + m.m[6] * dir.z;
float z = m.m[8] * dir.x + m.m[9] * dir.y + m.m[10] * dir.z;
return (float3){x, y, z};
}
static inline float4 float4x4_transform_float4(float4x4 m, float4 v) {
float x = m.m[0] * v.x + m.m[1] * v.y + m.m[2] * v.z + m.m[3] * v.w;
float y = m.m[4] * v.x + m.m[5] * v.y + m.m[6] * v.z + m.m[7] * v.w;
float z = m.m[8] * v.x + m.m[9] * v.y + m.m[10] * v.z + m.m[11] * v.w;
float w = m.m[12] * v.x + m.m[13] * v.y + m.m[14] * v.z + m.m[15] * v.w;
return (float4){x, y, z, w};
}
// Fast transforms using pre-transposed matrix
// With transposed layout, original columns are now at indices [0,1,2,3], [4,5,6,7], etc.
static inline float3 float4x4_transform_fast_pt(float4x4 mt, float3 pt) {
// mt is transposed: mt.m[0..3] = original col0, mt.m[4..7] = original col1, etc.
float x = mt.m[0] * pt.x + mt.m[4] * pt.y + mt.m[8] * pt.z + mt.m[12];
float y = mt.m[1] * pt.x + mt.m[5] * pt.y + mt.m[9] * pt.z + mt.m[13];
float z = mt.m[2] * pt.x + mt.m[6] * pt.y + mt.m[10] * pt.z + mt.m[14];
return (float3){x, y, z};
}
static inline float3 float4x4_transform_fast_dir(float4x4 mt, float3 dir) {
float x = mt.m[0] * dir.x + mt.m[4] * dir.y + mt.m[8] * dir.z;
float y = mt.m[1] * dir.x + mt.m[5] * dir.y + mt.m[9] * dir.z;
float z = mt.m[2] * dir.x + mt.m[6] * dir.y + mt.m[10] * dir.z;
return (float3){x, y, z};
}
static inline float4 float4x4_transform_fast_float4(float4x4 mt, float4 v) {
float4 result;
result.x = mt.m[0] * v.x + mt.m[4] * v.y + mt.m[8] * v.z + mt.m[12] * v.w;
result.y = mt.m[1] * v.x + mt.m[5] * v.y + mt.m[9] * v.z + mt.m[13] * v.w;
result.z = mt.m[2] * v.x + mt.m[6] * v.y + mt.m[10] * v.z + mt.m[14] * v.w;
result.w = mt.m[3] * v.x + mt.m[7] * v.y + mt.m[11] * v.z + mt.m[15] * v.w;
return result;
}
// Float3s versions - same as float3 in fallback (no SIMD benefit)
static inline float3s float4x4_transform_fast_pt3s(float4x4 mt, float3s pt) {
float3s result;
result.x = mt.m[0] * pt.x + mt.m[4] * pt.y + mt.m[8] * pt.z + mt.m[12];
result.y = mt.m[1] * pt.x + mt.m[5] * pt.y + mt.m[9] * pt.z + mt.m[13];
result.z = mt.m[2] * pt.x + mt.m[6] * pt.y + mt.m[10] * pt.z + mt.m[14];
return result;
}
static inline float3s float4x4_transform_fast_dir3s(float4x4 mt, float3s dir) {
float3s result;
result.x = mt.m[0] * dir.x + mt.m[4] * dir.y + mt.m[8] * dir.z;
result.y = mt.m[1] * dir.x + mt.m[5] * dir.y + mt.m[9] * dir.z;
result.z = mt.m[2] * dir.x + mt.m[6] * dir.y + mt.m[10] * dir.z;
return result;
}
static inline float4x4 float4x4_t(float3 translation) {
return (float4x4){{
1.0f, 0.0f, 0.0f, translation.x,
0.0f, 1.0f, 0.0f, translation.y,
0.0f, 0.0f, 1.0f, translation.z,
0.0f, 0.0f, 0.0f, 1.0f
}};
}
static inline float4x4 float4x4_r(float4 quat) {
// Normalize quaternion
float4 q = float4_norm(quat);
float xx = q.x * q.x;
float yy = q.y * q.y;
float zz = q.z * q.z;
float xy = q.x * q.y;
float xz = q.x * q.z;
float yz = q.y * q.z;
float wx = q.w * q.x;
float wy = q.w * q.y;
float wz = q.w * q.z;
return (float4x4){{
1.0f - 2.0f * (yy + zz), 2.0f * (xy - wz), 2.0f * (xz + wy), 0.0f,
2.0f * (xy + wz), 1.0f - 2.0f * (xx + zz), 2.0f * (yz - wx), 0.0f,
2.0f * (xz - wy), 2.0f * (yz + wx), 1.0f - 2.0f * (xx + yy), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
}};
}
static inline float4x4 float4x4_s(float3 scale) {
return (float4x4){{
scale.x, 0.0f, 0.0f, 0.0f,
0.0f, scale.y, 0.0f, 0.0f,
0.0f, 0.0f, scale.z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
}};
}
static inline float4x4 float4x4_trs(float3 translation, float4 rotation_quat, float3 scale) {
// Normalize quaternion
float4 q = float4_norm(rotation_quat);
float xx = q.x * q.x;
float yy = q.y * q.y;
float zz = q.z * q.z;
float xy = q.x * q.y;
float xz = q.x * q.z;
float yz = q.y * q.z;
float wx = q.w * q.x;
float wy = q.w * q.y;
float wz = q.w * q.z;
// Combined TRS matrix (row-major)
return (float4x4){{
scale.x * (1.0f - 2.0f * (yy + zz)), scale.x * (2.0f * (xy - wz)), scale.x * (2.0f * (xz + wy)), translation.x,
scale.y * (2.0f * (xy + wz)), scale.y * (1.0f - 2.0f * (xx + zz)), scale.y * (2.0f * (yz - wx)), translation.y,
scale.z * (2.0f * (xz - wy)), scale.z * (2.0f * (yz + wx)), scale.z * (1.0f - 2.0f * (xx + yy)), translation.z,
0.0f, 0.0f, 0.0f, 1.0f
}};
}
static inline float4x4 float4x4_lookat(float3 eye, float3 target, float3 up) {
// Right-handed coordinate system
float3 forward = float3_norm(float3_sub(target, eye));
float3 right = float3_norm(float3_cross(forward, up));
float3 actual_up = float3_cross(right, forward);
// Row-major view matrix
return (float4x4){{
right.x, right.y, right.z, -float3_dot(right, eye),
actual_up.x, actual_up.y, actual_up.z, -float3_dot(actual_up, eye),
-forward.x, -forward.y, -forward.z, float3_dot(forward, eye),
0.0f, 0.0f, 0.0f, 1.0f
}};
}
static inline float4x4 float4x4_perspective(float fov_y, float aspect, float near_plane, float far_plane) {
// Right-handed, zero-to-one depth
float tan_half_fov = tanf(fov_y * 0.5f);
// Row-major projection matrix
return (float4x4){{
1.0f / (aspect * tan_half_fov), 0.0f, 0.0f, 0.0f,
0.0f, 1.0f / tan_half_fov, 0.0f, 0.0f,
0.0f, 0.0f, far_plane / (near_plane - far_plane), -(far_plane * near_plane) / (far_plane - near_plane),
0.0f, 0.0f, -1.0f, 0.0f
}};
}
static inline float4x4 float4x4_orthographic(float left, float right, float bottom, float top, float near_plane, float far_plane) {
// Right-handed, zero-to-one depth
// Row-major orthographic matrix
return (float4x4){{
2.0f / (right - left), 0.0f, 0.0f, -(right + left) / (right - left),
0.0f, 2.0f / (top - bottom), 0.0f, -(top + bottom) / (top - bottom),
0.0f, 0.0f, -1.0f / (far_plane - near_plane), -near_plane / (far_plane - near_plane),
0.0f, 0.0f, 0.0f, 1.0f
}};
}
// ============================================================================
// Larger functions (kept at bottom for readability)
// ============================================================================
static inline float4x4 float4x4_invert(float4x4 m) {
float inv[16];
float det;
// Compute cofactors (standard matrix inversion formula)
inv[0] = m.m[5] * m.m[10] * m.m[15] -
m.m[5] * m.m[11] * m.m[14] -
m.m[9] * m.m[6] * m.m[15] +
m.m[9] * m.m[7] * m.m[14] +
m.m[13] * m.m[6] * m.m[11] -
m.m[13] * m.m[7] * m.m[10];
inv[4] = -m.m[4] * m.m[10] * m.m[15] +
m.m[4] * m.m[11] * m.m[14] +
m.m[8] * m.m[6] * m.m[15] -
m.m[8] * m.m[7] * m.m[14] -
m.m[12] * m.m[6] * m.m[11] +
m.m[12] * m.m[7] * m.m[10];
inv[8] = m.m[4] * m.m[9] * m.m[15] -
m.m[4] * m.m[11] * m.m[13] -
m.m[8] * m.m[5] * m.m[15] +
m.m[8] * m.m[7] * m.m[13] +
m.m[12] * m.m[5] * m.m[11] -
m.m[12] * m.m[7] * m.m[9];
inv[12] = -m.m[4] * m.m[9] * m.m[14] +
m.m[4] * m.m[10] * m.m[13] +
m.m[8] * m.m[5] * m.m[14] -
m.m[8] * m.m[6] * m.m[13] -
m.m[12] * m.m[5] * m.m[10] +
m.m[12] * m.m[6] * m.m[9];
inv[1] = -m.m[1] * m.m[10] * m.m[15] +
m.m[1] * m.m[11] * m.m[14] +
m.m[9] * m.m[2] * m.m[15] -
m.m[9] * m.m[3] * m.m[14] -
m.m[13] * m.m[2] * m.m[11] +
m.m[13] * m.m[3] * m.m[10];
inv[5] = m.m[0] * m.m[10] * m.m[15] -
m.m[0] * m.m[11] * m.m[14] -
m.m[8] * m.m[2] * m.m[15] +
m.m[8] * m.m[3] * m.m[14] +
m.m[12] * m.m[2] * m.m[11] -
m.m[12] * m.m[3] * m.m[10];
inv[9] = -m.m[0] * m.m[9] * m.m[15] +
m.m[0] * m.m[11] * m.m[13] +
m.m[8] * m.m[1] * m.m[15] -
m.m[8] * m.m[3] * m.m[13] -
m.m[12] * m.m[1] * m.m[11] +
m.m[12] * m.m[3] * m.m[9];
inv[13] = m.m[0] * m.m[9] * m.m[14] -
m.m[0] * m.m[10] * m.m[13] -
m.m[8] * m.m[1] * m.m[14] +
m.m[8] * m.m[2] * m.m[13] +
m.m[12] * m.m[1] * m.m[10] -
m.m[12] * m.m[2] * m.m[9];
inv[2] = m.m[1] * m.m[6] * m.m[15] -
m.m[1] * m.m[7] * m.m[14] -
m.m[5] * m.m[2] * m.m[15] +
m.m[5] * m.m[3] * m.m[14] +
m.m[13] * m.m[2] * m.m[7] -
m.m[13] * m.m[3] * m.m[6];
inv[6] = -m.m[0] * m.m[6] * m.m[15] +
m.m[0] * m.m[7] * m.m[14] +
m.m[4] * m.m[2] * m.m[15] -
m.m[4] * m.m[3] * m.m[14] -
m.m[12] * m.m[2] * m.m[7] +
m.m[12] * m.m[3] * m.m[6];
inv[10] = m.m[0] * m.m[5] * m.m[15] -
m.m[0] * m.m[7] * m.m[13] -
m.m[4] * m.m[1] * m.m[15] +
m.m[4] * m.m[3] * m.m[13] +
m.m[12] * m.m[1] * m.m[7] -
m.m[12] * m.m[3] * m.m[5];
inv[14] = -m.m[0] * m.m[5] * m.m[14] +
m.m[0] * m.m[6] * m.m[13] +
m.m[4] * m.m[1] * m.m[14] -
m.m[4] * m.m[2] * m.m[13] -
m.m[12] * m.m[1] * m.m[6] +
m.m[12] * m.m[2] * m.m[5];
inv[3] = -m.m[1] * m.m[6] * m.m[11] +
m.m[1] * m.m[7] * m.m[10] +
m.m[5] * m.m[2] * m.m[11] -
m.m[5] * m.m[3] * m.m[10] -
m.m[9] * m.m[2] * m.m[7] +
m.m[9] * m.m[3] * m.m[6];
inv[7] = m.m[0] * m.m[6] * m.m[11] -
m.m[0] * m.m[7] * m.m[10] -
m.m[4] * m.m[2] * m.m[11] +
m.m[4] * m.m[3] * m.m[10] +
m.m[8] * m.m[2] * m.m[7] -
m.m[8] * m.m[3] * m.m[6];
inv[11] = -m.m[0] * m.m[5] * m.m[11] +
m.m[0] * m.m[7] * m.m[9] +
m.m[4] * m.m[1] * m.m[11] -
m.m[4] * m.m[3] * m.m[9] -
m.m[8] * m.m[1] * m.m[7] +
m.m[8] * m.m[3] * m.m[5];
inv[15] = m.m[0] * m.m[5] * m.m[10] -
m.m[0] * m.m[6] * m.m[9] -
m.m[4] * m.m[1] * m.m[10] +
m.m[4] * m.m[2] * m.m[9] +
m.m[8] * m.m[1] * m.m[6] -
m.m[8] * m.m[2] * m.m[5];
det = m.m[0] * inv[0] + m.m[1] * inv[4] + m.m[2] * inv[8] + m.m[3] * inv[12];
if (det == 0.0f) {
return float4x4_identity();
}
det = 1.0f / det;
float4x4 result;
for (int i = 0; i < 16; i++) {
result.m[i] = inv[i] * det;
}
return result;
}
#ifdef __cplusplus
}
#endif
#endif // FLOAT_MATH_USE_FALLBACK
// ============================================================================
// SSE implementation
// ============================================================================
#if defined(FLOAT_MATH_USE_SSE)
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <xmmintrin.h> // SSE
#include <emmintrin.h> // SSE2
#include <smmintrin.h> // SSE4.1
#if defined(__AVX2__) || defined(__FMA__)
#include <immintrin.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_MSC_VER)
#define FM_ALIGN_PREFIX(x) __declspec(align(x))
#define FM_ALIGN_SUFFIX(x)
#else
#define FM_ALIGN_PREFIX(x)
#define FM_ALIGN_SUFFIX(x) __attribute__((aligned(x)))
#endif
// ============================================================================
// Vector types (SSE optimized)
// ============================================================================
typedef struct { float x, y; } float2;
// float3: 12 bytes, matches user expectation for size
typedef struct { float x, y, z; } float3;
// float3s: 16 bytes, SIMD-aligned for SSE operations
typedef FM_ALIGN_PREFIX(16) union {
struct { float x, y, z, _pad; };
float v[4];
__m128 simd;
} FM_ALIGN_SUFFIX(16) float3s;
// float4: 16 bytes, SIMD-aligned
typedef FM_ALIGN_PREFIX(16) union {
struct { float x, y, z, w; };
struct { float r, g, b, a; };
float v[4];
__m128 simd;
} FM_ALIGN_SUFFIX(16) float4;
// Matrix type (row-major for direct shader compatibility)
typedef FM_ALIGN_PREFIX(16) union {
float m[16]; // Row-major: [m00, m01, m02, m03, m10, m11, ...]
__m128 rows[4];
} FM_ALIGN_SUFFIX(16) float4x4;
// ============================================================================
// Conversions between float3 and float3s
// ============================================================================
static inline float3 float3s_to_float3(float3s v) {
return (float3){v.x, v.y, v.z};
}
static inline float3s float3_to_float3s(float3 v) {
float3s result;
result.simd = _mm_set_ps(0.0f, v.z, v.y, v.x);
return result;
}
// ============================================================================
// SSE Helper functions
// ============================================================================
static inline float3s m128_to_float3s(__m128 v) {
float3s result;
result.simd = v;
return result;
}
static inline float4 m128_to_float4(__m128 v) {
float4 result;
result.simd = v;
return result;
}
// ============================================================================
// Float2 operations (scalar - SSE not beneficial for 2 floats)
// ============================================================================
static inline float2 float2_add (float2 a, float2 b) { return (float2){a.x + b.x, a.y + b.y}; }
static inline float2 float2_add_s(float2 a, float s) { return (float2){a.x + s, a.y + s}; }
static inline float2 float2_sub (float2 a, float2 b) { return (float2){a.x - b.x, a.y - b.y}; }
static inline float2 float2_sub_s(float2 a, float s) { return (float2){a.x - s, a.y - s}; }
static inline float2 float2_mul (float2 a, float2 b) { return (float2){a.x * b.x, a.y * b.y}; }
static inline float2 float2_mul_s(float2 a, float s) { return (float2){a.x * s, a.y * s}; }
static inline float2 float2_div (float2 a, float2 b) { return (float2){a.x / b.x, a.y / b.y}; }
static inline float2 float2_div_s(float2 a, float s) { return (float2){a.x / s, a.y / s}; }
static inline float float2_dot (float2 a, float2 b) { return a.x * b.x + a.y * b.y; }
static inline float float2_mag2 (float2 v) { return float2_dot(v, v); }
static inline float float2_dist2(float2 a, float2 b) { return float2_mag2(float2_sub(a, b)); }
static inline float2 float2_frac(float2 v) {
return (float2){v.x - floorf(v.x), v.y - floorf(v.y)};
}
static inline float2 float2_floor(float2 v) {
return (float2){floorf(v.x), floorf(v.y)};
}
static inline float2 float2_ceil(float2 v) {
return (float2){ceilf(v.x), ceilf(v.y)};
}
static inline float2 float2_abs(float2 v) {
return (float2){fabsf(v.x), fabsf(v.y)};
}
static inline float2 float2_min(float2 a, float2 b) {
return (float2){fminf(a.x, b.x), fminf(a.y, b.y)};
}
static inline float2 float2_max(float2 a, float2 b) {
return (float2){fmaxf(a.x, b.x), fmaxf(a.y, b.y)};
}
static inline float float2_mag(float2 v) {
return sqrtf(float2_mag2(v));
}
static inline float float2_dist(float2 a, float2 b) {
return sqrtf(float2_dist2(a, b));
}
static inline float2 float2_norm(float2 v) {
float mag = float2_mag(v);
if (mag == 0.0f) {
return (float2){0.0f, 0.0f};
}
return float2_div_s(v, mag);
}
// ============================================================================
// Float3 operations (scalar - 12 bytes, no padding)
// ============================================================================
static inline float3 float3_add (float3 a, float3 b) { return (float3){a.x + b.x, a.y + b.y, a.z + b.z}; }
static inline float3 float3_add_s(float3 a, float s) { return (float3){a.x + s, a.y + s, a.z + s}; }
static inline float3 float3_sub (float3 a, float3 b) { return (float3){a.x - b.x, a.y - b.y, a.z - b.z}; }
static inline float3 float3_sub_s(float3 a, float s) { return (float3){a.x - s, a.y - s, a.z - s}; }
static inline float3 float3_mul (float3 a, float3 b) { return (float3){a.x * b.x, a.y * b.y, a.z * b.z}; }
static inline float3 float3_mul_s(float3 a, float s) { return (float3){a.x * s, a.y * s, a.z * s}; }
static inline float3 float3_div (float3 a, float3 b) { return (float3){a.x / b.x, a.y / b.y, a.z / b.z}; }
static inline float3 float3_div_s(float3 a, float s) { return (float3){a.x / s, a.y / s, a.z / s}; }
static inline float float3_dot (float3 a, float3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
static inline float float3_mag2 (float3 v) { return float3_dot(v, v); }
static inline float float3_dist2(float3 a, float3 b) { return float3_mag2(float3_sub(a, b)); }
static inline float3 float3_cross(float3 a, float3 b) {
return (float3){
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
};
}
static inline float3 float3_frac(float3 v) {
return (float3){v.x - floorf(v.x), v.y - floorf(v.y), v.z - floorf(v.z)};
}
static inline float3 float3_floor(float3 v) {
return (float3){floorf(v.x), floorf(v.y), floorf(v.z)};
}
static inline float3 float3_ceil(float3 v) {
return (float3){ceilf(v.x), ceilf(v.y), ceilf(v.z)};
}
static inline float3 float3_abs(float3 v) {
return (float3){fabsf(v.x), fabsf(v.y), fabsf(v.z)};
}
static inline float3 float3_min(float3 a, float3 b) {
return (float3){fminf(a.x, b.x), fminf(a.y, b.y), fminf(a.z, b.z)};
}
static inline float3 float3_max(float3 a, float3 b) {
return (float3){fmaxf(a.x, b.x), fmaxf(a.y, b.y), fmaxf(a.z, b.z)};
}
static inline float float3_mag(float3 v) {
return sqrtf(float3_mag2(v));
}
static inline float float3_dist(float3 a, float3 b) {
return sqrtf(float3_dist2(a, b));
}
static inline float3 float3_norm(float3 v) {
float mag = float3_mag(v);
if (mag == 0.0f) {
return (float3){0.0f, 0.0f, 0.0f};
}
return float3_div_s(v, mag);
}
// ============================================================================
// Float3s operations (SSE accelerated - 16 bytes, SIMD aligned)
// ============================================================================
static inline float3s float3s_add(float3s a, float3s b) {
return m128_to_float3s(_mm_add_ps(a.simd, b.simd));
}
static inline float3s float3s_add_s(float3s a, float s) {
return m128_to_float3s(_mm_add_ps(a.simd, _mm_set1_ps(s)));
}
static inline float3s float3s_sub(float3s a, float3s b) {
return m128_to_float3s(_mm_sub_ps(a.simd, b.simd));
}
static inline float3s float3s_sub_s(float3s a, float s) {
return m128_to_float3s(_mm_sub_ps(a.simd, _mm_set1_ps(s)));
}
static inline float3s float3s_mul(float3s a, float3s b) {
return m128_to_float3s(_mm_mul_ps(a.simd, b.simd));
}
static inline float3s float3s_mul_s(float3s a, float s) {
return m128_to_float3s(_mm_mul_ps(a.simd, _mm_set1_ps(s)));
}
static inline float3s float3s_div(float3s a, float3s b) {
return m128_to_float3s(_mm_div_ps(a.simd, b.simd));
}
static inline float3s float3s_div_s(float3s a, float s) {
return m128_to_float3s(_mm_div_ps(a.simd, _mm_set1_ps(s)));
}
static inline float float3s_dot(float3s a, float3s b) {
// SSE4.1 dot product: 0x71 = multiply xyz, store in x
return _mm_cvtss_f32(_mm_dp_ps(a.simd, b.simd, 0x71));
}
static inline float float3s_mag2(float3s v) {
return float3s_dot(v, v);
}
static inline float float3s_dist2(float3s a, float3s b) {
return float3s_mag2(float3s_sub(a, b));
}
static inline float3s float3s_cross(float3s a, float3s b) {
// Cross product using shuffles: a.yzx * b.zxy - a.zxy * b.yzx
__m128 a_yzx = _mm_shuffle_ps(a.simd, a.simd, _MM_SHUFFLE(3, 0, 2, 1));
__m128 b_zxy = _mm_shuffle_ps(b.simd, b.simd, _MM_SHUFFLE(3, 1, 0, 2));
__m128 a_zxy = _mm_shuffle_ps(a.simd, a.simd, _MM_SHUFFLE(3, 1, 0, 2));
__m128 b_yzx = _mm_shuffle_ps(b.simd, b.simd, _MM_SHUFFLE(3, 0, 2, 1));
return m128_to_float3s(_mm_sub_ps(_mm_mul_ps(a_yzx, b_zxy), _mm_mul_ps(a_zxy, b_yzx)));
}
static inline float3s float3s_frac(float3s v) {
__m128 floor_v = _mm_floor_ps(v.simd);
return m128_to_float3s(_mm_sub_ps(v.simd, floor_v));
}
static inline float3s float3s_floor(float3s v) {
return m128_to_float3s(_mm_floor_ps(v.simd));
}
static inline float3s float3s_ceil(float3s v) {
return m128_to_float3s(_mm_ceil_ps(v.simd));
}
static inline float3s float3s_abs(float3s v) {
__m128 sign_mask = _mm_castsi128_ps(_mm_set1_epi32(0x7FFFFFFF));
return m128_to_float3s(_mm_and_ps(v.simd, sign_mask));
}
static inline float3s float3s_min(float3s a, float3s b) {
return m128_to_float3s(_mm_min_ps(a.simd, b.simd));
}
static inline float3s float3s_max(float3s a, float3s b) {
return m128_to_float3s(_mm_max_ps(a.simd, b.simd));
}
static inline float float3s_mag(float3s v) {