1 /* Compute complex base 10 logarithm.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <complex.h>
20 #include <math.h>
21 #include <math_private.h>
22 #include <math-underflow.h>
23 #include <float.h>
24
25 /* log_10 (2). */
26 #define LOG10_2 M_LIT (0.3010299956639811952137388947244930267682)
27
28 /* pi * log10 (e). */
29 #define PI_LOG10E M_LIT (1.364376353841841347485783625431355770210)
30
31 CFLOAT
M_DECL_FUNC(__clog10)32 M_DECL_FUNC (__clog10) (CFLOAT x)
33 {
34 CFLOAT result;
35 int rcls = fpclassify (__real__ x);
36 int icls = fpclassify (__imag__ x);
37
38 if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO))
39 {
40 /* Real and imaginary part are 0.0. */
41 __imag__ result = signbit (__real__ x) ? PI_LOG10E : 0;
42 __imag__ result = M_COPYSIGN (__imag__ result, __imag__ x);
43 /* Yes, the following line raises an exception. */
44 __real__ result = -1 / M_FABS (__real__ x);
45 }
46 else if (__glibc_likely (rcls != FP_NAN && icls != FP_NAN))
47 {
48 /* Neither real nor imaginary part is NaN. */
49 FLOAT absx = M_FABS (__real__ x), absy = M_FABS (__imag__ x);
50 int scale = 0;
51
52 if (absx < absy)
53 {
54 FLOAT t = absx;
55 absx = absy;
56 absy = t;
57 }
58
59 if (absx > M_MAX / 2)
60 {
61 scale = -1;
62 absx = M_SCALBN (absx, scale);
63 absy = (absy >= M_MIN * 2 ? M_SCALBN (absy, scale) : 0);
64 }
65 else if (absx < M_MIN && absy < M_MIN)
66 {
67 scale = M_MANT_DIG;
68 absx = M_SCALBN (absx, scale);
69 absy = M_SCALBN (absy, scale);
70 }
71
72 if (absx == 1 && scale == 0)
73 {
74 __real__ result = (M_LOG1P (absy * absy)
75 * (M_MLIT (M_LOG10E) / 2));
76 math_check_force_underflow_nonneg (__real__ result);
77 }
78 else if (absx > 1 && absx < 2 && absy < 1 && scale == 0)
79 {
80 FLOAT d2m1 = (absx - 1) * (absx + 1);
81 if (absy >= M_EPSILON)
82 d2m1 += absy * absy;
83 __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2);
84 }
85 else if (absx < 1
86 && absx >= M_LIT (0.5)
87 && absy < M_EPSILON / 2
88 && scale == 0)
89 {
90 FLOAT d2m1 = (absx - 1) * (absx + 1);
91 __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2);
92 }
93 else if (absx < 1
94 && absx >= M_LIT (0.5)
95 && scale == 0
96 && absx * absx + absy * absy >= M_LIT (0.5))
97 {
98 FLOAT d2m1 = M_SUF (__x2y2m1) (absx, absy);
99 __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2);
100 }
101 else
102 {
103 FLOAT d = M_HYPOT (absx, absy);
104 __real__ result = M_SUF (__ieee754_log10) (d) - scale * LOG10_2;
105 }
106
107 __imag__ result = M_MLIT (M_LOG10E) * M_ATAN2 (__imag__ x, __real__ x);
108 }
109 else
110 {
111 __imag__ result = M_NAN;
112 if (rcls == FP_INFINITE || icls == FP_INFINITE)
113 /* Real or imaginary part is infinite. */
114 __real__ result = M_HUGE_VAL;
115 else
116 __real__ result = M_NAN;
117 }
118
119 return result;
120 }
121
122 declare_mgen_alias (__clog10, clog10)
123