2 * Copyright (c) 2012 Stephen Montgomery-Smith <stephen@FreeBSD.ORG>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: head/lib/msun/src/catrig.c 251121 2013-05-30 04:49:26Z das $
33 #include "math_private.h"
36 #define isinf(x) (fabs(x) == INFINITY)
38 #define isnan(x) ((x) != (x))
39 #define raise_inexact() do { volatile float junk = 1 + tiny; } while(0)
41 #define signbit(x) (__builtin_signbit(x))
43 /* We need that DBL_EPSILON^2/128 is larger than FOUR_SQRT_MIN. */
45 A_crossover = 10, /* Hull et al suggest 1.5, but 10 works better */
46 B_crossover = 0.6417, /* suggested by Hull et al */
47 FOUR_SQRT_MIN = 0x1p-509, /* >= 4 * sqrt(DBL_MIN) */
48 QUARTER_SQRT_MAX = 0x1p509, /* <= sqrt(DBL_MAX) / 4 */
49 m_e = 2.7182818284590452e0, /* 0x15bf0a8b145769.0p-51 */
50 m_ln2 = 6.9314718055994531e-1, /* 0x162e42fefa39ef.0p-53 */
51 pio2_hi = 1.5707963267948966e0, /* 0x1921fb54442d18.0p-52 */
52 RECIP_EPSILON = 1 / DBL_EPSILON,
53 SQRT_3_EPSILON = 2.5809568279517849e-8, /* 0x1bb67ae8584caa.0p-78 */
54 SQRT_6_EPSILON = 3.6500241499888571e-8, /* 0x13988e1409212e.0p-77 */
55 SQRT_MIN = 0x1p-511; /* >= sqrt(DBL_MIN) */
57 static const volatile double
58 pio2_lo = 6.1232339957367659e-17; /* 0x11a62633145c07.0p-106 */
59 static const volatile float
62 static double complex clog_for_large_values(double complex z);
65 * Testing indicates that all these functions are accurate up to 4 ULP.
66 * The functions casin(h) and cacos(h) are about 2.5 times slower than asinh.
67 * The functions catan(h) are a little under 2 times slower than atanh.
69 * The code for casinh, casin, cacos, and cacosh comes first. The code is
70 * rather complicated, and the four functions are highly interdependent.
72 * The code for catanh and catan comes at the end. It is much simpler than
73 * the other functions, and the code for these can be disconnected from the
78 * ================================
79 * | casinh, casin, cacos, cacosh |
80 * ================================
84 * The algorithm is very close to that in "Implementing the complex arcsine
85 * and arccosine functions using exception handling" by T. E. Hull, Thomas F.
86 * Fairgrieve, and Ping Tak Peter Tang, published in ACM Transactions on
87 * Mathematical Software, Volume 23 Issue 3, 1997, Pages 299-335,
88 * http://dl.acm.org/citation.cfm?id=275324.
90 * Throughout we use the convention z = x + I*y.
92 * casinh(z) = sign(x)*log(A+sqrt(A*A-1)) + I*asin(B)
94 * A = (|z+I| + |z-I|) / 2
95 * B = (|z+I| - |z-I|) / 2 = y/A
97 * These formulas become numerically unstable:
98 * (a) for Re(casinh(z)) when z is close to the line segment [-I, I] (that
99 * is, Re(casinh(z)) is close to 0);
100 * (b) for Im(casinh(z)) when z is close to either of the intervals
101 * [I, I*infinity) or (-I*infinity, -I] (that is, |Im(casinh(z))| is
104 * These numerical problems are overcome by defining
105 * f(a, b) = (hypot(a, b) - b) / 2 = a*a / (hypot(a, b) + b) / 2
106 * Then if A < A_crossover, we use
107 * log(A + sqrt(A*A-1)) = log1p((A-1) + sqrt((A-1)*(A+1)))
108 * A-1 = f(x, 1+y) + f(x, 1-y)
109 * and if B > B_crossover, we use
110 * asin(B) = atan2(y, sqrt(A*A - y*y)) = atan2(y, sqrt((A+y)*(A-y)))
111 * A-y = f(x, y+1) + f(x, y-1)
112 * where without loss of generality we have assumed that x and y are
115 * Much of the difficulty comes because the intermediate computations may
116 * produce overflows or underflows. This is dealt with in the paper by Hull
117 * et al by using exception handling. We do this by detecting when
118 * computations risk underflow or overflow. The hardest part is handling the
119 * underflows when computing f(a, b).
121 * Note that the function f(a, b) does not appear explicitly in the paper by
122 * Hull et al, but the idea may be found on pages 308 and 309. Introducing the
123 * function f(a, b) allows us to concentrate many of the clever tricks in this
124 * paper into one function.
128 * Function f(a, b, hypot_a_b) = (hypot(a, b) - b) / 2.
129 * Pass hypot(a, b) as the third argument.
132 f(double a, double b, double hypot_a_b)
135 return ((hypot_a_b - b) / 2);
138 return (a * a / (hypot_a_b + b) / 2);
142 * All the hard work is contained in this function.
143 * x and y are assumed positive or zero, and less than RECIP_EPSILON.
145 * rx = Re(casinh(z)) = -Im(cacos(y + I*x)).
146 * B_is_usable is set to 1 if the value of B is usable.
147 * If B_is_usable is set to 0, sqrt_A2my2 = sqrt(A*A - y*y), and new_y = y.
148 * If returning sqrt_A2my2 has potential to result in an underflow, it is
149 * rescaled, and new_y is similarly rescaled.
152 do_hard_work(double x, double y, double *rx, int *B_is_usable, double *B,
153 double *sqrt_A2my2, double *new_y)
155 double R, S, A; /* A, B, R, and S are as in Hull et al. */
156 double Am1, Amy; /* A-1, A-y. */
158 R = hypot(x, y + 1); /* |z+I| */
159 S = hypot(x, y - 1); /* |z-I| */
161 /* A = (|z+I| + |z-I|) / 2 */
164 * Mathematically A >= 1. There is a small chance that this will not
165 * be so because of rounding errors. So we will make certain it is
171 if (A < A_crossover) {
173 * Am1 = fp + fm, where fp = f(x, 1+y), and fm = f(x, 1-y).
174 * rx = log1p(Am1 + sqrt(Am1*(A+1)))
176 if (y == 1 && x < DBL_EPSILON*DBL_EPSILON / 128) {
178 * fp is of order x^2, and fm = x/2.
182 } else if (x >= DBL_EPSILON * fabs(y - 1)) {
184 * Underflow will not occur because
185 * x >= DBL_EPSILON^2/128 >= FOUR_SQRT_MIN
187 Am1 = f(x, 1 + y, R) + f(x, 1 - y, S);
188 *rx = log1p(Am1 + sqrt(Am1 * (A + 1)));
191 * fp = x*x/(1+y)/4, fm = x*x/(1-y)/4, and
194 *rx = x / sqrt((1 - y) * (1 + y));
195 } else /* if (y > 1) */ {
197 * A-1 = y-1 (inexactly).
199 *rx = log1p((y - 1) + sqrt((y - 1) * (y + 1)));
202 *rx = log(A + sqrt(A * A - 1));
207 if (y < FOUR_SQRT_MIN) {
209 * Avoid a possible underflow caused by y/A. For casinh this
210 * would be legitimate, but will be picked up by invoking atan2
211 * later on. For cacos this would not be legitimate.
214 *sqrt_A2my2 = A * (2 / DBL_EPSILON);
215 *new_y = y * (2 / DBL_EPSILON);
219 /* B = (|z+I| - |z-I|) / 2 = y/A */
223 if (*B > B_crossover) {
226 * Amy = fp + fm, where fp = f(x, y+1), and fm = f(x, y-1).
227 * sqrt_A2my2 = sqrt(Amy*(A+y))
229 if (y == 1 && x < DBL_EPSILON / 128) {
231 * fp is of order x^2, and fm = x/2.
234 *sqrt_A2my2 = sqrt(x) * sqrt((A + y) / 2);
235 } else if (x >= DBL_EPSILON * fabs(y - 1)) {
237 * Underflow will not occur because
238 * x >= DBL_EPSILON/128 >= FOUR_SQRT_MIN
240 * x >= DBL_EPSILON^2 >= FOUR_SQRT_MIN
242 Amy = f(x, y + 1, R) + f(x, y - 1, S);
243 *sqrt_A2my2 = sqrt(Amy * (A + y));
246 * fp = x*x/(y+1)/4, fm = x*x/(y-1)/4, and
249 * y < RECIP_EPSILON. So the following
250 * scaling should avoid any underflow problems.
252 *sqrt_A2my2 = x * (4 / DBL_EPSILON / DBL_EPSILON) * y /
253 sqrt((y + 1) * (y - 1));
254 *new_y = y * (4 / DBL_EPSILON / DBL_EPSILON);
255 } else /* if (y < 1) */ {
257 * fm = 1-y >= DBL_EPSILON, fp is of order x^2, and
260 *sqrt_A2my2 = sqrt((1 - y) * (1 + y));
266 * casinh(z) = z + O(z^3) as z -> 0
268 * casinh(z) = sign(x)*clog(sign(x)*z) + O(1/z^2) as z -> infinity
269 * The above formula works for the imaginary part as well, because
270 * Im(casinh(z)) = sign(x)*atan2(sign(x)*y, fabs(x)) + O(y/z^3)
271 * as z -> infinity, uniformly in y
274 casinh(double complex z)
276 double x, y, ax, ay, rx, ry, B, sqrt_A2my2, new_y;
285 if (isnan(x) || isnan(y)) {
286 /* casinh(+-Inf + I*NaN) = +-Inf + I*NaN */
288 return (cpack(x, y + y));
289 /* casinh(NaN + I*+-Inf) = opt(+-)Inf + I*NaN */
291 return (cpack(y, x + x));
292 /* casinh(NaN + I*0) = NaN + I*0 */
294 return (cpack(x + x, y));
296 * All other cases involving NaN return NaN + I*NaN.
297 * C99 leaves it optional whether to raise invalid if one of
298 * the arguments is not NaN, so we opt not to raise it.
300 /* Bruce Evans tells me this is the way to do this: */
301 return (cpack(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
304 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
305 /* clog...() will raise inexact unless x or y is infinite. */
307 w = clog_for_large_values(z) + m_ln2;
309 w = clog_for_large_values(-z) + m_ln2;
310 return (cpack(copysign(creal(w), x), copysign(cimag(w), y)));
313 /* Avoid spuriously raising inexact for z = 0. */
314 if (x == 0 && y == 0)
317 /* All remaining cases are inexact. */
320 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON / 4)
323 do_hard_work(ax, ay, &rx, &B_is_usable, &B, &sqrt_A2my2, &new_y);
327 ry = atan2(new_y, sqrt_A2my2);
328 return (cpack(copysign(rx, x), copysign(ry, y)));
332 * casin(z) = reverse(casinh(reverse(z)))
333 * where reverse(x + I*y) = y + I*x = I*conj(z).
336 casin(double complex z)
338 double complex w = casinh(cpack(cimag(z), creal(z)));
339 return (cpack(cimag(w), creal(w)));
343 * cacos(z) = PI/2 - casin(z)
344 * but do the computation carefully so cacos(z) is accurate when z is
347 * cacos(z) = PI/2 - z + O(z^3) as z -> 0
349 * cacos(z) = -sign(y)*I*clog(z) + O(1/z^2) as z -> infinity
350 * The above formula works for the real part as well, because
351 * Re(cacos(z)) = atan2(fabs(y), x) + O(y/z^3)
352 * as z -> infinity, uniformly in y
355 cacos(double complex z)
357 double x, y, ax, ay, rx, ry, B, sqrt_A2mx2, new_x;
369 if (isnan(x) || isnan(y)) {
370 /* cacos(+-Inf + I*NaN) = NaN + I*opt(-)Inf */
372 return (cpack(y + y, -INFINITY));
373 /* cacos(NaN + I*+-Inf) = NaN + I*-+Inf */
375 return (cpack(x + x, -y));
376 /* cacos(0 + I*NaN) = PI/2 + I*NaN with inexact */
378 return (cpack(pio2_hi + pio2_lo, y + y));
380 * All other cases involving NaN return NaN + I*NaN.
381 * C99 leaves it optional whether to raise invalid if one of
382 * the arguments is not NaN, so we opt not to raise it.
384 return (cpack(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
387 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
388 /* clog...() will raise inexact unless x or y is infinite. */
389 w = clog_for_large_values(z);
391 ry = creal(w) + m_ln2;
394 return (cpack(rx, ry));
397 /* Avoid spuriously raising inexact for z = 1. */
398 if (x == 1 && y == 0)
399 return (cpack(0, -y));
401 /* All remaining cases are inexact. */
404 if (ax < SQRT_6_EPSILON / 4 && ay < SQRT_6_EPSILON/4)
405 return (cpack(pio2_hi - (x - pio2_lo), -y));
407 do_hard_work(ay, ax, &ry, &B_is_usable, &B, &sqrt_A2mx2, &new_x);
415 rx = atan2(sqrt_A2mx2, new_x);
417 rx = atan2(sqrt_A2mx2, -new_x);
421 return (cpack(rx, ry));
425 * cacosh(z) = I*cacos(z) or -I*cacos(z)
426 * where the sign is chosen so Re(cacosh(z)) >= 0.
429 cacosh(double complex z)
437 /* cacosh(NaN + I*NaN) = NaN + I*NaN */
438 if (isnan(rx) && isnan(ry))
439 return (cpack(ry, rx));
440 /* cacosh(NaN + I*+-Inf) = +Inf + I*NaN */
441 /* cacosh(+-Inf + I*NaN) = +Inf + I*NaN */
443 return (cpack(fabs(ry), rx));
444 /* cacosh(0 + I*NaN) = NaN + I*NaN */
446 return (cpack(ry, ry));
447 return (cpack(fabs(ry), copysign(rx, cimag(z))));
451 * Optimized version of clog() for |z| finite and larger than ~RECIP_EPSILON.
453 static double complex
454 clog_for_large_values(double complex z)
470 * Avoid overflow in hypot() when x and y are both very large.
471 * Divide x and y by E, and then add 1 to the logarithm. This depends
472 * on E being larger than sqrt(2).
473 * Dividing by E causes an insignificant loss of accuracy; however
474 * this method is still poor since it is uneccessarily slow.
476 if (ax > DBL_MAX / 2)
477 return (cpack(log(hypot(x / m_e, y / m_e)) + 1, atan2(y, x)));
480 * Avoid overflow when x or y is large. Avoid underflow when x or
483 if (ax > QUARTER_SQRT_MAX || ay < SQRT_MIN)
484 return (cpack(log(hypot(x, y)), atan2(y, x)));
486 return (cpack(log(ax * ax + ay * ay) / 2, atan2(y, x)));
490 *=============================================================================
500 * sum_squares(x,y) = x*x + y*y (or just x*x if y*y would underflow).
501 * Assumes x*x and y*y will not overflow.
502 * Assumes x and y are finite.
503 * Assumes y is non-negative.
504 * Assumes fabs(x) >= DBL_EPSILON.
507 sum_squares(double x, double y)
510 /* Avoid underflow when y is small. */
513 return (x * x + y * y);
517 * real_part_reciprocal(x, y) = Re(1/(x+I*y)) = x/(x*x + y*y).
518 * Assumes x and y are not NaN, and one of x and y is larger than
519 * RECIP_EPSILON. We avoid unwarranted underflow. It is important to not use
520 * the code creal(1/z), because the imaginary part may produce an unwanted
522 * This is only called in a context where inexact is always raised before
523 * the call, so no effort is made to avoid or force inexact.
526 real_part_reciprocal(double x, double y)
533 * This code is inspired by the C99 document n1124.pdf, Section G.5.1,
536 GET_HIGH_WORD(hx, x);
537 ix = hx & 0x7ff00000;
538 GET_HIGH_WORD(hy, y);
539 iy = hy & 0x7ff00000;
540 #define BIAS (DBL_MAX_EXP - 1)
541 /* XXX more guard digits are useful iff there is extra precision. */
542 #define CUTOFF (DBL_MANT_DIG / 2 + 1) /* just half or 1 guard digit */
543 if (ix - iy >= CUTOFF << 20 || isinf(x))
544 return (1 / x); /* +-Inf -> +-0 is special */
545 if (iy - ix >= CUTOFF << 20)
546 return (x / y / y); /* should avoid double div, but hard */
547 if (ix <= (BIAS + DBL_MAX_EXP / 2 - CUTOFF) << 20)
548 return (x / (x * x + y * y));
550 SET_HIGH_WORD(scale, 0x7ff00000 - ix); /* 2**(1-ilogb(x)) */
553 return (x / (x * x + y * y) * scale);
557 * catanh(z) = log((1+z)/(1-z)) / 2
558 * = log1p(4*x / |z-1|^2) / 4
559 * + I * atan2(2*y, (1-x)*(1+x)-y*y) / 2
561 * catanh(z) = z + O(z^3) as z -> 0
563 * catanh(z) = 1/z + sign(y)*I*PI/2 + O(1/z^3) as z -> infinity
564 * The above formula works for the real part as well, because
565 * Re(catanh(z)) = x/|z|^2 + O(x/z^4)
566 * as z -> infinity, uniformly in x
569 catanh(double complex z)
571 double x, y, ax, ay, rx, ry;
578 /* This helps handle many cases. */
579 if (y == 0 && ax <= 1)
580 return (cpack(atanh(x), y));
582 /* To ensure the same accuracy as atan(), and to filter out z = 0. */
584 return (cpack(x, atan(y)));
586 if (isnan(x) || isnan(y)) {
587 /* catanh(+-Inf + I*NaN) = +-0 + I*NaN */
589 return (cpack(copysign(0, x), y + y));
590 /* catanh(NaN + I*+-Inf) = sign(NaN)0 + I*+-PI/2 */
592 return (cpack(copysign(0, x),
593 copysign(pio2_hi + pio2_lo, y)));
596 * All other cases involving NaN return NaN + I*NaN.
597 * C99 leaves it optional whether to raise invalid if one of
598 * the arguments is not NaN, so we opt not to raise it.
600 return (cpack(x + 0.0L + (y + 0), x + 0.0L + (y + 0)));
603 if (ax > RECIP_EPSILON || ay > RECIP_EPSILON) {
604 return (cpack(real_part_reciprocal(x, y),
605 copysign(pio2_hi + pio2_lo, y)));
608 if (ax < SQRT_3_EPSILON / 2 && ay < SQRT_3_EPSILON / 2) {
610 * z = 0 was filtered out above. All other cases must raise
611 * inexact, but this is the only only that needs to do it
618 if (ax == 1 && ay < DBL_EPSILON)
619 rx = (log(ay) - m_ln2) / -2;
621 rx = log1p(4 * ax / sum_squares(ax - 1, ay)) / 4;
624 ry = atan2(2, -ay) / 2;
625 else if (ay < DBL_EPSILON)
626 ry = atan2(2 * ay, (1 - ax) * (1 + ax)) / 2;
628 ry = atan2(2 * ay, (1 - ax) * (1 + ax) - ay * ay) / 2;
630 return (cpack(copysign(rx, x), copysign(ry, y)));
634 * catan(z) = reverse(catanh(reverse(z)))
635 * where reverse(x + I*y) = y + I*x = I*conj(z).
638 catan(double complex z)
640 double complex w = catanh(cpack(cimag(z), creal(z)));
641 return (cpack(cimag(w), creal(w)));