1 /* origin: FreeBSD /usr/src/lib/msun/ld80/k_cosl.c */
2 /* origin: FreeBSD /usr/src/lib/msun/ld128/k_cosl.c */
3 /*
4  * ====================================================
5  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6  * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
7  *
8  * Developed at SunSoft, a Sun Microsystems, Inc. business.
9  * Permission to use, copy, modify, and distribute this
10  * software is freely granted, provided that this notice
11  * is preserved.
12  * ====================================================
13  */
14 
15 
16 #include "libm.h"
17 
18 #if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
19 #if LDBL_MANT_DIG == 64
20 /*
21  * ld80 version of __cos.c.  See __cos.c for most comments.
22  */
23 /*
24  * Domain [-0.7854, 0.7854], range ~[-2.43e-23, 2.425e-23]:
25  * |cos(x) - c(x)| < 2**-75.1
26  *
27  * The coefficients of c(x) were generated by a pari-gp script using
28  * a Remez algorithm that searches for the best higher coefficients
29  * after rounding leading coefficients to a specified precision.
30  *
31  * Simpler methods like Chebyshev or basic Remez barely suffice for
32  * cos() in 64-bit precision, because we want the coefficient of x^2
33  * to be precisely -0.5 so that multiplying by it is exact, and plain
34  * rounding of the coefficients of a good polynomial approximation only
35  * gives this up to about 64-bit precision.  Plain rounding also gives
36  * a mediocre approximation for the coefficient of x^4, but a rounding
37  * error of 0.5 ulps for this coefficient would only contribute ~0.01
38  * ulps to the final error, so this is unimportant.  Rounding errors in
39  * higher coefficients are even less important.
40  *
41  * In fact, coefficients above the x^4 one only need to have 53-bit
42  * precision, and this is more efficient.  We get this optimization
43  * almost for free from the complications needed to search for the best
44  * higher coefficients.
45  */
46 static const long double
47 C1 =  0.0416666666666666666136L;        /*  0xaaaaaaaaaaaaaa9b.0p-68 */
48 static const double
49 C2 = -0.0013888888888888874,            /* -0x16c16c16c16c10.0p-62 */
50 C3 =  0.000024801587301571716,          /*  0x1a01a01a018e22.0p-68 */
51 C4 = -0.00000027557319215507120,        /* -0x127e4fb7602f22.0p-74 */
52 C5 =  0.0000000020876754400407278,      /*  0x11eed8caaeccf1.0p-81 */
53 C6 = -1.1470297442401303e-11,           /* -0x19393412bd1529.0p-89 */
54 C7 =  4.7383039476436467e-14;           /*  0x1aac9d9af5c43e.0p-97 */
55 #define POLY(z) (z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*(C6+z*C7)))))))
56 #elif LDBL_MANT_DIG == 113
57 /*
58  * ld128 version of __cos.c.  See __cos.c for most comments.
59  */
60 /*
61  * Domain [-0.7854, 0.7854], range ~[-1.80e-37, 1.79e-37]:
62  * |cos(x) - c(x))| < 2**-122.0
63  *
64  * 113-bit precision requires more care than 64-bit precision, since
65  * simple methods give a minimax polynomial with coefficient for x^2
66  * that is 1 ulp below 0.5, but we want it to be precisely 0.5.  See
67  * above for more details.
68  */
69 static const long double
70 C1 =  0.04166666666666666666666666666666658424671L,
71 C2 = -0.001388888888888888888888888888863490893732L,
72 C3 =  0.00002480158730158730158730158600795304914210L,
73 C4 = -0.2755731922398589065255474947078934284324e-6L,
74 C5 =  0.2087675698786809897659225313136400793948e-8L,
75 C6 = -0.1147074559772972315817149986812031204775e-10L,
76 C7 =  0.4779477332386808976875457937252120293400e-13L;
77 static const double
78 C8 = -0.1561920696721507929516718307820958119868e-15,
79 C9 =  0.4110317413744594971475941557607804508039e-18,
80 C10 = -0.8896592467191938803288521958313920156409e-21,
81 C11 =  0.1601061435794535138244346256065192782581e-23;
82 #define POLY(z) (z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*(C6+z*(C7+ \
83 	z*(C8+z*(C9+z*(C10+z*C11)))))))))))
84 #endif
85 
__cosl(long double x,long double y)86 long double __cosl(long double x, long double y)
87 {
88 	long double hz,z,r,w;
89 
90 	z  = x*x;
91 	r  = POLY(z);
92 	hz = 0.5*z;
93 	w  = 1.0-hz;
94 	return w + (((1.0-w)-hz) + (z*r-x*y));
95 }
96 #endif
97