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