xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/catanhq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* Return arc hyperbolic tangent for a complex float type.
2*181254a7Smrg    Copyright (C) 1997-2018 Free Software Foundation, Inc.
3*181254a7Smrg    This file is part of the GNU C Library.
4*181254a7Smrg    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5*181254a7Smrg 
6*181254a7Smrg    The GNU C Library is free software; you can redistribute it and/or
7*181254a7Smrg    modify it under the terms of the GNU Lesser General Public
8*181254a7Smrg    License as published by the Free Software Foundation; either
9*181254a7Smrg    version 2.1 of the License, or (at your option) any later version.
10*181254a7Smrg 
11*181254a7Smrg    The GNU C Library is distributed in the hope that it will be useful,
12*181254a7Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*181254a7Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*181254a7Smrg    Lesser General Public License for more details.
15*181254a7Smrg 
16*181254a7Smrg    You should have received a copy of the GNU Lesser General Public
17*181254a7Smrg    License along with the GNU C Library; if not, see
18*181254a7Smrg    <http://www.gnu.org/licenses/>.  */
19*181254a7Smrg 
20*181254a7Smrg #include "quadmath-imp.h"
21*181254a7Smrg 
22*181254a7Smrg __complex128
catanhq(__complex128 x)23*181254a7Smrg catanhq (__complex128 x)
24*181254a7Smrg {
25*181254a7Smrg   __complex128 res;
26*181254a7Smrg   int rcls = fpclassifyq (__real__ x);
27*181254a7Smrg   int icls = fpclassifyq (__imag__ x);
28*181254a7Smrg 
29*181254a7Smrg   if (__glibc_unlikely (rcls <= QUADFP_INFINITE || icls <= QUADFP_INFINITE))
30*181254a7Smrg     {
31*181254a7Smrg       if (icls == QUADFP_INFINITE)
32*181254a7Smrg 	{
33*181254a7Smrg 	  __real__ res = copysignq (0, __real__ x);
34*181254a7Smrg 	  __imag__ res = copysignq (M_PI_2q, __imag__ x);
35*181254a7Smrg 	}
36*181254a7Smrg       else if (rcls == QUADFP_INFINITE || rcls == QUADFP_ZERO)
37*181254a7Smrg 	{
38*181254a7Smrg 	  __real__ res = copysignq (0, __real__ x);
39*181254a7Smrg 	  if (icls >= QUADFP_ZERO)
40*181254a7Smrg 	    __imag__ res = copysignq (M_PI_2q, __imag__ x);
41*181254a7Smrg 	  else
42*181254a7Smrg 	    __imag__ res = nanq ("");
43*181254a7Smrg 	}
44*181254a7Smrg       else
45*181254a7Smrg 	{
46*181254a7Smrg 	  __real__ res = nanq ("");
47*181254a7Smrg 	  __imag__ res = nanq ("");
48*181254a7Smrg 	}
49*181254a7Smrg     }
50*181254a7Smrg   else if (__glibc_unlikely (rcls == QUADFP_ZERO && icls == QUADFP_ZERO))
51*181254a7Smrg     {
52*181254a7Smrg       res = x;
53*181254a7Smrg     }
54*181254a7Smrg   else
55*181254a7Smrg     {
56*181254a7Smrg       if (fabsq (__real__ x) >= 16 / FLT128_EPSILON
57*181254a7Smrg 	  || fabsq (__imag__ x) >= 16 / FLT128_EPSILON)
58*181254a7Smrg 	{
59*181254a7Smrg 	  __imag__ res = copysignq (M_PI_2q, __imag__ x);
60*181254a7Smrg 	  if (fabsq (__imag__ x) <= 1)
61*181254a7Smrg 	    __real__ res = 1 / __real__ x;
62*181254a7Smrg 	  else if (fabsq (__real__ x) <= 1)
63*181254a7Smrg 	    __real__ res = __real__ x / __imag__ x / __imag__ x;
64*181254a7Smrg 	  else
65*181254a7Smrg 	    {
66*181254a7Smrg 	      __float128 h = hypotq (__real__ x / 2, __imag__ x / 2);
67*181254a7Smrg 	      __real__ res = __real__ x / h / h / 4;
68*181254a7Smrg 	    }
69*181254a7Smrg 	}
70*181254a7Smrg       else
71*181254a7Smrg 	{
72*181254a7Smrg 	  if (fabsq (__real__ x) == 1
73*181254a7Smrg 	      && fabsq (__imag__ x) < FLT128_EPSILON * FLT128_EPSILON)
74*181254a7Smrg 	    __real__ res = (copysignq (0.5Q, __real__ x)
75*181254a7Smrg 			    * ((__float128) M_LN2q
76*181254a7Smrg 			       - logq (fabsq (__imag__ x))));
77*181254a7Smrg 	  else
78*181254a7Smrg 	    {
79*181254a7Smrg 	      __float128 i2 = 0;
80*181254a7Smrg 	      if (fabsq (__imag__ x) >= FLT128_EPSILON * FLT128_EPSILON)
81*181254a7Smrg 		i2 = __imag__ x * __imag__ x;
82*181254a7Smrg 
83*181254a7Smrg 	      __float128 num = 1 + __real__ x;
84*181254a7Smrg 	      num = i2 + num * num;
85*181254a7Smrg 
86*181254a7Smrg 	      __float128 den = 1 - __real__ x;
87*181254a7Smrg 	      den = i2 + den * den;
88*181254a7Smrg 
89*181254a7Smrg 	      __float128 f = num / den;
90*181254a7Smrg 	      if (f < 0.5Q)
91*181254a7Smrg 		__real__ res = 0.25Q * logq (f);
92*181254a7Smrg 	      else
93*181254a7Smrg 		{
94*181254a7Smrg 		  num = 4 * __real__ x;
95*181254a7Smrg 		  __real__ res = 0.25Q * log1pq (num / den);
96*181254a7Smrg 		}
97*181254a7Smrg 	    }
98*181254a7Smrg 
99*181254a7Smrg 	  __float128 absx, absy, den;
100*181254a7Smrg 
101*181254a7Smrg 	  absx = fabsq (__real__ x);
102*181254a7Smrg 	  absy = fabsq (__imag__ x);
103*181254a7Smrg 	  if (absx < absy)
104*181254a7Smrg 	    {
105*181254a7Smrg 	      __float128 t = absx;
106*181254a7Smrg 	      absx = absy;
107*181254a7Smrg 	      absy = t;
108*181254a7Smrg 	    }
109*181254a7Smrg 
110*181254a7Smrg 	  if (absy < FLT128_EPSILON / 2)
111*181254a7Smrg 	    {
112*181254a7Smrg 	      den = (1 - absx) * (1 + absx);
113*181254a7Smrg 	      if (den == 0)
114*181254a7Smrg 		den = 0;
115*181254a7Smrg 	    }
116*181254a7Smrg 	  else if (absx >= 1)
117*181254a7Smrg 	    den = (1 - absx) * (1 + absx) - absy * absy;
118*181254a7Smrg 	  else if (absx >= 0.75Q || absy >= 0.5Q)
119*181254a7Smrg 	    den = -__quadmath_x2y2m1q (absx, absy);
120*181254a7Smrg 	  else
121*181254a7Smrg 	    den = (1 - absx) * (1 + absx) - absy * absy;
122*181254a7Smrg 
123*181254a7Smrg 	  __imag__ res = 0.5Q * atan2q (2 * __imag__ x, den);
124*181254a7Smrg 	}
125*181254a7Smrg 
126*181254a7Smrg       math_check_force_underflow_complex (res);
127*181254a7Smrg     }
128*181254a7Smrg 
129*181254a7Smrg   return res;
130*181254a7Smrg }
131