summaryrefslogtreecommitdiff
path: root/regress/lib/libc/cephes/elog.c
blob: 079cc754f483657f7db517b8dc955abf1ac21a3c (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*	$OpenBSD: elog.c,v 1.1 2011/07/02 18:11:01 martynas Exp $	*/

/*
 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*						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 );
	}
}