summaryrefslogtreecommitdiff
path: root/usr/src/lib/libm/common/complex/ctanh.c
blob: ee0819f6366dc82cd8a16b4ba39ea2e8b8b69a09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma weak __ctanh = ctanh

/* INDENT OFF */
/*
 * dcomplex ctanh(dcomplex z);
 *
 *            tanh x  + i tan y      sinh 2x  +  i sin 2y
 * ctanh z = --------------------- = --------------------
 *           1 + i tanh(x)tan(y)       cosh 2x + cos 2y
 *
 * For |x| >= prec/2 (14,28,34,60 for single, double, double extended, quad),
 * we use
 *
 *                         1   2x                              2 sin 2y
 *    cosh 2x = sinh 2x = --- e    and hence  ctanh z = 1 + i -----------;
 *                         2                                       2x
 *                                                                e
 *
 * otherwise, to avoid cancellation, for |x| < prec/2,
 *                              2x     2
 *                            (e   - 1)        2       2
 *    cosh 2x + cos 2y = 1 + ------------ + cos y - sin y
 *                                 2x
 *                              2 e
 *
 *                        1    2x     2  -2x         2
 *                     = --- (e   - 1)  e     + 2 cos y
 *                        2
 * and
 *
 *                  [            2x      ]
 *               1  [  2x       e   - 1  ]
 *    sinh 2x = --- [ e  - 1 + --------- ]
 *               2  [               2x   ]
 *                  [              e     ]
 *                                             2x
 * Implementation notes:  let t = expm1(2x) = e   - 1, then
 *
 *                     1    [  t*t         2  ]              1    [      t  ]
 * cosh 2x + cos 2y = --- * [ ----- + 4 cos y ];  sinh 2x = --- * [ t + --- ]
 *                     2    [  t+1            ]              2    [     t+1 ]
 *
 * Hence,
 *
 *
 *                        t*t+2t                  [4(t+1)(cos y)]*(sin y)
 *    ctanh z = --------------------------- + i --------------------------
 *               t*t+[4(t+1)(cos y)](cos y)     t*t+[4(t+1)(cos y)](cos y)
 *
 * EXCEPTION (conform to ISO/IEC 9899:1999(E)):
 *      ctanh(0,0)=(0,0)
 *      ctanh(x,inf) = (NaN,NaN) for finite x
 *      ctanh(x,NaN) = (NaN,NaN) for finite x
 *      ctanh(inf,y) = 1+ i*0*sin(2y) for positive-signed finite y
 *      ctanh(inf,inf) = (1, +-0)
 *      ctanh(inf,NaN) = (1, +-0)
 *      ctanh(NaN,0) = (NaN,0)
 *      ctanh(NaN,y) = (NaN,NaN) for non-zero y
 *      ctanh(NaN,NaN) = (NaN,NaN)
 */
/* INDENT ON */

#include "libm.h"		/* exp/expm1/fabs/sin/tanh/sincos */
#include "complex_wrapper.h"

static const double four = 4.0, two = 2.0, one = 1.0, zero = 0.0;

dcomplex
ctanh(dcomplex z) {
	double t, r, v, u, x, y, S, C;
	int hx, ix, lx, hy, iy, ly;
	dcomplex ans;

	x = D_RE(z);
	y = D_IM(z);
	hx = HI_WORD(x);
	lx = LO_WORD(x);
	ix = hx & 0x7fffffff;
	hy = HI_WORD(y);
	ly = LO_WORD(y);
	iy = hy & 0x7fffffff;
	x = fabs(x);
	y = fabs(y);

	if ((iy | ly) == 0) {	/* ctanh(x,0) = (x,0) for x = 0 or NaN */
		D_RE(ans) = tanh(x);
		D_IM(ans) = zero;
	} else if (iy >= 0x7ff00000) {	/* y is inf or NaN */
		if (ix < 0x7ff00000)	/* catanh(finite x,inf/nan) is nan */
			D_RE(ans) = D_IM(ans) = y - y;
		else if (((ix - 0x7ff00000) | lx) == 0) {	/* x is inf */
			D_RE(ans) = one;
			D_IM(ans) = zero;
		} else {
			D_RE(ans) = x + y;
			D_IM(ans) = y - y;
		}
	} else if (ix >= 0x403c0000) {
		/*
		 * |x| > 28 = prec/2 (14,28,34,60)
		 * ctanh z ~ 1 + i (sin2y)/(exp(2x))
		 */
		D_RE(ans) = one;
		if (iy < 0x7fe00000)	/* t = sin(2y) */
			S = sin(y + y);
		else {
			(void) sincos(y, &S, &C);
			S = (S + S) * C;
		}
		if (ix >= 0x7fe00000) {	/* |x| > max/2 */
			if (ix >= 0x7ff00000) {	/* |x| is inf or NaN */
				if (((ix - 0x7ff00000) | lx) != 0)
					D_RE(ans) = D_IM(ans) = x + y;
								/* x is NaN */
				else
					D_IM(ans) = zero * S;	/* x is inf */
			} else
				D_IM(ans) = S * exp(-x);	/* underflow */
		} else
			D_IM(ans) = (S + S) * exp(-(x + x));
							/* 2 sin 2y / exp(2x) */
	} else {
		/* INDENT OFF */
		/*
		 *                        t*t+2t
		 *    ctanh z = --------------------------- +
		 *               t*t+[4(t+1)(cos y)](cos y)
		 *
		 *                  [4(t+1)(cos y)]*(sin y)
		 *              i --------------------------
		 *                t*t+[4(t+1)(cos y)](cos y)
		 */
		/* INDENT ON */
		(void) sincos(y, &S, &C);
		t = expm1(x + x);
		r = (four * C) * (t + one);
		u = t * t;
		v = one / (u + r * C);
		D_RE(ans) = (u + two * t) * v;
		D_IM(ans) = (r * S) * v;
	}
	if (hx < 0)
		D_RE(ans) = -D_RE(ans);
	if (hy < 0)
		D_IM(ans) = -D_IM(ans);
	return (ans);
}