Blame math/s_ctanh_template.c

Packit 6c4009
/* Complex hyperbolic tangent for float types.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <complex.h>
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math-underflow.h>
Packit 6c4009
#include <float.h>
Packit 6c4009
Packit 6c4009
CFLOAT
Packit 6c4009
M_DECL_FUNC (__ctanh) (CFLOAT x)
Packit 6c4009
{
Packit 6c4009
  CFLOAT res;
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (!isfinite (__real__ x) || !isfinite (__imag__ x)))
Packit 6c4009
    {
Packit 6c4009
      if (isinf (__real__ x))
Packit 6c4009
	{
Packit 6c4009
	  __real__ res = M_COPYSIGN (1, __real__ x);
Packit 6c4009
	  if (isfinite (__imag__ x) && M_FABS (__imag__ x) > 1)
Packit 6c4009
	    {
Packit 6c4009
	      FLOAT sinix, cosix;
Packit 6c4009
	      M_SINCOS (__imag__ x, &sinix, &cosix);
Packit 6c4009
	      __imag__ res = M_COPYSIGN (0, sinix * cosix);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    __imag__ res = M_COPYSIGN (0, __imag__ x);
Packit 6c4009
	}
Packit 6c4009
      else if (__imag__ x == 0)
Packit 6c4009
	{
Packit 6c4009
	  res = x;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  if (__real__ x == 0)
Packit 6c4009
	    __real__ res = __real__ x;
Packit 6c4009
	  else
Packit 6c4009
	    __real__ res = M_NAN;
Packit 6c4009
	  __imag__ res = M_NAN;
Packit 6c4009
Packit 6c4009
	  if (isinf (__imag__ x))
Packit 6c4009
	    feraiseexcept (FE_INVALID);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      FLOAT sinix, cosix;
Packit 6c4009
      FLOAT den;
Packit 6c4009
      const int t = (int) ((M_MAX_EXP - 1) * M_MLIT (M_LN2) / 2);
Packit 6c4009
Packit 6c4009
      /* tanh(x+iy) = (sinh(2x) + i*sin(2y))/(cosh(2x) + cos(2y))
Packit 6c4009
	 = (sinh(x)*cosh(x) + i*sin(y)*cos(y))/(sinh(x)^2 + cos(y)^2).  */
Packit 6c4009
Packit 6c4009
      if (__glibc_likely (M_FABS (__imag__ x) > M_MIN))
Packit 6c4009
	{
Packit 6c4009
	  M_SINCOS (__imag__ x, &sinix, &cosix);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  sinix = __imag__ x;
Packit 6c4009
	  cosix = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (M_FABS (__real__ x) > t)
Packit 6c4009
	{
Packit 6c4009
	  /* Avoid intermediate overflow when the imaginary part of
Packit 6c4009
	     the result may be subnormal.  Ignoring negligible terms,
Packit 6c4009
	     the real part is +/- 1, the imaginary part is
Packit 6c4009
	     sin(y)*cos(y)/sinh(x)^2 = 4*sin(y)*cos(y)/exp(2x).  */
Packit 6c4009
	  FLOAT exp_2t = M_EXP (2 * t);
Packit 6c4009
Packit 6c4009
	  __real__ res = M_COPYSIGN (1, __real__ x);
Packit 6c4009
	  __imag__ res = 4 * sinix * cosix;
Packit 6c4009
	  __real__ x = M_FABS (__real__ x);
Packit 6c4009
	  __real__ x -= t;
Packit 6c4009
	  __imag__ res /= exp_2t;
Packit 6c4009
	  if (__real__ x > t)
Packit 6c4009
	    {
Packit 6c4009
	      /* Underflow (original real part of x has absolute value
Packit 6c4009
		 > 2t).  */
Packit 6c4009
	      __imag__ res /= exp_2t;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    __imag__ res /= M_EXP (2 * __real__ x);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  FLOAT sinhrx, coshrx;
Packit 6c4009
	  if (M_FABS (__real__ x) > M_MIN)
Packit 6c4009
	    {
Packit 6c4009
	      sinhrx = M_SINH (__real__ x);
Packit 6c4009
	      coshrx = M_COSH (__real__ x);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      sinhrx = __real__ x;
Packit 6c4009
	      coshrx = 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (M_FABS (sinhrx) > M_FABS (cosix) * M_EPSILON)
Packit 6c4009
	    den = sinhrx * sinhrx + cosix * cosix;
Packit 6c4009
	  else
Packit 6c4009
	    den = cosix * cosix;
Packit 6c4009
	  __real__ res = sinhrx * coshrx / den;
Packit 6c4009
	  __imag__ res = sinix * cosix / den;
Packit 6c4009
	}
Packit 6c4009
      math_check_force_underflow_complex (res);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return res;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
declare_mgen_alias (__ctanh, ctanh)