commit 952e57ec42b92f4a7a2bce2d04904f625ec98f11 Author: Jens Ahrensfeld Date: Sat Jul 19 07:44:42 2014 +0000 Initial import git-svn-id: http://moon:8086/svn/software/trunk/libsrc/fast_trig@1 b431acfa-c32f-4a4a-93f1-934dc6c82436 diff --git a/fast_trig.c b/fast_trig.c new file mode 100755 index 0000000..ee8ad5e --- /dev/null +++ b/fast_trig.c @@ -0,0 +1,157 @@ +// -------------------------------------------------------------- +#define SINE_TABLE_START_INDEX 2 +#define COSINE_TABLE_START_INDEX 2 +#define EXP_TABLE_START_INDEX 0 + +// -------------------------------------------------------------- +#include +#include "synth_defs.h" + +// Taylor series coefficent for cos(pi*x) +// In this table x is premultiplied with pi! +static const synth_float_t _sg_cos_pmpi_lut[] = +{ + 1.92957430940392e-003, + -25.8068913900141e-003, + 235.330630358893e-003, + -1.33526276885459e+000, + 4.05871212641677e+000, + -4.93480220054468e+000, + 1.00000000000000e+000 + +}; + +// Taylor series coefficent for sin(x) +static const synth_float_t _sg_sin_lut[] = +{ + 1.605904383682161e-010, + -2.505210838544172e-008, + 2.755731922398589e-006, + -1.984126984126984e-004, + 8.333333333333333e-003, + -1.666666666666667e-001, + 1.000000000000000e+000 +}; + +// Taylor series coefficent for sin(x) +static const synth_float_t _sg_cos_lut[] = +{ + 2.087675698786810e-009, + -2.755731922398589e-007, + 2.480158730158730e-005, + -1.388888888888889e-003, + 4.166666666666666e-002, + -5.000000000000000e-001, + 1.000000000000000e+000 +}; + +// Taylor series coefficent for sin(x) +static const synth_float_t _sg_exp_lut[] = +{ + 1.984126984126984e-004, + 1.388888888888889e-003, + 8.333333333333333e-003, + 4.166666666666666e-002, + 1.666666666666667e-001, + 5.000000000000000e-001, + 1.000000000000000e+000, + 1.000000000000000e+000 +}; + +// -------------------------------------------------------------- +// Exported functions +// -------------------------------------------------------------- +// Cos (pi*x) +synth_float_t FastTrig_cos_premult_pi(synth_float_t x) +{ + int i, yneg; + synth_float_t y, xx; + + yneg = 0; + + if (x < 0) + x = -x; + + if (x > (synth_float_t)0.5) + { + if (x > (synth_float_t)1.5) + { + x = (synth_float_t)2. - x; + } + else + { + x = (synth_float_t)1. - x; + yneg = 1; + } + } + xx = x*x; + y = _sg_cos_pmpi_lut[0]; + + for (i=1; i < sizeof(_sg_cos_pmpi_lut)/sizeof(synth_float_t); i++) + y = y * xx + _sg_cos_pmpi_lut[i]; + + if (yneg) + y = -y; + + return y; +} + +// sin (x) +// Best in range -0.5*pi .. +0.5*pi +synth_float_t FastTrig_sin(synth_float_t x) +{ + int i = SINE_TABLE_START_INDEX; + synth_float_t xx, y; + + xx = x*x; + y = _sg_sin_lut[i++]; + + for (i=i; i < sizeof(_sg_sin_lut)/sizeof(synth_float_t); i++) + y = y * xx + _sg_sin_lut[i]; + + return y * x; +} + +// cos (x) +// Best in range -0.5*pi .. +0.5*pi +synth_float_t FastTrig_cos(synth_float_t x) +{ + int i = COSINE_TABLE_START_INDEX; + synth_float_t xx, y; + + xx = x*x; + y = _sg_cos_lut[i++]; + + for (i=i; i < sizeof(_sg_cos_lut)/sizeof(synth_float_t); i++) + y = y * xx + _sg_cos_lut[i]; + + return y; +} + +// exp (x) +synth_float_t FastTrig_exp(synth_float_t x) +{ + int i = EXP_TABLE_START_INDEX; + synth_float_t y; + + y = _sg_exp_lut[i++]; + + for (i=i; i < sizeof(_sg_exp_lut)/sizeof(synth_float_t); i++) + y = y * x + _sg_exp_lut[i]; + + return y; +} + +// exp (x) - 1 +synth_float_t FastTrig_exp_minus_1(synth_float_t x) +{ + int i = EXP_TABLE_START_INDEX; + synth_float_t y; + + y = _sg_exp_lut[i++]; + + for (i=i; i < sizeof(_sg_exp_lut)/sizeof(synth_float_t)-1; i++) + y = y * x + _sg_exp_lut[i]; + + return y*x; +} \ No newline at end of file diff --git a/fast_trig.h b/fast_trig.h new file mode 100755 index 0000000..113a81d --- /dev/null +++ b/fast_trig.h @@ -0,0 +1,31 @@ +// -------------------------------------------------------------- +#ifndef _FAST_TRIG_H_ +#define _FAST_TRIG_H_ + +#include "synth_types.h" + +// -------------------------------------------------------------- +// Types +// -------------------------------------------------------------- + +// -------------------------------------------------------------- +#if defined(__cplusplus) +extern "C" { +#endif + +// -------------------------------------------------------------- +// Exported functions +// -------------------------------------------------------------- +synth_float_t FastTrig_cos_premult_pi(synth_float_t x); +synth_float_t FastTrig_sin(synth_float_t x); +synth_float_t FastTrig_cos(synth_float_t x); +synth_float_t FastTrig_exp(synth_float_t x); +synth_float_t FastTrig_exp_minus_1(synth_float_t x); + +#if defined(__cplusplus) +} + +#endif +// -------------------------------------------------------------- + +#endif // _FAST_TRIG_H_