1 /* s_tanhl.c -- long double version of s_tanh.c.
2  */
3 
4 /*
5  * ====================================================
6  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7  *
8  * Developed at SunPro, a Sun Microsystems, Inc. business.
9  * Permission to use, copy, modify, and distribute this
10  * software is freely granted, provided that this notice
11  * is preserved.
12  * ====================================================
13  */
14 
15 #if defined(LIBM_SCCS) && !defined(lint)
16 static char rcsid[] = "$NetBSD: $";
17 #endif
18 
19 /* tanhl(x)
20  * Return the Hyperbolic Tangent of x
21  *
22  * Method :
23  *				        x    -x
24  *				       e  - e
25  *	0. tanhl(x) is defined to be -----------
26  *				        x    -x
27  *				       e  + e
28  *	1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
29  *	2.  0      <= x <= 2**-55 : tanhl(x) := x*(one+x)
30  *					         -t
31  *	    2**-55 <  x <=  1     : tanhl(x) := -----; t = expm1l(-2x)
32  *					        t + 2
33  *						      2
34  *	    1      <= x <=  23.0  : tanhl(x) := 1-  ----- ; t=expm1l(2x)
35  *						    t + 2
36  *	    23.0   <  x <= INF    : tanhl(x) := 1.
37  *
38  * Special cases:
39  *	tanhl(NaN) is NaN;
40  *	only tanhl(0)=0 is exact for finite argument.
41  */
42 
43 #include <float.h>
44 #include <math.h>
45 #include <math_private.h>
46 #include <math-underflow.h>
47 #include <libm-alias-ldouble.h>
48 
49 static const long double one=1.0, two=2.0, tiny = 1.0e-4900L;
50 
__tanhl(long double x)51 long double __tanhl(long double x)
52 {
53 	long double t,z;
54 	int32_t se;
55 	uint32_t j0,j1,ix;
56 
57     /* High word of |x|. */
58 	GET_LDOUBLE_WORDS(se,j0,j1,x);
59 	ix = se&0x7fff;
60 
61     /* x is INF or NaN */
62 	if(ix==0x7fff) {
63 	    /* for NaN it's not important which branch: tanhl(NaN) = NaN */
64 	    if (se&0x8000) return one/x-one;	/* tanhl(-inf)= -1; */
65 	    else           return one/x+one;	/* tanhl(+inf)=+1 */
66 	}
67 
68     /* |x| < 23 */
69 	if (ix < 0x4003 || (ix == 0x4003 && j0 < 0xb8000000u)) {/* |x|<23 */
70 	    if ((ix|j0|j1) == 0)
71 		return x;		/* x == +- 0 */
72 	    if (ix<0x3fc8)		/* |x|<2**-55 */
73 	      {
74 		math_check_force_underflow (x);
75 		return x*(one+tiny);	/* tanh(small) = small */
76 	      }
77 	    if (ix>=0x3fff) {	/* |x|>=1  */
78 		t = __expm1l(two*fabsl(x));
79 		z = one - two/(t+two);
80 	    } else {
81 	        t = __expm1l(-two*fabsl(x));
82 	        z= -t/(t+two);
83 	    }
84     /* |x| > 23, return +-1 */
85 	} else {
86 	    z = one - tiny;		/* raised inexact flag */
87 	}
88 	return (se&0x8000)? -z: z;
89 }
90 libm_alias_ldouble (__tanh, tanh)
91