The Socrates Gameboy Advance Development Engine
The SGADE is a development library for the Nintendo Gameboy
Advance. It's free for all uses and is distributed without guarantees.
For more information visit
the SGADE page.
Files | |
| file | SoMath.c |
| file | SoMath.h |
Defines | |
| #define | SO_FIXED_Q |
| Number of bits reserved for the fraction in fixed point numbers. | |
| #define | SO_FIXED_PI |
| PI a.k.a. 3.14 etc. in a fixed point format. | |
| #define | SO_MIN(x, y) |
| Returns the minimum of a pair of numbers. | |
| #define | SO_MAX(x, y) |
| Returns the maximum of a pair of numbers. | |
| #define | SO_ABS(x) |
| Returns the absolute value of a number. | |
| #define | SO_NUMBER_IS_EVEN(n) |
| Evaluates to true when a number is even, false otherwise. | |
| #define | SO_NUMBER_IS_UNEVEN(n) |
| Evaluates to false when a number is even, true otherwise. | |
| #define | SO_FIXED_FROM_WHOLE(n) |
| Converts a whole (integer) number to a fixedpoint number. | |
| #define | SO_FIXED_TO_WHOLE(n) |
| Converts a fixed point number to a whole number. Cuts of the fraction. | |
| #define | SO_FIXED_FROM_FLOAT(n) |
| Converts a whole number to a fixedpoint number. | |
| #define | SO_FIXED_TO_FLOAT(n) |
| Converts a fixed point number to a whole number. | |
| #define | SO_FIXED_TO_FRACTION(n) |
| Leaves only the fraction of a fixed point number. | |
| #define | SO_FIXED_MAKE_WHOLE(n) |
| Makes a fixed point number whole. | |
| #define | SO_FIXED_CEIL_WHOLE(n) |
| Returns the whole ceiling of the fixed point number. | |
| #define | SO_FIXED_TO_N_8_FORMAT(n) |
| Converts our fixed point format to a 24.8 format. | |
| #define | SO_FIXED_MULTIPLY_SMALL_SMALL(n, m) |
| NOTE: all of the SO_FIXED_MULTIPLY_* macros are very sensitive to input data. | |
| #define | SO_FIXED_MULTIPLY_BIG_SMALL(big, small) |
| Multiply macro to multiply a relatively small fixed point number by a relatively big one. | |
| #define | SO_FIXED_MULTIPLY(n, m) |
| Multiply macro to multiply two fixed point numbers. | |
| #define | SO_FIXED_DIVIDE_SMALL_BIG(n, m) |
| Divide macro to divide a small fixed number by a big fixed point number. | |
| #define | SO_FIXED_DIVIDE(n, m) |
| Divide macro to divide a fixed point number by another fixed point number. | |
| #define | SO_FIXED_ONE_OVER_FAST_INACCURATE(n) |
| One-over macro that uses a repricocal-table. | |
| #define | SO_FIXED_ONE_OVER_SLOW_ACCURATE(n) |
| Macro to calculate one-over-N the slow and accurate way. | |
| #define | SO_SINE(n) |
| Returns the fixed point sine of an angle. A full circle is 256 degrees. | |
| #define | SO_COSINE(n) |
| Returns the fixed point cosine of an angle. | |
Functions | |
| sofixedpoint | SoMathFixedMultiply (sofixedpoint a_A, sofixedpoint a_B) |
| Fixed-point multiplication function. | |
| sofixedpoint | SoMathFixedMultiplyByFraction (sofixedpoint a_A, sofixedpoint a_B) |
| Fixed-point multiplication by fraction function. | |
| sofixedpoint | SoMathFixedSqrt (sofixedpoint a_FixedValue) |
| Squareroot function. | |
| void | SoMathRandSeed (u32 a_Seed) |
| Seeds the random number generator. | |
| u32 | SoMathRand (void) |
| Returns a pseudo random number. | |
| s32 | SoMathDivideAndModulus (s32 a_Numerator, s32 a_Denominator, s32 *a_Remainder) |
| Fast divide and modulus function. | |
| s32 | SoMathModulus (s32 a_Numerator, s32 a_Denominator) |
| Fast modulus function. | |
| s32 | SoMathDivide (s32 a_Numerator, s32 a_Denominator) |
| Fast divide function. | |
Singleton
This file contains all math related stuff. It has a couple of constants, fixed point macros, precalculated sine and cosine tables, a pseudo random number generator and some math functions.
|
|
Returns the fixed point cosine of an angle. A full circle is 256 degrees. See SO_SINE for more information. |
|
|
Converts a whole number to a fixedpoint number.
|
|
|
NOTE: all of the SO_FIXED_MULTIPLY_* macros are very sensitive to input data. They can easily overflow, so use with extreme caution. Multiply macro to multiply two relatively small fixed point numbers. |
|
|
One-over macro that uses a repricocal-table. This can only be used when the denominator is a positive number smaller than SO_ONE_OVER_N_MAX_N. Be carefull with this one, and only use it if you understand its limitations. |
|
|
Macro to calculate one-over-N the slow and accurate way. Well, slow. Uses a bios SWI call for faster divides. |
|
|
Number of bits reserved for the fraction in fixed point numbers.
We use a N.Q fixed point format where Q is the number of bits for the fraction. Notice that some of the macros below rely heavily (hardcoded) on Q. This means that if you change this number, you will have to change these macros as well as some hardcoded stuff in some assembly code. If you change this macro, then an assertion is triggered during compilation of the SoSystem.c file to remind you about this. |
|
|
Converts a fixed point number to a whole number. Cuts of the fraction.
|
|
|
Returns the fixed point sine of an angle. A full circle is 256 degrees.
Some examples:
|
|
||||||||||||
|
Fast divide function.
Note, that in order to use this on an emulator you need a bios that supports SWI 6 calls; |
|
||||||||||||||||
|
Fast divide and modulus function.
Because sometimes you also need them modulus at the same time, and it's a by-product of the divide, you can use this routine if you need both. Note, that in order to use this on an emulator you need a bios that supports SWI 6 calls; |
|
||||||||||||
|
Fixed-point multiplication function.
|
|
||||||||||||
|
Fixed-point multiplication by fraction function.
|
|
|
Squareroot function.
Squareroot method taken from the internet. No credits were given. If you think this is your routine and I stole it, let me know and I'll give you credit. This was originally a squareroot function for integer math, but I adapted it to work for fixed point math. I don't know whether this is the fastest way, but it works. I don't want to spend more thinking on it anyway, cause you shouldn't be doing squareroots at runtime anyway. |
|
||||||||||||
|
Fast modulus function.
Note, that in order to use this on an emulator you need a bios that supports SWI 6 calls; |
|
|
Returns a pseudo random number.
|
|
|
Seeds the random number generator.
|