Blame math/multc3.c

Packit 6c4009
/* Copyright (C) 2005-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Richard Henderson <rth@redhat.com>, 2005.
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 <stdbool.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <complex.h>
Packit 6c4009
Packit 6c4009
attribute_hidden
Packit 6c4009
long double _Complex
Packit 6c4009
__multc3 (long double a, long double b, long double c, long double d)
Packit 6c4009
{
Packit 6c4009
  long double ac, bd, ad, bc, x, y;
Packit 6c4009
Packit 6c4009
  ac = a * c;
Packit 6c4009
  bd = b * d;
Packit 6c4009
  ad = a * d;
Packit 6c4009
  bc = b * c;
Packit 6c4009
Packit 6c4009
  x = ac - bd;
Packit 6c4009
  y = ad + bc;
Packit 6c4009
Packit 6c4009
  if (isnan (x) && isnan (y))
Packit 6c4009
    {
Packit 6c4009
      /* Recover infinities that computed as NaN + iNaN.  */
Packit 6c4009
      bool recalc = 0;
Packit 6c4009
      if (isinf (a) || isinf (b))
Packit 6c4009
	{
Packit 6c4009
	  /* z is infinite.  "Box" the infinity and change NaNs in
Packit 6c4009
	     the other factor to 0.  */
Packit 6c4009
	  a = __copysignl (isinf (a) ? 1 : 0, a);
Packit 6c4009
	  b = __copysignl (isinf (b) ? 1 : 0, b);
Packit 6c4009
	  if (isnan (c)) c = __copysignl (0, c);
Packit 6c4009
	  if (isnan (d)) d = __copysignl (0, d);
Packit 6c4009
	  recalc = 1;
Packit 6c4009
	}
Packit 6c4009
     if (isinf (c) || isinf (d))
Packit 6c4009
	{
Packit 6c4009
	  /* w is infinite.  "Box" the infinity and change NaNs in
Packit 6c4009
	     the other factor to 0.  */
Packit 6c4009
	  c = __copysignl (isinf (c) ? 1 : 0, c);
Packit 6c4009
	  d = __copysignl (isinf (d) ? 1 : 0, d);
Packit 6c4009
	  if (isnan (a)) a = __copysignl (0, a);
Packit 6c4009
	  if (isnan (b)) b = __copysignl (0, b);
Packit 6c4009
	  recalc = 1;
Packit 6c4009
	}
Packit 6c4009
     if (!recalc
Packit 6c4009
	  && (isinf (ac) || isinf (bd)
Packit 6c4009
	      || isinf (ad) || isinf (bc)))
Packit 6c4009
	{
Packit 6c4009
	  /* Recover infinities from overflow by changing NaNs to 0.  */
Packit 6c4009
	  if (isnan (a)) a = __copysignl (0, a);
Packit 6c4009
	  if (isnan (b)) b = __copysignl (0, b);
Packit 6c4009
	  if (isnan (c)) c = __copysignl (0, c);
Packit 6c4009
	  if (isnan (d)) d = __copysignl (0, d);
Packit 6c4009
	  recalc = 1;
Packit 6c4009
	}
Packit 6c4009
      if (recalc)
Packit 6c4009
	{
Packit 6c4009
	  x = INFINITY * (a * c - b * d);
Packit 6c4009
	  y = INFINITY * (a * d + b * c);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return x + I * y;
Packit 6c4009
}