]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/s_csqrt_template.c
Improve the accuracy of tgamma (BZ #26983)
[thirdparty/glibc.git] / math / s_csqrt_template.c
CommitLineData
feb62dda 1/* Complex square root of a float type.
2b778ceb 2 Copyright (C) 1997-2021 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
5a82c748 19 <https://www.gnu.org/licenses/>. */
1dbc54f6
PM
20
21#include <complex.h>
22#include <math.h>
23#include <math_private.h>
8f5b00d3 24#include <math-underflow.h>
1dbc54f6
PM
25#include <float.h>
26
feb62dda
PM
27CFLOAT
28M_DECL_FUNC (__csqrt) (CFLOAT x)
1dbc54f6 29{
feb62dda 30 CFLOAT res;
1dbc54f6
PM
31 int rcls = fpclassify (__real__ x);
32 int icls = fpclassify (__imag__ x);
33
34 if (__glibc_unlikely (rcls <= FP_INFINITE || icls <= FP_INFINITE))
35 {
36 if (icls == FP_INFINITE)
37 {
feb62dda 38 __real__ res = M_HUGE_VAL;
1dbc54f6
PM
39 __imag__ res = __imag__ x;
40 }
41 else if (rcls == FP_INFINITE)
42 {
feb62dda 43 if (__real__ x < 0)
1dbc54f6 44 {
feb62dda
PM
45 __real__ res = icls == FP_NAN ? M_NAN : 0;
46 __imag__ res = M_COPYSIGN (M_HUGE_VAL, __imag__ x);
1dbc54f6
PM
47 }
48 else
49 {
50 __real__ res = __real__ x;
51 __imag__ res = (icls == FP_NAN
feb62dda 52 ? M_NAN : M_COPYSIGN (0, __imag__ x));
1dbc54f6
PM
53 }
54 }
55 else
56 {
feb62dda
PM
57 __real__ res = M_NAN;
58 __imag__ res = M_NAN;
1dbc54f6
PM
59 }
60 }
61 else
62 {
63 if (__glibc_unlikely (icls == FP_ZERO))
64 {
feb62dda 65 if (__real__ x < 0)
1dbc54f6 66 {
feb62dda
PM
67 __real__ res = 0;
68 __imag__ res = M_COPYSIGN (M_SQRT (-__real__ x), __imag__ x);
1dbc54f6
PM
69 }
70 else
71 {
feb62dda
PM
72 __real__ res = M_FABS (M_SQRT (__real__ x));
73 __imag__ res = M_COPYSIGN (0, __imag__ x);
1dbc54f6
PM
74 }
75 }
76 else if (__glibc_unlikely (rcls == FP_ZERO))
77 {
feb62dda
PM
78 FLOAT r;
79 if (M_FABS (__imag__ x) >= 2 * M_MIN)
80 r = M_SQRT (M_LIT (0.5) * M_FABS (__imag__ x));
1dbc54f6 81 else
feb62dda 82 r = M_LIT (0.5) * M_SQRT (2 * M_FABS (__imag__ x));
1dbc54f6
PM
83
84 __real__ res = r;
feb62dda 85 __imag__ res = M_COPYSIGN (r, __imag__ x);
1dbc54f6
PM
86 }
87 else
88 {
feb62dda 89 FLOAT d, r, s;
1dbc54f6
PM
90 int scale = 0;
91
feb62dda 92 if (M_FABS (__real__ x) > M_MAX / 4)
1dbc54f6
PM
93 {
94 scale = 1;
feb62dda
PM
95 __real__ x = M_SCALBN (__real__ x, -2 * scale);
96 __imag__ x = M_SCALBN (__imag__ x, -2 * scale);
1dbc54f6 97 }
feb62dda 98 else if (M_FABS (__imag__ x) > M_MAX / 4)
1dbc54f6
PM
99 {
100 scale = 1;
feb62dda
PM
101 if (M_FABS (__real__ x) >= 4 * M_MIN)
102 __real__ x = M_SCALBN (__real__ x, -2 * scale);
1dbc54f6 103 else
feb62dda
PM
104 __real__ x = 0;
105 __imag__ x = M_SCALBN (__imag__ x, -2 * scale);
1dbc54f6 106 }
feb62dda
PM
107 else if (M_FABS (__real__ x) < 2 * M_MIN
108 && M_FABS (__imag__ x) < 2 * M_MIN)
1dbc54f6 109 {
feb62dda
PM
110 scale = -((M_MANT_DIG + 1) / 2);
111 __real__ x = M_SCALBN (__real__ x, -2 * scale);
112 __imag__ x = M_SCALBN (__imag__ x, -2 * scale);
1dbc54f6
PM
113 }
114
feb62dda 115 d = M_HYPOT (__real__ x, __imag__ x);
1dbc54f6
PM
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)
119 {
feb62dda
PM
120 r = M_SQRT (M_LIT (0.5) * (d + __real__ x));
121 if (scale == 1 && M_FABS (__imag__ x) < 1)
1dbc54f6
PM
122 {
123 /* Avoid possible intermediate underflow. */
124 s = __imag__ x / r;
feb62dda 125 r = M_SCALBN (r, scale);
1dbc54f6
PM
126 scale = 0;
127 }
128 else
feb62dda 129 s = M_LIT (0.5) * (__imag__ x / r);
1dbc54f6
PM
130 }
131 else
132 {
feb62dda
PM
133 s = M_SQRT (M_LIT (0.5) * (d - __real__ x));
134 if (scale == 1 && M_FABS (__imag__ x) < 1)
1dbc54f6
PM
135 {
136 /* Avoid possible intermediate underflow. */
feb62dda
PM
137 r = M_FABS (__imag__ x / s);
138 s = M_SCALBN (s, scale);
1dbc54f6
PM
139 scale = 0;
140 }
141 else
feb62dda 142 r = M_FABS (M_LIT (0.5) * (__imag__ x / s));
1dbc54f6
PM
143 }
144
145 if (scale)
146 {
feb62dda
PM
147 r = M_SCALBN (r, scale);
148 s = M_SCALBN (s, scale);
1dbc54f6
PM
149 }
150
151 math_check_force_underflow (r);
152 math_check_force_underflow (s);
153
154 __real__ res = r;
feb62dda 155 __imag__ res = M_COPYSIGN (s, __imag__ x);
1dbc54f6
PM
156 }
157 }
158
159 return res;
160}
feb62dda 161declare_mgen_alias (__csqrt, csqrt)