summaryrefslogtreecommitdiff
path: root/test/math/elog.c
blob: bc517b197de13cfee2ec137bb0b3e92df3eb8cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*						xlog.c	*/
/* natural logarithm */
/* by Stephen L. Moshier. */

#include "mconf.h"
#include "ehead.h"



void elog( x, y )
unsigned short *x, *y;
{
unsigned short xx[NE], z[NE], a[NE], b[NE], t[NE], qj[NE];
long ex;
int fex;


if( x[NE-1] & (unsigned short )0x8000 )
	{
	eclear(y);
	mtherr( "elog", DOMAIN );
	return;
	}
if( ecmp( x, ezero ) == 0 )
	{
	einfin( y );
	eneg(y);
	mtherr( "elog", SING );
	return;
	}
if( ecmp( x, eone ) == 0 )
	{
	eclear( y );
	return;
	}

/* range reduction: log x = log( 2**ex * m ) = ex * log2 + log m */
efrexp( x, &fex, xx );
/*
emov(x, xx );
ex = xx[NX-1] & 0x7fff;
ex -= 0x3ffe;
xx[NX-1] = 0x3ffe;
*/

/* Adjust range to 1/sqrt(2), sqrt(2) */
esqrt2[NE-1] -= 1;
if( ecmp( xx, esqrt2 ) < 0 )
	{
	fex -= 1;
	emul( xx, etwo, xx );
	}
esqrt2[NE-1] += 1;

esub( eone, xx, a );
if( a[NE-1] == 0 )
	{
	eclear( y );
	goto logdon;
	}
eadd( eone, xx, b );
ediv( b, a, y );	/* store (x-1)/(x+1) in y */

emul( y, y, z );

emov( eone, a );
emov( eone, b );
emov( eone, qj );
do
	{
	eadd( etwo, qj, qj );	/* 2 * i + 1		*/
	emul( z, a, a );
	ediv( qj, a, t );
	eadd( t, b, b );
	}
while( ((b[NE-1] & 0x7fff) - (t[NE-1] & 0x7fff)) < NBITS );


emul( b, y, y );
emul( y, etwo, y );

logdon:

/* now add log of 2**ex */
if( fex != 0 )
	{
	ex = fex;
	ltoe( &ex, b );
	emul( elog2, b, b );
	eadd( b, y, y );
	}
}