1 /* sf_tanh.c -- float version of s_tanh.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #include "fdlibm.h"
17 
18 static const volatile float one = 1.0, two = 2.0, tiny = 1.0e-30;
19 
20 float
tanhf(float x)21 tanhf(float x)
22 {
23     float t, z;
24     __int32_t jx, ix;
25 
26     GET_FLOAT_WORD(jx, x);
27     ix = jx & 0x7fffffff;
28 
29     /* x is INF or NaN */
30     if (!FLT_UWORD_IS_FINITE(ix)) {
31         if (jx >= 0)
32             return one / x + one; /* tanh(+-inf)=+-1 */
33         else
34             return one / x - one; /* tanh(NaN) = NaN */
35     }
36 
37     /* |x| < 22 */
38     if (ix < 0x41b00000) { /* |x|<22 */
39         if (ix < 0x24000000) /* |x|<2**-55 */
40             return x * (one + x); /* tanh(small) = small */
41         if (ix >= 0x3f800000) { /* |x|>=1  */
42             t = expm1f(two * fabsf(x));
43             z = one - two / (t + two);
44         } else {
45             t = expm1f(-two * fabsf(x));
46             z = -t / (t + two);
47         }
48         /* |x| > 22, return +-1 */
49     } else {
50         z = one - tiny; /* raised inexact flag */
51     }
52     return (jx >= 0) ? z : -z;
53 }
54 
55 _MATH_ALIAS_f_f(tanh)
56