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