xref: /netbsd-src/external/gpl3/gcc.old/dist/libquadmath/math/clog10q.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1*627f7eb2Smrg /* Compute complex base 10 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 /* log_10 (2).  */
23*627f7eb2Smrg #define LOG10_2 0.3010299956639811952137388947244930267682Q
24*627f7eb2Smrg 
25*627f7eb2Smrg /* pi * log10 (e).  */
26*627f7eb2Smrg #define PI_LOG10E 1.364376353841841347485783625431355770210Q
27*627f7eb2Smrg 
28*627f7eb2Smrg __complex128
clog10q(__complex128 x)29*627f7eb2Smrg clog10q (__complex128 x)
30*627f7eb2Smrg {
31*627f7eb2Smrg   __complex128 result;
32*627f7eb2Smrg   int rcls = fpclassifyq (__real__ x);
33*627f7eb2Smrg   int icls = fpclassifyq (__imag__ x);
34*627f7eb2Smrg 
35*627f7eb2Smrg   if (__glibc_unlikely (rcls == QUADFP_ZERO && icls == QUADFP_ZERO))
36*627f7eb2Smrg     {
37*627f7eb2Smrg       /* Real and imaginary part are 0.0.  */
38*627f7eb2Smrg       __imag__ result = signbitq (__real__ x) ? PI_LOG10E : 0;
39*627f7eb2Smrg       __imag__ result = copysignq (__imag__ result, __imag__ x);
40*627f7eb2Smrg       /* Yes, the following line raises an exception.  */
41*627f7eb2Smrg       __real__ result = -1 / fabsq (__real__ x);
42*627f7eb2Smrg     }
43*627f7eb2Smrg   else if (__glibc_likely (rcls != QUADFP_NAN && icls != QUADFP_NAN))
44*627f7eb2Smrg     {
45*627f7eb2Smrg       /* Neither real nor imaginary part is NaN.  */
46*627f7eb2Smrg       __float128 absx = fabsq (__real__ x), absy = fabsq (__imag__ x);
47*627f7eb2Smrg       int scale = 0;
48*627f7eb2Smrg 
49*627f7eb2Smrg       if (absx < absy)
50*627f7eb2Smrg 	{
51*627f7eb2Smrg 	  __float128 t = absx;
52*627f7eb2Smrg 	  absx = absy;
53*627f7eb2Smrg 	  absy = t;
54*627f7eb2Smrg 	}
55*627f7eb2Smrg 
56*627f7eb2Smrg       if (absx > FLT128_MAX / 2)
57*627f7eb2Smrg 	{
58*627f7eb2Smrg 	  scale = -1;
59*627f7eb2Smrg 	  absx = scalbnq (absx, scale);
60*627f7eb2Smrg 	  absy = (absy >= FLT128_MIN * 2 ? scalbnq (absy, scale) : 0);
61*627f7eb2Smrg 	}
62*627f7eb2Smrg       else if (absx < FLT128_MIN && absy < FLT128_MIN)
63*627f7eb2Smrg 	{
64*627f7eb2Smrg 	  scale = FLT128_MANT_DIG;
65*627f7eb2Smrg 	  absx = scalbnq (absx, scale);
66*627f7eb2Smrg 	  absy = scalbnq (absy, scale);
67*627f7eb2Smrg 	}
68*627f7eb2Smrg 
69*627f7eb2Smrg       if (absx == 1 && scale == 0)
70*627f7eb2Smrg 	{
71*627f7eb2Smrg 	  __real__ result = (log1pq (absy * absy)
72*627f7eb2Smrg 			     * ((__float128) M_LOG10Eq / 2));
73*627f7eb2Smrg 	  math_check_force_underflow_nonneg (__real__ result);
74*627f7eb2Smrg 	}
75*627f7eb2Smrg       else if (absx > 1 && absx < 2 && absy < 1 && scale == 0)
76*627f7eb2Smrg 	{
77*627f7eb2Smrg 	  __float128 d2m1 = (absx - 1) * (absx + 1);
78*627f7eb2Smrg 	  if (absy >= FLT128_EPSILON)
79*627f7eb2Smrg 	    d2m1 += absy * absy;
80*627f7eb2Smrg 	  __real__ result = log1pq (d2m1) * ((__float128) M_LOG10Eq / 2);
81*627f7eb2Smrg 	}
82*627f7eb2Smrg       else if (absx < 1
83*627f7eb2Smrg 	       && absx >= 0.5Q
84*627f7eb2Smrg 	       && absy < FLT128_EPSILON / 2
85*627f7eb2Smrg 	       && scale == 0)
86*627f7eb2Smrg 	{
87*627f7eb2Smrg 	  __float128 d2m1 = (absx - 1) * (absx + 1);
88*627f7eb2Smrg 	  __real__ result = log1pq (d2m1) * ((__float128) M_LOG10Eq / 2);
89*627f7eb2Smrg 	}
90*627f7eb2Smrg       else if (absx < 1
91*627f7eb2Smrg 	       && absx >= 0.5Q
92*627f7eb2Smrg 	       && scale == 0
93*627f7eb2Smrg 	       && absx * absx + absy * absy >= 0.5Q)
94*627f7eb2Smrg 	{
95*627f7eb2Smrg 	  __float128 d2m1 = __quadmath_x2y2m1q (absx, absy);
96*627f7eb2Smrg 	  __real__ result = log1pq (d2m1) * ((__float128) M_LOG10Eq / 2);
97*627f7eb2Smrg 	}
98*627f7eb2Smrg       else
99*627f7eb2Smrg 	{
100*627f7eb2Smrg 	  __float128 d = hypotq (absx, absy);
101*627f7eb2Smrg 	  __real__ result = log10q (d) - scale * LOG10_2;
102*627f7eb2Smrg 	}
103*627f7eb2Smrg 
104*627f7eb2Smrg       __imag__ result = M_LOG10Eq * atan2q (__imag__ x, __real__ x);
105*627f7eb2Smrg     }
106*627f7eb2Smrg   else
107*627f7eb2Smrg     {
108*627f7eb2Smrg       __imag__ result = nanq ("");
109*627f7eb2Smrg       if (rcls == QUADFP_INFINITE || icls == QUADFP_INFINITE)
110*627f7eb2Smrg 	/* Real or imaginary part is infinite.  */
111*627f7eb2Smrg 	__real__ result = HUGE_VALQ;
112*627f7eb2Smrg       else
113*627f7eb2Smrg 	__real__ result = nanq ("");
114*627f7eb2Smrg     }
115*627f7eb2Smrg 
116*627f7eb2Smrg   return result;
117*627f7eb2Smrg }
118