csqrtq.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Complex square root of a float type.
  2. Copyright (C) 1997-2018 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. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. 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.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "quadmath-imp.h"
  18. __complex128
  19. csqrtq (__complex128 x)
  20. {
  21. __complex128 res;
  22. int rcls = fpclassifyq (__real__ x);
  23. int icls = fpclassifyq (__imag__ x);
  24. if (__glibc_unlikely (rcls <= QUADFP_INFINITE || icls <= QUADFP_INFINITE))
  25. {
  26. if (icls == QUADFP_INFINITE)
  27. {
  28. __real__ res = HUGE_VALQ;
  29. __imag__ res = __imag__ x;
  30. }
  31. else if (rcls == QUADFP_INFINITE)
  32. {
  33. if (__real__ x < 0)
  34. {
  35. __real__ res = icls == QUADFP_NAN ? nanq ("") : 0;
  36. __imag__ res = copysignq (HUGE_VALQ, __imag__ x);
  37. }
  38. else
  39. {
  40. __real__ res = __real__ x;
  41. __imag__ res = (icls == QUADFP_NAN
  42. ? nanq ("") : copysignq (0, __imag__ x));
  43. }
  44. }
  45. else
  46. {
  47. __real__ res = nanq ("");
  48. __imag__ res = nanq ("");
  49. }
  50. }
  51. else
  52. {
  53. if (__glibc_unlikely (icls == QUADFP_ZERO))
  54. {
  55. if (__real__ x < 0)
  56. {
  57. __real__ res = 0;
  58. __imag__ res = copysignq (sqrtq (-__real__ x), __imag__ x);
  59. }
  60. else
  61. {
  62. __real__ res = fabsq (sqrtq (__real__ x));
  63. __imag__ res = copysignq (0, __imag__ x);
  64. }
  65. }
  66. else if (__glibc_unlikely (rcls == QUADFP_ZERO))
  67. {
  68. __float128 r;
  69. if (fabsq (__imag__ x) >= 2 * FLT128_MIN)
  70. r = sqrtq (0.5Q * fabsq (__imag__ x));
  71. else
  72. r = 0.5Q * sqrtq (2 * fabsq (__imag__ x));
  73. __real__ res = r;
  74. __imag__ res = copysignq (r, __imag__ x);
  75. }
  76. else
  77. {
  78. __float128 d, r, s;
  79. int scale = 0;
  80. if (fabsq (__real__ x) > FLT128_MAX / 4)
  81. {
  82. scale = 1;
  83. __real__ x = scalbnq (__real__ x, -2 * scale);
  84. __imag__ x = scalbnq (__imag__ x, -2 * scale);
  85. }
  86. else if (fabsq (__imag__ x) > FLT128_MAX / 4)
  87. {
  88. scale = 1;
  89. if (fabsq (__real__ x) >= 4 * FLT128_MIN)
  90. __real__ x = scalbnq (__real__ x, -2 * scale);
  91. else
  92. __real__ x = 0;
  93. __imag__ x = scalbnq (__imag__ x, -2 * scale);
  94. }
  95. else if (fabsq (__real__ x) < 2 * FLT128_MIN
  96. && fabsq (__imag__ x) < 2 * FLT128_MIN)
  97. {
  98. scale = -((FLT128_MANT_DIG + 1) / 2);
  99. __real__ x = scalbnq (__real__ x, -2 * scale);
  100. __imag__ x = scalbnq (__imag__ x, -2 * scale);
  101. }
  102. d = hypotq (__real__ x, __imag__ x);
  103. /* Use the identity 2 Re res Im res = Im x
  104. to avoid cancellation error in d +/- Re x. */
  105. if (__real__ x > 0)
  106. {
  107. r = sqrtq (0.5Q * (d + __real__ x));
  108. if (scale == 1 && fabsq (__imag__ x) < 1)
  109. {
  110. /* Avoid possible intermediate underflow. */
  111. s = __imag__ x / r;
  112. r = scalbnq (r, scale);
  113. scale = 0;
  114. }
  115. else
  116. s = 0.5Q * (__imag__ x / r);
  117. }
  118. else
  119. {
  120. s = sqrtq (0.5Q * (d - __real__ x));
  121. if (scale == 1 && fabsq (__imag__ x) < 1)
  122. {
  123. /* Avoid possible intermediate underflow. */
  124. r = fabsq (__imag__ x / s);
  125. s = scalbnq (s, scale);
  126. scale = 0;
  127. }
  128. else
  129. r = fabsq (0.5Q * (__imag__ x / s));
  130. }
  131. if (scale)
  132. {
  133. r = scalbnq (r, scale);
  134. s = scalbnq (s, scale);
  135. }
  136. math_check_force_underflow (r);
  137. math_check_force_underflow (s);
  138. __real__ res = r;
  139. __imag__ res = copysignq (s, __imag__ x);
  140. }
  141. }
  142. return res;
  143. }