xref: /titanic_52/usr/src/lib/libm/common/complex/ctanhf.c (revision d14abf155341d55053c76eeec58b787a456b753b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  */
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #pragma weak __ctanhf = ctanhf
31 
32 #include "libm.h"		/* expf/expm1f/fabsf/sincosf/sinf/tanhf */
33 #include "complex_wrapper.h"
34 
35 /* INDENT OFF */
36 static const float four = 4.0F, two = 2.0F, one = 1.0F, zero = 0.0F;
37 /* INDENT ON */
38 
39 fcomplex
40 ctanhf(fcomplex z) {
41 	float r, u, v, t, x, y, S, C;
42 	int hx, ix, hy, iy;
43 	fcomplex ans;
44 
45 	x = F_RE(z);
46 	y = F_IM(z);
47 	hx = THE_WORD(x);
48 	ix = hx & 0x7fffffff;
49 	hy = THE_WORD(y);
50 	iy = hy & 0x7fffffff;
51 	x = fabsf(x);
52 	y = fabsf(y);
53 
54 	if (iy == 0) {		/* ctanh(x,0) = (x,0) for x = 0 or NaN */
55 		F_RE(ans) = tanhf(x);
56 		F_IM(ans) = zero;
57 	} else if (iy >= 0x7f800000) {	/* y is inf or NaN */
58 		if (ix < 0x7f800000)	/* catanh(finite x,inf/nan) is nan */
59 			F_RE(ans) = F_IM(ans) = y - y;
60 		else if (ix == 0x7f800000) {	/* x is inf */
61 			F_RE(ans) = one;
62 			F_IM(ans) = zero;
63 		} else {
64 			F_RE(ans) = x + y;
65 			F_IM(ans) = y - y;
66 		}
67 	} else if (ix >= 0x41600000) {
68 		/*
69 		 * |x| > 14 = prec/2 (14,28,34,60)
70 		 * ctanh z ~ 1 + i (sin2y)/(exp(2x))
71 		 */
72 		F_RE(ans) = one;
73 		if (iy < 0x7f000000)	/* t = sin(2y) */
74 			S = sinf(y + y);
75 		else {
76 			(void) sincosf(y, &S, &C);
77 			S = (S + S) * C;
78 		}
79 		if (ix >= 0x7f000000) {	/* |x| > max/2 */
80 			if (ix >= 0x7f800000) {	/* |x| is inf or NaN */
81 				if (ix > 0x7f800000)	/* x is NaN */
82 					F_RE(ans) = F_IM(ans) = x + y;
83 				else
84 					F_IM(ans) = zero * S;	/* x is inf */
85 			} else
86 				F_IM(ans) = S * expf(-x);	/* underflow */
87 		} else
88 			F_IM(ans) = (S + S) * expf(-(x + x));
89 							/* 2 sin 2y / exp(2x) */
90 	} else {
91 		/* INDENT OFF */
92 		/*
93 		 *                        t*t+2t
94 		 *    ctanh z = ---------------------------
95 		 *               t*t+[4(t+1)(cos y)](cos y)
96 		 *
97 		 *                  [4(t+1)(cos y)]*(sin y)
98 		 *              i --------------------------
99 		 *                t*t+[4(t+1)(cos y)](cos y)
100 		 */
101 		/* INDENT ON */
102 		(void) sincosf(y, &S, &C);
103 		t = expm1f(x + x);
104 		r = (four * C) * (t + one);
105 		u = t * t;
106 		v = one / (u + r * C);
107 		F_RE(ans) = (u + two * t) * v;
108 		F_IM(ans) = (r * S) * v;
109 	}
110 	if (hx < 0)
111 		F_RE(ans) = -F_RE(ans);
112 	if (hy < 0)
113 		F_IM(ans) = -F_IM(ans);
114 	return (ans);
115 }
116