Blame math/s_ctan_template.c

Packit 6c4009
/* Complex tangent function for a complex float type.
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 (__ctan) (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 (__imag__ x))
Packit 6c4009
	{
Packit 6c4009
	  if (isfinite (__real__ x) && M_FABS (__real__ x) > 1)
Packit 6c4009
	    {
Packit 6c4009
	      FLOAT sinrx, cosrx;
Packit 6c4009
	      M_SINCOS (__real__ x, &sinrx, &cosrx);
Packit 6c4009
	      __real__ res = M_COPYSIGN (0, sinrx * cosrx);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    __real__ res = M_COPYSIGN (0, __real__ x);
Packit 6c4009
	  __imag__ res = M_COPYSIGN (1, __imag__ x);
Packit 6c4009
	}
Packit 6c4009
      else if (__real__ x == 0)
Packit 6c4009
	{
Packit 6c4009
	  res = x;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  __real__ res = M_NAN;
Packit 6c4009
	  if (__imag__ x == 0)
Packit 6c4009
	    __imag__ res = __imag__ x;
Packit 6c4009
	  else
Packit 6c4009
	    __imag__ res = M_NAN;
Packit 6c4009
Packit 6c4009
	  if (isinf (__real__ x))
Packit 6c4009
	    feraiseexcept (FE_INVALID);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      FLOAT sinrx, cosrx;
Packit 6c4009
      FLOAT den;
Packit 6c4009
      const int t = (int) ((M_MAX_EXP - 1) * M_MLIT (M_LN2) / 2);
Packit 6c4009
Packit 6c4009
      /* tan(x+iy) = (sin(2x) + i*sinh(2y))/(cos(2x) + cosh(2y))
Packit 6c4009
	 = (sin(x)*cos(x) + i*sinh(y)*cosh(y)/(cos(x)^2 + sinh(y)^2). */
Packit 6c4009
Packit 6c4009
      if (__glibc_likely (M_FABS (__real__ x) > M_MIN))
Packit 6c4009
	{
Packit 6c4009
	  M_SINCOS (__real__ x, &sinrx, &cosrx);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  sinrx = __real__ x;
Packit 6c4009
	  cosrx = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (M_FABS (__imag__ x) > t)
Packit 6c4009
	{
Packit 6c4009
	  /* Avoid intermediate overflow when the real part of the
Packit 6c4009
	     result may be subnormal.  Ignoring negligible terms, the
Packit 6c4009
	     imaginary part is +/- 1, the real part is
Packit 6c4009
	     sin(x)*cos(x)/sinh(y)^2 = 4*sin(x)*cos(x)/exp(2y).  */
Packit 6c4009
	  FLOAT exp_2t = M_EXP (2 * t);
Packit 6c4009
Packit 6c4009
	  __imag__ res = M_COPYSIGN (1, __imag__ x);
Packit 6c4009
	  __real__ res = 4 * sinrx * cosrx;
Packit 6c4009
	  __imag__ x = M_FABS (__imag__ x);
Packit 6c4009
	  __imag__ x -= t;
Packit 6c4009
	  __real__ res /= exp_2t;
Packit 6c4009
	  if (__imag__ x > t)
Packit 6c4009
	    {
Packit 6c4009
	      /* Underflow (original imaginary part of x has absolute
Packit 6c4009
		 value > 2t).  */
Packit 6c4009
	      __real__ res /= exp_2t;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    __real__ res /= M_EXP (2 * __imag__ x);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  FLOAT sinhix, coshix;
Packit 6c4009
	  if (M_FABS (__imag__ x) > M_MIN)
Packit 6c4009
	    {
Packit 6c4009
	      sinhix = M_SINH (__imag__ x);
Packit 6c4009
	      coshix = M_COSH (__imag__ x);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      sinhix = __imag__ x;
Packit 6c4009
	      coshix = 1;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (M_FABS (sinhix) > M_FABS (cosrx) * M_EPSILON)
Packit 6c4009
	    den = cosrx * cosrx + sinhix * sinhix;
Packit 6c4009
	  else
Packit 6c4009
	    den = cosrx * cosrx;
Packit 6c4009
	  __real__ res = sinrx * cosrx / den;
Packit 6c4009
	  __imag__ res = sinhix * coshix / 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 (__ctan, ctan)