initial cmath code and minor libm.h update
[libm] / src / cmath / ctanh.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_ctanh.c */
2 /*-
3  * Copyright (c) 2011 David Schultz
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    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.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /*
29  * Hyperbolic tangent of a complex argument z = x + i y.
30  *
31  * The algorithm is from:
32  *
33  *   W. Kahan.  Branch Cuts for Complex Elementary Functions or Much
34  *   Ado About Nothing's Sign Bit.  In The State of the Art in
35  *   Numerical Analysis, pp. 165 ff.  Iserles and Powell, eds., 1987.
36  *
37  * Method:
38  *
39  *   Let t    = tan(x)
40  *       beta = 1/cos^2(y)
41  *       s    = sinh(x)
42  *       rho  = cosh(x)
43  *
44  *   We have:
45  *
46  *   tanh(z) = sinh(z) / cosh(z)
47  *
48  *             sinh(x) cos(y) + i cosh(x) sin(y)
49  *           = ---------------------------------
50  *             cosh(x) cos(y) + i sinh(x) sin(y)
51  *
52  *             cosh(x) sinh(x) / cos^2(y) + i tan(y)
53  *           = -------------------------------------
54  *                    1 + sinh^2(x) / cos^2(y)
55  *
56  *             beta rho s + i t
57  *           = ----------------
58  *               1 + beta s^2
59  *
60  * Modifications:
61  *
62  *   I omitted the original algorithm's handling of overflow in tan(x) after
63  *   verifying with nearpi.c that this can't happen in IEEE single or double
64  *   precision.  I also handle large x differently.
65  */
66
67 #include "libm.h"
68
69 double complex ctanh(double complex z)
70 {
71         double x, y;
72         double t, beta, s, rho, denom;
73         uint32_t hx, ix, lx;
74
75         x = creal(z);
76         y = cimag(z);
77
78         EXTRACT_WORDS(hx, lx, x);
79         ix = hx & 0x7fffffff;
80
81         /*
82          * ctanh(NaN + i 0) = NaN + i 0
83          *
84          * ctanh(NaN + i y) = NaN + i NaN               for y != 0
85          *
86          * The imaginary part has the sign of x*sin(2*y), but there's no
87          * special effort to get this right.
88          *
89          * ctanh(+-Inf +- i Inf) = +-1 +- 0
90          *
91          * ctanh(+-Inf + i y) = +-1 + 0 sin(2y)         for y finite
92          *
93          * The imaginary part of the sign is unspecified.  This special
94          * case is only needed to avoid a spurious invalid exception when
95          * y is infinite.
96          */
97         if (ix >= 0x7ff00000) {
98                 if ((ix & 0xfffff) | lx)        /* x is NaN */
99                         return cpack(x, (y == 0 ? y : x * y));
100                 SET_HIGH_WORD(x, hx - 0x40000000);      /* x = copysign(1, x) */
101                 return cpack(x, copysign(0, isinf(y) ? y : sin(y) * cos(y)));
102         }
103
104         /*
105          * ctanh(x + i NAN) = NaN + i NaN
106          * ctanh(x +- i Inf) = NaN + i NaN
107          */
108         if (!isfinite(y))
109                 return cpack(y - y, y - y);
110
111         /*
112          * ctanh(+-huge + i +-y) ~= +-1 +- i 2sin(2y)/exp(2x), using the
113          * approximation sinh^2(huge) ~= exp(2*huge) / 4.
114          * We use a modified formula to avoid spurious overflow.
115          */
116         if (ix >= 0x40360000) { /* x >= 22 */
117                 double exp_mx = exp(-fabs(x));
118                 return cpack(copysign(1, x), 4 * sin(y) * cos(y) * exp_mx * exp_mx);
119         }
120
121         /* Kahan's algorithm */
122         t = tan(y);
123         beta = 1.0 + t * t;     /* = 1 / cos^2(y) */
124         s = sinh(x);
125         rho = sqrt(1 + s * s);  /* = cosh(x) */
126         denom = 1 + beta * s * s;
127         return cpack((beta * rho * s) / denom, t / denom);
128 }