blob: 8481e45d4ecb7faccc4c6f63e113462216a172ff [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Compute complex base 10 logarithm.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20#include <complex.h>
21#include <math.h>
22#include <math_private.h>
23#include <float.h>
24
25/* To avoid spurious underflows, use this definition to treat IBM long
26 double as approximating an IEEE-style format. */
27#if LDBL_MANT_DIG == 106
28# undef LDBL_EPSILON
29# define LDBL_EPSILON 0x1p-106L
30#endif
31
32/* log_10 (2). */
33#define M_LOG10_2l 0.3010299956639811952137388947244930267682L
34
35/* pi * log10 (e). */
36#define M_PI_LOG10El 1.364376353841841347485783625431355770210L
37
38__complex__ long double
39__clog10l (__complex__ long double x)
40{
41 __complex__ long double result;
42 int rcls = fpclassify (__real__ x);
43 int icls = fpclassify (__imag__ x);
44
45 if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO))
46 {
47 /* Real and imaginary part are 0.0. */
48 __imag__ result = signbit (__real__ x) ? M_PI_LOG10El : 0.0;
49 __imag__ result = __copysignl (__imag__ result, __imag__ x);
50 /* Yes, the following line raises an exception. */
51 __real__ result = -1.0 / fabsl (__real__ x);
52 }
53 else if (__glibc_likely (rcls != FP_NAN && icls != FP_NAN))
54 {
55 /* Neither real nor imaginary part is NaN. */
56 long double absx = fabsl (__real__ x), absy = fabsl (__imag__ x);
57 int scale = 0;
58
59 if (absx < absy)
60 {
61 long double t = absx;
62 absx = absy;
63 absy = t;
64 }
65
66 if (absx > LDBL_MAX / 2.0L)
67 {
68 scale = -1;
69 absx = __scalbnl (absx, scale);
70 absy = (absy >= LDBL_MIN * 2.0L ? __scalbnl (absy, scale) : 0.0L);
71 }
72 else if (absx < LDBL_MIN && absy < LDBL_MIN)
73 {
74 scale = LDBL_MANT_DIG;
75 absx = __scalbnl (absx, scale);
76 absy = __scalbnl (absy, scale);
77 }
78
79 if (absx == 1.0L && scale == 0)
80 {
81 long double absy2 = absy * absy;
82 if (absy2 <= LDBL_MIN * 2.0L * M_LN10l)
83 {
84 long double force_underflow = absy2 * absy2;
85 __real__ result = absy2 * (M_LOG10El / 2.0);
86 math_force_eval (force_underflow);
87 }
88 else
89 __real__ result = __log1pl (absy2) * (M_LOG10El / 2.0L);
90 }
91 else if (absx > 1.0L && absx < 2.0L && absy < 1.0L && scale == 0)
92 {
93 long double d2m1 = (absx - 1.0L) * (absx + 1.0L);
94 if (absy >= LDBL_EPSILON)
95 d2m1 += absy * absy;
96 __real__ result = __log1pl (d2m1) * (M_LOG10El / 2.0L);
97 }
98 else if (absx < 1.0L
99 && absx >= 0.75L
100 && absy < LDBL_EPSILON / 2.0L
101 && scale == 0)
102 {
103 long double d2m1 = (absx - 1.0L) * (absx + 1.0L);
104 __real__ result = __log1pl (d2m1) * (M_LOG10El / 2.0L);
105 }
106 else if (absx < 1.0L && (absx >= 0.75L || absy >= 0.5L) && scale == 0)
107 {
108 long double d2m1 = __x2y2m1l (absx, absy);
109 __real__ result = __log1pl (d2m1) * (M_LOG10El / 2.0L);
110 }
111 else
112 {
113 long double d = __ieee754_hypotl (absx, absy);
114 __real__ result = __ieee754_log10l (d) - scale * M_LOG10_2l;
115 }
116
117 __imag__ result = M_LOG10El * __ieee754_atan2l (__imag__ x, __real__ x);
118 }
119 else
120 {
121 __imag__ result = __nanl ("");
122 if (rcls == FP_INFINITE || icls == FP_INFINITE)
123 /* Real or imaginary part is infinite. */
124 __real__ result = HUGE_VALL;
125 else
126 __real__ result = __nanl ("");
127 }
128
129 return result;
130}
131weak_alias (__clog10l, clog10l)