Blame sysdeps/ieee754/ldbl-96/s_tanhl.c

Packit 6c4009
/* s_tanhl.c -- long double version of s_tanh.c.
Packit 6c4009
 * Conversion to long double by Ulrich Drepper,
Packit 6c4009
 * Cygnus Support, drepper@cygnus.com.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/*
Packit 6c4009
 * ====================================================
Packit 6c4009
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Developed at SunPro, a Sun Microsystems, Inc. business.
Packit 6c4009
 * Permission to use, copy, modify, and distribute this
Packit 6c4009
 * software is freely granted, provided that this notice
Packit 6c4009
 * is preserved.
Packit 6c4009
 * ====================================================
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#if defined(LIBM_SCCS) && !defined(lint)
Packit 6c4009
static char rcsid[] = "$NetBSD: $";
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* tanhl(x)
Packit 6c4009
 * Return the Hyperbolic Tangent of x
Packit 6c4009
 *
Packit 6c4009
 * Method :
Packit 6c4009
 *				        x    -x
Packit 6c4009
 *				       e  - e
Packit 6c4009
 *	0. tanhl(x) is defined to be -----------
Packit 6c4009
 *				        x    -x
Packit 6c4009
 *				       e  + e
Packit 6c4009
 *	1. reduce x to non-negative by tanhl(-x) = -tanhl(x).
Packit 6c4009
 *	2.  0      <= x <= 2**-55 : tanhl(x) := x*(one+x)
Packit 6c4009
 *					         -t
Packit 6c4009
 *	    2**-55 <  x <=  1     : tanhl(x) := -----; t = expm1l(-2x)
Packit 6c4009
 *					        t + 2
Packit 6c4009
 *						      2
Packit 6c4009
 *	    1      <= x <=  23.0  : tanhl(x) := 1-  ----- ; t=expm1l(2x)
Packit 6c4009
 *						    t + 2
Packit 6c4009
 *	    23.0   <  x <= INF    : tanhl(x) := 1.
Packit 6c4009
 *
Packit 6c4009
 * Special cases:
Packit 6c4009
 *	tanhl(NaN) is NaN;
Packit 6c4009
 *	only tanhl(0)=0 is exact for finite argument.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <float.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math-underflow.h>
Packit 6c4009
#include <libm-alias-ldouble.h>
Packit 6c4009
Packit 6c4009
static const long double one=1.0, two=2.0, tiny = 1.0e-4900L;
Packit 6c4009
Packit 6c4009
long double __tanhl(long double x)
Packit 6c4009
{
Packit 6c4009
	long double t,z;
Packit 6c4009
	int32_t se;
Packit 6c4009
	uint32_t j0,j1,ix;
Packit 6c4009
Packit 6c4009
    /* High word of |x|. */
Packit 6c4009
	GET_LDOUBLE_WORDS(se,j0,j1,x);
Packit 6c4009
	ix = se&0x7fff;
Packit 6c4009
Packit 6c4009
    /* x is INF or NaN */
Packit 6c4009
	if(ix==0x7fff) {
Packit 6c4009
	    /* for NaN it's not important which branch: tanhl(NaN) = NaN */
Packit 6c4009
	    if (se&0x8000) return one/x-one;	/* tanhl(-inf)= -1; */
Packit 6c4009
	    else           return one/x+one;	/* tanhl(+inf)=+1 */
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
    /* |x| < 23 */
Packit 6c4009
	if (ix < 0x4003 || (ix == 0x4003 && j0 < 0xb8000000u)) {/* |x|<23 */
Packit 6c4009
	    if ((ix|j0|j1) == 0)
Packit 6c4009
		return x;		/* x == +- 0 */
Packit 6c4009
	    if (ix<0x3fc8)		/* |x|<2**-55 */
Packit 6c4009
	      {
Packit 6c4009
		math_check_force_underflow (x);
Packit 6c4009
		return x*(one+tiny);	/* tanh(small) = small */
Packit 6c4009
	      }
Packit 6c4009
	    if (ix>=0x3fff) {	/* |x|>=1  */
Packit 6c4009
		t = __expm1l(two*fabsl(x));
Packit 6c4009
		z = one - two/(t+two);
Packit 6c4009
	    } else {
Packit 6c4009
	        t = __expm1l(-two*fabsl(x));
Packit 6c4009
	        z= -t/(t+two);
Packit 6c4009
	    }
Packit 6c4009
    /* |x| > 23, return +-1 */
Packit 6c4009
	} else {
Packit 6c4009
	    z = one - tiny;		/* raised inexact flag */
Packit 6c4009
	}
Packit 6c4009
	return (se&0x8000)? -z: z;
Packit 6c4009
}
Packit 6c4009
libm_alias_ldouble (__tanh, tanh)