1*cfe182f3Schristos /* From: @(#)k_tan.c 1.5 04/04/22 SMI */
2*cfe182f3Schristos
3*cfe182f3Schristos /*
4*cfe182f3Schristos * ====================================================
5*cfe182f3Schristos * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
6*cfe182f3Schristos * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
7*cfe182f3Schristos *
8*cfe182f3Schristos * Permission to use, copy, modify, and distribute this
9*cfe182f3Schristos * software is freely granted, provided that this notice
10*cfe182f3Schristos * is preserved.
11*cfe182f3Schristos * ====================================================
12*cfe182f3Schristos */
13*cfe182f3Schristos
14*cfe182f3Schristos #include <sys/cdefs.h>
15*cfe182f3Schristos /*
16*cfe182f3Schristos * ld80 version of k_tan.c. See ../src/k_tan.c for most comments.
17*cfe182f3Schristos */
18*cfe182f3Schristos
19*cfe182f3Schristos #include "math.h"
20*cfe182f3Schristos #include "math_private.h"
21*cfe182f3Schristos
22*cfe182f3Schristos /*
23*cfe182f3Schristos * Domain [-0.67434, 0.67434], range ~[-2.25e-22, 1.921e-22]
24*cfe182f3Schristos * |tan(x)/x - t(x)| < 2**-71.9
25*cfe182f3Schristos *
26*cfe182f3Schristos * See k_cosl.c for more details about the polynomial.
27*cfe182f3Schristos */
28*cfe182f3Schristos #if defined(__amd64__) || defined(__i386__)
29*cfe182f3Schristos /* Long double constants are slow on these arches, and broken on i386. */
30*cfe182f3Schristos static const volatile double
31*cfe182f3Schristos T3hi = 0.33333333333333331, /* 0x15555555555555.0p-54 */
32*cfe182f3Schristos T3lo = 1.8350121769317163e-17, /* 0x15280000000000.0p-108 */
33*cfe182f3Schristos T5hi = 0.13333333333333336, /* 0x11111111111112.0p-55 */
34*cfe182f3Schristos T5lo = 1.3051083651294260e-17, /* 0x1e180000000000.0p-109 */
35*cfe182f3Schristos T7hi = 0.053968253968250494, /* 0x1ba1ba1ba1b827.0p-57 */
36*cfe182f3Schristos T7lo = 3.1509625637859973e-18, /* 0x1d100000000000.0p-111 */
37*cfe182f3Schristos pio4_hi = 0.78539816339744828, /* 0x1921fb54442d18.0p-53 */
38*cfe182f3Schristos pio4_lo = 3.0628711372715500e-17, /* 0x11a80000000000.0p-107 */
39*cfe182f3Schristos pio4lo_hi = -1.2541394031670831e-20, /* -0x1d9cceba3f91f2.0p-119 */
40*cfe182f3Schristos pio4lo_lo = 6.1493048227390915e-37; /* 0x1a280000000000.0p-173 */
41*cfe182f3Schristos #define T3 ((long double)T3hi + T3lo)
42*cfe182f3Schristos #define T5 ((long double)T5hi + T5lo)
43*cfe182f3Schristos #define T7 ((long double)T7hi + T7lo)
44*cfe182f3Schristos #define pio4 ((long double)pio4_hi + pio4_lo)
45*cfe182f3Schristos #define pio4lo ((long double)pio4lo_hi + pio4lo_lo)
46*cfe182f3Schristos #else
47*cfe182f3Schristos static const long double
48*cfe182f3Schristos T3 = 0.333333333333333333180L, /* 0xaaaaaaaaaaaaaaa5.0p-65 */
49*cfe182f3Schristos T5 = 0.133333333333333372290L, /* 0x88888888888893c3.0p-66 */
50*cfe182f3Schristos T7 = 0.0539682539682504975744L, /* 0xdd0dd0dd0dc13ba2.0p-68 */
51*cfe182f3Schristos pio4 = 0.785398163397448309628L, /* 0xc90fdaa22168c235.0p-64 */
52*cfe182f3Schristos pio4lo = -1.25413940316708300586e-20L; /* -0xece675d1fc8f8cbb.0p-130 */
53*cfe182f3Schristos #endif
54*cfe182f3Schristos
55*cfe182f3Schristos static const double
56*cfe182f3Schristos T9 = 0.021869488536312216, /* 0x1664f4882cc1c2.0p-58 */
57*cfe182f3Schristos T11 = 0.0088632355256619590, /* 0x1226e355c17612.0p-59 */
58*cfe182f3Schristos T13 = 0.0035921281113786528, /* 0x1d6d3d185d7ff8.0p-61 */
59*cfe182f3Schristos T15 = 0.0014558334756312418, /* 0x17da354aa3f96b.0p-62 */
60*cfe182f3Schristos T17 = 0.00059003538700862256, /* 0x13559358685b83.0p-63 */
61*cfe182f3Schristos T19 = 0.00023907843576635544, /* 0x1f56242026b5be.0p-65 */
62*cfe182f3Schristos T21 = 0.000097154625656538905, /* 0x1977efc26806f4.0p-66 */
63*cfe182f3Schristos T23 = 0.000038440165747303162, /* 0x14275a09b3ceac.0p-67 */
64*cfe182f3Schristos T25 = 0.000018082171885432524, /* 0x12f5e563e5487e.0p-68 */
65*cfe182f3Schristos T27 = 0.0000024196006108814377, /* 0x144c0d80cc6896.0p-71 */
66*cfe182f3Schristos T29 = 0.0000078293456938132840, /* 0x106b59141a6cb3.0p-69 */
67*cfe182f3Schristos T31 = -0.0000032609076735050182, /* -0x1b5abef3ba4b59.0p-71 */
68*cfe182f3Schristos T33 = 0.0000023261313142559411; /* 0x13835436c0c87f.0p-71 */
69*cfe182f3Schristos
70*cfe182f3Schristos long double
__kernel_tanl(long double x,long double y,int iy)71*cfe182f3Schristos __kernel_tanl(long double x, long double y, int iy) {
72*cfe182f3Schristos long double z, r, v, w, s;
73*cfe182f3Schristos long double osign;
74*cfe182f3Schristos int i;
75*cfe182f3Schristos
76*cfe182f3Schristos iy = (iy == 1 ? -1 : 1); /* XXX recover original interface */
77*cfe182f3Schristos osign = (x >= 0 ? 1.0 : -1.0); /* XXX slow, probably wrong for -0 */
78*cfe182f3Schristos if (fabsl(x) >= 0.67434) {
79*cfe182f3Schristos if (x < 0) {
80*cfe182f3Schristos x = -x;
81*cfe182f3Schristos y = -y;
82*cfe182f3Schristos }
83*cfe182f3Schristos z = pio4 - x;
84*cfe182f3Schristos w = pio4lo - y;
85*cfe182f3Schristos x = z + w;
86*cfe182f3Schristos y = 0.0;
87*cfe182f3Schristos i = 1;
88*cfe182f3Schristos } else
89*cfe182f3Schristos i = 0;
90*cfe182f3Schristos z = x * x;
91*cfe182f3Schristos w = z * z;
92*cfe182f3Schristos r = T5 + w * (T9 + w * (T13 + w * (T17 + w * (T21 +
93*cfe182f3Schristos w * (T25 + w * (T29 + w * T33))))));
94*cfe182f3Schristos v = z * (T7 + w * (T11 + w * (T15 + w * (T19 + w * (T23 +
95*cfe182f3Schristos w * (T27 + w * T31))))));
96*cfe182f3Schristos s = z * x;
97*cfe182f3Schristos r = y + z * (s * (r + v) + y);
98*cfe182f3Schristos r += T3 * s;
99*cfe182f3Schristos w = x + r;
100*cfe182f3Schristos if (i == 1) {
101*cfe182f3Schristos v = (long double) iy;
102*cfe182f3Schristos return osign *
103*cfe182f3Schristos (v - 2.0 * (x - (w * w / (w + v) - r)));
104*cfe182f3Schristos }
105*cfe182f3Schristos if (iy == 1)
106*cfe182f3Schristos return w;
107*cfe182f3Schristos else {
108*cfe182f3Schristos /*
109*cfe182f3Schristos * if allow error up to 2 ulp, simply return
110*cfe182f3Schristos * -1.0 / (x+r) here
111*cfe182f3Schristos */
112*cfe182f3Schristos /* compute -1.0 / (x+r) accurately */
113*cfe182f3Schristos long double a, t;
114*cfe182f3Schristos z = w;
115*cfe182f3Schristos z = z + 0x1p32 - 0x1p32;
116*cfe182f3Schristos v = r - (z - x); /* z+v = r+x */
117*cfe182f3Schristos t = a = -1.0 / w; /* a = -1.0/w */
118*cfe182f3Schristos t = t + 0x1p32 - 0x1p32;
119*cfe182f3Schristos s = 1.0 + t * z;
120*cfe182f3Schristos return t + a * (s + t * v);
121*cfe182f3Schristos }
122*cfe182f3Schristos }
123