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