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