code cleanup of named constants
[musl] / src / math / tanhf.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_tanhf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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 "libm.h"
17
18 static const float
19 tiny = 1.0e-30,
20 huge = 1.0e30;
21
22 float tanhf(float x)
23 {
24         float t,z;
25         int32_t jx,ix;
26
27         GET_FLOAT_WORD(jx, x);
28         ix = jx & 0x7fffffff;
29
30         /* x is INF or NaN */
31         if(ix >= 0x7f800000) {
32                 if (jx >= 0)
33                         return 1.0f/x + 1.0f;  /* tanh(+-inf)=+-1 */
34                 else
35                         return 1.0f/x - 1.0f;  /* tanh(NaN) = NaN */
36         }
37
38         if (ix < 0x41100000) {  /* |x| < 9 */
39                 if (ix < 0x39800000) {  /* |x| < 2**-12 */
40                         /* tanh(tiny) = tiny with inexact */
41                         if (huge+x > 1.0f)
42                                 return x;
43                 }
44                 if (ix >= 0x3f800000) {  /* |x|>=1  */
45                         t = expm1f(2.0f*fabsf(x));
46                         z = 1.0f - 2.0f/(t+2.0f);
47                 } else {
48                         t = expm1f(-2.0f*fabsf(x));
49                         z = -t/(t+2.0f);
50                 }
51         } else {  /* |x| >= 9, return +-1 */
52                 z = 1.0f - tiny;  /* raise inexact */
53         }
54         return jx >= 0 ? z : -z;
55 }