deepstone
diff src/fixedp.h @ 26:61d97b17cd2b
shortened the name fixed_point.c/h to fixedp
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 22 Sep 2013 02:25:41 +0300 |
parents | src/fixed_point.h@5ff8ce78059a |
children | dcfe615c4c5f |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/fixedp.h Sun Sep 22 02:25:41 2013 +0300 1.3 @@ -0,0 +1,106 @@ 1.4 +#ifndef FIXED_POINT_H_ 1.5 +#define FIXED_POINT_H_ 1.6 + 1.7 +#include "inttypes.h" 1.8 + 1.9 +#ifdef DBG_USE_FLOAT 1.10 +typedef float fixed; 1.11 +#else 1.12 +typedef int32_t fixed; 1.13 +#endif 1.14 + 1.15 +/* valid choices for DECIMAL_BITS 1.16 + * 8: for fixed point 24:8 1.17 + * 16: for fixed point 16:16 1.18 + */ 1.19 +#define DECIMAL_BITS 16 1.20 + 1.21 +#if DECIMAL_BITS == 8 1.22 +#define FIXED_SHIFT 8 1.23 +#define FLT_SCALE 256.0f 1.24 +#define FRAC_MASK 0xff 1.25 +#else /* DECIMAL_BITS == 16 */ 1.26 +#define FIXED_SHIFT 16 1.27 +#define FLT_SCALE 65536.0f 1.28 +#define FRAC_MASK 0xffff 1.29 +#endif /* DECIMAL_BITS */ 1.30 + 1.31 +/*extern const fixed fixed_zero; 1.32 +extern const fixed fixed_one; 1.33 +extern const fixed fixed_half; 1.34 +extern const fixed fixed_tenth; 1.35 +extern const fixed fixed_255;*/ 1.36 + 1.37 +#ifdef DBG_USE_FLOAT 1.38 +/* ------- debug mode, use floating point -------- */ 1.39 +#define FIXED_INT_PART(n) ((int)(n)) 1.40 +#define FIXED_FRAC_PART(n) ((n) > 0.0f ? (FIXED_INT_PART(n) - (n)) : (FIXED_INT_PART(n) + (n))) 1.41 +#define FIXED_ROUND(n) FIXED_INT_PART((n) >= 0.0 ? (n) + 0.5f : (n) - 0.5f) 1.42 + 1.43 +#define FIXED_TO_FLOAT(n) (n) 1.44 +#define FLOAT_TO_FIXED(n) (n) 1.45 +#define INT_TO_FIXED(n) ((float)(n)) 1.46 + 1.47 +#define FIXED_EPSILON (1e-6) 1.48 + 1.49 +#else /* ---- really fixed point ---- */ 1.50 + 1.51 +#define FIXED_INT_PART(n) ((n) >> FIXED_SHIFT) 1.52 +#define FIXED_FRAC_PART(n) ((n) & FRAC_MASK) 1.53 +#define FIXED_ROUND(n) FIXED_INT_PART((n) >= 0 ? (n) + fixedf(0.5) : (n) - fixedf(0.5)) 1.54 +/*#define FIXED_ROUND(n) FIXED_INT_PART(n)*/ 1.55 + 1.56 +#define FIXED_TO_FLOAT(n) (float)((n) / FLT_SCALE) 1.57 +#define FLOAT_TO_FIXED(n) (fixed)((n) * FLT_SCALE) 1.58 +#define INT_TO_FIXED(n) (fixed)((n) << FIXED_SHIFT) 1.59 + 1.60 +#define FIXED_EPSILON (1) 1.61 + 1.62 +#endif 1.63 + 1.64 + 1.65 +#define fixed_int(n) FIXED_INT_PART(n) 1.66 +#define fixed_frac(n) FIXED_FRAC_PART(n) 1.67 +#define fixed_float(n) FIXED_TO_FLOAT(n) 1.68 +#define fixed_round(n) FIXED_ROUND(n) 1.69 + 1.70 +#define fixedf(n) FLOAT_TO_FIXED(n) 1.71 +#define fixedi(n) INT_TO_FIXED(n) 1.72 + 1.73 +#define fixed_add(n1, n2) ((n1) + (n2)) 1.74 +#define fixed_sub(n1, n2) ((n1) - (n2)) 1.75 + 1.76 + 1.77 + 1.78 +#ifdef DBG_USE_FLOAT 1.79 + 1.80 +#define fixed_mul(n1, n2) ((n1) * (n2)) 1.81 +#define fixed_div(n1, n2) ((n1) / (n2)) 1.82 + 1.83 +#define fixed_sin(x) (fixed)sin(x) 1.84 +#define fixed_cos(x) (fixed)cos(x) 1.85 + 1.86 +#else 1.87 + 1.88 +#if DECIMAL_BITS == 8 1.89 +#define fixed_mul(n1, n2) (fixed)((n1) * (n2) >> FIXED_SHIFT) 1.90 +#define fixed_div(n1, n2) (((n1) << FIXED_SHIFT) / (n2)) 1.91 +#else 1.92 +#define fixed_div(n1, n2) (((int64_t)(n1) << FIXED_SHIFT) / (int64_t)(n2)) 1.93 +#define fixed_mul(n1, n2) (((n1) >> 8) * ((n2) >> 8)) 1.94 +#endif /* DECIMAL_BITS */ 1.95 + 1.96 +#ifdef __cplusplus 1.97 +extern "C" { 1.98 +#endif 1.99 + 1.100 +fixed fixed_sin(fixed angle); 1.101 +fixed fixed_cos(fixed angle); 1.102 + 1.103 +#ifdef __cplusplus 1.104 +} 1.105 +#endif 1.106 + 1.107 +#endif 1.108 + 1.109 +#endif /* FIXED_POINT_H_ */