]>
Commit | Line | Data |
---|---|---|
feb62dda | 1 | /* Complex square root of a float type. |
bfff8b1b | 2 | Copyright (C) 1997-2017 Free Software Foundation, Inc. |
1dbc54f6 PM |
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 | |
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. | |
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 | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public | |
18 | License along with the GNU C Library; if not, see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include <complex.h> | |
22 | #include <math.h> | |
23 | #include <math_private.h> | |
24 | #include <float.h> | |
25 | ||
feb62dda PM |
26 | CFLOAT |
27 | M_DECL_FUNC (__csqrt) (CFLOAT x) | |
1dbc54f6 | 28 | { |
feb62dda | 29 | CFLOAT res; |
1dbc54f6 PM |
30 | int rcls = fpclassify (__real__ x); |
31 | int icls = fpclassify (__imag__ x); | |
32 | ||
33 | if (__glibc_unlikely (rcls <= FP_INFINITE || icls <= FP_INFINITE)) | |
34 | { | |
35 | if (icls == FP_INFINITE) | |
36 | { | |
feb62dda | 37 | __real__ res = M_HUGE_VAL; |
1dbc54f6 PM |
38 | __imag__ res = __imag__ x; |
39 | } | |
40 | else if (rcls == FP_INFINITE) | |
41 | { | |
feb62dda | 42 | if (__real__ x < 0) |
1dbc54f6 | 43 | { |
feb62dda PM |
44 | __real__ res = icls == FP_NAN ? M_NAN : 0; |
45 | __imag__ res = M_COPYSIGN (M_HUGE_VAL, __imag__ x); | |
1dbc54f6 PM |
46 | } |
47 | else | |
48 | { | |
49 | __real__ res = __real__ x; | |
50 | __imag__ res = (icls == FP_NAN | |
feb62dda | 51 | ? M_NAN : M_COPYSIGN (0, __imag__ x)); |
1dbc54f6 PM |
52 | } |
53 | } | |
54 | else | |
55 | { | |
feb62dda PM |
56 | __real__ res = M_NAN; |
57 | __imag__ res = M_NAN; | |
1dbc54f6 PM |
58 | } |
59 | } | |
60 | else | |
61 | { | |
62 | if (__glibc_unlikely (icls == FP_ZERO)) | |
63 | { | |
feb62dda | 64 | if (__real__ x < 0) |
1dbc54f6 | 65 | { |
feb62dda PM |
66 | __real__ res = 0; |
67 | __imag__ res = M_COPYSIGN (M_SQRT (-__real__ x), __imag__ x); | |
1dbc54f6 PM |
68 | } |
69 | else | |
70 | { | |
feb62dda PM |
71 | __real__ res = M_FABS (M_SQRT (__real__ x)); |
72 | __imag__ res = M_COPYSIGN (0, __imag__ x); | |
1dbc54f6 PM |
73 | } |
74 | } | |
75 | else if (__glibc_unlikely (rcls == FP_ZERO)) | |
76 | { | |
feb62dda PM |
77 | FLOAT r; |
78 | if (M_FABS (__imag__ x) >= 2 * M_MIN) | |
79 | r = M_SQRT (M_LIT (0.5) * M_FABS (__imag__ x)); | |
1dbc54f6 | 80 | else |
feb62dda | 81 | r = M_LIT (0.5) * M_SQRT (2 * M_FABS (__imag__ x)); |
1dbc54f6 PM |
82 | |
83 | __real__ res = r; | |
feb62dda | 84 | __imag__ res = M_COPYSIGN (r, __imag__ x); |
1dbc54f6 PM |
85 | } |
86 | else | |
87 | { | |
feb62dda | 88 | FLOAT d, r, s; |
1dbc54f6 PM |
89 | int scale = 0; |
90 | ||
feb62dda | 91 | if (M_FABS (__real__ x) > M_MAX / 4) |
1dbc54f6 PM |
92 | { |
93 | scale = 1; | |
feb62dda PM |
94 | __real__ x = M_SCALBN (__real__ x, -2 * scale); |
95 | __imag__ x = M_SCALBN (__imag__ x, -2 * scale); | |
1dbc54f6 | 96 | } |
feb62dda | 97 | else if (M_FABS (__imag__ x) > M_MAX / 4) |
1dbc54f6 PM |
98 | { |
99 | scale = 1; | |
feb62dda PM |
100 | if (M_FABS (__real__ x) >= 4 * M_MIN) |
101 | __real__ x = M_SCALBN (__real__ x, -2 * scale); | |
1dbc54f6 | 102 | else |
feb62dda PM |
103 | __real__ x = 0; |
104 | __imag__ x = M_SCALBN (__imag__ x, -2 * scale); | |
1dbc54f6 | 105 | } |
feb62dda PM |
106 | else if (M_FABS (__real__ x) < 2 * M_MIN |
107 | && M_FABS (__imag__ x) < 2 * M_MIN) | |
1dbc54f6 | 108 | { |
feb62dda PM |
109 | scale = -((M_MANT_DIG + 1) / 2); |
110 | __real__ x = M_SCALBN (__real__ x, -2 * scale); | |
111 | __imag__ x = M_SCALBN (__imag__ x, -2 * scale); | |
1dbc54f6 PM |
112 | } |
113 | ||
feb62dda | 114 | d = M_HYPOT (__real__ x, __imag__ x); |
1dbc54f6 PM |
115 | /* Use the identity 2 Re res Im res = Im x |
116 | to avoid cancellation error in d +/- Re x. */ | |
117 | if (__real__ x > 0) | |
118 | { | |
feb62dda PM |
119 | r = M_SQRT (M_LIT (0.5) * (d + __real__ x)); |
120 | if (scale == 1 && M_FABS (__imag__ x) < 1) | |
1dbc54f6 PM |
121 | { |
122 | /* Avoid possible intermediate underflow. */ | |
123 | s = __imag__ x / r; | |
feb62dda | 124 | r = M_SCALBN (r, scale); |
1dbc54f6 PM |
125 | scale = 0; |
126 | } | |
127 | else | |
feb62dda | 128 | s = M_LIT (0.5) * (__imag__ x / r); |
1dbc54f6 PM |
129 | } |
130 | else | |
131 | { | |
feb62dda PM |
132 | s = M_SQRT (M_LIT (0.5) * (d - __real__ x)); |
133 | if (scale == 1 && M_FABS (__imag__ x) < 1) | |
1dbc54f6 PM |
134 | { |
135 | /* Avoid possible intermediate underflow. */ | |
feb62dda PM |
136 | r = M_FABS (__imag__ x / s); |
137 | s = M_SCALBN (s, scale); | |
1dbc54f6 PM |
138 | scale = 0; |
139 | } | |
140 | else | |
feb62dda | 141 | r = M_FABS (M_LIT (0.5) * (__imag__ x / s)); |
1dbc54f6 PM |
142 | } |
143 | ||
144 | if (scale) | |
145 | { | |
feb62dda PM |
146 | r = M_SCALBN (r, scale); |
147 | s = M_SCALBN (s, scale); | |
1dbc54f6 PM |
148 | } |
149 | ||
150 | math_check_force_underflow (r); | |
151 | math_check_force_underflow (s); | |
152 | ||
153 | __real__ res = r; | |
feb62dda | 154 | __imag__ res = M_COPYSIGN (s, __imag__ x); |
1dbc54f6 PM |
155 | } |
156 | } | |
157 | ||
158 | return res; | |
159 | } | |
feb62dda | 160 | declare_mgen_alias (__csqrt, csqrt) |