Jo Engine 2026.11.6
Jo Sega Saturn Engine
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1/*
2** Jo Sega Saturn Engine
3** Copyright (c) 2012-2026, Johannes Fetz (johannesfetz@gmail.com)
4** All rights reserved.
5**
6** Redistribution and use in source and binary forms, with or without
7** modification, are permitted provided that the following conditions are met:
8** * Redistributions of source code must retain the above copyright
9** notice, this list of conditions and the following disclaimer.
10** * Redistributions in binary form must reproduce the above copyright
11** notice, this list of conditions and the following disclaimer in the
12** documentation and/or other materials provided with the distribution.
13** * Neither the name of the Johannes Fetz nor the
14** names of its contributors may be used to endorse or promote products
15** derived from this software without specific prior written permission.
16**
17** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
34
35#ifndef __JO_MATH_H__
36# define __JO_MATH_H__
37
38/*
39███╗ ███╗ █████╗ ████████╗██╗ ██╗ ██████╗ ██████╗ ███╗ ██╗███████╗████████╗███████╗
40████╗ ████║██╔══██╗╚══██╔══╝██║ ██║ ██╔════╝██╔═══██╗████╗ ██║██╔════╝╚══██╔══╝██╔════╝
41██╔████╔██║███████║ ██║ ███████║ ██║ ██║ ██║██╔██╗ ██║███████╗ ██║ ███████╗
42██║╚██╔╝██║██╔══██║ ██║ ██╔══██║ ██║ ██║ ██║██║╚██╗██║╚════██║ ██║ ╚════██║
43██║ ╚═╝ ██║██║ ██║ ██║ ██║ ██║ ╚██████╗╚██████╔╝██║ ╚████║███████║ ██║ ███████║
44╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚══════╝
45*/
46
51# define JO_PI (3.1415927f)
52
54# define JO_PI_2 (1.5707963f)
55
56/*
57███╗ ███╗██╗ ██╗██╗ ████████╗ ██╗██████╗ ██╗██╗ ██╗
58████╗ ████║██║ ██║██║ ╚══██╔══╝██╔╝██╔══██╗██║██║ ██║
59██╔████╔██║██║ ██║██║ ██║ ██╔╝ ██║ ██║██║██║ ██║
60██║╚██╔╝██║██║ ██║██║ ██║ ██╔╝ ██║ ██║██║╚██╗ ██╔╝
61██║ ╚═╝ ██║╚██████╔╝███████╗██║██╔╝ ██████╔╝██║ ╚████╔╝
62╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝
63*/
64
69# define JO_MULT_BY_2(X) ((X) << 1)
70
75# define JO_MULT_BY_4(X) ((X) << 2)
76
81# define JO_MULT_BY_8(X) ((X) << 3)
82
87# define JO_MULT_BY_16(X) ((X) << 4)
88
93# define JO_MULT_BY_32(X) ((X) << 5)
94
99# define JO_MULT_BY_64(X) ((X) << 6)
100
105# define JO_MULT_BY_128(X) ((X) << 7)
106
111# define JO_MULT_BY_256(X) ((X) << 8)
112
117# define JO_MULT_BY_512(X) ((X) << 9)
118
123# define JO_MULT_BY_1024(X) ((X) << 10)
124
129# define JO_MULT_BY_2048(X) ((X) << 11)
130
135# define JO_MULT_BY_4096(X) ((X) << 12)
136
141# define JO_MULT_BY_32768(X) ((X) << 15)
142
147# define JO_MULT_BY_65536(X) ((X) << 16)
148
153# define JO_DIV_BY_2(X) ((X) >> 1)
154
159# define JO_DIV_BY_4(X) ((X) >> 2)
160
165# define JO_DIV_BY_8(X) ((X) >> 3)
166
171# define JO_DIV_BY_16(X) ((X) >> 4)
172
177# define JO_DIV_BY_32(X) ((X) >> 5)
178
183# define JO_DIV_BY_64(X) ((X) >> 6)
184
189# define JO_DIV_BY_128(X) ((X) >> 7)
190
195# define JO_DIV_BY_256(X) ((X) >> 8)
196
201# define JO_DIV_BY_512(X) ((X) >> 9)
202
207# define JO_DIV_BY_1024(X) ((X) >> 10)
208
213# define JO_DIV_BY_32768(X) ((X) >> 15)
214
219# define JO_DIV_BY_65536(X) ((X) >> 16)
220
225# define JO_DIV_BY_2147483648(X) ((X) >> 31)
226
227/*
228████████╗ ██████╗ ██████╗ ██╗ ███████╗
229╚══██╔══╝██╔═══██╗██╔═══██╗██║ ██╔════╝
230 ██║ ██║ ██║██║ ██║██║ ███████╗
231 ██║ ██║ ██║██║ ██║██║ ╚════██║
232 ██║ ╚██████╔╝╚██████╔╝███████╗███████║
233 ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝╚══════╝
234*/
235
240# define JO_ZERO(X) X ^= X
241
247# define JO_MOD_POW2(N, M) ((N) & ((M) - 1))
248
252# define JO_IS_POW2(N) ((N) != 0 && (((N) & ((N) - 1)) == 0))
253
257# define JO_ABS(X) ((X) < 0 ? -(X) : (X))
258
262# define JO_FABS(X) ((X) < 0.0f ? -(X) : (X))
263
268# define JO_MIN(A, B) (((A) < (B)) ? (A) : (B))
269
274# define JO_MAX(A, B) (((A) > (B)) ? (A) : (B))
275
283# define JO_CLAMP(X, MIN, MAX) JO_MAX((MIN), JO_MIN((MAX), (X)))
284
288# define JO_CHANGE_SIGN(X) (-(X))
289
291# define JO_FLOAT_EPSILON (0.00001f)
292
297# define JO_FLOAT_NEARLY_EQUALS(A, B) ((A) <= ((B) + JO_FLOAT_EPSILON) && (A) >= ((B) - JO_FLOAT_EPSILON))
298
302# define JO_IS_FLOAT_NULL(A) ((A) <= JO_FLOAT_EPSILON && (A) >= (-JO_FLOAT_EPSILON))
303
307# define JO_RAD_TO_DEG(A) ((A) * 57.295780f)
308
312# define JO_DEG_TO_RAD(A) ((A) * 0.017453292f)
313
317# define JO_IS_ODD(A) ((A) & 1)
318
324# define JO_SWAP(A, B) { __typeof__((A) + 0) __jo_swap_tmp = (A); (A) = (B); (B) = __jo_swap_tmp; }
325
332# define JO_OPAQUE(VAR) __asm__ ("" : "+r" (VAR))
333
339# define JO_PERCENT_USED(TOTAL, FREE) ((TOTAL) == 0 ? 0 : (int)((unsigned int)((TOTAL) - (FREE)) * 100u / (unsigned int)(TOTAL)))
340
344# define JO_SQUARE(A) ((A) * (A))
345
349# define JO_BCD_INT(BCD) (((((BCD) & 0xF0) >> 4) * 10) + ((BCD) & 0x0F))
350
351/*
352██████╗ ██╗ ██╗████████╗███████╗███████╗██╗███████╗██╗ ██████╗
353██╔══██╗╚██╗ ██╔╝╚══██╔══╝██╔════╝██╔════╝██║██╔════╝██║ ██╔══██╗
354██████╔╝ ╚████╔╝ ██║ █████╗ █████╗ ██║█████╗ ██║ ██║ ██║
355██╔══██╗ ╚██╔╝ ██║ ██╔══╝ ██╔══╝ ██║██╔══╝ ██║ ██║ ██║
356██████╔╝ ██║ ██║ ███████╗██║ ██║███████╗███████╗██████╔╝
357╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝
358*/
359
364# define JO_SET_FLAGS(BYTEFIELD, FLAGS) BYTEFIELD = (FLAGS)
365
369# define JO_SET_ALL_FLAGS(BYTEFIELD) BYTEFIELD = (~0)
370
375# define JO_ADD_FLAG(BYTEFIELD, FLAG) BYTEFIELD |= (FLAG)
376
381# define JO_REMOVE_FLAG(BYTEFIELD, FLAG) BYTEFIELD &= ~(FLAG)
382
386# define JO_REMOVE_ALL_FLAGS(BYTEFIELD) BYTEFIELD = 0
387
392# define JO_HAS_FLAG(BYTEFIELD, FLAG) (((BYTEFIELD) & (FLAG)) == (FLAG))
393
394/*
395███████╗██╗██╗ ██╗███████╗██████╗ ██████╗ ██████╗ ███╗ ██╗██╗ ██╗███████╗██████╗ ████████╗██╗ ██████╗ ███╗ ██╗
396██╔════╝██║╚██╗██╔╝██╔════╝██╔══██╗ ██╔════╝██╔═══██╗████╗ ██║██║ ██║██╔════╝██╔══██╗╚══██╔══╝██║██╔═══██╗████╗ ██║
397█████╗ ██║ ╚███╔╝ █████╗ ██║ ██║ ██║ ██║ ██║██╔██╗ ██║██║ ██║█████╗ ██████╔╝ ██║ ██║██║ ██║██╔██╗ ██║
398██╔══╝ ██║ ██╔██╗ ██╔══╝ ██║ ██║ ██║ ██║ ██║██║╚██╗██║╚██╗ ██╔╝██╔══╝ ██╔══██╗ ██║ ██║██║ ██║██║╚██╗██║
399██║ ██║██╔╝ ██╗███████╗██████╔╝ ╚██████╗╚██████╔╝██║ ╚████║ ╚████╔╝ ███████╗██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║
400╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
401
402 ▲ NOTE ABOUT FIXED NUMBER ▲
403 Only values between -32767.99998 and 32767.99998 can be converted to fixed number.
404
405*/
406
408# define JO_FIXED_0 (0)
410# define JO_FIXED_1_DIV_2 (32768)
412# define JO_FIXED_ALMOST_1 (65470)
414# define JO_FIXED_1 (65536)
416# define JO_FIXED_2 JO_INT_TO_FIXED(2)
418# define JO_FIXED_3 JO_INT_TO_FIXED(3)
420# define JO_FIXED_4 JO_INT_TO_FIXED(4)
422# define JO_FIXED_8 JO_INT_TO_FIXED(8)
424# define JO_FIXED_16 JO_INT_TO_FIXED(16)
426# define JO_FIXED_32 JO_INT_TO_FIXED(32)
428# define JO_FIXED_120 JO_INT_TO_FIXED(120)
430# define JO_FIXED_150 JO_INT_TO_FIXED(150)
432# define JO_FIXED_180 JO_INT_TO_FIXED(180)
434# define JO_FIXED_360 JO_INT_TO_FIXED(360)
435
437# define JO_FIXED_MIN (-2147483647)
439# define JO_FIXED_MAX (2147483647)
441# define JO_FIXED_EPSILON (1)
443# define JO_FIXED_OVERFLOW (0x80000000)
445# define JO_FIXED_PI (205887)
447# define JO_FIXED_PI_2 (411775)
449# define JO_FIXED_180_DIV_PI (3754936)
451# define JO_FIXED_PI_DIV_180 (1144)
453# define JO_FIXED_PI_DIV_2 (102944)
455# define JO_FIXED_PI_OVER_2 JO_FIXED_PI_DIV_2
457# define JO_FIXED_3_PI_OVER_2 (308831)
459# define JO_FIXED_1_DIV (1.0f / 65536.0f)
460
464# define JO_INT_TO_FIXED(X) JO_MULT_BY_65536(X)
468# define JO_FIXED_TO_INT(X) JO_DIV_BY_65536(X)
470# define JO_FIXED_DECIMAL_MASK (0xFFFF)
475# define JO_FLOAT_TO_FIXED(X) ((jo_fixed)(65536.0f * (X)))
481# define JO_DEG_TO_ANGLE(X) ((jo_angle)(int)((X) * 8192 / 45))
486# define JO_RAD_TO_ANGLE(X) ((jo_angle)(int)((65536.0 * (X)) / (2.0 * 3.14159265358979)))
487
493{
494 return JO_INT_TO_FIXED(x);
495}
496
502{
503 return JO_FIXED_TO_INT(x);
504}
505
506/*
507** The float API of the engine works on the bits of the floats with integers: no float computation, no float routine
508** of libgcc (the SH-2 has no FPU: each float operation was a call of about 100 cycles). A constant is converted by the
509** compiler.
510*/
511
516static __jo_force_inline unsigned int __jo_float_bits(const float f)
517{
518 union
519 {
520 float f;
521 unsigned int i;
522 } u;
523
524 u.f = f;
525 return (u.i);
526}
527
532static __jo_force_inline float __jo_float_from_bits(const unsigned int i)
533{
534 union
535 {
536 float f;
537 unsigned int i;
538 } u;
539
540 u.i = i;
541 return (u.f);
542}
543
549
555
562{
563 if (__builtin_constant_p(x))
564 return ((jo_fixed)(x * (float)JO_FIXED_1));
565 return (__jo_float_to_fixed(x));
566}
567
574{
575 if (__builtin_constant_p(x))
576 return ((float)x * JO_FIXED_1_DIV);
577 return (__jo_fixed_to_float(x));
578}
579
585float __jo_float_scale2(const float x, const int shift);
586
591static __jo_force_inline int __jo_float_sign(const float x)
592{
593 unsigned int bits;
594
595 bits = __jo_float_bits(x);
596 if ((bits & 0x7FFFFFFFu) == 0)
597 return (0);
598 return ((bits & 0x80000000u) ? -1 : 1);
599}
600
608int __jo_floats_to_fixed_scaled(const float * const in, jo_fixed * const out, const int count,
609 const int top);
610
616jo_angle __jo_atan2_floats(const float y, const float x);
617
623{
624 // (angle x 2 pi / 65536 in Q16.16: angle x 2 pi)
625 return ((jo_fixed)(((long long)angle * JO_FIXED_PI_2) >> 16));
626}
627
633{
634 // (far from the range: the turns removed at once, rad x 2^32 / (2 pi) by the high word of a product (dmuls.l), at
635 // most one turn off. The loops alone made up to 5000 turns)
636 if (rad > 2 * JO_FIXED_PI_2 || rad < -2 * JO_FIXED_PI_2)
637 rad -= (int)(((long long)rad * 10430) >> 32) * JO_FIXED_PI_2;
638 while (rad > JO_FIXED_PI) rad -= JO_FIXED_PI_2;
639 while (rad <= -JO_FIXED_PI) rad += JO_FIXED_PI_2;
640 return (rad);
641}
642
648{
649 // (far from the range: the turns removed at once, deg x 2^32 / 360 by the high word of a product (dmuls.l), at most
650 // one turn off. The loops alone made up to 90 turns)
651 if (deg > 2 * JO_FIXED_360 || deg < -2 * JO_FIXED_360)
652 deg -= (int)(((long long)deg * 182) >> 32) * JO_FIXED_360;
653 while (deg > JO_FIXED_180) deg -= JO_FIXED_360;
654 while (deg <= -JO_FIXED_180) deg += JO_FIXED_360;
655 return (deg);
656}
657
664
672{
673 return ((jo_fixed)(((long long)x * y) >> 16));
674}
675
683{
684 int high;
685
686 __asm__ ("dmuls.l %1, %2\n\t"
687 "sts mach, %0"
688 : "=r" (high)
689 : "r" (x), "r" (y)
690 : "mach", "macl");
691 return (high);
692}
693
701static __jo_force_inline jo_fixed jo_fixed_dot(const jo_fixed ptA[3], const jo_fixed ptB[3])
702{
703 const jo_fixed *a;
704 const jo_fixed *b;
705 jo_fixed high;
706 jo_fixed low;
707
708 a = ptA;
709 b = ptB;
710 __asm__ ("clrmac\n\t"
711 "mac.l @%2+, @%3+\n\t"
712 "mac.l @%2+, @%3+\n\t"
713 "mac.l @%2+, @%3+\n\t"
714 "sts mach, %0\n\t"
715 "sts macl, %1\n\t"
716 "xtrct %0, %1"
717 : "=&r" (high), "=&r" (low), "+r" (a), "+r" (b)
718 : "m" (*(const jo_fixed (*)[3])ptA), "m" (*(const jo_fixed (*)[3])ptB)
719 : "mach", "macl");
720 return (low);
721}
722
730static __jo_force_inline jo_fixed jo_fixed_div(const jo_fixed dividend, const jo_fixed divisor)
731{
732 JO_SH2_DVSR = divisor;
733 // 64 bits dividend: (dividend << 16)
734 JO_SH2_DVDNTH = dividend >> 16;
735 // Writing the low part starts the division
736 JO_SH2_DVDNTL = dividend << 16;
737 return ((jo_fixed)JO_SH2_DVDNTL);
738}
739
748static __jo_force_inline int jo_int_div(const int dividend, const int divisor)
749{
750 JO_SH2_DVSR = divisor;
751 // (writing the dividend starts the division. The quotient read in DVDNTL, as jo_fixed_div(): the register saved and
752 // restored by the VBLANK interrupt)
753 JO_SH2_DVDNT = dividend;
754 return (JO_SH2_DVDNTL);
755}
756
763static __jo_force_inline int jo_shift_right8(const int x)
764{
765 return (x < 0 ? (int)~(~(unsigned int)x >> 8) : (int)((unsigned int)x >> 8));
766}
767
775{
776 return ((int)(((unsigned int)x + (1u << 23)) >> 8) - (1 << 15));
777}
778
785static __jo_force_inline void jo_fixed_div_start(const jo_fixed dividend, const jo_fixed divisor)
786{
787 JO_SH2_DVSR = divisor;
788 JO_SH2_DVDNTH = dividend >> 16;
789 JO_SH2_DVDNTL = dividend << 16;
790}
791
800
809
818
824{
825 return (x & 0xFFFF0000UL) + (x & 0x0000FFFFUL ? JO_FIXED_1 : 0);
826}
827
833{
834 return (x & 0xFFFF0000ul);
835}
836
837/*
838██████╗ █████╗ ███╗ ██╗██████╗ ██████╗ ███╗ ███╗
839██╔══██╗██╔══██╗████╗ ██║██╔══██╗██╔═══██╗████╗ ████║
840██████╔╝███████║██╔██╗ ██║██║ ██║██║ ██║██╔████╔██║
841██╔══██╗██╔══██║██║╚██╗██║██║ ██║██║ ██║██║╚██╔╝██║
842██║ ██║██║ ██║██║ ╚████║██████╔╝╚██████╔╝██║ ╚═╝ ██║
843╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚═════╝ ╚═════╝ ╚═╝ ╚═╝
844*/
845
850extern int jo_random_seed;
851
857int jo_random(int max);
858
864static __jo_force_inline int jo_random_using_multiple(int max, int multiple)
865{
866 // (the multiples from 1 x multiple to max: (jo_random(max) / multiple) * multiple gave 0 for the values under multiple)
867 return (max < multiple ? multiple : jo_random(max / multiple) * multiple);
868}
869
877static __jo_force_inline int jo_random_seeded(unsigned int * const seed)
878{
879 *seed = *seed * 1103515245u + 12345u;
880 return ((int)(*seed >> 16));
881}
882
883/*
884███████╗ ██████╗ ██████╗ ████████╗
885██╔════╝██╔═══██╗██╔══██╗╚══██╔══╝
886███████╗██║ ██║██████╔╝ ██║
887╚════██║██║▄▄ ██║██╔══██╗ ██║
888███████║╚██████╔╝██║ ██║ ██║
889╚══════╝ ╚══▀▀═╝ ╚═╝ ╚═╝ ╚═╝
890*/
891
897static __jo_force_inline unsigned int jo_highest_bit(unsigned int x)
898{
899 unsigned int bit;
900
901 bit = 0;
902 if (x & 0xffff0000)
903 {
904 bit = 16;
905 x >>= 16;
906 }
907 if (x & 0xff00)
908 {
909 bit += 8;
910 x >>= 8;
911 }
912 if (x & 0xf0)
913 {
914 bit += 4;
915 x >>= 4;
916 }
917 if (x & 0xc)
918 {
919 bit += 2;
920 x >>= 2;
921 }
922 if (x & 0x2)
923 ++bit;
924 return (bit);
925}
926
933
939unsigned int jo_sqrt(unsigned int value);
940
946static __jo_force_inline float jo_sqrtf(float value)
947{
948 // A union avoids the strict aliasing issue of *(unsigned int *)&value (undefined behavior with -O2)
949 union
950 {
951 float f;
952 unsigned int i;
953 } bits;
954
955 bits.f = value;
956 bits.i += 127 << 23;
957 bits.i >>= 1;
958 return (bits.f);
959}
960
961/*
962██╗███╗ ██╗██╗ ██╗███████╗██████╗ ███████╗███████╗ ███████╗ ██████╗ ██████╗ ████████╗
963██║████╗ ██║██║ ██║██╔════╝██╔══██╗██╔════╝██╔════╝ ██╔════╝██╔═══██╗██╔══██╗╚══██╔══╝
964██║██╔██╗ ██║██║ ██║█████╗ ██████╔╝███████╗█████╗ ███████╗██║ ██║██████╔╝ ██║
965██║██║╚██╗██║╚██╗ ██╔╝██╔══╝ ██╔══██╗╚════██║██╔══╝ ╚════██║██║▄▄ ██║██╔══██╗ ██║
966██║██║ ╚████║ ╚████╔╝ ███████╗██║ ██║███████║███████╗ ███████║╚██████╔╝██║ ██║ ██║
967╚═╝╚═╝ ╚═══╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝ ╚══════╝ ╚══▀▀═╝ ╚═╝ ╚═╝ ╚═╝
968AKA RECIPROCAL SQUARE ROOT
969*/
970
977
982float jo_rsqrt(float value);
983
984/*
985███████╗ ██████╗ ██╗ ██╗███╗ ██╗████████╗███████╗██████╗ ██████╗ ██████╗
986██╔════╝██╔════╝ ██║ ██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗██╔═══██╗██╔══██╗
987███████╗██║ ███╗██║ ██║██╔██╗ ██║ ██║ █████╗ ██████╔╝██║ ██║██████╔╝
988╚════██║██║ ██║██║ ██║██║╚██╗██║ ██║ ██╔══╝ ██╔══██╗██║ ██║██╔═══╝
989███████║╚██████╔╝███████╗ ██║██║ ╚████║ ██║ ███████╗██║ ██║╚██████╔╝██║
990╚══════╝ ╚═════╝ ╚══════╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝
991
992*/
993
995# define __JO_DEG_TO_ANGLE_MAGIC (182)
996
1002{
1003 // (in fixed numbers, from -32768 to 32767 degrees: deg x 65536 / 360, as jo_fixed_deg2ANGLE())
1004 return ((jo_angle)(((long long)jo_float2fixed(deg) * 11930465LL) >> 32));
1005}
1006
1012{
1013 unsigned int n;
1014 int a;
1015
1016 // deg * 65536 / 360 = deg * 8192 / 45: multiplication by 2^32 / 45 (rounded up) instead of a division.
1017 // Same result as the division for |deg| < 37000. Bigger: a full turn removed first (n * 8192 overflowed from 262144
1018 // degrees, jo_sin(1000000) was wrong; unsigned: -deg overflowed for the smallest int)
1019 n = deg < 0 ? 0u - (unsigned int)deg : (unsigned int)deg;
1020 if (n >= 37000u)
1021 n %= 360u;
1022 a = (int)(((long long)(n * 8192) * 95443718LL) >> 32);
1023 return ((jo_angle)(deg < 0 ? -a : a));
1024}
1025
1026
1032{
1033 // rad * 65536 / (2 pi) without floating point: 65536 / (2 pi) = 683565276 / 2^32 (Q16.16 input)
1034 return ((jo_angle)(((long long)rad * 683565276LL) >> 32));
1035}
1036
1037
1039extern const jo_fixed __jo_sin_table[1025];
1040
1045static __jo_force_inline jo_fixed __jo_sin_index(const unsigned int index)
1046{
1047 if (index < 1024)
1048 return (__jo_sin_table[index]);
1049 if (index < 2048)
1050 return (__jo_sin_table[2048 - index]);
1051 if (index < 3072)
1052 return (-__jo_sin_table[index - 2048]);
1053 return (-__jo_sin_table[4096 - index]);
1054}
1055
1061{
1062 return (((((unsigned short)angle) + 8) >> 4) & 0xfff);
1063}
1064
1071{
1072 return (__jo_sin_index(__jo_angle_to_sin_index(angle)));
1073}
1074
1080{
1081 return (__jo_sin_index((__jo_angle_to_sin_index(angle) + 1024) & 0xfff));
1082}
1083
1090static __jo_force_inline void jo_sincos_angle(const jo_angle angle, jo_fixed * const sin_value, jo_fixed * const cos_value)
1091{
1092 unsigned int index;
1093
1094 index = __jo_angle_to_sin_index(angle);
1095 *sin_value = __jo_sin_index(index);
1096 *cos_value = __jo_sin_index((index + 1024) & 0xfff);
1097}
1098
1104{
1105 // deg * 65536 / 360 without floating point: 1 / 360 = 11930465 / 2^32 (Q16.16 input)
1106 return ((jo_angle)(((long long)deg * 11930465LL) >> 32));
1107}
1108
1109/*
1110███████╗██╗███╗ ██╗██╗ ██╗███████╗
1111██╔════╝██║████╗ ██║██║ ██║██╔════╝
1112███████╗██║██╔██╗ ██║██║ ██║███████╗
1113╚════██║██║██║╚██╗██║██║ ██║╚════██║
1114███████║██║██║ ╚████║╚██████╔╝███████║
1115╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚══════╝
1116*/
1117
1124{
1125 return (jo_sin_angle(jo_fixed_rad2ANGLE(rad)));
1126}
1127
1132static __jo_force_inline jo_fixed jo_sin(const int deg)
1133{
1134 // Table lookup, without floating point
1135 return (jo_sin_angle(jo_DEGtoANG_int(deg)));
1136}
1137
1143static __jo_force_inline float jo_sinf(const int deg)
1144{
1145 return (jo_fixed2float(jo_sin(deg)));
1146}
1147
1154{
1155 return (jo_fixed_sin(jo_float2fixed(rad)));
1156}
1157
1163static __jo_force_inline float jo_sin_radf(const float rad)
1164{
1166}
1167
1174static __jo_force_inline int jo_sin_mult(const int value, const int deg)
1175{
1176 return (jo_fixed_mult(value, jo_sin(deg)));
1177}
1178
1184static __jo_force_inline float jo_sinf_mult(const float value, const int deg)
1185{
1186 return (jo_fixed2float(jo_fixed_mult(jo_float2fixed(value), jo_sin(deg))));
1187}
1188
1189/*
1190 ██████╗ ██████╗ ███████╗██╗███╗ ██╗██╗ ██╗███████╗
1191██╔════╝██╔═══██╗██╔════╝██║████╗ ██║██║ ██║██╔════╝
1192██║ ██║ ██║███████╗██║██╔██╗ ██║██║ ██║███████╗
1193██║ ██║ ██║╚════██║██║██║╚██╗██║██║ ██║╚════██║
1194╚██████╗╚██████╔╝███████║██║██║ ╚████║╚██████╔╝███████║
1195 ╚═════╝ ╚═════╝ ╚══════╝╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚══════╝
1196
1197*/
1198
1205{
1206 return (jo_cos_angle(jo_fixed_rad2ANGLE(rad)));
1207}
1208
1213static __jo_force_inline jo_fixed jo_cos(const int deg)
1214{
1215 // Table lookup, without floating point
1216 return (jo_cos_angle(jo_DEGtoANG_int(deg)));
1217}
1218
1225static __jo_force_inline void jo_sincos(const int deg, jo_fixed * const sin_value, jo_fixed * const cos_value)
1226{
1227 jo_sincos_angle(jo_DEGtoANG_int(deg), sin_value, cos_value);
1228}
1229
1235static __jo_force_inline float jo_cosf(const int deg)
1236{
1237 return (jo_fixed2float(jo_cos(deg)));
1238}
1239
1246{
1247 return (jo_fixed_cos(jo_float2fixed(rad)));
1248}
1249
1255static __jo_force_inline float jo_cos_radf(const float rad)
1256{
1258}
1259
1266static __jo_force_inline int jo_cos_mult(const int value, const int deg)
1267{
1268 return (jo_fixed_mult(value, jo_cos(deg)));
1269}
1270
1276static __jo_force_inline float jo_cosf_mult(const float value, const int deg)
1277{
1278 return (jo_fixed2float(jo_fixed_mult(jo_float2fixed(value), jo_cos(deg))));
1279}
1280
1281/*
1282████████╗ █████╗ ███╗ ██╗ ██████╗ ███████╗███╗ ██╗████████╗
1283╚══██╔══╝██╔══██╗████╗ ██║██╔════╝ ██╔════╝████╗ ██║╚══██╔══╝
1284 ██║ ███████║██╔██╗ ██║██║ ███╗█████╗ ██╔██╗ ██║ ██║
1285 ██║ ██╔══██║██║╚██╗██║██║ ██║██╔══╝ ██║╚██╗██║ ██║
1286 ██║ ██║ ██║██║ ╚████║╚██████╔╝███████╗██║ ╚████║ ██║
1287 ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚══════╝╚═╝ ╚═══╝ ╚═╝
1288
1289*/
1290
1295static __jo_force_inline jo_fixed jo_tan(const int deg)
1296{
1297 jo_fixed sin_value;
1298 jo_fixed cos_value;
1299
1300 jo_sincos(deg, &sin_value, &cos_value);
1301 return jo_fixed_div(sin_value, cos_value);
1302}
1303
1310{
1311 jo_fixed sin_value;
1312 jo_fixed cos_value;
1313
1314 jo_sincos_angle(angle, &sin_value, &cos_value);
1315 if (cos_value == 0)
1316 return (sin_value > 0 ? JO_FIXED_MAX : JO_FIXED_MIN);
1317 return jo_fixed_div(sin_value, cos_value);
1318}
1319
1325static __jo_force_inline float jo_tanf(const float deg)
1326{
1327 return (jo_fixed2float(jo_tan_angle(jo_DEGtoANG(deg))));
1328}
1329
1336{
1337 jo_fixed sin_value;
1338 jo_fixed cos_value;
1339
1340 jo_sincos_angle(jo_fixed_rad2ANGLE(jo_float2fixed(rad)), &sin_value, &cos_value);
1341 return jo_fixed_div(sin_value, cos_value);
1342}
1343
1349static __jo_force_inline float jo_tan_radf(const float rad)
1350{
1352}
1353
1354/*
1355 █████╗ ██████╗ ██████╗ ███████╗
1356██╔══██╗██╔════╝██╔═══██╗██╔════╝
1357███████║██║ ██║ ██║███████╗
1358██╔══██║██║ ██║ ██║╚════██║
1359██║ ██║╚██████╗╚██████╔╝███████║
1360╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝
1361*/
1362
1368
1376static __jo_force_inline float jo_acos_radf(const float angle)
1377{
1379}
1380
1381/*
1382 █████╗ ████████╗ █████╗ ███╗ ██╗██████╗
1383██╔══██╗╚══██╔══╝██╔══██╗████╗ ██║╚════██╗
1384███████║ ██║ ███████║██╔██╗ ██║ █████╔╝
1385██╔══██║ ██║ ██╔══██║██║╚██╗██║██╔═══╝
1386██║ ██║ ██║ ██║ ██║██║ ╚████║███████╗
1387╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝
1388
1389*/
1390
1397float jo_atan2f_rad(const float y, const float x);
1398
1405static __jo_force_inline int jo_atan2f(const float y, const float x)
1406{
1407 // (the angle in degrees, rounded toward 0: 65536 / 360 = 8192 / 45)
1408 return ((int)__jo_atan2_floats(y, x) * 45 / 8192);
1409}
1410
1417static __jo_force_inline jo_angle jo_atan2_angle(const float y, const float x)
1418{
1419 return (__jo_atan2_floats(y, x));
1420}
1421
1431
1440int jo_atan2_degrees(const jo_fixed y, const jo_fixed x);
1441
1448static __jo_force_inline int __jo_wrap_360(const int deg)
1449{
1450 int r;
1451
1452 r = deg - jo_fixed_mult_to_int(deg, 11930465) * 360;
1453 if (r < 0)
1454 r += 360;
1455 else if (r >= 360)
1456 r -= 360;
1457 return (r);
1458}
1459
1466static __jo_force_inline int jo_turn_toward(const int angle, const int target, const int max_step)
1467{
1468 int d;
1469
1470 // Shortest difference: -180 to 179 (the modulo only when the angles are not 0 to 359)
1471 d = target - angle;
1472 if (d >= 360 || d <= -360)
1473 d = __jo_wrap_360(d);
1474 if (d >= 180)
1475 d -= 360;
1476 else if (d < -180)
1477 d += 360;
1478 if (d > max_step)
1479 d = max_step;
1480 else if (d < -max_step)
1481 d = -max_step;
1482 d += angle;
1483 if (d >= 360 || d <= -360)
1484 return (__jo_wrap_360(d));
1485 return (d < 0 ? d + 360 : d);
1486}
1487
1488/*
1489██╗ ██╗███████╗ ██████╗████████╗ ██████╗ ██████╗
1490██║ ██║██╔════╝██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗
1491██║ ██║█████╗ ██║ ██║ ██║ ██║██████╔╝
1492╚██╗ ██╔╝██╔══╝ ██║ ██║ ██║ ██║██╔══██╗
1493 ╚████╔╝ ███████╗╚██████╗ ██║ ╚██████╔╝██║ ██║
1494 ╚═══╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
1495*/
1496
1497/*
1498 _______ __ _ __ __
1499 / ____(_) _____ ____/ / / | / /_ ______ ___ / /_ ___ _____
1500 / /_ / / |/_/ _ \/ __ / / |/ / / / / __ `__ \/ __ \/ _ \/ ___/
1501 / __/ / /> </ __/ /_/ / / /| / /_/ / / / / / / /_/ / __/ /
1502/_/ /_/_/|_|\___/\__,_/ /_/ |_/\__,_/_/ /_/ /_/_.___/\___/_/
1503
1504*/
1505
1515
1522static __jo_force_inline void jo_vector_fixed_add(const jo_vector_fixed * const a, const jo_vector_fixed * const b, jo_vector_fixed * const result)
1523{
1524 result->x = a->x + b->x;
1525 result->y = a->y + b->y;
1526 result->z = a->z + b->z;
1527}
1528
1535static __jo_force_inline void jo_vector_fixed_muls(const jo_vector_fixed * const a, const jo_fixed s, jo_vector_fixed * const result)
1536{
1537 result->x = jo_fixed_mult(a->x, s);
1538 result->y = jo_fixed_mult(a->y, s);
1539 result->z = jo_fixed_mult(a->z, s);
1540}
1541
1548static __jo_force_inline void jo_vector_fixed_sub(const jo_vector_fixed * const a, const jo_vector_fixed * const b, jo_vector_fixed * const result)
1549{
1550 result->x = a->x - b->x;
1551 result->y = a->y - b->y;
1552 result->z = a->z - b->z;
1553}
1554
1562{
1563 return (jo_fixed_dot((const jo_fixed *)a, (const jo_fixed *)b));
1564}
1565
1572static __jo_force_inline void jo_vector_fixed_cross(const jo_vector_fixed * const a, const jo_vector_fixed * const b, jo_vector_fixed * const result)
1573{
1574 result->x = jo_fixed_mult(a->y, b->z) - jo_fixed_mult(a->z, b->y);
1575 result->y = jo_fixed_mult(a->z, b->x) - jo_fixed_mult(a->x, b->z);
1576 result->z = jo_fixed_mult(a->x, b->y) - jo_fixed_mult(a->y, b->x);
1577}
1578
1585
1591void jo_vector_fixed_normalize(const jo_vector_fixed * const a, jo_vector_fixed * const result);
1592
1599static __jo_force_inline void jo_vector4_fixed_add(const jo_vector4_fixed * const a, const jo_vector4_fixed * const b, jo_vector4_fixed * const result)
1600{
1601 result->x = a->x + b->x;
1602 result->y = a->y + b->y;
1603 result->z = a->z + b->z;
1604 result->w = JO_FIXED_1;
1605}
1606
1613static __jo_force_inline void jo_vector4_fixed_sub(const jo_vector4_fixed * const a, const jo_vector4_fixed * const b, jo_vector4_fixed * const result)
1614{
1615 result->x = a->x - b->x;
1616 result->y = a->y - b->y;
1617 result->z = a->z - b->z;
1618 result->w = JO_FIXED_1;
1619}
1620
1626static __jo_force_inline void jo_vector4_fixed_cross(const jo_vector4_fixed * const a, const jo_vector4_fixed * const b, jo_vector4_fixed * const result)
1627{
1628 result->x = jo_fixed_mult(a->y, b->z) - jo_fixed_mult(a->z, b->y);
1629 result->y = jo_fixed_mult(a->z, b->x) - jo_fixed_mult(a->x, b->z);
1630 result->z = jo_fixed_mult(a->x, b->y) - jo_fixed_mult(a->y, b->x);
1631 result->w = JO_FIXED_1;
1632}
1633
1640{
1641 // x, y, z are contiguous (w is ignored)
1642 return (jo_fixed_dot((const jo_fixed *)a, (const jo_fixed *)b));
1643}
1644
1650{
1651 JO_SWAP(a->x, b->x);
1652 JO_SWAP(a->y, b->y);
1653 JO_SWAP(a->z, b->z);
1654 JO_SWAP(a->w, b->w);
1655}
1656
1657/*
1658 ________ __ _ _ __ __
1659 / ____/ /___ ____ _/ /_(_)___ ____ _ / | / /_ ______ ___ / /_ ___ _____
1660 / /_ / / __ \/ __ `/ __/ / __ \/ __ `/ / |/ / / / / __ `__ \/ __ \/ _ \/ ___/
1661 / __/ / / /_/ / /_/ / /_/ / / / / /_/ / / /| / /_/ / / / / / / /_/ / __/ /
1662/_/ /_/\____/\__,_/\__/_/_/ /_/\__, / /_/ |_/\__,_/_/ /_/ /_/_.___/\___/_/
1663 /____/
1664*/
1665
1675
1676/*
1677** The functions of the vectors and of the matrices of floats convert them to fixed numbers and compute with them
1678** (integers only, no float routine). The values must fit in a jo_fixed (-32768 to 32767.99998, a precision of
1679** 1/65536), except the length, the normalization, the dot and cross products, the projection and the angle of vectors:
1680** brought to the same power of 2 first, any size
1681*/
1682
1689void jo_vectorf_add(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result);
1690
1697void jo_vectorf_adds(const jo_vectorf * const a, const float s, jo_vectorf * const result);
1698
1705void jo_vectorf_sub(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result);
1706
1713void jo_vectorf_subs(const jo_vectorf * const a, const float s, jo_vectorf * const result);
1714
1721void jo_vectorf_mul(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result);
1722
1729void jo_vectorf_muls(const jo_vectorf * const a, const float s, jo_vectorf * const result);
1730
1737void jo_vectorf_div(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result);
1738
1745void jo_vectorf_divs(const jo_vectorf * const a, const float s, jo_vectorf * const result);
1746
1751float jo_vectorf_length(const jo_vectorf * const a);
1752
1758float jo_vectorf_dot(const jo_vectorf * const a, const jo_vectorf * const b);
1759
1765void jo_vectorf_normalize(const jo_vectorf * const a, jo_vectorf * const result);
1766
1772void jo_vectorf_proj(const jo_vectorf * const v, const jo_vectorf * const onto, jo_vectorf * const result);
1773
1780void jo_vectorf_cross(const jo_vectorf * const a, const jo_vectorf * const b, jo_vectorf * const result);
1781
1787float jo_vectorf_angle_between_radf(const jo_vectorf * const a, const jo_vectorf * const b);
1788
1789/*
1790██╗ ██╗██╗ ██╗██╗ ██╗ ███╗ ███╗ █████╗ ████████╗██████╗ ██╗██╗ ██╗
1791██║ ██║╚██╗██╔╝██║ ██║ ████╗ ████║██╔══██╗╚══██╔══╝██╔══██╗██║╚██╗██╔╝
1792███████║ ╚███╔╝ ███████║ ██╔████╔██║███████║ ██║ ██████╔╝██║ ╚███╔╝
1793╚════██║ ██╔██╗ ╚════██║ ██║╚██╔╝██║██╔══██║ ██║ ██╔══██╗██║ ██╔██╗
1794 ██║██╔╝ ██╗ ██║ ██║ ╚═╝ ██║██║ ██║ ██║ ██║ ██║██║██╔╝ ██╗
1795 ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═╝
1796
1797*/
1798
1803{
1804 register int i;
1805
1806 for (i = 1; i < 15; ++i)
1807 JO_ZERO(result->table[i]);
1808 result->m00 = JO_FIXED_1;
1809 result->m11 = JO_FIXED_1;
1810 result->m22 = JO_FIXED_1;
1811 result->m33 = JO_FIXED_1;
1812}
1813
1818{
1819 register int i;
1820
1821 for (i = 1; i < 15; ++i)
1822 result->table[i] = 0;
1823 result->m00 = 1;
1824 result->m11 = 1;
1825 result->m22 = 1;
1826 result->m33 = 1;
1827}
1828
1833static __jo_force_inline void jo_matrix_translation(const jo_vector_fixed * const offset, jo_matrix * const result)
1834{
1835 jo_matrix_identity(result);
1836 result->m30 = offset->x;
1837 result->m31 = offset->y;
1838 result->m32 = offset->z;
1839}
1840
1845static __jo_force_inline void jo_matrixf_translation(const jo_vectorf * const offset, jo_matrixf * const result)
1846{
1847 jo_matrixf_identity(result);
1848 result->m30 = offset->x;
1849 result->m31 = offset->y;
1850 result->m32 = offset->z;
1851}
1852
1857static __jo_force_inline void jo_matrix_scaling(const jo_vector_fixed * const scale, jo_matrix * const result)
1858{
1859 jo_matrix_identity(result);
1860 result->m00 = scale->x;
1861 result->m11 = scale->y;
1862 result->m22 = scale->z;
1863}
1864
1869static __jo_force_inline void jo_matrixf_scaling(const jo_vectorf * const scale, jo_matrixf * const result)
1870{
1871 jo_matrixf_identity(result);
1872 result->m00 = scale->x;
1873 result->m11 = scale->y;
1874 result->m22 = scale->z;
1875}
1876
1881static __jo_force_inline void jo_matrix_rotation_x_angle(const jo_angle angle, jo_matrix * const result)
1882{
1883 jo_matrix_identity(result);
1884 jo_sincos_angle(angle, &result->m12, &result->m11);
1885 result->m21 = -result->m12;
1886 result->m22 = result->m11;
1887}
1888
1893static __jo_force_inline void jo_matrix_rotation_x_rad(const float angle_in_rad, jo_matrix * const result)
1894{
1896}
1897
1902static __jo_force_inline void jo_matrixf_rotation_x_rad(const float angle_in_rad, jo_matrixf * const result)
1903{
1904 jo_matrixf_identity(result);
1905 result->m11 = jo_cos_radf(angle_in_rad);
1906 result->m12 = jo_sin_radf(angle_in_rad);
1907 result->m21 = -result->m12;
1908 result->m22 = result->m11;
1909}
1910
1915static __jo_force_inline void jo_matrix_rotation_y_angle(const jo_angle angle, jo_matrix * const result)
1916{
1917 jo_matrix_identity(result);
1918 jo_sincos_angle(angle, &result->m20, &result->m00);
1919 result->m02 = -result->m20;
1920 result->m22 = result->m00;
1921}
1922
1927static __jo_force_inline void jo_matrix_rotation_y_rad(const float angle_in_rad, jo_matrix * const result)
1928{
1930}
1931
1936static __jo_force_inline void jo_matrixf_rotation_y_rad(const float angle_in_rad, jo_matrixf * const result)
1937{
1938 jo_matrixf_identity(result);
1939 result->m00 = jo_cos_radf(angle_in_rad);
1940 result->m20 = jo_sin_radf(angle_in_rad);
1941 result->m02 = -result->m20;
1942 result->m22 = result->m00;
1943}
1944
1949static __jo_force_inline void jo_matrix_rotation_z_angle(const jo_angle angle, jo_matrix * const result)
1950{
1951 jo_matrix_identity(result);
1952 jo_sincos_angle(angle, &result->m01, &result->m00);
1953 result->m10 = -result->m01;
1954 result->m11 = result->m00;
1955}
1956
1961static __jo_force_inline void jo_matrix_rotation_z_rad(const float angle_in_rad, jo_matrix * const result)
1962{
1964}
1965
1970static __jo_force_inline void jo_matrixf_rotation_z_rad(const float angle_in_rad, jo_matrixf * const result)
1971{
1972 jo_matrixf_identity(result);
1973 result->m00 = jo_cos_radf(angle_in_rad);
1974 result->m01 = jo_sin_radf(angle_in_rad);
1975 result->m10 = -result->m01;
1976 result->m11 = result->m00;
1977}
1978
1984static __jo_force_inline void jo_matrix_transpose(const jo_matrix * const matrix, jo_matrix * const result)
1985{
1986 result->m00 = matrix->m00;
1987 result->m10 = matrix->m01;
1988 result->m20 = matrix->m02;
1989 result->m30 = matrix->m03;
1990 result->m01 = matrix->m10;
1991 result->m11 = matrix->m11;
1992 result->m21 = matrix->m12;
1993 result->m31 = matrix->m13;
1994 result->m02 = matrix->m20;
1995 result->m12 = matrix->m21;
1996 result->m22 = matrix->m22;
1997 result->m32 = matrix->m23;
1998 result->m03 = matrix->m30;
1999 result->m13 = matrix->m31;
2000 result->m23 = matrix->m32;
2001 result->m33 = matrix->m33;
2002}
2003
2009static __jo_force_inline void jo_matrixf_transpose(const jo_matrixf * const matrix, jo_matrixf * const result)
2010{
2011 result->m00 = matrix->m00;
2012 result->m10 = matrix->m01;
2013 result->m20 = matrix->m02;
2014 result->m30 = matrix->m03;
2015 result->m01 = matrix->m10;
2016 result->m11 = matrix->m11;
2017 result->m21 = matrix->m12;
2018 result->m31 = matrix->m13;
2019 result->m02 = matrix->m20;
2020 result->m12 = matrix->m21;
2021 result->m22 = matrix->m22;
2022 result->m32 = matrix->m23;
2023 result->m03 = matrix->m30;
2024 result->m13 = matrix->m31;
2025 result->m23 = matrix->m32;
2026 result->m33 = matrix->m33;
2027}
2028
2036{
2037 const jo_fixed *a;
2038 const jo_fixed *b;
2039 jo_fixed high;
2040 jo_fixed low;
2041
2042 a = ptA;
2043 b = ptB;
2044 __asm__ ("clrmac\n\t"
2045 "mac.l @%2+, @%3+\n\t"
2046 "mac.l @%2+, @%3+\n\t"
2047 "mac.l @%2+, @%3+\n\t"
2048 "mac.l @%2+, @%3+\n\t"
2049 "sts mach, %0\n\t"
2050 "sts macl, %1\n\t"
2051 "xtrct %0, %1"
2052 : "=&r" (high), "=&r" (low), "+r" (a), "+r" (b)
2053 : "m" (*(const jo_fixed (*)[4])ptA), "m" (*(const jo_fixed (*)[4])ptB)
2054 : "mach", "macl");
2055 return (low);
2056}
2057
2066static __jo_force_inline void jo_matrix_mul_vector4(const jo_matrix * const m, const jo_vector4_fixed * const v, jo_vector4_fixed * const result)
2067{
2068 jo_fixed columns[4][4];
2069 jo_fixed in[4];
2070 register int i;
2071
2072 // (a copy: v and result can be the same vector)
2073 in[0] = v->x;
2074 in[1] = v->y;
2075 in[2] = v->z;
2076 in[3] = v->w;
2077 for (i = 0; i < 4; ++i)
2078 {
2079 columns[i][0] = m->m[0][i];
2080 columns[i][1] = m->m[1][i];
2081 columns[i][2] = m->m[2][i];
2082 columns[i][3] = m->m[3][i];
2083 }
2084 result->x = __jo_fixed_dot4(columns[0], in);
2085 result->y = __jo_fixed_dot4(columns[1], in);
2086 result->z = __jo_fixed_dot4(columns[2], in);
2087 result->w = __jo_fixed_dot4(columns[3], in);
2088}
2089
2098static __jo_force_inline void jo_matrix_mul(const jo_matrix * const a, const jo_matrix * const b, jo_matrix * const result)
2099{
2100 jo_matrix at;
2101 register int i;
2102 register int j;
2103
2104 // The columns of a become contiguous rows: result->m[i][j] = sum of a->m[k][j] * b->m[i][k]
2105 jo_matrix_transpose(a, &at);
2106 for (i = 0; i < 4; ++i)
2107 for (j = 0; j < 4; ++j)
2108 result->m[i][j] = __jo_fixed_dot4(at.m[j], b->m[i]);
2109}
2110
2117
2118void jo_matrixf_mul(const jo_matrixf * const a, const jo_matrixf * const b, jo_matrixf * const result);
2119
2125void jo_matrixf_rotation(const float angle_in_rad, const jo_vectorf * const axis, jo_matrixf * const result);
2126
2136void jo_matrixf_ortho(const float left, const float right, const float bottom, const float top,
2137 const float back, const float front, jo_matrixf * const result);
2138
2147void jo_matrixf_perspective(const float vertical_field_of_view_in_deg, const float aspect_ratio,
2148 const float near_view_distance, const float far_view_distance, jo_matrixf * const result);
2149
2156void jo_matrixf_look_at(const jo_vectorf * const from, const jo_vectorf * const to,
2157 const jo_vectorf * const up, jo_matrixf * const result);
2158
2163void jo_matrixf_invert_affine(const jo_matrixf * const matrix, jo_matrixf * const result);
2164
2170void jo_matrixf_mul_pos(const jo_matrixf * const matrix, const jo_vectorf * const position,
2171 jo_vectorf * const result);
2172
2178void jo_matrixf_mul_dir(const jo_matrixf * const matrix, const jo_vectorf * const direction,
2179 jo_vectorf * const result);
2180
2181/*
2182███╗ ███╗██╗███████╗ ██████╗
2183████╗ ████║██║██╔════╝██╔════╝
2184██╔████╔██║██║███████╗██║
2185██║╚██╔╝██║██║╚════██║██║
2186██║ ╚═╝ ██║██║███████║╚██████╗
2187╚═╝ ╚═╝╚═╝╚══════╝ ╚═════╝
2188
2189*/
2190
2199static __jo_force_inline jo_fixed jo_lerp(const jo_fixed v0, const jo_fixed v1, const jo_fixed t)
2200{
2201 return (v0 + jo_fixed_mult(t, v1 - v0));
2202}
2203
2209{
2210 // (|f| < 0.00000001f: the bits of 2 positive floats are in the same order as the floats)
2211 return ((__jo_float_bits(f) & 0x7FFFFFFFu) < 0x322BCC77u);
2212}
2213
2225static __jo_force_inline bool jo_square_intersect(const int x1, const int y1, const int w1, const int h1,
2226 const int x2, const int y2, const int w2, const int h2)
2227{
2228 return ((x1 < x2 + w2) && (x2 < x1 + w1)) && ((y1 < y2 + h2) && (y2 < y1 + h1));
2229}
2230
2237int jo_gcd(int a, int b);
2238
2246void jo_planar_rotate(const jo_pos2D * const point, const jo_pos2D * const origin, const int angle, jo_pos2D * const result);
2247
2253{
2254 switch (direction)
2255 {
2256 case LEFT:
2257 return (180);
2258 case RIGHT:
2259 return (0);
2260 case UP:
2261 return (270);
2262 case DOWN:
2263 return (90);
2264 case UP_LEFT:
2265 return (225);
2266 case UP_RIGHT:
2267 return (315);
2268 case DOWN_LEFT:
2269 return (135);
2270 case DOWN_RIGHT:
2271 return (45);
2272 default:
2273 return (0);
2274 }
2275}
2276
2277#endif /* !__JO_MATH_H__ */
2278
2279/*
2280** END OF FILE
2281*/
2282
jo_pos3D_fixed position
Definition entity.h:2
jo_fixed jo_vector_fixed_length(const jo_vector_fixed *const a)
Length of a vector (using fixed numbers).
void jo_vectorf_sub(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Subtract 2 vectors (using floating numbers).
static __jo_force_inline void jo_matrixf_transpose(const jo_matrixf *const matrix, jo_matrixf *const result)
Creates transpose matrix (using floating numbers).
Definition math.h:2009
static __jo_force_inline jo_fixed jo_int2fixed(const int x)
Convert int to jo engine fixed.
Definition math.h:492
static __jo_force_inline void jo_matrix_rotation_y_rad(const float angle_in_rad, jo_matrix *const result)
Rotation matrix around the Y axis (radian, using floating numbers: converted, then jo_matrix_rotation...
Definition math.h:1927
static __jo_force_inline jo_angle jo_DEGtoANG(const float deg)
Convert a degree to jo_angle using floating number.
Definition math.h:1001
static __jo_force_inline void jo_matrix_mul(const jo_matrix *const a, const jo_matrix *const b, jo_matrix *const result)
Multiply 2 matrix (using fixed numbers).
Definition math.h:2098
static __jo_force_inline float __jo_float_from_bits(const unsigned int i)
A float from its bits (Internal usage).
Definition math.h:532
static __jo_force_inline void jo_matrixf_rotation_z_rad(const float angle_in_rad, jo_matrixf *const result)
Creates rotating matrix (Z axis) (using floating numbers).
Definition math.h:1970
static __jo_force_inline void jo_vector4_fixed_cross(const jo_vector4_fixed *const a, const jo_vector4_fixed *const b, jo_vector4_fixed *const result)
Cross product of 2 vectors4 (using fixed numbers).
Definition math.h:1626
void jo_vectorf_cross(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Cross 2 vectors (using floating numbers).
static __jo_force_inline void jo_matrix_rotation_z_rad(const float angle_in_rad, jo_matrix *const result)
Rotation matrix around the Z axis (radian, using floating numbers: converted, then jo_matrix_rotation...
Definition math.h:1961
void jo_planar_rotate(const jo_pos2D *const point, const jo_pos2D *const origin, const int angle, jo_pos2D *const result)
Rotate a point on the plan with a specific origin.
static __jo_force_inline jo_angle jo_DEGtoANG_int(const int deg)
Convert a degree to jo_angle using interger.
Definition math.h:1011
void jo_vector_fixed_compute_bezier_point(const jo_fixed t, jo_vector_fixed p0, jo_vector_fixed p1, jo_vector_fixed p2, jo_vector_fixed p3, jo_vector_fixed *result)
Compute cubic bezier curve point for vectors (using fixed numbers).
static __jo_force_inline jo_fixed jo_sin_rad(const float rad)
Sinus computation.
Definition math.h:1153
static __jo_force_inline jo_fixed __jo_sin_index(const unsigned int index)
Sinus of a table index (Internal usage).
Definition math.h:1045
static __jo_force_inline int __jo_wrap_360(const int deg)
An angle in degree brought in 0 to 359 (Internal usage).
Definition math.h:1448
float __jo_fixed_to_float(const jo_fixed x)
Convert jo engine fixed to float at run time (Internal usage, see jo_fixed2float()).
float jo_vectorf_dot(const jo_vectorf *const a, const jo_vectorf *const b)
Get the dot product of 2 vectors (using floating numbers).
unsigned int jo_sqrt(unsigned int value)
Fast square root.
jo_angle jo_atan2_fixed(const jo_fixed y, const jo_fixed x)
ATAN2 as a jo_angle without floating point (fixed numbers).
static __jo_force_inline jo_fixed jo_fixed_div_result(void)
Get the result of the division started by jo_fixed_div_start().
Definition math.h:796
static __jo_force_inline void jo_matrixf_translation(const jo_vectorf *const offset, jo_matrixf *const result)
Creates translation matrix (using floating numbers).
Definition math.h:1845
static __jo_force_inline float jo_cosf_mult(const float value, const int deg)
Fast cosinus multiplication.
Definition math.h:1276
void jo_vectorf_compute_bezier_point(const float t, jo_vectorf p0, jo_vectorf p1, jo_vectorf p2, jo_vectorf p3, jo_vectorf *result)
Compute cubic bezier curve point for vectors (using floating numbers).
void jo_vectorf_subs(const jo_vectorf *const a, const float s, jo_vectorf *const result)
Subtract value to vector (using floating numbers).
float jo_rsqrt(float value)
Fast Reciprocal Square root using floating number.
#define JO_FIXED_PI_DIV_180
Fixed value of PI/180.
Definition math.h:451
void jo_vector_fixed_normalize(const jo_vector_fixed *const a, jo_vector_fixed *const result)
Normalize a vector (using fixed numbers).
static __jo_force_inline jo_fixed __jo_angle_to_fixed_rad(const jo_angle angle)
Convert jo_angle to fixed radian (Internal usage).
Definition math.h:622
void jo_vectorf_adds(const jo_vectorf *const a, const float s, jo_vectorf *const result)
Add value to vector (using floating numbers).
static __jo_force_inline jo_fixed __jo_fixed_dot4(const jo_fixed ptA[4], const jo_fixed ptB[4])
Dot product of 2 vectors of 4 fixed numbers (Internal usage).
Definition math.h:2035
static __jo_force_inline float jo_sin_radf(const float rad)
Sinus computation.
Definition math.h:1163
static __jo_force_inline void jo_matrixf_rotation_y_rad(const float angle_in_rad, jo_matrixf *const result)
Creates rotating matrix (Y axis) (using floating numbers).
Definition math.h:1936
float jo_vectorf_length(const jo_vectorf *const a)
Get the length of a vector (using floating numbers).
static __jo_force_inline float jo_sinf(const int deg)
Sinus computation.
Definition math.h:1143
int jo_random(int max)
Get a random number.
static __jo_force_inline int jo_atan2f(const float y, const float x)
Fast ATAN2 computation in degree.
Definition math.h:1405
static __jo_force_inline void jo_matrix_mul_vector4(const jo_matrix *const m, const jo_vector4_fixed *const v, jo_vector4_fixed *const result)
Multiply a matrix by a vector4 (using fixed numbers).
Definition math.h:2066
static __jo_force_inline int jo_random_seeded(unsigned int *const seed)
Get a random number of a sequence of its own (the effects of a game: the same at each run,...
Definition math.h:877
static __jo_force_inline void jo_vector_fixed_sub(const jo_vector_fixed *const a, const jo_vector_fixed *const b, jo_vector_fixed *const result)
Substract 2 vectors (using fixed numbers).
Definition math.h:1548
static __jo_force_inline unsigned int __jo_float_bits(const float f)
The bits of a float (Internal usage).
Definition math.h:516
static __jo_force_inline jo_fixed jo_fixed_floor(const jo_fixed x)
Returns the largest (fixed) integer value less than or equal to x.
Definition math.h:832
static __jo_force_inline void jo_matrix_translation(const jo_vector_fixed *const offset, jo_matrix *const result)
Creates translation matrix (using fixed numbers).
Definition math.h:1833
static __jo_force_inline jo_fixed jo_fixed_dot(const jo_fixed ptA[3], const jo_fixed ptB[3])
Dot product two fixed 3D points.
Definition math.h:701
int jo_gcd(int a, int b)
Get the greatest common divisor.
static __jo_force_inline int __jo_float_sign(const float x)
The sign of a float, in integers on its bits (Internal usage).
Definition math.h:591
static __jo_force_inline void jo_vector_fixed_cross(const jo_vector_fixed *const a, const jo_vector_fixed *const b, jo_vector_fixed *const result)
Cross product of 2 vectors (using fixed numbers).
Definition math.h:1572
static __jo_force_inline unsigned int jo_highest_bit(unsigned int x)
Index of the highest bit set in a value (31 - number of leading zeros).
Definition math.h:897
static __jo_force_inline void jo_sincos(const int deg, jo_fixed *const sin_value, jo_fixed *const cos_value)
Sinus and cosinus of the same angle in degree (table lookup, fast).
Definition math.h:1225
void jo_matrixf_invert_affine(const jo_matrixf *const matrix, jo_matrixf *const result)
Inverts an affine transformation matrix (using floating numbers).
static __jo_force_inline void jo_sincos_angle(const jo_angle angle, jo_fixed *const sin_value, jo_fixed *const cos_value)
Sinus and cosinus of the same angle (table lookup, fast).
Definition math.h:1090
static __jo_force_inline void jo_matrixf_scaling(const jo_vectorf *const scale, jo_matrixf *const result)
Creates scaling matrix (using floating numbers).
Definition math.h:1869
const jo_fixed __jo_sin_table[1025]
Sinus table: sin(x) for x in [0, pi/2] (1024 steps, Q16.16) (Internal usage, see math....
static __jo_force_inline jo_fixed jo_cos_angle(const jo_angle angle)
Cosinus of an angle (table lookup, fast).
Definition math.h:1079
static __jo_force_inline int jo_turn_toward(const int angle, const int target, const int max_step)
Turn an angle toward another angle, by the shortest way.
Definition math.h:1466
static __jo_force_inline int jo_fixed_mult_to_int(const jo_fixed x, const jo_fixed y)
Multiply to fixed number and keep the integer part: jo_fixed2int(jo_fixed_mult(x, y)),...
Definition math.h:682
float jo_vectorf_angle_between_radf(const jo_vectorf *const a, const jo_vectorf *const b)
Get the angle between 2 vectors (using floating numbers).
static __jo_force_inline jo_angle jo_atan2_angle(const float y, const float x)
ATAN2 as a jo_angle (65536 per turn, like jo_sin_angle() and the rotations of the 3D).
Definition math.h:1417
void jo_matrixf_mul_dir(const jo_matrixf *const matrix, const jo_vectorf *const direction, jo_vectorf *const result)
Multiplies a 4x4 matrix with a 3D vector representing a direction in 3D space (using floating numbers...
void jo_vectorf_div(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Divide 2 vectors (using floating numbers).
static __jo_force_inline void jo_matrix_rotation_z_angle(const jo_angle angle, jo_matrix *const result)
Rotation matrix around the Z axis (using fixed numbers).
Definition math.h:1949
static __jo_force_inline float jo_tan_radf(const float rad)
Tangent computation.
Definition math.h:1349
static __jo_force_inline jo_fixed jo_sin_angle(const jo_angle angle)
Sinus of an angle (table lookup, fast).
Definition math.h:1070
void jo_vectorf_mul(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Multiply 2 vectors (using floating numbers).
static __jo_force_inline jo_fixed jo_tan(const int deg)
Fast tangent computation.
Definition math.h:1295
static __jo_force_inline void jo_vector4_fixed_sub(const jo_vector4_fixed *const a, const jo_vector4_fixed *const b, jo_vector4_fixed *const result)
Substract 2 vectors4 (using fixed numbers).
Definition math.h:1613
void jo_vectorf_add(const jo_vectorf *const a, const jo_vectorf *const b, jo_vectorf *const result)
Add 2 vectors (using floating numbers).
static __jo_force_inline void jo_vector4_fixed_add(const jo_vector4_fixed *const a, const jo_vector4_fixed *const b, jo_vector4_fixed *const result)
Add 2 vectors4 (using fixed numbers).
Definition math.h:1599
static __jo_force_inline jo_angle jo_fixed_deg2ANGLE(const jo_fixed deg)
Convert fixed degree to jo_angle.
Definition math.h:1103
void jo_vectorf_muls(const jo_vectorf *const a, const float s, jo_vectorf *const result)
Multiply value to vector (using floating numbers).
#define JO_SWAP(A, B)
Swap A and B values.
Definition math.h:324
#define JO_FIXED_TO_INT(X)
Convert jo_fixed to int.
Definition math.h:468
static __jo_force_inline bool jo_square_intersect(const int x1, const int y1, const int w1, const int h1, const int x2, const int y2, const int w2, const int h2)
Fast method to get if two square intersects (HitBox processing).
Definition math.h:2225
int jo_random_seed
Get or set current random seed.
static __jo_force_inline void jo_matrix_rotation_x_angle(const jo_angle angle, jo_matrix *const result)
Rotation matrix around the X axis (using fixed numbers).
Definition math.h:1881
static __jo_force_inline float jo_fixed2float(const jo_fixed x)
Convert jo engine fixed to float.
Definition math.h:573
static __jo_force_inline float jo_acos_radf(const float angle)
Fast Arc Cosinus computation.
Definition math.h:1376
static __jo_force_inline void jo_vector_fixed_add(const jo_vector_fixed *const a, const jo_vector_fixed *const b, jo_vector_fixed *const result)
Add 2 vectors (using fixed numbers).
Definition math.h:1522
static __jo_force_inline int jo_shift_right8_small(const int x)
Arithmetic right shift by 8 bits (x >> 8) of a value in [-2^23, 2^23) (a 16 bits value x 0 to 256 for...
Definition math.h:774
static __jo_force_inline jo_fixed jo_lerp(const jo_fixed v0, const jo_fixed v1, const jo_fixed t)
Linear interpolation which guarantees v = v1 when t = 1.
Definition math.h:2199
static __jo_force_inline void jo_vector_fixed_muls(const jo_vector_fixed *const a, const jo_fixed s, jo_vector_fixed *const result)
Multiply value to vector (using fixed numbers).
Definition math.h:1535
static __jo_force_inline int jo_shift_right8(const int x)
Arithmetic right shift by 8 bits (x >> 8, rounded toward minus infinity) of any value.
Definition math.h:763
static __jo_force_inline float jo_sinf_mult(const float value, const int deg)
Fast sinus multiplication.
Definition math.h:1184
static __jo_force_inline short jo_direction_to_angle(const jo_8_directions direction)
Convert jo_8_directions to angle in degree.
Definition math.h:2252
jo_fixed jo_fixed_rsqrt(jo_fixed value)
Fast Reciprocal Square root using fixed number.
static __jo_force_inline float jo_cos_radf(const float rad)
Cosinus computation.
Definition math.h:1255
static __jo_force_inline void jo_matrix_rotation_y_angle(const jo_angle angle, jo_matrix *const result)
Rotation matrix around the Y axis (using fixed numbers).
Definition math.h:1915
jo_fixed jo_fixed_pow(jo_fixed x, jo_fixed y)
x raised to the power of the integer part of y
static __jo_force_inline jo_fixed jo_tan_rad(const float rad)
Tangent computation.
Definition math.h:1335
static __jo_force_inline jo_fixed jo_fixed_rad2deg(const jo_fixed rad)
Convert fixed radian to fixed degree.
Definition math.h:814
void jo_matrixf_mul_pos(const jo_matrixf *const matrix, const jo_vectorf *const position, jo_vectorf *const result)
Multiplies a 4x4 matrix with a 3D vector representing a point in 3D space (using floating numbers).
static __jo_force_inline jo_fixed jo_float2fixed(const float x)
Convert float to jo engine fixed (avoid usage of GCC Soft Float).
Definition math.h:561
static __jo_force_inline int jo_random_using_multiple(int max, int multiple)
Get a random number with a specific multiple.
Definition math.h:864
static __jo_force_inline void jo_vector4_swap(jo_vector4_fixed *const a, jo_vector4_fixed *const b)
Swap 2 vectors4 (using fixed numbers).
Definition math.h:1649
static __jo_force_inline int jo_fixed2int(const jo_fixed x)
Convert jo engine fixed to int.
Definition math.h:501
jo_fixed jo_fixed_sqrt(jo_fixed value)
Fast Square root using fixed number.
void jo_matrixf_mul(const jo_matrixf *const a, const jo_matrixf *const b, jo_matrixf *const result)
Multiply 2 matrix (using floating numbers).
void jo_matrixf_ortho(const float left, const float right, const float bottom, const float top, const float back, const float front, jo_matrixf *const result)
Creates an orthographic projection matrix (using floating numbers).
static __jo_force_inline float jo_cosf(const int deg)
Cosinus computation.
Definition math.h:1235
#define JO_FIXED_360
Fixed floating point value for 360.
Definition math.h:434
static __jo_force_inline jo_fixed jo_cos_rad(const float rad)
Cosinus computation.
Definition math.h:1245
static __jo_force_inline void jo_matrix_scaling(const jo_vector_fixed *const scale, jo_matrix *const result)
Creates scaling matrix (using fixed numbers).
Definition math.h:1857
static __jo_force_inline jo_fixed jo_fixed_wrap_to_180(jo_fixed deg)
Wrap deg in [-180 180].
Definition math.h:647
static __jo_force_inline void jo_matrix_transpose(const jo_matrix *const matrix, jo_matrix *const result)
Creates transpose matrix (using fixed numbers).
Definition math.h:1984
#define JO_FIXED_MIN
Fixed floating point value for -32767.99998.
Definition math.h:437
static __jo_force_inline void jo_fixed_div_start(const jo_fixed dividend, const jo_fixed divisor)
Start a fixed-point division in the background (see jo_fixed_div_result()).
Definition math.h:785
static __jo_force_inline jo_fixed jo_fixed_cos(const jo_fixed rad)
Fast cosinus computation using fixed number.
Definition math.h:1204
int jo_atan2_degrees(const jo_fixed y, const jo_fixed x)
ATAN2 in degree without floating point (fixed numbers).
#define JO_INT_TO_FIXED(X)
Convert int to jo_fixed.
Definition math.h:464
#define JO_FIXED_MAX
Fixed floating point value for +32767.99998.
Definition math.h:439
static __jo_force_inline jo_fixed jo_fixed_sin(const jo_fixed rad)
Fast sinus computation using fixed number.
Definition math.h:1123
static __jo_force_inline jo_fixed jo_tan_angle(const jo_angle angle)
Fast tangent of a jo_angle (table lookup and the division unit).
Definition math.h:1309
float __jo_float_scale2(const float x, const int shift)
A float times 2^shift, in integers on its bits (Internal usage).
void jo_vectorf_proj(const jo_vectorf *const v, const jo_vectorf *const onto, jo_vectorf *const result)
Compute projection vector (using floating numbers).
static __jo_force_inline jo_fixed jo_fixed_mult(const jo_fixed x, const jo_fixed y)
Multiply to fixed number.
Definition math.h:671
#define JO_FIXED_180
Fixed floating point value for 180.
Definition math.h:432
static __jo_force_inline int jo_int_div(const int dividend, const int divisor)
Divide 2 integers (dividend / divisor, rounded toward 0 as in C).
Definition math.h:748
static __jo_force_inline void jo_matrixf_rotation_x_rad(const float angle_in_rad, jo_matrixf *const result)
Creates rotating matrix (X axis) (using floating numbers).
Definition math.h:1902
#define JO_FIXED_180_DIV_PI
Fixed value of 180/PI.
Definition math.h:449
static __jo_force_inline void jo_matrixf_identity(jo_matrixf *const result)
Creates the identity matrix (using floating numbers).
Definition math.h:1817
static __jo_force_inline unsigned int __jo_angle_to_sin_index(const jo_angle angle)
Table index of an angle (Internal usage).
Definition math.h:1060
static __jo_force_inline jo_fixed jo_vector4_fixed_dot(const jo_vector4_fixed *const a, const jo_vector4_fixed *const b)
Dot product of 2 vectors4 (using fixed numbers).
Definition math.h:1639
static __jo_force_inline jo_angle jo_fixed_rad2ANGLE(const jo_fixed rad)
Convert fixed radian to jo_angle.
Definition math.h:1031
#define JO_FIXED_PI
Fixed value of PI.
Definition math.h:445
static __jo_force_inline jo_fixed jo_fixed_ceil(const jo_fixed x)
Returns the smallest (fixed) integer value greater than or equal to x.
Definition math.h:823
static __jo_force_inline jo_fixed jo_fixed_deg2rad(const jo_fixed deg)
Convert fixed degree to fixed radian.
Definition math.h:805
jo_angle __jo_atan2_floats(const float y, const float x)
The angle of a vector of 2 floats (Internal usage, see jo_atan2f_rad()).
void jo_vectorf_normalize(const jo_vectorf *const a, jo_vectorf *const result)
Normalize a vector (using floating numbers).
void jo_matrixf_rotation(const float angle_in_rad, const jo_vectorf *const axis, jo_matrixf *const result)
Creates a matrix to rotate around an axis by a given angle in radiant (using floating numbers).
float jo_atan2f_rad(const float y, const float x)
Fast ATAN2 computation in radian.
static __jo_force_inline jo_fixed jo_fixed_wrap_to_pi(jo_fixed rad)
Wrap rad in [−pi pi].
Definition math.h:632
static __jo_force_inline bool jo_is_float_equals_zero(const float f)
Check if float almost equals 0.
Definition math.h:2208
#define JO_FIXED_PI_2
Fixed value of 2 PI.
Definition math.h:447
static __jo_force_inline jo_fixed jo_sin(const int deg)
Fast sinus computation.
Definition math.h:1132
static __jo_force_inline int jo_cos_mult(const int value, const int deg)
Fast cosinus multiplication.
Definition math.h:1266
static __jo_force_inline jo_fixed jo_cos(const int deg)
Fast cosinus computation.
Definition math.h:1213
void jo_matrixf_perspective(const float vertical_field_of_view_in_deg, const float aspect_ratio, const float near_view_distance, const float far_view_distance, jo_matrixf *const result)
Creates a perspective projection matrix for a camera (using floating numbers).
void jo_vectorf_divs(const jo_vectorf *const a, const float s, jo_vectorf *const result)
Divide value to vector (using floating numbers).
void jo_matrixf_look_at(const jo_vectorf *const from, const jo_vectorf *const to, const jo_vectorf *const up, jo_matrixf *const result)
Builds a transformation matrix for a camera (using floating numbers).
int __jo_floats_to_fixed_scaled(const float *const in, jo_fixed *const out, const int count, const int top)
Convert floats to fixed numbers with the same power of 2 for all, in integers (Internal usage).
static __jo_force_inline void jo_matrix_rotation_x_rad(const float angle_in_rad, jo_matrix *const result)
Rotation matrix around the X axis (radian, using floating numbers: converted, then jo_matrix_rotation...
Definition math.h:1893
static __jo_force_inline int jo_sin_mult(const int value, const int deg)
Fast sinus multiplication.
Definition math.h:1174
jo_fixed __jo_float_to_fixed(const float x)
Convert float to jo engine fixed at run time (Internal usage, see jo_float2fixed()).
#define JO_FIXED_1_DIV
Fixed value of 1/65536.
Definition math.h:459
static __jo_force_inline float jo_sqrtf(float value)
Fast Square root using floating number.
Definition math.h:946
static __jo_force_inline void jo_matrix_identity(jo_matrix *const result)
Creates the identity matrix (using fixed numbers).
Definition math.h:1802
#define JO_FIXED_1
Fixed floating point value for 1.
Definition math.h:414
static __jo_force_inline float jo_tanf(const float deg)
Tangent computation.
Definition math.h:1325
static __jo_force_inline jo_fixed jo_fixed_div(const jo_fixed dividend, const jo_fixed divisor)
Divide fixed-point numbers (expresses dividend / divisor).
Definition math.h:730
jo_fixed __jo_acos_fixed_rad(jo_fixed cosinus)
Arc cosinus in fixed numbers (Internal usage, see jo_acos_radf()).
static __jo_force_inline jo_fixed jo_vector_fixed_dot(const jo_vector_fixed *const a, const jo_vector_fixed *const b)
Dot product of 2 vectors (using fixed numbers).
Definition math.h:1561
#define JO_ZERO(X)
Set a variable to zero.
Definition math.h:240
#define JO_SH2_DVDNTL
Division unit: dividend register low (64 bits division, writing starts the division,...
Definition sega_saturn.h:629
#define JO_SH2_DVDNT
Division unit: dividend register (32 bits division).
Definition sega_saturn.h:625
#define JO_SH2_DVSR
Division unit: divisor register.
Definition sega_saturn.h:623
#define JO_SH2_DVDNTH
Division unit: dividend register high (64 bits division).
Definition sega_saturn.h:627
float y
Definition types.h:150
jo_fixed w
Definition types.h:183
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39
short jo_angle
Angle.
Definition types.h:65
float x
Definition types.h:149
float z
Definition types.h:151
int jo_fixed
Fixed point Q16.16 number.
Definition types.h:59
jo_8_directions
8 directions definition
Definition types.h:326
@ DOWN_LEFT
Definition types.h:334
@ DOWN_RIGHT
Definition types.h:335
@ UP_RIGHT
Definition types.h:333
@ DOWN
Definition types.h:331
@ UP_LEFT
Definition types.h:332
@ UP
Definition types.h:330
@ LEFT
Definition types.h:328
@ RIGHT
Definition types.h:329
2D position
Definition types.h:69
Vector4 for 3D computation using fixed number.
Definition types.h:181
Vector for 3D computation using fixed number.
Definition types.h:166
Vector for 3D computation using floating numbers.
Definition types.h:148
4x4 MATRIX for 3D computation using fixed number
Definition types.h:202
jo_fixed table[16]
Definition types.h:204
jo_fixed m[4][4]
Definition types.h:203
4x4 MATRIX for 3D computation using floating numbers
Definition types.h:188
float table[16]
Definition types.h:190