1 /* Complex square root of a float type.
2 Copyright (C) 1997-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
21 #include <math_private.h>
22 #include <math-underflow.h>
26 M_DECL_FUNC (__csqrt
) (CFLOAT x
)
29 int rcls
= fpclassify (__real__ x
);
30 int icls
= fpclassify (__imag__ x
);
32 if (__glibc_unlikely (rcls
<= FP_INFINITE
|| icls
<= FP_INFINITE
))
34 if (icls
== FP_INFINITE
)
36 __real__ res
= M_HUGE_VAL
;
37 __imag__ res
= __imag__ x
;
39 else if (rcls
== FP_INFINITE
)
43 __real__ res
= icls
== FP_NAN
? M_NAN
: 0;
44 __imag__ res
= M_COPYSIGN (M_HUGE_VAL
, __imag__ x
);
48 __real__ res
= __real__ x
;
49 __imag__ res
= (icls
== FP_NAN
50 ? M_NAN
: M_COPYSIGN (0, __imag__ x
));
61 if (__glibc_unlikely (icls
== FP_ZERO
))
66 __imag__ res
= M_COPYSIGN (M_SQRT (-__real__ x
), __imag__ x
);
70 __real__ res
= M_FABS (M_SQRT (__real__ x
));
71 __imag__ res
= M_COPYSIGN (0, __imag__ x
);
74 else if (__glibc_unlikely (rcls
== FP_ZERO
))
77 if (M_FABS (__imag__ x
) >= 2 * M_MIN
)
78 r
= M_SQRT (M_LIT (0.5) * M_FABS (__imag__ x
));
80 r
= M_LIT (0.5) * M_SQRT (2 * M_FABS (__imag__ x
));
83 __imag__ res
= M_COPYSIGN (r
, __imag__ x
);
90 if (M_FABS (__real__ x
) > M_MAX
/ 4)
93 __real__ x
= M_SCALBN (__real__ x
, -2 * scale
);
94 __imag__ x
= M_SCALBN (__imag__ x
, -2 * scale
);
96 else if (M_FABS (__imag__ x
) > M_MAX
/ 4)
99 if (M_FABS (__real__ x
) >= 4 * M_MIN
)
100 __real__ x
= M_SCALBN (__real__ x
, -2 * scale
);
103 __imag__ x
= M_SCALBN (__imag__ x
, -2 * scale
);
105 else if (M_FABS (__real__ x
) < 2 * M_MIN
106 && M_FABS (__imag__ x
) < 2 * M_MIN
)
108 scale
= -((M_MANT_DIG
+ 1) / 2);
109 __real__ x
= M_SCALBN (__real__ x
, -2 * scale
);
110 __imag__ x
= M_SCALBN (__imag__ x
, -2 * scale
);
113 d
= M_HYPOT (__real__ x
, __imag__ x
);
114 /* Use the identity 2 Re res Im res = Im x
115 to avoid cancellation error in d +/- Re x. */
118 r
= M_SQRT (M_LIT (0.5) * (d
+ __real__ x
));
119 if (scale
== 1 && M_FABS (__imag__ x
) < 1)
121 /* Avoid possible intermediate underflow. */
123 r
= M_SCALBN (r
, scale
);
127 s
= M_LIT (0.5) * (__imag__ x
/ r
);
131 s
= M_SQRT (M_LIT (0.5) * (d
- __real__ x
));
132 if (scale
== 1 && M_FABS (__imag__ x
) < 1)
134 /* Avoid possible intermediate underflow. */
135 r
= M_FABS (__imag__ x
/ s
);
136 s
= M_SCALBN (s
, scale
);
140 r
= M_FABS (M_LIT (0.5) * (__imag__ x
/ s
));
145 r
= M_SCALBN (r
, scale
);
146 s
= M_SCALBN (s
, scale
);
149 math_check_force_underflow (r
);
150 math_check_force_underflow (s
);
153 __imag__ res
= M_COPYSIGN (s
, __imag__ x
);
159 declare_mgen_alias (__csqrt
, csqrt
)