1/* Copyright (c) 2002 Michael Stumpf <mistumpf@de.pepperl-fuchs.com> 2 Copyright (c) 2006 Dmitry Xmelkov 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are met: 7 8 * Redistributions of source code must retain the above copyright 9 notice, this list of conditions and the following disclaimer. 10 * Redistributions in binary form must reproduce the above copyright 11 notice, this list of conditions and the following disclaimer in 12 the documentation and/or other materials provided with the 13 distribution. 14 * Neither the name of the copyright holders nor the names of 15 contributors may be used to endorse or promote products derived 16 from this software without specific prior written permission. 17 18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 POSSIBILITY OF SUCH DAMAGE. */ 29 30/* $Id: tanh.S 2473 2015-04-09 08:10:22Z pitchumani $ */ 31 32#if !defined(__AVR_TINY__) 33 34#include "fp32def.h" 35#include "asmdef.h" 36 37/* float tanh (float x); 38 The tanh() function returns the hyperbolic tangent of x, which is 39 defined mathematically as sinh(x) / cosh(x). 40 41 Algorithm: 42 if (fabs(x) <= X2SMALL) 43 return Polinom(x) 44 else 45 return sign(x) * (1 - exp(-2*fabs(x))) / (1 + exp(-2*fabs(x))) 46 */ 47 48#define X2SMALL 0x3e0fffff /* < 0.140625 */ 49#define FL_P1 0x3f800000 /* +1.0 */ 50 51#define exp_lo r20 /* float ldexp (float, int exp); */ 52#define exp_hi r21 53 54ENTRY tanh 55 ; save sign 56 push rA3 57 ; is arg too small ? 58 andi rA3, 0x7f 59 ldi ZH, hhi8 (X2SMALL + 1) 60 cpi rA2, hlo8 (X2SMALL + 1) 61 cpc rA3, ZH 62 brsh 1f 63 ; for small x 64 ldi ZL, lo8(.L_table) 65 ldi ZH, hi8(.L_table) 66 XCALL _U(__fp_powsodd) 67 rjmp 2f 68 ; exp(-2*fabs(x)) 691: ori rA3, 0x80 70 ldi exp_lo, lo8(1) 71 ldi exp_hi, hi8(1) 72 XCALL _U(ldexp) ; possible overflow -- no matter 73 XCALL _U(exp) 74 ; save result and calculate 1 + exp(-2*fabs(x)) 75 push rA3 76 push rA2 77 push rA1 78 push rA0 79 ldi rB0, lo8(FL_P1) 80 ldi rB1, hi8(FL_P1) 81 ldi rB2, hlo8(FL_P1) 82 ldi rB3, hhi8(FL_P1) 83 XCALL _U(__addsf3) 84 ; save/restore and calculate 1 - exp(-2*fabs(x)) 85 pop rB0 86 pop rB1 87 pop rB2 88 pop rB3 89 push rA3 90 push rA2 91 push rA1 92 push rA0 93 ldi rA0, lo8(FL_P1) 94 ldi rA1, hi8(FL_P1) 95 ldi rA2, hlo8(FL_P1) 96 ldi rA3, hhi8(FL_P1) 97 XCALL _U(__subsf3) 98 ; restore 99 pop rB0 100 pop rB1 101 pop rB2 102 pop rB3 103 ; divide 104 XCALL _U(__divsf3) 105 ; sign 1062: pop r0 107 sbrc r0, 7 108 subi rA3, 0x80 109 ret 110ENDFUNC 111 112 PGM_SECTION 113.L_table: 114 .byte 2 115 .byte 0x89,0x88,0x08,0x3e ; 2/15 116 .byte 0xab,0xaa,0xaa,0xaa,0xbe ; -1/3 117 .byte 0x00,0x00,0x00,0x80,0x3f ; 1 118 .end 119 120#endif /* !defined(__AVR_TINY__) */ 121