cexpq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Return value of complex exponential function for a float type.
  2. Copyright (C) 1997-2018 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. cexpq (__complex128 x)
  19. {
  20. __complex128 retval;
  21. int rcls = fpclassifyq (__real__ x);
  22. int icls = fpclassifyq (__imag__ x);
  23. if (__glibc_likely (rcls >= QUADFP_ZERO))
  24. {
  25. /* Real part is finite. */
  26. if (__glibc_likely (icls >= QUADFP_ZERO))
  27. {
  28. /* Imaginary part is finite. */
  29. const int t = (int) ((FLT128_MAX_EXP - 1) * M_LN2q);
  30. __float128 sinix, cosix;
  31. if (__glibc_likely (fabsq (__imag__ x) > FLT128_MIN))
  32. {
  33. sincosq (__imag__ x, &sinix, &cosix);
  34. }
  35. else
  36. {
  37. sinix = __imag__ x;
  38. cosix = 1;
  39. }
  40. if (__real__ x > t)
  41. {
  42. __float128 exp_t = expq (t);
  43. __real__ x -= t;
  44. sinix *= exp_t;
  45. cosix *= exp_t;
  46. if (__real__ x > t)
  47. {
  48. __real__ x -= t;
  49. sinix *= exp_t;
  50. cosix *= exp_t;
  51. }
  52. }
  53. if (__real__ x > t)
  54. {
  55. /* Overflow (original real part of x > 3t). */
  56. __real__ retval = FLT128_MAX * cosix;
  57. __imag__ retval = FLT128_MAX * sinix;
  58. }
  59. else
  60. {
  61. __float128 exp_val = expq (__real__ x);
  62. __real__ retval = exp_val * cosix;
  63. __imag__ retval = exp_val * sinix;
  64. }
  65. math_check_force_underflow_complex (retval);
  66. }
  67. else
  68. {
  69. /* If the imaginary part is +-inf or NaN and the real part
  70. is not +-inf the result is NaN + iNaN. */
  71. __real__ retval = nanq ("");
  72. __imag__ retval = nanq ("");
  73. feraiseexcept (FE_INVALID);
  74. }
  75. }
  76. else if (__glibc_likely (rcls == QUADFP_INFINITE))
  77. {
  78. /* Real part is infinite. */
  79. if (__glibc_likely (icls >= QUADFP_ZERO))
  80. {
  81. /* Imaginary part is finite. */
  82. __float128 value = signbitq (__real__ x) ? 0 : HUGE_VALQ;
  83. if (icls == QUADFP_ZERO)
  84. {
  85. /* Imaginary part is 0.0. */
  86. __real__ retval = value;
  87. __imag__ retval = __imag__ x;
  88. }
  89. else
  90. {
  91. __float128 sinix, cosix;
  92. if (__glibc_likely (fabsq (__imag__ x) > FLT128_MIN))
  93. {
  94. sincosq (__imag__ x, &sinix, &cosix);
  95. }
  96. else
  97. {
  98. sinix = __imag__ x;
  99. cosix = 1;
  100. }
  101. __real__ retval = copysignq (value, cosix);
  102. __imag__ retval = copysignq (value, sinix);
  103. }
  104. }
  105. else if (signbitq (__real__ x) == 0)
  106. {
  107. __real__ retval = HUGE_VALQ;
  108. __imag__ retval = __imag__ x - __imag__ x;
  109. }
  110. else
  111. {
  112. __real__ retval = 0;
  113. __imag__ retval = copysignq (0, __imag__ x);
  114. }
  115. }
  116. else
  117. {
  118. /* If the real part is NaN the result is NaN + iNaN unless the
  119. imaginary part is zero. */
  120. __real__ retval = nanq ("");
  121. if (icls == QUADFP_ZERO)
  122. __imag__ retval = __imag__ x;
  123. else
  124. {
  125. __imag__ retval = nanq ("");
  126. if (rcls != QUADFP_NAN || icls != QUADFP_NAN)
  127. feraiseexcept (FE_INVALID);
  128. }
  129. }
  130. return retval;
  131. }