Prevent check-local-headers.sh hang.
[glibc.git] / math / s_csqrt.c
blobb86f53322e08d07026181334a509dbaeef8b27f3
1 /* Complex square root of double value.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
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.
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.
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.
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/>. */
21 #include <complex.h>
22 #include <math.h>
23 #include <math_private.h>
24 #include <float.h>
26 __complex__ double
27 __csqrt (__complex__ double x)
29 __complex__ double res;
30 int rcls = fpclassify (__real__ x);
31 int icls = fpclassify (__imag__ x);
33 if (__glibc_unlikely (rcls <= FP_INFINITE || icls <= FP_INFINITE))
35 if (icls == FP_INFINITE)
37 __real__ res = HUGE_VAL;
38 __imag__ res = __imag__ x;
40 else if (rcls == FP_INFINITE)
42 if (__real__ x < 0.0)
44 __real__ res = icls == FP_NAN ? __nan ("") : 0;
45 __imag__ res = __copysign (HUGE_VAL, __imag__ x);
47 else
49 __real__ res = __real__ x;
50 __imag__ res = (icls == FP_NAN
51 ? __nan ("") : __copysign (0.0, __imag__ x));
54 else
56 __real__ res = __nan ("");
57 __imag__ res = __nan ("");
60 else
62 if (__glibc_unlikely (icls == FP_ZERO))
64 if (__real__ x < 0.0)
66 __real__ res = 0.0;
67 __imag__ res = __copysign (__ieee754_sqrt (-__real__ x),
68 __imag__ x);
70 else
72 __real__ res = fabs (__ieee754_sqrt (__real__ x));
73 __imag__ res = __copysign (0.0, __imag__ x);
76 else if (__glibc_unlikely (rcls == FP_ZERO))
78 double r;
79 if (fabs (__imag__ x) >= 2.0 * DBL_MIN)
80 r = __ieee754_sqrt (0.5 * fabs (__imag__ x));
81 else
82 r = 0.5 * __ieee754_sqrt (2.0 * fabs (__imag__ x));
84 __real__ res = r;
85 __imag__ res = __copysign (r, __imag__ x);
87 else
89 double d, r, s;
90 int scale = 0;
92 if (fabs (__real__ x) > DBL_MAX / 4.0)
94 scale = 1;
95 __real__ x = __scalbn (__real__ x, -2 * scale);
96 __imag__ x = __scalbn (__imag__ x, -2 * scale);
98 else if (fabs (__imag__ x) > DBL_MAX / 4.0)
100 scale = 1;
101 if (fabs (__real__ x) >= 4.0 * DBL_MIN)
102 __real__ x = __scalbn (__real__ x, -2 * scale);
103 else
104 __real__ x = 0.0;
105 __imag__ x = __scalbn (__imag__ x, -2 * scale);
107 else if (fabs (__real__ x) < 2.0 * DBL_MIN
108 && fabs (__imag__ x) < 2.0 * DBL_MIN)
110 scale = -((DBL_MANT_DIG + 1) / 2);
111 __real__ x = __scalbn (__real__ x, -2 * scale);
112 __imag__ x = __scalbn (__imag__ x, -2 * scale);
115 d = __ieee754_hypot (__real__ x, __imag__ x);
116 /* Use the identity 2 Re res Im res = Im x
117 to avoid cancellation error in d +/- Re x. */
118 if (__real__ x > 0)
120 r = __ieee754_sqrt (0.5 * (d + __real__ x));
121 if (scale == 1 && fabs (__imag__ x) < 1.0)
123 /* Avoid possible intermediate underflow. */
124 s = __imag__ x / r;
125 r = __scalbn (r, scale);
126 scale = 0;
128 else
129 s = 0.5 * (__imag__ x / r);
131 else
133 s = __ieee754_sqrt (0.5 * (d - __real__ x));
134 if (scale == 1 && fabs (__imag__ x) < 1.0)
136 /* Avoid possible intermediate underflow. */
137 r = fabs (__imag__ x / s);
138 s = __scalbn (s, scale);
139 scale = 0;
141 else
142 r = fabs (0.5 * (__imag__ x / s));
145 if (scale)
147 r = __scalbn (r, scale);
148 s = __scalbn (s, scale);
151 if (fabs (r) < DBL_MIN)
153 double force_underflow = r * r;
154 math_force_eval (force_underflow);
156 if (fabs (s) < DBL_MIN)
158 double force_underflow = s * s;
159 math_force_eval (force_underflow);
162 __real__ res = r;
163 __imag__ res = __copysign (s, __imag__ x);
167 return res;
169 weak_alias (__csqrt, csqrt)
170 #ifdef NO_LONG_DOUBLE
171 strong_alias (__csqrt, __csqrtl)
172 weak_alias (__csqrt, csqrtl)
173 #endif