2010-07-21 16:03:44 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS CRT
|
2015-09-19 13:50:57 +00:00
|
|
|
* FILE: lib/sdk/crt/math/sin.c
|
2010-07-21 16:03:44 +00:00
|
|
|
* PURPOSE: Generic C Implementation of sin
|
|
|
|
* PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
|
|
|
|
*/
|
|
|
|
|
2015-05-14 20:20:59 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(suppress:4164) /* intrinsic not declared */
|
|
|
|
#pragma function(sin)
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2010-07-21 16:03:44 +00:00
|
|
|
#define PRECISION 9
|
|
|
|
#define M_PI 3.141592653589793238462643
|
|
|
|
|
|
|
|
static double sin_off_tbl[] = {0.0, -M_PI/2., 0, -M_PI/2.};
|
|
|
|
static double sin_sign_tbl[] = {1,-1,-1,1};
|
|
|
|
|
|
|
|
double
|
|
|
|
sin(double x)
|
|
|
|
{
|
|
|
|
int quadrant;
|
|
|
|
double x2, result;
|
|
|
|
|
|
|
|
/* Calculate the quadrant */
|
2011-09-15 17:11:53 +00:00
|
|
|
quadrant = (int)(x * (2./M_PI));
|
2010-07-21 16:03:44 +00:00
|
|
|
|
|
|
|
/* Get offset inside quadrant */
|
|
|
|
x = x - quadrant * (M_PI/2.);
|
|
|
|
|
|
|
|
/* Normalize quadrant to [0..3] */
|
|
|
|
quadrant = (quadrant - 1) & 0x3;
|
|
|
|
|
|
|
|
/* Fixup value for the generic function */
|
|
|
|
x += sin_off_tbl[quadrant];
|
|
|
|
|
|
|
|
/* Calculate the negative of the square of x */
|
|
|
|
x2 = - (x * x);
|
|
|
|
|
2011-09-15 17:11:53 +00:00
|
|
|
/* This is an unrolled taylor series using <PRECISION> iterations
|
2010-07-21 16:03:44 +00:00
|
|
|
* Example with 4 iterations:
|
|
|
|
* result = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8!
|
|
|
|
* To save multiplications and to keep the precision high, it's performed
|
|
|
|
* like this:
|
|
|
|
* result = 1 - x^2 * (1/2! - x^2 * (1/4! - x^2 * (1/6! - x^2 * (1/8!))))
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Start with 0, compiler will optimize this away */
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
#if (PRECISION >= 10)
|
|
|
|
result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
|
|
|
|
result *= x2;
|
|
|
|
#endif
|
|
|
|
#if (PRECISION >= 9)
|
|
|
|
result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18);
|
|
|
|
result *= x2;
|
|
|
|
#endif
|
|
|
|
#if (PRECISION >= 8)
|
|
|
|
result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16);
|
|
|
|
result *= x2;
|
|
|
|
#endif
|
|
|
|
#if (PRECISION >= 7)
|
|
|
|
result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14);
|
|
|
|
result *= x2;
|
|
|
|
#endif
|
|
|
|
#if (PRECISION >= 6)
|
|
|
|
result += 1./(1.*2*3*4*5*6*7*8*9*10*11*12);
|
|
|
|
result *= x2;
|
|
|
|
#endif
|
|
|
|
#if (PRECISION >= 5)
|
|
|
|
result += 1./(1.*2*3*4*5*6*7*8*9*10);
|
|
|
|
result *= x2;
|
|
|
|
#endif
|
|
|
|
result += 1./(1.*2*3*4*5*6*7*8);
|
|
|
|
result *= x2;
|
|
|
|
|
|
|
|
result += 1./(1.*2*3*4*5*6);
|
|
|
|
result *= x2;
|
|
|
|
|
|
|
|
result += 1./(1.*2*3*4);
|
|
|
|
result *= x2;
|
|
|
|
|
|
|
|
result += 1./(1.*2);
|
|
|
|
result *= x2;
|
|
|
|
|
|
|
|
result += 1;
|
|
|
|
|
|
|
|
/* Apply correct sign */
|
|
|
|
result *= sin_sign_tbl[quadrant];
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|