1176358Sdas/* From: @(#)k_tan.c 1.5 04/04/22 SMI */
2176358Sdas
3176358Sdas/*
4176358Sdas * ====================================================
5176358Sdas * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
6176358Sdas * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
7176358Sdas *
8176358Sdas * Permission to use, copy, modify, and distribute this
9176358Sdas * software is freely granted, provided that this notice
10176358Sdas * is preserved.
11176358Sdas * ====================================================
12176358Sdas */
13176358Sdas
14176358Sdas#include <sys/cdefs.h>
15176358Sdas__FBSDID("$FreeBSD: releng/11.0/lib/msun/ld128/k_tanl.c 176358 2008-02-17 07:32:31Z das $");
16176358Sdas
17176358Sdas/*
18176358Sdas * ld128 version of k_tan.c.  See ../src/k_tan.c for most comments.
19176358Sdas */
20176358Sdas
21176358Sdas#include "math.h"
22176358Sdas#include "math_private.h"
23176358Sdas
24176358Sdas/*
25176358Sdas * Domain [-0.67434, 0.67434], range ~[-3.37e-36, 1.982e-37]
26176358Sdas * |tan(x)/x - t(x)| < 2**-117.8 (XXX should be ~1e-37)
27176358Sdas *
28176358Sdas * See ../ld80/k_cosl.c for more details about the polynomial.
29176358Sdas */
30176358Sdasstatic const long double
31176358SdasT3 = 0x1.5555555555555555555555555553p-2L,
32176358SdasT5 = 0x1.1111111111111111111111111eb5p-3L,
33176358SdasT7 = 0x1.ba1ba1ba1ba1ba1ba1ba1b694cd6p-5L,
34176358SdasT9 = 0x1.664f4882c10f9f32d6bbe09d8bcdp-6L,
35176358SdasT11 = 0x1.226e355e6c23c8f5b4f5762322eep-7L,
36176358SdasT13 = 0x1.d6d3d0e157ddfb5fed8e84e27b37p-9L,
37176358SdasT15 = 0x1.7da36452b75e2b5fce9ee7c2c92ep-10L,
38176358SdasT17 = 0x1.355824803674477dfcf726649efep-11L,
39176358SdasT19 = 0x1.f57d7734d1656e0aceb716f614c2p-13L,
40176358SdasT21 = 0x1.967e18afcb180ed942dfdc518d6cp-14L,
41176358SdasT23 = 0x1.497d8eea21e95bc7e2aa79b9f2cdp-15L,
42176358SdasT25 = 0x1.0b132d39f055c81be49eff7afd50p-16L,
43176358SdasT27 = 0x1.b0f72d33eff7bfa2fbc1059d90b6p-18L,
44176358SdasT29 = 0x1.5ef2daf21d1113df38d0fbc00267p-19L,
45176358SdasT31 = 0x1.1c77d6eac0234988cdaa04c96626p-20L,
46176358SdasT33 = 0x1.cd2a5a292b180e0bdd701057dfe3p-22L,
47176358SdasT35 = 0x1.75c7357d0298c01a31d0a6f7d518p-23L,
48176358SdasT37 = 0x1.2f3190f4718a9a520f98f50081fcp-24L,
49176358Sdaspio4 = 0x1.921fb54442d18469898cc51701b8p-1L,
50176358Sdaspio4lo = 0x1.cd129024e088a67cc74020bbea60p-116L;
51176358Sdas
52176358Sdasstatic const double
53176358SdasT39 =  0.000000028443389121318352,	/*  0x1e8a7592977938.0p-78 */
54176358SdasT41 =  0.000000011981013102001973,	/*  0x19baa1b1223219.0p-79 */
55176358SdasT43 =  0.0000000038303578044958070,	/*  0x107385dfb24529.0p-80 */
56176358SdasT45 =  0.0000000034664378216909893,	/*  0x1dc6c702a05262.0p-81 */
57176358SdasT47 = -0.0000000015090641701997785,	/* -0x19ecef3569ebb6.0p-82 */
58176358SdasT49 =  0.0000000029449552300483952,	/*  0x194c0668da786a.0p-81 */
59176358SdasT51 = -0.0000000022006995706097711,	/* -0x12e763b8845268.0p-81 */
60176358SdasT53 =  0.0000000015468200913196612,	/*  0x1a92fc98c29554.0p-82 */
61176358SdasT55 = -0.00000000061311613386849674,	/* -0x151106cbc779a9.0p-83 */
62176358SdasT57 =  1.4912469681508012e-10;		/*  0x147edbdba6f43a.0p-85 */
63176358Sdas
64176358Sdaslong double
65176358Sdas__kernel_tanl(long double x, long double y, int iy) {
66176358Sdas	long double z, r, v, w, s;
67176358Sdas	long double osign;
68176358Sdas	int i;
69176358Sdas
70176358Sdas	iy = (iy == 1 ? -1 : 1);	/* XXX recover original interface */
71176358Sdas	osign = (x >= 0 ? 1.0 : -1.0);	/* XXX slow, probably wrong for -0 */
72176358Sdas	if (fabsl(x) >= 0.67434) {
73176358Sdas		if (x < 0) {
74176358Sdas			x = -x;
75176358Sdas			y = -y;
76176358Sdas		}
77176358Sdas		z = pio4 - x;
78176358Sdas		w = pio4lo - y;
79176358Sdas		x = z + w;
80176358Sdas		y = 0.0;
81176358Sdas		i = 1;
82176358Sdas	} else
83176358Sdas		i = 0;
84176358Sdas	z = x * x;
85176358Sdas	w = z * z;
86176358Sdas	r = T5 + w * (T9 + w * (T13 + w * (T17 + w * (T21 +
87176358Sdas	    w * (T25 + w * (T29 + w * (T33 +
88176358Sdas	    w * (T37 + w * (T41 + w * (T45 + w * (T49 + w * (T53 +
89176358Sdas	    w * T57))))))))))));
90176358Sdas	v = z * (T7 + w * (T11 + w * (T15 + w * (T19 + w * (T23 +
91176358Sdas	    w * (T27 + w * (T31 + w * (T35 +
92176358Sdas	    w * (T39 + w * (T43 + w * (T47 + w * (T51 + w * T55))))))))))));
93176358Sdas	s = z * x;
94176358Sdas	r = y + z * (s * (r + v) + y);
95176358Sdas	r += T3 * s;
96176358Sdas	w = x + r;
97176358Sdas	if (i == 1) {
98176358Sdas		v = (long double) iy;
99176358Sdas		return osign *
100176358Sdas			(v - 2.0 * (x - (w * w / (w + v) - r)));
101176358Sdas	}
102176358Sdas	if (iy == 1)
103176358Sdas		return w;
104176358Sdas	else {
105176358Sdas		/*
106176358Sdas		 * if allow error up to 2 ulp, simply return
107176358Sdas		 * -1.0 / (x+r) here
108176358Sdas		 */
109176358Sdas		/* compute -1.0 / (x+r) accurately */
110176358Sdas		long double a, t;
111176358Sdas		z = w;
112176358Sdas		z = z + 0x1p32 - 0x1p32;
113176358Sdas		v = r - (z - x);	/* z+v = r+x */
114176358Sdas		t = a = -1.0 / w;	/* a = -1.0/w */
115176358Sdas		t = t + 0x1p32 - 0x1p32;
116176358Sdas		s = 1.0 + t * z;
117176358Sdas		return t + a * (s + t * v);
118176358Sdas	}
119176358Sdas}
120