catrig.c revision 1.2.2.2 1 1.2.2.2 pgoyette /* $NetBSD: catrig.c,v 1.2.2.2 2016/11/04 14:48:54 pgoyette Exp $ */
2 1.2.2.2 pgoyette /*-
3 1.2.2.2 pgoyette * Copyright (c) 2012 Stephen Montgomery-Smith <stephen (at) FreeBSD.ORG>
4 1.2.2.2 pgoyette * All rights reserved.
5 1.2.2.2 pgoyette *
6 1.2.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
7 1.2.2.2 pgoyette * modification, are permitted provided that the following conditions
8 1.2.2.2 pgoyette * are met:
9 1.2.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
10 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
11 1.2.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
12 1.2.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
13 1.2.2.2 pgoyette * documentation and/or other materials provided with the distribution.
14 1.2.2.2 pgoyette *
15 1.2.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.2.2.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.2.2.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.2.2.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.2.2.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.2.2.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.2.2.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.2.2.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.2.2.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.2.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.2.2.2 pgoyette * SUCH DAMAGE.
26 1.2.2.2 pgoyette */
27 1.2.2.2 pgoyette
28 1.2.2.2 pgoyette #include <sys/cdefs.h>
29 1.2.2.2 pgoyette #if 0
30 1.2.2.2 pgoyette __FBSDID("$FreeBSD: head/lib/msun/src/catrig.c 275819 2014-12-16 09:21:56Z ed $");
31 1.2.2.2 pgoyette #endif
32 1.2.2.2 pgoyette __RCSID("$NetBSD: catrig.c,v 1.2.2.2 2016/11/04 14:48:54 pgoyette Exp $");
33 1.2.2.2 pgoyette
34 1.2.2.2 pgoyette #include "namespace.h"
35 1.2.2.2 pgoyette #ifdef __weak_alias
36 1.2.2.2 pgoyette __weak_alias(casin, _casin)
37 1.2.2.2 pgoyette #endif
38 1.2.2.2 pgoyette #ifdef __weak_alias
39 1.2.2.2 pgoyette __weak_alias(catan, _catan)
40 1.2.2.2 pgoyette #endif
41 1.2.2.2 pgoyette
42 1.2.2.2 pgoyette #include <complex.h>
43 1.2.2.2 pgoyette #include <float.h>
44 1.2.2.2 pgoyette
45 1.2.2.2 pgoyette #include "math.h"
46 1.2.2.2 pgoyette #include "math_private.h"
47 1.2.2.2 pgoyette
48 1.2.2.2 pgoyette
49 1.2.2.2 pgoyette
50 1.2.2.2 pgoyette #undef isinf
51 1.2.2.2 pgoyette #define isinf(x) (fabs(x) == INFINITY)
52 1.2.2.2 pgoyette #undef isnan
53 1.2.2.2 pgoyette #define isnan(x) ((x) != (x))
54 1.2.2.2 pgoyette #define raise_inexact() do { volatile float junk __unused = /*LINTED*/1 + tiny; } while(/*CONSTCOND*/0)
55 1.2.2.2 pgoyette #undef signbit
56 1.2.2.2 pgoyette #define signbit(x) (__builtin_signbit(x))
57 1.2.2.2 pgoyette
58 1.2.2.2 pgoyette /* We need that DBL_EPSILON^2/128 is larger than FOUR_SQRT_MIN. */
59 1.2.2.2 pgoyette static const double
60 1.2.2.2 pgoyette A_crossover = 10, /* Hull et al suggest 1.5, but 10 works better */
61 1.2.2.2 pgoyette B_crossover = 0.6417, /* suggested by Hull et al */
62 1.2.2.2 pgoyette m_e = 2.7182818284590452e0, /* 0x15bf0a8b145769.0p-51 */
63 1.2.2.2 pgoyette m_ln2 = 6.9314718055994531e-1, /* 0x162e42fefa39ef.0p-53 */
64 1.2.2.2 pgoyette pio2_hi = 1.5707963267948966e0, /* 0x1921fb54442d18.0p-52 */
65 1.2.2.2 pgoyette RECIP_EPSILON = 1 / DBL_EPSILON,
66 1.2.2.2 pgoyette SQRT_3_EPSILON = 2.5809568279517849e-8, /* 0x1bb67ae8584caa.0p-78 */
67 1.2.2.2 pgoyette SQRT_6_EPSILON = 3.6500241499888571e-8, /* 0x13988e1409212e.0p-77 */
68 1.2.2.2 pgoyette #if DBL_MAX_EXP == 1024 /* IEEE */
69 1.2.2.2 pgoyette FOUR_SQRT_MIN = 0x1p-509, /* >= 4 * sqrt(DBL_MIN) */
70 1.2.2.2 pgoyette QUARTER_SQRT_MAX = 0x1p509, /* <= sqrt(DBL_MAX) / 4 */
71 1.2.2.2 pgoyette SQRT_MIN = 0x1p-511; /* >= sqrt(DBL_MIN) */
72 1.2.2.2 pgoyette #elif DBL_MAX_EXP == 127 /* VAX */
73 1.2.2.2 pgoyette FOUR_SQRT_MIN = 0x1p-62, /* >= 4 * sqrt(DBL_MIN) */
74 1.2.2.2 pgoyette QUARTER_SQRT_MAX = 0x1p62, /* <= sqrt(DBL_MAX) / 4 */
75 1.2.2.2 pgoyette SQRT_MIN = 0x1p-64; /* >= sqrt(DBL_MIN) */
76 1.2.2.2 pgoyette #else
77 1.2.2.2 pgoyette #error "unsupported floating point format"
78 1.2.2.2 pgoyette #endif
79 1.2.2.2 pgoyette
80 1.2.2.2 pgoyette
81 1.2.2.2 pgoyette static const volatile double
82 1.2.2.2 pgoyette pio2_lo = 6.1232339957367659e-17; /* 0x11a62633145c07.0p-106 */
83 1.2.2.2 pgoyette static const volatile float
84 1.2.2.2 pgoyette tiny = 0x1p-100;
85 1.2.2.2 pgoyette
86 1.2.2.2 pgoyette static double complex clog_for_large_values(double complex z);
87 1.2.2.2 pgoyette
88 1.2.2.2 pgoyette /*
89 1.2.2.2 pgoyette * Testing indicates that all these functions are accurate up to 4 ULP.
90 1.2.2.2 pgoyette * The functions casin(h) and cacos(h) are about 2.5 times slower than asinh.
91 1.2.2.2 pgoyette * The functions catan(h) are a little under 2 times slower than atanh.
92 1.2.2.2 pgoyette *
93 1.2.2.2 pgoyette * The code for casinh, casin, cacos, and cacosh comes first. The code is
94 1.2.2.2 pgoyette * rather complicated, and the four functions are highly interdependent.
95 1.2.2.2 pgoyette *
96 1.2.2.2 pgoyette * The code for catanh and catan comes at the end. It is much simpler than
97 1.2.2.2 pgoyette * the other functions, and the code for these can be disconnected from the
98 1.2.2.2 pgoyette * rest of the code.
99 1.2.2.2 pgoyette */
100 1.2.2.2 pgoyette
101 1.2.2.2 pgoyette /*
102 1.2.2.2 pgoyette * ================================
103 1.2.2.2 pgoyette * | casinh, casin, cacos, cacosh |
104 1.2.2.2 pgoyette * ================================
105 1.2.2.2 pgoyette */
106 1.2.2.2 pgoyette
107 1.2.2.2 pgoyette /*
108 1.2.2.2 pgoyette * The algorithm is very close to that in "Implementing the complex arcsine
109 1.2.2.2 pgoyette * and arccosine functions using exception handling" by T. E. Hull, Thomas F.
110 1.2.2.2 pgoyette * Fairgrieve, and Ping Tak Peter Tang, published in ACM Transactions on
111 1.2.2.2 pgoyette * Mathematical Software, Volume 23 Issue 3, 1997, Pages 299-335,
112 1.2.2.2 pgoyette * http://dl.acm.org/citation.cfm?id=275324.
113 1.2.2.2 pgoyette *
114 1.2.2.2 pgoyette * Throughout we use the convention z = x + I*y.
115 1.2.2.2 pgoyette *
116 1.2.2.2 pgoyette * casinh(z) = sign(x)*log(A+sqrt(A*A-1)) + I*asin(B)
117 1.2.2.2 pgoyette * where
118 1.2.2.2 pgoyette * A = (|z+I| + |z-I|) / 2
119 1.2.2.2 pgoyette * B = (|z+I| - |z-I|) / 2 = y/A
120 1.2.2.2 pgoyette *
121 1.2.2.2 pgoyette * These formulas become numerically unstable:
122 1.2.2.2 pgoyette * (a) for Re(casinh(z)) when z is close to the line segment [-I, I] (that
123 1.2.2.2 pgoyette * is, Re(casinh(z)) is close to 0);
124 1.2.2.2 pgoyette * (b) for Im(casinh(z)) when z is close to either of the intervals
125 1.2.2.2 pgoyette * [I, I*infinity) or (-I*infinity, -I] (that is, |Im(casinh(z))| is
126 1.2.2.2 pgoyette * close to PI/2).
127 1.2.2.2 pgoyette *
128 1.2.2.2 pgoyette * These numerical problems are overcome by defining
129 1.2.2.2 pgoyette * f(a, b) = (hypot(a, b) - b) / 2 = a*a / (hypot(a, b) + b) / 2
130 1.2.2.2 pgoyette * Then if A < A_crossover, we use
131 1.2.2.2 pgoyette * log(A + sqrt(A*A-1)) = log1p((A-1) + sqrt((A-1)*(A+1)))
132 1.2.2.2 pgoyette * A-1 = f(x, 1+y) + f(x, 1-y)
133 1.2.2.2 pgoyette * and if B > B_crossover, we use
134 1.2.2.2 pgoyette * asin(B) = atan2(y, sqrt(A*A - y*y)) = atan2(y, sqrt((A+y)*(A-y)))
135 1.2.2.2 pgoyette * A-y = f(x, y+1) + f(x, y-1)
136 1.2.2.2 pgoyette * where without loss of generality we have assumed that x and y are
137 1.2.2.2 pgoyette * non-negative.
138 1.2.2.2 pgoyette *
139 1.2.2.2 pgoyette * Much of the difficulty comes because the intermediate computations may
140 1.2.2.2 pgoyette * produce overflows or underflows. This is dealt with in the paper by Hull
141 1.2.2.2 pgoyette * et al by using exception handling. We do this by detecting when
142 1.2.2.2 pgoyette * computations risk underflow or overflow. The hardest part is handling the
143 1.2.2.2 pgoyette * underflows when computing f(a, b).
144 1.2.2.2 pgoyette *
145 1.2.2.2 pgoyette * Note that the function f(a, b) does not appear explicitly in the paper by
146 1.2.2.2 pgoyette * Hull et al, but the idea may be found on pages 308 and 309. Introducing the
147 1.2.2.2 pgoyette * function f(a, b) allows us to concentrate many of the clever tricks in this
148 1.2.2.2 pgoyette * paper into one function.
149 1.2.2.2 pgoyette */
150 1.2.2.2 pgoyette
151 1.2.2.2 pgoyette /*
152 1.2.2.2 pgoyette * Function f(a, b, hypot_a_b) = (hypot(a, b) - b) / 2.
153 1.2.2.2 pgoyette * Pass hypot(a, b) as the third argument.
154 1.2.2.2 pgoyette */
155 1.2.2.2 pgoyette static inline double
156 1.2.2.2 pgoyette f(double a, double b, double hypot_a_b)
157 1.2.2.2 pgoyette {
158 1.2.2.2 pgoyette if (b < 0)
159 1.2.2.2 pgoyette return ((hypot_a_b - b) / 2);
160 1.2.2.2 pgoyette if (b == 0)
161 1.2.2.2 pgoyette return (a / 2);
162 1.2.2.2 pgoyette return (a * a / (hypot_a_b + b) / 2);
163 1.2.2.2 pgoyette }
164 1.2.2.2 pgoyette
165 1.2.2.2 pgoyette /*
166 1.2.2.2 pgoyette * All the hard work is contained in this function.
167 1.2.2.2 pgoyette * x and y are assumed positive or zero, and less than RECIP_EPSILON.
168 1.2.2.2 pgoyette * Upon return:
169 1.2.2.2 pgoyette * rx = Re(casinh(z)) = -Im(cacos(y + I*x)).
170 1.2.2.2 pgoyette * B_is_usable is set to 1 if the value of B is usable.
171 1.2.2.2 pgoyette * If B_is_usable is set to 0, sqrt_A2my2 = sqrt(A*A - y*y), and new_y = y.
172 1.2.2.2 pgoyette * If returning sqrt_A2my2 has potential to result in an underflow, it is
173 1.2.2.2 pgoyette * rescaled, and new_y is similarly rescaled.
174 1.2.2.2 pgoyette */
175 1.2.2.2 pgoyette static inline void
176 1.2.2.2 pgoyette do_hard_work(double x, double y, double *rx, int *B_is_usable, double *B,
177 1.2.2.2 pgoyette double *sqrt_A2my2, double *new_y)
178 1.2.2.2 pgoyette {
179 1.2.2.2 pgoyette double R, S, A; /* A, B, R, and S are as in Hull et al. */
180 1.2.2.2 pgoyette double Am1, Amy; /* A-1, A-y. */
181 1.2.2.2 pgoyette
182 1.2.2.2 pgoyette R = hypot(x, y + 1); /* |z+I| */
183 1.2.2.2 pgoyette S = hypot(x, y - 1); /* |z-I| */
184 1.2.2.2 pgoyette
185 1.2.2.2 pgoyette /* A = (|z+I| + |z-I|) / 2 */
186 1.2.2.2 pgoyette A = (R + S) / 2;
187 1.2.2.2 pgoyette /*
188 1.2.2.2 pgoyette * Mathematically A >= 1. There is a small chance that this will not
189 1.2.2.2 pgoyette * be so because of rounding errors. So we will make certain it is
190 1.2.2.2 pgoyette * so.
191 1.2.2.2 pgoyette */
192 1.2.2.2 pgoyette if (A < 1)
193 1.2.2.2 pgoyette A = 1;
194 1.2.2.2 pgoyette
195 1.2.2.2 pgoyette if (A < A_crossover) {
196 1.2.2.2 pgoyette /*
197 1.2.2.2 pgoyette * Am1 = fp + fm, where fp = f(x, 1+y), and fm = f(x, 1-y).
198 1.2.2.2 pgoyette * rx = log1p(Am1 + sqrt(Am1*(A+1)))
199 1.2.2.2 pgoyette */
200 1.2.2.2 pgoyette if (y == 1 && x < DBL_EPSILON * DBL_EPSILON / 128) {
201 1.2.2.2 pgoyette /*
202 1.2.2.2 pgoyette * fp is of order x^2, and fm = x/2.
203 1.2.2.2 pgoyette * A = 1 (inexactly).
204 1.2.2.2 pgoyette */
205 1.2.2.2 pgoyette *rx = sqrt(x);
206 1.2.2.2 pgoyette } else if (x >= DBL_EPSILON * fabs(y - 1)) {
207 1.2.2.2 pgoyette /*
208 1.2.2.2 pgoyette * Underflow will not occur because
209 1.2.2.2 pgoyette * x >= DBL_EPSILON^2/128 >= FOUR_SQRT_MIN
210 1.2.2.2 pgoyette */
211 1.2.2.2 pgoyette Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
212 1.2.2.2 pgoyette *rx = log1p(Am1 + sqrt(Am1 * (A + 1)));
213 1.2.2.2 pgoyette } else if (y < 1) {
214 1.2.2.2 pgoyette /*
215 1.2.2.2 pgoyette * fp = x*x/(1+y)/4, fm = x*x/(1-y)/4, and
216 1.2.2.2 pgoyette * A = 1 (inexactly).
217 1.2.2.2 pgoyette */
218 1.2.2.2 pgoyette *rx = x / sqrt((1 - y) * (1 + y));
219 1.2.2.2 pgoyette } else { /* if (y > 1) */
220 1.2.2.2 pgoyette /*
221 1.2.2.2 pgoyette * A-1 = y-1 (inexactly).
222 1.2.2.2 pgoyette */
223 1.2.2.2 pgoyette *rx = log1p((y - 1) + sqrt((y - 1) * (y + 1)));
224 1.2.2.2 pgoyette }
225 1.2.2.2 pgoyette } else {
226 1.2.2.2 pgoyette *rx = log(A + sqrt(A * A - 1));
227 1.2.2.2 pgoyette }
228 1.2.2.2 pgoyette
229 1.2.2.2 pgoyette *new_y = y;
230 1.2.2.2 pgoyette
231 1.2.2.2 pgoyette if (y < FOUR_SQRT_MIN) {
232 1.2.2.2 pgoyette /*
233 1.2.2.2 pgoyette * Avoid a possible underflow caused by y/A. For casinh this
234 1.2.2.2 pgoyette * would be legitimate, but will be picked up by invoking atan2
235 1.2.2.2 pgoyette * later on. For cacos this would not be legitimate.
236 1.2.2.2 pgoyette */
237 1.2.2.2 pgoyette *B_is_usable = 0;
238 1.2.2.2 pgoyette *sqrt_A2my2 = A * (2 / DBL_EPSILON);
239 1.2.2.2 pgoyette *new_y = y * (2 / DBL_EPSILON);
240 1.2.2.2 pgoyette return;
241 1.2.2.2 pgoyette }
242 1.2.2.2 pgoyette
243 1.2.2.2 pgoyette /* B = (|z+I| - |z-I|) / 2 = y/A */
244 1.2.2.2 pgoyette *B = y / A;
245 1.2.2.2 pgoyette *B_is_usable = 1;
246 1.2.2.2 pgoyette
247 1.2.2.2 pgoyette if (*B > B_crossover) {
248 1.2.2.2 pgoyette *B_is_usable = 0;
249 1.2.2.2 pgoyette /*
250 1.2.2.2 pgoyette * Amy = fp + fm, where fp = f(x, y+1), and fm = f(x, y-1).
251 1.2.2.2 pgoyette * sqrt_A2my2 = sqrt(Amy*(A+y))
252 1.2.2.2 pgoyette */
253 1.2.2.2 pgoyette if (y == 1 && x < DBL_EPSILON / 128) {
254 1.2.2.2 pgoyette /*
255 1.2.2.2 pgoyette * fp is of order x^2, and fm = x/2.
256 1.2.2.2 pgoyette * A = 1 (inexactly).
257 1.2.2.2 pgoyette */
258 1.2.2.2 pgoyette *sqrt_A2my2 = sqrt(x) * sqrt((A + y) / 2);
259 1.2.2.2 pgoyette } else if (x >= DBL_EPSILON * fabs(y - 1)) {
260 1.2.2.2 pgoyette /*
261 1.2.2.2 pgoyette * Underflow will not occur because
262 1.2.2.2 pgoyette * x >= DBL_EPSILON/128 >= FOUR_SQRT_MIN
263 1.2.2.2 pgoyette * and
264 1.2.2.2 pgoyette * x >= DBL_EPSILON^2 >= FOUR_SQRT_MIN
265 1.2.2.2 pgoyette */
266 1.2.2.2 pgoyette Amy = f(x, y + 1, R) + f(x, y - 1, S);
267 1.2.2.2 pgoyette *sqrt_A2my2 = sqrt(Amy * (A + y));
268 1.2.2.2 pgoyette } else if (y > 1) {
269 1.2.2.2 pgoyette /*
270 1.2.2.2 pgoyette * fp = x*x/(y+1)/4, fm = x*x/(y-1)/4, and
271 1.2.2.2 pgoyette * A = y (inexactly).
272 1.2.2.2 pgoyette *
273 1.2.2.2 pgoyette * y < RECIP_EPSILON. So the following
274 1.2.2.2 pgoyette * scaling should avoid any underflow problems.
275 1.2.2.2 pgoyette */
276 1.2.2.2 pgoyette *sqrt_A2my2 = x * (4 / DBL_EPSILON / DBL_EPSILON) * y /
277 1.2.2.2 pgoyette sqrt((y + 1) * (y - 1));
278 1.2.2.2 pgoyette *new_y = y * (4 / DBL_EPSILON / DBL_EPSILON);
279 1.2.2.2 pgoyette } else { /* if (y < 1) */
280 1.2.2.2 pgoyette /*
281 1.2.2.2 pgoyette * fm = 1-y >= DBL_EPSILON, fp is of order x^2, and
282 1.2.2.2 pgoyette * A = 1 (inexactly).
283 1.2.2.2 pgoyette */
284 1.2.2.2 pgoyette *sqrt_A2my2 = sqrt((1 - y) * (1 + y));
285 1.2.2.2 pgoyette }
286 1.2.2.2 pgoyette }
287 1.2.2.2 pgoyette }
288 1.2.2.2 pgoyette
289 1.2.2.2 pgoyette /*
290 1.2.2.2 pgoyette * casinh(z) = z + O(z^3) as z -> 0
291 1.2.2.2 pgoyette *
292 1.2.2.2 pgoyette * casinh(z) = sign(x)*clog(sign(x)*z) + O(1/z^2) as z -> infinity
293 1.2.2.2 pgoyette * The above formula works for the imaginary part as well, because
294 1.2.2.2 pgoyette * Im(casinh(z)) = sign(x)*atan2(sign(x)*y, fabs(x)) + O(y/z^3)
295 1.2.2.2 pgoyette * as z -> infinity, uniformly in y
296 1.2.2.2 pgoyette */
297 1.2.2.2 pgoyette double complex
298 1.2.2.2 pgoyette casinh(double complex z)
299 1.2.2.2 pgoyette {
300 1.2.2.2 pgoyette double x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
301 1.2.2.2 pgoyette int B_is_usable;
302 1.2.2.2 pgoyette double complex w;
303 1.2.2.2 pgoyette
304 1.2.2.2 pgoyette x = creal(z);
305 1.2.2.2 pgoyette y = cimag(z);
306 1.2.2.2 pgoyette ax = fabs(x);
307 1.2.2.2 pgoyette ay = fabs(y);
308 1.2.2.2 pgoyette
309 1.2.2.2 pgoyette if (isnan(x) || isnan(y)) {
310 1.2.2.2 pgoyette /* casinh(+-Inf + I*NaN) = +-Inf + I*NaN */
311 1.2.2.2 pgoyette if (isinf(x))
312 1.2.2.2 pgoyette return (CMPLX(x, y + y));
313 1.2.2.2 pgoyette /* casinh(NaN + I*+-Inf) = opt(+-)Inf + I*NaN */
314 1.2.2.2 pgoyette if (isinf(y))
315 1.2.2.2 pgoyette return (CMPLX(y, x + x));
316 1.2.2.2 pgoyette /* casinh(NaN + I*0) = NaN + I*0 */
317 1.2.2.2 pgoyette if (y == 0)
318 1.2.2.2 pgoyette return (CMPLX(x + x, y));
319 1.2.2.2 pgoyette /*
320 1.2.2.2 pgoyette * All other cases involving NaN return NaN + I*NaN.
321 1.2.2.2 pgoyette * C99 leaves it optional whether to raise invalid if one of
322 1.2.2.2 pgoyette * the arguments is not NaN, so we opt not to raise it.
323 1.2.2.2 pgoyette */
324 1.2.2.2 pgoyette return (CMPLX(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
325 1.2.2.2 pgoyette }
326 1.2.2.2 pgoyette
327 1.2.2.2 pgoyette if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
328 1.2.2.2 pgoyette /* clog...() will raise inexact unless x or y is infinite. */
329 1.2.2.2 pgoyette if (signbit(x) == 0)
330 1.2.2.2 pgoyette w = clog_for_large_values(z) + m_ln2;
331 1.2.2.2 pgoyette else
332 1.2.2.2 pgoyette w = clog_for_large_values(-z) + m_ln2;
333 1.2.2.2 pgoyette return (CMPLX(copysign(creal(w), x), copysign(cimag(w), y)));
334 1.2.2.2 pgoyette }
335 1.2.2.2 pgoyette
336 1.2.2.2 pgoyette /* Avoid spuriously raising inexact for z = 0. */
337 1.2.2.2 pgoyette if (x == 0 && y == 0)
338 1.2.2.2 pgoyette return (z);
339 1.2.2.2 pgoyette
340 1.2.2.2 pgoyette /* All remaining cases are inexact. */
341 1.2.2.2 pgoyette raise_inexact();
342 1.2.2.2 pgoyette
343 1.2.2.2 pgoyette if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
344 1.2.2.2 pgoyette return (z);
345 1.2.2.2 pgoyette
346 1.2.2.2 pgoyette do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
347 1.2.2.2 pgoyette if (B_is_usable)
348 1.2.2.2 pgoyette ry = asin(B);
349 1.2.2.2 pgoyette else
350 1.2.2.2 pgoyette ry = atan2(new_y, sqrt_A2my2);
351 1.2.2.2 pgoyette return (CMPLX(copysign(rx, x), copysign(ry, y)));
352 1.2.2.2 pgoyette }
353 1.2.2.2 pgoyette
354 1.2.2.2 pgoyette /*
355 1.2.2.2 pgoyette * casin(z) = reverse(casinh(reverse(z)))
356 1.2.2.2 pgoyette * where reverse(x + I*y) = y + I*x = I*conj(z).
357 1.2.2.2 pgoyette */
358 1.2.2.2 pgoyette double complex
359 1.2.2.2 pgoyette casin(double complex z)
360 1.2.2.2 pgoyette {
361 1.2.2.2 pgoyette double complex w = casinh(CMPLX(cimag(z), creal(z)));
362 1.2.2.2 pgoyette
363 1.2.2.2 pgoyette return (CMPLX(cimag(w), creal(w)));
364 1.2.2.2 pgoyette }
365 1.2.2.2 pgoyette
366 1.2.2.2 pgoyette /*
367 1.2.2.2 pgoyette * cacos(z) = PI/2 - casin(z)
368 1.2.2.2 pgoyette * but do the computation carefully so cacos(z) is accurate when z is
369 1.2.2.2 pgoyette * close to 1.
370 1.2.2.2 pgoyette *
371 1.2.2.2 pgoyette * cacos(z) = PI/2 - z + O(z^3) as z -> 0
372 1.2.2.2 pgoyette *
373 1.2.2.2 pgoyette * cacos(z) = -sign(y)*I*clog(z) + O(1/z^2) as z -> infinity
374 1.2.2.2 pgoyette * The above formula works for the real part as well, because
375 1.2.2.2 pgoyette * Re(cacos(z)) = atan2(fabs(y), x) + O(y/z^3)
376 1.2.2.2 pgoyette * as z -> infinity, uniformly in y
377 1.2.2.2 pgoyette */
378 1.2.2.2 pgoyette double complex
379 1.2.2.2 pgoyette cacos(double complex z)
380 1.2.2.2 pgoyette {
381 1.2.2.2 pgoyette double x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
382 1.2.2.2 pgoyette int sx, sy;
383 1.2.2.2 pgoyette int B_is_usable;
384 1.2.2.2 pgoyette double complex w;
385 1.2.2.2 pgoyette
386 1.2.2.2 pgoyette x = creal(z);
387 1.2.2.2 pgoyette y = cimag(z);
388 1.2.2.2 pgoyette sx = signbit(x);
389 1.2.2.2 pgoyette sy = signbit(y);
390 1.2.2.2 pgoyette ax = fabs(x);
391 1.2.2.2 pgoyette ay = fabs(y);
392 1.2.2.2 pgoyette
393 1.2.2.2 pgoyette if (isnan(x) || isnan(y)) {
394 1.2.2.2 pgoyette /* cacos(+-Inf + I*NaN) = NaN + I*opt(-)Inf */
395 1.2.2.2 pgoyette if (isinf(x))
396 1.2.2.2 pgoyette return (CMPLX(y + y, -INFINITY));
397 1.2.2.2 pgoyette /* cacos(NaN + I*+-Inf) = NaN + I*-+Inf */
398 1.2.2.2 pgoyette if (isinf(y))
399 1.2.2.2 pgoyette return (CMPLX(x + x, -y));
400 1.2.2.2 pgoyette /* cacos(0 + I*NaN) = PI/2 + I*NaN with inexact */
401 1.2.2.2 pgoyette if (x == 0)
402 1.2.2.2 pgoyette return (CMPLX(pio2_hi + pio2_lo, y + y));
403 1.2.2.2 pgoyette /*
404 1.2.2.2 pgoyette * All other cases involving NaN return NaN + I*NaN.
405 1.2.2.2 pgoyette * C99 leaves it optional whether to raise invalid if one of
406 1.2.2.2 pgoyette * the arguments is not NaN, so we opt not to raise it.
407 1.2.2.2 pgoyette */
408 1.2.2.2 pgoyette return (CMPLX(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
409 1.2.2.2 pgoyette }
410 1.2.2.2 pgoyette
411 1.2.2.2 pgoyette if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
412 1.2.2.2 pgoyette /* clog...() will raise inexact unless x or y is infinite. */
413 1.2.2.2 pgoyette w = clog_for_large_values(z);
414 1.2.2.2 pgoyette rx = fabs(cimag(w));
415 1.2.2.2 pgoyette ry = creal(w) + m_ln2;
416 1.2.2.2 pgoyette if (sy == 0)
417 1.2.2.2 pgoyette ry = -ry;
418 1.2.2.2 pgoyette return (CMPLX(rx, ry));
419 1.2.2.2 pgoyette }
420 1.2.2.2 pgoyette
421 1.2.2.2 pgoyette /* Avoid spuriously raising inexact for z = 1. */
422 1.2.2.2 pgoyette if (x == 1 && y == 0)
423 1.2.2.2 pgoyette return (CMPLX(0, -y));
424 1.2.2.2 pgoyette
425 1.2.2.2 pgoyette /* All remaining cases are inexact. */
426 1.2.2.2 pgoyette raise_inexact();
427 1.2.2.2 pgoyette
428 1.2.2.2 pgoyette if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
429 1.2.2.2 pgoyette return (CMPLX(pio2_hi - (x - pio2_lo), -y));
430 1.2.2.2 pgoyette
431 1.2.2.2 pgoyette do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
432 1.2.2.2 pgoyette if (B_is_usable) {
433 1.2.2.2 pgoyette if (sx == 0)
434 1.2.2.2 pgoyette rx = acos(B);
435 1.2.2.2 pgoyette else
436 1.2.2.2 pgoyette rx = acos(-B);
437 1.2.2.2 pgoyette } else {
438 1.2.2.2 pgoyette if (sx == 0)
439 1.2.2.2 pgoyette rx = atan2(sqrt_A2mx2, new_x);
440 1.2.2.2 pgoyette else
441 1.2.2.2 pgoyette rx = atan2(sqrt_A2mx2, -new_x);
442 1.2.2.2 pgoyette }
443 1.2.2.2 pgoyette if (sy == 0)
444 1.2.2.2 pgoyette ry = -ry;
445 1.2.2.2 pgoyette return (CMPLX(rx, ry));
446 1.2.2.2 pgoyette }
447 1.2.2.2 pgoyette
448 1.2.2.2 pgoyette /*
449 1.2.2.2 pgoyette * cacosh(z) = I*cacos(z) or -I*cacos(z)
450 1.2.2.2 pgoyette * where the sign is chosen so Re(cacosh(z)) >= 0.
451 1.2.2.2 pgoyette */
452 1.2.2.2 pgoyette double complex
453 1.2.2.2 pgoyette cacosh(double complex z)
454 1.2.2.2 pgoyette {
455 1.2.2.2 pgoyette double complex w;
456 1.2.2.2 pgoyette double rx, ry;
457 1.2.2.2 pgoyette
458 1.2.2.2 pgoyette w = cacos(z);
459 1.2.2.2 pgoyette rx = creal(w);
460 1.2.2.2 pgoyette ry = cimag(w);
461 1.2.2.2 pgoyette /* cacosh(NaN + I*NaN) = NaN + I*NaN */
462 1.2.2.2 pgoyette if (isnan(rx) && isnan(ry))
463 1.2.2.2 pgoyette return (CMPLX(ry, rx));
464 1.2.2.2 pgoyette /* cacosh(NaN + I*+-Inf) = +Inf + I*NaN */
465 1.2.2.2 pgoyette /* cacosh(+-Inf + I*NaN) = +Inf + I*NaN */
466 1.2.2.2 pgoyette if (isnan(rx))
467 1.2.2.2 pgoyette return (CMPLX(fabs(ry), rx));
468 1.2.2.2 pgoyette /* cacosh(0 + I*NaN) = NaN + I*NaN */
469 1.2.2.2 pgoyette if (isnan(ry))
470 1.2.2.2 pgoyette return (CMPLX(ry, ry));
471 1.2.2.2 pgoyette return (CMPLX(fabs(ry), copysign(rx, cimag(z))));
472 1.2.2.2 pgoyette }
473 1.2.2.2 pgoyette
474 1.2.2.2 pgoyette /*
475 1.2.2.2 pgoyette * Optimized version of clog() for |z| finite and larger than ~RECIP_EPSILON.
476 1.2.2.2 pgoyette */
477 1.2.2.2 pgoyette static double complex
478 1.2.2.2 pgoyette clog_for_large_values(double complex z)
479 1.2.2.2 pgoyette {
480 1.2.2.2 pgoyette double x, y;
481 1.2.2.2 pgoyette double ax, ay, t;
482 1.2.2.2 pgoyette
483 1.2.2.2 pgoyette x = creal(z);
484 1.2.2.2 pgoyette y = cimag(z);
485 1.2.2.2 pgoyette ax = fabs(x);
486 1.2.2.2 pgoyette ay = fabs(y);
487 1.2.2.2 pgoyette if (ax < ay) {
488 1.2.2.2 pgoyette t = ax;
489 1.2.2.2 pgoyette ax = ay;
490 1.2.2.2 pgoyette ay = t;
491 1.2.2.2 pgoyette }
492 1.2.2.2 pgoyette
493 1.2.2.2 pgoyette /*
494 1.2.2.2 pgoyette * Avoid overflow in hypot() when x and y are both very large.
495 1.2.2.2 pgoyette * Divide x and y by E, and then add 1 to the logarithm. This depends
496 1.2.2.2 pgoyette * on E being larger than sqrt(2).
497 1.2.2.2 pgoyette * Dividing by E causes an insignificant loss of accuracy; however
498 1.2.2.2 pgoyette * this method is still poor since it is uneccessarily slow.
499 1.2.2.2 pgoyette */
500 1.2.2.2 pgoyette if (ax > DBL_MAX / 2)
501 1.2.2.2 pgoyette return (CMPLX(log(hypot(x / m_e, y / m_e)) + 1, atan2(y, x)));
502 1.2.2.2 pgoyette
503 1.2.2.2 pgoyette /*
504 1.2.2.2 pgoyette * Avoid overflow when x or y is large. Avoid underflow when x or
505 1.2.2.2 pgoyette * y is small.
506 1.2.2.2 pgoyette */
507 1.2.2.2 pgoyette if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
508 1.2.2.2 pgoyette return (CMPLX(log(hypot(x, y)), atan2(y, x)));
509 1.2.2.2 pgoyette
510 1.2.2.2 pgoyette return (CMPLX(log(ax * ax + ay * ay) / 2, atan2(y, x)));
511 1.2.2.2 pgoyette }
512 1.2.2.2 pgoyette
513 1.2.2.2 pgoyette /*
514 1.2.2.2 pgoyette * =================
515 1.2.2.2 pgoyette * | catanh, catan |
516 1.2.2.2 pgoyette * =================
517 1.2.2.2 pgoyette */
518 1.2.2.2 pgoyette
519 1.2.2.2 pgoyette /*
520 1.2.2.2 pgoyette * sum_squares(x,y) = x*x + y*y (or just x*x if y*y would underflow).
521 1.2.2.2 pgoyette * Assumes x*x and y*y will not overflow.
522 1.2.2.2 pgoyette * Assumes x and y are finite.
523 1.2.2.2 pgoyette * Assumes y is non-negative.
524 1.2.2.2 pgoyette * Assumes fabs(x) >= DBL_EPSILON.
525 1.2.2.2 pgoyette */
526 1.2.2.2 pgoyette static inline double
527 1.2.2.2 pgoyette sum_squares(double x, double y)
528 1.2.2.2 pgoyette {
529 1.2.2.2 pgoyette
530 1.2.2.2 pgoyette /* Avoid underflow when y is small. */
531 1.2.2.2 pgoyette if (y < SQRT_MIN)
532 1.2.2.2 pgoyette return (x * x);
533 1.2.2.2 pgoyette
534 1.2.2.2 pgoyette return (x * x + y * y);
535 1.2.2.2 pgoyette }
536 1.2.2.2 pgoyette
537 1.2.2.2 pgoyette /*
538 1.2.2.2 pgoyette * real_part_reciprocal(x, y) = Re(1/(x+I*y)) = x/(x*x + y*y).
539 1.2.2.2 pgoyette * Assumes x and y are not NaN, and one of x and y is larger than
540 1.2.2.2 pgoyette * RECIP_EPSILON. We avoid unwarranted underflow. It is important to not use
541 1.2.2.2 pgoyette * the code creal(1/z), because the imaginary part may produce an unwanted
542 1.2.2.2 pgoyette * underflow.
543 1.2.2.2 pgoyette * This is only called in a context where inexact is always raised before
544 1.2.2.2 pgoyette * the call, so no effort is made to avoid or force inexact.
545 1.2.2.2 pgoyette */
546 1.2.2.2 pgoyette static inline double
547 1.2.2.2 pgoyette real_part_reciprocal(double x, double y)
548 1.2.2.2 pgoyette {
549 1.2.2.2 pgoyette double scale;
550 1.2.2.2 pgoyette uint32_t hx, hy;
551 1.2.2.2 pgoyette int32_t ix, iy;
552 1.2.2.2 pgoyette
553 1.2.2.2 pgoyette /*
554 1.2.2.2 pgoyette * This code is inspired by the C99 document n1124.pdf, Section G.5.1,
555 1.2.2.2 pgoyette * example 2.
556 1.2.2.2 pgoyette */
557 1.2.2.2 pgoyette GET_HIGH_WORD(hx, x);
558 1.2.2.2 pgoyette ix = hx & 0x7ff00000;
559 1.2.2.2 pgoyette GET_HIGH_WORD(hy, y);
560 1.2.2.2 pgoyette iy = hy & 0x7ff00000;
561 1.2.2.2 pgoyette #define BIAS (DBL_MAX_EXP - 1)
562 1.2.2.2 pgoyette /* XXX more guard digits are useful iff there is extra precision. */
563 1.2.2.2 pgoyette #define CUTOFF (DBL_MANT_DIG / 2 + 1) /* just half or 1 guard digit */
564 1.2.2.2 pgoyette if (ix - iy >= CUTOFF << 20 || isinf(x))
565 1.2.2.2 pgoyette return (1 / x); /* +-Inf -> +-0 is special */
566 1.2.2.2 pgoyette if (iy - ix >= CUTOFF << 20)
567 1.2.2.2 pgoyette return (x / y / y); /* should avoid double div, but hard */
568 1.2.2.2 pgoyette if (ix <= (BIAS + DBL_MAX_EXP / 2 - CUTOFF) << 20)
569 1.2.2.2 pgoyette return (x / (x * x + y * y));
570 1.2.2.2 pgoyette scale = 1;
571 1.2.2.2 pgoyette SET_HIGH_WORD(scale, 0x7ff00000 - ix); /* 2**(1-ilogb(x)) */
572 1.2.2.2 pgoyette x *= scale;
573 1.2.2.2 pgoyette y *= scale;
574 1.2.2.2 pgoyette return (x / (x * x + y * y) * scale);
575 1.2.2.2 pgoyette }
576 1.2.2.2 pgoyette
577 1.2.2.2 pgoyette /*
578 1.2.2.2 pgoyette * catanh(z) = log((1+z)/(1-z)) / 2
579 1.2.2.2 pgoyette * = log1p(4*x / |z-1|^2) / 4
580 1.2.2.2 pgoyette * + I * atan2(2*y, (1-x)*(1+x)-y*y) / 2
581 1.2.2.2 pgoyette *
582 1.2.2.2 pgoyette * catanh(z) = z + O(z^3) as z -> 0
583 1.2.2.2 pgoyette *
584 1.2.2.2 pgoyette * catanh(z) = 1/z + sign(y)*I*PI/2 + O(1/z^3) as z -> infinity
585 1.2.2.2 pgoyette * The above formula works for the real part as well, because
586 1.2.2.2 pgoyette * Re(catanh(z)) = x/|z|^2 + O(x/z^4)
587 1.2.2.2 pgoyette * as z -> infinity, uniformly in x
588 1.2.2.2 pgoyette */
589 1.2.2.2 pgoyette double complex
590 1.2.2.2 pgoyette catanh(double complex z)
591 1.2.2.2 pgoyette {
592 1.2.2.2 pgoyette double x, y, ax, ay, rx, ry;
593 1.2.2.2 pgoyette
594 1.2.2.2 pgoyette x = creal(z);
595 1.2.2.2 pgoyette y = cimag(z);
596 1.2.2.2 pgoyette ax = fabs(x);
597 1.2.2.2 pgoyette ay = fabs(y);
598 1.2.2.2 pgoyette
599 1.2.2.2 pgoyette /* This helps handle many cases. */
600 1.2.2.2 pgoyette if (y == 0 && ax <= 1)
601 1.2.2.2 pgoyette return (CMPLX(atanh(x), y));
602 1.2.2.2 pgoyette
603 1.2.2.2 pgoyette /* To ensure the same accuracy as atan(), and to filter out z = 0. */
604 1.2.2.2 pgoyette if (x == 0)
605 1.2.2.2 pgoyette return (CMPLX(x, atan(y)));
606 1.2.2.2 pgoyette
607 1.2.2.2 pgoyette if (isnan(x) || isnan(y)) {
608 1.2.2.2 pgoyette /* catanh(+-Inf + I*NaN) = +-0 + I*NaN */
609 1.2.2.2 pgoyette if (isinf(x))
610 1.2.2.2 pgoyette return (CMPLX(copysign(0, x), y + y));
611 1.2.2.2 pgoyette /* catanh(NaN + I*+-Inf) = sign(NaN)0 + I*+-PI/2 */
612 1.2.2.2 pgoyette if (isinf(y))
613 1.2.2.2 pgoyette return (CMPLX(copysign(0, x),
614 1.2.2.2 pgoyette copysign(pio2_hi + pio2_lo, y)));
615 1.2.2.2 pgoyette /*
616 1.2.2.2 pgoyette * All other cases involving NaN return NaN + I*NaN.
617 1.2.2.2 pgoyette * C99 leaves it optional whether to raise invalid if one of
618 1.2.2.2 pgoyette * the arguments is not NaN, so we opt not to raise it.
619 1.2.2.2 pgoyette */
620 1.2.2.2 pgoyette return (CMPLX(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
621 1.2.2.2 pgoyette }
622 1.2.2.2 pgoyette
623 1.2.2.2 pgoyette if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
624 1.2.2.2 pgoyette return (CMPLX(real_part_reciprocal(x, y),
625 1.2.2.2 pgoyette copysign(pio2_hi + pio2_lo, y)));
626 1.2.2.2 pgoyette
627 1.2.2.2 pgoyette if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
628 1.2.2.2 pgoyette /*
629 1.2.2.2 pgoyette * z = 0 was filtered out above. All other cases must raise
630 1.2.2.2 pgoyette * inexact, but this is the only only that needs to do it
631 1.2.2.2 pgoyette * explicitly.
632 1.2.2.2 pgoyette */
633 1.2.2.2 pgoyette raise_inexact();
634 1.2.2.2 pgoyette return (z);
635 1.2.2.2 pgoyette }
636 1.2.2.2 pgoyette
637 1.2.2.2 pgoyette if (ax == 1 && ay < DBL_EPSILON)
638 1.2.2.2 pgoyette rx = (m_ln2 - log(ay)) / 2;
639 1.2.2.2 pgoyette else
640 1.2.2.2 pgoyette rx = log1p(4 * ax / sum_squares(ax - 1, ay)) / 4;
641 1.2.2.2 pgoyette
642 1.2.2.2 pgoyette if (ax == 1)
643 1.2.2.2 pgoyette ry = atan2(2, -ay) / 2;
644 1.2.2.2 pgoyette else if (ay < DBL_EPSILON)
645 1.2.2.2 pgoyette ry = atan2(2 * ay, (1 - ax) * (1 + ax)) / 2;
646 1.2.2.2 pgoyette else
647 1.2.2.2 pgoyette ry = atan2(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
648 1.2.2.2 pgoyette
649 1.2.2.2 pgoyette return (CMPLX(copysign(rx, x), copysign(ry, y)));
650 1.2.2.2 pgoyette }
651 1.2.2.2 pgoyette
652 1.2.2.2 pgoyette /*
653 1.2.2.2 pgoyette * catan(z) = reverse(catanh(reverse(z)))
654 1.2.2.2 pgoyette * where reverse(x + I*y) = y + I*x = I*conj(z).
655 1.2.2.2 pgoyette */
656 1.2.2.2 pgoyette double complex
657 1.2.2.2 pgoyette catan(double complex z)
658 1.2.2.2 pgoyette {
659 1.2.2.2 pgoyette double complex w = catanh(CMPLX(cimag(z), creal(z)));
660 1.2.2.2 pgoyette
661 1.2.2.2 pgoyette return (CMPLX(cimag(w), creal(w)));
662 1.2.2.2 pgoyette }
663