xref: /freebsd/lib/msun/ld80/k_cosl.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
1de336b0cSDavid Schultz /*
2de336b0cSDavid Schultz  * ====================================================
3de336b0cSDavid Schultz  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4de336b0cSDavid Schultz  * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
5de336b0cSDavid Schultz  *
6de336b0cSDavid Schultz  * Developed at SunSoft, a Sun Microsystems, Inc. business.
7de336b0cSDavid Schultz  * Permission to use, copy, modify, and distribute this
8de336b0cSDavid Schultz  * software is freely granted, provided that this notice
9de336b0cSDavid Schultz  * is preserved.
10de336b0cSDavid Schultz  * ====================================================
11de336b0cSDavid Schultz  */
12de336b0cSDavid Schultz 
13de336b0cSDavid Schultz /*
14de336b0cSDavid Schultz  * ld80 version of k_cos.c.  See ../src/k_cos.c for most comments.
15de336b0cSDavid Schultz  */
16de336b0cSDavid Schultz 
17de336b0cSDavid Schultz #include "math_private.h"
18de336b0cSDavid Schultz 
19de336b0cSDavid Schultz /*
20de336b0cSDavid Schultz  * Domain [-0.7854, 0.7854], range ~[-2.43e-23, 2.425e-23]:
21de336b0cSDavid Schultz  * |cos(x) - c(x)| < 2**-75.1
22de336b0cSDavid Schultz  *
23de336b0cSDavid Schultz  * The coefficients of c(x) were generated by a pari-gp script using
24de336b0cSDavid Schultz  * a Remez algorithm that searches for the best higher coefficients
25de336b0cSDavid Schultz  * after rounding leading coefficients to a specified precision.
26de336b0cSDavid Schultz  *
27de336b0cSDavid Schultz  * Simpler methods like Chebyshev or basic Remez barely suffice for
28de336b0cSDavid Schultz  * cos() in 64-bit precision, because we want the coefficient of x^2
29de336b0cSDavid Schultz  * to be precisely -0.5 so that multiplying by it is exact, and plain
30de336b0cSDavid Schultz  * rounding of the coefficients of a good polynomial approximation only
31de336b0cSDavid Schultz  * gives this up to about 64-bit precision.  Plain rounding also gives
32de336b0cSDavid Schultz  * a mediocre approximation for the coefficient of x^4, but a rounding
33de336b0cSDavid Schultz  * error of 0.5 ulps for this coefficient would only contribute ~0.01
34de336b0cSDavid Schultz  * ulps to the final error, so this is unimportant.  Rounding errors in
35de336b0cSDavid Schultz  * higher coefficients are even less important.
36de336b0cSDavid Schultz  *
37de336b0cSDavid Schultz  * In fact, coefficients above the x^4 one only need to have 53-bit
38de336b0cSDavid Schultz  * precision, and this is more efficient.  We get this optimization
39de336b0cSDavid Schultz  * almost for free from the complications needed to search for the best
40de336b0cSDavid Schultz  * higher coefficients.
41de336b0cSDavid Schultz  */
42de336b0cSDavid Schultz static const double
43de336b0cSDavid Schultz one = 1.0;
44de336b0cSDavid Schultz 
45de336b0cSDavid Schultz #if defined(__amd64__) || defined(__i386__)
46de336b0cSDavid Schultz /* Long double constants are slow on these arches, and broken on i386. */
47de336b0cSDavid Schultz static const volatile double
48de336b0cSDavid Schultz C1hi = 0.041666666666666664,		/*  0x15555555555555.0p-57 */
49de336b0cSDavid Schultz C1lo = 2.2598839032744733e-18;		/*  0x14d80000000000.0p-111 */
50de336b0cSDavid Schultz #define	C1	((long double)C1hi + C1lo)
51de336b0cSDavid Schultz #else
52de336b0cSDavid Schultz static const long double
53de336b0cSDavid Schultz C1 =  0.0416666666666666666136L;	/*  0xaaaaaaaaaaaaaa9b.0p-68 */
54de336b0cSDavid Schultz #endif
55de336b0cSDavid Schultz 
56de336b0cSDavid Schultz static const double
57de336b0cSDavid Schultz C2 = -0.0013888888888888874,		/* -0x16c16c16c16c10.0p-62 */
58de336b0cSDavid Schultz C3 =  0.000024801587301571716,		/*  0x1a01a01a018e22.0p-68 */
59de336b0cSDavid Schultz C4 = -0.00000027557319215507120,	/* -0x127e4fb7602f22.0p-74 */
60de336b0cSDavid Schultz C5 =  0.0000000020876754400407278,	/*  0x11eed8caaeccf1.0p-81 */
61de336b0cSDavid Schultz C6 = -1.1470297442401303e-11,		/* -0x19393412bd1529.0p-89 */
62de336b0cSDavid Schultz C7 =  4.7383039476436467e-14;		/*  0x1aac9d9af5c43e.0p-97 */
63de336b0cSDavid Schultz 
64de336b0cSDavid Schultz long double
__kernel_cosl(long double x,long double y)65de336b0cSDavid Schultz __kernel_cosl(long double x, long double y)
66de336b0cSDavid Schultz {
67de336b0cSDavid Schultz 	long double hz,z,r,w;
68de336b0cSDavid Schultz 
69de336b0cSDavid Schultz 	z  = x*x;
70de336b0cSDavid Schultz 	r  = z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*(C6+z*C7))))));
71de336b0cSDavid Schultz 	hz = 0.5*z;
72de336b0cSDavid Schultz 	w  = one-hz;
73de336b0cSDavid Schultz 	return w + (((one-w)-hz) + (z*r-x*y));
74de336b0cSDavid Schultz }
75