clogq.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Compute complex natural logarithm for complex __float128.
  2. Copyright (C) 1997-2012 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "quadmath-imp.h"
  17. __complex128
  18. clogq (__complex128 x)
  19. {
  20. __complex128 result;
  21. int rcls = fpclassifyq (__real__ x);
  22. int icls = fpclassifyq (__imag__ x);
  23. if (__builtin_expect (rcls == QUADFP_ZERO && icls == QUADFP_ZERO, 0))
  24. {
  25. /* Real and imaginary part are 0.0. */
  26. __imag__ result = signbitq (__real__ x) ? M_PIq : 0.0Q;
  27. __imag__ result = copysignq (__imag__ result, __imag__ x);
  28. /* Yes, the following line raises an exception. */
  29. __real__ result = -1.0Q / fabsq (__real__ x);
  30. }
  31. else if (__builtin_expect (rcls != QUADFP_NAN && icls != QUADFP_NAN, 1))
  32. {
  33. /* Neither real nor imaginary part is NaN. */
  34. __float128 absx = fabsq (__real__ x), absy = fabsq (__imag__ x);
  35. int scale = 0;
  36. if (absx < absy)
  37. {
  38. __float128 t = absx;
  39. absx = absy;
  40. absy = t;
  41. }
  42. if (absx > FLT128_MAX / 2.0)
  43. {
  44. scale = -1;
  45. absx = scalbnq (absx, scale);
  46. absy = (absy >= FLT128_MIN * 2.0Q ? scalbnq (absy, scale) : 0.0Q);
  47. }
  48. else if (absx < FLT128_MIN && absy < FLT128_MIN)
  49. {
  50. scale = FLT128_MANT_DIG;
  51. absx = scalbnq (absx, scale);
  52. absy = scalbnq (absy, scale);
  53. }
  54. if (absx == 1.0Q && scale == 0)
  55. {
  56. __float128 absy2 = absy * absy;
  57. if (absy2 <= FLT128_MIN * 2.0Q)
  58. __real__ result = absy2 / 2.0Q - absy2 * absy2 / 4.0Q;
  59. else
  60. __real__ result = log1pq (absy2) / 2.0Q;
  61. }
  62. else if (absx > 1.0Q && absx < 2.0Q && absy < 1.0Q && scale == 0)
  63. {
  64. __float128 d2m1 = (absx - 1.0Q) * (absx + 1.0Q);
  65. if (absy >= FLT128_EPSILON)
  66. d2m1 += absy * absy;
  67. __real__ result = log1pq (d2m1) / 2.0Q;
  68. }
  69. else if (absx < 1.0Q
  70. && absx >= 0.75Q
  71. && absy < FLT128_EPSILON / 2.0Q
  72. && scale == 0)
  73. {
  74. __float128 d2m1 = (absx - 1.0Q) * (absx + 1.0Q);
  75. __real__ result = log1pq (d2m1) / 2.0Q;
  76. }
  77. else if (absx < 1.0 && (absx >= 0.75Q || absy >= 0.5Q) && scale == 0)
  78. {
  79. __float128 d2m1 = __quadmath_x2y2m1q (absx, absy);
  80. __real__ result = log1pq (d2m1) / 2.0Q;
  81. }
  82. else
  83. {
  84. __float128 d = hypotq (absx, absy);
  85. __real__ result = logq (d) - scale * M_LN2q;
  86. }
  87. __imag__ result = atan2q (__imag__ x, __real__ x);
  88. }
  89. else
  90. {
  91. __imag__ result = nanq ("");
  92. if (rcls == QUADFP_INFINITE || icls == QUADFP_INFINITE)
  93. /* Real or imaginary part is infinite. */
  94. __real__ result = HUGE_VALQ;
  95. else
  96. __real__ result = nanq ("");
  97. }
  98. return result;
  99. }