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