gba-x3dtest

diff 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
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sincos.c	Sat Jun 14 01:56:58 2014 +0300
     1.3 @@ -0,0 +1,42 @@
     1.4 +#include <math.h>
     1.5 +#include "sincos.h"
     1.6 +#include "logger.h"
     1.7 +
     1.8 +void sincos_init(void)
     1.9 +{
    1.10 +	int i;
    1.11 +
    1.12 +	logmsg(LOG_ALL, "calculating sin/cos lut...\n");
    1.13 +
    1.14 +	for(i=0; i<SINLUT_SIZE; i++) {
    1.15 +		float angle = 2.0 * M_PI * ((float)i / (float)SINLUT_SIZE);
    1.16 +		float val = sin(angle);
    1.17 +		sinlut[i] = (int16_t)(val * SINLUT_SCALE);
    1.18 +	}
    1.19 +}
    1.20 +
    1.21 +int16_t sin_int(int16_t norm_angle)
    1.22 +{
    1.23 +	norm_angle %= SINLUT_SIZE;
    1.24 +	if(norm_angle < 0) {
    1.25 +		norm_angle += SINLUT_SIZE;
    1.26 +	}
    1.27 +	return sinlut[norm_angle];
    1.28 +}
    1.29 +
    1.30 +int16_t cos_int(int16_t norm_angle)
    1.31 +{
    1.32 +	return sin_int(norm_angle + SINLUT_SIZE / 4);
    1.33 +}
    1.34 +
    1.35 +int32_t sin_x16(int32_t radians)
    1.36 +{
    1.37 +	int32_t na = (radians << 16) / (M_PI_X16 * 2);
    1.38 +	return sin_int((na >> 8) * (SINLUT_SIZE << 8));
    1.39 +}
    1.40 +
    1.41 +int32_t cos_x16(int32_t radians)
    1.42 +{
    1.43 +	int32_t na = (radians << 16) / (M_PI_X16 * 2);
    1.44 +	return cos_int((na >> 8) * (SINLUT_SIZE << 8));
    1.45 +}