1
0

ccosh.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_ccosh.c */
  2. /*-
  3. * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice unmodified, this list of conditions, and the following
  11. * disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * Hyperbolic cosine of a complex argument z = x + i y.
  29. *
  30. * cosh(z) = cosh(x+iy)
  31. * = cosh(x) cos(y) + i sinh(x) sin(y).
  32. *
  33. * Exceptional values are noted in the comments within the source code.
  34. * These values and the return value were taken from n1124.pdf.
  35. */
  36. #include "libm.h"
  37. static const double huge = 0x1p1023;
  38. double complex ccosh(double complex z)
  39. {
  40. double x, y, h;
  41. int32_t hx, hy, ix, iy, lx, ly;
  42. x = creal(z);
  43. y = cimag(z);
  44. EXTRACT_WORDS(hx, lx, x);
  45. EXTRACT_WORDS(hy, ly, y);
  46. ix = 0x7fffffff & hx;
  47. iy = 0x7fffffff & hy;
  48. /* Handle the nearly-non-exceptional cases where x and y are finite. */
  49. if (ix < 0x7ff00000 && iy < 0x7ff00000) {
  50. if ((iy | ly) == 0)
  51. return CMPLX(cosh(x), x * y);
  52. if (ix < 0x40360000) /* small x: normal case */
  53. return CMPLX(cosh(x) * cos(y), sinh(x) * sin(y));
  54. /* |x| >= 22, so cosh(x) ~= exp(|x|) */
  55. if (ix < 0x40862e42) {
  56. /* x < 710: exp(|x|) won't overflow */
  57. h = exp(fabs(x)) * 0.5;
  58. return CMPLX(h * cos(y), copysign(h, x) * sin(y));
  59. } else if (ix < 0x4096bbaa) {
  60. /* x < 1455: scale to avoid overflow */
  61. z = __ldexp_cexp(CMPLX(fabs(x), y), -1);
  62. return CMPLX(creal(z), cimag(z) * copysign(1, x));
  63. } else {
  64. /* x >= 1455: the result always overflows */
  65. h = huge * x;
  66. return CMPLX(h * h * cos(y), h * sin(y));
  67. }
  68. }
  69. /*
  70. * cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0.
  71. * The sign of 0 in the result is unspecified. Choice = normally
  72. * the same as dNaN. Raise the invalid floating-point exception.
  73. *
  74. * cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0.
  75. * The sign of 0 in the result is unspecified. Choice = normally
  76. * the same as d(NaN).
  77. */
  78. if ((ix | lx) == 0 && iy >= 0x7ff00000)
  79. return CMPLX(y - y, copysign(0, x * (y - y)));
  80. /*
  81. * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
  82. *
  83. * cosh(NaN +- I 0) = d(NaN) + I sign(d(NaN, +-0))0.
  84. * The sign of 0 in the result is unspecified.
  85. */
  86. if ((iy | ly) == 0 && ix >= 0x7ff00000) {
  87. if (((hx & 0xfffff) | lx) == 0)
  88. return CMPLX(x * x, copysign(0, x) * y);
  89. return CMPLX(x * x, copysign(0, (x + x) * y));
  90. }
  91. /*
  92. * cosh(x +- I Inf) = dNaN + I dNaN.
  93. * Raise the invalid floating-point exception for finite nonzero x.
  94. *
  95. * cosh(x + I NaN) = d(NaN) + I d(NaN).
  96. * Optionally raises the invalid floating-point exception for finite
  97. * nonzero x. Choice = don't raise (except for signaling NaNs).
  98. */
  99. if (ix < 0x7ff00000 && iy >= 0x7ff00000)
  100. return CMPLX(y - y, x * (y - y));
  101. /*
  102. * cosh(+-Inf + I NaN) = +Inf + I d(NaN).
  103. *
  104. * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
  105. * The sign of Inf in the result is unspecified. Choice = always +.
  106. * Raise the invalid floating-point exception.
  107. *
  108. * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y)
  109. */
  110. if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) {
  111. if (iy >= 0x7ff00000)
  112. return CMPLX(x * x, x * (y - y));
  113. return CMPLX((x * x) * cos(y), x * sin(y));
  114. }
  115. /*
  116. * cosh(NaN + I NaN) = d(NaN) + I d(NaN).
  117. *
  118. * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
  119. * Optionally raises the invalid floating-point exception.
  120. * Choice = raise.
  121. *
  122. * cosh(NaN + I y) = d(NaN) + I d(NaN).
  123. * Optionally raises the invalid floating-point exception for finite
  124. * nonzero y. Choice = don't raise (except for signaling NaNs).
  125. */
  126. return CMPLX((x * x) * (y - y), (x + x) * (y - y));
  127. }