deepstone
annotate src/fixedp.h @ 37:e5e01cd2c995
changed the VC project file to use the non-dll versions of msvcrt
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 23 Sep 2013 08:06:04 +0300 |
parents | 61d97b17cd2b |
children | 1fa939507d8b |
rev | line source |
---|---|
nuclear@25 | 1 #ifndef FIXED_POINT_H_ |
nuclear@25 | 2 #define FIXED_POINT_H_ |
nuclear@25 | 3 |
nuclear@25 | 4 #include "inttypes.h" |
nuclear@25 | 5 |
nuclear@25 | 6 #ifdef DBG_USE_FLOAT |
nuclear@25 | 7 typedef float fixed; |
nuclear@25 | 8 #else |
nuclear@25 | 9 typedef int32_t fixed; |
nuclear@25 | 10 #endif |
nuclear@25 | 11 |
nuclear@25 | 12 /* valid choices for DECIMAL_BITS |
nuclear@25 | 13 * 8: for fixed point 24:8 |
nuclear@25 | 14 * 16: for fixed point 16:16 |
nuclear@25 | 15 */ |
nuclear@25 | 16 #define DECIMAL_BITS 16 |
nuclear@25 | 17 |
nuclear@25 | 18 #if DECIMAL_BITS == 8 |
nuclear@25 | 19 #define FIXED_SHIFT 8 |
nuclear@25 | 20 #define FLT_SCALE 256.0f |
nuclear@25 | 21 #define FRAC_MASK 0xff |
nuclear@25 | 22 #else /* DECIMAL_BITS == 16 */ |
nuclear@25 | 23 #define FIXED_SHIFT 16 |
nuclear@25 | 24 #define FLT_SCALE 65536.0f |
nuclear@25 | 25 #define FRAC_MASK 0xffff |
nuclear@25 | 26 #endif /* DECIMAL_BITS */ |
nuclear@25 | 27 |
nuclear@25 | 28 /*extern const fixed fixed_zero; |
nuclear@25 | 29 extern const fixed fixed_one; |
nuclear@25 | 30 extern const fixed fixed_half; |
nuclear@25 | 31 extern const fixed fixed_tenth; |
nuclear@25 | 32 extern const fixed fixed_255;*/ |
nuclear@25 | 33 |
nuclear@25 | 34 #ifdef DBG_USE_FLOAT |
nuclear@25 | 35 /* ------- debug mode, use floating point -------- */ |
nuclear@25 | 36 #define FIXED_INT_PART(n) ((int)(n)) |
nuclear@25 | 37 #define FIXED_FRAC_PART(n) ((n) > 0.0f ? (FIXED_INT_PART(n) - (n)) : (FIXED_INT_PART(n) + (n))) |
nuclear@25 | 38 #define FIXED_ROUND(n) FIXED_INT_PART((n) >= 0.0 ? (n) + 0.5f : (n) - 0.5f) |
nuclear@25 | 39 |
nuclear@25 | 40 #define FIXED_TO_FLOAT(n) (n) |
nuclear@25 | 41 #define FLOAT_TO_FIXED(n) (n) |
nuclear@25 | 42 #define INT_TO_FIXED(n) ((float)(n)) |
nuclear@25 | 43 |
nuclear@25 | 44 #define FIXED_EPSILON (1e-6) |
nuclear@25 | 45 |
nuclear@25 | 46 #else /* ---- really fixed point ---- */ |
nuclear@25 | 47 |
nuclear@25 | 48 #define FIXED_INT_PART(n) ((n) >> FIXED_SHIFT) |
nuclear@25 | 49 #define FIXED_FRAC_PART(n) ((n) & FRAC_MASK) |
nuclear@25 | 50 #define FIXED_ROUND(n) FIXED_INT_PART((n) >= 0 ? (n) + fixedf(0.5) : (n) - fixedf(0.5)) |
nuclear@25 | 51 /*#define FIXED_ROUND(n) FIXED_INT_PART(n)*/ |
nuclear@25 | 52 |
nuclear@25 | 53 #define FIXED_TO_FLOAT(n) (float)((n) / FLT_SCALE) |
nuclear@25 | 54 #define FLOAT_TO_FIXED(n) (fixed)((n) * FLT_SCALE) |
nuclear@25 | 55 #define INT_TO_FIXED(n) (fixed)((n) << FIXED_SHIFT) |
nuclear@25 | 56 |
nuclear@25 | 57 #define FIXED_EPSILON (1) |
nuclear@25 | 58 |
nuclear@25 | 59 #endif |
nuclear@25 | 60 |
nuclear@25 | 61 |
nuclear@25 | 62 #define fixed_int(n) FIXED_INT_PART(n) |
nuclear@25 | 63 #define fixed_frac(n) FIXED_FRAC_PART(n) |
nuclear@25 | 64 #define fixed_float(n) FIXED_TO_FLOAT(n) |
nuclear@25 | 65 #define fixed_round(n) FIXED_ROUND(n) |
nuclear@25 | 66 |
nuclear@25 | 67 #define fixedf(n) FLOAT_TO_FIXED(n) |
nuclear@25 | 68 #define fixedi(n) INT_TO_FIXED(n) |
nuclear@25 | 69 |
nuclear@25 | 70 #define fixed_add(n1, n2) ((n1) + (n2)) |
nuclear@25 | 71 #define fixed_sub(n1, n2) ((n1) - (n2)) |
nuclear@25 | 72 |
nuclear@25 | 73 |
nuclear@25 | 74 |
nuclear@25 | 75 #ifdef DBG_USE_FLOAT |
nuclear@25 | 76 |
nuclear@25 | 77 #define fixed_mul(n1, n2) ((n1) * (n2)) |
nuclear@25 | 78 #define fixed_div(n1, n2) ((n1) / (n2)) |
nuclear@25 | 79 |
nuclear@25 | 80 #define fixed_sin(x) (fixed)sin(x) |
nuclear@25 | 81 #define fixed_cos(x) (fixed)cos(x) |
nuclear@25 | 82 |
nuclear@25 | 83 #else |
nuclear@25 | 84 |
nuclear@25 | 85 #if DECIMAL_BITS == 8 |
nuclear@25 | 86 #define fixed_mul(n1, n2) (fixed)((n1) * (n2) >> FIXED_SHIFT) |
nuclear@25 | 87 #define fixed_div(n1, n2) (((n1) << FIXED_SHIFT) / (n2)) |
nuclear@25 | 88 #else |
nuclear@27 | 89 #define fixed_div(n1, n2) (((n2) >> 8) ? (((n1) << 8) / ((n2) >> 8)) : (n1)) |
nuclear@25 | 90 #define fixed_mul(n1, n2) (((n1) >> 8) * ((n2) >> 8)) |
nuclear@25 | 91 #endif /* DECIMAL_BITS */ |
nuclear@25 | 92 |
nuclear@25 | 93 #ifdef __cplusplus |
nuclear@25 | 94 extern "C" { |
nuclear@25 | 95 #endif |
nuclear@25 | 96 |
nuclear@25 | 97 fixed fixed_sin(fixed angle); |
nuclear@25 | 98 fixed fixed_cos(fixed angle); |
nuclear@25 | 99 |
nuclear@25 | 100 #ifdef __cplusplus |
nuclear@25 | 101 } |
nuclear@25 | 102 #endif |
nuclear@25 | 103 |
nuclear@25 | 104 #endif |
nuclear@25 | 105 |
nuclear@25 | 106 #endif /* FIXED_POINT_H_ */ |