diff --git a/reactos/lib/sdk/crt/crt.rbuild b/reactos/lib/sdk/crt/crt.rbuild index b763d97727e..c33cb823b31 100644 --- a/reactos/lib/sdk/crt/crt.rbuild +++ b/reactos/lib/sdk/crt/crt.rbuild @@ -162,6 +162,7 @@ cos.c + sin.c alldiv.S atan.S @@ -178,7 +179,6 @@ log.S log10.S pow.S - sin.S sqrt.S sqrtf.S tan.S diff --git a/reactos/lib/sdk/crt/libcntpr.rbuild b/reactos/lib/sdk/crt/libcntpr.rbuild index deb0db03718..2ef42f05913 100644 --- a/reactos/lib/sdk/crt/libcntpr.rbuild +++ b/reactos/lib/sdk/crt/libcntpr.rbuild @@ -72,7 +72,6 @@ log.S log10.S pow.S - sin.S sqrt.S tan.S diff --git a/reactos/lib/sdk/crt/math/amd64/sin.S b/reactos/lib/sdk/crt/math/amd64/sin.S deleted file mode 100644 index 683eedcd647..00000000000 --- a/reactos/lib/sdk/crt/math/amd64/sin.S +++ /dev/null @@ -1,21 +0,0 @@ -/* - * COPYRIGHT: See COPYING in the top level directory - * PROJECT: ReactOS system libraries - * PURPOSE: Implementation of sin - * FILE: lib/sdk/crt/math/amd64/sin.S - * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org) - */ - -/* INCLUDES ******************************************************************/ - -#include -#include - -.intel_syntax noprefix - - -.proc sin - UNIMPLEMENTED sin - ret - -.endproc diff --git a/reactos/lib/sdk/crt/math/sin.c b/reactos/lib/sdk/crt/math/sin.c new file mode 100644 index 00000000000..de99d50ee74 --- /dev/null +++ b/reactos/lib/sdk/crt/math/sin.c @@ -0,0 +1,89 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS CRT + * FILE: lib/crt/math/sin.c + * PURPOSE: Generic C Implementation of sin + * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org) + */ + +#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 */ + quadrant = x * (2./M_PI); + + /* 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); + + /* This is an unrolled taylor series using iterations + * 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*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; +}