Branch data Line data Source code
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 "cdefs-compat.h"
17 : : //__FBSDID("$FreeBSD: src/lib/msun/src/s_tanhf.c,v 1.9 2008/02/22 02:30:36 das Exp $");
18 : :
19 : : #include <openlibm_math.h>
20 : :
21 : : #include "math_private.h"
22 : :
23 : : static const float one=1.0, two=2.0, tiny = 1.0e-30, huge = 1.0e30;
24 : : OLM_DLLEXPORT float
25 : 11 : tanhf(float x)
26 : : {
27 : : float t,z;
28 : : int32_t jx,ix;
29 : :
30 : 11 : GET_FLOAT_WORD(jx,x);
31 : 11 : ix = jx&0x7fffffff;
32 : :
33 : : /* x is INF or NaN */
34 [ + + ]: 11 : if(ix>=0x7f800000) {
35 [ + + ]: 3 : if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */
36 : 2 : else return one/x-one; /* tanh(NaN) = NaN */
37 : : }
38 : :
39 : : /* |x| < 9 */
40 [ + - ]: 8 : if (ix < 0x41100000) { /* |x|<9 */
41 [ + + ]: 8 : if (ix<0x39800000) { /* |x|<2**-12 */
42 [ + - ]: 3 : if(huge+x>one) return x; /* tanh(tiny) = tiny with inexact */
43 : : }
44 [ + + ]: 5 : if (ix>=0x3f800000) { /* |x|>=1 */
45 : 2 : t = expm1f(two*fabsf(x));
46 : 2 : z = one - two/(t+two);
47 : : } else {
48 : 3 : t = expm1f(-two*fabsf(x));
49 : 3 : z= -t/(t+two);
50 : : }
51 : : /* |x| >= 9, return +-1 */
52 : : } else {
53 : 0 : z = one - tiny; /* raise inexact flag */
54 : : }
55 [ + + ]: 5 : return (jx>=0)? z: -z;
56 : : }
|