gba-x3dtest
annotate src/sincos.c @ 3:8e9225853d75
added sincos luts
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 14 Jun 2014 01:56:58 +0300 |
parents | |
children | 78d1664c2443 |
rev | line source |
---|---|
nuclear@3 | 1 #include <math.h> |
nuclear@3 | 2 #include "sincos.h" |
nuclear@3 | 3 #include "logger.h" |
nuclear@3 | 4 |
nuclear@3 | 5 void sincos_init(void) |
nuclear@3 | 6 { |
nuclear@3 | 7 int i; |
nuclear@3 | 8 |
nuclear@3 | 9 logmsg(LOG_ALL, "calculating sin/cos lut...\n"); |
nuclear@3 | 10 |
nuclear@3 | 11 for(i=0; i<SINLUT_SIZE; i++) { |
nuclear@3 | 12 float angle = 2.0 * M_PI * ((float)i / (float)SINLUT_SIZE); |
nuclear@3 | 13 float val = sin(angle); |
nuclear@3 | 14 sinlut[i] = (int16_t)(val * SINLUT_SCALE); |
nuclear@3 | 15 } |
nuclear@3 | 16 } |
nuclear@3 | 17 |
nuclear@3 | 18 int16_t sin_int(int16_t norm_angle) |
nuclear@3 | 19 { |
nuclear@3 | 20 norm_angle %= SINLUT_SIZE; |
nuclear@3 | 21 if(norm_angle < 0) { |
nuclear@3 | 22 norm_angle += SINLUT_SIZE; |
nuclear@3 | 23 } |
nuclear@3 | 24 return sinlut[norm_angle]; |
nuclear@3 | 25 } |
nuclear@3 | 26 |
nuclear@3 | 27 int16_t cos_int(int16_t norm_angle) |
nuclear@3 | 28 { |
nuclear@3 | 29 return sin_int(norm_angle + SINLUT_SIZE / 4); |
nuclear@3 | 30 } |
nuclear@3 | 31 |
nuclear@3 | 32 int32_t sin_x16(int32_t radians) |
nuclear@3 | 33 { |
nuclear@3 | 34 int32_t na = (radians << 16) / (M_PI_X16 * 2); |
nuclear@3 | 35 return sin_int((na >> 8) * (SINLUT_SIZE << 8)); |
nuclear@3 | 36 } |
nuclear@3 | 37 |
nuclear@3 | 38 int32_t cos_x16(int32_t radians) |
nuclear@3 | 39 { |
nuclear@3 | 40 int32_t na = (radians << 16) / (M_PI_X16 * 2); |
nuclear@3 | 41 return cos_int((na >> 8) * (SINLUT_SIZE << 8)); |
nuclear@3 | 42 } |