xref: /freebsd/lib/msun/ld128/k_tanl.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1 /*
2  * ====================================================
3  * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
4  * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
5  *
6  * Permission to use, copy, modify, and distribute this
7  * software is freely granted, provided that this notice
8  * is preserved.
9  * ====================================================
10  */
11 
12 /*
13  * ld128 version of k_tan.c.  See ../src/k_tan.c for most comments.
14  */
15 
16 #include "math.h"
17 #include "math_private.h"
18 
19 /*
20  * Domain [-0.67434, 0.67434], range ~[-3.37e-36, 1.982e-37]
21  * |tan(x)/x - t(x)| < 2**-117.8 (XXX should be ~1e-37)
22  *
23  * See ../ld80/k_cosl.c for more details about the polynomial.
24  */
25 static const long double
26 T3 = 0x1.5555555555555555555555555553p-2L,
27 T5 = 0x1.1111111111111111111111111eb5p-3L,
28 T7 = 0x1.ba1ba1ba1ba1ba1ba1ba1b694cd6p-5L,
29 T9 = 0x1.664f4882c10f9f32d6bbe09d8bcdp-6L,
30 T11 = 0x1.226e355e6c23c8f5b4f5762322eep-7L,
31 T13 = 0x1.d6d3d0e157ddfb5fed8e84e27b37p-9L,
32 T15 = 0x1.7da36452b75e2b5fce9ee7c2c92ep-10L,
33 T17 = 0x1.355824803674477dfcf726649efep-11L,
34 T19 = 0x1.f57d7734d1656e0aceb716f614c2p-13L,
35 T21 = 0x1.967e18afcb180ed942dfdc518d6cp-14L,
36 T23 = 0x1.497d8eea21e95bc7e2aa79b9f2cdp-15L,
37 T25 = 0x1.0b132d39f055c81be49eff7afd50p-16L,
38 T27 = 0x1.b0f72d33eff7bfa2fbc1059d90b6p-18L,
39 T29 = 0x1.5ef2daf21d1113df38d0fbc00267p-19L,
40 T31 = 0x1.1c77d6eac0234988cdaa04c96626p-20L,
41 T33 = 0x1.cd2a5a292b180e0bdd701057dfe3p-22L,
42 T35 = 0x1.75c7357d0298c01a31d0a6f7d518p-23L,
43 T37 = 0x1.2f3190f4718a9a520f98f50081fcp-24L,
44 pio4 = 0x1.921fb54442d18469898cc51701b8p-1L,
45 pio4lo = 0x1.cd129024e088a67cc74020bbea60p-116L;
46 
47 static const double
48 T39 =  0.000000028443389121318352,	/*  0x1e8a7592977938.0p-78 */
49 T41 =  0.000000011981013102001973,	/*  0x19baa1b1223219.0p-79 */
50 T43 =  0.0000000038303578044958070,	/*  0x107385dfb24529.0p-80 */
51 T45 =  0.0000000034664378216909893,	/*  0x1dc6c702a05262.0p-81 */
52 T47 = -0.0000000015090641701997785,	/* -0x19ecef3569ebb6.0p-82 */
53 T49 =  0.0000000029449552300483952,	/*  0x194c0668da786a.0p-81 */
54 T51 = -0.0000000022006995706097711,	/* -0x12e763b8845268.0p-81 */
55 T53 =  0.0000000015468200913196612,	/*  0x1a92fc98c29554.0p-82 */
56 T55 = -0.00000000061311613386849674,	/* -0x151106cbc779a9.0p-83 */
57 T57 =  1.4912469681508012e-10;		/*  0x147edbdba6f43a.0p-85 */
58 
59 long double
60 __kernel_tanl(long double x, long double y, int iy) {
61 	long double z, r, v, w, s;
62 	long double osign;
63 	int i;
64 
65 	iy = (iy == 1 ? -1 : 1);	/* XXX recover original interface */
66 	osign = (x >= 0 ? 1.0 : -1.0);	/* XXX slow, probably wrong for -0 */
67 	if (fabsl(x) >= 0.67434) {
68 		if (x < 0) {
69 			x = -x;
70 			y = -y;
71 		}
72 		z = pio4 - x;
73 		w = pio4lo - y;
74 		x = z + w;
75 		y = 0.0;
76 		i = 1;
77 	} else
78 		i = 0;
79 	z = x * x;
80 	w = z * z;
81 	r = T5 + w * (T9 + w * (T13 + w * (T17 + w * (T21 +
82 	    w * (T25 + w * (T29 + w * (T33 +
83 	    w * (T37 + w * (T41 + w * (T45 + w * (T49 + w * (T53 +
84 	    w * T57))))))))))));
85 	v = z * (T7 + w * (T11 + w * (T15 + w * (T19 + w * (T23 +
86 	    w * (T27 + w * (T31 + w * (T35 +
87 	    w * (T39 + w * (T43 + w * (T47 + w * (T51 + w * T55))))))))))));
88 	s = z * x;
89 	r = y + z * (s * (r + v) + y);
90 	r += T3 * s;
91 	w = x + r;
92 	if (i == 1) {
93 		v = (long double) iy;
94 		return osign *
95 			(v - 2.0 * (x - (w * w / (w + v) - r)));
96 	}
97 	if (iy == 1)
98 		return w;
99 	else {
100 		/*
101 		 * if allow error up to 2 ulp, simply return
102 		 * -1.0 / (x+r) here
103 		 */
104 		/* compute -1.0 / (x+r) accurately */
105 		long double a, t;
106 		z = w;
107 		z = z + 0x1p32 - 0x1p32;
108 		v = r - (z - x);	/* z+v = r+x */
109 		t = a = -1.0 / w;	/* a = -1.0/w */
110 		t = t + 0x1p32 - 0x1p32;
111 		s = 1.0 + t * z;
112 		return t + a * (s + t * v);
113 	}
114 }
115