1*b7275c88Smartynas /* $OpenBSD: eexp.c,v 1.1 2011/07/02 18:11:01 martynas Exp $ */
2*b7275c88Smartynas
3*b7275c88Smartynas /*
4*b7275c88Smartynas * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5*b7275c88Smartynas *
6*b7275c88Smartynas * Permission to use, copy, modify, and distribute this software for any
7*b7275c88Smartynas * purpose with or without fee is hereby granted, provided that the above
8*b7275c88Smartynas * copyright notice and this permission notice appear in all copies.
9*b7275c88Smartynas *
10*b7275c88Smartynas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*b7275c88Smartynas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*b7275c88Smartynas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*b7275c88Smartynas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*b7275c88Smartynas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*b7275c88Smartynas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*b7275c88Smartynas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*b7275c88Smartynas */
18*b7275c88Smartynas
19*b7275c88Smartynas /* xexp.c */
20*b7275c88Smartynas /* exponential function check routine */
21*b7275c88Smartynas /* by Stephen L. Moshier. */
22*b7275c88Smartynas
23*b7275c88Smartynas
24*b7275c88Smartynas #include "ehead.h"
25*b7275c88Smartynas
eexp(x,y)26*b7275c88Smartynas void eexp( x, y )
27*b7275c88Smartynas unsigned short *x, *y;
28*b7275c88Smartynas {
29*b7275c88Smartynas unsigned short num[NE], den[NE], x2[NE];
30*b7275c88Smartynas long i;
31*b7275c88Smartynas unsigned short sign, expchk;
32*b7275c88Smartynas
33*b7275c88Smartynas /* range reduction theory: x = i + f, 0<=f<1;
34*b7275c88Smartynas * e**x = e**i * e**f
35*b7275c88Smartynas * e**i = 2**(i/log 2).
36*b7275c88Smartynas * Let i/log2 = i1 + f1, 0<=f1<1.
37*b7275c88Smartynas * Then e**i = 2**i1 * 2**f1, so
38*b7275c88Smartynas * e**x = 2**i1 * e**(log 2 * f1) * e**f.
39*b7275c88Smartynas */
40*b7275c88Smartynas if( ecmp(x, ezero) == 0 )
41*b7275c88Smartynas {
42*b7275c88Smartynas emov( eone, y );
43*b7275c88Smartynas return;
44*b7275c88Smartynas }
45*b7275c88Smartynas emov(x, x2);
46*b7275c88Smartynas expchk = x2[NE-1];
47*b7275c88Smartynas sign = expchk & 0x8000;
48*b7275c88Smartynas x2[NE-1] &= 0x7fff;
49*b7275c88Smartynas
50*b7275c88Smartynas /* Test for excessively large argument */
51*b7275c88Smartynas expchk &= 0x7fff;
52*b7275c88Smartynas if( expchk > (EXONE + 15) )
53*b7275c88Smartynas {
54*b7275c88Smartynas eclear( y );
55*b7275c88Smartynas if( sign == 0 )
56*b7275c88Smartynas einfin( y );
57*b7275c88Smartynas return;
58*b7275c88Smartynas }
59*b7275c88Smartynas
60*b7275c88Smartynas eifrac( x2, &i, num ); /* x = i + f */
61*b7275c88Smartynas
62*b7275c88Smartynas if( i != 0 )
63*b7275c88Smartynas {
64*b7275c88Smartynas ltoe( &i, den ); /* floating point i */
65*b7275c88Smartynas ediv( elog2, den, den ); /* i/log 2 */
66*b7275c88Smartynas eifrac( den, &i, den ); /* i/log 2 = i1 + f1 */
67*b7275c88Smartynas emul( elog2, den, den ); /* log 2 * f1 */
68*b7275c88Smartynas eadd( den, num, x2 ); /* log 2 * f1 + f */
69*b7275c88Smartynas }
70*b7275c88Smartynas
71*b7275c88Smartynas /*x2[NE-1] -= 1;*/
72*b7275c88Smartynas eldexp( x2, -1L, x2 ); /* divide by 2 */
73*b7275c88Smartynas etanh( x2, x2 ); /* tanh( x/2 ) */
74*b7275c88Smartynas eadd( x2, eone, num ); /* 1 + tanh */
75*b7275c88Smartynas eneg( x2 );
76*b7275c88Smartynas eadd( x2, eone, den ); /* 1 - tanh */
77*b7275c88Smartynas ediv( den, num, y ); /* (1 + tanh)/(1 - tanh) */
78*b7275c88Smartynas
79*b7275c88Smartynas /*y[NE-1] += i;*/
80*b7275c88Smartynas if( sign )
81*b7275c88Smartynas {
82*b7275c88Smartynas ediv( y, eone, y );
83*b7275c88Smartynas i = -i;
84*b7275c88Smartynas }
85*b7275c88Smartynas eldexp( y, i, y ); /* multiply by 2**i */
86*b7275c88Smartynas }
87