s_tanhf.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* s_tanhf.c -- float version of s_tanh.c.
  2. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  3. */
  4. /*
  5. * ====================================================
  6. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  7. *
  8. * Developed at SunPro, a Sun Microsystems, Inc. business.
  9. * Permission to use, copy, modify, and distribute this
  10. * software is freely granted, provided that this notice
  11. * is preserved.
  12. * ====================================================
  13. */
  14. #include "cdefs-compat.h"
  15. //__FBSDID("$FreeBSD: src/lib/msun/src/s_tanhf.c,v 1.9 2008/02/22 02:30:36 das Exp $");
  16. #include <openlibm_math.h>
  17. #include "math_private.h"
  18. static const float one=1.0, two=2.0, tiny = 1.0e-30, huge = 1.0e30;
  19. OLM_DLLEXPORT float
  20. tanhf(float x)
  21. {
  22. float t,z;
  23. int32_t jx,ix;
  24. GET_FLOAT_WORD(jx,x);
  25. ix = jx&0x7fffffff;
  26. /* x is INF or NaN */
  27. if(ix>=0x7f800000) {
  28. if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */
  29. else return one/x-one; /* tanh(NaN) = NaN */
  30. }
  31. /* |x| < 9 */
  32. if (ix < 0x41100000) { /* |x|<9 */
  33. if (ix<0x39800000) { /* |x|<2**-12 */
  34. if(huge+x>one) return x; /* tanh(tiny) = tiny with inexact */
  35. }
  36. if (ix>=0x3f800000) { /* |x|>=1 */
  37. t = expm1f(two*fabsf(x));
  38. z = one - two/(t+two);
  39. } else {
  40. t = expm1f(-two*fabsf(x));
  41. z= -t/(t+two);
  42. }
  43. /* |x| >= 9, return +-1 */
  44. } else {
  45. z = one - tiny; /* raise inexact flag */
  46. }
  47. return (jx>=0)? z: -z;
  48. }