1*beb15867Skrw /* $OpenBSD: k_cosl.c,v 1.2 2017/01/21 08:29:13 krw Exp $ */
2390c8400Smartynas /* From: @(#)k_cos.c 1.3 95/01/18 */
3390c8400Smartynas /*
4390c8400Smartynas * ====================================================
5390c8400Smartynas * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6390c8400Smartynas * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
7390c8400Smartynas *
8390c8400Smartynas * Developed at SunSoft, a Sun Microsystems, Inc. business.
9390c8400Smartynas * Permission to use, copy, modify, and distribute this
10390c8400Smartynas * software is freely granted, provided that this notice
11390c8400Smartynas * is preserved.
12390c8400Smartynas * ====================================================
13390c8400Smartynas */
14390c8400Smartynas
15390c8400Smartynas /*
16390c8400Smartynas * ld80 version of k_cos.c. See ../k_cos.c for most comments.
17390c8400Smartynas */
18390c8400Smartynas
19390c8400Smartynas #include "math_private.h"
20390c8400Smartynas
21390c8400Smartynas /*
22390c8400Smartynas * Domain [-0.7854, 0.7854], range ~[-2.43e-23, 2.425e-23]:
23390c8400Smartynas * |cos(x) - c(x)| < 2**-75.1
24390c8400Smartynas *
25390c8400Smartynas * The coefficients of c(x) were generated by a pari-gp script using
26390c8400Smartynas * a Remez algorithm that searches for the best higher coefficients
27390c8400Smartynas * after rounding leading coefficients to a specified precision.
28390c8400Smartynas *
29390c8400Smartynas * Simpler methods like Chebyshev or basic Remez barely suffice for
30390c8400Smartynas * cos() in 64-bit precision, because we want the coefficient of x^2
31390c8400Smartynas * to be precisely -0.5 so that multiplying by it is exact, and plain
32390c8400Smartynas * rounding of the coefficients of a good polynomial approximation only
33390c8400Smartynas * gives this up to about 64-bit precision. Plain rounding also gives
34390c8400Smartynas * a mediocre approximation for the coefficient of x^4, but a rounding
35390c8400Smartynas * error of 0.5 ulps for this coefficient would only contribute ~0.01
36390c8400Smartynas * ulps to the final error, so this is unimportant. Rounding errors in
37390c8400Smartynas * higher coefficients are even less important.
38390c8400Smartynas *
39390c8400Smartynas * In fact, coefficients above the x^4 one only need to have 53-bit
40390c8400Smartynas * precision, and this is more efficient. We get this optimization
41390c8400Smartynas * almost for free from the complications needed to search for the best
42390c8400Smartynas * higher coefficients.
43390c8400Smartynas */
44390c8400Smartynas static const double
45390c8400Smartynas one = 1.0;
46390c8400Smartynas
47390c8400Smartynas #if defined(__amd64__) || defined(__i386__)
48390c8400Smartynas /* Long double constants are slow on these arches, and broken on i386. */
49390c8400Smartynas static const volatile double
50390c8400Smartynas C1hi = 0.041666666666666664, /* 0x15555555555555.0p-57 */
51390c8400Smartynas C1lo = 2.2598839032744733e-18; /* 0x14d80000000000.0p-111 */
52390c8400Smartynas #define C1 ((long double)C1hi + C1lo)
53390c8400Smartynas #else
54390c8400Smartynas static const long double
55390c8400Smartynas C1 = 0.0416666666666666666136L; /* 0xaaaaaaaaaaaaaa9b.0p-68 */
56390c8400Smartynas #endif
57390c8400Smartynas
58390c8400Smartynas static const double
59390c8400Smartynas C2 = -0.0013888888888888874, /* -0x16c16c16c16c10.0p-62 */
60390c8400Smartynas C3 = 0.000024801587301571716, /* 0x1a01a01a018e22.0p-68 */
61390c8400Smartynas C4 = -0.00000027557319215507120, /* -0x127e4fb7602f22.0p-74 */
62390c8400Smartynas C5 = 0.0000000020876754400407278, /* 0x11eed8caaeccf1.0p-81 */
63390c8400Smartynas C6 = -1.1470297442401303e-11, /* -0x19393412bd1529.0p-89 */
64390c8400Smartynas C7 = 4.7383039476436467e-14; /* 0x1aac9d9af5c43e.0p-97 */
65390c8400Smartynas
66390c8400Smartynas long double
__kernel_cosl(long double x,long double y)67390c8400Smartynas __kernel_cosl(long double x, long double y)
68390c8400Smartynas {
69390c8400Smartynas long double hz,z,r,w;
70390c8400Smartynas
71390c8400Smartynas z = x*x;
72390c8400Smartynas r = z*(C1+z*(C2+z*(C3+z*(C4+z*(C5+z*(C6+z*C7))))));
73390c8400Smartynas hz = 0.5*z;
74390c8400Smartynas w = one-hz;
75390c8400Smartynas return w + (((one-w)-hz) + (z*r-x*y));
76390c8400Smartynas }
77