ccoshq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* Complex cosine hyperbole function 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. #ifdef HAVE_FENV_H
  18. # include <fenv.h>
  19. #endif
  20. __complex128
  21. ccoshq (__complex128 x)
  22. {
  23. __complex128 retval;
  24. int rcls = fpclassifyq (__real__ x);
  25. int icls = fpclassifyq (__imag__ x);
  26. if (__builtin_expect (rcls >= QUADFP_ZERO, 1))
  27. {
  28. /* Real part is finite. */
  29. if (__builtin_expect (icls >= QUADFP_ZERO, 1))
  30. {
  31. /* Imaginary part is finite. */
  32. const int t = (int) ((FLT128_MAX_EXP - 1) * M_LN2q);
  33. __float128 sinix, cosix;
  34. if (__builtin_expect (icls != QUADFP_SUBNORMAL, 1))
  35. {
  36. sincosq (__imag__ x, &sinix, &cosix);
  37. }
  38. else
  39. {
  40. sinix = __imag__ x;
  41. cosix = 1.0Q;
  42. }
  43. if (fabsq (__real__ x) > t)
  44. {
  45. __float128 exp_t = expq (t);
  46. __float128 rx = fabsq (__real__ x);
  47. if (signbitq (__real__ x))
  48. sinix = -sinix;
  49. rx -= t;
  50. sinix *= exp_t / 2.0Q;
  51. cosix *= exp_t / 2.0Q;
  52. if (rx > t)
  53. {
  54. rx -= t;
  55. sinix *= exp_t;
  56. cosix *= exp_t;
  57. }
  58. if (rx > t)
  59. {
  60. /* Overflow (original real part of x > 3t). */
  61. __real__ retval = FLT128_MAX * cosix;
  62. __imag__ retval = FLT128_MAX * sinix;
  63. }
  64. else
  65. {
  66. __float128 exp_val = expq (rx);
  67. __real__ retval = exp_val * cosix;
  68. __imag__ retval = exp_val * sinix;
  69. }
  70. }
  71. else
  72. {
  73. __real__ retval = coshq (__real__ x) * cosix;
  74. __imag__ retval = sinhq (__real__ x) * sinix;
  75. }
  76. }
  77. else
  78. {
  79. __imag__ retval = __real__ x == 0.0Q ? 0.0Q : nanq ("");
  80. __real__ retval = nanq ("") + nanq ("");
  81. #ifdef HAVE_FENV_H
  82. if (icls == QUADFP_INFINITE)
  83. feraiseexcept (FE_INVALID);
  84. #endif
  85. }
  86. }
  87. else if (rcls == QUADFP_INFINITE)
  88. {
  89. /* Real part is infinite. */
  90. if (__builtin_expect (icls > QUADFP_ZERO, 1))
  91. {
  92. /* Imaginary part is finite. */
  93. __float128 sinix, cosix;
  94. if (__builtin_expect (icls != QUADFP_SUBNORMAL, 1))
  95. {
  96. sincosq (__imag__ x, &sinix, &cosix);
  97. }
  98. else
  99. {
  100. sinix = __imag__ x;
  101. cosix = 1.0Q;
  102. }
  103. __real__ retval = copysignq (HUGE_VALQ, cosix);
  104. __imag__ retval = (copysignq (HUGE_VALQ, sinix)
  105. * copysignq (1.0Q, __real__ x));
  106. }
  107. else if (icls == QUADFP_ZERO)
  108. {
  109. /* Imaginary part is 0.0. */
  110. __real__ retval = HUGE_VALQ;
  111. __imag__ retval = __imag__ x * copysignq (1.0Q, __real__ x);
  112. }
  113. else
  114. {
  115. /* The addition raises the invalid exception. */
  116. __real__ retval = HUGE_VALQ;
  117. __imag__ retval = nanq ("") + nanq ("");
  118. #ifdef HAVE_FENV_H
  119. if (icls == QUADFP_INFINITE)
  120. feraiseexcept (FE_INVALID);
  121. #endif
  122. }
  123. }
  124. else
  125. {
  126. __real__ retval = nanq ("");
  127. __imag__ retval = __imag__ x == 0.0 ? __imag__ x : nanq ("");
  128. }
  129. return retval;
  130. }