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