]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/k_casinh_template.c
Add per-thread cache to malloc
[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.
bfff8b1b 4 Copyright (C) 1997-2017 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
19 <http://www.gnu.org/licenses/>. */
20
21#include <complex.h>
22#include <math.h>
23#include <math_private.h>
24#include <float.h>
25
26/* Return the complex inverse hyperbolic sine of finite nonzero Z,
27 with the imaginary part of the result subtracted from pi/2 if ADJ
28 is nonzero. */
29
c50eee19
PM
30CFLOAT
31M_DECL_FUNC (__kernel_casinh) (CFLOAT x, int adj)
ffb84f5e 32{
c50eee19
PM
33 CFLOAT res;
34 FLOAT rx, ix;
35 CFLOAT y;
ffb84f5e
PM
36
37 /* Avoid cancellation by reducing to the first quadrant. */
c50eee19
PM
38 rx = M_FABS (__real__ x);
39 ix = M_FABS (__imag__ x);
ffb84f5e 40
c50eee19 41 if (rx >= 1 / M_EPSILON || ix >= 1 / M_EPSILON)
ffb84f5e
PM
42 {
43 /* For large x in the first quadrant, x + csqrt (1 + x * x)
44 is sufficiently close to 2 * x to make no significant
45 difference to the result; avoid possible overflow from
46 the squaring and addition. */
47 __real__ y = rx;
48 __imag__ y = ix;
49
50 if (adj)
51 {
c50eee19
PM
52 FLOAT t = __real__ y;
53 __real__ y = M_COPYSIGN (__imag__ y, __imag__ x);
ffb84f5e
PM
54 __imag__ y = t;
55 }
56
c50eee19
PM
57 res = M_SUF (__clog) (y);
58 __real__ res += (FLOAT) M_MLIT (M_LN2);
ffb84f5e 59 }
c50eee19 60 else if (rx >= M_LIT (0.5) && ix < M_EPSILON / 8)
ffb84f5e 61 {
c50eee19 62 FLOAT s = M_HYPOT (1, rx);
ffb84f5e 63
c50eee19 64 __real__ res = M_LOG (rx + s);
ffb84f5e 65 if (adj)
c50eee19 66 __imag__ res = M_ATAN2 (s, __imag__ x);
ffb84f5e 67 else
c50eee19 68 __imag__ res = M_ATAN2 (ix, s);
ffb84f5e 69 }
c50eee19 70 else if (rx < M_EPSILON / 8 && ix >= M_LIT (1.5))
ffb84f5e 71 {
c50eee19 72 FLOAT s = M_SQRT ((ix + 1) * (ix - 1));
ffb84f5e 73
c50eee19 74 __real__ res = M_LOG (ix + s);
ffb84f5e 75 if (adj)
c50eee19 76 __imag__ res = M_ATAN2 (rx, M_COPYSIGN (s, __imag__ x));
ffb84f5e 77 else
c50eee19 78 __imag__ res = M_ATAN2 (s, rx);
ffb84f5e 79 }
c50eee19 80 else if (ix > 1 && ix < M_LIT (1.5) && rx < M_LIT (0.5))
ffb84f5e 81 {
c50eee19 82 if (rx < M_EPSILON * M_EPSILON)
ffb84f5e 83 {
c50eee19
PM
84 FLOAT ix2m1 = (ix + 1) * (ix - 1);
85 FLOAT s = M_SQRT (ix2m1);
ffb84f5e 86
c50eee19 87 __real__ res = M_LOG1P (2 * (ix2m1 + ix * s)) / 2;
ffb84f5e 88 if (adj)
c50eee19 89 __imag__ res = M_ATAN2 (rx, M_COPYSIGN (s, __imag__ x));
ffb84f5e 90 else
c50eee19 91 __imag__ res = M_ATAN2 (s, rx);
ffb84f5e
PM
92 }
93 else
94 {
c50eee19
PM
95 FLOAT ix2m1 = (ix + 1) * (ix - 1);
96 FLOAT rx2 = rx * rx;
97 FLOAT f = rx2 * (2 + rx2 + 2 * ix * ix);
98 FLOAT d = M_SQRT (ix2m1 * ix2m1 + f);
99 FLOAT dp = d + ix2m1;
100 FLOAT dm = f / dp;
101 FLOAT r1 = M_SQRT ((dm + rx2) / 2);
102 FLOAT r2 = rx * ix / r1;
103
104 __real__ res = M_LOG1P (rx2 + dp + 2 * (rx * r1 + ix * r2)) / 2;
ffb84f5e 105 if (adj)
c50eee19 106 __imag__ res = M_ATAN2 (rx + r1, M_COPYSIGN (ix + r2, __imag__ x));
ffb84f5e 107 else
c50eee19 108 __imag__ res = M_ATAN2 (ix + r2, rx + r1);
ffb84f5e
PM
109 }
110 }
c50eee19 111 else if (ix == 1 && rx < M_LIT (0.5))
ffb84f5e 112 {
c50eee19 113 if (rx < M_EPSILON / 8)
ffb84f5e 114 {
c50eee19 115 __real__ res = M_LOG1P (2 * (rx + M_SQRT (rx))) / 2;
ffb84f5e 116 if (adj)
c50eee19 117 __imag__ res = M_ATAN2 (M_SQRT (rx), M_COPYSIGN (1, __imag__ x));
ffb84f5e 118 else
c50eee19 119 __imag__ res = M_ATAN2 (1, M_SQRT (rx));
ffb84f5e
PM
120 }
121 else
122 {
c50eee19
PM
123 FLOAT d = rx * M_SQRT (4 + rx * rx);
124 FLOAT s1 = M_SQRT ((d + rx * rx) / 2);
125 FLOAT s2 = M_SQRT ((d - rx * rx) / 2);
ffb84f5e 126
c50eee19 127 __real__ res = M_LOG1P (rx * rx + d + 2 * (rx * s1 + s2)) / 2;
ffb84f5e 128 if (adj)
c50eee19 129 __imag__ res = M_ATAN2 (rx + s1, M_COPYSIGN (1 + s2, __imag__ x));
ffb84f5e 130 else
c50eee19 131 __imag__ res = M_ATAN2 (1 + s2, rx + s1);
ffb84f5e
PM
132 }
133 }
c50eee19 134 else if (ix < 1 && rx < M_LIT (0.5))
ffb84f5e 135 {
c50eee19 136 if (ix >= M_EPSILON)
ffb84f5e 137 {
c50eee19 138 if (rx < M_EPSILON * M_EPSILON)
ffb84f5e 139 {
c50eee19
PM
140 FLOAT onemix2 = (1 + ix) * (1 - ix);
141 FLOAT s = M_SQRT (onemix2);
ffb84f5e 142
c50eee19 143 __real__ res = M_LOG1P (2 * rx / s) / 2;
ffb84f5e 144 if (adj)
c50eee19 145 __imag__ res = M_ATAN2 (s, __imag__ x);
ffb84f5e 146 else
c50eee19 147 __imag__ res = M_ATAN2 (ix, s);
ffb84f5e
PM
148 }
149 else
150 {
c50eee19
PM
151 FLOAT onemix2 = (1 + ix) * (1 - ix);
152 FLOAT rx2 = rx * rx;
153 FLOAT f = rx2 * (2 + rx2 + 2 * ix * ix);
154 FLOAT d = M_SQRT (onemix2 * onemix2 + f);
155 FLOAT dp = d + onemix2;
156 FLOAT dm = f / dp;
157 FLOAT r1 = M_SQRT ((dp + rx2) / 2);
158 FLOAT r2 = rx * ix / r1;
159
160 __real__ res = M_LOG1P (rx2 + dm + 2 * (rx * r1 + ix * r2)) / 2;
ffb84f5e 161 if (adj)
c50eee19
PM
162 __imag__ res = M_ATAN2 (rx + r1, M_COPYSIGN (ix + r2,
163 __imag__ x));
ffb84f5e 164 else
c50eee19 165 __imag__ res = M_ATAN2 (ix + r2, rx + r1);
ffb84f5e
PM
166 }
167 }
168 else
169 {
c50eee19 170 FLOAT s = M_HYPOT (1, rx);
ffb84f5e 171
c50eee19 172 __real__ res = M_LOG1P (2 * rx * (rx + s)) / 2;
ffb84f5e 173 if (adj)
c50eee19 174 __imag__ res = M_ATAN2 (s, __imag__ x);
ffb84f5e 175 else
c50eee19 176 __imag__ res = M_ATAN2 (ix, s);
ffb84f5e
PM
177 }
178 math_check_force_underflow_nonneg (__real__ res);
179 }
180 else
181 {
c50eee19
PM
182 __real__ y = (rx - ix) * (rx + ix) + 1;
183 __imag__ y = 2 * rx * ix;
ffb84f5e 184
c50eee19 185 y = M_SUF (__csqrt) (y);
ffb84f5e
PM
186
187 __real__ y += rx;
188 __imag__ y += ix;
189
190 if (adj)
191 {
c50eee19
PM
192 FLOAT t = __real__ y;
193 __real__ y = M_COPYSIGN (__imag__ y, __imag__ x);
ffb84f5e
PM
194 __imag__ y = t;
195 }
196
c50eee19 197 res = M_SUF (__clog) (y);
ffb84f5e
PM
198 }
199
200 /* Give results the correct sign for the original argument. */
c50eee19
PM
201 __real__ res = M_COPYSIGN (__real__ res, __real__ x);
202 __imag__ res = M_COPYSIGN (__imag__ res, (adj ? 1 : __imag__ x));
ffb84f5e
PM
203
204 return res;
205}