sync
[bitrig.git] / lib / libm / src / ld80 / s_tanhl.c
bloba0b7bd8e4c3f53af912edce75f842036ff50568c
1 /* @(#)s_tanh.c 5.1 93/09/24 */
2 /*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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 * ====================================================
13 /* tanhl(x)
14 * Return the Hyperbolic Tangent of x
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.
32 * Special cases:
33 * tanhl(NaN) is NaN;
34 * only tanhl(0)=0 is exact for finite argument.
37 #include <math.h>
39 #include "math_private.h"
41 static const long double one=1.0, two=2.0, tiny = 1.0e-4900L;
43 long double
44 tanhl(long double x)
46 long double t,z;
47 int32_t se;
48 u_int32_t jj0,jj1,ix;
50 /* High word of |x|. */
51 GET_LDOUBLE_WORDS(se,jj0,jj1,x);
52 ix = se&0x7fff;
54 /* x is INF or NaN */
55 if(ix==0x7fff) {
56 /* for NaN it's not important which branch: tanhl(NaN) = NaN */
57 if (se&0x8000) return one/x-one; /* tanhl(-inf)= -1; */
58 else return one/x+one; /* tanhl(+inf)=+1 */
61 /* |x| < 23 */
62 if (ix < 0x4003 || (ix == 0x4003 && jj0 < 0xb8000000u)) {/* |x|<23 */
63 if ((ix|jj0|jj1) == 0)
64 return x; /* x == +- 0 */
65 if (ix<0x3fc8) /* |x|<2**-55 */
66 return x*(one+tiny); /* tanh(small) = small */
67 if (ix>=0x3fff) { /* |x|>=1 */
68 t = expm1l(two*fabsl(x));
69 z = one - two/(t+two);
70 } else {
71 t = expm1l(-two*fabsl(x));
72 z= -t/(t+two);
74 /* |x| > 23, return +-1 */
75 } else {
76 z = one - tiny; /* raised inexact flag */
78 return (se&0x8000)? -z: z;