Home | History | Annotate | Line # | Download | only in src
      1 /* s_tanhf.c -- float version of s_tanh.c.
      2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian (at) 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.7 2002/05/26 22:01:59 wiz Exp $");
     19 #endif
     20 
     21 #include "math.h"
     22 #include "math_private.h"
     23 
     24 static const float one=1.0, two=2.0, tiny = 1.0e-30;
     25 
     26 float
     27 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<0x24000000) 		/* |x|<2**-55 */
     44 		return x*(one+x);    	/* tanh(small) = small */
     45 	    if (ix>=0x3f800000) {	/* |x|>=1  */
     46 		t = expm1f(two*fabsf(x));
     47 		z = one - two/(t+two);
     48 	    } else {
     49 	        t = expm1f(-two*fabsf(x));
     50 	        z= -t/(t+two);
     51 	    }
     52     /* |x| > 22, return +-1 */
     53 	} else {
     54 	    z = one - tiny;		/* raised inexact flag */
     55 	}
     56 	return (jx>=0)? z: -z;
     57 }
     58