iconv mapping of 0xA8 0xEC in CP1258 is non-canonical
[glibc.git] / math / s_csqrt.c
blob1691a01eccb9fad32af357765c61a95834ad8f2b
1 /* Complex square root of double value.
2 Copyright (C) 1997, 1998, 2005, 2011 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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #include <complex.h>
23 #include <math.h>
24 #include <math_private.h>
27 __complex__ double
28 __csqrt (__complex__ double x)
30 __complex__ double res;
31 int rcls = fpclassify (__real__ x);
32 int icls = fpclassify (__imag__ x);
34 if (__builtin_expect (rcls <= FP_INFINITE || icls <= FP_INFINITE, 0))
36 if (icls == FP_INFINITE)
38 __real__ res = HUGE_VAL;
39 __imag__ res = __imag__ x;
41 else if (rcls == FP_INFINITE)
43 if (__real__ x < 0.0)
45 __real__ res = icls == FP_NAN ? __nan ("") : 0;
46 __imag__ res = __copysign (HUGE_VAL, __imag__ x);
48 else
50 __real__ res = __real__ x;
51 __imag__ res = (icls == FP_NAN
52 ? __nan ("") : __copysign (0.0, __imag__ x));
55 else
57 __real__ res = __nan ("");
58 __imag__ res = __nan ("");
61 else
63 if (__builtin_expect (icls == FP_ZERO, 0))
65 if (__real__ x < 0.0)
67 __real__ res = 0.0;
68 __imag__ res = __copysign (__ieee754_sqrt (-__real__ x),
69 __imag__ x);
71 else
73 __real__ res = fabs (__ieee754_sqrt (__real__ x));
74 __imag__ res = __copysign (0.0, __imag__ x);
77 else if (__builtin_expect (rcls == FP_ZERO, 0))
79 double r = __ieee754_sqrt (0.5 * fabs (__imag__ x));
81 __real__ res = r;
82 __imag__ res = __copysign (r, __imag__ x);
84 else
86 double d, r, s;
88 d = __ieee754_hypot (__real__ x, __imag__ x);
89 /* Use the identity 2 Re res Im res = Im x
90 to avoid cancellation error in d +/- Re x. */
91 if (__real__ x > 0)
93 r = __ieee754_sqrt (0.5 * d + 0.5 * __real__ x);
94 s = (0.5 * __imag__ x) / r;
96 else
98 s = __ieee754_sqrt (0.5 * d - 0.5 * __real__ x);
99 r = fabs ((0.5 * __imag__ x) / s);
102 __real__ res = r;
103 __imag__ res = __copysign (s, __imag__ x);
107 return res;
109 weak_alias (__csqrt, csqrt)
110 #ifdef NO_LONG_DOUBLE
111 strong_alias (__csqrt, __csqrtl)
112 weak_alias (__csqrt, csqrtl)
113 #endif