xref: /openbsd/lib/libm/src/ld128/s_tanhl.c (revision 49393c00)
1 /* @(#)s_tanh.c 5.1 93/09/24 */
2 /*
3  * ====================================================
4  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5  *
6  * Developed at SunPro, a Sun Microsystems, Inc. business.
7  * Permission to use, copy, modify, and distribute this
8  * software is freely granted, provided that this notice
9  * is preserved.
10  * ====================================================
11  */
12 
13 /*
14  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
15  *
16  * Permission to use, copy, modify, and distribute this software for any
17  * purpose with or without fee is hereby granted, provided that the above
18  * copyright notice and this permission notice appear in all copies.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
21  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
23  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
25  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
26  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27  */
28 
29 /* tanhl(x)
30  * Return the Hyperbolic Tangent of x
31  *
32  * Method :
33  *                                      x    -x
34  *                                     e  - e
35  *      0. tanhl(x) is defined to be -----------
36  *                                      x    -x
37  *                                     e  + e
38  *      1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
39  *      2.  0      <= x <= 2**-57 : tanhl(x) := x*(one+x)
40  *                                               -t
41  *          2**-57 <  x <=  1     : tanhl(x) := -----; t = expm1l(-2x)
42  *                                              t + 2
43  *                                                    2
44  *          1      <= x <=  40.0  : tanhl(x) := 1-  ----- ; t=expm1l(2x)
45  *                                                  t + 2
46  *          40.0   <  x <= INF    : tanhl(x) := 1.
47  *
48  * Special cases:
49  *      tanhl(NaN) is NaN;
50  *      only tanhl(0)=0 is exact for finite argument.
51  */
52 
53 #include "math.h"
54 #include "math_private.h"
55 
56 static const long double one = 1.0, two = 2.0, tiny = 1.0e-4900L;
57 
58 long double
tanhl(long double x)59 tanhl(long double x)
60 {
61   long double t, z;
62   u_int32_t jx, ix;
63   ieee_quad_shape_type u;
64 
65   /* Words of |x|. */
66   u.value = x;
67   jx = u.parts32.mswhi;
68   ix = jx & 0x7fffffff;
69   /* x is INF or NaN */
70   if (ix >= 0x7fff0000)
71     {
72       /* for NaN it's not important which branch: tanhl(NaN) = NaN */
73       if (jx & 0x80000000)
74 	return one / x - one;	/* tanhl(-inf)= -1; */
75       else
76 	return one / x + one;	/* tanhl(+inf)=+1 */
77     }
78 
79   /* |x| < 40 */
80   if (ix < 0x40044000)
81     {
82       if (u.value == 0)
83 	return x;		/* x == +- 0 */
84       if (ix < 0x3fc60000)	/* |x| < 2^-57 */
85 	return x * (one + tiny); /* tanh(small) = small */
86       u.parts32.mswhi = ix;	/* Absolute value of x.  */
87       if (ix >= 0x3fff0000)
88 	{			/* |x| >= 1  */
89 	  t = expm1l (two * u.value);
90 	  z = one - two / (t + two);
91 	}
92       else
93 	{
94 	  t = expm1l (-two * u.value);
95 	  z = -t / (t + two);
96 	}
97       /* |x| > 40, return +-1 */
98     }
99   else
100     {
101       z = one - tiny;		/* raised inexact flag */
102     }
103   return (jx & 0x80000000) ? -z : z;
104 }
105