deepstone
annotate src/fixedp.c @ 27:dcfe615c4c5f
moved firstp.c to main.c
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 22 Sep 2013 02:47:46 +0300 |
parents | 5ff8ce78059a |
children |
rev | line source |
---|---|
nuclear@25 | 1 #include <math.h> |
nuclear@26 | 2 #include "fixedp.h" |
nuclear@25 | 3 |
nuclear@25 | 4 const fixed fixed_zero = 0; |
nuclear@25 | 5 const fixed fixed_one = fixedi(1); |
nuclear@25 | 6 const fixed fixed_half = fixedf(0.5); |
nuclear@25 | 7 const fixed fixed_tenth = fixedf(0.1); |
nuclear@25 | 8 const fixed fixed_255 = fixedi(255); |
nuclear@25 | 9 |
nuclear@25 | 10 #ifndef DBG_USE_FLOAT |
nuclear@25 | 11 |
nuclear@25 | 12 #define PI 3.1415927 |
nuclear@25 | 13 #define TWO_PI 6.2831853 |
nuclear@25 | 14 |
nuclear@25 | 15 #define LUT_SIZE 256 |
nuclear@25 | 16 |
nuclear@25 | 17 static fixed sin_lut[LUT_SIZE], cos_lut[LUT_SIZE]; |
nuclear@25 | 18 static int initialized; |
nuclear@25 | 19 |
nuclear@25 | 20 static void precalc_lut(void) |
nuclear@25 | 21 { |
nuclear@25 | 22 int i; |
nuclear@25 | 23 |
nuclear@25 | 24 for(i=0; i<LUT_SIZE; i++) { |
nuclear@25 | 25 float angle = TWO_PI * (float)i / (float)LUT_SIZE; |
nuclear@25 | 26 |
nuclear@25 | 27 sin_lut[i] = FLOAT_TO_FIXED(sin(angle)); |
nuclear@25 | 28 cos_lut[i] = FLOAT_TO_FIXED(cos(angle)); |
nuclear@25 | 29 } |
nuclear@25 | 30 |
nuclear@25 | 31 initialized = 1; |
nuclear@25 | 32 } |
nuclear@25 | 33 |
nuclear@25 | 34 static const fixed fix_two_pi = FLOAT_TO_FIXED(TWO_PI); |
nuclear@25 | 35 |
nuclear@25 | 36 fixed fixed_sin(fixed angle) { |
nuclear@25 | 37 int a; |
nuclear@25 | 38 |
nuclear@25 | 39 if(!initialized) precalc_lut(); |
nuclear@25 | 40 a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256; |
nuclear@25 | 41 return a >= 0 ? sin_lut[a] : -sin_lut[-a]; |
nuclear@25 | 42 } |
nuclear@25 | 43 |
nuclear@25 | 44 fixed fixed_cos(fixed angle) { |
nuclear@25 | 45 int a; |
nuclear@25 | 46 |
nuclear@25 | 47 if(!initialized) precalc_lut(); |
nuclear@25 | 48 a = FIXED_INT_PART(fixed_div(angle, fix_two_pi) * 255) % 256; |
nuclear@25 | 49 return a >= 0 ? cos_lut[a] : cos_lut[-a]; |
nuclear@25 | 50 } |
nuclear@25 | 51 |
nuclear@25 | 52 #endif |