sgdk
Loading...
Searching...
No Matches
maths.h
Go to the documentation of this file.
1
11#ifndef _MATHS_H_
12#define _MATHS_H_
13
14
15extern const fix32 sintab_f32[1024];
16extern const fix16 sintab_f16[1024];
17
18extern const fix16 log2tab_f16[0x10000];
19extern const fix16 log10tab_f16[0x10000];
20extern const fix16 sqrttab_f16[0x10000];
21
22
27#define min(X, Y) (((X) < (Y))?(X):(Y))
28
33#define max(X, Y) (((X) > (Y))?(X):(Y))
34
39#define clamp(X, L, H) (min(max((X), (L)), (H)))
40
41#if (ENABLE_NEWLIB == 0)
46#define abs(X) (((X) < 0)?-(X):(X))
47#endif // ENABLE_NEWLIB
48
49#ifndef PI
54#define PI 3.14159265358979323846
55#endif
56
57
58#define FIX16_INT_BITS 10
59#define FIX16_FRAC_BITS (16 - FIX16_INT_BITS)
60
61#define FIX16_INT_MASK (((1 << FIX16_INT_BITS) - 1) << FIX16_FRAC_BITS)
62#define FIX16_FRAC_MASK ((1 << FIX16_FRAC_BITS) - 1)
63
64
65#define FIX32_INT_BITS 22
66#define FIX32_FRAC_BITS (32 - FIX32_INT_BITS)
67
68#define FIX32_INT_MASK (((1 << FIX32_INT_BITS) - 1) << FIX32_FRAC_BITS)
69#define FIX32_FRAC_MASK ((1 << FIX32_FRAC_BITS) - 1)
70
71
72#define FASTFIX16_INT_BITS 8
73#define FASTFIX16_FRAC_BITS (16 - FASTFIX16_INT_BITS)
74
75#define FASTFIX16_INT_MASK (((1 << FASTFIX16_INT_BITS) - 1) << FASTFIX16_FRAC_BITS)
76#define FASTFIX16_FRAC_MASK ((1 << FASTFIX16_FRAC_BITS) - 1)
77
78
79#define FASTFIX32_INT_BITS 16
80#define FASTFIX32_FRAC_BITS (32 - FASTFIX32_INT_BITS)
81
82#define FASTFIX32_INT_MASK (((1 << FASTFIX32_INT_BITS) - 1) << FASTFIX32_FRAC_BITS)
83#define FASTFIX32_FRAC_MASK ((1 << FASTFIX32_FRAC_BITS) - 1)
84
85
93#define FIX16(value) ((fix16) ((value) * (1 << FIX16_FRAC_BITS)))
94
102#define FIX32(value) ((fix32) ((value) * (1 << FIX32_FRAC_BITS)))
103
111#define FASTFIX16(value) ((fastfix16) ((value) * (1 << FASTFIX16_FRAC_BITS)))
112
120#define FASTFIX32(value) ((fastfix32) ((value) * (1 << FASTFIX32_FRAC_BITS)))
121
122
123// 2D STUFF
124
129typedef struct
130{
131 u16 x;
132 u16 y;
133} Vect2D_u16;
134
139typedef struct
140{
141 s16 x;
142 s16 y;
143} Vect2D_s16;
144
149typedef struct
150{
151 u32 x;
152 u32 y;
153} Vect2D_u32;
154
159typedef struct
160{
161 s32 x;
162 s32 y;
163} Vect2D_s32;
164
169typedef struct
170{
171 fix16 x;
172 fix16 y;
173} Vect2D_f16;
174
179typedef struct
180{
181 fix32 x;
182 fix32 y;
183} Vect2D_f32;
184
190typedef struct
191{
192 Vect2D_f16 a;
193 Vect2D_f16 b;
194} Mat2D_f16;
195
201typedef struct
202{
203 Vect2D_f32 a;
204 Vect2D_f32 b;
205} Mat2D_f32;
206
207
208// 3D STUFF
209
214typedef struct
215{
216 u16 x;
217 u16 y;
218 u16 z;
219} Vect3D_u16;
220
225typedef struct
226{
227 s16 x;
228 s16 y;
229 s16 z;
230} Vect3D_s16;
231
236typedef struct
237{
238 u32 x;
239 u32 y;
240 u32 z;
241} Vect3D_u32;
242
247typedef struct
248{
249 s32 x;
250 s32 y;
251 s32 z;
252} Vect3D_s32;
253
258typedef struct
259{
260 fix16 x;
261 fix16 y;
262 fix16 z;
263} Vect3D_f16;
264
269typedef struct
270{
271 fix32 x;
272 fix32 y;
273 fix32 z;
274} Vect3D_f32;
275
281typedef struct
282{
283 Vect3D_f16 a;
284 Vect3D_f16 b;
285 Vect3D_f16 c;
286} Mat3D_f16;
287
293typedef struct
294{
295 Vect3D_f32 a;
296 Vect3D_f32 b;
297 Vect3D_f32 c;
298} Mat3D_f32;
299
300
301// 4D STUFF
302
307typedef struct
308{
309 fix16 x;
310 fix16 y;
311 fix16 z;
312 fix16 w;
313} Vect4D_f16;
314
319typedef struct
320{
321 fix32 x;
322 fix32 y;
323 fix32 z;
324 fix32 w;
325} Vect4D_f32;
326
332typedef struct
333{
334 Vect4D_f16 a;
335 Vect4D_f16 b;
336 Vect4D_f16 c;
337 Vect4D_f16 d;
338} Mat4D_f16;
339
345typedef struct
346{
347 Vect4D_f32 a;
348 Vect4D_f32 b;
349 Vect4D_f32 c;
350 Vect4D_f32 d;
351} Mat4D_f32;
352
353
354// short alias
355
380
405
414
439
440
451u32 mulu(u16 op1, u16 op2);
462s32 muls(s16 op1, s16 op2);
474u16 divu(u32 op1, u16 op2);
486s16 divs(s32 op1, s16 op2);
497u16 modu(u32 op1, u16 op2);
508s16 mods(s32 op1, s16 op2);
509
522u32 divmodu(u32 op1, u16 op2);
535s32 divmods(s32 op1, s16 op2);
536
537
542fix16 intToFix16(s16 value);
547s16 fix16ToInt(fix16 value);
553
558fix16 fix16Round(fix16 value);
568fix16 fix16Frac(fix16 value);
573fix16 fix16Int(fix16 value);
574
575#define fix16Add(a, b) _Pragma("GCC error \"This method is deprecated, simply use '+' operator to add fix16 values together.\"")
576#define fix16Sub(a, b) _Pragma("GCC error \"This method is deprecated, simply use '-' operator to subtract fix16 values.\"")
577#define fix16Neg(a) _Pragma("GCC error \"This method is deprecated, simply use '0 - value' to get the negative fix16 value.\"")
578
583fix16 fix16Mul(fix16 val1, fix16 val2);
588fix16 fix16Div(fix16 val1, fix16 val2);
593fix16 fix16Avg(fix16 val1, fix16 val2);
594
599fix16 fix16Log2(fix16 value);
604fix16 fix16Log10(fix16 value);
609fix16 fix16Sqrt(fix16 value);
610
616fix16 sinFix16(u16 value);
622fix16 cosFix16(u16 value);
623
624
629fix32 intToFix32(s32 value);
634s32 fix32ToInt(fix32 value);
640
645fix32 fix32Round(fix32 value);
655fix32 fix32Frac(fix32 value);
660fix32 fix32Int(fix32 value);
661
662#define fix32Add(a, b) _Pragma("GCC error \"This method is deprecated, simply use '+' operator to add fix32 values together.\"")
663#define fix32Sub(a, b) _Pragma("GCC error \"This method is deprecated, simply use '-' operator to subtract fix32 values.\"")
664#define fix32Neg(a) _Pragma("GCC error \"This method is deprecated, simply use '0 - value' to get the negative fix32 value.\"")
665
671fix32 fix32Mul(fix32 val1, fix32 val2);
677fix32 fix32Div(fix32 val1, fix32 val2);
682fix32 fix32Avg(fix32 val1, fix32 val2);
683
689fix32 sinFix32(u16 value);
695fix32 cosFix32(u16 value);
696
697
698
699
700
711
732
743
744
755
776
789
790
791
792
793
794
802u32 intToBCD(u32 value);
803
808#define distance_approx(dx, dy) _Pragma("GCC error \"This method is deprecated, use getApproximatedDistance(..) instead.\"")
809
851u16 getLog2Int(u32 value);
852
863u32 getNextPow2(u32 value);
864
865
866#endif // _MATHS_H_
fix16 sinFix16(u16 value)
Compute sinus of specified value and return it as fix16. The input value is an integer defined as [0...
Definition maths.c:165
Vect2D_u32 V2u32
alias for Vect2D_u32
Definition maths.h:367
fix32 fix32Frac(fix32 value)
Return fractional part of the specified value (fix32).
Definition maths.c:196
Vect3D_u16 V3u16
alias for Vect3D_u16
Definition maths.h:384
s32 getApproximatedLog2(s32 value)
Return 16.16 fixed point approximation of log2 of the specified 16.16 fixed point value....
Definition maths.c:411
fix16 fix16Div(fix16 val1, fix16 val2)
Compute and return the result of the division of val1 by val2 (fix16).
Definition maths.c:139
u32 getNextPow2(u32 value)
Return next pow2 value which is greater than specified 32 bits unsigned value. Ex: getNextPow2(700) ...
Definition maths.c:523
fix32 fix32Avg(fix32 val1, fix32 val2)
Compute and return the result of the average of val1 by val2 (fix32).
Definition maths.c:223
Vect4D_f32 V4f32
alias for Vect4D_f32
Definition maths.h:413
fastfix32 fastFix32Div(fastfix32 val1, fastfix32 val2)
Compute and return the result of the division of val1 by val2 (fastfix32). WARNING: result can easil...
Definition maths.c:331
fix16 fix16Int(fix16 value)
Return integer part of the specified value (fix16).
Definition maths.c:128
fix32 fix32Int(fix32 value)
Return integer part of the specified value (fix32).
Definition maths.c:201
fix16 fix16Sqrt(fix16 value)
Compute and return the result of the root square of specified value (fix16).
Definition maths.c:160
fastfix16 fastFix16Mul(fastfix16 val1, fastfix16 val2)
Compute and return the result of the multiplication of val1 and val2 (fastfix16).
Definition maths.c:282
u16 divu(u32 op1, u16 op2)
Direct divu instruction (unsigned 32/16=16:16) access using inline assembly to process op1/op2 operat...
Definition maths.c:40
Vect3D_s16 V3s16
alias for Vect3D_s16
Definition maths.h:388
s32 muls(s16 op1, s16 op2)
16x16=32 signed multiplication. Force GCC to use proper 68000 muls instruction.
Definition maths.c:15
Vect2D_f16 V2f16
alias for Vect2D_f16
Definition maths.h:375
fix32 intToFix32(s32 value)
Convert integer to fix32.
Definition maths.c:176
fix32 fix32Mul(fix32 val1, fix32 val2)
Compute and return the result of the multiplication of val1 and val2 (fix32). WARNING: result can ea...
Definition maths.c:207
Mat4D_f16 M4f16
alias for Mat4D_f16
Definition maths.h:434
s32 divmods(s32 op1, s16 op2)
Direct divs instruction (signed 32/16=16:16) access using inline assembly to process op1/op2 operatio...
Definition maths.c:92
fix16 fix16Log2(fix16 value)
Compute and return the result of the Log2 of specified value (fix16).
Definition maths.c:150
fastfix16 fastFix16Div(fastfix16 val1, fastfix16 val2)
Compute and return the result of the division of val1 by val2 (fastfix16).
Definition maths.c:287
fastfix32 fastFix32Round(fastfix32 value)
Round the specified value to nearest integer (fastfix32).
Definition maths.c:303
Mat3D_f32 M3f32
alias for Mat3D_f32
Definition maths.h:430
u16 modu(u32 op1, u16 op2)
Direct divu instruction (unsigned 32/16=16:16) access using inline assembly.
Definition maths.c:60
fix32 fix16ToFix32(fix16 value)
Convert specified fix16 value to fix32.
Definition maths.c:234
s16 fastFix16ToRoundedInt(fastfix16 value)
Round and convert the specified fastfix16 value to integer (fastfix16).
Definition maths.c:266
fix16 fix16Mul(fix16 val1, fix16 val2)
Compute and return the result of the multiplication of val1 and val2 (fix16).
Definition maths.c:134
fastfix16 fastFix16Frac(fastfix16 value)
Return fractional part of the specified value (fastfix16).
Definition maths.c:271
Vect3D_f16 V3f16
alias for Vect3D_f16
Definition maths.h:400
s16 fastFix16ToInt(fastfix16 value)
Convert fastfix16 to integer.
Definition maths.c:256
s16 fix16ToInt(fix16 value)
Convert fix16 to integer.
Definition maths.c:108
fix16 fix16Frac(fix16 value)
Return fractional part of the specified value (fix16).
Definition maths.c:123
Vect2D_s16 V2s16
alias for Vect2D_s16
Definition maths.h:363
s16 fix16ToRoundedInt(fix16 value)
Round and convert the specified fix16 value to integer.
Definition maths.c:118
s32 fix32ToInt(fix32 value)
Convert fix32 to integer.
Definition maths.c:181
fastfix32 fastFix32Mul(fastfix32 val1, fastfix32 val2)
Compute and return the result of the multiplication of val1 and val2 (fastfix32). WARNING: result ca...
Definition maths.c:323
fix16 fix16Round(fix16 value)
Round the specified value to nearest integer (fix16).
Definition maths.c:113
s16 divs(s32 op1, s16 op2)
Direct divs instruction (signed 32/16=16:16) access using inline assembly to process op1/op2 operatio...
Definition maths.c:50
Mat2D_f32 M2f32
alias for Mat2D_f32
Definition maths.h:422
Vect2D_u16 V2u16
alias for Vect2D_u16
Definition maths.h:359
fastfix32 fastFix32Frac(fastfix32 value)
Return fractional part of the specified value (fastfix32).
Definition maths.c:313
Mat2D_f16 M2f16
alias for Mat2D_f16
Definition maths.h:418
fix32 sinFix32(u16 value)
Compute sinus of specified value and return it as fix32. The input value is an integer defined as [0...
Definition maths.c:240
Vect4D_f16 V4f16
alias for Vect4D_f16
Definition maths.h:409
u32 getApproximatedDistance(s32 dx, s32 dy)
Return euclidean distance approximation for specified vector. The returned distance is not 100% perf...
Definition maths.c:382
fix16 fix16Log10(fix16 value)
Compute and return the result of the Log10 of specified value (fix16).
Definition maths.c:155
u32 divmodu(u32 op1, u16 op2)
Direct divu instruction (unsigned 32/16=16:16) access using inline assembly to process op1/op2 operat...
Definition maths.c:82
fastfix32 fastFix32Int(fastfix32 value)
Return integer part of the specified value (fastfix32).
Definition maths.c:318
u16 getLog2Int(u32 value)
Return integer log2 of specified 32 bits unsigned value. Ex: getLog2Int(1024) = 10 getLog2Int(12345...
Definition maths.c:493
Vect3D_u32 V3u32
alias for Vect3D_u32
Definition maths.h:392
u32 mulu(u16 op1, u16 op2)
16x16=32 unsigned multiplication. Force GCC to use proper 68000 mulu instruction.
Definition maths.c:10
Vect3D_f32 V3f32
alias for Vect3D_f32
Definition maths.h:404
Vect2D_f32 V2f32
alias for Vect2D_f32
Definition maths.h:379
u32 intToBCD(u32 value)
Binary to Decimal conversion.
Definition maths.c:340
s32 fix32ToRoundedInt(fix32 value)
Round and convert the specified fix32 value to integer.
Definition maths.c:191
s16 mods(s32 op1, s16 op2)
Direct divs instruction (signed 32/16=16:16) access using inline assembly.
Definition maths.c:71
s32 fastFix32ToRoundedInt(fastfix32 value)
Round and convert the specified fastfix32 value to integer.
Definition maths.c:308
fix16 fix32ToFix16(fix32 value)
Convert specified fix32 value to fix16.
Definition maths.c:229
fastfix16 fastFix16Int(fastfix16 value)
Return integer part of the specified value (fastfix16).
Definition maths.c:276
fix16 fix16Avg(fix16 val1, fix16 val2)
Compute and return the result of the average of val1 by val2 (fix16).
Definition maths.c:144
Vect3D_s32 V3s32
alias for Vect3D_s32
Definition maths.h:396
fastfix16 fastFix16Round(fastfix16 value)
Round the specified value to nearest integer (fastfix16).
Definition maths.c:261
fastfix16 intToFastFix16(s16 value)
Convert integer to fastfix16.
Definition maths.c:251
u32 getApproximatedDistanceV(V2s32 *v)
Return euclidean distance approximation for specified vector. The returned distance is not 100% perf...
Definition maths.c:405
Vect2D_s32 V2s32
alias for Vect2D_s32
Definition maths.h:371
fastfix32 intToFastFix32(s16 value)
Convert integer to fastfix32.
Definition maths.c:293
fix16 intToFix16(s16 value)
Convert integer to fix16.
Definition maths.c:103
Mat4D_f32 M4f32
alias for Mat4D_f32
Definition maths.h:438
fix32 cosFix32(u16 value)
Compute cosinus of specified value and return it as fix32. The input value is an integer defined as ...
Definition maths.c:245
fix32 fix32Round(fix32 value)
Round the specified value to nearest integer (fix32).
Definition maths.c:186
Mat3D_f16 M3f16
alias for Mat3D_f16
Definition maths.h:426
s16 fastFix32ToInt(fastfix32 value)
Convert fastfix32 to integer.
Definition maths.c:298
fix32 fix32Div(fix32 val1, fix32 val2)
Compute and return the result of the division of val1 by val2 (fix32). WARNING: result can easily ov...
Definition maths.c:215
fix16 cosFix16(u16 value)
Compute cosinus of specified value and return it as fix16. The input value is an integer defined as ...
Definition maths.c:170
2x2 Matrice structure - f16 (fix16) type. Internally uses 2 2D vectors.
Definition maths.h:191
2x2 Matrice structure - f32 (fix32) type. Internally uses 2 2D vectors.
Definition maths.h:202
3x3 Matrice structure - f16 (fix16) type. Internally uses 3 3D vectors.
Definition maths.h:282
3x3 Matrice structure - f32 (fix32) type. Internally uses 3 3D vectors.
Definition maths.h:294
4x4 Matrice structure - f16 (fix16) type. Internally uses 4 4D vectors.
Definition maths.h:333
4x4 Matrice structure - f32 (fix32) type. Internally uses 4 4D vectors.
Definition maths.h:346
2D Vector structure - f16 (fix16) type.
Definition maths.h:170
2D Vector structure - f32 (fix32) type.
Definition maths.h:180
2D Vector structure - s16 type.
Definition maths.h:140
2D Vector structure - s32 type.
Definition maths.h:160
2D Vector structure - u16 type.
Definition maths.h:130
2D Vector structure - u32 type.
Definition maths.h:150
3D Vector structure - f16 (fix16) type.
Definition maths.h:259
3D Vector structure - f32 (fix32) type.
Definition maths.h:270
3D Vector structure - s16 type.
Definition maths.h:226
3D Vector structure - s32 type.
Definition maths.h:248
3D Vector structure - u16 type.
Definition maths.h:215
3D Vector structure - u32 type.
Definition maths.h:237
4D Vector structure - f16 (fix16) type.
Definition maths.h:308
4D Vector structure - f32 (fix32) type.
Definition maths.h:320
s32 fastfix32
Definition types.h:207
s16 fastfix16
Definition types.h:202
unsigned long u32
Definition types.h:105
s32 fix32
Definition types.h:186
unsigned short u16
Definition types.h:100
long s32
Definition types.h:89
short s16
Definition types.h:84
s16 fix16
Definition types.h:181