134126Sbostic /*
224604Szliu  * Copyright (c) 1985 Regents of the University of California.
334126Sbostic  * All rights reserved.
434126Sbostic  *
534126Sbostic  * Redistribution and use in source and binary forms are permitted
6*34931Sbostic  * provided that the above copyright notice and this paragraph are
7*34931Sbostic  * duplicated in all such forms and that any documentation,
8*34931Sbostic  * advertising materials, and other materials related to such
9*34931Sbostic  * distribution and use acknowledge that the software was developed
10*34931Sbostic  * by the University of California, Berkeley.  The name of the
11*34931Sbostic  * University may not be used to endorse or promote products derived
12*34931Sbostic  * from this software without specific prior written permission.
13*34931Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34931Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34931Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634126Sbostic  *
1734126Sbostic  * All recipients should regard themselves as participants in an ongoing
1834126Sbostic  * research project and hence should feel obligated to report their
1934126Sbostic  * experiences (good or bad) with these elementary function codes, using
2034126Sbostic  * the sendbug(8) program, to the authors.
2124604Szliu  */
2224604Szliu 
2324604Szliu #ifndef lint
24*34931Sbostic static char sccsid[] = "@(#)log__L.c	5.3 (Berkeley) 06/30/88";
2534126Sbostic #endif /* not lint */
2624604Szliu 
2724604Szliu /* log__L(Z)
2824604Szliu  *		LOG(1+X) - 2S			       X
2924604Szliu  * RETURN      ---------------  WHERE Z = S*S,  S = ------- , 0 <= Z <= .0294...
3024604Szliu  *		      S				     2 + X
3124604Szliu  *
3224604Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 bits or IEEE DOUBLE 53 BITS)
3324604Szliu  * KERNEL FUNCTION FOR LOG; TO BE USED IN LOG1P, LOG, AND POW FUNCTIONS
3424604Szliu  * CODED IN C BY K.C. NG, 1/19/85;
3524604Szliu  * REVISED BY K.C. Ng, 2/3/85, 4/16/85.
3624604Szliu  *
3724604Szliu  * Method :
3824604Szliu  *	1. Polynomial approximation: let s = x/(2+x).
3924604Szliu  *	   Based on log(1+x) = log(1+s) - log(1-s)
4024604Szliu  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
4124604Szliu  *
4224604Szliu  *	   (log(1+x) - 2s)/s is computed by
4324604Szliu  *
4424604Szliu  *	       z*(L1 + z*(L2 + z*(... (L7 + z*L8)...)))
4524604Szliu  *
4624604Szliu  *	   where z=s*s. (See the listing below for Lk's values.) The
4724604Szliu  *	   coefficients are obtained by a special Remez algorithm.
4824604Szliu  *
4924604Szliu  * Accuracy:
5024604Szliu  *	Assuming no rounding error, the maximum magnitude of the approximation
5124604Szliu  *	error (absolute) is 2**(-58.49) for IEEE double, and 2**(-63.63)
5224604Szliu  *	for VAX D format.
5324604Szliu  *
5424604Szliu  * Constants:
5524604Szliu  * The hexadecimal values are the intended ones for the following constants.
5624604Szliu  * The decimal values may be used, provided that the compiler will convert
5724604Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
5824604Szliu  * shown.
5924604Szliu  */
6024604Szliu 
6131853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format (56 bits) */
6231853Szliu #ifdef vax
6331812Szliu #define _0x(A,B)	0x/**/A/**/B
6431853Szliu #else	/* vax */
6531812Szliu #define _0x(A,B)	0x/**/B/**/A
6631853Szliu #endif	/* vax */
6724604Szliu /* static double */
6824604Szliu /* L1     =  6.6666666666666703212E-1    , Hex  2^  0   *  .AAAAAAAAAAAAC5 */
6924604Szliu /* L2     =  3.9999999999970461961E-1    , Hex  2^ -1   *  .CCCCCCCCCC2684 */
7024604Szliu /* L3     =  2.8571428579395698188E-1    , Hex  2^ -1   *  .92492492F85782 */
7124604Szliu /* L4     =  2.2222221233634724402E-1    , Hex  2^ -2   *  .E38E3839B7AF2C */
7224604Szliu /* L5     =  1.8181879517064680057E-1    , Hex  2^ -2   *  .BA2EB4CC39655E */
7324604Szliu /* L6     =  1.5382888777946145467E-1    , Hex  2^ -2   *  .9D8551E8C5781D */
7424604Szliu /* L7     =  1.3338356561139403517E-1    , Hex  2^ -2   *  .8895B3907FCD92 */
7524604Szliu /* L8     =  1.2500000000000000000E-1    , Hex  2^ -2   *  .80000000000000 */
7631812Szliu static long        L1x[] = { _0x(aaaa,402a), _0x(aac5,aaaa)};
7731812Szliu static long        L2x[] = { _0x(cccc,3fcc), _0x(2684,cccc)};
7831812Szliu static long        L3x[] = { _0x(4924,3f92), _0x(5782,92f8)};
7931812Szliu static long        L4x[] = { _0x(8e38,3f63), _0x(af2c,39b7)};
8031812Szliu static long        L5x[] = { _0x(2eb4,3f3a), _0x(655e,cc39)};
8131812Szliu static long        L6x[] = { _0x(8551,3f1d), _0x(781d,e8c5)};
8231812Szliu static long        L7x[] = { _0x(95b3,3f08), _0x(cd92,907f)};
8331812Szliu static long        L8x[] = { _0x(0000,3f00), _0x(0000,0000)};
8424604Szliu #define       L1    (*(double*)L1x)
8524604Szliu #define       L2    (*(double*)L2x)
8624604Szliu #define       L3    (*(double*)L3x)
8724604Szliu #define       L4    (*(double*)L4x)
8824604Szliu #define       L5    (*(double*)L5x)
8924604Szliu #define       L6    (*(double*)L6x)
9024604Szliu #define       L7    (*(double*)L7x)
9124604Szliu #define       L8    (*(double*)L8x)
9231853Szliu #else	/* defined(vax)||defined(tahoe)	*/
9324604Szliu static double
9424604Szliu L1     =  6.6666666666667340202E-1    , /*Hex  2^ -1   *  1.5555555555592 */
9524604Szliu L2     =  3.9999999999416702146E-1    , /*Hex  2^ -2   *  1.999999997FF24 */
9624604Szliu L3     =  2.8571428742008753154E-1    , /*Hex  2^ -2   *  1.24924941E07B4 */
9724604Szliu L4     =  2.2222198607186277597E-1    , /*Hex  2^ -3   *  1.C71C52150BEA6 */
9824604Szliu L5     =  1.8183562745289935658E-1    , /*Hex  2^ -3   *  1.74663CC94342F */
9924604Szliu L6     =  1.5314087275331442206E-1    , /*Hex  2^ -3   *  1.39A1EC014045B */
10024604Szliu L7     =  1.4795612545334174692E-1    ; /*Hex  2^ -3   *  1.2F039F0085122 */
10131853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
10224604Szliu 
10324604Szliu double log__L(z)
10424604Szliu double z;
10524604Szliu {
10631853Szliu #if defined(vax)||defined(tahoe)
10724604Szliu     return(z*(L1+z*(L2+z*(L3+z*(L4+z*(L5+z*(L6+z*(L7+z*L8))))))));
10831853Szliu #else	/* defined(vax)||defined(tahoe) */
10924604Szliu     return(z*(L1+z*(L2+z*(L3+z*(L4+z*(L5+z*(L6+z*L7)))))));
11031853Szliu #endif	/* defined(vax)||defined(tahoe) */
11124604Szliu }
112