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 /* tanhl(x)
14 * Return the Hyperbolic Tangent of x
15 *
16 * Method :
17 * x -x
18 * e - e
19 * 0. tanhl(x) is defined to be -----------
20 * x -x
21 * e + e
22 * 1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
23 * 2. 0 <= x <= 2**-55 : tanhl(x) := x*(one+x)
24 * -t
25 * 2**-55 < x <= 1 : tanhl(x) := -----; t = expm1l(-2x)
26 * t + 2
27 * 2
28 * 1 <= x <= 23.0 : tanhl(x) := 1- ----- ; t=expm1l(2x)
29 * t + 2
30 * 23.0 < x <= INF : tanhl(x) := 1.
31 *
32 * Special cases:
33 * tanhl(NaN) is NaN;
34 * only tanhl(0)=0 is exact for finite argument.
35 */
36
37
38
39 static const long double one=1.0L, two=2.0L, tiny = 1.0e-4900L;
40
41 long double
tanhl(long double x)42 tanhl(long double x)
43 {
44 long double t,z;
45 int32_t se;
46 u_int32_t jj0,jj1,ix;
47
48 /* High word of |x|. */
49 GET_LDOUBLE_WORDS(se,jj0,jj1,x);
50 ix = se&0x7fff;
51
52 /* x is INF or NaN */
53 if(ix==0x7fff) {
54 /* for NaN it's not important which branch: tanhl(NaN) = NaN */
55 if (se&0x8000) return one/x-one; /* tanhl(-inf)= -1; */
56 else return one/x+one; /* tanhl(+inf)=+1 */
57 }
58
59 /* |x| < 23 */
60 if (ix < 0x4003 || (ix == 0x4003 && jj0 < 0xb8000000u)) {/* |x|<23 */
61 if ((ix|jj0|jj1) == 0)
62 return x; /* x == +- 0 */
63 if (ix<0x3fc8) /* |x|<2**-55 */
64 return x*(one+tiny); /* tanh(small) = small */
65 if (ix>=0x3fff) { /* |x|>=1 */
66 t = expm1l(two*fabsl(x));
67 z = one - two/(t+two);
68 } else {
69 t = expm1l(-two*fabsl(x));
70 z= -t/(t+two);
71 }
72 /* |x| > 23, return +-1 */
73 } else {
74 z = one - tiny; /* raised inexact flag */
75 }
76 return (se&0x8000)? -z: z;
77 }
78