sgdk
maths.h
Go to the documentation of this file.
00001 
00011 #ifndef _MATHS_H_
00012 #define _MATHS_H_
00013 
00014 
00015 extern const fix32 sintab32[1024];
00016 extern const fix16 sintab16[1024];
00017 
00018 extern const fix16 log2tab16[0x10000];
00019 extern const fix16 log10tab16[0x10000];
00020 extern const fix16 sqrttab16[0x10000];
00021 
00022 
00027 #define min(X, Y)   (((X) < (Y))?(X):(Y))
00028 
00033 #define max(X, Y)   (((X) > (Y))?(X):(Y))
00034 
00039 #define clamp(X, L, H)   (min(max((X), (L)), (H)))
00040 
00041 #if (ENABLE_NEWLIB == 0)
00042 
00046 #define abs(X)      (((X) < 0)?-(X):(X))
00047 #endif  // ENABLE_NEWLIB
00048 
00049 #ifndef PI
00050 
00054 #define PI 3.14159265358979323846
00055 #endif
00056 
00057 
00058 #define FIX32_INT_BITS              22
00059 #define FIX32_FRAC_BITS             (32 - FIX32_INT_BITS)
00060 
00061 #define FIX32_INT_MASK              (((1 << FIX32_INT_BITS) - 1) << FIX32_FRAC_BITS)
00062 #define FIX32_FRAC_MASK             ((1 << FIX32_FRAC_BITS) - 1)
00063 
00071 #define FIX32(value)                ((fix32) ((value) * (1 << FIX32_FRAC_BITS)))
00072 
00077 #define intToFix32(value)           ((fix32) ((value) << FIX32_FRAC_BITS))
00078 
00082 #define fix32ToInt(value)           ((s32) ((value) >> FIX32_FRAC_BITS))
00083 
00088 #define fix32Round(value)           \
00089     ((fix32Frac(value) > FIX32(0.5))?fix32Int(value + FIX32(1)) + 1:fix32Int(value))
00090 
00095 #define fix32ToRoundedInt(value)    \
00096     ((fix32Frac(value) > FIX32(0.5))?fix32ToInt(value) + 1:fix32ToInt(value))
00097 
00102 #define fix32Frac(value)            ((value) & FIX32_FRAC_MASK)
00103 
00107 #define fix32Int(value)             ((value) & FIX32_INT_MASK)
00108 
00113 #define fix32Add(val1, val2)        ((val1) + (val2))
00114 
00118 #define fix32Sub(val1, val2)        ((val1) - (val2))
00119 
00123 #define fix32Neg(value)             (0 - (value))
00124 
00130 #define fix32Mul(val1, val2)        (((val1) >> (FIX32_FRAC_BITS / 2)) * ((val2) >> (FIX32_FRAC_BITS / 2)))
00131 
00136 #define fix32Div(val1, val2)        (((val1) << (FIX32_FRAC_BITS / 2)) / ((val2) >> (FIX32_FRAC_BITS / 2)))
00137 
00141 #define fix32Avg(val1, val2)        (((val1) + (val2)) >> 1)
00142 
00143 #define FIX16_INT_BITS              10
00144 #define FIX16_FRAC_BITS             (16 - FIX16_INT_BITS)
00145 
00146 #define FIX16_INT_MASK              (((1 << FIX16_INT_BITS) - 1) << FIX16_FRAC_BITS)
00147 #define FIX16_FRAC_MASK             ((1 << FIX16_FRAC_BITS) - 1)
00148 
00156 #define FIX16(value)                ((fix16) ((value) * (1 << FIX16_FRAC_BITS)))
00157 
00162 #define intToFix16(value)           ((fix16) ((value) << FIX16_FRAC_BITS))
00163 
00167 #define fix16ToInt(value)           ((s16) ((value) >> FIX16_FRAC_BITS))
00168 
00173 #define fix16Round(value)           \
00174     ((fix16Frac(value) > FIX16(0.5))?fix16Int(value + FIX16(1)) + 1:fix16Int(value))
00175 
00180 #define fix16ToRoundedInt(value)    \
00181     ((fix16Frac(value) > FIX16(0.5))?fix16ToInt(value) + 1:fix16ToInt(value))
00182 
00187 #define fix16Frac(value)            ((value) & FIX16_FRAC_MASK)
00188 
00192 #define fix16Int(value)             ((value) & FIX16_INT_MASK)
00193 
00198 #define fix16Add(val1, val2)        ((val1) + (val2))
00199 
00203 #define fix16Sub(val1, val2)        ((val1) - (val2))
00204 
00208 #define fix16Neg(value)             (0 - (value))
00209 
00214 #define fix16Mul(val1, val2)        (muls(val1, val2) >> FIX16_FRAC_BITS)
00215 
00219 #define fix16Div(val1, val2)        (divs(val1 << FIX16_FRAC_BITS, val2))
00220 
00224 #define fix16Avg(val1, val2)        (((val1) + (val2)) >> 1)
00225 
00230 #define fix16Log2(v)                log2tab16[v]
00231 
00235 #define fix16Log10(v)               log10tab16[v]
00236 
00240 #define fix16Sqrt(v)                sqrttab16[v]
00241 
00242 
00247 #define fix32ToFix16(value)         (((value) << FIX16_FRAC_BITS) >> FIX32_FRAC_BITS)
00248 
00252 #define fix16ToFix32(value)         (((value) << FIX32_FRAC_BITS) >> FIX16_FRAC_BITS)
00253 
00259 #define sinFix32(value)             sintab32[(value) & 1023]
00260 
00265 #define cosFix32(value)             sintab32[((value) + 256) & 1023]
00266 
00272 #define sinFix16(value)             sintab16[(value) & 1023]
00273 
00278 #define cosFix16(value)             sintab16[((value) + 256) & 1023]
00279 
00280 
00281 // 2D STUFF
00282 
00287 typedef struct
00288 {
00289     u16 x;
00290     u16 y;
00291 } Vect2D_u16;
00292 
00297 typedef struct
00298 {
00299     s16 x;
00300     s16 y;
00301 } Vect2D_s16;
00302 
00307 typedef struct
00308 {
00309     u32 x;
00310     u32 y;
00311 } Vect2D_u32;
00312 
00317 typedef struct
00318 {
00319     s32 x;
00320     s32 y;
00321 } Vect2D_s32;
00322 
00327 typedef struct
00328 {
00329     fix16 x;
00330     fix16 y;
00331 } Vect2D_f16;
00332 
00337 typedef struct
00338 {
00339     fix32 x;
00340     fix32 y;
00341 } Vect2D_f32;
00342 
00348 typedef struct
00349 {
00350     Vect2D_f16 a;
00351     Vect2D_f16 b;
00352 } Mat2D_f16;
00353 
00359 typedef struct
00360 {
00361     Vect2D_f32 a;
00362     Vect2D_f32 b;
00363 } Mat2D_f32;
00364 
00365 
00366 // 3D STUFF
00367 
00372 typedef struct
00373 {
00374     u16 x;
00375     u16 y;
00376     u16 z;
00377 } Vect3D_u16;
00378 
00383 typedef struct
00384 {
00385     s16 x;
00386     s16 y;
00387     s16 z;
00388 } Vect3D_s16;
00389 
00394 typedef struct
00395 {
00396     u32 x;
00397     u32 y;
00398     u32 z;
00399 } Vect3D_u32;
00400 
00405 typedef struct
00406 {
00407     s32 x;
00408     s32 y;
00409     s32 z;
00410 } Vect3D_s32;
00411 
00416 typedef struct
00417 {
00418     fix16 x;
00419     fix16 y;
00420     fix16 z;
00421 } Vect3D_f16;
00422 
00427 typedef struct
00428 {
00429     fix32 x;
00430     fix32 y;
00431     fix32 z;
00432 } Vect3D_f32;
00433 
00439 typedef struct
00440 {
00441     Vect3D_f16 a;
00442     Vect3D_f16 b;
00443     Vect3D_f16 c;
00444 } Mat3D_f16;
00445 
00451 typedef struct
00452 {
00453     Vect3D_f32 a;
00454     Vect3D_f32 b;
00455     Vect3D_f32 c;
00456 } Mat3D_f32;
00457 
00458 
00459 // 4D STUFF
00460 
00465 typedef struct
00466 {
00467     fix16 x;
00468     fix16 y;
00469     fix16 z;
00470     fix16 w;
00471 } Vect4D_f16;
00472 
00477 typedef struct
00478 {
00479     fix32 x;
00480     fix32 y;
00481     fix32 z;
00482     fix32 w;
00483 } Vect4D_f32;
00484 
00490 typedef struct
00491 {
00492     Vect4D_f16 a;
00493     Vect4D_f16 b;
00494     Vect4D_f16 c;
00495     Vect4D_f16 d;
00496 } Mat4D_f16;
00497 
00503 typedef struct
00504 {
00505     Vect4D_f32 a;
00506     Vect4D_f32 b;
00507     Vect4D_f32 c;
00508     Vect4D_f32 d;
00509 } Mat4D_f32;
00510 
00511 
00512 // short alias
00513 
00517 typedef Vect2D_u16 V2u16;
00521 typedef Vect2D_s16 V2s16;
00525 typedef Vect2D_u32 V2u32;
00529 typedef Vect2D_s32 V2s32;
00533 typedef Vect2D_f16 V2f16;
00537 typedef Vect2D_f32 V2f32;
00538 
00542 typedef Vect3D_u16 V3u16;
00546 typedef Vect3D_s16 V3s16;
00550 typedef Vect3D_u32 V3u32;
00554 typedef Vect3D_s32 V3s32;
00558 typedef Vect3D_f16 V3f16;
00562 typedef Vect3D_f32 V3f32;
00563 
00567 typedef Vect4D_f16 V4f16;
00571 typedef Vect4D_f32 V4f32;
00572 
00576 typedef Mat2D_f16 M2f16;
00580 typedef Mat2D_f32 M2f32;
00584 typedef Mat3D_f16 M3f16;
00588 typedef Mat3D_f32 M3f32;
00592 typedef Mat4D_f16 M4f16;
00596 typedef Mat4D_f32 M4f32;
00597 
00598 
00609 u32 mulu(u16 op1, u16 op2);
00620 s32 muls(s16 op1, s16 op2);
00632 u16 divu(u32 op1, u16 op2);
00644 s16 divs(s32 op1, s16 op2);
00655 u16 modu(u32 op1, u16 op2);
00666 s16 mods(s32 op1, s16 op2);
00667 
00680 u32 divmodu(u32 op1, u16 op2);
00693 s32 divmods(s32 op1, s16 op2);
00694 
00702 u32 intToBCD(u32 value);
00703 
00708 u32 distance_approx(s32 dx, s32 dy);
00719 u32 getApproximatedDistance(s32 dx, s32 dy);
00730 s32 getApproximatedLog2(s32 value);
00741 u16 getLog2Int(u32 value);
00742 
00753 u32 getNextPow2(u32 value);
00754 
00755 
00756 #endif // _MATHS_H_
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines