1*57fb77a1Scgd* $NetBSD: stanh.sa,v 1.3 1994/10/26 07:50:12 cgd Exp $ 2*57fb77a1Scgd 322ef5fa9Smycroft* MOTOROLA MICROPROCESSOR & MEMORY TECHNOLOGY GROUP 422ef5fa9Smycroft* M68000 Hi-Performance Microprocessor Division 522ef5fa9Smycroft* M68040 Software Package 622ef5fa9Smycroft* 722ef5fa9Smycroft* M68040 Software Package Copyright (c) 1993, 1994 Motorola Inc. 822ef5fa9Smycroft* All rights reserved. 922ef5fa9Smycroft* 1022ef5fa9Smycroft* THE SOFTWARE is provided on an "AS IS" basis and without warranty. 1122ef5fa9Smycroft* To the maximum extent permitted by applicable law, 1222ef5fa9Smycroft* MOTOROLA DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED, 1322ef5fa9Smycroft* INCLUDING IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A 1422ef5fa9Smycroft* PARTICULAR PURPOSE and any warranty against infringement with 1522ef5fa9Smycroft* regard to the SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF) 1622ef5fa9Smycroft* and any accompanying written materials. 1722ef5fa9Smycroft* 1822ef5fa9Smycroft* To the maximum extent permitted by applicable law, 1922ef5fa9Smycroft* IN NO EVENT SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER 2022ef5fa9Smycroft* (INCLUDING WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS 2122ef5fa9Smycroft* PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR 2222ef5fa9Smycroft* OTHER PECUNIARY LOSS) ARISING OF THE USE OR INABILITY TO USE THE 2322ef5fa9Smycroft* SOFTWARE. Motorola assumes no responsibility for the maintenance 2422ef5fa9Smycroft* and support of the SOFTWARE. 2522ef5fa9Smycroft* 2622ef5fa9Smycroft* You are hereby granted a copyright license to use, modify, and 2722ef5fa9Smycroft* distribute the SOFTWARE so long as this entire notice is retained 2822ef5fa9Smycroft* without alteration in any modified and/or redistributed versions, 2922ef5fa9Smycroft* and that such modified versions are clearly identified as such. 3022ef5fa9Smycroft* No licenses are granted by implication, estoppel or otherwise 3122ef5fa9Smycroft* under any patents or trademarks of Motorola, Inc. 3222ef5fa9Smycroft 3322ef5fa9Smycroft* 3422ef5fa9Smycroft* stanh.sa 3.1 12/10/90 3522ef5fa9Smycroft* 3622ef5fa9Smycroft* The entry point sTanh computes the hyperbolic tangent of 3722ef5fa9Smycroft* an input argument; sTanhd does the same except for denormalized 3822ef5fa9Smycroft* input. 3922ef5fa9Smycroft* 4022ef5fa9Smycroft* Input: Double-extended number X in location pointed to 4122ef5fa9Smycroft* by address register a0. 4222ef5fa9Smycroft* 4322ef5fa9Smycroft* Output: The value tanh(X) returned in floating-point register Fp0. 4422ef5fa9Smycroft* 4522ef5fa9Smycroft* Accuracy and Monotonicity: The returned result is within 3 ulps in 4622ef5fa9Smycroft* 64 significant bit, i.e. within 0.5001 ulp to 53 bits if the 4722ef5fa9Smycroft* result is subsequently rounded to double precision. The 4822ef5fa9Smycroft* result is provably monotonic in double precision. 4922ef5fa9Smycroft* 5022ef5fa9Smycroft* Speed: The program stanh takes approximately 270 cycles. 5122ef5fa9Smycroft* 5222ef5fa9Smycroft* Algorithm: 5322ef5fa9Smycroft* 5422ef5fa9Smycroft* TANH 5522ef5fa9Smycroft* 1. If |X| >= (5/2) log2 or |X| <= 2**(-40), go to 3. 5622ef5fa9Smycroft* 5722ef5fa9Smycroft* 2. (2**(-40) < |X| < (5/2) log2) Calculate tanh(X) by 5822ef5fa9Smycroft* sgn := sign(X), y := 2|X|, z := expm1(Y), and 5922ef5fa9Smycroft* tanh(X) = sgn*( z/(2+z) ). 6022ef5fa9Smycroft* Exit. 6122ef5fa9Smycroft* 6222ef5fa9Smycroft* 3. (|X| <= 2**(-40) or |X| >= (5/2) log2). If |X| < 1, 6322ef5fa9Smycroft* go to 7. 6422ef5fa9Smycroft* 6522ef5fa9Smycroft* 4. (|X| >= (5/2) log2) If |X| >= 50 log2, go to 6. 6622ef5fa9Smycroft* 6722ef5fa9Smycroft* 5. ((5/2) log2 <= |X| < 50 log2) Calculate tanh(X) by 6822ef5fa9Smycroft* sgn := sign(X), y := 2|X|, z := exp(Y), 6922ef5fa9Smycroft* tanh(X) = sgn - [ sgn*2/(1+z) ]. 7022ef5fa9Smycroft* Exit. 7122ef5fa9Smycroft* 7222ef5fa9Smycroft* 6. (|X| >= 50 log2) Tanh(X) = +-1 (round to nearest). Thus, we 7322ef5fa9Smycroft* calculate Tanh(X) by 7422ef5fa9Smycroft* sgn := sign(X), Tiny := 2**(-126), 7522ef5fa9Smycroft* tanh(X) := sgn - sgn*Tiny. 7622ef5fa9Smycroft* Exit. 7722ef5fa9Smycroft* 7822ef5fa9Smycroft* 7. (|X| < 2**(-40)). Tanh(X) = X. Exit. 7922ef5fa9Smycroft* 8022ef5fa9Smycroft 8122ef5fa9SmycroftSTANH IDNT 2,1 Motorola 040 Floating Point Software Package 8222ef5fa9Smycroft 8322ef5fa9Smycroft section 8 8422ef5fa9Smycroft 8522ef5fa9Smycroft include fpsp.h 8622ef5fa9Smycroft 8722ef5fa9SmycroftX equ FP_SCR5 8822ef5fa9SmycroftXDCARE equ X+2 8922ef5fa9SmycroftXFRAC equ X+4 9022ef5fa9Smycroft 9122ef5fa9SmycroftSGN equ L_SCR3 9222ef5fa9Smycroft 9322ef5fa9SmycroftV equ FP_SCR6 9422ef5fa9Smycroft 9522ef5fa9SmycroftBOUNDS1 DC.L $3FD78000,$3FFFDDCE ... 2^(-40), (5/2)LOG2 9622ef5fa9Smycroft 9722ef5fa9Smycroft xref t_frcinx 9822ef5fa9Smycroft xref t_extdnrm 9922ef5fa9Smycroft xref setox 10022ef5fa9Smycroft xref setoxm1 10122ef5fa9Smycroft 10222ef5fa9Smycroft xdef stanhd 10322ef5fa9Smycroftstanhd: 10422ef5fa9Smycroft*--TANH(X) = X FOR DENORMALIZED X 10522ef5fa9Smycroft 10622ef5fa9Smycroft bra t_extdnrm 10722ef5fa9Smycroft 10822ef5fa9Smycroft xdef stanh 10922ef5fa9Smycroftstanh: 11022ef5fa9Smycroft FMOVE.X (a0),FP0 ...LOAD INPUT 11122ef5fa9Smycroft 11222ef5fa9Smycroft FMOVE.X FP0,X(a6) 11322ef5fa9Smycroft move.l (a0),d0 11422ef5fa9Smycroft move.w 4(a0),d0 11522ef5fa9Smycroft MOVE.L D0,X(a6) 11622ef5fa9Smycroft AND.L #$7FFFFFFF,D0 11722ef5fa9Smycroft CMP2.L BOUNDS1(pc),D0 ...2**(-40) < |X| < (5/2)LOG2 ? 11822ef5fa9Smycroft BCS.B TANHBORS 11922ef5fa9Smycroft 12022ef5fa9Smycroft*--THIS IS THE USUAL CASE 12122ef5fa9Smycroft*--Y = 2|X|, Z = EXPM1(Y), TANH(X) = SIGN(X) * Z / (Z+2). 12222ef5fa9Smycroft 12322ef5fa9Smycroft MOVE.L X(a6),D0 12422ef5fa9Smycroft MOVE.L D0,SGN(a6) 12522ef5fa9Smycroft AND.L #$7FFF0000,D0 12622ef5fa9Smycroft ADD.L #$00010000,D0 ...EXPONENT OF 2|X| 12722ef5fa9Smycroft MOVE.L D0,X(a6) 12822ef5fa9Smycroft AND.L #$80000000,SGN(a6) 12922ef5fa9Smycroft FMOVE.X X(a6),FP0 ...FP0 IS Y = 2|X| 13022ef5fa9Smycroft 13122ef5fa9Smycroft move.l d1,-(a7) 13222ef5fa9Smycroft clr.l d1 13322ef5fa9Smycroft fmovem.x fp0,(a0) 13422ef5fa9Smycroft bsr setoxm1 ...FP0 IS Z = EXPM1(Y) 13522ef5fa9Smycroft move.l (a7)+,d1 13622ef5fa9Smycroft 13722ef5fa9Smycroft FMOVE.X FP0,FP1 13822ef5fa9Smycroft FADD.S #:40000000,FP1 ...Z+2 13922ef5fa9Smycroft MOVE.L SGN(a6),D0 14022ef5fa9Smycroft FMOVE.X FP1,V(a6) 14122ef5fa9Smycroft EOR.L D0,V(a6) 14222ef5fa9Smycroft 14322ef5fa9Smycroft FMOVE.L d1,FPCR ;restore users exceptions 14422ef5fa9Smycroft FDIV.X V(a6),FP0 14522ef5fa9Smycroft bra t_frcinx 14622ef5fa9Smycroft 14722ef5fa9SmycroftTANHBORS: 14822ef5fa9Smycroft CMP.L #$3FFF8000,D0 14922ef5fa9Smycroft BLT.W TANHSM 15022ef5fa9Smycroft 15122ef5fa9Smycroft CMP.L #$40048AA1,D0 15222ef5fa9Smycroft BGT.W TANHHUGE 15322ef5fa9Smycroft 15422ef5fa9Smycroft*-- (5/2) LOG2 < |X| < 50 LOG2, 15522ef5fa9Smycroft*--TANH(X) = 1 - (2/[EXP(2X)+1]). LET Y = 2|X|, SGN = SIGN(X), 15622ef5fa9Smycroft*--TANH(X) = SGN - SGN*2/[EXP(Y)+1]. 15722ef5fa9Smycroft 15822ef5fa9Smycroft MOVE.L X(a6),D0 15922ef5fa9Smycroft MOVE.L D0,SGN(a6) 16022ef5fa9Smycroft AND.L #$7FFF0000,D0 16122ef5fa9Smycroft ADD.L #$00010000,D0 ...EXPO OF 2|X| 16222ef5fa9Smycroft MOVE.L D0,X(a6) ...Y = 2|X| 16322ef5fa9Smycroft AND.L #$80000000,SGN(a6) 16422ef5fa9Smycroft MOVE.L SGN(a6),D0 16522ef5fa9Smycroft FMOVE.X X(a6),FP0 ...Y = 2|X| 16622ef5fa9Smycroft 16722ef5fa9Smycroft move.l d1,-(a7) 16822ef5fa9Smycroft clr.l d1 16922ef5fa9Smycroft fmovem.x fp0,(a0) 17022ef5fa9Smycroft bsr setox ...FP0 IS EXP(Y) 17122ef5fa9Smycroft move.l (a7)+,d1 17222ef5fa9Smycroft move.l SGN(a6),d0 17322ef5fa9Smycroft FADD.S #:3F800000,FP0 ...EXP(Y)+1 17422ef5fa9Smycroft 17522ef5fa9Smycroft EOR.L #$C0000000,D0 ...-SIGN(X)*2 17622ef5fa9Smycroft FMOVE.S d0,FP1 ...-SIGN(X)*2 IN SGL FMT 17722ef5fa9Smycroft FDIV.X FP0,FP1 ...-SIGN(X)2 / [EXP(Y)+1 ] 17822ef5fa9Smycroft 17922ef5fa9Smycroft MOVE.L SGN(a6),D0 18022ef5fa9Smycroft OR.L #$3F800000,D0 ...SGN 18122ef5fa9Smycroft FMOVE.S d0,FP0 ...SGN IN SGL FMT 18222ef5fa9Smycroft 18322ef5fa9Smycroft FMOVE.L d1,FPCR ;restore users exceptions 18422ef5fa9Smycroft FADD.X fp1,FP0 18522ef5fa9Smycroft 18622ef5fa9Smycroft bra t_frcinx 18722ef5fa9Smycroft 18822ef5fa9SmycroftTANHSM: 189eddb30abSmycroft CLR.W XDCARE(a6) 19022ef5fa9Smycroft 19122ef5fa9Smycroft FMOVE.L d1,FPCR ;restore users exceptions 19222ef5fa9Smycroft FMOVE.X X(a6),FP0 ;last inst - possible exception set 19322ef5fa9Smycroft 19422ef5fa9Smycroft bra t_frcinx 19522ef5fa9Smycroft 19622ef5fa9SmycroftTANHHUGE: 19722ef5fa9Smycroft*---RETURN SGN(X) - SGN(X)EPS 19822ef5fa9Smycroft MOVE.L X(a6),D0 19922ef5fa9Smycroft AND.L #$80000000,D0 20022ef5fa9Smycroft OR.L #$3F800000,D0 20122ef5fa9Smycroft FMOVE.S d0,FP0 20222ef5fa9Smycroft AND.L #$80000000,D0 20322ef5fa9Smycroft EOR.L #$80800000,D0 ...-SIGN(X)*EPS 20422ef5fa9Smycroft 20522ef5fa9Smycroft FMOVE.L d1,FPCR ;restore users exceptions 20622ef5fa9Smycroft FADD.S d0,FP0 20722ef5fa9Smycroft 20822ef5fa9Smycroft bra t_frcinx 20922ef5fa9Smycroft 21022ef5fa9Smycroft end 211