s_clog10_template.c 3.5 KB

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