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