Blame math/divtc3.c

Packit Service 82fcde
/* Copyright (C) 2005-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
   Contributed by Richard Henderson <rth@redhat.com>, 2005.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <stdbool.h>
Packit Service 82fcde
#include <math.h>
Packit Service 82fcde
#include <complex.h>
Packit Service 82fcde
Packit Service 82fcde
attribute_hidden
Packit Service 82fcde
long double _Complex
Packit Service 82fcde
__divtc3 (long double a, long double b, long double c, long double d)
Packit Service 82fcde
{
Packit Service 82fcde
  long double denom, ratio, x, y;
Packit Service 82fcde
Packit Service 82fcde
  /* ??? We can get better behavior from logarithmic scaling instead of
Packit Service 82fcde
     the division.  But that would mean starting to link libgcc against
Packit Service 82fcde
     libm.  We could implement something akin to ldexp/frexp as gcc builtins
Packit Service 82fcde
     fairly easily...  */
Packit Service 82fcde
  if (fabsl (c) < fabsl (d))
Packit Service 82fcde
    {
Packit Service 82fcde
      ratio = c / d;
Packit Service 82fcde
      denom = (c * ratio) + d;
Packit Service 82fcde
      x = ((a * ratio) + b) / denom;
Packit Service 82fcde
      y = ((b * ratio) - a) / denom;
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      ratio = d / c;
Packit Service 82fcde
      denom = (d * ratio) + c;
Packit Service 82fcde
      x = ((b * ratio) + a) / denom;
Packit Service 82fcde
      y = (b - (a * ratio)) / denom;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
Packit Service 82fcde
     are nonzero/zero, infinite/finite, and finite/infinite.  */
Packit Service 82fcde
  if (isnan (x) && isnan (y))
Packit Service 82fcde
    {
Packit Service 82fcde
      if (denom == 0.0 && (!isnan (a) || !isnan (b)))
Packit Service 82fcde
	{
Packit Service 82fcde
	  x = __copysignl (INFINITY, c) * a;
Packit Service 82fcde
	  y = __copysignl (INFINITY, c) * b;
Packit Service 82fcde
	}
Packit Service 82fcde
      else if ((isinf (a) || isinf (b))
Packit Service 82fcde
	       && isfinite (c) && isfinite (d))
Packit Service 82fcde
	{
Packit Service 82fcde
	  a = __copysignl (isinf (a) ? 1 : 0, a);
Packit Service 82fcde
	  b = __copysignl (isinf (b) ? 1 : 0, b);
Packit Service 82fcde
	  x = INFINITY * (a * c + b * d);
Packit Service 82fcde
	  y = INFINITY * (b * c - a * d);
Packit Service 82fcde
	}
Packit Service 82fcde
      else if ((isinf (c) || isinf (d))
Packit Service 82fcde
	       && isfinite (a) && isfinite (b))
Packit Service 82fcde
	{
Packit Service 82fcde
	  c = __copysignl (isinf (c) ? 1 : 0, c);
Packit Service 82fcde
	  d = __copysignl (isinf (d) ? 1 : 0, d);
Packit Service 82fcde
	  x = 0.0 * (a * c + b * d);
Packit Service 82fcde
	  y = 0.0 * (b * c - a * d);
Packit Service 82fcde
	}
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  return x + I * y;
Packit Service 82fcde
}