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