s_ccosh.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. */
  35. #include "cdefs-compat.h"
  36. //__FBSDID("$FreeBSD: src/lib/msun/src/s_ccosh.c,v 1.2 2011/10/21 06:29:32 das Exp $");
  37. #include <complex.h>
  38. #include <openlibm.h>
  39. #include "math_private.h"
  40. static const double huge = 0x1p1023;
  41. DLLEXPORT double complex
  42. ccosh(double complex z)
  43. {
  44. double x, y, h;
  45. int32_t hx, hy, ix, iy, lx, ly;
  46. x = creal(z);
  47. y = cimag(z);
  48. EXTRACT_WORDS(hx, lx, x);
  49. EXTRACT_WORDS(hy, ly, y);
  50. ix = 0x7fffffff & hx;
  51. iy = 0x7fffffff & hy;
  52. /* Handle the nearly-non-exceptional cases where x and y are finite. */
  53. if (ix < 0x7ff00000 && iy < 0x7ff00000) {
  54. if ((iy | ly) == 0)
  55. return (cpack(cosh(x), x * y));
  56. if (ix < 0x40360000) /* small x: normal case */
  57. return (cpack(cosh(x) * cos(y), sinh(x) * sin(y)));
  58. /* |x| >= 22, so cosh(x) ~= exp(|x|) */
  59. if (ix < 0x40862e42) {
  60. /* x < 710: exp(|x|) won't overflow */
  61. h = exp(fabs(x)) * 0.5;
  62. return (cpack(h * cos(y), copysign(h, x) * sin(y)));
  63. } else if (ix < 0x4096bbaa) {
  64. /* x < 1455: scale to avoid overflow */
  65. z = __ldexp_cexp(cpack(fabs(x), y), -1);
  66. return (cpack(creal(z), cimag(z) * copysign(1, x)));
  67. } else {
  68. /* x >= 1455: the result always overflows */
  69. h = huge * x;
  70. return (cpack(h * h * cos(y), h * sin(y)));
  71. }
  72. }
  73. /*
  74. * cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0.
  75. * The sign of 0 in the result is unspecified. Choice = normally
  76. * the same as dNaN. Raise the invalid floating-point exception.
  77. *
  78. * cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0.
  79. * The sign of 0 in the result is unspecified. Choice = normally
  80. * the same as d(NaN).
  81. */
  82. if ((ix | lx) == 0 && iy >= 0x7ff00000)
  83. return (cpack(y - y, copysign(0, x * (y - y))));
  84. /*
  85. * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
  86. *
  87. * cosh(NaN +- I 0) = d(NaN) + I sign(d(NaN, +-0))0.
  88. * The sign of 0 in the result is unspecified.
  89. */
  90. if ((iy | ly) == 0 && ix >= 0x7ff00000) {
  91. if (((hx & 0xfffff) | lx) == 0)
  92. return (cpack(x * x, copysign(0, x) * y));
  93. return (cpack(x * x, copysign(0, (x + x) * y)));
  94. }
  95. /*
  96. * cosh(x +- I Inf) = dNaN + I dNaN.
  97. * Raise the invalid floating-point exception for finite nonzero x.
  98. *
  99. * cosh(x + I NaN) = d(NaN) + I d(NaN).
  100. * Optionally raises the invalid floating-point exception for finite
  101. * nonzero x. Choice = don't raise (except for signaling NaNs).
  102. */
  103. if (ix < 0x7ff00000 && iy >= 0x7ff00000)
  104. return (cpack(y - y, x * (y - y)));
  105. /*
  106. * cosh(+-Inf + I NaN) = +Inf + I d(NaN).
  107. *
  108. * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
  109. * The sign of Inf in the result is unspecified. Choice = always +.
  110. * Raise the invalid floating-point exception.
  111. *
  112. * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y)
  113. */
  114. if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) {
  115. if (iy >= 0x7ff00000)
  116. return (cpack(x * x, x * (y - y)));
  117. return (cpack((x * x) * cos(y), x * sin(y)));
  118. }
  119. /*
  120. * cosh(NaN + I NaN) = d(NaN) + I d(NaN).
  121. *
  122. * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
  123. * Optionally raises the invalid floating-point exception.
  124. * Choice = raise.
  125. *
  126. * cosh(NaN + I y) = d(NaN) + I d(NaN).
  127. * Optionally raises the invalid floating-point exception for finite
  128. * nonzero y. Choice = don't raise (except for signaling NaNs).
  129. */
  130. return (cpack((x * x) * (y - y), (x + x) * (y - y)));
  131. }
  132. DLLEXPORT double complex
  133. ccos(double complex z)
  134. {
  135. /* ccos(z) = ccosh(I * z) */
  136. return (ccosh(cpack(-cimag(z), creal(z))));
  137. }