2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
36 #include "math_private.h"
39 #define isinf(x) (fabs(x) == INFINITY)
41 #define isnan(x) ((x) != (x))
42 #define raise_inexact() do { volatile float junk __unused = 1 + tiny; } while(0)
44 #define signbit(x) (__builtin_signbit(x))
46 /* We need that DBL_EPSILON^2/128 is larger than FOUR_SQRT_MIN. */
48 A_crossover = 10, /* Hull et al suggest 1.5, but 10 works better */
49 B_crossover = 0.6417, /* suggested by Hull et al */
50 FOUR_SQRT_MIN = 0x1p-509, /* >= 4 * sqrt(DBL_MIN) */
51 QUARTER_SQRT_MAX = 0x1p509, /* <= sqrt(DBL_MAX) / 4 */
52 m_e = 2.7182818284590452e0, /* 0x15bf0a8b145769.0p-51 */
53 m_ln2 = 6.9314718055994531e-1, /* 0x162e42fefa39ef.0p-53 */
54 pio2_hi = 1.5707963267948966e0, /* 0x1921fb54442d18.0p-52 */
55 RECIP_EPSILON = 1 / DBL_EPSILON,
56 SQRT_3_EPSILON = 2.5809568279517849e-8, /* 0x1bb67ae8584caa.0p-78 */
57 SQRT_6_EPSILON = 3.6500241499888571e-8, /* 0x13988e1409212e.0p-77 */
58 SQRT_MIN = 0x1p-511; /* >= sqrt(DBL_MIN) */
60 static const volatile double
61 pio2_lo = 6.1232339957367659e-17; /* 0x11a62633145c07.0p-106 */
62 static const volatile float
65 static double complex clog_for_large_values(double complex z);
68 * Testing indicates that all these functions are accurate up to 4 ULP.
69 * The functions casin(h) and cacos(h) are about 2.5 times slower than asinh.
70 * The functions catan(h) are a little under 2 times slower than atanh.
72 * The code for casinh, casin, cacos, and cacosh comes first. The code is
73 * rather complicated, and the four functions are highly interdependent.
75 * The code for catanh and catan comes at the end. It is much simpler than
76 * the other functions, and the code for these can be disconnected from the
81 * ================================
82 * | casinh, casin, cacos, cacosh |
83 * ================================
87 * The algorithm is very close to that in "Implementing the complex arcsine
88 * and arccosine functions using exception handling" by T. E. Hull, Thomas F.
89 * Fairgrieve, and Ping Tak Peter Tang, published in ACM Transactions on
90 * Mathematical Software, Volume 23 Issue 3, 1997, Pages 299-335,
91 * http://dl.acm.org/citation.cfm?id=275324.
93 * Throughout we use the convention z = x + I*y.
95 * casinh(z) = sign(x)*log(A+sqrt(A*A-1)) + I*asin(B)
97 * A = (|z+I| + |z-I|) / 2
98 * B = (|z+I| - |z-I|) / 2 = y/A
100 * These formulas become numerically unstable:
101 * (a) for Re(casinh(z)) when z is close to the line segment [-I, I] (that
102 * is, Re(casinh(z)) is close to 0);
103 * (b) for Im(casinh(z)) when z is close to either of the intervals
104 * [I, I*infinity) or (-I*infinity, -I] (that is, |Im(casinh(z))| is
107 * These numerical problems are overcome by defining
108 * f(a, b) = (hypot(a, b) - b) / 2 = a*a / (hypot(a, b) + b) / 2
109 * Then if A < A_crossover, we use
110 * log(A + sqrt(A*A-1)) = log1p((A-1) + sqrt((A-1)*(A+1)))
111 * A-1 = f(x, 1+y) + f(x, 1-y)
112 * and if B > B_crossover, we use
113 * asin(B) = atan2(y, sqrt(A*A - y*y)) = atan2(y, sqrt((A+y)*(A-y)))
114 * A-y = f(x, y+1) + f(x, y-1)
115 * where without loss of generality we have assumed that x and y are
118 * Much of the difficulty comes because the intermediate computations may
119 * produce overflows or underflows. This is dealt with in the paper by Hull
120 * et al by using exception handling. We do this by detecting when
121 * computations risk underflow or overflow. The hardest part is handling the
122 * underflows when computing f(a, b).
124 * Note that the function f(a, b) does not appear explicitly in the paper by
125 * Hull et al, but the idea may be found on pages 308 and 309. Introducing the
126 * function f(a, b) allows us to concentrate many of the clever tricks in this
127 * paper into one function.
131 * Function f(a, b, hypot_a_b) = (hypot(a, b) - b) / 2.
132 * Pass hypot(a, b) as the third argument.
135 f(double a, double b, double hypot_a_b)
138 return ((hypot_a_b - b) / 2);
141 return (a * a / (hypot_a_b + b) / 2);
145 * All the hard work is contained in this function.
146 * x and y are assumed positive or zero, and less than RECIP_EPSILON.
148 * rx = Re(casinh(z)) = -Im(cacos(y + I*x)).
149 * B_is_usable is set to 1 if the value of B is usable.
150 * If B_is_usable is set to 0, sqrt_A2my2 = sqrt(A*A - y*y), and new_y = y.
151 * If returning sqrt_A2my2 has potential to result in an underflow, it is
152 * rescaled, and new_y is similarly rescaled.
155 do_hard_work(double x, double y, double *rx, int *B_is_usable, double *B,
156 double *sqrt_A2my2, double *new_y)
158 double R, S, A; /* A, B, R, and S are as in Hull et al. */
159 double Am1, Amy; /* A-1, A-y. */
161 R = hypot(x, y + 1); /* |z+I| */
162 S = hypot(x, y - 1); /* |z-I| */
164 /* A = (|z+I| + |z-I|) / 2 */
167 * Mathematically A >= 1. There is a small chance that this will not
168 * be so because of rounding errors. So we will make certain it is
174 if (A < A_crossover) {
176 * Am1 = fp + fm, where fp = f(x, 1+y), and fm = f(x, 1-y).
177 * rx = log1p(Am1 + sqrt(Am1*(A+1)))
179 if (y == 1 && x < DBL_EPSILON * DBL_EPSILON / 128) {
181 * fp is of order x^2, and fm = x/2.
185 } else if (x >= DBL_EPSILON * fabs(y - 1)) {
187 * Underflow will not occur because
188 * x >= DBL_EPSILON^2/128 >= FOUR_SQRT_MIN
190 Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
191 *rx = log1p(Am1 + sqrt(Am1 * (A + 1)));
194 * fp = x*x/(1+y)/4, fm = x*x/(1-y)/4, and
197 *rx = x / sqrt((1 - y) * (1 + y));
198 } else { /* if (y > 1) */
200 * A-1 = y-1 (inexactly).
202 *rx = log1p((y - 1) + sqrt((y - 1) * (y + 1)));
205 *rx = log(A + sqrt(A * A - 1));
210 if (y < FOUR_SQRT_MIN) {
212 * Avoid a possible underflow caused by y/A. For casinh this
213 * would be legitimate, but will be picked up by invoking atan2
214 * later on. For cacos this would not be legitimate.
217 *sqrt_A2my2 = A * (2 / DBL_EPSILON);
218 *new_y = y * (2 / DBL_EPSILON);
222 /* B = (|z+I| - |z-I|) / 2 = y/A */
226 if (*B > B_crossover) {
229 * Amy = fp + fm, where fp = f(x, y+1), and fm = f(x, y-1).
230 * sqrt_A2my2 = sqrt(Amy*(A+y))
232 if (y == 1 && x < DBL_EPSILON / 128) {
234 * fp is of order x^2, and fm = x/2.
237 *sqrt_A2my2 = sqrt(x) * sqrt((A + y) / 2);
238 } else if (x >= DBL_EPSILON * fabs(y - 1)) {
240 * Underflow will not occur because
241 * x >= DBL_EPSILON/128 >= FOUR_SQRT_MIN
243 * x >= DBL_EPSILON^2 >= FOUR_SQRT_MIN
245 Amy = f(x, y + 1, R) + f(x, y - 1, S);
246 *sqrt_A2my2 = sqrt(Amy * (A + y));
249 * fp = x*x/(y+1)/4, fm = x*x/(y-1)/4, and
252 * y < RECIP_EPSILON. So the following
253 * scaling should avoid any underflow problems.
255 *sqrt_A2my2 = x * (4 / DBL_EPSILON / DBL_EPSILON) * y /
256 sqrt((y + 1) * (y - 1));
257 *new_y = y * (4 / DBL_EPSILON / DBL_EPSILON);
258 } else { /* if (y < 1) */
260 * fm = 1-y >= DBL_EPSILON, fp is of order x^2, and
263 *sqrt_A2my2 = sqrt((1 - y) * (1 + y));
269 * casinh(z) = z + O(z^3) as z -> 0
271 * casinh(z) = sign(x)*clog(sign(x)*z) + O(1/z^2) as z -> infinity
272 * The above formula works for the imaginary part as well, because
273 * Im(casinh(z)) = sign(x)*atan2(sign(x)*y, fabs(x)) + O(y/z^3)
274 * as z -> infinity, uniformly in y
277 casinh(double complex z)
279 double x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
288 if (isnan(x) || isnan(y)) {
289 /* casinh(+-Inf + I*NaN) = +-Inf + I*NaN */
291 return (CMPLX(x, y + y));
292 /* casinh(NaN + I*+-Inf) = opt(+-)Inf + I*NaN */
294 return (CMPLX(y, x + x));
295 /* casinh(NaN + I*0) = NaN + I*0 */
297 return (CMPLX(x + x, y));
299 * All other cases involving NaN return NaN + I*NaN.
300 * C99 leaves it optional whether to raise invalid if one of
301 * the arguments is not NaN, so we opt not to raise it.
303 return (CMPLX(nan_mix(x, y), nan_mix(x, y)));
306 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
307 /* clog...() will raise inexact unless x or y is infinite. */
309 w = clog_for_large_values(z) + m_ln2;
311 w = clog_for_large_values(-z) + m_ln2;
312 return (CMPLX(copysign(creal(w), x), copysign(cimag(w), y)));
315 /* Avoid spuriously raising inexact for z = 0. */
316 if (x == 0 && y == 0)
319 /* All remaining cases are inexact. */
322 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
325 do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
329 ry = atan2(new_y, sqrt_A2my2);
330 return (CMPLX(copysign(rx, x), copysign(ry, y)));
334 * casin(z) = reverse(casinh(reverse(z)))
335 * where reverse(x + I*y) = y + I*x = I*conj(z).
338 casin(double complex z)
340 double complex w = casinh(CMPLX(cimag(z), creal(z)));
342 return (CMPLX(cimag(w), creal(w)));
346 * cacos(z) = PI/2 - casin(z)
347 * but do the computation carefully so cacos(z) is accurate when z is
350 * cacos(z) = PI/2 - z + O(z^3) as z -> 0
352 * cacos(z) = -sign(y)*I*clog(z) + O(1/z^2) as z -> infinity
353 * The above formula works for the real part as well, because
354 * Re(cacos(z)) = atan2(fabs(y), x) + O(y/z^3)
355 * as z -> infinity, uniformly in y
358 cacos(double complex z)
360 double x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
372 if (isnan(x) || isnan(y)) {
373 /* cacos(+-Inf + I*NaN) = NaN + I*opt(-)Inf */
375 return (CMPLX(y + y, -INFINITY));
376 /* cacos(NaN + I*+-Inf) = NaN + I*-+Inf */
378 return (CMPLX(x + x, -y));
379 /* cacos(0 + I*NaN) = PI/2 + I*NaN with inexact */
381 return (CMPLX(pio2_hi + pio2_lo, y + y));
383 * All other cases involving NaN return NaN + I*NaN.
384 * C99 leaves it optional whether to raise invalid if one of
385 * the arguments is not NaN, so we opt not to raise it.
387 return (CMPLX(nan_mix(x, y), nan_mix(x, y)));
390 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
391 /* clog...() will raise inexact unless x or y is infinite. */
392 w = clog_for_large_values(z);
394 ry = creal(w) + m_ln2;
397 return (CMPLX(rx, ry));
400 /* Avoid spuriously raising inexact for z = 1. */
401 if (x == 1 && y == 0)
402 return (CMPLX(0, -y));
404 /* All remaining cases are inexact. */
407 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
408 return (CMPLX(pio2_hi - (x - pio2_lo), -y));
410 do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
418 rx = atan2(sqrt_A2mx2, new_x);
420 rx = atan2(sqrt_A2mx2, -new_x);
424 return (CMPLX(rx, ry));
428 * cacosh(z) = I*cacos(z) or -I*cacos(z)
429 * where the sign is chosen so Re(cacosh(z)) >= 0.
432 cacosh(double complex z)
440 /* cacosh(NaN + I*NaN) = NaN + I*NaN */
441 if (isnan(rx) && isnan(ry))
442 return (CMPLX(ry, rx));
443 /* cacosh(NaN + I*+-Inf) = +Inf + I*NaN */
444 /* cacosh(+-Inf + I*NaN) = +Inf + I*NaN */
446 return (CMPLX(fabs(ry), rx));
447 /* cacosh(0 + I*NaN) = NaN + I*NaN */
449 return (CMPLX(ry, ry));
450 return (CMPLX(fabs(ry), copysign(rx, cimag(z))));
454 * Optimized version of clog() for |z| finite and larger than ~RECIP_EPSILON.
456 static double complex
457 clog_for_large_values(double complex z)
473 * Avoid overflow in hypot() when x and y are both very large.
474 * Divide x and y by E, and then add 1 to the logarithm. This
475 * depends on E being larger than sqrt(2), since the return value of
476 * hypot cannot overflow if neither argument is greater in magnitude
477 * than 1/sqrt(2) of the maximum value of the return type. Likewise
478 * this determines the necessary threshold for using this method
479 * (however, actually use 1/2 instead as it is simpler).
481 * Dividing by E causes an insignificant loss of accuracy; however
482 * this method is still poor since it is uneccessarily slow.
484 if (ax > DBL_MAX / 2)
485 return (CMPLX(log(hypot(x / m_e, y / m_e)) + 1, atan2(y, x)));
488 * Avoid overflow when x or y is large. Avoid underflow when x or
491 if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
492 return (CMPLX(log(hypot(x, y)), atan2(y, x)));
494 return (CMPLX(log(ax * ax + ay * ay) / 2, atan2(y, x)));
504 * sum_squares(x,y) = x*x + y*y (or just x*x if y*y would underflow).
505 * Assumes x*x and y*y will not overflow.
506 * Assumes x and y are finite.
507 * Assumes y is non-negative.
508 * Assumes fabs(x) >= DBL_EPSILON.
511 sum_squares(double x, double y)
514 /* Avoid underflow when y is small. */
518 return (x * x + y * y);
522 * real_part_reciprocal(x, y) = Re(1/(x+I*y)) = x/(x*x + y*y).
523 * Assumes x and y are not NaN, and one of x and y is larger than
524 * RECIP_EPSILON. We avoid unwarranted underflow. It is important to not use
525 * the code creal(1/z), because the imaginary part may produce an unwanted
527 * This is only called in a context where inexact is always raised before
528 * the call, so no effort is made to avoid or force inexact.
531 real_part_reciprocal(double x, double y)
538 * This code is inspired by the C99 document n1124.pdf, Section G.5.1,
541 GET_HIGH_WORD(hx, x);
542 ix = hx & 0x7ff00000;
543 GET_HIGH_WORD(hy, y);
544 iy = hy & 0x7ff00000;
545 #define BIAS (DBL_MAX_EXP - 1)
546 /* XXX more guard digits are useful iff there is extra precision. */
547 #define CUTOFF (DBL_MANT_DIG / 2 + 1) /* just half or 1 guard digit */
548 if (ix - iy >= CUTOFF << 20 || isinf(x))
549 return (1 / x); /* +-Inf -> +-0 is special */
550 if (iy - ix >= CUTOFF << 20)
551 return (x / y / y); /* should avoid double div, but hard */
552 if (ix <= (BIAS + DBL_MAX_EXP / 2 - CUTOFF) << 20)
553 return (x / (x * x + y * y));
555 SET_HIGH_WORD(scale, 0x7ff00000 - ix); /* 2**(1-ilogb(x)) */
558 return (x / (x * x + y * y) * scale);
562 * catanh(z) = log((1+z)/(1-z)) / 2
563 * = log1p(4*x / |z-1|^2) / 4
564 * + I * atan2(2*y, (1-x)*(1+x)-y*y) / 2
566 * catanh(z) = z + O(z^3) as z -> 0
568 * catanh(z) = 1/z + sign(y)*I*PI/2 + O(1/z^3) as z -> infinity
569 * The above formula works for the real part as well, because
570 * Re(catanh(z)) = x/|z|^2 + O(x/z^4)
571 * as z -> infinity, uniformly in x
574 catanh(double complex z)
576 double x, y, ax, ay, rx, ry;
583 /* This helps handle many cases. */
584 if (y == 0 && ax <= 1)
585 return (CMPLX(atanh(x), y));
587 /* To ensure the same accuracy as atan(), and to filter out z = 0. */
589 return (CMPLX(x, atan(y)));
591 if (isnan(x) || isnan(y)) {
592 /* catanh(+-Inf + I*NaN) = +-0 + I*NaN */
594 return (CMPLX(copysign(0, x), y + y));
595 /* catanh(NaN + I*+-Inf) = sign(NaN)0 + I*+-PI/2 */
597 return (CMPLX(copysign(0, x),
598 copysign(pio2_hi + pio2_lo, y)));
600 * All other cases involving NaN return NaN + I*NaN.
601 * C99 leaves it optional whether to raise invalid if one of
602 * the arguments is not NaN, so we opt not to raise it.
604 return (CMPLX(nan_mix(x, y), nan_mix(x, y)));
607 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON)
608 return (CMPLX(real_part_reciprocal(x, y),
609 copysign(pio2_hi + pio2_lo, y)));
611 if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
613 * z = 0 was filtered out above. All other cases must raise
614 * inexact, but this is the only case that needs to do it
621 if (ax == 1 && ay < DBL_EPSILON)
622 rx = (m_ln2 - log(ay)) / 2;
624 rx = log1p(4 * ax / sum_squares(ax - 1, ay)) / 4;
627 ry = atan2(2, -ay) / 2;
628 else if (ay < DBL_EPSILON)
629 ry = atan2(2 * ay, (1 - ax) * (1 + ax)) / 2;
631 ry = atan2(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
633 return (CMPLX(copysign(rx, x), copysign(ry, y)));
637 * catan(z) = reverse(catanh(reverse(z)))
638 * where reverse(x + I*y) = y + I*x = I*conj(z).
641 catan(double complex z)
643 double complex w = catanh(CMPLX(cimag(z), creal(z)));
645 return (CMPLX(cimag(w), creal(w)));
648 #if LDBL_MANT_DIG == 53
649 __weak_reference(cacosh, cacoshl);
650 __weak_reference(cacos, cacosl);
651 __weak_reference(casinh, casinhl);
652 __weak_reference(casin, casinl);
653 __weak_reference(catanh, catanhl);
654 __weak_reference(catan, catanl);