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.
00001 // ---------------------------------------------------------------------------- 00011 // ---------------------------------------------------------------------------- 00012 00013 #ifndef SO_MATH_H 00014 #define SO_MATH_H 00015 00016 #ifdef __cplusplus 00017 extern "C" { 00018 #endif 00019 00020 00021 // ---------------------------------------------------------------------------- //! @{ 00033 // ---------------------------------------------------------------------------- 00034 00035 // ---------------------------------------------------------------------------- 00036 // Includes 00037 // ---------------------------------------------------------------------------- 00038 #include "SoSystem.h" 00039 #include "SoDebug.h" 00040 00041 // ---------------------------------------------------------------------------- 00042 // Defines 00043 // ---------------------------------------------------------------------------- 00044 00059 #define SO_FIXED_Q 16 00060 00062 #define SO_FIXED_PI 205887 00063 00064 // ---------------------------------------------------------------------------- 00065 // Functions implemented in C; 00066 // ---------------------------------------------------------------------------- 00067 00068 sofixedpoint SoMathFixedMultiply(sofixedpoint a_A, sofixedpoint a_B); 00069 sofixedpoint SoMathFixedMultiplyByFraction(sofixedpoint a_A, sofixedpoint a_B); 00070 sofixedpoint SoMathFixedSqrt(sofixedpoint a_FixedValue); 00071 void SoMathRandSeed(u32 a_Seed); 00072 u32 SoMathRand(void); 00073 00074 00075 // ---------------------------------------------------------------------------- 00076 // Functions implemented in asm; 00077 // Sadly, these need to be documented in here, cause Doxygen can't handle .s 00078 // files very well. 00079 // ---------------------------------------------------------------------------- 00098 // ---------------------------------------------------------------------------- 00099 s32 SoMathDivideAndModulus( s32 a_Numerator, s32 a_Denominator, s32 *a_Remainder ); 00100 00101 // ---------------------------------------------------------------------------- 00115 // ---------------------------------------------------------------------------- 00116 s32 SoMathModulus( s32 a_Numerator, s32 a_Denominator ); 00117 00118 // ---------------------------------------------------------------------------- 00134 // ---------------------------------------------------------------------------- 00135 s32 SoMathDivide( s32 a_Numerator, s32 a_Denominator ); 00136 00137 // ---------------------------------------------------------------------------- 00138 // Macros; 00139 // ---------------------------------------------------------------------------- 00140 00142 #define SO_MIN(x,y) ((x) < (y) ? (x) : (y)) 00143 00145 #define SO_MAX(x,y) ((x) > (y) ? (x) : (y)) 00146 00148 #define SO_ABS(x) ((x) < 0 ? -(x) : (x)) 00149 00151 #define SO_NUMBER_IS_EVEN( n ) (!((n) & 1)) 00152 00154 #define SO_NUMBER_IS_UNEVEN( n ) ( (n) & 1) 00155 00157 #define SO_FIXED_FROM_WHOLE( n ) ( (n) << SO_FIXED_Q) 00158 00160 #define SO_FIXED_TO_WHOLE( n ) ( (n) >> SO_FIXED_Q) 00161 00164 #define SO_FIXED_FROM_FLOAT( n ) ( (n) * (float)(1<<SO_FIXED_Q)) 00165 00168 #define SO_FIXED_TO_FLOAT( n ) ( (n) / (float)(1<<SO_FIXED_Q)) 00169 00171 #define SO_FIXED_TO_FRACTION( n ) ( (n) & ((1 << SO_FIXED_Q)-1)) 00172 00174 #define SO_FIXED_MAKE_WHOLE( n ) ( (n) >>= SO_FIXED_Q) 00175 00177 #define SO_FIXED_CEIL_WHOLE( n ) ( ((n) + ((1<<SO_FIXED_Q)-1)) >> SO_FIXED_Q ) 00178 00180 #define SO_FIXED_TO_N_8_FORMAT( n ) ( (n) >> (SO_FIXED_Q - 8) ) 00181 00186 #define SO_FIXED_MULTIPLY_SMALL_SMALL( n, m ) ( (((m)>>2) * ((n)>>2)) >> 12 ) 00187 00189 #define SO_FIXED_MULTIPLY_BIG_SMALL( big, small ) ( (((big)>>6) * ((small)>>2)) >> 8 ) 00190 00192 #define SO_FIXED_MULTIPLY( n, m ) ( (((n)>>4) * ((m)>>4)) >> 8 ) 00193 00195 #define SO_FIXED_DIVIDE_SMALL_BIG( n, m ) ( (((n)<<8) / (m)) << 8 ) 00196 00198 #define SO_FIXED_DIVIDE( n, m ) ( (((n)<<4) / ((m)>>4)) << 8 ) 00199 00203 #define SO_FIXED_ONE_OVER_FAST_INACCURATE( n ) (g_OneOver[ (n) >> (SO_FIXED_Q - SO_ONE_OVER_N_INDEX_Q) ] ) 00204 //#define SO_FIXED_ONE_OVER_FAST_INACCURATE( n ) ( SoMathDivide( (SO_FIXED_FROM_WHOLE( 1 ) << (30 - SO_FIXED_Q)), ((n)>>(SO_FIXED_Q - (30 - SO_FIXED_Q))))) 00205 00208 #define SO_FIXED_ONE_OVER_SLOW_ACCURATE( n ) ( SoMathDivide( (SO_FIXED_FROM_WHOLE( 1 ) << (30 - SO_FIXED_Q)), ((n)>>(SO_FIXED_Q - (30 - SO_FIXED_Q))))) 00209 00227 #define SO_SINE( n ) ( g_Sine[ (n) & 0xFF ] ) 00228 00231 #define SO_COSINE( n ) ( g_Cosine[ (n) & 0xFF ] ) 00232 00233 00234 // ---------------------------------------------------------------------------- 00235 // EOF 00236 // ---------------------------------------------------------------------------- 00237 00239 00240 #ifdef __cplusplus 00241 } // extern "C" 00242 #endif 00243 00244 #endif