145
|
1 /* Compute complex natural logarithm.
|
|
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
|
111
|
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 "quadmath-imp.h"
|
|
21
|
|
22 __complex128
|
|
23 clogq (__complex128 x)
|
|
24 {
|
|
25 __complex128 result;
|
|
26 int rcls = fpclassifyq (__real__ x);
|
|
27 int icls = fpclassifyq (__imag__ x);
|
|
28
|
145
|
29 if (__glibc_unlikely (rcls == QUADFP_ZERO && icls == QUADFP_ZERO))
|
111
|
30 {
|
|
31 /* Real and imaginary part are 0.0. */
|
145
|
32 __imag__ result = signbitq (__real__ x) ? (__float128) M_PIq : 0;
|
111
|
33 __imag__ result = copysignq (__imag__ result, __imag__ x);
|
|
34 /* Yes, the following line raises an exception. */
|
145
|
35 __real__ result = -1 / fabsq (__real__ x);
|
111
|
36 }
|
145
|
37 else if (__glibc_likely (rcls != QUADFP_NAN && icls != QUADFP_NAN))
|
111
|
38 {
|
|
39 /* Neither real nor imaginary part is NaN. */
|
|
40 __float128 absx = fabsq (__real__ x), absy = fabsq (__imag__ x);
|
|
41 int scale = 0;
|
|
42
|
|
43 if (absx < absy)
|
|
44 {
|
|
45 __float128 t = absx;
|
|
46 absx = absy;
|
|
47 absy = t;
|
|
48 }
|
|
49
|
145
|
50 if (absx > FLT128_MAX / 2)
|
111
|
51 {
|
|
52 scale = -1;
|
|
53 absx = scalbnq (absx, scale);
|
145
|
54 absy = (absy >= FLT128_MIN * 2 ? scalbnq (absy, scale) : 0);
|
111
|
55 }
|
|
56 else if (absx < FLT128_MIN && absy < FLT128_MIN)
|
|
57 {
|
|
58 scale = FLT128_MANT_DIG;
|
|
59 absx = scalbnq (absx, scale);
|
|
60 absy = scalbnq (absy, scale);
|
|
61 }
|
|
62
|
145
|
63 if (absx == 1 && scale == 0)
|
111
|
64 {
|
145
|
65 __real__ result = log1pq (absy * absy) / 2;
|
|
66 math_check_force_underflow_nonneg (__real__ result);
|
111
|
67 }
|
145
|
68 else if (absx > 1 && absx < 2 && absy < 1 && scale == 0)
|
111
|
69 {
|
145
|
70 __float128 d2m1 = (absx - 1) * (absx + 1);
|
111
|
71 if (absy >= FLT128_EPSILON)
|
|
72 d2m1 += absy * absy;
|
145
|
73 __real__ result = log1pq (d2m1) / 2;
|
111
|
74 }
|
145
|
75 else if (absx < 1
|
|
76 && absx >= 0.5Q
|
|
77 && absy < FLT128_EPSILON / 2
|
111
|
78 && scale == 0)
|
|
79 {
|
145
|
80 __float128 d2m1 = (absx - 1) * (absx + 1);
|
|
81 __real__ result = log1pq (d2m1) / 2;
|
111
|
82 }
|
145
|
83 else if (absx < 1
|
|
84 && absx >= 0.5Q
|
|
85 && scale == 0
|
|
86 && absx * absx + absy * absy >= 0.5Q)
|
111
|
87 {
|
|
88 __float128 d2m1 = __quadmath_x2y2m1q (absx, absy);
|
145
|
89 __real__ result = log1pq (d2m1) / 2;
|
111
|
90 }
|
|
91 else
|
|
92 {
|
|
93 __float128 d = hypotq (absx, absy);
|
145
|
94 __real__ result = logq (d) - scale * (__float128) M_LN2q;
|
111
|
95 }
|
|
96
|
|
97 __imag__ result = atan2q (__imag__ x, __real__ x);
|
|
98 }
|
|
99 else
|
|
100 {
|
|
101 __imag__ result = nanq ("");
|
|
102 if (rcls == QUADFP_INFINITE || icls == QUADFP_INFINITE)
|
|
103 /* Real or imaginary part is infinite. */
|
|
104 __real__ result = HUGE_VALQ;
|
|
105 else
|
|
106 __real__ result = nanq ("");
|
|
107 }
|
|
108
|
|
109 return result;
|
|
110 }
|