gba-x3dtest

changeset 3:8e9225853d75

added sincos luts
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Jun 2014 01:56:58 +0300
parents 5143908d0220
children 78d1664c2443
files src/sincos.c src/sincos.h
diffstat 2 files changed, 69 insertions(+), 0 deletions(-) [+]
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 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/sincos.h	Sat Jun 14 01:56:58 2014 +0300
     2.3 @@ -0,0 +1,27 @@
     2.4 +#ifndef SINCOS_H_
     2.5 +#define SINCOS_H_
     2.6 +
     2.7 +#include <stdint.h>
     2.8 +
     2.9 +#define M_PI_X16	(int32_t)(M_PI * 65536.0)
    2.10 +/*#define M_PI_X16	(int32_t)((31416 << 16) / 10000)*/
    2.11 +
    2.12 +#define SINLUT_SCALE	512
    2.13 +#define SINLUT_SIZE		512
    2.14 +int16_t sinlut[SINLUT_SIZE];
    2.15 +
    2.16 +void sincos_init(void);
    2.17 +
    2.18 +/* takes angle in [0, SINLUT_SIZE] and returns:
    2.19 + * sin(2 * angle / SINLUT_SIZE / pi) * SINLUT_SCALE
    2.20 + */
    2.21 +int16_t sin_int(int16_t norm_angle);
    2.22 +int16_t cos_int(int16_t norm_angle);
    2.23 +
    2.24 +/* takes angle in fixed point 16.16 radians [0, 2pi << 16]
    2.25 + * and returns 16.16 fixed point in [-1 << 16, 1 << 16]
    2.26 + */
    2.27 +int32_t sin_x16(int32_t radians);
    2.28 +int32_t cos_x16(int32_t radians);
    2.29 +
    2.30 +#endif	/* SINCOS_H_ */