-
Notifications
You must be signed in to change notification settings - Fork 47
/
gb_math.h
2234 lines (1732 loc) · 66.3 KB
/
gb_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
/* gb_math.h - v0.07c - public domain C math library - no warranty implied; use at your own risk
A C math library geared towards game development
use '#define GB_MATH_IMPLEMENTATION' before including to create the implementation in _ONE_ file
Version History:
0.07f - Fix constants
0.07e - Fixed a warning
0.07d - Fix mat4_inverse
0.07c - Add gb_random01
0.07b - Fix mat4_inverse
0.07a - Fix Mat2
0.07 - Better Mat4 procedures
0.06h - Ignore silly warnings
0.06g - Remove memzero
0.06f - Remove warning on MSVC
0.06e - Change brace style and fix some warnings
0.06d - Bug fix
0.06c - Remove extra needed define for C++ and inline all operators
0.06b - Just formatting
0.06a - Implement rough versions of mod, remainder, copy_sign
0.06 - Windows GCC Support and C90-ish Support
0.05 - Less/no dependencies or CRT
0.04d - License Update
0.04c - Use 64-bit murmur64 version on WIN64
0.04b - Fix strict aliasing in gb_quake_rsqrt
0.04a - Minor bug fixes
0.04 - Namespace everything with gb
0.03 - Complete Replacement
0.01 - Initial Version
LICENSE
This software is dual-licensed to the public domain and under the following
license: you are granted a perpetual, irrevocable license to copy, modify,
publish, and distribute this file as you see fit.
WARNING
- This library is _slightly_ experimental and features may not work as expected.
- This also means that many functions are not documented.
CONTENTS
- Common Macros
- Types
- gbVec(2,3,4)
- gbMat(2,3,4)
- gbFloat(2,3,4)
- gbQuat
- gbRect(2,3)
- gbAabb(2,3)
- gbHalf (16-bit floating point) (storage only)
- Operations
- Functions
- Type Functions
- Random
- Hash
*/
#ifndef GB_MATH_INCLUDE_GB_MATH_H
#define GB_MATH_INCLUDE_GB_MATH_H
#include <stddef.h>
#if !defined(GB_MATH_NO_MATH_H)
#include <math.h>
#else
#include <intrin.h>
#endif
#ifndef GB_MATH_DEF
#ifdef GB_MATH_STATIC
#define GB_MATH_DEF static
#else
#define GB_MATH_DEF extern
#endif
#endif
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4201)
#endif
typedef union gbVec2 {
struct { float x, y; };
float e[2];
} gbVec2;
typedef union gbVec3 {
struct { float x, y, z; };
struct { float r, g, b; };
gbVec2 xy;
float e[3];
} gbVec3;
typedef union gbVec4 {
struct { float x, y, z, w; };
struct { float r, g, b, a; };
struct { gbVec2 xy, zw; };
gbVec3 xyz;
gbVec3 rgb;
float e[4];
} gbVec4;
typedef union gbMat2 {
struct { gbVec2 x, y; };
gbVec2 col[2];
float e[4];
} gbMat2;
typedef union gbMat3 {
struct { gbVec3 x, y, z; };
gbVec3 col[3];
float e[9];
} gbMat3;
typedef union gbMat4 {
struct { gbVec4 x, y, z, w; };
gbVec4 col[4];
float e[16];
} gbMat4;
typedef union gbQuat {
struct { float x, y, z, w; };
gbVec4 xyzw;
gbVec3 xyz;
float e[4];
} gbQuat;
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
typedef float gbFloat2[2];
typedef float gbFloat3[3];
typedef float gbFloat4[4];
typedef struct gbRect2 { gbVec2 pos, dim; } gbRect2;
typedef struct gbRect3 { gbVec3 pos, dim; } gbRect3;
typedef struct gbAabb2 { gbVec2 centre, half_size; } gbAabb2;
typedef struct gbAabb3 { gbVec3 centre, half_size; } gbAabb3;
#if defined(_MSC_VER)
typedef unsigned __int32 gb_math_u32;
typedef unsigned __int64 gb_math_u64;
#else
#if defined(GB_USE_STDINT)
#include <stdint.h>
typedef uint32_t gb_math_u32;
typedef uint64_t gb_math_u64;
#else
typedef unsigned int gb_math_u32;
typedef unsigned long long gb_math_u64;
#endif
#endif
typedef short gbHalf;
#ifndef GB_MATH_CONSTANTS
#define GB_MATH_CONSTANTS
#define GB_MATH_EPSILON 1.19209290e-7f
#define GB_MATH_ZERO 0.0f
#define GB_MATH_ONE 1.0f
#define GB_MATH_TWO_THIRDS 0.666666666666666666666666666666666666667f
#define GB_MATH_TAU 6.28318530717958647692528676655900576f
#define GB_MATH_PI 3.14159265358979323846264338327950288f
#define GB_MATH_ONE_OVER_TAU 0.159154943091895335768883763372514362f
#define GB_MATH_ONE_OVER_PI 0.318309886183790671537767526745028724f
#define GB_MATH_TAU_OVER_2 3.14159265358979323846264338327950288f
#define GB_MATH_TAU_OVER_4 1.570796326794896619231321691639751442f
#define GB_MATH_TAU_OVER_8 0.785398163397448309615660845819875721f
#define GB_MATH_E 2.7182818284590452353602874713526625f
#define GB_MATH_SQRT_TWO 1.41421356237309504880168872420969808f
#define GB_MATH_SQRT_THREE 1.73205080756887729352744634150587236f
#define GB_MATH_SQRT_FIVE 2.23606797749978969640917366873127623f
#define GB_MATH_LOG_TWO 0.693147180559945309417232121458176568f
#define GB_MATH_LOG_TEN 2.30258509299404568401799145468436421f
#endif
#if defined(__cplusplus)
extern "C" {
#endif
#ifndef gb_clamp
#define gb_clamp(x, lower, upper) (gb_min(gb_max(x, (lower)), (upper)))
#endif
#ifndef gb_clamp01
#define gb_clamp01(x) gb_clamp(x, 0, 1)
#endif
#ifndef gb_square
#define gb_square(x) ((x)*(x))
#endif
#ifndef gb_cube
#define gb_cube(x) ((x)*(x)*(x))
#endif
#ifndef gb_abs
#define gb_abs(x) ((x) > 0 ? (x) : -(x))
#endif
#ifndef gb_sign
#define gb_sign(x) ((x) >= 0 ? 1 : -1)
#endif
GB_MATH_DEF float gb_to_radians(float degrees);
GB_MATH_DEF float gb_to_degrees(float radians);
/* NOTE(bill): Because to interpolate angles */
GB_MATH_DEF float gb_angle_diff(float radians_a, float radians_b);
#ifndef gb_min
#define gb_min(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef gb_max
#define gb_max(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef gb_min3
#define gb_min3(a, b, c) gb_min(gb_min(a, b), c)
#endif
#ifndef gb_max3
#define gb_max3(a, b, c) gb_max(gb_max(a, b), c)
#endif
GB_MATH_DEF float gb_copy_sign (float x, float y);
GB_MATH_DEF float gb_remainder (float x, float y);
GB_MATH_DEF float gb_mod (float x, float y);
GB_MATH_DEF float gb_sqrt (float a);
GB_MATH_DEF float gb_rsqrt (float a);
GB_MATH_DEF float gb_quake_rsqrt(float a); /* NOTE(bill): It's probably better to use 1.0f/gb_sqrt(a)
* And for simd, there is usually isqrt functions too!
*/
GB_MATH_DEF float gb_sin (float radians);
GB_MATH_DEF float gb_cos (float radians);
GB_MATH_DEF float gb_tan (float radians);
GB_MATH_DEF float gb_arcsin (float a);
GB_MATH_DEF float gb_arccos (float a);
GB_MATH_DEF float gb_arctan (float a);
GB_MATH_DEF float gb_arctan2(float y, float x);
GB_MATH_DEF float gb_exp (float x);
GB_MATH_DEF float gb_exp2 (float x);
GB_MATH_DEF float gb_log (float x);
GB_MATH_DEF float gb_log2 (float x);
GB_MATH_DEF float gb_fast_exp (float x); /* NOTE(bill): Only valid from -1 <= x <= +1 */
GB_MATH_DEF float gb_fast_exp2(float x); /* NOTE(bill): Only valid from -1 <= x <= +1 */
GB_MATH_DEF float gb_pow (float x, float y); /* x^y */
GB_MATH_DEF float gb_round(float x);
GB_MATH_DEF float gb_floor(float x);
GB_MATH_DEF float gb_ceil (float x);
GB_MATH_DEF float gb_half_to_float(gbHalf value);
GB_MATH_DEF gbHalf gb_float_to_half(float value);
GB_MATH_DEF gbVec2 gb_vec2_zero(void);
GB_MATH_DEF gbVec2 gb_vec2 (float x, float y);
GB_MATH_DEF gbVec2 gb_vec2v (float x[2]);
GB_MATH_DEF gbVec3 gb_vec3_zero(void);
GB_MATH_DEF gbVec3 gb_vec3 (float x, float y, float z);
GB_MATH_DEF gbVec3 gb_vec3v (float x[3]);
GB_MATH_DEF gbVec4 gb_vec4_zero(void);
GB_MATH_DEF gbVec4 gb_vec4 (float x, float y, float z, float w);
GB_MATH_DEF gbVec4 gb_vec4v (float x[4]);
GB_MATH_DEF void gb_vec2_add(gbVec2 *d, gbVec2 v0, gbVec2 v1);
GB_MATH_DEF void gb_vec2_sub(gbVec2 *d, gbVec2 v0, gbVec2 v1);
GB_MATH_DEF void gb_vec2_mul(gbVec2 *d, gbVec2 v, float s);
GB_MATH_DEF void gb_vec2_div(gbVec2 *d, gbVec2 v, float s);
GB_MATH_DEF void gb_vec3_add(gbVec3 *d, gbVec3 v0, gbVec3 v1);
GB_MATH_DEF void gb_vec3_sub(gbVec3 *d, gbVec3 v0, gbVec3 v1);
GB_MATH_DEF void gb_vec3_mul(gbVec3 *d, gbVec3 v, float s);
GB_MATH_DEF void gb_vec3_div(gbVec3 *d, gbVec3 v, float s);
GB_MATH_DEF void gb_vec4_add(gbVec4 *d, gbVec4 v0, gbVec4 v1);
GB_MATH_DEF void gb_vec4_sub(gbVec4 *d, gbVec4 v0, gbVec4 v1);
GB_MATH_DEF void gb_vec4_mul(gbVec4 *d, gbVec4 v, float s);
GB_MATH_DEF void gb_vec4_div(gbVec4 *d, gbVec4 v, float s);
GB_MATH_DEF void gb_vec2_addeq(gbVec2 *d, gbVec2 v);
GB_MATH_DEF void gb_vec2_subeq(gbVec2 *d, gbVec2 v);
GB_MATH_DEF void gb_vec2_muleq(gbVec2 *d, float s);
GB_MATH_DEF void gb_vec2_diveq(gbVec2 *d, float s);
GB_MATH_DEF void gb_vec3_addeq(gbVec3 *d, gbVec3 v);
GB_MATH_DEF void gb_vec3_subeq(gbVec3 *d, gbVec3 v);
GB_MATH_DEF void gb_vec3_muleq(gbVec3 *d, float s);
GB_MATH_DEF void gb_vec3_diveq(gbVec3 *d, float s);
GB_MATH_DEF void gb_vec4_addeq(gbVec4 *d, gbVec4 v);
GB_MATH_DEF void gb_vec4_subeq(gbVec4 *d, gbVec4 v);
GB_MATH_DEF void gb_vec4_muleq(gbVec4 *d, float s);
GB_MATH_DEF void gb_vec4_diveq(gbVec4 *d, float s);
GB_MATH_DEF float gb_vec2_dot(gbVec2 v0, gbVec2 v1);
GB_MATH_DEF float gb_vec3_dot(gbVec3 v0, gbVec3 v1);
GB_MATH_DEF float gb_vec4_dot(gbVec4 v0, gbVec4 v1);
GB_MATH_DEF void gb_vec2_cross(float *d, gbVec2 v0, gbVec2 v1);
GB_MATH_DEF void gb_vec3_cross(gbVec3 *d, gbVec3 v0, gbVec3 v1);
GB_MATH_DEF float gb_vec2_mag2(gbVec2 v);
GB_MATH_DEF float gb_vec3_mag2(gbVec3 v);
GB_MATH_DEF float gb_vec4_mag2(gbVec4 v);
GB_MATH_DEF float gb_vec2_mag(gbVec2 v);
GB_MATH_DEF float gb_vec3_mag(gbVec3 v);
GB_MATH_DEF float gb_vec4_mag(gbVec4 v);
GB_MATH_DEF void gb_vec2_norm(gbVec2 *d, gbVec2 v);
GB_MATH_DEF void gb_vec3_norm(gbVec3 *d, gbVec3 v);
GB_MATH_DEF void gb_vec4_norm(gbVec4 *d, gbVec4 v);
GB_MATH_DEF void gb_vec2_norm0(gbVec2 *d, gbVec2 v);
GB_MATH_DEF void gb_vec3_norm0(gbVec3 *d, gbVec3 v);
GB_MATH_DEF void gb_vec4_norm0(gbVec4 *d, gbVec4 v);
GB_MATH_DEF void gb_vec2_reflect(gbVec2 *d, gbVec2 i, gbVec2 n);
GB_MATH_DEF void gb_vec3_reflect(gbVec3 *d, gbVec3 i, gbVec3 n);
GB_MATH_DEF void gb_vec2_refract(gbVec2 *d, gbVec2 i, gbVec2 n, float eta);
GB_MATH_DEF void gb_vec3_refract(gbVec3 *d, gbVec3 i, gbVec3 n, float eta);
GB_MATH_DEF float gb_vec2_aspect_ratio(gbVec2 v);
GB_MATH_DEF void gb_mat2_identity (gbMat2 *m);
GB_MATH_DEF void gb_float22_identity(float m[2][2]);
GB_MATH_DEF void gb_mat2_transpose (gbMat2 *m);
GB_MATH_DEF void gb_mat2_mul (gbMat2 *out, gbMat2 *m1, gbMat2 *m2);
GB_MATH_DEF void gb_mat2_mul_vec2 (gbVec2 *out, gbMat2 *m, gbVec2 in);
GB_MATH_DEF void gb_mat2_inverse (gbMat2 *out, gbMat2 *in);
GB_MATH_DEF float gb_mat2_determinate(gbMat2 *m);
GB_MATH_DEF gbMat2 *gb_mat2_v(gbVec2 m[2]);
GB_MATH_DEF gbMat2 *gb_mat2_f(float m[2][2]);
GB_MATH_DEF gbFloat2 *gb_float22_m(gbMat2 *m);
GB_MATH_DEF gbFloat2 *gb_float22_v(gbVec2 m[2]);
GB_MATH_DEF gbFloat2 *gb_float22_4(float m[4]);
GB_MATH_DEF void gb_float22_transpose(float (*vec)[2]);
GB_MATH_DEF void gb_float22_mul (float (*out)[2], float (*mat1)[2], float (*mat2)[2]);
GB_MATH_DEF void gb_float22_mul_vec2 (gbVec2 *out, float m[2][2], gbVec2 in);
GB_MATH_DEF void gb_mat3_identity (gbMat3 *m);
GB_MATH_DEF void gb_float33_identity(float m[3][3]);
GB_MATH_DEF void gb_mat3_transpose (gbMat3 *m);
GB_MATH_DEF void gb_mat3_mul (gbMat3 *out, gbMat3 *m1, gbMat3 *m2);
GB_MATH_DEF void gb_mat3_mul_vec3 (gbVec3 *out, gbMat3 *m, gbVec3 in);
GB_MATH_DEF void gb_mat3_inverse (gbMat3 *out, gbMat3 *in);
GB_MATH_DEF float gb_mat3_determinate(gbMat3 *m);
GB_MATH_DEF gbMat3 *gb_mat3_v(gbVec3 m[3]);
GB_MATH_DEF gbMat3 *gb_mat3_f(float m[3][3]);
GB_MATH_DEF gbFloat3 *gb_float33_m(gbMat3 *m);
GB_MATH_DEF gbFloat3 *gb_float33_v(gbVec3 m[3]);
GB_MATH_DEF gbFloat3 *gb_float33_9(float m[9]);
GB_MATH_DEF void gb_float33_transpose(float (*vec)[3]);
GB_MATH_DEF void gb_float33_mul (float (*out)[3], float (*mat1)[3], float (*mat2)[3]);
GB_MATH_DEF void gb_float33_mul_vec3 (gbVec3 *out, float m[3][3], gbVec3 in);
GB_MATH_DEF void gb_mat4_identity (gbMat4 *m);
GB_MATH_DEF void gb_float44_identity(float m[4][4]);
GB_MATH_DEF void gb_mat4_transpose (gbMat4 *m);
GB_MATH_DEF void gb_mat4_mul (gbMat4 *out, gbMat4 *m1, gbMat4 *m2);
GB_MATH_DEF void gb_mat4_mul_vec4 (gbVec4 *out, gbMat4 *m, gbVec4 in);
GB_MATH_DEF void gb_mat4_inverse (gbMat4 *out, gbMat4 *in);
GB_MATH_DEF gbMat4 *gb_mat4_v(gbVec4 m[4]);
GB_MATH_DEF gbMat4 *gb_mat4_f(float m[4][4]);
GB_MATH_DEF gbFloat4 *gb_float44_m (gbMat4 *m);
GB_MATH_DEF gbFloat4 *gb_float44_v (gbVec4 m[4]);
GB_MATH_DEF gbFloat4 *gb_float44_16(float m[16]);
GB_MATH_DEF void gb_float44_transpose(float (*vec)[4]);
GB_MATH_DEF void gb_float44_mul (float (*out)[4], float (*mat1)[4], float (*mat2)[4]);
GB_MATH_DEF void gb_float44_mul_vec4 (gbVec4 *out, float m[4][4], gbVec4 in);
GB_MATH_DEF void gb_mat4_translate (gbMat4 *out, gbVec3 v);
GB_MATH_DEF void gb_mat4_rotate (gbMat4 *out, gbVec3 v, float angle_radians);
GB_MATH_DEF void gb_mat4_scale (gbMat4 *out, gbVec3 v);
GB_MATH_DEF void gb_mat4_scalef (gbMat4 *out, float s);
GB_MATH_DEF void gb_mat4_ortho2d (gbMat4 *out, float left, float right, float bottom, float top);
GB_MATH_DEF void gb_mat4_ortho3d (gbMat4 *out, float left, float right, float bottom, float top, float z_near, float z_far);
GB_MATH_DEF void gb_mat4_perspective (gbMat4 *out, float fovy, float aspect, float z_near, float z_far);
GB_MATH_DEF void gb_mat4_infinite_perspective(gbMat4 *out, float fovy, float aspect, float z_near);
GB_MATH_DEF void gb_mat4_look_at(gbMat4 *out, gbVec3 eye, gbVec3 centre, gbVec3 up);
GB_MATH_DEF gbQuat gb_quat (float x, float y, float z, float w);
GB_MATH_DEF gbQuat gb_quatv (float e[4]);
GB_MATH_DEF gbQuat gb_quat_axis_angle (gbVec3 axis, float angle_radians);
GB_MATH_DEF gbQuat gb_quat_euler_angles(float pitch, float yaw, float roll);
GB_MATH_DEF gbQuat gb_quat_identity (void);
GB_MATH_DEF void gb_quat_add(gbQuat *d, gbQuat q0, gbQuat q1);
GB_MATH_DEF void gb_quat_sub(gbQuat *d, gbQuat q0, gbQuat q1);
GB_MATH_DEF void gb_quat_mul(gbQuat *d, gbQuat q0, gbQuat q1);
GB_MATH_DEF void gb_quat_div(gbQuat *d, gbQuat q0, gbQuat q1);
GB_MATH_DEF void gb_quat_mulf(gbQuat *d, gbQuat q, float s);
GB_MATH_DEF void gb_quat_divf(gbQuat *d, gbQuat q, float s);
GB_MATH_DEF void gb_quat_addeq(gbQuat *d, gbQuat q);
GB_MATH_DEF void gb_quat_subeq(gbQuat *d, gbQuat q);
GB_MATH_DEF void gb_quat_muleq(gbQuat *d, gbQuat q);
GB_MATH_DEF void gb_quat_diveq(gbQuat *d, gbQuat q);
GB_MATH_DEF void gb_quat_muleqf(gbQuat *d, float s);
GB_MATH_DEF void gb_quat_diveqf(gbQuat *d, float s);
GB_MATH_DEF float gb_quat_dot(gbQuat q0, gbQuat q1);
GB_MATH_DEF float gb_quat_mag(gbQuat q);
GB_MATH_DEF void gb_quat_norm (gbQuat *d, gbQuat q);
GB_MATH_DEF void gb_quat_conj (gbQuat *d, gbQuat q);
GB_MATH_DEF void gb_quat_inverse(gbQuat *d, gbQuat q);
GB_MATH_DEF void gb_quat_axis (gbVec3 *axis, gbQuat q);
GB_MATH_DEF float gb_quat_angle(gbQuat q);
GB_MATH_DEF float gb_quat_pitch(gbQuat q);
GB_MATH_DEF float gb_quat_yaw (gbQuat q);
GB_MATH_DEF float gb_quat_roll (gbQuat q);
/* NOTE(bill): Rotate v by q */
GB_MATH_DEF void gb_quat_rotate_vec3(gbVec3 *d, gbQuat q, gbVec3 v);
GB_MATH_DEF void gb_mat4_from_quat (gbMat4 *out, gbQuat q);
GB_MATH_DEF void gb_quat_from_mat4 (gbQuat *out, gbMat4 *m);
/* Interpolations */
GB_MATH_DEF float gb_lerp (float a, float b, float t);
GB_MATH_DEF float gb_unlerp (float t, float a, float b);
GB_MATH_DEF float gb_smooth_step (float a, float b, float t);
GB_MATH_DEF float gb_smoother_step(float a, float b, float t);
GB_MATH_DEF void gb_vec2_lerp(gbVec2 *d, gbVec2 a, gbVec2 b, float t);
GB_MATH_DEF void gb_vec3_lerp(gbVec3 *d, gbVec3 a, gbVec3 b, float t);
GB_MATH_DEF void gb_vec4_lerp(gbVec4 *d, gbVec4 a, gbVec4 b, float t);
GB_MATH_DEF void gb_quat_lerp (gbQuat *d, gbQuat a, gbQuat b, float t);
GB_MATH_DEF void gb_quat_nlerp(gbQuat *d, gbQuat a, gbQuat b, float t);
GB_MATH_DEF void gb_quat_slerp(gbQuat *d, gbQuat a, gbQuat b, float t);
GB_MATH_DEF void gb_quat_nquad(gbQuat *d, gbQuat p, gbQuat a, gbQuat b, gbQuat q, float t);
GB_MATH_DEF void gb_quat_squad(gbQuat *d, gbQuat p, gbQuat a, gbQuat b, gbQuat q, float t);
GB_MATH_DEF void gb_quat_slerp_approx(gbQuat *d, gbQuat a, gbQuat b, float t);
GB_MATH_DEF void gb_quat_squad_approx(gbQuat *d, gbQuat p, gbQuat a, gbQuat b, gbQuat q, float t);
/* Rects */
GB_MATH_DEF gbRect2 gb_rect2(gbVec2 pos, gbVec2 dim);
GB_MATH_DEF gbRect2 gb_rect2v(float v[4]);
GB_MATH_DEF gbRect3 gb_rect3(gbVec3 pos, gbVec3 dim);
GB_MATH_DEF gbRect3 gb_rect3v(float v[6]);
GB_MATH_DEF int gb_rect2_contains (gbRect2 a, float x, float y);
GB_MATH_DEF int gb_rect2_contains_vec2 (gbRect2 a, gbVec2 p);
GB_MATH_DEF int gb_rect2_intersects (gbRect2 a, gbRect2 b);
GB_MATH_DEF int gb_rect2_intersection_result(gbRect2 a, gbRect2 b, gbRect2 *intersection);
#ifndef GB_MURMUR64_DEFAULT_SEED
#define GB_MURMUR64_DEFAULT_SEED 0x9747b28c
#endif
/* Hashing */
GB_MATH_DEF gb_math_u64 gb_hash_murmur64(void const *key, size_t num_bytes, gb_math_u64 seed);
/* Random */
/* TODO(bill): Use a generator for the random numbers */
GB_MATH_DEF float gb_random_range_float(float min_inc, float max_inc);
GB_MATH_DEF int gb_random_range_int (int min_inc, int max_inc);
GB_MATH_DEF float gb_random01 (void);
#if defined(__cplusplus)
}
#endif
#if defined(__cplusplus)
/* TODO(bill): How should I apply GB_MATH_DEF to these operator overloads? */
inline bool operator==(gbVec2 a, gbVec2 b) { return (a.x == b.x) && (a.y == b.y); }
inline bool operator!=(gbVec2 a, gbVec2 b) { return !operator==(a, b); }
inline gbVec2 operator+(gbVec2 a) { return a; }
inline gbVec2 operator-(gbVec2 a) { gbVec2 r = {-a.x, -a.y}; return r; }
inline gbVec2 operator+(gbVec2 a, gbVec2 b) { gbVec2 r; gb_vec2_add(&r, a, b); return r; }
inline gbVec2 operator-(gbVec2 a, gbVec2 b) { gbVec2 r; gb_vec2_sub(&r, a, b); return r; }
inline gbVec2 operator*(gbVec2 a, float scalar) { gbVec2 r; gb_vec2_mul(&r, a, scalar); return r; }
inline gbVec2 operator*(float scalar, gbVec2 a) { return operator*(a, scalar); }
inline gbVec2 operator/(gbVec2 a, float scalar) { return operator*(a, 1.0f/scalar); }
/* Hadamard Product */
inline gbVec2 operator*(gbVec2 a, gbVec2 b) { gbVec2 r = {a.x*b.x, a.y*b.y}; return r; }
inline gbVec2 operator/(gbVec2 a, gbVec2 b) { gbVec2 r = {a.x/b.x, a.y/b.y}; return r; }
inline gbVec2 &operator+=(gbVec2 &a, gbVec2 b) { return (a = a + b); }
inline gbVec2 &operator-=(gbVec2 &a, gbVec2 b) { return (a = a - b); }
inline gbVec2 &operator*=(gbVec2 &a, float scalar) { return (a = a * scalar); }
inline gbVec2 &operator/=(gbVec2 &a, float scalar) { return (a = a / scalar); }
inline bool operator==(gbVec3 a, gbVec3 b) { return (a.x == b.x) && (a.y == b.y) && (a.z == b.z); }
inline bool operator!=(gbVec3 a, gbVec3 b) { return !operator==(a, b); }
inline gbVec3 operator+(gbVec3 a) { return a; }
inline gbVec3 operator-(gbVec3 a) { gbVec3 r = {-a.x, -a.y, -a.z}; return r; }
inline gbVec3 operator+(gbVec3 a, gbVec3 b) { gbVec3 r; gb_vec3_add(&r, a, b); return r; }
inline gbVec3 operator-(gbVec3 a, gbVec3 b) { gbVec3 r; gb_vec3_sub(&r, a, b); return r; }
inline gbVec3 operator*(gbVec3 a, float scalar) { gbVec3 r; gb_vec3_mul(&r, a, scalar); return r; }
inline gbVec3 operator*(float scalar, gbVec3 a) { return operator*(a, scalar); }
inline gbVec3 operator/(gbVec3 a, float scalar) { return operator*(a, 1.0f/scalar); }
/* Hadamard Product */
inline gbVec3 operator*(gbVec3 a, gbVec3 b) { gbVec3 r = {a.x*b.x, a.y*b.y, a.z*b.z}; return r; }
inline gbVec3 operator/(gbVec3 a, gbVec3 b) { gbVec3 r = {a.x/b.x, a.y/b.y, a.z/b.z}; return r; }
inline gbVec3 &operator+=(gbVec3 &a, gbVec3 b) { return (a = a + b); }
inline gbVec3 &operator-=(gbVec3 &a, gbVec3 b) { return (a = a - b); }
inline gbVec3 &operator*=(gbVec3 &a, float scalar) { return (a = a * scalar); }
inline gbVec3 &operator/=(gbVec3 &a, float scalar) { return (a = a / scalar); }
inline bool operator==(gbVec4 a, gbVec4 b) { return (a.x == b.x) && (a.y == b.y) && (a.z == b.z) && (a.w == b.w); }
inline bool operator!=(gbVec4 a, gbVec4 b) { return !operator==(a, b); }
inline gbVec4 operator+(gbVec4 a) { return a; }
inline gbVec4 operator-(gbVec4 a) { gbVec4 r = {-a.x, -a.y, -a.z, -a.w}; return r; }
inline gbVec4 operator+(gbVec4 a, gbVec4 b) { gbVec4 r; gb_vec4_add(&r, a, b); return r; }
inline gbVec4 operator-(gbVec4 a, gbVec4 b) { gbVec4 r; gb_vec4_sub(&r, a, b); return r; }
inline gbVec4 operator*(gbVec4 a, float scalar) { gbVec4 r; gb_vec4_mul(&r, a, scalar); return r; }
inline gbVec4 operator*(float scalar, gbVec4 a) { return operator*(a, scalar); }
inline gbVec4 operator/(gbVec4 a, float scalar) { return operator*(a, 1.0f/scalar); }
/* Hadamard Product */
inline gbVec4 operator*(gbVec4 a, gbVec4 b) { gbVec4 r = {a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w}; return r; }
inline gbVec4 operator/(gbVec4 a, gbVec4 b) { gbVec4 r = {a.x/b.x, a.y/b.y, a.z/b.z, a.w/b.w}; return r; }
inline gbVec4 &operator+=(gbVec4 &a, gbVec4 b) { return (a = a + b); }
inline gbVec4 &operator-=(gbVec4 &a, gbVec4 b) { return (a = a - b); }
inline gbVec4 &operator*=(gbVec4 &a, float scalar) { return (a = a * scalar); }
inline gbVec4 &operator/=(gbVec4 &a, float scalar) { return (a = a / scalar); }
inline gbMat2 operator+(gbMat2 const &a, gbMat2 const &b) {
int i, j;
gbMat2 r = {0};
for (j = 0; j < 2; j++) {
for (i = 0; i < 2; i++)
r.e[2*j+i] = a.e[2*j+i] + b.e[2*j+i];
}
return r;
}
inline gbMat2 operator-(gbMat2 const &a, gbMat2 const &b) {
int i, j;
gbMat2 r = {0};
for (j = 0; j < 2; j++) {
for (i = 0; i < 2; i++)
r.e[2*j+i] = a.e[2*j+i] - b.e[2*j+i];
}
return r;
}
inline gbMat2 operator*(gbMat2 const &a, gbMat2 const &b) { gbMat2 r; gb_mat2_mul(&r, (gbMat2 *)&a, (gbMat2 *)&b); return r; }
inline gbVec2 operator*(gbMat2 const &a, gbVec2 v) { gbVec2 r; gb_mat2_mul_vec2(&r, (gbMat2 *)&a, v); return r; }
inline gbMat2 operator*(gbMat2 const &a, float scalar) {
gbMat2 r = {0};
int i;
for (i = 0; i < 2*2; i++) r.e[i] = a.e[i] * scalar;
return r;
}
inline gbMat2 operator*(float scalar, gbMat2 const &a) { return operator*(a, scalar); }
inline gbMat2 operator/(gbMat2 const &a, float scalar) { return operator*(a, 1.0f/scalar); }
inline gbMat2& operator+=(gbMat2& a, gbMat2 const &b) { return (a = a + b); }
inline gbMat2& operator-=(gbMat2& a, gbMat2 const &b) { return (a = a - b); }
inline gbMat2& operator*=(gbMat2& a, gbMat2 const &b) { return (a = a * b); }
inline gbMat3 operator+(gbMat3 const &a, gbMat3 const &b) {
int i, j;
gbMat3 r = {0};
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++)
r.e[3*j+i] = a.e[3*j+i] + b.e[3*j+i];
}
return r;
}
inline gbMat3 operator-(gbMat3 const &a, gbMat3 const &b) {
int i, j;
gbMat3 r = {0};
for (j = 0; j < 3; j++) {
for (i = 0; i < 3; i++)
r.e[3*j+i] = a.e[3*j+i] - b.e[3*j+i];
}
return r;
}
inline gbMat3 operator*(gbMat3 const &a, gbMat3 const &b) { gbMat3 r; gb_mat3_mul(&r, (gbMat3 *)&a, (gbMat3 *)&b); return r; }
inline gbVec3 operator*(gbMat3 const &a, gbVec3 v) { gbVec3 r; gb_mat3_mul_vec3(&r, (gbMat3 *)&a, v); return r; } inline gbMat3 operator*(gbMat3 const &a, float scalar) {
gbMat3 r = {0};
int i;
for (i = 0; i < 3*3; i++) r.e[i] = a.e[i] * scalar;
return r;
}
inline gbMat3 operator*(float scalar, gbMat3 const &a) { return operator*(a, scalar); }
inline gbMat3 operator/(gbMat3 const &a, float scalar) { return operator*(a, 1.0f/scalar); }
inline gbMat3& operator+=(gbMat3& a, gbMat3 const &b) { return (a = a + b); }
inline gbMat3& operator-=(gbMat3& a, gbMat3 const &b) { return (a = a - b); }
inline gbMat3& operator*=(gbMat3& a, gbMat3 const &b) { return (a = a * b); }
inline gbMat4 operator+(gbMat4 const &a, gbMat4 const &b) {
int i, j;
gbMat4 r = {0};
for (j = 0; j < 4; j++) {
for (i = 0; i < 4; i++)
r.e[4*j+i] = a.e[4*j+i] + b.e[4*j+i];
}
return r;
}
inline gbMat4 operator-(gbMat4 const &a, gbMat4 const &b) {
int i, j;
gbMat4 r = {0};
for (j = 0; j < 4; j++) {
for (i = 0; i < 4; i++)
r.e[4*j+i] = a.e[4*j+i] - b.e[4*j+i];
}
return r;
}
inline gbMat4 operator*(gbMat4 const &a, gbMat4 const &b) { gbMat4 r; gb_mat4_mul(&r, (gbMat4 *)&a, (gbMat4 *)&b); return r; }
inline gbVec4 operator*(gbMat4 const &a, gbVec4 v) { gbVec4 r; gb_mat4_mul_vec4(&r, (gbMat4 *)&a, v); return r; }
inline gbMat4 operator*(gbMat4 const &a, float scalar) {
gbMat4 r = {0};
int i;
for (i = 0; i < 4*4; i++) r.e[i] = a.e[i] * scalar;
return r;
}
inline gbMat4 operator*(float scalar, gbMat4 const &a) { return operator*(a, scalar); }
inline gbMat4 operator/(gbMat4 const &a, float scalar) { return operator*(a, 1.0f/scalar); }
inline gbMat4& operator+=(gbMat4 &a, gbMat4 const &b) { return (a = a + b); }
inline gbMat4& operator-=(gbMat4 &a, gbMat4 const &b) { return (a = a - b); }
inline gbMat4& operator*=(gbMat4 &a, gbMat4 const &b) { return (a = a * b); }
inline bool operator==(gbQuat a, gbQuat b) { return a.xyzw == b.xyzw; }
inline bool operator!=(gbQuat a, gbQuat b) { return !operator==(a, b); }
inline gbQuat operator+(gbQuat q) { return q; }
inline gbQuat operator-(gbQuat q) { return gb_quat(-q.x, -q.y, -q.z, -q.w); }
inline gbQuat operator+(gbQuat a, gbQuat b) { gbQuat r; gb_quat_add(&r, a, b); return r; }
inline gbQuat operator-(gbQuat a, gbQuat b) { gbQuat r; gb_quat_sub(&r, a, b); return r; }
inline gbQuat operator*(gbQuat a, gbQuat b) { gbQuat r; gb_quat_mul(&r, a, b); return r; }
inline gbQuat operator*(gbQuat q, float s) { gbQuat r; gb_quat_mulf(&r, q, s); return r; }
inline gbQuat operator*(float s, gbQuat q) { return operator*(q, s); }
inline gbQuat operator/(gbQuat q, float s) { gbQuat r; gb_quat_divf(&r, q, s); return r; }
inline gbQuat &operator+=(gbQuat &a, gbQuat b) { gb_quat_addeq(&a, b); return a; }
inline gbQuat &operator-=(gbQuat &a, gbQuat b) { gb_quat_subeq(&a, b); return a; }
inline gbQuat &operator*=(gbQuat &a, gbQuat b) { gb_quat_muleq(&a, b); return a; }
inline gbQuat &operator/=(gbQuat &a, gbQuat b) { gb_quat_diveq(&a, b); return a; }
inline gbQuat &operator*=(gbQuat &a, float b) { gb_quat_muleqf(&a, b); return a; }
inline gbQuat &operator/=(gbQuat &a, float b) { gb_quat_diveqf(&a, b); return a; }
/* Rotate v by a */
inline gbVec3 operator*(gbQuat q, gbVec3 v) { gbVec3 r; gb_quat_rotate_vec3(&r, q, v); return r; }
#endif
#endif /* GB_MATH_INCLUDE_GB_MATH_H */
/****************************************************************
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* Implementation
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
****************************************************************/
#if defined(GB_MATH_IMPLEMENTATION) && !defined(GB_MATH_IMPLEMENTATION_DONE)
#define GB_MATH_IMPLEMENTATION_DONE
#if (defined(__GCC__) || defined(__GNUC__)) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#pragma GCC diagnostic ignored "-Wmissing-braces"
#elif __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wattributes"
#pragma clang diagnostic ignored "-Wmissing-braces"
#endif
/* NOTE(bill): To remove the need for memcpy */
static void gb__memcpy_4byte(void *dest, void const *src, size_t size) {
size_t i;
unsigned int *d, *s;
d = (unsigned int *)dest;
s = (unsigned int *)src;
for (i = 0; i < size/4; i++) {
*d++ = *s++;
}
}
float gb_to_radians(float degrees) { return degrees * GB_MATH_TAU / 360.0f; }
float gb_to_degrees(float radians) { return radians * 360.0f / GB_MATH_TAU; }
float gb_angle_diff(float radians_a, float radians_b) {
float delta = gb_mod(radians_b-radians_a, GB_MATH_TAU);
delta = gb_mod(delta + 1.5f*GB_MATH_TAU, GB_MATH_TAU);
delta -= 0.5f*GB_MATH_TAU;
return delta;
}
float gb_copy_sign(float x, float y) {
int ix, iy;
ix = *(int *)&x;
iy = *(int *)&y;
ix &= 0x7fffffff;
ix |= iy & 0x80000000;
return *(float *)&ix;
}
float gb_remainder(float x, float y) {
return x - (gb_round(x/y)*y);
}
float gb_mod(float x, float y) {
float result;
y = gb_abs(y);
result = gb_remainder(gb_abs(x), y);
if (gb_sign(result)) result += y;
return gb_copy_sign(result, x);
}
float gb_quake_rsqrt(float a) {
union {
int i;
float f;
} t;
float x2;
float const three_halfs = 1.5f;
x2 = a * 0.5f;
t.f = a;
t.i = 0x5f375a86 - (t.i >> 1); /* What the fuck? */
t.f = t.f * (three_halfs - (x2 * t.f * t.f)); /* 1st iteration */
t.f = t.f * (three_halfs - (x2 * t.f * t.f)); /* 2nd iteration, this can be removed */
return t.f;
}
#if defined(GB_MATH_NO_MATH_H)
#if defined(_MSC_VER)
float gb_rsqrt(float a) { return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(a))); }
float gb_sqrt(float a) { return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(a))); };
float
gb_sin(float a)
{
static float const a0 = +1.91059300966915117e-31f;
static float const a1 = +1.00086760103908896f;
static float const a2 = -1.21276126894734565e-2f;
static float const a3 = -1.38078780785773762e-1f;
static float const a4 = -2.67353392911981221e-2f;
static float const a5 = +2.08026600266304389e-2f;
static float const a6 = -3.03996055049204407e-3f;
static float const a7 = +1.38235642404333740e-4f;
return a0 + a*(a1 + a*(a2 + a*(a3 + a*(a4 + a*(a5 + a*(a6 + a*a7))))));
}
float
gb_cos(float a)
{
static float const a0 = +1.00238601909309722f;
static float const a1 = -3.81919947353040024e-2f;
static float const a2 = -3.94382342128062756e-1f;
static float const a3 = -1.18134036025221444e-1f;
static float const a4 = +1.07123798512170878e-1f;
static float const a5 = -1.86637164165180873e-2f;
static float const a6 = +9.90140908664079833e-4f;
static float const a7 = -5.23022132118824778e-14f;
return a0 + a*(a1 + a*(a2 + a*(a3 + a*(a4 + a*(a5 + a*(a6 + a*a7))))));
}
float
gb_tan(float radians)
{
float rr = radians*radians;
float a = 9.5168091e-03f;
a *= rr;
a += 2.900525e-03f;
a *= rr;
a += 2.45650893e-02f;
a *= rr;
a += 5.33740603e-02f;
a *= rr;
a += 1.333923995e-01f;
a *= rr;
a += 3.333314036e-01f;
a *= rr;
a += 1.0f;
a *= radians;
return a;
}
float gb_arcsin(float a) { return gb_arctan2(a, gb_sqrt((1.0f + a) * (1.0f - a))); }
float gb_arccos(float a) { return gb_arctan2(gb_sqrt((1.0f + a) * (1.0 - a)), a); }
float
gb_arctan(float a)
{
float u = a*a;
float u2 = u*u;
float u3 = u2*u;
float u4 = u3*u;
float f = 1.0f+0.33288950512027f*u-0.08467922817644f*u2+0.03252232640125f*u3-0.00749305860992f*u4;
return a/f;
}
float
gb_arctan2(float y, float x)
{
if (gb_abs(x) > gb_abs(y)) {
float a = gb_arctan(y/x);
if (x > 0.0f)
return a;
else
return y > 0.0f ? a+GB_MATH_TAU_OVER_2:a-GB_MATH_TAU_OVER_2;
} else {
float a = gb_arctan(x/y);
if (x > 0.0f)
return y > 0.0f ? GB_MATH_TAU_OVER_4-a:-GB_MATH_TAU_OVER_4-a;
else
return y > 0.0f ? GB_MATH_TAU_OVER_4+a:-GB_MATH_TAU_OVER_4+a;
}
}
float
gb_exp(float a)
{
union { float f; int i; } u, v;
u.i = (int)(6051102 * a + 1056478197);
v.i = (int)(1056478197 - 6051102 * a);
return u.f / v.f;
}
float
gb_log(float a)
{
union { float f; int i; } u = {a};
return (u.i - 1064866805) * 8.262958405176314e-8f; /* 1 / 12102203.0; */
}
float
gb_pow(float a, float b)
{
int flipped = 0, e;
float f, r = 1.0f;
if (b < 0) {
flipped = 1;
b = -b;
}
e = (int)b;
f = gb_exp(b - e);
while (e) {
if (e & 1) r *= a;
a *= a;
e >>= 1;
}
r *= f;
return flipped ? 1.0f/r : r;
}
#else
float gb_rsqrt(float a) { return 1.0f/__builtin_sqrt(a); }
float gb_sqrt(float a) { return __builtin_sqrt(a); }
float gb_sin(float radians) { return __builtin_sinf(radians); }
float gb_cos(float radians) { return __builtin_cosf(radians); }
float gb_tan(float radians) { return __builtin_tanf(radians); }
float gb_arcsin(float a) { return __builtin_asinf(a); }
float gb_arccos(float a) { return __builtin_acosf(a); }
float gb_arctan(float a) { return __builtin_atanf(a); }
float gb_arctan2(float y, float x) { return __builtin_atan2f(y, x); }
float gb_exp(float x) { return __builtin_expf(x); }
float gb_log(float x) { return __builtin_logf(x); }
// TODO(bill): Should this be gb_exp(y * gb_log(x)) ???
float gb_pow(float x, float y) { return __builtin_powf(x, y); }
#endif
#else
float gb_rsqrt(float a) { return 1.0f/sqrtf(a); }
float gb_sqrt(float a) { return sqrtf(a); };
float gb_sin(float radians) { return sinf(radians); };
float gb_cos(float radians) { return cosf(radians); };
float gb_tan(float radians) { return tanf(radians); };
float gb_arcsin(float a) { return asinf(a); };