]>
Commit | Line | Data |
---|---|---|
63551311 | 1 | /* Complex square root of double value. |
e456826d | 2 | Copyright (C) 1997-2012 Free Software Foundation, Inc. |
63551311 UD |
3 | This file is part of the GNU C Library. |
4 | Based on an algorithm by Stephen L. Moshier <moshier@world.std.com>. | |
5 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. | |
6 | ||
7 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
8 | modify it under the terms of the GNU Lesser General Public |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
63551311 UD |
11 | |
12 | The GNU C Library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 15 | Lesser General Public License for more details. |
63551311 | 16 | |
41bdb6e2 | 17 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
18 | License along with the GNU C Library; if not, see |
19 | <http://www.gnu.org/licenses/>. */ | |
63551311 UD |
20 | |
21 | #include <complex.h> | |
22 | #include <math.h> | |
9277c064 | 23 | #include <math_private.h> |
e456826d | 24 | #include <float.h> |
63551311 UD |
25 | |
26 | __complex__ double | |
27 | __csqrt (__complex__ double x) | |
28 | { | |
29 | __complex__ double res; | |
30 | int rcls = fpclassify (__real__ x); | |
31 | int icls = fpclassify (__imag__ x); | |
32 | ||
77425c63 | 33 | if (__builtin_expect (rcls <= FP_INFINITE || icls <= FP_INFINITE, 0)) |
63551311 UD |
34 | { |
35 | if (icls == FP_INFINITE) | |
36 | { | |
37 | __real__ res = HUGE_VAL; | |
38 | __imag__ res = __imag__ x; | |
39 | } | |
40 | else if (rcls == FP_INFINITE) | |
41 | { | |
42 | if (__real__ x < 0.0) | |
43 | { | |
44 | __real__ res = icls == FP_NAN ? __nan ("") : 0; | |
45 | __imag__ res = __copysign (HUGE_VAL, __imag__ x); | |
46 | } | |
47 | else | |
48 | { | |
49 | __real__ res = __real__ x; | |
50 | __imag__ res = (icls == FP_NAN | |
51 | ? __nan ("") : __copysign (0.0, __imag__ x)); | |
52 | } | |
53 | } | |
54 | else | |
55 | { | |
56 | __real__ res = __nan (""); | |
57 | __imag__ res = __nan (""); | |
58 | } | |
59 | } | |
60 | else | |
61 | { | |
77425c63 | 62 | if (__builtin_expect (icls == FP_ZERO, 0)) |
63551311 UD |
63 | { |
64 | if (__real__ x < 0.0) | |
65 | { | |
66 | __real__ res = 0.0; | |
67 | __imag__ res = __copysign (__ieee754_sqrt (-__real__ x), | |
68 | __imag__ x); | |
69 | } | |
70 | else | |
71 | { | |
72 | __real__ res = fabs (__ieee754_sqrt (__real__ x)); | |
73 | __imag__ res = __copysign (0.0, __imag__ x); | |
74 | } | |
75 | } | |
77425c63 | 76 | else if (__builtin_expect (rcls == FP_ZERO, 0)) |
63551311 UD |
77 | { |
78 | double r = __ieee754_sqrt (0.5 * fabs (__imag__ x)); | |
79 | ||
72c7a71d UD |
80 | __real__ res = r; |
81 | __imag__ res = __copysign (r, __imag__ x); | |
63551311 UD |
82 | } |
83 | else | |
84 | { | |
ec986e23 | 85 | double d, r, s; |
e456826d JM |
86 | int scale = 0; |
87 | ||
88 | if (fabs (__real__ x) > DBL_MAX / 2.0 | |
89 | || fabs (__imag__ x) > DBL_MAX / 2.0) | |
90 | { | |
91 | scale = 1; | |
92 | __real__ x = __scalbn (__real__ x, -2 * scale); | |
93 | __imag__ x = __scalbn (__imag__ x, -2 * scale); | |
94 | } | |
95 | else if (fabs (__real__ x) < DBL_MIN | |
96 | && fabs (__imag__ x) < DBL_MIN) | |
97 | { | |
98 | scale = -(DBL_MANT_DIG / 2); | |
99 | __real__ x = __scalbn (__real__ x, -2 * scale); | |
100 | __imag__ x = __scalbn (__imag__ x, -2 * scale); | |
101 | } | |
63551311 | 102 | |
ec986e23 UD |
103 | d = __ieee754_hypot (__real__ x, __imag__ x); |
104 | /* Use the identity 2 Re res Im res = Im x | |
105 | to avoid cancellation error in d +/- Re x. */ | |
106 | if (__real__ x > 0) | |
107 | { | |
108 | r = __ieee754_sqrt (0.5 * d + 0.5 * __real__ x); | |
109 | s = (0.5 * __imag__ x) / r; | |
110 | } | |
63551311 | 111 | else |
ec986e23 UD |
112 | { |
113 | s = __ieee754_sqrt (0.5 * d - 0.5 * __real__ x); | |
786a5421 | 114 | r = fabs ((0.5 * __imag__ x) / s); |
ec986e23 | 115 | } |
63551311 | 116 | |
e456826d JM |
117 | if (scale) |
118 | { | |
119 | r = __scalbn (r, scale); | |
120 | s = __scalbn (s, scale); | |
121 | } | |
122 | ||
ec986e23 UD |
123 | __real__ res = r; |
124 | __imag__ res = __copysign (s, __imag__ x); | |
63551311 UD |
125 | } |
126 | } | |
127 | ||
128 | return res; | |
129 | } | |
130 | weak_alias (__csqrt, csqrt) | |
131 | #ifdef NO_LONG_DOUBLE | |
132 | strong_alias (__csqrt, __csqrtl) | |
133 | weak_alias (__csqrt, csqrtl) | |
134 | #endif |