deepstone

view src/fixedp.c @ 39:f9b1ff21fd62

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 10 Mar 2014 17:28:28 +0200
parents 5ff8ce78059a
children
line source
1 #include <math.h>
2 #include "fixedp.h"
4 const fixed fixed_zero = 0;
5 const fixed fixed_one = fixedi(1);
6 const fixed fixed_half = fixedf(0.5);
7 const fixed fixed_tenth = fixedf(0.1);
8 const fixed fixed_255 = fixedi(255);
10 #ifndef DBG_USE_FLOAT
12 #define PI 3.1415927
13 #define TWO_PI 6.2831853
15 #define LUT_SIZE 256
17 static fixed sin_lut[LUT_SIZE], cos_lut[LUT_SIZE];
18 static int initialized;
20 static void precalc_lut(void)
21 {
22 int i;
24 for(i=0; i<LUT_SIZE; i++) {
25 float angle = TWO_PI * (float)i / (float)LUT_SIZE;
27 sin_lut[i] = FLOAT_TO_FIXED(sin(angle));
28 cos_lut[i] = FLOAT_TO_FIXED(cos(angle));
29 }
31 initialized = 1;
32 }
34 static const fixed fix_two_pi = FLOAT_TO_FIXED(TWO_PI);
36 fixed fixed_sin(fixed angle) {
37 int a;
39 if(!initialized) precalc_lut();
40 a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256;
41 return a >= 0 ? sin_lut[a] : -sin_lut[-a];
42 }
44 fixed fixed_cos(fixed angle) {
45 int a;
47 if(!initialized) precalc_lut();
48 a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256;
49 return a >= 0 ? cos_lut[a] : cos_lut[-a];
50 }
52 #endif