161f95582SDavid Schultz /*
261f95582SDavid Schultz * ====================================================
361f95582SDavid Schultz * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
461f95582SDavid Schultz * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
561f95582SDavid Schultz *
661f95582SDavid Schultz * Permission to use, copy, modify, and distribute this
761f95582SDavid Schultz * software is freely granted, provided that this notice
861f95582SDavid Schultz * is preserved.
961f95582SDavid Schultz * ====================================================
1061f95582SDavid Schultz */
1161f95582SDavid Schultz
1261f95582SDavid Schultz /*
1361f95582SDavid Schultz * ld128 version of k_tan.c. See ../src/k_tan.c for most comments.
1461f95582SDavid Schultz */
1561f95582SDavid Schultz
1661f95582SDavid Schultz #include "math.h"
1761f95582SDavid Schultz #include "math_private.h"
1861f95582SDavid Schultz
1961f95582SDavid Schultz /*
2061f95582SDavid Schultz * Domain [-0.67434, 0.67434], range ~[-3.37e-36, 1.982e-37]
2161f95582SDavid Schultz * |tan(x)/x - t(x)| < 2**-117.8 (XXX should be ~1e-37)
2261f95582SDavid Schultz *
2361f95582SDavid Schultz * See ../ld80/k_cosl.c for more details about the polynomial.
2461f95582SDavid Schultz */
2561f95582SDavid Schultz static const long double
2661f95582SDavid Schultz T3 = 0x1.5555555555555555555555555553p-2L,
2761f95582SDavid Schultz T5 = 0x1.1111111111111111111111111eb5p-3L,
2861f95582SDavid Schultz T7 = 0x1.ba1ba1ba1ba1ba1ba1ba1b694cd6p-5L,
2961f95582SDavid Schultz T9 = 0x1.664f4882c10f9f32d6bbe09d8bcdp-6L,
3061f95582SDavid Schultz T11 = 0x1.226e355e6c23c8f5b4f5762322eep-7L,
3161f95582SDavid Schultz T13 = 0x1.d6d3d0e157ddfb5fed8e84e27b37p-9L,
3261f95582SDavid Schultz T15 = 0x1.7da36452b75e2b5fce9ee7c2c92ep-10L,
3361f95582SDavid Schultz T17 = 0x1.355824803674477dfcf726649efep-11L,
3461f95582SDavid Schultz T19 = 0x1.f57d7734d1656e0aceb716f614c2p-13L,
3561f95582SDavid Schultz T21 = 0x1.967e18afcb180ed942dfdc518d6cp-14L,
3661f95582SDavid Schultz T23 = 0x1.497d8eea21e95bc7e2aa79b9f2cdp-15L,
3761f95582SDavid Schultz T25 = 0x1.0b132d39f055c81be49eff7afd50p-16L,
3861f95582SDavid Schultz T27 = 0x1.b0f72d33eff7bfa2fbc1059d90b6p-18L,
3961f95582SDavid Schultz T29 = 0x1.5ef2daf21d1113df38d0fbc00267p-19L,
4061f95582SDavid Schultz T31 = 0x1.1c77d6eac0234988cdaa04c96626p-20L,
4161f95582SDavid Schultz T33 = 0x1.cd2a5a292b180e0bdd701057dfe3p-22L,
4261f95582SDavid Schultz T35 = 0x1.75c7357d0298c01a31d0a6f7d518p-23L,
4361f95582SDavid Schultz T37 = 0x1.2f3190f4718a9a520f98f50081fcp-24L,
4461f95582SDavid Schultz pio4 = 0x1.921fb54442d18469898cc51701b8p-1L,
4561f95582SDavid Schultz pio4lo = 0x1.cd129024e088a67cc74020bbea60p-116L;
4661f95582SDavid Schultz
4761f95582SDavid Schultz static const double
4861f95582SDavid Schultz T39 = 0.000000028443389121318352, /* 0x1e8a7592977938.0p-78 */
4961f95582SDavid Schultz T41 = 0.000000011981013102001973, /* 0x19baa1b1223219.0p-79 */
5061f95582SDavid Schultz T43 = 0.0000000038303578044958070, /* 0x107385dfb24529.0p-80 */
5161f95582SDavid Schultz T45 = 0.0000000034664378216909893, /* 0x1dc6c702a05262.0p-81 */
5261f95582SDavid Schultz T47 = -0.0000000015090641701997785, /* -0x19ecef3569ebb6.0p-82 */
5361f95582SDavid Schultz T49 = 0.0000000029449552300483952, /* 0x194c0668da786a.0p-81 */
5461f95582SDavid Schultz T51 = -0.0000000022006995706097711, /* -0x12e763b8845268.0p-81 */
5561f95582SDavid Schultz T53 = 0.0000000015468200913196612, /* 0x1a92fc98c29554.0p-82 */
5661f95582SDavid Schultz T55 = -0.00000000061311613386849674, /* -0x151106cbc779a9.0p-83 */
5761f95582SDavid Schultz T57 = 1.4912469681508012e-10; /* 0x147edbdba6f43a.0p-85 */
5861f95582SDavid Schultz
5961f95582SDavid Schultz long double
__kernel_tanl(long double x,long double y,int iy)6061f95582SDavid Schultz __kernel_tanl(long double x, long double y, int iy) {
6161f95582SDavid Schultz long double z, r, v, w, s;
6261f95582SDavid Schultz long double osign;
6361f95582SDavid Schultz int i;
6461f95582SDavid Schultz
6561f95582SDavid Schultz iy = (iy == 1 ? -1 : 1); /* XXX recover original interface */
6661f95582SDavid Schultz osign = (x >= 0 ? 1.0 : -1.0); /* XXX slow, probably wrong for -0 */
6761f95582SDavid Schultz if (fabsl(x) >= 0.67434) {
6861f95582SDavid Schultz if (x < 0) {
6961f95582SDavid Schultz x = -x;
7061f95582SDavid Schultz y = -y;
7161f95582SDavid Schultz }
7261f95582SDavid Schultz z = pio4 - x;
7361f95582SDavid Schultz w = pio4lo - y;
7461f95582SDavid Schultz x = z + w;
7561f95582SDavid Schultz y = 0.0;
7661f95582SDavid Schultz i = 1;
7761f95582SDavid Schultz } else
7861f95582SDavid Schultz i = 0;
7961f95582SDavid Schultz z = x * x;
8061f95582SDavid Schultz w = z * z;
8161f95582SDavid Schultz r = T5 + w * (T9 + w * (T13 + w * (T17 + w * (T21 +
8261f95582SDavid Schultz w * (T25 + w * (T29 + w * (T33 +
8361f95582SDavid Schultz w * (T37 + w * (T41 + w * (T45 + w * (T49 + w * (T53 +
8461f95582SDavid Schultz w * T57))))))))))));
8561f95582SDavid Schultz v = z * (T7 + w * (T11 + w * (T15 + w * (T19 + w * (T23 +
8661f95582SDavid Schultz w * (T27 + w * (T31 + w * (T35 +
8761f95582SDavid Schultz w * (T39 + w * (T43 + w * (T47 + w * (T51 + w * T55))))))))))));
8861f95582SDavid Schultz s = z * x;
8961f95582SDavid Schultz r = y + z * (s * (r + v) + y);
9061f95582SDavid Schultz r += T3 * s;
9161f95582SDavid Schultz w = x + r;
9261f95582SDavid Schultz if (i == 1) {
9361f95582SDavid Schultz v = (long double) iy;
9461f95582SDavid Schultz return osign *
9561f95582SDavid Schultz (v - 2.0 * (x - (w * w / (w + v) - r)));
9661f95582SDavid Schultz }
9761f95582SDavid Schultz if (iy == 1)
9861f95582SDavid Schultz return w;
9961f95582SDavid Schultz else {
10061f95582SDavid Schultz /*
10161f95582SDavid Schultz * if allow error up to 2 ulp, simply return
10261f95582SDavid Schultz * -1.0 / (x+r) here
10361f95582SDavid Schultz */
10461f95582SDavid Schultz /* compute -1.0 / (x+r) accurately */
10561f95582SDavid Schultz long double a, t;
10661f95582SDavid Schultz z = w;
10761f95582SDavid Schultz z = z + 0x1p32 - 0x1p32;
10861f95582SDavid Schultz v = r - (z - x); /* z+v = r+x */
10961f95582SDavid Schultz t = a = -1.0 / w; /* a = -1.0/w */
11061f95582SDavid Schultz t = t + 0x1p32 - 0x1p32;
11161f95582SDavid Schultz s = 1.0 + t * z;
11261f95582SDavid Schultz return t + a * (s + t * v);
11361f95582SDavid Schultz }
11461f95582SDavid Schultz }
115