deepstone
changeset 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 | 5ff8ce78059a |
children | dcfe615c4c5f |
files | Makefile src/fixed_point.c src/fixed_point.h src/fixedp.c src/fixedp.h src/vmath.h |
diffstat | 6 files changed, 160 insertions(+), 160 deletions(-) [+] |
line diff
1.1 --- a/Makefile Sun Sep 22 02:21:30 2013 +0300 1.2 +++ b/Makefile Sun Sep 22 02:25:41 2013 +0300 1.3 @@ -1,7 +1,7 @@ 1.4 obj = src/test.o \ 1.5 src/mingl.o src/mglrast.o src/mglgen.o \ 1.6 src/texture.o src/palman.o \ 1.7 - src/scene.o src/cvec.o src/fixed_point.o \ 1.8 + src/scene.o src/cvec.o src/fixedp.o \ 1.9 dosemu/dosemu.o 1.10 dep = $(obj:.o=.d) 1.11 bin = deepstone
2.1 --- a/src/fixed_point.c Sun Sep 22 02:21:30 2013 +0300 2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 @@ -1,52 +0,0 @@ 2.4 -#include <math.h> 2.5 -#include "fixed_point.h" 2.6 - 2.7 -const fixed fixed_zero = 0; 2.8 -const fixed fixed_one = fixedi(1); 2.9 -const fixed fixed_half = fixedf(0.5); 2.10 -const fixed fixed_tenth = fixedf(0.1); 2.11 -const fixed fixed_255 = fixedi(255); 2.12 - 2.13 -#ifndef DBG_USE_FLOAT 2.14 - 2.15 -#define PI 3.1415927 2.16 -#define TWO_PI 6.2831853 2.17 - 2.18 -#define LUT_SIZE 256 2.19 - 2.20 -static fixed sin_lut[LUT_SIZE], cos_lut[LUT_SIZE]; 2.21 -static int initialized; 2.22 - 2.23 -static void precalc_lut(void) 2.24 -{ 2.25 - int i; 2.26 - 2.27 - for(i=0; i<LUT_SIZE; i++) { 2.28 - float angle = TWO_PI * (float)i / (float)LUT_SIZE; 2.29 - 2.30 - sin_lut[i] = FLOAT_TO_FIXED(sin(angle)); 2.31 - cos_lut[i] = FLOAT_TO_FIXED(cos(angle)); 2.32 - } 2.33 - 2.34 - initialized = 1; 2.35 -} 2.36 - 2.37 -static const fixed fix_two_pi = FLOAT_TO_FIXED(TWO_PI); 2.38 - 2.39 -fixed fixed_sin(fixed angle) { 2.40 - int a; 2.41 - 2.42 - if(!initialized) precalc_lut(); 2.43 - a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256; 2.44 - return a >= 0 ? sin_lut[a] : -sin_lut[-a]; 2.45 -} 2.46 - 2.47 -fixed fixed_cos(fixed angle) { 2.48 - int a; 2.49 - 2.50 - if(!initialized) precalc_lut(); 2.51 - a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256; 2.52 - return a >= 0 ? cos_lut[a] : cos_lut[-a]; 2.53 -} 2.54 - 2.55 -#endif
3.1 --- a/src/fixed_point.h Sun Sep 22 02:21:30 2013 +0300 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,106 +0,0 @@ 3.4 -#ifndef FIXED_POINT_H_ 3.5 -#define FIXED_POINT_H_ 3.6 - 3.7 -#include "inttypes.h" 3.8 - 3.9 -#ifdef DBG_USE_FLOAT 3.10 -typedef float fixed; 3.11 -#else 3.12 -typedef int32_t fixed; 3.13 -#endif 3.14 - 3.15 -/* valid choices for DECIMAL_BITS 3.16 - * 8: for fixed point 24:8 3.17 - * 16: for fixed point 16:16 3.18 - */ 3.19 -#define DECIMAL_BITS 16 3.20 - 3.21 -#if DECIMAL_BITS == 8 3.22 -#define FIXED_SHIFT 8 3.23 -#define FLT_SCALE 256.0f 3.24 -#define FRAC_MASK 0xff 3.25 -#else /* DECIMAL_BITS == 16 */ 3.26 -#define FIXED_SHIFT 16 3.27 -#define FLT_SCALE 65536.0f 3.28 -#define FRAC_MASK 0xffff 3.29 -#endif /* DECIMAL_BITS */ 3.30 - 3.31 -/*extern const fixed fixed_zero; 3.32 -extern const fixed fixed_one; 3.33 -extern const fixed fixed_half; 3.34 -extern const fixed fixed_tenth; 3.35 -extern const fixed fixed_255;*/ 3.36 - 3.37 -#ifdef DBG_USE_FLOAT 3.38 -/* ------- debug mode, use floating point -------- */ 3.39 -#define FIXED_INT_PART(n) ((int)(n)) 3.40 -#define FIXED_FRAC_PART(n) ((n) > 0.0f ? (FIXED_INT_PART(n) - (n)) : (FIXED_INT_PART(n) + (n))) 3.41 -#define FIXED_ROUND(n) FIXED_INT_PART((n) >= 0.0 ? (n) + 0.5f : (n) - 0.5f) 3.42 - 3.43 -#define FIXED_TO_FLOAT(n) (n) 3.44 -#define FLOAT_TO_FIXED(n) (n) 3.45 -#define INT_TO_FIXED(n) ((float)(n)) 3.46 - 3.47 -#define FIXED_EPSILON (1e-6) 3.48 - 3.49 -#else /* ---- really fixed point ---- */ 3.50 - 3.51 -#define FIXED_INT_PART(n) ((n) >> FIXED_SHIFT) 3.52 -#define FIXED_FRAC_PART(n) ((n) & FRAC_MASK) 3.53 -#define FIXED_ROUND(n) FIXED_INT_PART((n) >= 0 ? (n) + fixedf(0.5) : (n) - fixedf(0.5)) 3.54 -/*#define FIXED_ROUND(n) FIXED_INT_PART(n)*/ 3.55 - 3.56 -#define FIXED_TO_FLOAT(n) (float)((n) / FLT_SCALE) 3.57 -#define FLOAT_TO_FIXED(n) (fixed)((n) * FLT_SCALE) 3.58 -#define INT_TO_FIXED(n) (fixed)((n) << FIXED_SHIFT) 3.59 - 3.60 -#define FIXED_EPSILON (1) 3.61 - 3.62 -#endif 3.63 - 3.64 - 3.65 -#define fixed_int(n) FIXED_INT_PART(n) 3.66 -#define fixed_frac(n) FIXED_FRAC_PART(n) 3.67 -#define fixed_float(n) FIXED_TO_FLOAT(n) 3.68 -#define fixed_round(n) FIXED_ROUND(n) 3.69 - 3.70 -#define fixedf(n) FLOAT_TO_FIXED(n) 3.71 -#define fixedi(n) INT_TO_FIXED(n) 3.72 - 3.73 -#define fixed_add(n1, n2) ((n1) + (n2)) 3.74 -#define fixed_sub(n1, n2) ((n1) - (n2)) 3.75 - 3.76 - 3.77 - 3.78 -#ifdef DBG_USE_FLOAT 3.79 - 3.80 -#define fixed_mul(n1, n2) ((n1) * (n2)) 3.81 -#define fixed_div(n1, n2) ((n1) / (n2)) 3.82 - 3.83 -#define fixed_sin(x) (fixed)sin(x) 3.84 -#define fixed_cos(x) (fixed)cos(x) 3.85 - 3.86 -#else 3.87 - 3.88 -#if DECIMAL_BITS == 8 3.89 -#define fixed_mul(n1, n2) (fixed)((n1) * (n2) >> FIXED_SHIFT) 3.90 -#define fixed_div(n1, n2) (((n1) << FIXED_SHIFT) / (n2)) 3.91 -#else 3.92 -#define fixed_div(n1, n2) (((int64_t)(n1) << FIXED_SHIFT) / (int64_t)(n2)) 3.93 -#define fixed_mul(n1, n2) (((n1) >> 8) * ((n2) >> 8)) 3.94 -#endif /* DECIMAL_BITS */ 3.95 - 3.96 -#ifdef __cplusplus 3.97 -extern "C" { 3.98 -#endif 3.99 - 3.100 -fixed fixed_sin(fixed angle); 3.101 -fixed fixed_cos(fixed angle); 3.102 - 3.103 -#ifdef __cplusplus 3.104 -} 3.105 -#endif 3.106 - 3.107 -#endif 3.108 - 3.109 -#endif /* FIXED_POINT_H_ */
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/fixedp.c Sun Sep 22 02:25:41 2013 +0300 4.3 @@ -0,0 +1,52 @@ 4.4 +#include <math.h> 4.5 +#include "fixedp.h" 4.6 + 4.7 +const fixed fixed_zero = 0; 4.8 +const fixed fixed_one = fixedi(1); 4.9 +const fixed fixed_half = fixedf(0.5); 4.10 +const fixed fixed_tenth = fixedf(0.1); 4.11 +const fixed fixed_255 = fixedi(255); 4.12 + 4.13 +#ifndef DBG_USE_FLOAT 4.14 + 4.15 +#define PI 3.1415927 4.16 +#define TWO_PI 6.2831853 4.17 + 4.18 +#define LUT_SIZE 256 4.19 + 4.20 +static fixed sin_lut[LUT_SIZE], cos_lut[LUT_SIZE]; 4.21 +static int initialized; 4.22 + 4.23 +static void precalc_lut(void) 4.24 +{ 4.25 + int i; 4.26 + 4.27 + for(i=0; i<LUT_SIZE; i++) { 4.28 + float angle = TWO_PI * (float)i / (float)LUT_SIZE; 4.29 + 4.30 + sin_lut[i] = FLOAT_TO_FIXED(sin(angle)); 4.31 + cos_lut[i] = FLOAT_TO_FIXED(cos(angle)); 4.32 + } 4.33 + 4.34 + initialized = 1; 4.35 +} 4.36 + 4.37 +static const fixed fix_two_pi = FLOAT_TO_FIXED(TWO_PI); 4.38 + 4.39 +fixed fixed_sin(fixed angle) { 4.40 + int a; 4.41 + 4.42 + if(!initialized) precalc_lut(); 4.43 + a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256; 4.44 + return a >= 0 ? sin_lut[a] : -sin_lut[-a]; 4.45 +} 4.46 + 4.47 +fixed fixed_cos(fixed angle) { 4.48 + int a; 4.49 + 4.50 + if(!initialized) precalc_lut(); 4.51 + a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256; 4.52 + return a >= 0 ? cos_lut[a] : cos_lut[-a]; 4.53 +} 4.54 + 4.55 +#endif
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/fixedp.h Sun Sep 22 02:25:41 2013 +0300 5.3 @@ -0,0 +1,106 @@ 5.4 +#ifndef FIXED_POINT_H_ 5.5 +#define FIXED_POINT_H_ 5.6 + 5.7 +#include "inttypes.h" 5.8 + 5.9 +#ifdef DBG_USE_FLOAT 5.10 +typedef float fixed; 5.11 +#else 5.12 +typedef int32_t fixed; 5.13 +#endif 5.14 + 5.15 +/* valid choices for DECIMAL_BITS 5.16 + * 8: for fixed point 24:8 5.17 + * 16: for fixed point 16:16 5.18 + */ 5.19 +#define DECIMAL_BITS 16 5.20 + 5.21 +#if DECIMAL_BITS == 8 5.22 +#define FIXED_SHIFT 8 5.23 +#define FLT_SCALE 256.0f 5.24 +#define FRAC_MASK 0xff 5.25 +#else /* DECIMAL_BITS == 16 */ 5.26 +#define FIXED_SHIFT 16 5.27 +#define FLT_SCALE 65536.0f 5.28 +#define FRAC_MASK 0xffff 5.29 +#endif /* DECIMAL_BITS */ 5.30 + 5.31 +/*extern const fixed fixed_zero; 5.32 +extern const fixed fixed_one; 5.33 +extern const fixed fixed_half; 5.34 +extern const fixed fixed_tenth; 5.35 +extern const fixed fixed_255;*/ 5.36 + 5.37 +#ifdef DBG_USE_FLOAT 5.38 +/* ------- debug mode, use floating point -------- */ 5.39 +#define FIXED_INT_PART(n) ((int)(n)) 5.40 +#define FIXED_FRAC_PART(n) ((n) > 0.0f ? (FIXED_INT_PART(n) - (n)) : (FIXED_INT_PART(n) + (n))) 5.41 +#define FIXED_ROUND(n) FIXED_INT_PART((n) >= 0.0 ? (n) + 0.5f : (n) - 0.5f) 5.42 + 5.43 +#define FIXED_TO_FLOAT(n) (n) 5.44 +#define FLOAT_TO_FIXED(n) (n) 5.45 +#define INT_TO_FIXED(n) ((float)(n)) 5.46 + 5.47 +#define FIXED_EPSILON (1e-6) 5.48 + 5.49 +#else /* ---- really fixed point ---- */ 5.50 + 5.51 +#define FIXED_INT_PART(n) ((n) >> FIXED_SHIFT) 5.52 +#define FIXED_FRAC_PART(n) ((n) & FRAC_MASK) 5.53 +#define FIXED_ROUND(n) FIXED_INT_PART((n) >= 0 ? (n) + fixedf(0.5) : (n) - fixedf(0.5)) 5.54 +/*#define FIXED_ROUND(n) FIXED_INT_PART(n)*/ 5.55 + 5.56 +#define FIXED_TO_FLOAT(n) (float)((n) / FLT_SCALE) 5.57 +#define FLOAT_TO_FIXED(n) (fixed)((n) * FLT_SCALE) 5.58 +#define INT_TO_FIXED(n) (fixed)((n) << FIXED_SHIFT) 5.59 + 5.60 +#define FIXED_EPSILON (1) 5.61 + 5.62 +#endif 5.63 + 5.64 + 5.65 +#define fixed_int(n) FIXED_INT_PART(n) 5.66 +#define fixed_frac(n) FIXED_FRAC_PART(n) 5.67 +#define fixed_float(n) FIXED_TO_FLOAT(n) 5.68 +#define fixed_round(n) FIXED_ROUND(n) 5.69 + 5.70 +#define fixedf(n) FLOAT_TO_FIXED(n) 5.71 +#define fixedi(n) INT_TO_FIXED(n) 5.72 + 5.73 +#define fixed_add(n1, n2) ((n1) + (n2)) 5.74 +#define fixed_sub(n1, n2) ((n1) - (n2)) 5.75 + 5.76 + 5.77 + 5.78 +#ifdef DBG_USE_FLOAT 5.79 + 5.80 +#define fixed_mul(n1, n2) ((n1) * (n2)) 5.81 +#define fixed_div(n1, n2) ((n1) / (n2)) 5.82 + 5.83 +#define fixed_sin(x) (fixed)sin(x) 5.84 +#define fixed_cos(x) (fixed)cos(x) 5.85 + 5.86 +#else 5.87 + 5.88 +#if DECIMAL_BITS == 8 5.89 +#define fixed_mul(n1, n2) (fixed)((n1) * (n2) >> FIXED_SHIFT) 5.90 +#define fixed_div(n1, n2) (((n1) << FIXED_SHIFT) / (n2)) 5.91 +#else 5.92 +#define fixed_div(n1, n2) (((int64_t)(n1) << FIXED_SHIFT) / (int64_t)(n2)) 5.93 +#define fixed_mul(n1, n2) (((n1) >> 8) * ((n2) >> 8)) 5.94 +#endif /* DECIMAL_BITS */ 5.95 + 5.96 +#ifdef __cplusplus 5.97 +extern "C" { 5.98 +#endif 5.99 + 5.100 +fixed fixed_sin(fixed angle); 5.101 +fixed fixed_cos(fixed angle); 5.102 + 5.103 +#ifdef __cplusplus 5.104 +} 5.105 +#endif 5.106 + 5.107 +#endif 5.108 + 5.109 +#endif /* FIXED_POINT_H_ */