clog10q.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Compute complex base 10 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. /* log_10 (2). */
  18. #define M_LOG10_2q 0.3010299956639811952137388947244930267682Q
  19. __complex128
  20. clog10q (__complex128 x)
  21. {
  22. __complex128 result;
  23. int rcls = fpclassifyq (__real__ x);
  24. int icls = fpclassifyq (__imag__ x);
  25. if (__builtin_expect (rcls == QUADFP_ZERO && icls == QUADFP_ZERO, 0))
  26. {
  27. /* Real and imaginary part are 0.0. */
  28. __imag__ result = signbitq (__real__ x) ? M_PIq : 0.0Q;
  29. __imag__ result = copysignq (__imag__ result, __imag__ x);
  30. /* Yes, the following line raises an exception. */
  31. __real__ result = -1.0Q / fabsq (__real__ x);
  32. }
  33. else if (__builtin_expect (rcls != QUADFP_NAN && icls != QUADFP_NAN, 1))
  34. {
  35. /* Neither real nor imaginary part is NaN. */
  36. __float128 absx = fabsq (__real__ x), absy = fabsq (__imag__ x);
  37. int scale = 0;
  38. if (absx < absy)
  39. {
  40. __float128 t = absx;
  41. absx = absy;
  42. absy = t;
  43. }
  44. if (absx > FLT128_MAX / 2.0Q)
  45. {
  46. scale = -1;
  47. absx = scalbnq (absx, scale);
  48. absy = (absy >= FLT128_MIN * 2.0Q ? scalbnq (absy, scale) : 0.0Q);
  49. }
  50. else if (absx < FLT128_MIN && absy < FLT128_MIN)
  51. {
  52. scale = FLT128_MANT_DIG;
  53. absx = scalbnq (absx, scale);
  54. absy = scalbnq (absy, scale);
  55. }
  56. if (absx == 1.0Q && scale == 0)
  57. {
  58. __float128 absy2 = absy * absy;
  59. if (absy2 <= FLT128_MIN * 2.0Q * M_LN10q)
  60. __real__ result
  61. = (absy2 / 2.0Q - absy2 * absy2 / 4.0Q) * M_LOG10Eq;
  62. else
  63. __real__ result = log1pq (absy2) * (M_LOG10Eq / 2.0Q);
  64. }
  65. else if (absx > 1.0Q && absx < 2.0Q && absy < 1.0Q && scale == 0)
  66. {
  67. __float128 d2m1 = (absx - 1.0Q) * (absx + 1.0Q);
  68. if (absy >= FLT128_EPSILON)
  69. d2m1 += absy * absy;
  70. __real__ result = log1pq (d2m1) * (M_LOG10Eq / 2.0Q);
  71. }
  72. else if (absx < 1.0Q
  73. && absx >= 0.75Q
  74. && absy < FLT128_EPSILON / 2.0Q
  75. && scale == 0)
  76. {
  77. __float128 d2m1 = (absx - 1.0Q) * (absx + 1.0Q);
  78. __real__ result = log1pq (d2m1) * (M_LOG10Eq / 2.0Q);
  79. }
  80. else if (absx < 1.0Q && (absx >= 0.75Q || absy >= 0.5Q) && scale == 0)
  81. {
  82. __float128 d2m1 = __quadmath_x2y2m1q (absx, absy);
  83. __real__ result = log1pq (d2m1) * (M_LOG10Eq / 2.0Q);
  84. }
  85. else
  86. {
  87. __float128 d = hypotq (absx, absy);
  88. __real__ result = log10q (d) - scale * M_LOG10_2q;
  89. }
  90. __imag__ result = M_LOG10Eq * atan2q (__imag__ x, __real__ x);
  91. }
  92. else
  93. {
  94. __imag__ result = nanq ("");
  95. if (rcls == QUADFP_INFINITE || icls == QUADFP_INFINITE)
  96. /* Real or imaginary part is infinite. */
  97. __real__ result = HUGE_VALQ;
  98. else
  99. __real__ result = nanq ("");
  100. }
  101. return result;
  102. }