ctanhf.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_ctanhf.c */
  2. /*-
  3. * Copyright (c) 2011 David Schultz
  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 tangent of a complex argument z. See s_ctanh.c for details.
  29. */
  30. #include "libm.h"
  31. float complex ctanhf(float complex z)
  32. {
  33. float x, y;
  34. float t, beta, s, rho, denom;
  35. uint32_t hx, ix;
  36. x = crealf(z);
  37. y = cimagf(z);
  38. GET_FLOAT_WORD(hx, x);
  39. ix = hx & 0x7fffffff;
  40. if (ix >= 0x7f800000) {
  41. if (ix & 0x7fffff)
  42. return CMPLXF(x, (y == 0 ? y : x * y));
  43. SET_FLOAT_WORD(x, hx - 0x40000000);
  44. return CMPLXF(x, copysignf(0, isinf(y) ? y : sinf(y) * cosf(y)));
  45. }
  46. if (!isfinite(y))
  47. return CMPLXF(y - y, y - y);
  48. if (ix >= 0x41300000) { /* x >= 11 */
  49. float exp_mx = expf(-fabsf(x));
  50. return CMPLXF(copysignf(1, x), 4 * sinf(y) * cosf(y) * exp_mx * exp_mx);
  51. }
  52. t = tanf(y);
  53. beta = 1.0 + t * t;
  54. s = sinhf(x);
  55. rho = sqrtf(1 + s * s);
  56. denom = 1 + beta * s * s;
  57. return CMPLXF((beta * rho * s) / denom, t / denom);
  58. }