Back to index

glibc  2.9
multc3.c
Go to the documentation of this file.
00001 /* Copyright (C) 2005, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Richard Henderson <rth@redhat.com>, 2005.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdbool.h>
00021 #include <math.h>
00022 #include <complex.h>
00023 
00024 attribute_hidden
00025 long double _Complex
00026 __multc3 (long double a, long double b, long double c, long double d)
00027 {
00028   long double ac, bd, ad, bc, x, y;
00029 
00030   ac = a * c;
00031   bd = b * d;
00032   ad = a * d;
00033   bc = b * c;
00034 
00035   x = ac - bd;
00036   y = ad + bc;
00037 
00038   if (isnan (x) && isnan (y))
00039     {
00040       /* Recover infinities that computed as NaN + iNaN.  */
00041       bool recalc = 0;
00042       if (isinf (a) || isinf (b))
00043        {
00044          /* z is infinite.  "Box" the infinity and change NaNs in
00045             the other factor to 0.  */
00046          a = __copysignl (isinf (a) ? 1 : 0, a);
00047          b = __copysignl (isinf (b) ? 1 : 0, b);
00048          if (isnan (c)) c = __copysignl (0, c);
00049          if (isnan (d)) d = __copysignl (0, d);
00050          recalc = 1;
00051        }
00052      if (isinf (c) || isinf (d))
00053        {
00054          /* w is infinite.  "Box" the infinity and change NaNs in
00055             the other factor to 0.  */
00056          c = __copysignl (isinf (c) ? 1 : 0, c);
00057          d = __copysignl (isinf (d) ? 1 : 0, d);
00058          if (isnan (a)) a = __copysignl (0, a);
00059          if (isnan (b)) b = __copysignl (0, b);
00060          recalc = 1;
00061        }
00062      if (!recalc
00063          && (isinf (ac) || isinf (bd) || isinf (ad) || isinf (bc)))
00064        {
00065          /* Recover infinities from overflow by changing NaNs to 0.  */
00066          if (isnan (a)) a = __copysignl (0, a);
00067          if (isnan (b)) b = __copysignl (0, b);
00068          if (isnan (c)) c = __copysignl (0, c);
00069          if (isnan (d)) d = __copysignl (0, d);
00070          recalc = 1;
00071        }
00072       if (recalc)
00073        {
00074          x = INFINITY * (a * c - b * d);
00075          y = INFINITY * (a * d + b * c);
00076        }
00077     }
00078 
00079   return x + I * y;
00080 }