Blame math/s_csqrt_template.c

Packit 6c4009
/* Complex square root of a 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
   Based on an algorithm by Stephen L. Moshier <moshier@world.std.com>.
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 <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 (__csqrt) (CFLOAT x)
Packit 6c4009
{
Packit 6c4009
  CFLOAT res;
Packit 6c4009
  int rcls = fpclassify (__real__ x);
Packit 6c4009
  int icls = fpclassify (__imag__ x);
Packit 6c4009
Packit 6c4009
  if (__glibc_unlikely (rcls <= FP_INFINITE || icls <= FP_INFINITE))
Packit 6c4009
    {
Packit 6c4009
      if (icls == FP_INFINITE)
Packit 6c4009
	{
Packit 6c4009
	  __real__ res = M_HUGE_VAL;
Packit 6c4009
	  __imag__ res = __imag__ x;
Packit 6c4009
	}
Packit 6c4009
      else if (rcls == FP_INFINITE)
Packit 6c4009
	{
Packit 6c4009
	  if (__real__ x < 0)
Packit 6c4009
	    {
Packit 6c4009
	      __real__ res = icls == FP_NAN ? M_NAN : 0;
Packit 6c4009
	      __imag__ res = M_COPYSIGN (M_HUGE_VAL, __imag__ x);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      __real__ res = __real__ x;
Packit 6c4009
	      __imag__ res = (icls == FP_NAN
Packit 6c4009
			      ? M_NAN : M_COPYSIGN (0, __imag__ x));
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  __real__ res = M_NAN;
Packit 6c4009
	  __imag__ res = M_NAN;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (__glibc_unlikely (icls == FP_ZERO))
Packit 6c4009
	{
Packit 6c4009
	  if (__real__ x < 0)
Packit 6c4009
	    {
Packit 6c4009
	      __real__ res = 0;
Packit 6c4009
	      __imag__ res = M_COPYSIGN (M_SQRT (-__real__ x), __imag__ x);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      __real__ res = M_FABS (M_SQRT (__real__ x));
Packit 6c4009
	      __imag__ res = M_COPYSIGN (0, __imag__ x);
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else if (__glibc_unlikely (rcls == FP_ZERO))
Packit 6c4009
	{
Packit 6c4009
	  FLOAT r;
Packit 6c4009
	  if (M_FABS (__imag__ x) >= 2 * M_MIN)
Packit 6c4009
	    r = M_SQRT (M_LIT (0.5) * M_FABS (__imag__ x));
Packit 6c4009
	  else
Packit 6c4009
	    r = M_LIT (0.5) * M_SQRT (2 * M_FABS (__imag__ x));
Packit 6c4009
Packit 6c4009
	  __real__ res = r;
Packit 6c4009
	  __imag__ res = M_COPYSIGN (r, __imag__ x);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  FLOAT d, r, s;
Packit 6c4009
	  int scale = 0;
Packit 6c4009
Packit 6c4009
	  if (M_FABS (__real__ x) > M_MAX / 4)
Packit 6c4009
	    {
Packit 6c4009
	      scale = 1;
Packit 6c4009
	      __real__ x = M_SCALBN (__real__ x, -2 * scale);
Packit 6c4009
	      __imag__ x = M_SCALBN (__imag__ x, -2 * scale);
Packit 6c4009
	    }
Packit 6c4009
	  else if (M_FABS (__imag__ x) > M_MAX / 4)
Packit 6c4009
	    {
Packit 6c4009
	      scale = 1;
Packit 6c4009
	      if (M_FABS (__real__ x) >= 4 * M_MIN)
Packit 6c4009
		__real__ x = M_SCALBN (__real__ x, -2 * scale);
Packit 6c4009
	      else
Packit 6c4009
		__real__ x = 0;
Packit 6c4009
	      __imag__ x = M_SCALBN (__imag__ x, -2 * scale);
Packit 6c4009
	    }
Packit 6c4009
	  else if (M_FABS (__real__ x) < 2 * M_MIN
Packit 6c4009
		   && M_FABS (__imag__ x) < 2 * M_MIN)
Packit 6c4009
	    {
Packit 6c4009
	      scale = -((M_MANT_DIG + 1) / 2);
Packit 6c4009
	      __real__ x = M_SCALBN (__real__ x, -2 * scale);
Packit 6c4009
	      __imag__ x = M_SCALBN (__imag__ x, -2 * scale);
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  d = M_HYPOT (__real__ x, __imag__ x);
Packit 6c4009
	  /* Use the identity   2  Re res  Im res = Im x
Packit 6c4009
	     to avoid cancellation error in  d +/- Re x.  */
Packit 6c4009
	  if (__real__ x > 0)
Packit 6c4009
	    {
Packit 6c4009
	      r = M_SQRT (M_LIT (0.5) * (d + __real__ x));
Packit 6c4009
	      if (scale == 1 && M_FABS (__imag__ x) < 1)
Packit 6c4009
		{
Packit 6c4009
		  /* Avoid possible intermediate underflow.  */
Packit 6c4009
		  s = __imag__ x / r;
Packit 6c4009
		  r = M_SCALBN (r, scale);
Packit 6c4009
		  scale = 0;
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		s = M_LIT (0.5) * (__imag__ x / r);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      s = M_SQRT (M_LIT (0.5) * (d - __real__ x));
Packit 6c4009
	      if (scale == 1 && M_FABS (__imag__ x) < 1)
Packit 6c4009
		{
Packit 6c4009
		  /* Avoid possible intermediate underflow.  */
Packit 6c4009
		  r = M_FABS (__imag__ x / s);
Packit 6c4009
		  s = M_SCALBN (s, scale);
Packit 6c4009
		  scale = 0;
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		r = M_FABS (M_LIT (0.5) * (__imag__ x / s));
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  if (scale)
Packit 6c4009
	    {
Packit 6c4009
	      r = M_SCALBN (r, scale);
Packit 6c4009
	      s = M_SCALBN (s, scale);
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  math_check_force_underflow (r);
Packit 6c4009
	  math_check_force_underflow (s);
Packit 6c4009
Packit 6c4009
	  __real__ res = r;
Packit 6c4009
	  __imag__ res = M_COPYSIGN (s, __imag__ x);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return res;
Packit 6c4009
}
Packit 6c4009
declare_mgen_alias (__csqrt, csqrt)