blob: 54abc3daf1ad126306b363151aaa8dcca5928423 [file] [log] [blame]
Rich Felkerb69f6952012-03-13 01:17:53 -04001/* origin: FreeBSD /usr/src/lib/msun/ld80/k_tanl.c */
Szabolcs Nagyea9bb952013-09-03 18:50:58 +00002/* origin: FreeBSD /usr/src/lib/msun/ld128/k_tanl.c */
Rich Felkerb69f6952012-03-13 01:17:53 -04003/*
4 * ====================================================
5 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
6 * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
7 *
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
10 * is preserved.
11 * ====================================================
12 */
13
14#include "libm.h"
15
Szabolcs Nagyea9bb952013-09-03 18:50:58 +000016#if (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
17#if LDBL_MANT_DIG == 64
Rich Felkerb69f6952012-03-13 01:17:53 -040018/*
19 * ld80 version of __tan.c. See __tan.c for most comments.
20 */
21/*
22 * Domain [-0.67434, 0.67434], range ~[-2.25e-22, 1.921e-22]
23 * |tan(x)/x - t(x)| < 2**-71.9
24 *
25 * See __cosl.c for more details about the polynomial.
26 */
Rich Felkerb69f6952012-03-13 01:17:53 -040027static const long double
28T3 = 0.333333333333333333180L, /* 0xaaaaaaaaaaaaaaa5.0p-65 */
29T5 = 0.133333333333333372290L, /* 0x88888888888893c3.0p-66 */
30T7 = 0.0539682539682504975744L, /* 0xdd0dd0dd0dc13ba2.0p-68 */
31pio4 = 0.785398163397448309628L, /* 0xc90fdaa22168c235.0p-64 */
32pio4lo = -1.25413940316708300586e-20L; /* -0xece675d1fc8f8cbb.0p-130 */
Rich Felkerb69f6952012-03-13 01:17:53 -040033static const double
34T9 = 0.021869488536312216, /* 0x1664f4882cc1c2.0p-58 */
35T11 = 0.0088632355256619590, /* 0x1226e355c17612.0p-59 */
36T13 = 0.0035921281113786528, /* 0x1d6d3d185d7ff8.0p-61 */
37T15 = 0.0014558334756312418, /* 0x17da354aa3f96b.0p-62 */
38T17 = 0.00059003538700862256, /* 0x13559358685b83.0p-63 */
39T19 = 0.00023907843576635544, /* 0x1f56242026b5be.0p-65 */
40T21 = 0.000097154625656538905, /* 0x1977efc26806f4.0p-66 */
41T23 = 0.000038440165747303162, /* 0x14275a09b3ceac.0p-67 */
42T25 = 0.000018082171885432524, /* 0x12f5e563e5487e.0p-68 */
43T27 = 0.0000024196006108814377, /* 0x144c0d80cc6896.0p-71 */
44T29 = 0.0000078293456938132840, /* 0x106b59141a6cb3.0p-69 */
45T31 = -0.0000032609076735050182, /* -0x1b5abef3ba4b59.0p-71 */
46T33 = 0.0000023261313142559411; /* 0x13835436c0c87f.0p-71 */
Szabolcs Nagyea9bb952013-09-03 18:50:58 +000047#define RPOLY(w) (T5 + w * (T9 + w * (T13 + w * (T17 + w * (T21 + \
48 w * (T25 + w * (T29 + w * T33)))))))
49#define VPOLY(w) (T7 + w * (T11 + w * (T15 + w * (T19 + w * (T23 + \
50 w * (T27 + w * T31))))))
51#elif LDBL_MANT_DIG == 113
52/*
53 * ld128 version of __tan.c. See __tan.c for most comments.
54 */
55/*
56 * Domain [-0.67434, 0.67434], range ~[-3.37e-36, 1.982e-37]
57 * |tan(x)/x - t(x)| < 2**-117.8 (XXX should be ~1e-37)
58 *
59 * See __cosl.c for more details about the polynomial.
60 */
61static const long double
62T3 = 0x1.5555555555555555555555555553p-2L,
63T5 = 0x1.1111111111111111111111111eb5p-3L,
64T7 = 0x1.ba1ba1ba1ba1ba1ba1ba1b694cd6p-5L,
65T9 = 0x1.664f4882c10f9f32d6bbe09d8bcdp-6L,
66T11 = 0x1.226e355e6c23c8f5b4f5762322eep-7L,
67T13 = 0x1.d6d3d0e157ddfb5fed8e84e27b37p-9L,
68T15 = 0x1.7da36452b75e2b5fce9ee7c2c92ep-10L,
69T17 = 0x1.355824803674477dfcf726649efep-11L,
70T19 = 0x1.f57d7734d1656e0aceb716f614c2p-13L,
71T21 = 0x1.967e18afcb180ed942dfdc518d6cp-14L,
72T23 = 0x1.497d8eea21e95bc7e2aa79b9f2cdp-15L,
73T25 = 0x1.0b132d39f055c81be49eff7afd50p-16L,
74T27 = 0x1.b0f72d33eff7bfa2fbc1059d90b6p-18L,
75T29 = 0x1.5ef2daf21d1113df38d0fbc00267p-19L,
76T31 = 0x1.1c77d6eac0234988cdaa04c96626p-20L,
77T33 = 0x1.cd2a5a292b180e0bdd701057dfe3p-22L,
78T35 = 0x1.75c7357d0298c01a31d0a6f7d518p-23L,
79T37 = 0x1.2f3190f4718a9a520f98f50081fcp-24L,
80pio4 = 0x1.921fb54442d18469898cc51701b8p-1L,
81pio4lo = 0x1.cd129024e088a67cc74020bbea60p-116L;
82static const double
83T39 = 0.000000028443389121318352, /* 0x1e8a7592977938.0p-78 */
84T41 = 0.000000011981013102001973, /* 0x19baa1b1223219.0p-79 */
85T43 = 0.0000000038303578044958070, /* 0x107385dfb24529.0p-80 */
86T45 = 0.0000000034664378216909893, /* 0x1dc6c702a05262.0p-81 */
87T47 = -0.0000000015090641701997785, /* -0x19ecef3569ebb6.0p-82 */
88T49 = 0.0000000029449552300483952, /* 0x194c0668da786a.0p-81 */
89T51 = -0.0000000022006995706097711, /* -0x12e763b8845268.0p-81 */
90T53 = 0.0000000015468200913196612, /* 0x1a92fc98c29554.0p-82 */
91T55 = -0.00000000061311613386849674, /* -0x151106cbc779a9.0p-83 */
92T57 = 1.4912469681508012e-10; /* 0x147edbdba6f43a.0p-85 */
93#define RPOLY(w) (T5 + w * (T9 + w * (T13 + w * (T17 + w * (T21 + \
94 w * (T25 + w * (T29 + w * (T33 + w * (T37 + w * (T41 + \
95 w * (T45 + w * (T49 + w * (T53 + w * T57)))))))))))))
96#define VPOLY(w) (T7 + w * (T11 + w * (T15 + w * (T19 + w * (T23 + \
97 w * (T27 + w * (T31 + w * (T35 + w * (T39 + w * (T43 + \
98 w * (T47 + w * (T51 + w * T55))))))))))))
99#endif
Rich Felkerb69f6952012-03-13 01:17:53 -0400100
Szabolcs Nagy1d5ba3b2013-05-18 12:34:00 +0000101long double __tanl(long double x, long double y, int odd) {
Rich Felkerb69f6952012-03-13 01:17:53 -0400102 long double z, r, v, w, s, a, t;
Szabolcs Nagy1d5ba3b2013-05-18 12:34:00 +0000103 int big, sign;
Rich Felkerb69f6952012-03-13 01:17:53 -0400104
Szabolcs Nagy1d5ba3b2013-05-18 12:34:00 +0000105 big = fabsl(x) >= 0.67434;
106 if (big) {
107 sign = 0;
Rich Felkerb69f6952012-03-13 01:17:53 -0400108 if (x < 0) {
Szabolcs Nagy1d5ba3b2013-05-18 12:34:00 +0000109 sign = 1;
Rich Felkerb69f6952012-03-13 01:17:53 -0400110 x = -x;
111 y = -y;
112 }
Szabolcs Nagy1d5ba3b2013-05-18 12:34:00 +0000113 x = (pio4 - x) + (pio4lo - y);
Rich Felkerb69f6952012-03-13 01:17:53 -0400114 y = 0.0;
Szabolcs Nagy1d5ba3b2013-05-18 12:34:00 +0000115 }
Rich Felkerb69f6952012-03-13 01:17:53 -0400116 z = x * x;
117 w = z * z;
Szabolcs Nagyea9bb952013-09-03 18:50:58 +0000118 r = RPOLY(w);
119 v = z * VPOLY(w);
Rich Felkerb69f6952012-03-13 01:17:53 -0400120 s = z * x;
Szabolcs Nagy1d5ba3b2013-05-18 12:34:00 +0000121 r = y + z * (s * (r + v) + y) + T3 * s;
Rich Felkerb69f6952012-03-13 01:17:53 -0400122 w = x + r;
Szabolcs Nagy1d5ba3b2013-05-18 12:34:00 +0000123 if (big) {
124 s = 1 - 2*odd;
125 v = s - 2.0 * (x + (r - w * w / (w + s)));
126 return sign ? -v : v;
Rich Felkerb69f6952012-03-13 01:17:53 -0400127 }
Szabolcs Nagy1d5ba3b2013-05-18 12:34:00 +0000128 if (!odd)
Rich Felkerb69f6952012-03-13 01:17:53 -0400129 return w;
Rich Felkerb69f6952012-03-13 01:17:53 -0400130 /*
131 * if allow error up to 2 ulp, simply return
132 * -1.0 / (x+r) here
133 */
134 /* compute -1.0 / (x+r) accurately */
135 z = w;
136 z = z + 0x1p32 - 0x1p32;
137 v = r - (z - x); /* z+v = r+x */
138 t = a = -1.0 / w; /* a = -1.0/w */
139 t = t + 0x1p32 - 0x1p32;
140 s = 1.0 + t * z;
141 return t + a * (s + t * v);
142}
143#endif