1 /* $OpenBSD: etanh.c,v 1.1 2011/07/02 18:11:01 martynas Exp $ */
2
3 /*
4 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /* xtanh.c */
20 /* hyperbolic tangent check routine */
21 /* this subroutine is used by the exponential function routine */
22 /* by Stephen L. Moshier. */
23
24
25
26 #include "ehead.h"
27
28
etanh(x,y)29 void etanh( x, y )
30 unsigned short *x, *y;
31 {
32 unsigned short e[NE], r[NE], j[NE], xx[NE], m2[NE];
33 short i, n;
34 long lj;
35
36 emov( x, r );
37 r[NE-1] &= (unsigned short )0x7fff;
38 if( ecmp(r, eone) >= 0 )
39 {
40 /* tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
41 * Note eexp() calls xtanh, but with an argument less than (1 + log 2)/2.
42 */
43 eexp( r, e );
44 ediv( e, eone, r );
45 esub( r, e, xx );
46 eadd( r, e, j );
47 ediv( j, xx, y );
48 return;
49 }
50
51 emov( etwo, m2 );
52 eneg( m2 );
53
54 n = NBITS/8; /* Number of terms to do in the continued fraction */
55 lj = 2 * n + 1;
56 ltoe( &lj, j );
57
58 emov( j, e );
59 emul( x, x, xx );
60
61 /* continued fraction */
62 for( i=0; i<n; i++)
63 {
64 ediv( e, xx, r );
65 eadd( m2, j, j );
66 eadd( r, j, e );
67 }
68
69 ediv( e, x, y );
70 }
71