ccoshf.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_ccoshf.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. See s_ccosh.c for details.
  29. */
  30. #include "libm.h"
  31. static const float huge = 0x1p127;
  32. float complex ccoshf(float complex z)
  33. {
  34. float x, y, h;
  35. int32_t hx, hy, ix, iy;
  36. x = crealf(z);
  37. y = cimagf(z);
  38. GET_FLOAT_WORD(hx, x);
  39. GET_FLOAT_WORD(hy, y);
  40. ix = 0x7fffffff & hx;
  41. iy = 0x7fffffff & hy;
  42. if (ix < 0x7f800000 && iy < 0x7f800000) {
  43. if (iy == 0)
  44. return CMPLXF(coshf(x), x * y);
  45. if (ix < 0x41100000) /* small x: normal case */
  46. return CMPLXF(coshf(x) * cosf(y), sinhf(x) * sinf(y));
  47. /* |x| >= 9, so cosh(x) ~= exp(|x|) */
  48. if (ix < 0x42b17218) {
  49. /* x < 88.7: expf(|x|) won't overflow */
  50. h = expf(fabsf(x)) * 0.5f;
  51. return CMPLXF(h * cosf(y), copysignf(h, x) * sinf(y));
  52. } else if (ix < 0x4340b1e7) {
  53. /* x < 192.7: scale to avoid overflow */
  54. z = __ldexp_cexpf(CMPLXF(fabsf(x), y), -1);
  55. return CMPLXF(crealf(z), cimagf(z) * copysignf(1, x));
  56. } else {
  57. /* x >= 192.7: the result always overflows */
  58. h = huge * x;
  59. return CMPLXF(h * h * cosf(y), h * sinf(y));
  60. }
  61. }
  62. if (ix == 0 && iy >= 0x7f800000)
  63. return CMPLXF(y - y, copysignf(0, x * (y - y)));
  64. if (iy == 0 && ix >= 0x7f800000) {
  65. if ((hx & 0x7fffff) == 0)
  66. return CMPLXF(x * x, copysignf(0, x) * y);
  67. return CMPLXF(x * x, copysignf(0, (x + x) * y));
  68. }
  69. if (ix < 0x7f800000 && iy >= 0x7f800000)
  70. return CMPLXF(y - y, x * (y - y));
  71. if (ix >= 0x7f800000 && (hx & 0x7fffff) == 0) {
  72. if (iy >= 0x7f800000)
  73. return CMPLXF(x * x, x * (y - y));
  74. return CMPLXF((x * x) * cosf(y), x * sinf(y));
  75. }
  76. return CMPLXF((x * x) * (y - y), (x + x) * (y - y));
  77. }