xref: /netbsd-src/lib/libm/src/s_tanhf.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /* s_tanhf.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 <sys/cdefs.h>
17 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: s_tanhf.c,v 1.5 1997/10/09 11:33:43 lukem Exp $");
19 #endif
20 
21 #include "math.h"
22 #include "math_private.h"
23 
24 #ifdef __STDC__
25 static const float one=1.0, two=2.0, tiny = 1.0e-30;
26 #else
27 static float one=1.0, two=2.0, tiny = 1.0e-30;
28 #endif
29 
30 #ifdef __STDC__
31 	float tanhf(float x)
32 #else
33 	float tanhf(x)
34 	float x;
35 #endif
36 {
37 	float t,z;
38 	int32_t jx,ix;
39 
40 	GET_FLOAT_WORD(jx,x);
41 	ix = jx&0x7fffffff;
42 
43     /* x is INF or NaN */
44 	if(ix>=0x7f800000) {
45 	    if (jx>=0) return one/x+one;    /* tanh(+-inf)=+-1 */
46 	    else       return one/x-one;    /* tanh(NaN) = NaN */
47 	}
48 
49     /* |x| < 22 */
50 	if (ix < 0x41b00000) {		/* |x|<22 */
51 	    if (ix<0x24000000) 		/* |x|<2**-55 */
52 		return x*(one+x);    	/* tanh(small) = small */
53 	    if (ix>=0x3f800000) {	/* |x|>=1  */
54 		t = expm1f(two*fabsf(x));
55 		z = one - two/(t+two);
56 	    } else {
57 	        t = expm1f(-two*fabsf(x));
58 	        z= -t/(t+two);
59 	    }
60     /* |x| > 22, return +-1 */
61 	} else {
62 	    z = one - tiny;		/* raised inexact flag */
63 	}
64 	return (jx>=0)? z: -z;
65 }
66