s_clog_template.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Compute complex natural logarithm.
  2. Copyright (C) 1997-2019 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 <complex.h>
  17. #include <math.h>
  18. #include <math_private.h>
  19. #include <math-underflow.h>
  20. #include <float.h>
  21. CFLOAT
  22. M_DECL_FUNC (__clog) (CFLOAT x)
  23. {
  24. CFLOAT result;
  25. int rcls = fpclassify (__real__ x);
  26. int icls = fpclassify (__imag__ x);
  27. if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO))
  28. {
  29. /* Real and imaginary part are 0.0. */
  30. __imag__ result = signbit (__real__ x) ? (FLOAT) M_MLIT (M_PI) : 0;
  31. __imag__ result = M_COPYSIGN (__imag__ result, __imag__ x);
  32. /* Yes, the following line raises an exception. */
  33. __real__ result = -1 / M_FABS (__real__ x);
  34. }
  35. else if (__glibc_likely (rcls != FP_NAN && icls != FP_NAN))
  36. {
  37. /* Neither real nor imaginary part is NaN. */
  38. FLOAT absx = M_FABS (__real__ x), absy = M_FABS (__imag__ x);
  39. int scale = 0;
  40. if (absx < absy)
  41. {
  42. FLOAT t = absx;
  43. absx = absy;
  44. absy = t;
  45. }
  46. if (absx > M_MAX / 2)
  47. {
  48. scale = -1;
  49. absx = M_SCALBN (absx, scale);
  50. absy = (absy >= M_MIN * 2 ? M_SCALBN (absy, scale) : 0);
  51. }
  52. else if (absx < M_MIN && absy < M_MIN)
  53. {
  54. scale = M_MANT_DIG;
  55. absx = M_SCALBN (absx, scale);
  56. absy = M_SCALBN (absy, scale);
  57. }
  58. if (absx == 1 && scale == 0)
  59. {
  60. __real__ result = M_LOG1P (absy * absy) / 2;
  61. math_check_force_underflow_nonneg (__real__ result);
  62. }
  63. else if (absx > 1 && absx < 2 && absy < 1 && scale == 0)
  64. {
  65. FLOAT d2m1 = (absx - 1) * (absx + 1);
  66. if (absy >= M_EPSILON)
  67. d2m1 += absy * absy;
  68. __real__ result = M_LOG1P (d2m1) / 2;
  69. }
  70. else if (absx < 1
  71. && absx >= M_LIT (0.5)
  72. && absy < M_EPSILON / 2
  73. && scale == 0)
  74. {
  75. FLOAT d2m1 = (absx - 1) * (absx + 1);
  76. __real__ result = M_LOG1P (d2m1) / 2;
  77. }
  78. else if (absx < 1
  79. && absx >= M_LIT (0.5)
  80. && scale == 0
  81. && absx * absx + absy * absy >= M_LIT (0.5))
  82. {
  83. FLOAT d2m1 = M_SUF (__x2y2m1) (absx, absy);
  84. __real__ result = M_LOG1P (d2m1) / 2;
  85. }
  86. else
  87. {
  88. FLOAT d = M_HYPOT (absx, absy);
  89. __real__ result = M_LOG (d) - scale * (FLOAT) M_MLIT (M_LN2);
  90. }
  91. __imag__ result = M_ATAN2 (__imag__ x, __real__ x);
  92. }
  93. else
  94. {
  95. __imag__ result = M_NAN;
  96. if (rcls == FP_INFINITE || icls == FP_INFINITE)
  97. /* Real or imaginary part is infinite. */
  98. __real__ result = M_HUGE_VAL;
  99. else
  100. __real__ result = M_NAN;
  101. }
  102. return result;
  103. }
  104. declare_mgen_alias (__clog, clog)