Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.7 KiB

  1. /***
  2. *tanh.c - hyperbolic tangent
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *Revision History:
  9. * 8-15-91 GDP written
  10. * 12-22-91 GDP support IEEE exceptions
  11. * 06-23-92 GDP tanh(denormal) now raises underflow exception (NCEG)
  12. * 02-06-95 JWM Mac merge
  13. * 05-17-99 PML Remove all Macintosh support.
  14. *
  15. *******************************************************************************/
  16. #include <math.h>
  17. #include <trans.h>
  18. /* constants */
  19. static double const EPS = 5.16987882845642297e-26; /* 2^(-53) / 2 */
  20. static double const XBIG = 1.90615474653984960096e+001; /* ln(2)(53+2)/2 */
  21. static double const C0 = 0.54930614433405484570; /* ln(3)/2 */
  22. /* constants for rational approximation */
  23. static double const p0 = -0.16134119023996228053e+4;
  24. static double const p1 = -0.99225929672236083313e+2;
  25. static double const p2 = -0.96437492777225469787e+0;
  26. static double const q0 = 0.48402357071988688686e+4;
  27. static double const q1 = 0.22337720718962312926e+4;
  28. static double const q2 = 0.11274474380534949335e+3;
  29. static double const q3 = 0.10000000000000000000e+1;
  30. #define Q(g) ((((g) + q2) * (g) + q1) * (g) + q0)
  31. #define R(g) ((((p2 * (g) + p1) * (g) + p0) * (g)) / Q(g))
  32. #if !defined(_M_PPC) && !defined(_M_AMD64)
  33. #pragma function(tanh)
  34. #endif
  35. /***
  36. *double tanh(double x) - hyperbolic tangent
  37. *
  38. *Purpose:
  39. * Compute the hyperbolic tangent of a number.
  40. * The algorithm (reduction / rational approximation) is
  41. * taken from Cody & Waite.
  42. *
  43. *Entry:
  44. *
  45. *Exit:
  46. *
  47. *Exceptions:
  48. * I P
  49. *******************************************************************************/
  50. double tanh(double x)
  51. {
  52. uintptr_t savedcw;
  53. double f,g;
  54. double result;
  55. /* save user fp control word */
  56. savedcw = _maskfp();
  57. if (IS_D_SPECIAL(x)){
  58. switch(_sptype(x)) {
  59. case T_PINF:
  60. RETURN(savedcw,1.0);
  61. case T_NINF:
  62. RETURN(savedcw,-1.0);
  63. case T_QNAN:
  64. return _handle_qnan1(OP_TANH, x, savedcw);
  65. default: //T_SNAN
  66. return _except1(FP_I,OP_TANH,x,_s2qnan(x),savedcw);
  67. }
  68. }
  69. if (x == 0.0) {
  70. // no precision exception
  71. RETURN(savedcw,x);
  72. }
  73. f = ABS(x);
  74. if (f > XBIG) {
  75. result = 1;
  76. }
  77. else if (f > C0) {
  78. result = 0.5 - 1.0 / (exp(f+f) + 1.0);
  79. result = result + result;
  80. }
  81. else if (f < EPS) {
  82. result = f;
  83. if (IS_D_DENORM(result)) {
  84. return _except1(FP_U | FP_P,OP_TANH,x,_add_exp(x, IEEE_ADJUST),savedcw);
  85. }
  86. }
  87. else {
  88. g = f * f;
  89. result = f + f * R(g);
  90. }
  91. if (x < 0)
  92. result = -result;
  93. RETURN_INEXACT1(OP_TANH,x,result,savedcw);
  94. }