s_ctanhf.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*-
  2. * Copyright (c) 2011 David Schultz
  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 tangent of a complex argument z. See s_ctanh.c for details.
  28. */
  29. #include "cdefs-compat.h"
  30. //__FBSDID("$FreeBSD: src/lib/msun/src/s_ctanhf.c,v 1.2 2011/10/21 06:30:16 das Exp $");
  31. #include <openlibm_complex.h>
  32. #include <openlibm_math.h>
  33. #include "math_private.h"
  34. OLM_DLLEXPORT float complex
  35. ctanhf(float complex z)
  36. {
  37. float x, y;
  38. float t, beta, s, rho, denom;
  39. u_int32_t hx, ix;
  40. x = crealf(z);
  41. y = cimagf(z);
  42. GET_FLOAT_WORD(hx, x);
  43. ix = hx & 0x7fffffff;
  44. if (ix >= 0x7f800000) {
  45. if (ix & 0x7fffff)
  46. return (CMPLXF(x, (y == 0 ? y : x * y)));
  47. SET_FLOAT_WORD(x, hx - 0x40000000);
  48. return (CMPLXF(x,
  49. copysignf(0, isinf(y) ? y : sinf(y) * cosf(y))));
  50. }
  51. if (!isfinite(y))
  52. return (CMPLXF(y - y, y - y));
  53. if (ix >= 0x41300000) { /* x >= 11 */
  54. float exp_mx = expf(-fabsf(x));
  55. return (CMPLXF(copysignf(1, x),
  56. 4 * sinf(y) * cosf(y) * exp_mx * exp_mx));
  57. }
  58. t = tanf(y);
  59. beta = 1.0 + t * t;
  60. s = sinhf(x);
  61. rho = sqrtf(1 + s * s);
  62. denom = 1 + beta * s * s;
  63. return (CMPLXF((beta * rho * s) / denom, t / denom));
  64. }
  65. OLM_DLLEXPORT float complex
  66. ctanf(float complex z)
  67. {
  68. z = ctanhf(CMPLXF(-cimagf(z), crealf(z)));
  69. return (CMPLXF(cimagf(z), -crealf(z)));
  70. }