xref: /openbsd/regress/lib/libc/cephes/elog.c (revision b7275c88)
1 /*	$OpenBSD: elog.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 /*						xlog.c	*/
20 /* natural logarithm */
21 /* by Stephen L. Moshier. */
22 
23 #include "mconf.h"
24 #include "ehead.h"
25 
26 
27 
elog(x,y)28 void elog( x, y )
29 unsigned short *x, *y;
30 {
31 unsigned short xx[NE], z[NE], a[NE], b[NE], t[NE], qj[NE];
32 long ex;
33 int fex;
34 
35 
36 if( x[NE-1] & (unsigned short )0x8000 )
37 	{
38 	eclear(y);
39 	mtherr( "elog", DOMAIN );
40 	return;
41 	}
42 if( ecmp( x, ezero ) == 0 )
43 	{
44 	einfin( y );
45 	eneg(y);
46 	mtherr( "elog", SING );
47 	return;
48 	}
49 if( ecmp( x, eone ) == 0 )
50 	{
51 	eclear( y );
52 	return;
53 	}
54 
55 /* range reduction: log x = log( 2**ex * m ) = ex * log2 + log m */
56 efrexp( x, &fex, xx );
57 /*
58 emov(x, xx );
59 ex = xx[NX-1] & 0x7fff;
60 ex -= 0x3ffe;
61 xx[NX-1] = 0x3ffe;
62 */
63 
64 /* Adjust range to 1/sqrt(2), sqrt(2) */
65 esqrt2[NE-1] -= 1;
66 if( ecmp( xx, esqrt2 ) < 0 )
67 	{
68 	fex -= 1;
69 	emul( xx, etwo, xx );
70 	}
71 esqrt2[NE-1] += 1;
72 
73 esub( eone, xx, a );
74 if( a[NE-1] == 0 )
75 	{
76 	eclear( y );
77 	goto logdon;
78 	}
79 eadd( eone, xx, b );
80 ediv( b, a, y );	/* store (x-1)/(x+1) in y */
81 
82 emul( y, y, z );
83 
84 emov( eone, a );
85 emov( eone, b );
86 emov( eone, qj );
87 do
88 	{
89 	eadd( etwo, qj, qj );	/* 2 * i + 1		*/
90 	emul( z, a, a );
91 	ediv( qj, a, t );
92 	eadd( t, b, b );
93 	}
94 while( ((b[NE-1] & 0x7fff) - (t[NE-1] & 0x7fff)) < NBITS );
95 
96 
97 emul( b, y, y );
98 emul( y, etwo, y );
99 
100 logdon:
101 
102 /* now add log of 2**ex */
103 if( fex != 0 )
104 	{
105 	ex = fex;
106 	ltoe( &ex, b );
107 	emul( elog2, b, b );
108 	eadd( b, y, y );
109 	}
110 }
111