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

https://gitlab.com/generic-library/bionic · C · 156 lines · 52 code · 18 blank · 86 comment · 19 complexity · e4ca4c1e2ed6cbe16e1318ec892af676 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 sine of a complex argument z = x + i y.
  28. *
  29. * sinh(z) = sinh(x+iy)
  30. * = sinh(x) cos(y) + i cosh(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 sinh(conj(z)) == conj(sinh(z)) and sinh(-z) == -sinh(z).
  36. */
  37. #include <sys/cdefs.h>
  38. __FBSDID("$FreeBSD: head/lib/msun/src/s_csinh.c 284426 2015-06-15 20:16:53Z tijl $");
  39. #include <complex.h>
  40. #include <math.h>
  41. #include "math_private.h"
  42. static const double huge = 0x1p1023;
  43. double complex
  44. csinh(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(sinh(x), y));
  58. if (ix < 0x40360000) /* |x| < 22: normal case */
  59. return (CMPLX(sinh(x) * cos(y), cosh(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(copysign(h, x) * cos(y), h * 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) * copysign(1, x), cimag(z)));
  69. } else {
  70. /* x >= 1455: the result always overflows */
  71. h = huge * x;
  72. return (CMPLX(h * cos(y), h * h * sin(y)));
  73. }
  74. }
  75. /*
  76. * sinh(+-0 +- I Inf) = +-0 + I dNaN.
  77. * The sign of 0 in the result is unspecified. Choice = same sign
  78. * as the argument. Raise the invalid floating-point exception.
  79. *
  80. * sinh(+-0 +- I NaN) = +-0 + I d(NaN).
  81. * The sign of 0 in the result is unspecified. Choice = same sign
  82. * as the argument.
  83. */
  84. if ((ix | lx) == 0) /* && iy >= 0x7ff00000 */
  85. return (CMPLX(x, y - y));
  86. /*
  87. * sinh(+-Inf +- I 0) = +-Inf + I +-0.
  88. *
  89. * sinh(NaN +- I 0) = d(NaN) + I +-0.
  90. */
  91. if ((iy | ly) == 0) /* && ix >= 0x7ff00000 */
  92. return (CMPLX(x + x, y));
  93. /*
  94. * sinh(x +- I Inf) = dNaN + I dNaN.
  95. * Raise the invalid floating-point exception for finite nonzero x.
  96. *
  97. * sinh(x + I NaN) = d(NaN) + I d(NaN).
  98. * Optionally raises the invalid floating-point exception for finite
  99. * nonzero x. Choice = don't raise (except for signaling NaNs).
  100. */
  101. if (ix < 0x7ff00000) /* && iy >= 0x7ff00000 */
  102. return (CMPLX(y - y, y - y));
  103. /*
  104. * sinh(+-Inf + I NaN) = +-Inf + I d(NaN).
  105. * The sign of Inf in the result is unspecified. Choice = same sign
  106. * as the argument.
  107. *
  108. * sinh(+-Inf +- I Inf) = +-Inf + I dNaN.
  109. * The sign of Inf in the result is unspecified. Choice = same sign
  110. * as the argument. Raise the invalid floating-point exception.
  111. *
  112. * sinh(+-Inf + I y) = +-Inf cos(y) + I Inf sin(y)
  113. */
  114. if (ix == 0x7ff00000 && lx == 0) {
  115. if (iy >= 0x7ff00000)
  116. return (CMPLX(x, y - y));
  117. return (CMPLX(x * cos(y), INFINITY * sin(y)));
  118. }
  119. /*
  120. * sinh(NaN1 + I NaN2) = d(NaN1, NaN2) + I d(NaN1, NaN2).
  121. *
  122. * sinh(NaN +- I Inf) = d(NaN, dNaN) + I d(NaN, dNaN).
  123. * Optionally raises the invalid floating-point exception.
  124. * Choice = raise.
  125. *
  126. * sinh(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 (CMPLX((x + x) * (y - y), (x * x) * (y - y)));
  131. }
  132. double complex
  133. csin(double complex z)
  134. {
  135. /* csin(z) = -I * csinh(I * z) = I * conj(csinh(I * conj(z))). */
  136. z = csinh(CMPLX(cimag(z), creal(z)));
  137. return (CMPLX(cimag(z), creal(z)));
  138. }