1 /* $OpenBSD: eexp.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 /* xexp.c */
20 /* exponential function check routine */
21 /* by Stephen L. Moshier. */
22
23
24 #include "ehead.h"
25
eexp(x,y)26 void eexp( x, y )
27 unsigned short *x, *y;
28 {
29 unsigned short num[NE], den[NE], x2[NE];
30 long i;
31 unsigned short sign, expchk;
32
33 /* range reduction theory: x = i + f, 0<=f<1;
34 * e**x = e**i * e**f
35 * e**i = 2**(i/log 2).
36 * Let i/log2 = i1 + f1, 0<=f1<1.
37 * Then e**i = 2**i1 * 2**f1, so
38 * e**x = 2**i1 * e**(log 2 * f1) * e**f.
39 */
40 if( ecmp(x, ezero) == 0 )
41 {
42 emov( eone, y );
43 return;
44 }
45 emov(x, x2);
46 expchk = x2[NE-1];
47 sign = expchk & 0x8000;
48 x2[NE-1] &= 0x7fff;
49
50 /* Test for excessively large argument */
51 expchk &= 0x7fff;
52 if( expchk > (EXONE + 15) )
53 {
54 eclear( y );
55 if( sign == 0 )
56 einfin( y );
57 return;
58 }
59
60 eifrac( x2, &i, num ); /* x = i + f */
61
62 if( i != 0 )
63 {
64 ltoe( &i, den ); /* floating point i */
65 ediv( elog2, den, den ); /* i/log 2 */
66 eifrac( den, &i, den ); /* i/log 2 = i1 + f1 */
67 emul( elog2, den, den ); /* log 2 * f1 */
68 eadd( den, num, x2 ); /* log 2 * f1 + f */
69 }
70
71 /*x2[NE-1] -= 1;*/
72 eldexp( x2, -1L, x2 ); /* divide by 2 */
73 etanh( x2, x2 ); /* tanh( x/2 ) */
74 eadd( x2, eone, num ); /* 1 + tanh */
75 eneg( x2 );
76 eadd( x2, eone, den ); /* 1 - tanh */
77 ediv( den, num, y ); /* (1 + tanh)/(1 - tanh) */
78
79 /*y[NE-1] += i;*/
80 if( sign )
81 {
82 ediv( y, eone, y );
83 i = -i;
84 }
85 eldexp( y, i, y ); /* multiply by 2**i */
86 }
87