1 /* s_tanhf.c -- float 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: s_tanhf.c,v 1.4 1995/05/10 20:48:24 jtc Exp $";
17 #endif
18 
19 #include <float.h>
20 #include <math.h>
21 #include <math_private.h>
22 #include <math-underflow.h>
23 #include <libm-alias-float.h>
24 
25 static const float one=1.0, two=2.0, tiny = 1.0e-30;
26 
__tanhf(float x)27 float __tanhf(float x)
28 {
29 	float t,z;
30 	int32_t jx,ix;
31 
32 	GET_FLOAT_WORD(jx,x);
33 	ix = jx&0x7fffffff;
34 
35     /* x is INF or NaN */
36 	if(ix>=0x7f800000) {
37 	    if (jx>=0) return one/x+one;    /* tanh(+-inf)=+-1 */
38 	    else       return one/x-one;    /* tanh(NaN) = NaN */
39 	}
40 
41     /* |x| < 22 */
42 	if (ix < 0x41b00000) {		/* |x|<22 */
43 	    if (ix == 0)
44 		return x;		/* x == +-0 */
45 	    if (ix<0x24000000) 		/* |x|<2**-55 */
46 	      {
47 		math_check_force_underflow (x);
48 		return x*(one+x);    	/* tanh(small) = small */
49 	      }
50 	    if (ix>=0x3f800000) {	/* |x|>=1  */
51 		t = __expm1f(two*fabsf(x));
52 		z = one - two/(t+two);
53 	    } else {
54 	        t = __expm1f(-two*fabsf(x));
55 	        z= -t/(t+two);
56 	    }
57     /* |x| > 22, return +-1 */
58 	} else {
59 	    z = one - tiny;		/* raised inexact flag */
60 	}
61 	return (jx>=0)? z: -z;
62 }
63 libm_alias_float (__tanh, tanh)
64