xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Complex square root of long double value. |
| 2 | Copyright (C) 1997-2016 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. |
| 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 |
| 19 | <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include <complex.h> |
| 22 | #include <math.h> |
| 23 | #include <math_private.h> |
| 24 | #include <float.h> |
| 25 | |
| 26 | __complex__ long double |
| 27 | __csqrtl (__complex__ long double x) |
| 28 | { |
| 29 | __complex__ long double res; |
| 30 | int rcls = fpclassify (__real__ x); |
| 31 | int icls = fpclassify (__imag__ x); |
| 32 | |
| 33 | if (__glibc_unlikely (rcls <= FP_INFINITE || icls <= FP_INFINITE)) |
| 34 | { |
| 35 | if (icls == FP_INFINITE) |
| 36 | { |
| 37 | __real__ res = HUGE_VALL; |
| 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 ? __nanl ("") : 0; |
| 45 | __imag__ res = __copysignl (HUGE_VALL, __imag__ x); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | __real__ res = __real__ x; |
| 50 | __imag__ res = (icls == FP_NAN |
| 51 | ? __nanl ("") : __copysignl (0.0, __imag__ x)); |
| 52 | } |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | __real__ res = __nanl (""); |
| 57 | __imag__ res = __nanl (""); |
| 58 | } |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | if (__glibc_unlikely (icls == FP_ZERO)) |
| 63 | { |
| 64 | if (__real__ x < 0.0) |
| 65 | { |
| 66 | __real__ res = 0.0; |
| 67 | __imag__ res = __copysignl (__ieee754_sqrtl (-__real__ x), |
| 68 | __imag__ x); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | __real__ res = fabsl (__ieee754_sqrtl (__real__ x)); |
| 73 | __imag__ res = __copysignl (0.0, __imag__ x); |
| 74 | } |
| 75 | } |
| 76 | else if (__glibc_unlikely (rcls == FP_ZERO)) |
| 77 | { |
| 78 | long double r; |
| 79 | if (fabsl (__imag__ x) >= 2.0L * LDBL_MIN) |
| 80 | r = __ieee754_sqrtl (0.5L * fabsl (__imag__ x)); |
| 81 | else |
| 82 | r = 0.5L * __ieee754_sqrtl (2.0L * fabsl (__imag__ x)); |
| 83 | |
| 84 | __real__ res = r; |
| 85 | __imag__ res = __copysignl (r, __imag__ x); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | long double d, r, s; |
| 90 | int scale = 0; |
| 91 | |
| 92 | if (fabsl (__real__ x) > LDBL_MAX / 4.0L) |
| 93 | { |
| 94 | scale = 1; |
| 95 | __real__ x = __scalbnl (__real__ x, -2 * scale); |
| 96 | __imag__ x = __scalbnl (__imag__ x, -2 * scale); |
| 97 | } |
| 98 | else if (fabsl (__imag__ x) > LDBL_MAX / 4.0L) |
| 99 | { |
| 100 | scale = 1; |
| 101 | if (fabsl (__real__ x) >= 4.0L * LDBL_MIN) |
| 102 | __real__ x = __scalbnl (__real__ x, -2 * scale); |
| 103 | else |
| 104 | __real__ x = 0.0L; |
| 105 | __imag__ x = __scalbnl (__imag__ x, -2 * scale); |
| 106 | } |
| 107 | else if (fabsl (__real__ x) < 2.0L * LDBL_MIN |
| 108 | && fabsl (__imag__ x) < 2.0L * LDBL_MIN) |
| 109 | { |
| 110 | scale = -((LDBL_MANT_DIG + 1) / 2); |
| 111 | __real__ x = __scalbnl (__real__ x, -2 * scale); |
| 112 | __imag__ x = __scalbnl (__imag__ x, -2 * scale); |
| 113 | } |
| 114 | |
| 115 | d = __ieee754_hypotl (__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 | { |
| 120 | r = __ieee754_sqrtl (0.5L * (d + __real__ x)); |
| 121 | if (scale == 1 && fabsl (__imag__ x) < 1.0L) |
| 122 | { |
| 123 | /* Avoid possible intermediate underflow. */ |
| 124 | s = __imag__ x / r; |
| 125 | r = __scalbnl (r, scale); |
| 126 | scale = 0; |
| 127 | } |
| 128 | else |
| 129 | s = 0.5L * (__imag__ x / r); |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | s = __ieee754_sqrtl (0.5L * (d - __real__ x)); |
| 134 | if (scale == 1 && fabsl (__imag__ x) < 1.0L) |
| 135 | { |
| 136 | /* Avoid possible intermediate underflow. */ |
| 137 | r = fabsl (__imag__ x / s); |
| 138 | s = __scalbnl (s, scale); |
| 139 | scale = 0; |
| 140 | } |
| 141 | else |
| 142 | r = fabsl (0.5L * (__imag__ x / s)); |
| 143 | } |
| 144 | |
| 145 | if (scale) |
| 146 | { |
| 147 | r = __scalbnl (r, scale); |
| 148 | s = __scalbnl (s, scale); |
| 149 | } |
| 150 | |
| 151 | math_check_force_underflow (r); |
| 152 | math_check_force_underflow (s); |
| 153 | |
| 154 | __real__ res = r; |
| 155 | __imag__ res = __copysignl (s, __imag__ x); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return res; |
| 160 | } |
| 161 | weak_alias (__csqrtl, csqrtl) |