s_ccosh_template.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Complex cosine hyperbolic function for float types.
  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 (__ccosh) (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 (M_FABS (__real__ x) > t)
  46. {
  47. FLOAT exp_t = M_EXP (t);
  48. FLOAT rx = M_FABS (__real__ x);
  49. if (signbit (__real__ x))
  50. sinix = -sinix;
  51. rx -= t;
  52. sinix *= exp_t / 2;
  53. cosix *= exp_t / 2;
  54. if (rx > t)
  55. {
  56. rx -= t;
  57. sinix *= exp_t;
  58. cosix *= exp_t;
  59. }
  60. if (rx > t)
  61. {
  62. /* Overflow (original real part of x > 3t). */
  63. __real__ retval = M_MAX * cosix;
  64. __imag__ retval = M_MAX * sinix;
  65. }
  66. else
  67. {
  68. FLOAT exp_val = M_EXP (rx);
  69. __real__ retval = exp_val * cosix;
  70. __imag__ retval = exp_val * sinix;
  71. }
  72. }
  73. else
  74. {
  75. __real__ retval = M_COSH (__real__ x) * cosix;
  76. __imag__ retval = M_SINH (__real__ x) * sinix;
  77. }
  78. math_check_force_underflow_complex (retval);
  79. }
  80. else
  81. {
  82. __imag__ retval = __real__ x == 0 ? 0 : M_NAN;
  83. __real__ retval = __imag__ x - __imag__ x;
  84. }
  85. }
  86. else if (rcls == FP_INFINITE)
  87. {
  88. /* Real part is infinite. */
  89. if (__glibc_likely (icls > FP_ZERO))
  90. {
  91. /* Imaginary part is finite. */
  92. FLOAT sinix, cosix;
  93. if (__glibc_likely (M_FABS (__imag__ x) > M_MIN))
  94. {
  95. M_SINCOS (__imag__ x, &sinix, &cosix);
  96. }
  97. else
  98. {
  99. sinix = __imag__ x;
  100. cosix = 1;
  101. }
  102. __real__ retval = M_COPYSIGN (M_HUGE_VAL, cosix);
  103. __imag__ retval = (M_COPYSIGN (M_HUGE_VAL, sinix)
  104. * M_COPYSIGN (1, __real__ x));
  105. }
  106. else if (icls == FP_ZERO)
  107. {
  108. /* Imaginary part is 0.0. */
  109. __real__ retval = M_HUGE_VAL;
  110. __imag__ retval = __imag__ x * M_COPYSIGN (1, __real__ x);
  111. }
  112. else
  113. {
  114. __real__ retval = M_HUGE_VAL;
  115. __imag__ retval = __imag__ x - __imag__ x;
  116. }
  117. }
  118. else
  119. {
  120. __real__ retval = M_NAN;
  121. __imag__ retval = __imag__ x == 0 ? __imag__ x : M_NAN;
  122. }
  123. return retval;
  124. }
  125. declare_mgen_alias (__ccosh, ccosh);