stanh.S 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. |
  2. | stanh.sa 3.1 12/10/90
  3. |
  4. | The entry point sTanh computes the hyperbolic tangent of
  5. | an input argument; sTanhd does the same except for denormalized
  6. | input.
  7. |
  8. | Input: Double-extended number X in location pointed to
  9. | by address register a0.
  10. |
  11. | Output: The value tanh(X) returned in floating-point register Fp0.
  12. |
  13. | Accuracy and Monotonicity: The returned result is within 3 ulps in
  14. | 64 significant bit, i.e. within 0.5001 ulp to 53 bits if the
  15. | result is subsequently rounded to double precision. The
  16. | result is provably monotonic in double precision.
  17. |
  18. | Speed: The program stanh takes approximately 270 cycles.
  19. |
  20. | Algorithm:
  21. |
  22. | TANH
  23. | 1. If |X| >= (5/2) log2 or |X| <= 2**(-40), go to 3.
  24. |
  25. | 2. (2**(-40) < |X| < (5/2) log2) Calculate tanh(X) by
  26. | sgn := sign(X), y := 2|X|, z := expm1(Y), and
  27. | tanh(X) = sgn*( z/(2+z) ).
  28. | Exit.
  29. |
  30. | 3. (|X| <= 2**(-40) or |X| >= (5/2) log2). If |X| < 1,
  31. | go to 7.
  32. |
  33. | 4. (|X| >= (5/2) log2) If |X| >= 50 log2, go to 6.
  34. |
  35. | 5. ((5/2) log2 <= |X| < 50 log2) Calculate tanh(X) by
  36. | sgn := sign(X), y := 2|X|, z := exp(Y),
  37. | tanh(X) = sgn - [ sgn*2/(1+z) ].
  38. | Exit.
  39. |
  40. | 6. (|X| >= 50 log2) Tanh(X) = +-1 (round to nearest). Thus, we
  41. | calculate Tanh(X) by
  42. | sgn := sign(X), Tiny := 2**(-126),
  43. | tanh(X) := sgn - sgn*Tiny.
  44. | Exit.
  45. |
  46. | 7. (|X| < 2**(-40)). Tanh(X) = X. Exit.
  47. |
  48. | Copyright (C) Motorola, Inc. 1990
  49. | All Rights Reserved
  50. |
  51. | For details on the license for this file, please see the
  52. | file, README, in this same directory.
  53. |STANH idnt 2,1 | Motorola 040 Floating Point Software Package
  54. |section 8
  55. #include "fpsp.h"
  56. .set X,FP_SCR5
  57. .set XDCARE,X+2
  58. .set XFRAC,X+4
  59. .set SGN,L_SCR3
  60. .set V,FP_SCR6
  61. BOUNDS1: .long 0x3FD78000,0x3FFFDDCE | ... 2^(-40), (5/2)LOG2
  62. |xref t_frcinx
  63. |xref t_extdnrm
  64. |xref setox
  65. |xref setoxm1
  66. .global stanhd
  67. stanhd:
  68. |--TANH(X) = X FOR DENORMALIZED X
  69. bra t_extdnrm
  70. .global stanh
  71. stanh:
  72. fmovex (%a0),%fp0 | ...LOAD INPUT
  73. fmovex %fp0,X(%a6)
  74. movel (%a0),%d0
  75. movew 4(%a0),%d0
  76. movel %d0,X(%a6)
  77. andl #0x7FFFFFFF,%d0
  78. cmp2l BOUNDS1(%pc),%d0 | ...2**(-40) < |X| < (5/2)LOG2 ?
  79. bcss TANHBORS
  80. |--THIS IS THE USUAL CASE
  81. |--Y = 2|X|, Z = EXPM1(Y), TANH(X) = SIGN(X) * Z / (Z+2).
  82. movel X(%a6),%d0
  83. movel %d0,SGN(%a6)
  84. andl #0x7FFF0000,%d0
  85. addl #0x00010000,%d0 | ...EXPONENT OF 2|X|
  86. movel %d0,X(%a6)
  87. andl #0x80000000,SGN(%a6)
  88. fmovex X(%a6),%fp0 | ...FP0 IS Y = 2|X|
  89. movel %d1,-(%a7)
  90. clrl %d1
  91. fmovemx %fp0-%fp0,(%a0)
  92. bsr setoxm1 | ...FP0 IS Z = EXPM1(Y)
  93. movel (%a7)+,%d1
  94. fmovex %fp0,%fp1
  95. fadds #0x40000000,%fp1 | ...Z+2
  96. movel SGN(%a6),%d0
  97. fmovex %fp1,V(%a6)
  98. eorl %d0,V(%a6)
  99. fmovel %d1,%FPCR |restore users exceptions
  100. fdivx V(%a6),%fp0
  101. bra t_frcinx
  102. TANHBORS:
  103. cmpl #0x3FFF8000,%d0
  104. blt TANHSM
  105. cmpl #0x40048AA1,%d0
  106. bgt TANHHUGE
  107. |-- (5/2) LOG2 < |X| < 50 LOG2,
  108. |--TANH(X) = 1 - (2/[EXP(2X)+1]). LET Y = 2|X|, SGN = SIGN(X),
  109. |--TANH(X) = SGN - SGN*2/[EXP(Y)+1].
  110. movel X(%a6),%d0
  111. movel %d0,SGN(%a6)
  112. andl #0x7FFF0000,%d0
  113. addl #0x00010000,%d0 | ...EXPO OF 2|X|
  114. movel %d0,X(%a6) | ...Y = 2|X|
  115. andl #0x80000000,SGN(%a6)
  116. movel SGN(%a6),%d0
  117. fmovex X(%a6),%fp0 | ...Y = 2|X|
  118. movel %d1,-(%a7)
  119. clrl %d1
  120. fmovemx %fp0-%fp0,(%a0)
  121. bsr setox | ...FP0 IS EXP(Y)
  122. movel (%a7)+,%d1
  123. movel SGN(%a6),%d0
  124. fadds #0x3F800000,%fp0 | ...EXP(Y)+1
  125. eorl #0xC0000000,%d0 | ...-SIGN(X)*2
  126. fmoves %d0,%fp1 | ...-SIGN(X)*2 IN SGL FMT
  127. fdivx %fp0,%fp1 | ...-SIGN(X)2 / [EXP(Y)+1 ]
  128. movel SGN(%a6),%d0
  129. orl #0x3F800000,%d0 | ...SGN
  130. fmoves %d0,%fp0 | ...SGN IN SGL FMT
  131. fmovel %d1,%FPCR |restore users exceptions
  132. faddx %fp1,%fp0
  133. bra t_frcinx
  134. TANHSM:
  135. movew #0x0000,XDCARE(%a6)
  136. fmovel %d1,%FPCR |restore users exceptions
  137. fmovex X(%a6),%fp0 |last inst - possible exception set
  138. bra t_frcinx
  139. TANHHUGE:
  140. |---RETURN SGN(X) - SGN(X)EPS
  141. movel X(%a6),%d0
  142. andl #0x80000000,%d0
  143. orl #0x3F800000,%d0
  144. fmoves %d0,%fp0
  145. andl #0x80000000,%d0
  146. eorl #0x80800000,%d0 | ...-SIGN(X)*EPS
  147. fmovel %d1,%FPCR |restore users exceptions
  148. fadds %d0,%fp0
  149. bra t_frcinx
  150. |end