Back to index

glibc  2.9
divtc3.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 __divtc3 (long double a, long double b, long double c, long double d)
00027 {
00028   long double denom, ratio, x, y;
00029 
00030   /* ??? We can get better behavior from logarithmic scaling instead of
00031      the division.  But that would mean starting to link libgcc against
00032      libm.  We could implement something akin to ldexp/frexp as gcc builtins
00033      fairly easily...  */
00034   if (fabsl (c) < fabsl (d))
00035     {
00036       ratio = c / d;
00037       denom = (c * ratio) + d;
00038       x = ((a * ratio) + b) / denom;
00039       y = ((b * ratio) - a) / denom;
00040     }
00041   else
00042     {
00043       ratio = d / c;
00044       denom = (d * ratio) + c;
00045       x = ((b * ratio) + a) / denom;
00046       y = (b - (a * ratio)) / denom;
00047     }
00048 
00049   /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
00050      are nonzero/zero, infinite/finite, and finite/infinite.  */
00051   if (isnan (x) && isnan (y))
00052     {
00053       if (denom == 0.0 && (!isnan (a) || !isnan (b)))
00054        {
00055          x = __copysignl (INFINITY, c) * a;
00056          y = __copysignl (INFINITY, c) * b;
00057        }
00058       else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
00059        {
00060          a = __copysignl (isinf (a) ? 1 : 0, a);
00061          b = __copysignl (isinf (b) ? 1 : 0, b);
00062          x = INFINITY * (a * c + b * d);
00063          y = INFINITY * (b * c - a * d);
00064        }
00065       else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
00066        {
00067          c = __copysignl (isinf (c) ? 1 : 0, c);
00068          d = __copysignl (isinf (d) ? 1 : 0, d);
00069          x = 0.0 * (a * c + b * d);
00070          y = 0.0 * (b * c - a * d);
00071        }
00072     }
00073 
00074   return x + I * y;
00075 }