xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/tanq_kernel.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /*
2*181254a7Smrg  * ====================================================
3*181254a7Smrg  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4*181254a7Smrg  *
5*181254a7Smrg  * Developed at SunPro, a Sun Microsystems, Inc. business.
6*181254a7Smrg  * Permission to use, copy, modify, and distribute this
7*181254a7Smrg  * software is freely granted, provided that this notice
8*181254a7Smrg  * is preserved.
9*181254a7Smrg  * ====================================================
10*181254a7Smrg  */
11*181254a7Smrg 
12*181254a7Smrg /*
13*181254a7Smrg   Long double expansions are
14*181254a7Smrg   Copyright (C) 2001 Stephen L. Moshier <moshier@na-net.ornl.gov>
15*181254a7Smrg   and are incorporated herein by permission of the author.  The author
16*181254a7Smrg   reserves the right to distribute this material elsewhere under different
17*181254a7Smrg   copying permissions.  These modifications are distributed here under
18*181254a7Smrg   the following terms:
19*181254a7Smrg 
20*181254a7Smrg     This library is free software; you can redistribute it and/or
21*181254a7Smrg     modify it under the terms of the GNU Lesser General Public
22*181254a7Smrg     License as published by the Free Software Foundation; either
23*181254a7Smrg     version 2.1 of the License, or (at your option) any later version.
24*181254a7Smrg 
25*181254a7Smrg     This library is distributed in the hope that it will be useful,
26*181254a7Smrg     but WITHOUT ANY WARRANTY; without even the implied warranty of
27*181254a7Smrg     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28*181254a7Smrg     Lesser General Public License for more details.
29*181254a7Smrg 
30*181254a7Smrg     You should have received a copy of the GNU Lesser General Public
31*181254a7Smrg     License along with this library; if not, see
32*181254a7Smrg     <http://www.gnu.org/licenses/>.  */
33*181254a7Smrg 
34*181254a7Smrg /* __quadmath_kernel_tanq( x, y, k )
35*181254a7Smrg  * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854
36*181254a7Smrg  * Input x is assumed to be bounded by ~pi/4 in magnitude.
37*181254a7Smrg  * Input y is the tail of x.
38*181254a7Smrg  * Input k indicates whether tan (if k=1) or
39*181254a7Smrg  * -1/tan (if k= -1) is returned.
40*181254a7Smrg  *
41*181254a7Smrg  * Algorithm
42*181254a7Smrg  *	1. Since tan(-x) = -tan(x), we need only to consider positive x.
43*181254a7Smrg  *	2. if x < 2^-57, return x with inexact if x!=0.
44*181254a7Smrg  *	3. tan(x) is approximated by a rational form x + x^3 / 3 + x^5 R(x^2)
45*181254a7Smrg  *          on [0,0.67433].
46*181254a7Smrg  *
47*181254a7Smrg  *	   Note: tan(x+y) = tan(x) + tan'(x)*y
48*181254a7Smrg  *		          ~ tan(x) + (1+x*x)*y
49*181254a7Smrg  *	   Therefore, for better accuracy in computing tan(x+y), let
50*181254a7Smrg  *		r = x^3 * R(x^2)
51*181254a7Smrg  *	   then
52*181254a7Smrg  *		tan(x+y) = x + (x^3 / 3 + (x^2 *(r+y)+y))
53*181254a7Smrg  *
54*181254a7Smrg  *      4. For x in [0.67433,pi/4],  let y = pi/4 - x, then
55*181254a7Smrg  *		tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y))
56*181254a7Smrg  *		       = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y)))
57*181254a7Smrg  */
58*181254a7Smrg 
59*181254a7Smrg #include "quadmath-imp.h"
60*181254a7Smrg 
61*181254a7Smrg static const __float128
62*181254a7Smrg   one = 1,
63*181254a7Smrg   pio4hi = 7.8539816339744830961566084581987569936977E-1Q,
64*181254a7Smrg   pio4lo = 2.1679525325309452561992610065108379921906E-35Q,
65*181254a7Smrg 
66*181254a7Smrg   /* tan x = x + x^3 / 3 + x^5 T(x^2)/U(x^2)
67*181254a7Smrg      0 <= x <= 0.6743316650390625
68*181254a7Smrg      Peak relative error 8.0e-36  */
69*181254a7Smrg  TH =  3.333333333333333333333333333333333333333E-1Q,
70*181254a7Smrg  T0 = -1.813014711743583437742363284336855889393E7Q,
71*181254a7Smrg  T1 =  1.320767960008972224312740075083259247618E6Q,
72*181254a7Smrg  T2 = -2.626775478255838182468651821863299023956E4Q,
73*181254a7Smrg  T3 =  1.764573356488504935415411383687150199315E2Q,
74*181254a7Smrg  T4 = -3.333267763822178690794678978979803526092E-1Q,
75*181254a7Smrg 
76*181254a7Smrg  U0 = -1.359761033807687578306772463253710042010E8Q,
77*181254a7Smrg  U1 =  6.494370630656893175666729313065113194784E7Q,
78*181254a7Smrg  U2 = -4.180787672237927475505536849168729386782E6Q,
79*181254a7Smrg  U3 =  8.031643765106170040139966622980914621521E4Q,
80*181254a7Smrg  U4 = -5.323131271912475695157127875560667378597E2Q;
81*181254a7Smrg   /* 1.000000000000000000000000000000000000000E0 */
82*181254a7Smrg 
83*181254a7Smrg 
84*181254a7Smrg __float128
__quadmath_kernel_tanq(__float128 x,__float128 y,int iy)85*181254a7Smrg __quadmath_kernel_tanq (__float128 x, __float128 y, int iy)
86*181254a7Smrg {
87*181254a7Smrg   __float128 z, r, v, w, s;
88*181254a7Smrg   int32_t ix, sign;
89*181254a7Smrg   ieee854_float128 u, u1;
90*181254a7Smrg 
91*181254a7Smrg   u.value = x;
92*181254a7Smrg   ix = u.words32.w0 & 0x7fffffff;
93*181254a7Smrg   if (ix < 0x3fc60000)		/* x < 2**-57 */
94*181254a7Smrg     {
95*181254a7Smrg       if ((int) x == 0)
96*181254a7Smrg 	{			/* generate inexact */
97*181254a7Smrg 	  if ((ix | u.words32.w1 | u.words32.w2 | u.words32.w3
98*181254a7Smrg 	       | (iy + 1)) == 0)
99*181254a7Smrg 	    return one / fabsq (x);
100*181254a7Smrg 	  else if (iy == 1)
101*181254a7Smrg 	    {
102*181254a7Smrg 	      math_check_force_underflow (x);
103*181254a7Smrg 	      return x;
104*181254a7Smrg 	    }
105*181254a7Smrg 	  else
106*181254a7Smrg 	    return -one / x;
107*181254a7Smrg 	}
108*181254a7Smrg     }
109*181254a7Smrg   if (ix >= 0x3ffe5942) /* |x| >= 0.6743316650390625 */
110*181254a7Smrg     {
111*181254a7Smrg       if ((u.words32.w0 & 0x80000000) != 0)
112*181254a7Smrg 	{
113*181254a7Smrg 	  x = -x;
114*181254a7Smrg 	  y = -y;
115*181254a7Smrg 	  sign = -1;
116*181254a7Smrg 	}
117*181254a7Smrg       else
118*181254a7Smrg 	sign = 1;
119*181254a7Smrg       z = pio4hi - x;
120*181254a7Smrg       w = pio4lo - y;
121*181254a7Smrg       x = z + w;
122*181254a7Smrg       y = 0.0;
123*181254a7Smrg     }
124*181254a7Smrg   z = x * x;
125*181254a7Smrg   r = T0 + z * (T1 + z * (T2 + z * (T3 + z * T4)));
126*181254a7Smrg   v = U0 + z * (U1 + z * (U2 + z * (U3 + z * (U4 + z))));
127*181254a7Smrg   r = r / v;
128*181254a7Smrg 
129*181254a7Smrg   s = z * x;
130*181254a7Smrg   r = y + z * (s * r + y);
131*181254a7Smrg   r += TH * s;
132*181254a7Smrg   w = x + r;
133*181254a7Smrg   if (ix >= 0x3ffe5942)
134*181254a7Smrg     {
135*181254a7Smrg       v = (__float128) iy;
136*181254a7Smrg       w = (v - 2.0 * (x - (w * w / (w + v) - r)));
137*181254a7Smrg       /* SIGN is set for arguments that reach this code, but not
138*181254a7Smrg 	 otherwise, resulting in warnings that it may be used
139*181254a7Smrg 	 uninitialized although in the cases where it is used it has
140*181254a7Smrg 	 always been set.  */
141*181254a7Smrg 
142*181254a7Smrg 
143*181254a7Smrg       if (sign < 0)
144*181254a7Smrg 	w = -w;
145*181254a7Smrg 
146*181254a7Smrg       return w;
147*181254a7Smrg     }
148*181254a7Smrg   if (iy == 1)
149*181254a7Smrg     return w;
150*181254a7Smrg   else
151*181254a7Smrg     {				/* if allow error up to 2 ulp,
152*181254a7Smrg 				   simply return -1.0/(x+r) here */
153*181254a7Smrg       /*  compute -1.0/(x+r) accurately */
154*181254a7Smrg       u1.value = w;
155*181254a7Smrg       u1.words32.w2 = 0;
156*181254a7Smrg       u1.words32.w3 = 0;
157*181254a7Smrg       v = r - (u1.value - x);		/* u1+v = r+x */
158*181254a7Smrg       z = -1.0 / w;
159*181254a7Smrg       u.value = z;
160*181254a7Smrg       u.words32.w2 = 0;
161*181254a7Smrg       u.words32.w3 = 0;
162*181254a7Smrg       s = 1.0 + u.value * u1.value;
163*181254a7Smrg       return u.value + z * (s + u.value * v);
164*181254a7Smrg     }
165*181254a7Smrg }
166