1*         $NetBSD: stanh.sa,v 1.3 1994/10/26 07:50:12 cgd Exp $
2
3*         MOTOROLA MICROPROCESSOR & MEMORY TECHNOLOGY GROUP
4*         M68000 Hi-Performance Microprocessor Division
5*         M68040 Software Package
6*
7*         M68040 Software Package Copyright (c) 1993, 1994 Motorola Inc.
8*         All rights reserved.
9*
10*         THE SOFTWARE is provided on an "AS IS" basis and without warranty.
11*         To the maximum extent permitted by applicable law,
12*         MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED,
13*         INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
14*         PARTICULAR PURPOSE and any warranty against infringement with
15*         regard to the SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF)
16*         and any accompanying written materials.
17*
18*         To the maximum extent permitted by applicable law,
19*         IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER
20*         (INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS
21*         PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR
22*         OTHER PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE
23*         SOFTWARE.  Motorola assumes no responsibility for the maintenance
24*         and support of the SOFTWARE.
25*
26*         You are hereby granted a copyright license to use, modify, and
27*         distribute the SOFTWARE so long as this entire notice is retained
28*         without alteration in any modified and/or redistributed versions,
29*         and that such modified versions are clearly identified as such.
30*         No licenses are granted by implication, estoppel or otherwise
31*         under any patents or trademarks of Motorola, Inc.
32
33*
34*         stanh.sa 3.1 12/10/90
35*
36*         The entry point sTanh computes the hyperbolic tangent of
37*         an input argument; sTanhd does the same except for denormalized
38*         input.
39*
40*         Input: Double-extended number X in location pointed to
41*                   by address register a0.
42*
43*         Output: The value tanh(X) returned in floating-point register Fp0.
44*
45*         Accuracy and Monotonicity: The returned result is within 3 ulps in
46*                   64 significant bit, i.e. within 0.5001 ulp to 53 bits if the
47*                   result is subsequently rounded to double precision. The
48*                   result is provably monotonic in double precision.
49*
50*         Speed: The program stanh takes approximately 270 cycles.
51*
52*         Algorithm:
53*
54*         TANH
55*         1. If |X| >= (5/2) log2 or |X| <= 2**(-40), go to 3.
56*
57*         2. (2**(-40) < |X| < (5/2) log2) Calculate tanh(X) by
58*                   sgn := sign(X), y := 2|X|, z := expm1(Y), and
59*                   tanh(X) = sgn*( z/(2+z) ).
60*                   Exit.
61*
62*         3. (|X| <= 2**(-40) or |X| >= (5/2) log2). If |X| < 1,
63*                   go to 7.
64*
65*         4. (|X| >= (5/2) log2) If |X| >= 50 log2, go to 6.
66*
67*         5. ((5/2) log2 <= |X| < 50 log2) Calculate tanh(X) by
68*                   sgn := sign(X), y := 2|X|, z := exp(Y),
69*                   tanh(X) = sgn - [ sgn*2/(1+z) ].
70*                   Exit.
71*
72*         6. (|X| >= 50 log2) Tanh(X) = +-1 (round to nearest). Thus, we
73*                   calculate Tanh(X) by
74*                   sgn := sign(X), Tiny := 2**(-126),
75*                   tanh(X) := sgn - sgn*Tiny.
76*                   Exit.
77*
78*         7. (|X| < 2**(-40)). Tanh(X) = X.       Exit.
79*
80
81STANH     IDNT      2,1 Motorola 040 Floating Point Software Package
82
83          section   8
84
85          include fpsp.h
86
87X         equ       FP_SCR5
88XDCARE    equ       X+2
89XFRAC     equ       X+4
90
91SGN       equ       L_SCR3
92
93V         equ       FP_SCR6
94
95BOUNDS1   DC.L $3FD78000,$3FFFDDCE ... 2^(-40), (5/2)LOG2
96
97          xref      t_frcinx
98          xref      t_extdnrm
99          xref      setox
100          xref      setoxm1
101
102          xdef      stanhd
103stanhd:
104*--TANH(X) = X FOR DENORMALIZED X
105
106          bra                 t_extdnrm
107
108          xdef      stanh
109stanh:
110          FMOVE.X             (a0),FP0  ...LOAD INPUT
111
112          FMOVE.X             FP0,X(a6)
113          move.l              (a0),d0
114          move.w              4(a0),d0
115          MOVE.L              D0,X(a6)
116          AND.L               #$7FFFFFFF,D0
117          CMP2.L              BOUNDS1(pc),D0      ...2**(-40) < |X| < (5/2)LOG2 ?
118          BCS.B               TANHBORS
119
120*--THIS IS THE USUAL CASE
121*--Y = 2|X|, Z = EXPM1(Y), TANH(X) = SIGN(X) * Z / (Z+2).
122
123          MOVE.L              X(a6),D0
124          MOVE.L              D0,SGN(a6)
125          AND.L               #$7FFF0000,D0
126          ADD.L               #$00010000,D0       ...EXPONENT OF 2|X|
127          MOVE.L              D0,X(a6)
128          AND.L               #$80000000,SGN(a6)
129          FMOVE.X             X(a6),FP0           ...FP0 IS Y = 2|X|
130
131          move.l              d1,-(a7)
132          clr.l               d1
133          fmovem.x  fp0,(a0)
134          bsr                 setoxm1             ...FP0 IS Z = EXPM1(Y)
135          move.l              (a7)+,d1
136
137          FMOVE.X             FP0,FP1
138          FADD.S              #:40000000,FP1      ...Z+2
139          MOVE.L              SGN(a6),D0
140          FMOVE.X             FP1,V(a6)
141          EOR.L               D0,V(a6)
142
143          FMOVE.L             d1,FPCR             ;restore users exceptions
144          FDIV.X              V(a6),FP0
145          bra                 t_frcinx
146
147TANHBORS:
148          CMP.L               #$3FFF8000,D0
149          BLT.W               TANHSM
150
151          CMP.L               #$40048AA1,D0
152          BGT.W               TANHHUGE
153
154*-- (5/2) LOG2 < |X| < 50 LOG2,
155*--TANH(X) = 1 - (2/[EXP(2X)+1]). LET Y = 2|X|, SGN = SIGN(X),
156*--TANH(X) = SGN -  SGN*2/[EXP(Y)+1].
157
158          MOVE.L              X(a6),D0
159          MOVE.L              D0,SGN(a6)
160          AND.L               #$7FFF0000,D0
161          ADD.L               #$00010000,D0       ...EXPO OF 2|X|
162          MOVE.L              D0,X(a6)            ...Y = 2|X|
163          AND.L               #$80000000,SGN(a6)
164          MOVE.L              SGN(a6),D0
165          FMOVE.X             X(a6),FP0           ...Y = 2|X|
166
167          move.l              d1,-(a7)
168          clr.l               d1
169          fmovem.x  fp0,(a0)
170          bsr                 setox               ...FP0 IS EXP(Y)
171          move.l              (a7)+,d1
172          move.l              SGN(a6),d0
173          FADD.S              #:3F800000,FP0      ...EXP(Y)+1
174
175          EOR.L               #$C0000000,D0       ...-SIGN(X)*2
176          FMOVE.S             d0,FP1              ...-SIGN(X)*2 IN SGL FMT
177          FDIV.X              FP0,FP1             ...-SIGN(X)2 / [EXP(Y)+1 ]
178
179          MOVE.L              SGN(a6),D0
180          OR.L                #$3F800000,D0       ...SGN
181          FMOVE.S             d0,FP0              ...SGN IN SGL FMT
182
183          FMOVE.L             d1,FPCR             ;restore users exceptions
184          FADD.X              fp1,FP0
185
186          bra                 t_frcinx
187
188TANHSM:
189          CLR.W               XDCARE(a6)
190
191          FMOVE.L             d1,FPCR             ;restore users exceptions
192          FMOVE.X             X(a6),FP0           ;last inst - possible exception set
193
194          bra                 t_frcinx
195
196TANHHUGE:
197*---RETURN SGN(X) - SGN(X)EPS
198          MOVE.L              X(a6),D0
199          AND.L               #$80000000,D0
200          OR.L                #$3F800000,D0
201          FMOVE.S             d0,FP0
202          AND.L               #$80000000,D0
203          EOR.L               #$80800000,D0       ...-SIGN(X)*EPS
204
205          FMOVE.L             d1,FPCR             ;restore users exceptions
206          FADD.S              d0,FP0
207
208          bra                 t_frcinx
209
210          end
211