deepstone
view src/fixedp.c @ 36:e234f2a4b6fa
ops
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 23 Sep 2013 07:42:56 +0300 |
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