xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Compute complex base 10 logarithm. |
| 2 | Copyright (C) 1997-2016 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 | __real__ result = __log1pl (absy * absy) * (M_LOG10El / 2.0L); |
| 82 | math_check_force_underflow_nonneg (__real__ result); |
| 83 | } |
| 84 | else if (absx > 1.0L && absx < 2.0L && absy < 1.0L && scale == 0) |
| 85 | { |
| 86 | long double d2m1 = (absx - 1.0L) * (absx + 1.0L); |
| 87 | if (absy >= LDBL_EPSILON) |
| 88 | d2m1 += absy * absy; |
| 89 | __real__ result = __log1pl (d2m1) * (M_LOG10El / 2.0L); |
| 90 | } |
| 91 | else if (absx < 1.0L |
| 92 | && absx >= 0.5L |
| 93 | && absy < LDBL_EPSILON / 2.0L |
| 94 | && scale == 0) |
| 95 | { |
| 96 | long double d2m1 = (absx - 1.0L) * (absx + 1.0L); |
| 97 | __real__ result = __log1pl (d2m1) * (M_LOG10El / 2.0L); |
| 98 | } |
| 99 | else if (absx < 1.0L |
| 100 | && absx >= 0.5L |
| 101 | && scale == 0 |
| 102 | && absx * absx + absy * absy >= 0.5L) |
| 103 | { |
| 104 | long double d2m1 = __x2y2m1l (absx, absy); |
| 105 | __real__ result = __log1pl (d2m1) * (M_LOG10El / 2.0L); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | long double d = __ieee754_hypotl (absx, absy); |
| 110 | __real__ result = __ieee754_log10l (d) - scale * M_LOG10_2l; |
| 111 | } |
| 112 | |
| 113 | __imag__ result = M_LOG10El * __ieee754_atan2l (__imag__ x, __real__ x); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | __imag__ result = __nanl (""); |
| 118 | if (rcls == FP_INFINITE || icls == FP_INFINITE) |
| 119 | /* Real or imaginary part is infinite. */ |
| 120 | __real__ result = HUGE_VALL; |
| 121 | else |
| 122 | __real__ result = __nanl (""); |
| 123 | } |
| 124 | |
| 125 | return result; |
| 126 | } |
| 127 | weak_alias (__clog10l, clog10l) |