1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  *
4  * Use and reproduction of this software are granted  in  accordance  with
5  * the terms and conditions specified in  the  Berkeley  Software  License
6  * Agreement (in particular, this entails acknowledgement of the programs'
7  * source, and inclusion of this notice) with the additional understanding
8  * that  all  recipients  should regard themselves as participants  in  an
9  * ongoing  research  project and hence should  feel  obligated  to report
10  * their  experiences (good or bad) with these elementary function  codes,
11  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12  */
13 
14 #ifndef lint
15 static char sccsid[] =
16 "@(#)log__L.c	1.2 (Berkeley) 8/21/85; 5.1 (ucb.elefunt) 11/30/87";
17 #endif	/* not lint */
18 
19 /* log__L(Z)
20  *		LOG(1+X) - 2S			       X
21  * RETURN      ---------------  WHERE Z = S*S,  S = ------- , 0 <= Z <= .0294...
22  *		      S				     2 + X
23  *
24  * DOUBLE PRECISION (VAX D FORMAT 56 bits or IEEE DOUBLE 53 BITS)
25  * KERNEL FUNCTION FOR LOG; TO BE USED IN LOG1P, LOG, AND POW FUNCTIONS
26  * CODED IN C BY K.C. NG, 1/19/85;
27  * REVISED BY K.C. Ng, 2/3/85, 4/16/85.
28  *
29  * Method :
30  *	1. Polynomial approximation: let s = x/(2+x).
31  *	   Based on log(1+x) = log(1+s) - log(1-s)
32  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
33  *
34  *	   (log(1+x) - 2s)/s is computed by
35  *
36  *	       z*(L1 + z*(L2 + z*(... (L7 + z*L8)...)))
37  *
38  *	   where z=s*s. (See the listing below for Lk's values.) The
39  *	   coefficients are obtained by a special Remez algorithm.
40  *
41  * Accuracy:
42  *	Assuming no rounding error, the maximum magnitude of the approximation
43  *	error (absolute) is 2**(-58.49) for IEEE double, and 2**(-63.63)
44  *	for VAX D format.
45  *
46  * Constants:
47  * The hexadecimal values are the intended ones for the following constants.
48  * The decimal values may be used, provided that the compiler will convert
49  * from decimal to binary accurately enough to produce the hexadecimal values
50  * shown.
51  */
52 
53 #if defined(vax)||defined(tahoe)	/* VAX D format (56 bits) */
54 #ifdef vax
55 #define _0x(A,B)	0x/**/A/**/B
56 #else	/* vax */
57 #define _0x(A,B)	0x/**/B/**/A
58 #endif	/* vax */
59 /* static double */
60 /* L1     =  6.6666666666666703212E-1    , Hex  2^  0   *  .AAAAAAAAAAAAC5 */
61 /* L2     =  3.9999999999970461961E-1    , Hex  2^ -1   *  .CCCCCCCCCC2684 */
62 /* L3     =  2.8571428579395698188E-1    , Hex  2^ -1   *  .92492492F85782 */
63 /* L4     =  2.2222221233634724402E-1    , Hex  2^ -2   *  .E38E3839B7AF2C */
64 /* L5     =  1.8181879517064680057E-1    , Hex  2^ -2   *  .BA2EB4CC39655E */
65 /* L6     =  1.5382888777946145467E-1    , Hex  2^ -2   *  .9D8551E8C5781D */
66 /* L7     =  1.3338356561139403517E-1    , Hex  2^ -2   *  .8895B3907FCD92 */
67 /* L8     =  1.2500000000000000000E-1    , Hex  2^ -2   *  .80000000000000 */
68 static long        L1x[] = { _0x(aaaa,402a), _0x(aac5,aaaa)};
69 static long        L2x[] = { _0x(cccc,3fcc), _0x(2684,cccc)};
70 static long        L3x[] = { _0x(4924,3f92), _0x(5782,92f8)};
71 static long        L4x[] = { _0x(8e38,3f63), _0x(af2c,39b7)};
72 static long        L5x[] = { _0x(2eb4,3f3a), _0x(655e,cc39)};
73 static long        L6x[] = { _0x(8551,3f1d), _0x(781d,e8c5)};
74 static long        L7x[] = { _0x(95b3,3f08), _0x(cd92,907f)};
75 static long        L8x[] = { _0x(0000,3f00), _0x(0000,0000)};
76 #define       L1    (*(double*)L1x)
77 #define       L2    (*(double*)L2x)
78 #define       L3    (*(double*)L3x)
79 #define       L4    (*(double*)L4x)
80 #define       L5    (*(double*)L5x)
81 #define       L6    (*(double*)L6x)
82 #define       L7    (*(double*)L7x)
83 #define       L8    (*(double*)L8x)
84 #else	/* defined(vax)||defined(tahoe)	*/
85 static double
86 L1     =  6.6666666666667340202E-1    , /*Hex  2^ -1   *  1.5555555555592 */
87 L2     =  3.9999999999416702146E-1    , /*Hex  2^ -2   *  1.999999997FF24 */
88 L3     =  2.8571428742008753154E-1    , /*Hex  2^ -2   *  1.24924941E07B4 */
89 L4     =  2.2222198607186277597E-1    , /*Hex  2^ -3   *  1.C71C52150BEA6 */
90 L5     =  1.8183562745289935658E-1    , /*Hex  2^ -3   *  1.74663CC94342F */
91 L6     =  1.5314087275331442206E-1    , /*Hex  2^ -3   *  1.39A1EC014045B */
92 L7     =  1.4795612545334174692E-1    ; /*Hex  2^ -3   *  1.2F039F0085122 */
93 #endif	/* defined(vax)||defined(tahoe)	*/
94 
95 double log__L(z)
96 double z;
97 {
98 #if defined(vax)||defined(tahoe)
99     return(z*(L1+z*(L2+z*(L3+z*(L4+z*(L5+z*(L6+z*(L7+z*L8))))))));
100 #else	/* defined(vax)||defined(tahoe) */
101     return(z*(L1+z*(L2+z*(L3+z*(L4+z*(L5+z*(L6+z*L7)))))));
102 #endif	/* defined(vax)||defined(tahoe) */
103 }
104