Blame sysdeps/ieee754/dbl-64/s_tanh.c

Packit 6c4009
/* @(#)s_tanh.c 5.1 93/09/24 */
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: s_tanh.c,v 1.7 1995/05/10 20:48:22 jtc Exp $";
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Tanh(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. tanh(x) is defined to be -----------
Packit 6c4009
 *				       x    -x
Packit 6c4009
 *				      e  + e
Packit 6c4009
 *	1. reduce x to non-negative by tanh(-x) = -tanh(x).
Packit 6c4009
 *	2.  0      <= x <= 2**-55 : tanh(x) := x*(one+x)
Packit 6c4009
 *					        -t
Packit 6c4009
 *	    2**-55 <  x <=  1     : tanh(x) := -----; t = expm1(-2x)
Packit 6c4009
 *					       t + 2
Packit 6c4009
 *						     2
Packit 6c4009
 *	    1      <= x <=  22.0  : tanh(x) := 1-  ----- ; t=expm1(2x)
Packit 6c4009
 *						   t + 2
Packit 6c4009
 *	    22.0   <  x <= INF    : tanh(x) := 1.
Packit 6c4009
 *
Packit 6c4009
 * Special cases:
Packit 6c4009
 *	tanh(NaN) is NaN;
Packit 6c4009
 *	only tanh(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-double.h>
Packit 6c4009
Packit 6c4009
static const double one = 1.0, two = 2.0, tiny = 1.0e-300;
Packit 6c4009
Packit 6c4009
double
Packit 6c4009
__tanh (double x)
Packit 6c4009
{
Packit 6c4009
  double t, z;
Packit 6c4009
  int32_t jx, ix, lx;
Packit 6c4009
Packit 6c4009
  /* High word of |x|. */
Packit 6c4009
  EXTRACT_WORDS (jx, lx, x);
Packit 6c4009
  ix = jx & 0x7fffffff;
Packit 6c4009
Packit 6c4009
  /* x is INF or NaN */
Packit 6c4009
  if (ix >= 0x7ff00000)
Packit 6c4009
    {
Packit 6c4009
      if (jx >= 0)
Packit 6c4009
	return one / x + one;               /* tanh(+-inf)=+-1 */
Packit 6c4009
      else
Packit 6c4009
	return one / x - one;               /* tanh(NaN) = NaN */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* |x| < 22 */
Packit 6c4009
  if (ix < 0x40360000)                  /* |x|<22 */
Packit 6c4009
    {
Packit 6c4009
      if ((ix | lx) == 0)
Packit 6c4009
	return x;                       /* x == +-0 */
Packit 6c4009
      if (ix < 0x3c800000)              /* |x|<2**-55 */
Packit 6c4009
	{
Packit 6c4009
	  math_check_force_underflow (x);
Packit 6c4009
	  return x * (one + x);           /* tanh(small) = small */
Packit 6c4009
	}
Packit 6c4009
      if (ix >= 0x3ff00000)             /* |x|>=1  */
Packit 6c4009
	{
Packit 6c4009
	  t = __expm1 (two * fabs (x));
Packit 6c4009
	  z = one - two / (t + two);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  t = __expm1 (-two * fabs (x));
Packit 6c4009
	  z = -t / (t + two);
Packit 6c4009
	}
Packit 6c4009
      /* |x| > 22, return +-1 */
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      z = one - tiny;                   /* raised inexact flag */
Packit 6c4009
    }
Packit 6c4009
  return (jx >= 0) ? z : -z;
Packit 6c4009
}
Packit 6c4009
libm_alias_double (__tanh, tanh)