00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00032 #ifndef MATH_ARM9_INCLUDE
00033 #define MATH_ARM9_INCLUDE
00034
00035 #include "nds/ndstypes.h"
00036
00037 #ifndef STATIC_INL
00038 #define STATIC_INL static inline
00039 #endif
00040
00041 #define REG_DIVCNT (*(vu16*)(0x04000280))
00042 #define REG_DIV_NUMER (*(vs64*) (0x04000290))
00043 #define REG_DIV_NUMER_L (*(vs32*) (0x04000290))
00044 #define REG_DIV_NUMER_H (*(vs32*) (0x04000294))
00045 #define REG_DIV_DENOM (*(vs64*) (0x04000298))
00046 #define REG_DIV_DENOM_L (*(vs32*) (0x04000298))
00047 #define REG_DIV_DENOM_H (*(vs32*) (0x0400029C))
00048 #define REG_DIV_RESULT (*(vs64*) (0x040002A0))
00049 #define REG_DIV_RESULT_L (*(vs32*) (0x040002A0))
00050 #define REG_DIV_RESULT_H (*(vs32*) (0x040002A4))
00051 #define REG_DIVREM_RESULT (*(vs64*) (0x040002A8))
00052 #define REG_DIVREM_RESULT_L (*(vs32*) (0x040002A8))
00053 #define REG_DIVREM_RESULT_H (*(vs32*) (0x040002AC))
00054
00055 #define REG_SQRTCNT (*(vu16*)(0x040002B0))
00056 #define REG_SQRT_PARAM (*(vs64*) (0x040002B8))
00057 #define REG_SQRT_PARAM_L (*(vs32*) (0x040002B8))
00058 #define REG_SQRT_PARAM_H (*(vs32*) (0x040002BC))
00059 #define REG_SQRT_RESULT (*(vu32*) (0x040002B4))
00060
00061
00062
00063 #define DIV_64_64 2
00064 #define DIV_64_32 1
00065 #define DIV_32_32 0
00066 #define DIV_BUSY (1<<15)
00067
00068 #define SQRT_64 1
00069 #define SQRT_32 0
00070 #define SQRT_BUSY (1<<15)
00071
00072
00073
00074 STATIC_INL
00081 int32 divf32(int32 num, int32 den)
00082 {
00083 REG_DIVCNT = DIV_64_32;
00084
00085 while(REG_DIVCNT & DIV_BUSY);
00086
00087 REG_DIV_NUMER = ((int64)num) << 12;
00088 REG_DIV_DENOM_L = den;
00089
00090 while(REG_DIVCNT & DIV_BUSY);
00091
00092 return (REG_DIV_RESULT_L);
00093 }
00094
00095 STATIC_INL
00102 int32 mulf32(int32 a, int32 b)
00103 {
00104 long long result = (long long)a*(long long)b;
00105 return (int32)(result >> 12);
00106 }
00107
00108 STATIC_INL
00114 int32 sqrtf32(int32 a)
00115 {
00116 REG_SQRTCNT = SQRT_64;
00117
00118 while(REG_SQRTCNT & SQRT_BUSY);
00119
00120 REG_SQRT_PARAM = ((int64)a) << 12;
00121
00122 while(REG_SQRTCNT & SQRT_BUSY);
00123
00124 return REG_SQRT_RESULT;
00125 }
00126
00127
00128
00129 STATIC_INL
00136 int32 div32(int32 num, int32 den)
00137 {
00138 REG_DIVCNT = DIV_32_32;
00139
00140 while(REG_DIVCNT & DIV_BUSY);
00141
00142 REG_DIV_NUMER_L = num;
00143 REG_DIV_DENOM_L = den;
00144
00145 while(REG_DIVCNT & DIV_BUSY);
00146
00147 return (REG_DIV_RESULT_L);
00148 }
00149
00150 STATIC_INL
00157 int32 mod32(int32 num, int32 den)
00158 {
00159 REG_DIVCNT = DIV_32_32;
00160
00161 while(REG_DIVCNT & DIV_BUSY);
00162
00163 REG_DIV_NUMER_L = num;
00164 REG_DIV_DENOM_L = den;
00165
00166 while(REG_DIVCNT & DIV_BUSY);
00167
00168 return (REG_DIVREM_RESULT_L);
00169 }
00170
00171 STATIC_INL
00178 int32 div64(int64 num, int32 den)
00179 {
00180 REG_DIVCNT = DIV_64_32;
00181
00182 while(REG_DIVCNT & DIV_BUSY);
00183
00184 REG_DIV_NUMER = num;
00185 REG_DIV_DENOM_L = den;
00186
00187 while(REG_DIVCNT & DIV_BUSY);
00188
00189 return (REG_DIV_RESULT_L);
00190 }
00191
00192 STATIC_INL
00199 int32 mod64(int64 num, int32 den)
00200 {
00201 REG_DIVCNT = DIV_64_32;
00202
00203 while(REG_DIVCNT & DIV_BUSY);
00204
00205 REG_DIV_NUMER = num;
00206 REG_DIV_DENOM_L = den;
00207
00208 while(REG_DIVCNT & DIV_BUSY);
00209
00210 return (REG_DIVREM_RESULT_L);
00211 }
00212
00213 STATIC_INL
00219 u32 sqrt32(int a)
00220 {
00221 REG_SQRTCNT = SQRT_32;
00222
00223 while(REG_SQRTCNT & SQRT_BUSY);
00224
00225 REG_SQRT_PARAM_L = a;
00226
00227 while(REG_SQRTCNT & SQRT_BUSY);
00228
00229 return REG_SQRT_RESULT;
00230 }
00231
00232 STATIC_INL
00238 u32 sqrt64(long long a)
00239 {
00240 REG_SQRTCNT = SQRT_64;
00241
00242 while(REG_SQRTCNT & SQRT_BUSY);
00243
00244 REG_SQRT_PARAM = a;
00245
00246 while(REG_SQRTCNT & SQRT_BUSY);
00247
00248 return REG_SQRT_RESULT;
00249 }
00250 STATIC_INL
00261 void crossf32(int32 *a, int32 *b, int32 *result)
00262 {
00263 result[0] = mulf32(a[1], b[2]) - mulf32(b[1], a[2]);
00264 result[1] = mulf32(a[2], b[0]) - mulf32(b[2], a[0]);
00265 result[2] = mulf32(a[0], b[1]) - mulf32(b[0], a[1]);
00266 }
00267
00268
00269 STATIC_INL
00278 int32 dotf32(int32 *a, int32 *b)
00279 {
00280 return mulf32(a[0], b[0]) + mulf32(a[1], b[1]) + mulf32(a[2], b[2]);
00281 }
00282
00283
00284
00285 STATIC_INL
00294 void normalizef32(int32* a)
00295 {
00296
00297 int32 magnitude = sqrtf32( mulf32(a[0], a[0]) + mulf32(a[1], a[1]) + mulf32(a[2], a[2]) );
00298
00299 a[0] = divf32(a[0], magnitude);
00300 a[1] = divf32(a[1], magnitude);
00301 a[2] = divf32(a[2], magnitude);
00302 }
00303 #endif
00304