/libm/upstream-freebsd/lib/msun/src/s_ccosh.c

https://gitlab.com/generic-library/bionic · C · 156 lines · 51 code · 18 blank · 87 comment · 19 complexity · b55d538bb41c6ced81f94d156930b8e5 MD5 · raw file

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