]>
Commit | Line | Data |
---|---|---|
1dbc54f6 | 1 | /* Compute complex base 10 logarithm. |
581c785b | 2 | Copyright (C) 1997-2022 Free Software Foundation, Inc. |
1dbc54f6 | 3 | This file is part of the GNU C Library. |
1dbc54f6 PM |
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 | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
1dbc54f6 PM |
18 | |
19 | #include <complex.h> | |
20 | #include <math.h> | |
21 | #include <math_private.h> | |
8f5b00d3 | 22 | #include <math-underflow.h> |
1dbc54f6 PM |
23 | #include <float.h> |
24 | ||
25 | /* log_10 (2). */ | |
feb62dda | 26 | #define LOG10_2 M_LIT (0.3010299956639811952137388947244930267682) |
1dbc54f6 PM |
27 | |
28 | /* pi * log10 (e). */ | |
feb62dda | 29 | #define PI_LOG10E M_LIT (1.364376353841841347485783625431355770210) |
1dbc54f6 | 30 | |
feb62dda PM |
31 | CFLOAT |
32 | M_DECL_FUNC (__clog10) (CFLOAT x) | |
1dbc54f6 | 33 | { |
feb62dda | 34 | CFLOAT result; |
1dbc54f6 PM |
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. */ | |
feb62dda PM |
41 | __imag__ result = signbit (__real__ x) ? PI_LOG10E : 0; |
42 | __imag__ result = M_COPYSIGN (__imag__ result, __imag__ x); | |
1dbc54f6 | 43 | /* Yes, the following line raises an exception. */ |
feb62dda | 44 | __real__ result = -1 / M_FABS (__real__ x); |
1dbc54f6 PM |
45 | } |
46 | else if (__glibc_likely (rcls != FP_NAN && icls != FP_NAN)) | |
47 | { | |
48 | /* Neither real nor imaginary part is NaN. */ | |
feb62dda | 49 | FLOAT absx = M_FABS (__real__ x), absy = M_FABS (__imag__ x); |
1dbc54f6 PM |
50 | int scale = 0; |
51 | ||
52 | if (absx < absy) | |
53 | { | |
feb62dda | 54 | FLOAT t = absx; |
1dbc54f6 PM |
55 | absx = absy; |
56 | absy = t; | |
57 | } | |
58 | ||
feb62dda | 59 | if (absx > M_MAX / 2) |
1dbc54f6 PM |
60 | { |
61 | scale = -1; | |
feb62dda PM |
62 | absx = M_SCALBN (absx, scale); |
63 | absy = (absy >= M_MIN * 2 ? M_SCALBN (absy, scale) : 0); | |
1dbc54f6 | 64 | } |
feb62dda | 65 | else if (absx < M_MIN && absy < M_MIN) |
1dbc54f6 | 66 | { |
feb62dda PM |
67 | scale = M_MANT_DIG; |
68 | absx = M_SCALBN (absx, scale); | |
69 | absy = M_SCALBN (absy, scale); | |
1dbc54f6 PM |
70 | } |
71 | ||
feb62dda | 72 | if (absx == 1 && scale == 0) |
1dbc54f6 | 73 | { |
feb62dda | 74 | __real__ result = (M_LOG1P (absy * absy) |
347a5b59 | 75 | * (M_MLIT (M_LOG10E) / 2)); |
1dbc54f6 PM |
76 | math_check_force_underflow_nonneg (__real__ result); |
77 | } | |
feb62dda | 78 | else if (absx > 1 && absx < 2 && absy < 1 && scale == 0) |
1dbc54f6 | 79 | { |
feb62dda PM |
80 | FLOAT d2m1 = (absx - 1) * (absx + 1); |
81 | if (absy >= M_EPSILON) | |
1dbc54f6 | 82 | d2m1 += absy * absy; |
347a5b59 | 83 | __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2); |
1dbc54f6 | 84 | } |
feb62dda PM |
85 | else if (absx < 1 |
86 | && absx >= M_LIT (0.5) | |
87 | && absy < M_EPSILON / 2 | |
1dbc54f6 PM |
88 | && scale == 0) |
89 | { | |
feb62dda | 90 | FLOAT d2m1 = (absx - 1) * (absx + 1); |
347a5b59 | 91 | __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2); |
1dbc54f6 | 92 | } |
feb62dda PM |
93 | else if (absx < 1 |
94 | && absx >= M_LIT (0.5) | |
1dbc54f6 | 95 | && scale == 0 |
feb62dda | 96 | && absx * absx + absy * absy >= M_LIT (0.5)) |
1dbc54f6 | 97 | { |
feb62dda | 98 | FLOAT d2m1 = M_SUF (__x2y2m1) (absx, absy); |
347a5b59 | 99 | __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2); |
1dbc54f6 PM |
100 | } |
101 | else | |
102 | { | |
feb62dda PM |
103 | FLOAT d = M_HYPOT (absx, absy); |
104 | __real__ result = M_SUF (__ieee754_log10) (d) - scale * LOG10_2; | |
1dbc54f6 PM |
105 | } |
106 | ||
feb62dda | 107 | __imag__ result = M_MLIT (M_LOG10E) * M_ATAN2 (__imag__ x, __real__ x); |
1dbc54f6 PM |
108 | } |
109 | else | |
110 | { | |
feb62dda | 111 | __imag__ result = M_NAN; |
1dbc54f6 PM |
112 | if (rcls == FP_INFINITE || icls == FP_INFINITE) |
113 | /* Real or imaginary part is infinite. */ | |
feb62dda | 114 | __real__ result = M_HUGE_VAL; |
1dbc54f6 | 115 | else |
feb62dda | 116 | __real__ result = M_NAN; |
1dbc54f6 PM |
117 | } |
118 | ||
119 | return result; | |
120 | } | |
feb62dda PM |
121 | |
122 | declare_mgen_alias (__clog10, clog10) |