/math/s_csqrtl.c

https://github.com/nitinkamble/x32-glibc · C · 110 lines · 79 code · 9 blank · 22 comment · 17 complexity · 71bfdb61a31600dedad4deecef47747f MD5 · raw file

  1. /* Complex square root of long double value.
  2. Copyright (C) 1997, 1998, 2005 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, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. #include <complex.h>
  19. #include <math.h>
  20. #include "math_private.h"
  21. __complex__ long double
  22. __csqrtl (__complex__ long double x)
  23. {
  24. __complex__ long double res;
  25. int rcls = fpclassify (__real__ x);
  26. int icls = fpclassify (__imag__ x);
  27. if (rcls <= FP_INFINITE || icls <= FP_INFINITE)
  28. {
  29. if (icls == FP_INFINITE)
  30. {
  31. __real__ res = HUGE_VALL;
  32. __imag__ res = __imag__ x;
  33. }
  34. else if (rcls == FP_INFINITE)
  35. {
  36. if (__real__ x < 0.0)
  37. {
  38. __real__ res = icls == FP_NAN ? __nanl ("") : 0;
  39. __imag__ res = __copysignl (HUGE_VALL, __imag__ x);
  40. }
  41. else
  42. {
  43. __real__ res = __real__ x;
  44. __imag__ res = (icls == FP_NAN
  45. ? __nanl ("") : __copysignl (0.0, __imag__ x));
  46. }
  47. }
  48. else
  49. {
  50. __real__ res = __nanl ("");
  51. __imag__ res = __nanl ("");
  52. }
  53. }
  54. else
  55. {
  56. if (icls == FP_ZERO)
  57. {
  58. if (__real__ x < 0.0)
  59. {
  60. __real__ res = 0.0;
  61. __imag__ res = __copysignl (__ieee754_sqrtl (-__real__ x),
  62. __imag__ x);
  63. }
  64. else
  65. {
  66. __real__ res = fabsl (__ieee754_sqrtl (__real__ x));
  67. __imag__ res = __copysignl (0.0, __imag__ x);
  68. }
  69. }
  70. else if (rcls == FP_ZERO)
  71. {
  72. long double r = __ieee754_sqrtl (0.5 * fabsl (__imag__ x));
  73. __real__ res = r;
  74. __imag__ res = __copysignl (r, __imag__ x);
  75. }
  76. else
  77. {
  78. long double d, r, s;
  79. d = __ieee754_hypotl (__real__ x, __imag__ x);
  80. /* Use the identity 2 Re res Im res = Im x
  81. to avoid cancellation error in d +/- Re x. */
  82. if (__real__ x > 0)
  83. {
  84. r = __ieee754_sqrtl (0.5L * d + 0.5L * __real__ x);
  85. s = (0.5L * __imag__ x) / r;
  86. }
  87. else
  88. {
  89. s = __ieee754_sqrtl (0.5L * d - 0.5L * __real__ x);
  90. r = fabsl ((0.5L * __imag__ x) / s);
  91. }
  92. __real__ res = r;
  93. __imag__ res = __copysignl (s, __imag__ x);
  94. }
  95. }
  96. return res;
  97. }
  98. weak_alias (__csqrtl, csqrtl)