xref: /netbsd-src/external/gpl3/gcc.old/dist/libquadmath/math/casinhq_kernel.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1*627f7eb2Smrg /* Return arc hyperbolic sine for a complex float type, with the
2*627f7eb2Smrg    imaginary part of the result possibly adjusted for use in
3*627f7eb2Smrg    computing other functions.
4*627f7eb2Smrg    Copyright (C) 1997-2018 Free Software Foundation, Inc.
5*627f7eb2Smrg    This file is part of the GNU C Library.
6*627f7eb2Smrg 
7*627f7eb2Smrg    The GNU C Library is free software; you can redistribute it and/or
8*627f7eb2Smrg    modify it under the terms of the GNU Lesser General Public
9*627f7eb2Smrg    License as published by the Free Software Foundation; either
10*627f7eb2Smrg    version 2.1 of the License, or (at your option) any later version.
11*627f7eb2Smrg 
12*627f7eb2Smrg    The GNU C Library is distributed in the hope that it will be useful,
13*627f7eb2Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*627f7eb2Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15*627f7eb2Smrg    Lesser General Public License for more details.
16*627f7eb2Smrg 
17*627f7eb2Smrg    You should have received a copy of the GNU Lesser General Public
18*627f7eb2Smrg    License along with the GNU C Library; if not, see
19*627f7eb2Smrg    <http://www.gnu.org/licenses/>.  */
20*627f7eb2Smrg 
21*627f7eb2Smrg #include "quadmath-imp.h"
22*627f7eb2Smrg 
23*627f7eb2Smrg /* Return the complex inverse hyperbolic sine of finite nonzero Z,
24*627f7eb2Smrg    with the imaginary part of the result subtracted from pi/2 if ADJ
25*627f7eb2Smrg    is nonzero.  */
26*627f7eb2Smrg 
27*627f7eb2Smrg __complex128
__quadmath_kernel_casinhq(__complex128 x,int adj)28*627f7eb2Smrg __quadmath_kernel_casinhq (__complex128 x, int adj)
29*627f7eb2Smrg {
30*627f7eb2Smrg   __complex128 res;
31*627f7eb2Smrg   __float128 rx, ix;
32*627f7eb2Smrg   __complex128 y;
33*627f7eb2Smrg 
34*627f7eb2Smrg   /* Avoid cancellation by reducing to the first quadrant.  */
35*627f7eb2Smrg   rx = fabsq (__real__ x);
36*627f7eb2Smrg   ix = fabsq (__imag__ x);
37*627f7eb2Smrg 
38*627f7eb2Smrg   if (rx >= 1 / FLT128_EPSILON || ix >= 1 / FLT128_EPSILON)
39*627f7eb2Smrg     {
40*627f7eb2Smrg       /* For large x in the first quadrant, x + csqrt (1 + x * x)
41*627f7eb2Smrg 	 is sufficiently close to 2 * x to make no significant
42*627f7eb2Smrg 	 difference to the result; avoid possible overflow from
43*627f7eb2Smrg 	 the squaring and addition.  */
44*627f7eb2Smrg       __real__ y = rx;
45*627f7eb2Smrg       __imag__ y = ix;
46*627f7eb2Smrg 
47*627f7eb2Smrg       if (adj)
48*627f7eb2Smrg 	{
49*627f7eb2Smrg 	  __float128 t = __real__ y;
50*627f7eb2Smrg 	  __real__ y = copysignq (__imag__ y, __imag__ x);
51*627f7eb2Smrg 	  __imag__ y = t;
52*627f7eb2Smrg 	}
53*627f7eb2Smrg 
54*627f7eb2Smrg       res = clogq (y);
55*627f7eb2Smrg       __real__ res += (__float128) M_LN2q;
56*627f7eb2Smrg     }
57*627f7eb2Smrg   else if (rx >= 0.5Q && ix < FLT128_EPSILON / 8)
58*627f7eb2Smrg     {
59*627f7eb2Smrg       __float128 s = hypotq (1, rx);
60*627f7eb2Smrg 
61*627f7eb2Smrg       __real__ res = logq (rx + s);
62*627f7eb2Smrg       if (adj)
63*627f7eb2Smrg 	__imag__ res = atan2q (s, __imag__ x);
64*627f7eb2Smrg       else
65*627f7eb2Smrg 	__imag__ res = atan2q (ix, s);
66*627f7eb2Smrg     }
67*627f7eb2Smrg   else if (rx < FLT128_EPSILON / 8 && ix >= 1.5Q)
68*627f7eb2Smrg     {
69*627f7eb2Smrg       __float128 s = sqrtq ((ix + 1) * (ix - 1));
70*627f7eb2Smrg 
71*627f7eb2Smrg       __real__ res = logq (ix + s);
72*627f7eb2Smrg       if (adj)
73*627f7eb2Smrg 	__imag__ res = atan2q (rx, copysignq (s, __imag__ x));
74*627f7eb2Smrg       else
75*627f7eb2Smrg 	__imag__ res = atan2q (s, rx);
76*627f7eb2Smrg     }
77*627f7eb2Smrg   else if (ix > 1 && ix < 1.5Q && rx < 0.5Q)
78*627f7eb2Smrg     {
79*627f7eb2Smrg       if (rx < FLT128_EPSILON * FLT128_EPSILON)
80*627f7eb2Smrg 	{
81*627f7eb2Smrg 	  __float128 ix2m1 = (ix + 1) * (ix - 1);
82*627f7eb2Smrg 	  __float128 s = sqrtq (ix2m1);
83*627f7eb2Smrg 
84*627f7eb2Smrg 	  __real__ res = log1pq (2 * (ix2m1 + ix * s)) / 2;
85*627f7eb2Smrg 	  if (adj)
86*627f7eb2Smrg 	    __imag__ res = atan2q (rx, copysignq (s, __imag__ x));
87*627f7eb2Smrg 	  else
88*627f7eb2Smrg 	    __imag__ res = atan2q (s, rx);
89*627f7eb2Smrg 	}
90*627f7eb2Smrg       else
91*627f7eb2Smrg 	{
92*627f7eb2Smrg 	  __float128 ix2m1 = (ix + 1) * (ix - 1);
93*627f7eb2Smrg 	  __float128 rx2 = rx * rx;
94*627f7eb2Smrg 	  __float128 f = rx2 * (2 + rx2 + 2 * ix * ix);
95*627f7eb2Smrg 	  __float128 d = sqrtq (ix2m1 * ix2m1 + f);
96*627f7eb2Smrg 	  __float128 dp = d + ix2m1;
97*627f7eb2Smrg 	  __float128 dm = f / dp;
98*627f7eb2Smrg 	  __float128 r1 = sqrtq ((dm + rx2) / 2);
99*627f7eb2Smrg 	  __float128 r2 = rx * ix / r1;
100*627f7eb2Smrg 
101*627f7eb2Smrg 	  __real__ res = log1pq (rx2 + dp + 2 * (rx * r1 + ix * r2)) / 2;
102*627f7eb2Smrg 	  if (adj)
103*627f7eb2Smrg 	    __imag__ res = atan2q (rx + r1, copysignq (ix + r2, __imag__ x));
104*627f7eb2Smrg 	  else
105*627f7eb2Smrg 	    __imag__ res = atan2q (ix + r2, rx + r1);
106*627f7eb2Smrg 	}
107*627f7eb2Smrg     }
108*627f7eb2Smrg   else if (ix == 1 && rx < 0.5Q)
109*627f7eb2Smrg     {
110*627f7eb2Smrg       if (rx < FLT128_EPSILON / 8)
111*627f7eb2Smrg 	{
112*627f7eb2Smrg 	  __real__ res = log1pq (2 * (rx + sqrtq (rx))) / 2;
113*627f7eb2Smrg 	  if (adj)
114*627f7eb2Smrg 	    __imag__ res = atan2q (sqrtq (rx), copysignq (1, __imag__ x));
115*627f7eb2Smrg 	  else
116*627f7eb2Smrg 	    __imag__ res = atan2q (1, sqrtq (rx));
117*627f7eb2Smrg 	}
118*627f7eb2Smrg       else
119*627f7eb2Smrg 	{
120*627f7eb2Smrg 	  __float128 d = rx * sqrtq (4 + rx * rx);
121*627f7eb2Smrg 	  __float128 s1 = sqrtq ((d + rx * rx) / 2);
122*627f7eb2Smrg 	  __float128 s2 = sqrtq ((d - rx * rx) / 2);
123*627f7eb2Smrg 
124*627f7eb2Smrg 	  __real__ res = log1pq (rx * rx + d + 2 * (rx * s1 + s2)) / 2;
125*627f7eb2Smrg 	  if (adj)
126*627f7eb2Smrg 	    __imag__ res = atan2q (rx + s1, copysignq (1 + s2, __imag__ x));
127*627f7eb2Smrg 	  else
128*627f7eb2Smrg 	    __imag__ res = atan2q (1 + s2, rx + s1);
129*627f7eb2Smrg 	}
130*627f7eb2Smrg     }
131*627f7eb2Smrg   else if (ix < 1 && rx < 0.5Q)
132*627f7eb2Smrg     {
133*627f7eb2Smrg       if (ix >= FLT128_EPSILON)
134*627f7eb2Smrg 	{
135*627f7eb2Smrg 	  if (rx < FLT128_EPSILON * FLT128_EPSILON)
136*627f7eb2Smrg 	    {
137*627f7eb2Smrg 	      __float128 onemix2 = (1 + ix) * (1 - ix);
138*627f7eb2Smrg 	      __float128 s = sqrtq (onemix2);
139*627f7eb2Smrg 
140*627f7eb2Smrg 	      __real__ res = log1pq (2 * rx / s) / 2;
141*627f7eb2Smrg 	      if (adj)
142*627f7eb2Smrg 		__imag__ res = atan2q (s, __imag__ x);
143*627f7eb2Smrg 	      else
144*627f7eb2Smrg 		__imag__ res = atan2q (ix, s);
145*627f7eb2Smrg 	    }
146*627f7eb2Smrg 	  else
147*627f7eb2Smrg 	    {
148*627f7eb2Smrg 	      __float128 onemix2 = (1 + ix) * (1 - ix);
149*627f7eb2Smrg 	      __float128 rx2 = rx * rx;
150*627f7eb2Smrg 	      __float128 f = rx2 * (2 + rx2 + 2 * ix * ix);
151*627f7eb2Smrg 	      __float128 d = sqrtq (onemix2 * onemix2 + f);
152*627f7eb2Smrg 	      __float128 dp = d + onemix2;
153*627f7eb2Smrg 	      __float128 dm = f / dp;
154*627f7eb2Smrg 	      __float128 r1 = sqrtq ((dp + rx2) / 2);
155*627f7eb2Smrg 	      __float128 r2 = rx * ix / r1;
156*627f7eb2Smrg 
157*627f7eb2Smrg 	      __real__ res = log1pq (rx2 + dm + 2 * (rx * r1 + ix * r2)) / 2;
158*627f7eb2Smrg 	      if (adj)
159*627f7eb2Smrg 		__imag__ res = atan2q (rx + r1, copysignq (ix + r2,
160*627f7eb2Smrg 							     __imag__ x));
161*627f7eb2Smrg 	      else
162*627f7eb2Smrg 		__imag__ res = atan2q (ix + r2, rx + r1);
163*627f7eb2Smrg 	    }
164*627f7eb2Smrg 	}
165*627f7eb2Smrg       else
166*627f7eb2Smrg 	{
167*627f7eb2Smrg 	  __float128 s = hypotq (1, rx);
168*627f7eb2Smrg 
169*627f7eb2Smrg 	  __real__ res = log1pq (2 * rx * (rx + s)) / 2;
170*627f7eb2Smrg 	  if (adj)
171*627f7eb2Smrg 	    __imag__ res = atan2q (s, __imag__ x);
172*627f7eb2Smrg 	  else
173*627f7eb2Smrg 	    __imag__ res = atan2q (ix, s);
174*627f7eb2Smrg 	}
175*627f7eb2Smrg       math_check_force_underflow_nonneg (__real__ res);
176*627f7eb2Smrg     }
177*627f7eb2Smrg   else
178*627f7eb2Smrg     {
179*627f7eb2Smrg       __real__ y = (rx - ix) * (rx + ix) + 1;
180*627f7eb2Smrg       __imag__ y = 2 * rx * ix;
181*627f7eb2Smrg 
182*627f7eb2Smrg       y = csqrtq (y);
183*627f7eb2Smrg 
184*627f7eb2Smrg       __real__ y += rx;
185*627f7eb2Smrg       __imag__ y += ix;
186*627f7eb2Smrg 
187*627f7eb2Smrg       if (adj)
188*627f7eb2Smrg 	{
189*627f7eb2Smrg 	  __float128 t = __real__ y;
190*627f7eb2Smrg 	  __real__ y = copysignq (__imag__ y, __imag__ x);
191*627f7eb2Smrg 	  __imag__ y = t;
192*627f7eb2Smrg 	}
193*627f7eb2Smrg 
194*627f7eb2Smrg       res = clogq (y);
195*627f7eb2Smrg     }
196*627f7eb2Smrg 
197*627f7eb2Smrg   /* Give results the correct sign for the original argument.  */
198*627f7eb2Smrg   __real__ res = copysignq (__real__ res, __real__ x);
199*627f7eb2Smrg   __imag__ res = copysignq (__imag__ res, (adj ? 1 : __imag__ x));
200*627f7eb2Smrg 
201*627f7eb2Smrg   return res;
202*627f7eb2Smrg }
203