1e7d8c4206c86ce316415b19bca35e5b9fad04ae
2 * COPYRIGHT: See COPYING in the top level directory
4 * FILE: lib/crt/math/cos.c
5 * PURPOSE: Generic C Implementation of cos
6 * PROGRAMMER: Timo Kreuzer (timo.kreuzer@reactos.org)
10 #define M_PI 3.141592653589793238462643
12 static double cos_off_tbl
[] = {0.0, -M_PI
/2., 0, -M_PI
/2.};
13 static double cos_sign_tbl
[] = {1,-1,-1,1};
21 /* Calculate the quadrant */
22 quadrant
= x
* (2./M_PI
);
24 /* Get offset inside quadrant */
25 x
= x
- quadrant
* (M_PI
/2.);
27 /* Normalize quadrant to [0..3] */
28 quadrant
= quadrant
& 0x3;
30 /* Fixup value for the generic function */
31 x
+= cos_off_tbl
[quadrant
];
33 /* Calculate the negative of the square of x */
36 /* This is an unrolled taylor series using <PRECISION> iterations
37 * Example with 4 iterations:
38 * result = 1 - x^2/2! + x^4/4! - x^6/6! + x^8/8!
39 * To save multiplications and to keep the precision high, it's performed
41 * result = 1 - x^2 * (1/2! - x^2 * (1/4! - x^2 * (1/6! - x^2 * (1/8!))))
44 /* Start with 0, compiler will optimize this away */
48 result
+= 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20);
52 result
+= 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18);
56 result
+= 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16);
60 result
+= 1./(1.*2*3*4*5*6*7*8*9*10*11*12*13*14);
64 result
+= 1./(1.*2*3*4*5*6*7*8*9*10*11*12);
68 result
+= 1./(1.*2*3*4*5*6*7*8*9*10);
71 result
+= 1./(1.*2*3*4*5*6*7*8);
74 result
+= 1./(1.*2*3*4*5*6);
77 result
+= 1./(1.*2*3*4);
85 /* Apply correct sign */
86 result
*= cos_sign_tbl
[quadrant
];