xref: /titanic_50/usr/src/lib/libm/common/C/__tan.c (revision 25c28e83beb90e7c80452a7c818c5e6f73a07dc8)
1*25c28e83SPiotr Jasiukajtis /*
2*25c28e83SPiotr Jasiukajtis  * CDDL HEADER START
3*25c28e83SPiotr Jasiukajtis  *
4*25c28e83SPiotr Jasiukajtis  * The contents of this file are subject to the terms of the
5*25c28e83SPiotr Jasiukajtis  * Common Development and Distribution License (the "License").
6*25c28e83SPiotr Jasiukajtis  * You may not use this file except in compliance with the License.
7*25c28e83SPiotr Jasiukajtis  *
8*25c28e83SPiotr Jasiukajtis  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*25c28e83SPiotr Jasiukajtis  * or http://www.opensolaris.org/os/licensing.
10*25c28e83SPiotr Jasiukajtis  * See the License for the specific language governing permissions
11*25c28e83SPiotr Jasiukajtis  * and limitations under the License.
12*25c28e83SPiotr Jasiukajtis  *
13*25c28e83SPiotr Jasiukajtis  * When distributing Covered Code, include this CDDL HEADER in each
14*25c28e83SPiotr Jasiukajtis  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*25c28e83SPiotr Jasiukajtis  * If applicable, add the following below this CDDL HEADER, with the
16*25c28e83SPiotr Jasiukajtis  * fields enclosed by brackets "[]" replaced with your own identifying
17*25c28e83SPiotr Jasiukajtis  * information: Portions Copyright [yyyy] [name of copyright owner]
18*25c28e83SPiotr Jasiukajtis  *
19*25c28e83SPiotr Jasiukajtis  * CDDL HEADER END
20*25c28e83SPiotr Jasiukajtis  */
21*25c28e83SPiotr Jasiukajtis 
22*25c28e83SPiotr Jasiukajtis /*
23*25c28e83SPiotr Jasiukajtis  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24*25c28e83SPiotr Jasiukajtis  */
25*25c28e83SPiotr Jasiukajtis /*
26*25c28e83SPiotr Jasiukajtis  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27*25c28e83SPiotr Jasiukajtis  * Use is subject to license terms.
28*25c28e83SPiotr Jasiukajtis  */
29*25c28e83SPiotr Jasiukajtis 
30*25c28e83SPiotr Jasiukajtis /* INDENT OFF */
31*25c28e83SPiotr Jasiukajtis /*
32*25c28e83SPiotr Jasiukajtis  * __k_tan( double x;  double y; int k )
33*25c28e83SPiotr Jasiukajtis  * kernel tan/cotan function on [-pi/4, pi/4], pi/4 ~ 0.785398164
34*25c28e83SPiotr Jasiukajtis  * Input x is assumed to be bounded by ~pi/4 in magnitude.
35*25c28e83SPiotr Jasiukajtis  * Input y is the tail of x.
36*25c28e83SPiotr Jasiukajtis  * Input k indicate -- tan if k=0; else -1/tan
37*25c28e83SPiotr Jasiukajtis  *
38*25c28e83SPiotr Jasiukajtis  * Table look up algorithm
39*25c28e83SPiotr Jasiukajtis  *	1. by tan(-x) = -tan(x), need only to consider positive x
40*25c28e83SPiotr Jasiukajtis  *	2. if x < 5/32 = [0x3fc40000, 0] = 0.15625 , then
41*25c28e83SPiotr Jasiukajtis  *	     if x < 2^-27 (hx < 0x3e400000 0), set w=x with inexact if x !=  0
42*25c28e83SPiotr Jasiukajtis  *	     else
43*25c28e83SPiotr Jasiukajtis  *		z = x*x;
44*25c28e83SPiotr Jasiukajtis  *		w = x + (y+(x*z)*(t1+z*(t2+z*(t3+z*(t4+z*(t5+z*t6))))))
45*25c28e83SPiotr Jasiukajtis  *	   return (k == 0)? w: 1/w;
46*25c28e83SPiotr Jasiukajtis  *	3. else
47*25c28e83SPiotr Jasiukajtis  *		ht = (hx + 0x4000)&0x7fff8000	(round x to a break point t)
48*25c28e83SPiotr Jasiukajtis  *		lt = 0
49*25c28e83SPiotr Jasiukajtis  *		i  = (hy-0x3fc40000)>>15;	(i<=64)
50*25c28e83SPiotr Jasiukajtis  *		x' = (x - t)+y 			(|x'| ~<= 2^-7)
51*25c28e83SPiotr Jasiukajtis  *	   By
52*25c28e83SPiotr Jasiukajtis  *		tan(t+x')
53*25c28e83SPiotr Jasiukajtis  *		  = (tan(t)+tan(x'))/(1-tan(x')tan(t))
54*25c28e83SPiotr Jasiukajtis  *	   We have
55*25c28e83SPiotr Jasiukajtis  *		             sin(x')+tan(t)*(tan(t)*sin(x'))
56*25c28e83SPiotr Jasiukajtis  *		  = tan(t) + -------------------------------	for k=0
57*25c28e83SPiotr Jasiukajtis  *			        cos(x') - tan(t)*sin(x')
58*25c28e83SPiotr Jasiukajtis  *
59*25c28e83SPiotr Jasiukajtis  *		             cos(x') - tan(t)*sin(x')
60*25c28e83SPiotr Jasiukajtis  *		  = - --------------------------------------	for k=1
61*25c28e83SPiotr Jasiukajtis  *		       tan(t) + tan(t)*(cos(x')-1) + sin(x')
62*25c28e83SPiotr Jasiukajtis  *
63*25c28e83SPiotr Jasiukajtis  *
64*25c28e83SPiotr Jasiukajtis  *	   where 	tan(t) is from the table,
65*25c28e83SPiotr Jasiukajtis  *			sin(x') = x + pp1*x^3 + pp2*x^5
66*25c28e83SPiotr Jasiukajtis  *			cos(x') = 1 + qq1*x^2 + qq2*x^4
67*25c28e83SPiotr Jasiukajtis  */
68*25c28e83SPiotr Jasiukajtis 
69*25c28e83SPiotr Jasiukajtis #include "libm.h"
70*25c28e83SPiotr Jasiukajtis 
71*25c28e83SPiotr Jasiukajtis extern const double _TBL_tan_hi[], _TBL_tan_lo[];
72*25c28e83SPiotr Jasiukajtis static const double q[] = {
73*25c28e83SPiotr Jasiukajtis /* one  = */  1.0,
74*25c28e83SPiotr Jasiukajtis /*
75*25c28e83SPiotr Jasiukajtis  *                       2       2       -59.56
76*25c28e83SPiotr Jasiukajtis  * |sin(x) - pp1*x*(pp2+x *(pp3+x )| <= 2        for |x|<1/64
77*25c28e83SPiotr Jasiukajtis  */
78*25c28e83SPiotr Jasiukajtis /* pp1  = */  8.33326120969096230395312119298978359438478946686e-0003,
79*25c28e83SPiotr Jasiukajtis /* pp2  = */  1.20001038589438965215025680596868692381425944526e+0002,
80*25c28e83SPiotr Jasiukajtis /* pp3  = */ -2.00001730975089451192161504877731204032897949219e+0001,
81*25c28e83SPiotr Jasiukajtis 
82*25c28e83SPiotr Jasiukajtis /*
83*25c28e83SPiotr Jasiukajtis  *                   2      2        -56.19
84*25c28e83SPiotr Jasiukajtis  * |cos(x) - (1+qq1*x (qq2+x ))| <= 2        for |x|<=1/128
85*25c28e83SPiotr Jasiukajtis  */
86*25c28e83SPiotr Jasiukajtis /* qq1  = */  4.16665486385721928197511942926212213933467864990e-0002,
87*25c28e83SPiotr Jasiukajtis /* qq2  = */ -1.20000339921340035687080671777948737144470214844e+0001,
88*25c28e83SPiotr Jasiukajtis 
89*25c28e83SPiotr Jasiukajtis /*
90*25c28e83SPiotr Jasiukajtis  * |tan(x) - PF(x)|
91*25c28e83SPiotr Jasiukajtis  * |--------------| <= 2^-58.57 for |x|<0.15625
92*25c28e83SPiotr Jasiukajtis  * |      x       |
93*25c28e83SPiotr Jasiukajtis  *
94*25c28e83SPiotr Jasiukajtis  * where (let z = x*x)
95*25c28e83SPiotr Jasiukajtis  *	PF(x) = x + (t1*x*z)(t2 + z(t3 + z))(t4 + z)(t5 + z(t6 + z))
96*25c28e83SPiotr Jasiukajtis  */
97*25c28e83SPiotr Jasiukajtis /* t1 = */  3.71923358986516816929168705030406272271648049355e-0003,
98*25c28e83SPiotr Jasiukajtis /* t2 = */  6.02645120354857866118436504621058702468872070312e+0000,
99*25c28e83SPiotr Jasiukajtis /* t3 = */  2.42627327587398156083509093150496482849121093750e+0000,
100*25c28e83SPiotr Jasiukajtis /* t4 = */  2.44968983934252770851003333518747240304946899414e+0000,
101*25c28e83SPiotr Jasiukajtis /* t5 = */  6.07089252571767978849948121933266520500183105469e+0000,
102*25c28e83SPiotr Jasiukajtis /* t6 = */ -2.49403756995593761658369658107403665781021118164e+0000,
103*25c28e83SPiotr Jasiukajtis };
104*25c28e83SPiotr Jasiukajtis 
105*25c28e83SPiotr Jasiukajtis 
106*25c28e83SPiotr Jasiukajtis #define	one q[0]
107*25c28e83SPiotr Jasiukajtis #define	pp1 q[1]
108*25c28e83SPiotr Jasiukajtis #define	pp2 q[2]
109*25c28e83SPiotr Jasiukajtis #define	pp3 q[3]
110*25c28e83SPiotr Jasiukajtis #define	qq1 q[4]
111*25c28e83SPiotr Jasiukajtis #define	qq2 q[5]
112*25c28e83SPiotr Jasiukajtis #define	t1  q[6]
113*25c28e83SPiotr Jasiukajtis #define	t2  q[7]
114*25c28e83SPiotr Jasiukajtis #define	t3  q[8]
115*25c28e83SPiotr Jasiukajtis #define	t4  q[9]
116*25c28e83SPiotr Jasiukajtis #define	t5  q[10]
117*25c28e83SPiotr Jasiukajtis #define	t6  q[11]
118*25c28e83SPiotr Jasiukajtis 
119*25c28e83SPiotr Jasiukajtis /* INDENT ON */
120*25c28e83SPiotr Jasiukajtis 
121*25c28e83SPiotr Jasiukajtis 
122*25c28e83SPiotr Jasiukajtis double
__k_tan(double x,double y,int k)123*25c28e83SPiotr Jasiukajtis __k_tan(double x, double y, int k) {
124*25c28e83SPiotr Jasiukajtis 	double a, t, z, w = 0.0L, s, c, r, rh, xh, xl;
125*25c28e83SPiotr Jasiukajtis 	int i, j, hx, ix;
126*25c28e83SPiotr Jasiukajtis 
127*25c28e83SPiotr Jasiukajtis 	t = one;
128*25c28e83SPiotr Jasiukajtis 	hx = ((int *) &x)[HIWORD];
129*25c28e83SPiotr Jasiukajtis 	ix = hx & 0x7fffffff;
130*25c28e83SPiotr Jasiukajtis 	if (ix < 0x3fc40000) {		/* 0.15625 */
131*25c28e83SPiotr Jasiukajtis 		if (ix < 0x3e400000) {	/* 2^-27 */
132*25c28e83SPiotr Jasiukajtis 			if ((i = (int) x) == 0)		/* generate inexact */
133*25c28e83SPiotr Jasiukajtis 				w = x;
134*25c28e83SPiotr Jasiukajtis 			t = y;
135*25c28e83SPiotr Jasiukajtis 		} else {
136*25c28e83SPiotr Jasiukajtis 			z = x * x;
137*25c28e83SPiotr Jasiukajtis 			t = y + (((t1 * x) * z) * (t2 + z * (t3 + z))) *
138*25c28e83SPiotr Jasiukajtis 				((t4 + z) * (t5 + z * (t6 + z)));
139*25c28e83SPiotr Jasiukajtis 			w = x + t;
140*25c28e83SPiotr Jasiukajtis 		}
141*25c28e83SPiotr Jasiukajtis 		if (k == 0)
142*25c28e83SPiotr Jasiukajtis 			return (w);
143*25c28e83SPiotr Jasiukajtis 		/*
144*25c28e83SPiotr Jasiukajtis 		 * Compute -1/(x+T) with great care
145*25c28e83SPiotr Jasiukajtis 		 * Let r = -1/(x+T), rh = r chopped to 20 bits.
146*25c28e83SPiotr Jasiukajtis 		 * Also let xh	= x+T chopped to 20 bits, xl = (x-xh)+T. Then
147*25c28e83SPiotr Jasiukajtis 		 *   -1/(x+T)	= rh + (-1/(x+T)-rh) = rh + r*(1+rh*(x+T))
148*25c28e83SPiotr Jasiukajtis 		 *		= rh + r*((1+rh*xh)+rh*xl).
149*25c28e83SPiotr Jasiukajtis 		 */
150*25c28e83SPiotr Jasiukajtis 		rh = r = -one / w;
151*25c28e83SPiotr Jasiukajtis 		((int *) &rh)[LOWORD] = 0;
152*25c28e83SPiotr Jasiukajtis 		xh = w;
153*25c28e83SPiotr Jasiukajtis 		((int *) &xh)[LOWORD] = 0;
154*25c28e83SPiotr Jasiukajtis 		xl = (x - xh) + t;
155*25c28e83SPiotr Jasiukajtis 		return (rh + r * ((one + rh * xh) + rh * xl));
156*25c28e83SPiotr Jasiukajtis 	}
157*25c28e83SPiotr Jasiukajtis 	j = (ix + 0x4000) & 0x7fff8000;
158*25c28e83SPiotr Jasiukajtis 	i = (j - 0x3fc40000) >> 15;
159*25c28e83SPiotr Jasiukajtis 	((int *) &t)[HIWORD] = j;
160*25c28e83SPiotr Jasiukajtis 	if (hx > 0)
161*25c28e83SPiotr Jasiukajtis 		x = y - (t - x);
162*25c28e83SPiotr Jasiukajtis 	else
163*25c28e83SPiotr Jasiukajtis 		x = -y - (t + x);
164*25c28e83SPiotr Jasiukajtis 	a = _TBL_tan_hi[i];
165*25c28e83SPiotr Jasiukajtis 	z = x * x;
166*25c28e83SPiotr Jasiukajtis 	s = (pp1 * x) * (pp2 + z * (pp3 + z));	/* sin(x) */
167*25c28e83SPiotr Jasiukajtis 	t = (qq1 * z) * (qq2 + z);		/* cos(x) - 1 */
168*25c28e83SPiotr Jasiukajtis 	if (k == 0) {
169*25c28e83SPiotr Jasiukajtis 		w = a * s;
170*25c28e83SPiotr Jasiukajtis 		t = _TBL_tan_lo[i] + (s + a * w) / (one - (w - t));
171*25c28e83SPiotr Jasiukajtis 		return (hx < 0 ? -a - t : a + t);
172*25c28e83SPiotr Jasiukajtis 	} else {
173*25c28e83SPiotr Jasiukajtis 		w = s + a * t;
174*25c28e83SPiotr Jasiukajtis 		c = w + _TBL_tan_lo[i];
175*25c28e83SPiotr Jasiukajtis 		t = a * s - t;
176*25c28e83SPiotr Jasiukajtis 		/*
177*25c28e83SPiotr Jasiukajtis 		 * Now try to compute [(1-T)/(a+c)] accurately
178*25c28e83SPiotr Jasiukajtis 		 *
179*25c28e83SPiotr Jasiukajtis 		 * Let r = 1/(a+c), rh = (1-T)*r chopped to 20 bits.
180*25c28e83SPiotr Jasiukajtis 		 * Also let xh = a+c chopped to 20 bits, xl = (a-xh)+c. Then
181*25c28e83SPiotr Jasiukajtis 		 *	(1-T)/(a+c) = rh + ((1-T)/(a+c)-rh)
182*25c28e83SPiotr Jasiukajtis 		 *		= rh + r*(1-T-rh*(a+c))
183*25c28e83SPiotr Jasiukajtis 		 *		= rh + r*((1-T-rh*xh)-rh*xl)
184*25c28e83SPiotr Jasiukajtis 		 *		= rh + r*(((1-rh*xh)-T)-rh*xl)
185*25c28e83SPiotr Jasiukajtis 		 */
186*25c28e83SPiotr Jasiukajtis 		r = one / (a + c);
187*25c28e83SPiotr Jasiukajtis 		rh = (one - t) * r;
188*25c28e83SPiotr Jasiukajtis 		((int *) &rh)[LOWORD] = 0;
189*25c28e83SPiotr Jasiukajtis 		xh = a + c;
190*25c28e83SPiotr Jasiukajtis 		((int *) &xh)[LOWORD] = 0;
191*25c28e83SPiotr Jasiukajtis 		xl = (a - xh) + c;
192*25c28e83SPiotr Jasiukajtis 		z = rh + r * (((one - rh * xh) - t) - rh * xl);
193*25c28e83SPiotr Jasiukajtis 		return (hx >= 0 ? -z : z);
194*25c28e83SPiotr Jasiukajtis 	}
195*25c28e83SPiotr Jasiukajtis }
196