xref: /netbsd-src/external/gpl3/gcc.old/dist/libquadmath/math/clogq.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1*627f7eb2Smrg /* Compute complex natural logarithm.
2*627f7eb2Smrg    Copyright (C) 1997-2018 Free Software Foundation, Inc.
3*627f7eb2Smrg    This file is part of the GNU C Library.
4*627f7eb2Smrg    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5*627f7eb2Smrg 
6*627f7eb2Smrg    The GNU C Library is free software; you can redistribute it and/or
7*627f7eb2Smrg    modify it under the terms of the GNU Lesser General Public
8*627f7eb2Smrg    License as published by the Free Software Foundation; either
9*627f7eb2Smrg    version 2.1 of the License, or (at your option) any later version.
10*627f7eb2Smrg 
11*627f7eb2Smrg    The GNU C Library is distributed in the hope that it will be useful,
12*627f7eb2Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*627f7eb2Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*627f7eb2Smrg    Lesser General Public License for more details.
15*627f7eb2Smrg 
16*627f7eb2Smrg    You should have received a copy of the GNU Lesser General Public
17*627f7eb2Smrg    License along with the GNU C Library; if not, see
18*627f7eb2Smrg    <http://www.gnu.org/licenses/>.  */
19*627f7eb2Smrg 
20*627f7eb2Smrg #include "quadmath-imp.h"
21*627f7eb2Smrg 
22*627f7eb2Smrg __complex128
clogq(__complex128 x)23*627f7eb2Smrg clogq (__complex128 x)
24*627f7eb2Smrg {
25*627f7eb2Smrg   __complex128 result;
26*627f7eb2Smrg   int rcls = fpclassifyq (__real__ x);
27*627f7eb2Smrg   int icls = fpclassifyq (__imag__ x);
28*627f7eb2Smrg 
29*627f7eb2Smrg   if (__glibc_unlikely (rcls == QUADFP_ZERO && icls == QUADFP_ZERO))
30*627f7eb2Smrg     {
31*627f7eb2Smrg       /* Real and imaginary part are 0.0.  */
32*627f7eb2Smrg       __imag__ result = signbitq (__real__ x) ? (__float128) M_PIq : 0;
33*627f7eb2Smrg       __imag__ result = copysignq (__imag__ result, __imag__ x);
34*627f7eb2Smrg       /* Yes, the following line raises an exception.  */
35*627f7eb2Smrg       __real__ result = -1 / fabsq (__real__ x);
36*627f7eb2Smrg     }
37*627f7eb2Smrg   else if (__glibc_likely (rcls != QUADFP_NAN && icls != QUADFP_NAN))
38*627f7eb2Smrg     {
39*627f7eb2Smrg       /* Neither real nor imaginary part is NaN.  */
40*627f7eb2Smrg       __float128 absx = fabsq (__real__ x), absy = fabsq (__imag__ x);
41*627f7eb2Smrg       int scale = 0;
42*627f7eb2Smrg 
43*627f7eb2Smrg       if (absx < absy)
44*627f7eb2Smrg 	{
45*627f7eb2Smrg 	  __float128 t = absx;
46*627f7eb2Smrg 	  absx = absy;
47*627f7eb2Smrg 	  absy = t;
48*627f7eb2Smrg 	}
49*627f7eb2Smrg 
50*627f7eb2Smrg       if (absx > FLT128_MAX / 2)
51*627f7eb2Smrg 	{
52*627f7eb2Smrg 	  scale = -1;
53*627f7eb2Smrg 	  absx = scalbnq (absx, scale);
54*627f7eb2Smrg 	  absy = (absy >= FLT128_MIN * 2 ? scalbnq (absy, scale) : 0);
55*627f7eb2Smrg 	}
56*627f7eb2Smrg       else if (absx < FLT128_MIN && absy < FLT128_MIN)
57*627f7eb2Smrg 	{
58*627f7eb2Smrg 	  scale = FLT128_MANT_DIG;
59*627f7eb2Smrg 	  absx = scalbnq (absx, scale);
60*627f7eb2Smrg 	  absy = scalbnq (absy, scale);
61*627f7eb2Smrg 	}
62*627f7eb2Smrg 
63*627f7eb2Smrg       if (absx == 1 && scale == 0)
64*627f7eb2Smrg 	{
65*627f7eb2Smrg 	  __real__ result = log1pq (absy * absy) / 2;
66*627f7eb2Smrg 	  math_check_force_underflow_nonneg (__real__ result);
67*627f7eb2Smrg 	}
68*627f7eb2Smrg       else if (absx > 1 && absx < 2 && absy < 1 && scale == 0)
69*627f7eb2Smrg 	{
70*627f7eb2Smrg 	  __float128 d2m1 = (absx - 1) * (absx + 1);
71*627f7eb2Smrg 	  if (absy >= FLT128_EPSILON)
72*627f7eb2Smrg 	    d2m1 += absy * absy;
73*627f7eb2Smrg 	  __real__ result = log1pq (d2m1) / 2;
74*627f7eb2Smrg 	}
75*627f7eb2Smrg       else if (absx < 1
76*627f7eb2Smrg 	       && absx >= 0.5Q
77*627f7eb2Smrg 	       && absy < FLT128_EPSILON / 2
78*627f7eb2Smrg 	       && scale == 0)
79*627f7eb2Smrg 	{
80*627f7eb2Smrg 	  __float128 d2m1 = (absx - 1) * (absx + 1);
81*627f7eb2Smrg 	  __real__ result = log1pq (d2m1) / 2;
82*627f7eb2Smrg 	}
83*627f7eb2Smrg       else if (absx < 1
84*627f7eb2Smrg 	       && absx >= 0.5Q
85*627f7eb2Smrg 	       && scale == 0
86*627f7eb2Smrg 	       && absx * absx + absy * absy >= 0.5Q)
87*627f7eb2Smrg 	{
88*627f7eb2Smrg 	  __float128 d2m1 = __quadmath_x2y2m1q (absx, absy);
89*627f7eb2Smrg 	  __real__ result = log1pq (d2m1) / 2;
90*627f7eb2Smrg 	}
91*627f7eb2Smrg       else
92*627f7eb2Smrg 	{
93*627f7eb2Smrg 	  __float128 d = hypotq (absx, absy);
94*627f7eb2Smrg 	  __real__ result = logq (d) - scale * (__float128) M_LN2q;
95*627f7eb2Smrg 	}
96*627f7eb2Smrg 
97*627f7eb2Smrg       __imag__ result = atan2q (__imag__ x, __real__ x);
98*627f7eb2Smrg     }
99*627f7eb2Smrg   else
100*627f7eb2Smrg     {
101*627f7eb2Smrg       __imag__ result = nanq ("");
102*627f7eb2Smrg       if (rcls == QUADFP_INFINITE || icls == QUADFP_INFINITE)
103*627f7eb2Smrg 	/* Real or imaginary part is infinite.  */
104*627f7eb2Smrg 	__real__ result = HUGE_VALQ;
105*627f7eb2Smrg       else
106*627f7eb2Smrg 	__real__ result = nanq ("");
107*627f7eb2Smrg     }
108*627f7eb2Smrg 
109*627f7eb2Smrg   return result;
110*627f7eb2Smrg }
111