s_cexp_template.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Return value of complex exponential function for a float type.
  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 <fenv.h>
  18. #include <math.h>
  19. #include <math_private.h>
  20. #include <math-underflow.h>
  21. #include <float.h>
  22. CFLOAT
  23. M_DECL_FUNC (__cexp) (CFLOAT x)
  24. {
  25. CFLOAT retval;
  26. int rcls = fpclassify (__real__ x);
  27. int icls = fpclassify (__imag__ x);
  28. if (__glibc_likely (rcls >= FP_ZERO))
  29. {
  30. /* Real part is finite. */
  31. if (__glibc_likely (icls >= FP_ZERO))
  32. {
  33. /* Imaginary part is finite. */
  34. const int t = (int) ((M_MAX_EXP - 1) * M_MLIT (M_LN2));
  35. FLOAT sinix, cosix;
  36. if (__glibc_likely (M_FABS (__imag__ x) > M_MIN))
  37. {
  38. M_SINCOS (__imag__ x, &sinix, &cosix);
  39. }
  40. else
  41. {
  42. sinix = __imag__ x;
  43. cosix = 1;
  44. }
  45. if (__real__ x > t)
  46. {
  47. FLOAT exp_t = M_EXP (t);
  48. __real__ x -= t;
  49. sinix *= exp_t;
  50. cosix *= exp_t;
  51. if (__real__ x > t)
  52. {
  53. __real__ x -= t;
  54. sinix *= exp_t;
  55. cosix *= exp_t;
  56. }
  57. }
  58. if (__real__ x > t)
  59. {
  60. /* Overflow (original real part of x > 3t). */
  61. __real__ retval = M_MAX * cosix;
  62. __imag__ retval = M_MAX * sinix;
  63. }
  64. else
  65. {
  66. FLOAT exp_val = M_EXP (__real__ x);
  67. __real__ retval = exp_val * cosix;
  68. __imag__ retval = exp_val * sinix;
  69. }
  70. math_check_force_underflow_complex (retval);
  71. }
  72. else
  73. {
  74. /* If the imaginary part is +-inf or NaN and the real part
  75. is not +-inf the result is NaN + iNaN. */
  76. __real__ retval = M_NAN;
  77. __imag__ retval = M_NAN;
  78. feraiseexcept (FE_INVALID);
  79. }
  80. }
  81. else if (__glibc_likely (rcls == FP_INFINITE))
  82. {
  83. /* Real part is infinite. */
  84. if (__glibc_likely (icls >= FP_ZERO))
  85. {
  86. /* Imaginary part is finite. */
  87. FLOAT value = signbit (__real__ x) ? 0 : M_HUGE_VAL;
  88. if (icls == FP_ZERO)
  89. {
  90. /* Imaginary part is 0.0. */
  91. __real__ retval = value;
  92. __imag__ retval = __imag__ x;
  93. }
  94. else
  95. {
  96. FLOAT sinix, cosix;
  97. if (__glibc_likely (M_FABS (__imag__ x) > M_MIN))
  98. {
  99. M_SINCOS (__imag__ x, &sinix, &cosix);
  100. }
  101. else
  102. {
  103. sinix = __imag__ x;
  104. cosix = 1;
  105. }
  106. __real__ retval = M_COPYSIGN (value, cosix);
  107. __imag__ retval = M_COPYSIGN (value, sinix);
  108. }
  109. }
  110. else if (signbit (__real__ x) == 0)
  111. {
  112. __real__ retval = M_HUGE_VAL;
  113. __imag__ retval = __imag__ x - __imag__ x;
  114. }
  115. else
  116. {
  117. __real__ retval = 0;
  118. __imag__ retval = M_COPYSIGN (0, __imag__ x);
  119. }
  120. }
  121. else
  122. {
  123. /* If the real part is NaN the result is NaN + iNaN unless the
  124. imaginary part is zero. */
  125. __real__ retval = M_NAN;
  126. if (icls == FP_ZERO)
  127. __imag__ retval = __imag__ x;
  128. else
  129. {
  130. __imag__ retval = M_NAN;
  131. if (rcls != FP_NAN || icls != FP_NAN)
  132. feraiseexcept (FE_INVALID);
  133. }
  134. }
  135. return retval;
  136. }
  137. declare_mgen_alias (__cexp, cexp)