stdlib: qsort: Move some macros to inline function
[glibc.git] / math / s_clog10_template.c
blob3b512feb72f93529e0c2044fa64f88a05bd19e1e
1 /* Compute complex base 10 logarithm.
2 Copyright (C) 1997-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include <complex.h>
20 #include <math.h>
21 #include <math_private.h>
22 #include <math-underflow.h>
23 #include <float.h>
25 /* log_10 (2). */
26 #define LOG10_2 M_LIT (0.3010299956639811952137388947244930267682)
28 /* pi * log10 (e). */
29 #define PI_LOG10E M_LIT (1.364376353841841347485783625431355770210)
31 CFLOAT
32 M_DECL_FUNC (__clog10) (CFLOAT x)
34 CFLOAT result;
35 int rcls = fpclassify (__real__ x);
36 int icls = fpclassify (__imag__ x);
38 if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO))
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);
46 else if (__glibc_likely (rcls != FP_NAN && icls != FP_NAN))
48 /* Neither real nor imaginary part is NaN. */
49 FLOAT absx = M_FABS (__real__ x), absy = M_FABS (__imag__ x);
50 int scale = 0;
52 if (absx < absy)
54 FLOAT t = absx;
55 absx = absy;
56 absy = t;
59 if (absx > M_MAX / 2)
61 scale = -1;
62 absx = M_SCALBN (absx, scale);
63 absy = (absy >= M_MIN * 2 ? M_SCALBN (absy, scale) : 0);
65 else if (absx < M_MIN && absy < M_MIN)
67 scale = M_MANT_DIG;
68 absx = M_SCALBN (absx, scale);
69 absy = M_SCALBN (absy, scale);
72 if (absx == 1 && scale == 0)
74 __real__ result = (M_LOG1P (absy * absy)
75 * (M_MLIT (M_LOG10E) / 2));
76 math_check_force_underflow_nonneg (__real__ result);
78 else if (absx > 1 && absx < 2 && absy < 1 && scale == 0)
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);
85 else if (absx < 1
86 && absx >= M_LIT (0.5)
87 && absy < M_EPSILON / 2
88 && scale == 0)
90 FLOAT d2m1 = (absx - 1) * (absx + 1);
91 __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2);
93 else if (absx < 1
94 && absx >= M_LIT (0.5)
95 && scale == 0
96 && absx * absx + absy * absy >= M_LIT (0.5))
98 FLOAT d2m1 = M_SUF (__x2y2m1) (absx, absy);
99 __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2);
101 else
103 FLOAT d = M_HYPOT (absx, absy);
104 __real__ result = M_SUF (__ieee754_log10) (d) - scale * LOG10_2;
107 __imag__ result = M_MLIT (M_LOG10E) * M_ATAN2 (__imag__ x, __real__ x);
109 else
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;
119 return result;
122 declare_mgen_alias (__clog10, clog10)