ctanhq.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Complex hyperbole tangent for __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. ctanhq (__complex128 x)
  22. {
  23. __complex128 res;
  24. if (__builtin_expect (!finiteq (__real__ x) || !finiteq (__imag__ x), 0))
  25. {
  26. if (__quadmath_isinf_nsq (__real__ x))
  27. {
  28. __real__ res = copysignq (1.0Q, __real__ x);
  29. __imag__ res = copysignq (0.0Q, __imag__ x);
  30. }
  31. else if (__imag__ x == 0.0Q)
  32. {
  33. res = x;
  34. }
  35. else
  36. {
  37. __real__ res = nanq ("");
  38. __imag__ res = nanq ("");
  39. #ifdef HAVE_FENV_H
  40. if (__quadmath_isinf_nsq (__imag__ x))
  41. feraiseexcept (FE_INVALID);
  42. #endif
  43. }
  44. }
  45. else
  46. {
  47. __float128 sinix, cosix;
  48. __float128 den;
  49. const int t = (int) ((FLT128_MAX_EXP - 1) * M_LN2q / 2);
  50. int icls = fpclassifyq (__imag__ x);
  51. /* tanh(x+iy) = (sinh(2x) + i*sin(2y))/(cosh(2x) + cos(2y))
  52. = (sinh(x)*cosh(x) + i*sin(y)*cos(y))/(sinh(x)^2 + cos(y)^2). */
  53. if (__builtin_expect (icls != QUADFP_SUBNORMAL, 1))
  54. {
  55. sincosq (__imag__ x, &sinix, &cosix);
  56. }
  57. else
  58. {
  59. sinix = __imag__ x;
  60. cosix = 1.0Q;
  61. }
  62. if (fabsq (__real__ x) > t)
  63. {
  64. /* Avoid intermediate overflow when the imaginary part of
  65. the result may be subnormal. Ignoring negligible terms,
  66. the real part is +/- 1, the imaginary part is
  67. sin(y)*cos(y)/sinh(x)^2 = 4*sin(y)*cos(y)/exp(2x). */
  68. __float128 exp_2t = expq (2 * t);
  69. __real__ res = copysignq (1.0, __real__ x);
  70. __imag__ res = 4 * sinix * cosix;
  71. __real__ x = fabsq (__real__ x);
  72. __real__ x -= t;
  73. __imag__ res /= exp_2t;
  74. if (__real__ x > t)
  75. {
  76. /* Underflow (original real part of x has absolute value
  77. > 2t). */
  78. __imag__ res /= exp_2t;
  79. }
  80. else
  81. __imag__ res /= expq (2 * __real__ x);
  82. }
  83. else
  84. {
  85. __float128 sinhrx, coshrx;
  86. if (fabsq (__real__ x) > FLT128_MIN)
  87. {
  88. sinhrx = sinhq (__real__ x);
  89. coshrx = coshq (__real__ x);
  90. }
  91. else
  92. {
  93. sinhrx = __real__ x;
  94. coshrx = 1.0Q;
  95. }
  96. if (fabsq (sinhrx) > fabsq (cosix) * FLT128_EPSILON)
  97. den = sinhrx * sinhrx + cosix * cosix;
  98. else
  99. den = cosix * cosix;
  100. __real__ res = sinhrx * coshrx / den;
  101. __imag__ res = sinix * cosix / den;
  102. }
  103. }
  104. return res;
  105. }