Merge commit '281819e5f8b19cd8627541a22d261906fd190276' into merges
[unleashed.git] / usr / src / lib / libm / common / LD / tanhl.c
blob7819c1a9dc0f456e89c8e78c1bd0c6ce11aceb88
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #pragma weak __tanhl = tanhl
33 * tanhl(x) returns the hyperbolic tangent of x
35 * Method :
36 * 1. reduce x to non-negative: tanhl(-x) = - tanhl(x).
37 * 2.
38 * 0 < x <= small : tanhl(x) := x
39 * -expm1l(-2x)
40 * small < x <= 1 : tanhl(x) := --------------
41 * expm1l(-2x) + 2
42 * 2
43 * 1 <= x <= threshold : tanhl(x) := 1 - ---------------
44 * expm1l(2x) + 2
45 * threshold < x <= INF : tanhl(x) := 1.
47 * where
48 * single : small = 1.e-5 threshold = 11.0
49 * double : small = 1.e-10 threshold = 22.0
50 * quad : small = 1.e-20 threshold = 45.0
52 * Note: threshold was chosen so that
53 * fl(1.0+2/(expm1(2*threshold)+2)) == 1.
55 * Special cases:
56 * tanhl(NaN) is NaN;
57 * only tanhl(0.0)=0.0 is exact for finite argument.
60 #include "libm.h"
61 #include "longdouble.h"
63 static const long double small = 1.0e-20L, one = 1.0, two = 2.0,
64 big = 1.0e+20L,
65 threshold = 45.0L;
67 long double
68 tanhl(long double x) {
69 long double t, y, z;
70 int signx;
71 volatile long double dummy __unused;
73 if (isnanl(x))
74 return (x + x); /* x is NaN */
75 signx = signbitl(x);
76 t = fabsl(x);
77 z = one;
78 if (t <= threshold) {
79 if (t > one)
80 z = one - two / (expm1l(t + t) + two);
81 else if (t > small) {
82 y = expm1l(-t - t);
83 z = -y / (y + two);
84 } else {
85 dummy = t + big;
86 /* inexact if t != 0 */
87 return (x);
89 } else if (!finitel(t))
90 return (copysignl(one, x));
91 else
92 return (signx ? -z + small * small : z - small * small);
93 return (signx ? -z : z);