deepstone

diff src/fixed_point.c @ 25:5ff8ce78059a

first pass at converting the rasterizer to fixed point
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Sep 2013 02:21:30 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/fixed_point.c	Sun Sep 22 02:21:30 2013 +0300
     1.3 @@ -0,0 +1,52 @@
     1.4 +#include <math.h>
     1.5 +#include "fixed_point.h"
     1.6 +
     1.7 +const fixed fixed_zero = 0;
     1.8 +const fixed fixed_one = fixedi(1);
     1.9 +const fixed fixed_half = fixedf(0.5);
    1.10 +const fixed fixed_tenth = fixedf(0.1);
    1.11 +const fixed fixed_255 = fixedi(255);
    1.12 +
    1.13 +#ifndef DBG_USE_FLOAT
    1.14 +
    1.15 +#define PI			3.1415927
    1.16 +#define TWO_PI		6.2831853
    1.17 +
    1.18 +#define LUT_SIZE	256
    1.19 +
    1.20 +static fixed sin_lut[LUT_SIZE], cos_lut[LUT_SIZE];
    1.21 +static int initialized;
    1.22 +
    1.23 +static void precalc_lut(void)
    1.24 +{
    1.25 +	int i;
    1.26 +
    1.27 +	for(i=0; i<LUT_SIZE; i++) {
    1.28 +		float angle = TWO_PI * (float)i / (float)LUT_SIZE;
    1.29 +
    1.30 +		sin_lut[i] = FLOAT_TO_FIXED(sin(angle));
    1.31 +		cos_lut[i] = FLOAT_TO_FIXED(cos(angle));
    1.32 +	}
    1.33 +
    1.34 +	initialized = 1;
    1.35 +}
    1.36 +
    1.37 +static const fixed fix_two_pi = FLOAT_TO_FIXED(TWO_PI);
    1.38 +
    1.39 +fixed fixed_sin(fixed angle) {
    1.40 +	int a;
    1.41 +
    1.42 +	if(!initialized) precalc_lut();
    1.43 +	a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256;
    1.44 +	return a >= 0 ? sin_lut[a] : -sin_lut[-a];
    1.45 +}
    1.46 +
    1.47 +fixed fixed_cos(fixed angle) {
    1.48 +	int a;
    1.49 +
    1.50 +	if(!initialized) precalc_lut();
    1.51 +	a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256;
    1.52 +	return a >= 0 ? cos_lut[a] : cos_lut[-a];
    1.53 +}
    1.54 +
    1.55 +#endif