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