]> git.ipfire.org Git - thirdparty/glibc.git/blame - math/s_csqrtf.c
Fix trailing space.
[thirdparty/glibc.git] / math / s_csqrtf.c
CommitLineData
63551311 1/* Complex square root of float value.
b168057a 2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
63551311
UD
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
41bdb6e2
AJ
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.
63551311
UD
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
41bdb6e2 15 Lesser General Public License for more details.
63551311 16
41bdb6e2 17 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
63551311
UD
20
21#include <complex.h>
22#include <math.h>
9277c064 23#include <math_private.h>
e456826d 24#include <float.h>
63551311
UD
25
26__complex__ float
27__csqrtf (__complex__ float x)
28{
29 __complex__ float res;
30 int rcls = fpclassify (__real__ x);
31 int icls = fpclassify (__imag__ x);
32
a1ffb40e 33 if (__glibc_unlikely (rcls <= FP_INFINITE || icls <= FP_INFINITE))
63551311
UD
34 {
35 if (icls == FP_INFINITE)
36 {
37 __real__ res = HUGE_VALF;
38 __imag__ res = __imag__ x;
39 }
40 else if (rcls == FP_INFINITE)
41 {
42 if (__real__ x < 0.0)
43 {
44 __real__ res = icls == FP_NAN ? __nanf ("") : 0;
45 __imag__ res = __copysignf (HUGE_VALF, __imag__ x);
46 }
47 else
48 {
49 __real__ res = __real__ x;
50 __imag__ res = (icls == FP_NAN
51 ? __nanf ("") : __copysignf (0.0, __imag__ x));
52 }
53 }
54 else
55 {
56 __real__ res = __nanf ("");
57 __imag__ res = __nanf ("");
58 }
59 }
60 else
61 {
a1ffb40e 62 if (__glibc_unlikely (icls == FP_ZERO))
63551311
UD
63 {
64 if (__real__ x < 0.0)
65 {
66 __real__ res = 0.0;
67 __imag__ res = __copysignf (__ieee754_sqrtf (-__real__ x),
68 __imag__ x);
69 }
70 else
71 {
72 __real__ res = fabsf (__ieee754_sqrtf (__real__ x));
73 __imag__ res = __copysignf (0.0, __imag__ x);
74 }
75 }
a1ffb40e 76 else if (__glibc_unlikely (rcls == FP_ZERO))
63551311 77 {
cdfe2c5e
JM
78 float r;
79 if (fabsf (__imag__ x) >= 2.0f * FLT_MIN)
80 r = __ieee754_sqrtf (0.5f * fabsf (__imag__ x));
81 else
82 r = 0.5f * __ieee754_sqrtf (2.0f * fabsf (__imag__ x));
63551311 83
72c7a71d
UD
84 __real__ res = r;
85 __imag__ res = __copysignf (r, __imag__ x);
63551311
UD
86 }
87 else
88 {
ec986e23 89 float d, r, s;
e456826d
JM
90 int scale = 0;
91
cdfe2c5e 92 if (fabsf (__real__ x) > FLT_MAX / 4.0f)
e456826d
JM
93 {
94 scale = 1;
95 __real__ x = __scalbnf (__real__ x, -2 * scale);
96 __imag__ x = __scalbnf (__imag__ x, -2 * scale);
97 }
cdfe2c5e
JM
98 else if (fabsf (__imag__ x) > FLT_MAX / 4.0f)
99 {
100 scale = 1;
101 if (fabsf (__real__ x) >= 4.0f * FLT_MIN)
102 __real__ x = __scalbnf (__real__ x, -2 * scale);
103 else
104 __real__ x = 0.0f;
105 __imag__ x = __scalbnf (__imag__ x, -2 * scale);
106 }
e456826d
JM
107 else if (fabsf (__real__ x) < FLT_MIN
108 && fabsf (__imag__ x) < FLT_MIN)
109 {
110 scale = -(FLT_MANT_DIG / 2);
111 __real__ x = __scalbnf (__real__ x, -2 * scale);
112 __imag__ x = __scalbnf (__imag__ x, -2 * scale);
113 }
63551311 114
ec986e23
UD
115 d = __ieee754_hypotf (__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)
119 {
cdfe2c5e 120 r = __ieee754_sqrtf (0.5f * (d + __real__ x));
718d34a3
JM
121 if (scale == 1 && fabsf (__imag__ x) < 1.0f)
122 {
123 /* Avoid possible intermediate underflow. */
124 s = __imag__ x / r;
125 r = __scalbnf (r, scale);
126 scale = 0;
127 }
128 else
129 s = 0.5f * (__imag__ x / r);
ec986e23 130 }
63551311 131 else
ec986e23 132 {
cdfe2c5e 133 s = __ieee754_sqrtf (0.5f * (d - __real__ x));
718d34a3
JM
134 if (scale == 1 && fabsf (__imag__ x) < 1.0f)
135 {
136 /* Avoid possible intermediate underflow. */
137 r = fabsf (__imag__ x / s);
138 s = __scalbnf (s, scale);
139 scale = 0;
140 }
141 else
142 r = fabsf (0.5f * (__imag__ x / s));
ec986e23 143 }
63551311 144
e456826d
JM
145 if (scale)
146 {
147 r = __scalbnf (r, scale);
148 s = __scalbnf (s, scale);
149 }
150
ec986e23
UD
151 __real__ res = r;
152 __imag__ res = __copysignf (s, __imag__ x);
63551311
UD
153 }
154 }
155
156 return res;
157}
1705f0a3 158#ifndef __csqrtf
63551311 159weak_alias (__csqrtf, csqrtf)
1705f0a3 160#endif