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