xref: /minix3/lib/libm/noieee_src/n_exp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*      $NetBSD: n_exp.c,v 1.9 2014/10/10 20:58:09 martin Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras  * Copyright (c) 1985, 1993
42fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras  *
62fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
82fe8fb19SBen Gras  * are met:
92fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras  *    without specific prior written permission.
172fe8fb19SBen Gras  *
182fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras  * SUCH DAMAGE.
292fe8fb19SBen Gras  */
302fe8fb19SBen Gras 
312fe8fb19SBen Gras #ifndef lint
322fe8fb19SBen Gras #if 0
332fe8fb19SBen Gras static char sccsid[] = "@(#)exp.c	8.1 (Berkeley) 6/4/93";
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras #endif /* not lint */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras /* EXP(X)
382fe8fb19SBen Gras  * RETURN THE EXPONENTIAL OF X
392fe8fb19SBen Gras  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
402fe8fb19SBen Gras  * CODED IN C BY K.C. NG, 1/19/85;
412fe8fb19SBen Gras  * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85, 6/14/86.
422fe8fb19SBen Gras  *
432fe8fb19SBen Gras  * Required system supported functions:
442fe8fb19SBen Gras  *	scalb(x,n)
452fe8fb19SBen Gras  *	copysign(x,y)
462fe8fb19SBen Gras  *	finite(x)
472fe8fb19SBen Gras  *
482fe8fb19SBen Gras  * Method:
492fe8fb19SBen Gras  *	1. Argument Reduction: given the input x, find r and integer k such
502fe8fb19SBen Gras  *	   that
512fe8fb19SBen Gras  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
522fe8fb19SBen Gras  *	   r will be represented as r := z+c for better accuracy.
532fe8fb19SBen Gras  *
542fe8fb19SBen Gras  *	2. Compute exp(r) by
552fe8fb19SBen Gras  *
562fe8fb19SBen Gras  *		exp(r) = 1 + r + r*R1/(2-R1),
572fe8fb19SBen Gras  *	   where
582fe8fb19SBen Gras  *		R1 = x - x^2*(p1+x^2*(p2+x^2*(p3+x^2*(p4+p5*x^2)))).
592fe8fb19SBen Gras  *
602fe8fb19SBen Gras  *	3. exp(x) = 2^k * exp(r) .
612fe8fb19SBen Gras  *
622fe8fb19SBen Gras  * Special cases:
632fe8fb19SBen Gras  *	exp(INF) is INF, exp(NaN) is NaN;
642fe8fb19SBen Gras  *	exp(-INF)=  0;
652fe8fb19SBen Gras  *	for finite argument, only exp(0)=1 is exact.
662fe8fb19SBen Gras  *
672fe8fb19SBen Gras  * Accuracy:
682fe8fb19SBen Gras  *	exp(x) returns the exponential of x nearly rounded. In a test run
692fe8fb19SBen Gras  *	with 1,156,000 random arguments on a VAX, the maximum observed
702fe8fb19SBen Gras  *	error was 0.869 ulps (units in the last place).
712fe8fb19SBen Gras  *
722fe8fb19SBen Gras  * Constants:
732fe8fb19SBen Gras  * The hexadecimal values are the intended ones for the following constants.
742fe8fb19SBen Gras  * The decimal values may be used, provided that the compiler will convert
752fe8fb19SBen Gras  * from decimal to binary accurately enough to produce the hexadecimal values
762fe8fb19SBen Gras  * shown.
772fe8fb19SBen Gras  */
782fe8fb19SBen Gras 
792fe8fb19SBen Gras #define _LIBM_STATIC
802fe8fb19SBen Gras #include "../src/namespace.h"
812fe8fb19SBen Gras #include "mathimpl.h"
822fe8fb19SBen Gras 
832fe8fb19SBen Gras #ifdef __weak_alias
842fe8fb19SBen Gras __weak_alias(exp, _exp);
85*0a6a1f1dSLionel Sambuc __weak_alias(_expl, _exp);
862fe8fb19SBen Gras __weak_alias(expf, _expf);
872fe8fb19SBen Gras #endif
882fe8fb19SBen Gras 
892fe8fb19SBen Gras vc(ln2hi,  6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
902fe8fb19SBen Gras vc(ln2lo,  1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
912fe8fb19SBen Gras vc(lnhuge, 9.4961163736712506989E1   ,ec1d,43bd,9010,a73e,   7, .BDEC1DA73E9010)
922fe8fb19SBen Gras vc(lntiny,-9.5654310917272452386E1   ,4f01,c3bf,33af,d72e,   7,-.BF4F01D72E33AF)
932fe8fb19SBen Gras vc(invln2, 1.4426950408889634148E0   ,aa3b,40b8,17f1,295c,   1, .B8AA3B295C17F1)
942fe8fb19SBen Gras vc(p1,     1.6666666666666602251E-1  ,aaaa,3f2a,a9f1,aaaa,  -2, .AAAAAAAAAAA9F1)
952fe8fb19SBen Gras vc(p2,    -2.7777777777015591216E-3  ,0b60,bc36,ec94,b5f5,  -8,-.B60B60B5F5EC94)
962fe8fb19SBen Gras vc(p3,     6.6137563214379341918E-5  ,b355,398a,f15f,792e, -13, .8AB355792EF15F)
972fe8fb19SBen Gras vc(p4,    -1.6533902205465250480E-6  ,ea0e,b6dd,5f84,2e93, -19,-.DDEA0E2E935F84)
982fe8fb19SBen Gras vc(p5,     4.1381367970572387085E-8  ,bb4b,3431,2683,95f5, -24, .B1BB4B95F52683)
992fe8fb19SBen Gras 
1002fe8fb19SBen Gras #ifdef vccast
1012fe8fb19SBen Gras #define    ln2hi    vccast(ln2hi)
1022fe8fb19SBen Gras #define    ln2lo    vccast(ln2lo)
1032fe8fb19SBen Gras #define   lnhuge    vccast(lnhuge)
1042fe8fb19SBen Gras #define   lntiny    vccast(lntiny)
1052fe8fb19SBen Gras #define   invln2    vccast(invln2)
1062fe8fb19SBen Gras #define       p1    vccast(p1)
1072fe8fb19SBen Gras #define       p2    vccast(p2)
1082fe8fb19SBen Gras #define       p3    vccast(p3)
1092fe8fb19SBen Gras #define       p4    vccast(p4)
1102fe8fb19SBen Gras #define       p5    vccast(p5)
1112fe8fb19SBen Gras #endif
1122fe8fb19SBen Gras 
1132fe8fb19SBen Gras ic(p1,     1.6666666666666601904E-1,  -3,  1.555555555553E)
1142fe8fb19SBen Gras ic(p2,    -2.7777777777015593384E-3,  -9, -1.6C16C16BEBD93)
1152fe8fb19SBen Gras ic(p3,     6.6137563214379343612E-5, -14,  1.1566AAF25DE2C)
1162fe8fb19SBen Gras ic(p4,    -1.6533902205465251539E-6, -20, -1.BBD41C5D26BF1)
1172fe8fb19SBen Gras ic(p5,     4.1381367970572384604E-8, -25,  1.6376972BEA4D0)
1182fe8fb19SBen Gras ic(ln2hi,  6.9314718036912381649E-1,  -1,  1.62E42FEE00000)
1192fe8fb19SBen Gras ic(ln2lo,  1.9082149292705877000E-10,-33,  1.A39EF35793C76)
1202fe8fb19SBen Gras ic(lnhuge, 7.1602103751842355450E2,    9,  1.6602B15B7ECF2)
1212fe8fb19SBen Gras ic(lntiny,-7.5137154372698068983E2,    9, -1.77AF8EBEAE354)
1222fe8fb19SBen Gras ic(invln2, 1.4426950408889633870E0,    0,  1.71547652B82FE)
1232fe8fb19SBen Gras 
1242fe8fb19SBen Gras double
exp(double x)1252fe8fb19SBen Gras exp(double x)
1262fe8fb19SBen Gras {
1272fe8fb19SBen Gras 	double  z,hi,lo,c;
1282fe8fb19SBen Gras 	int k;
1292fe8fb19SBen Gras 
1302fe8fb19SBen Gras #if !defined(__vax__)&&!defined(tahoe)
1312fe8fb19SBen Gras 	if(x!=x) return(x);	/* x is NaN */
1322fe8fb19SBen Gras #endif	/* !defined(__vax__)&&!defined(tahoe) */
1332fe8fb19SBen Gras 	if( x <= lnhuge ) {
1342fe8fb19SBen Gras 		if( x >= lntiny ) {
1352fe8fb19SBen Gras 
1362fe8fb19SBen Gras 		    /* argument reduction : x --> x - k*ln2 */
1372fe8fb19SBen Gras 
1382fe8fb19SBen Gras 			k=invln2*x+copysign(0.5,x);	/* k=NINT(x/ln2) */
1392fe8fb19SBen Gras 
1402fe8fb19SBen Gras 		    /* express x-k*ln2 as hi-lo and let x=hi-lo rounded */
1412fe8fb19SBen Gras 
1422fe8fb19SBen Gras 			hi=x-k*ln2hi;
1432fe8fb19SBen Gras 			x=hi-(lo=k*ln2lo);
1442fe8fb19SBen Gras 
1452fe8fb19SBen Gras 		    /* return 2^k*[1+x+x*c/(2+c)]  */
1462fe8fb19SBen Gras 			z=x*x;
1472fe8fb19SBen Gras 			c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5))));
1482fe8fb19SBen Gras 			return  scalb(1.0+(hi-(lo-(x*c)/(2.0-c))),k);
1492fe8fb19SBen Gras 
1502fe8fb19SBen Gras 		}
1512fe8fb19SBen Gras 		/* end of x > lntiny */
1522fe8fb19SBen Gras 
1532fe8fb19SBen Gras 		else
1542fe8fb19SBen Gras 		     /* exp(-big#) underflows to zero */
1552fe8fb19SBen Gras 		     if(finite(x))  return(scalb(1.0,-5000));
1562fe8fb19SBen Gras 
1572fe8fb19SBen Gras 		     /* exp(-INF) is zero */
1582fe8fb19SBen Gras 		     else return(0.0);
1592fe8fb19SBen Gras 	}
1602fe8fb19SBen Gras 	/* end of x < lnhuge */
1612fe8fb19SBen Gras 
1622fe8fb19SBen Gras 	else
1632fe8fb19SBen Gras 	/* exp(INF) is INF, exp(+big#) overflows to INF */
1642fe8fb19SBen Gras 	    return( finite(x) ?  scalb(1.0,5000)  : x);
1652fe8fb19SBen Gras }
1662fe8fb19SBen Gras 
1672fe8fb19SBen Gras float
expf(float x)1682fe8fb19SBen Gras expf(float x)
1692fe8fb19SBen Gras {
1702fe8fb19SBen Gras 	return(exp((double)x));
1712fe8fb19SBen Gras }
1722fe8fb19SBen Gras 
1732fe8fb19SBen Gras /* returns exp(r = x + c) for |c| < |x| with no overlap.  */
1742fe8fb19SBen Gras 
1752fe8fb19SBen Gras double
__exp__D(double x,double c)1762fe8fb19SBen Gras __exp__D(double x, double c)
1772fe8fb19SBen Gras {
1782fe8fb19SBen Gras 	double  z,hi,lo;
1792fe8fb19SBen Gras 	int k;
1802fe8fb19SBen Gras 
1812fe8fb19SBen Gras #if !defined(__vax__)&&!defined(tahoe)
1822fe8fb19SBen Gras 	if (x!=x) return(x);	/* x is NaN */
1832fe8fb19SBen Gras #endif	/* !defined(__vax__)&&!defined(tahoe) */
1842fe8fb19SBen Gras 	if ( x <= lnhuge ) {
1852fe8fb19SBen Gras 		if ( x >= lntiny ) {
1862fe8fb19SBen Gras 
1872fe8fb19SBen Gras 		    /* argument reduction : x --> x - k*ln2 */
1882fe8fb19SBen Gras 			z = invln2*x;
1892fe8fb19SBen Gras 			k = z + copysign(.5, x);
1902fe8fb19SBen Gras 
1912fe8fb19SBen Gras 		    /* express (x+c)-k*ln2 as hi-lo and let x=hi-lo rounded */
1922fe8fb19SBen Gras 
1932fe8fb19SBen Gras 			hi=(x-k*ln2hi);			/* Exact. */
1942fe8fb19SBen Gras 			x= hi - (lo = k*ln2lo-c);
1952fe8fb19SBen Gras 		    /* return 2^k*[1+x+x*c/(2+c)]  */
1962fe8fb19SBen Gras 			z=x*x;
1972fe8fb19SBen Gras 			c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5))));
1982fe8fb19SBen Gras 			c = (x*c)/(2.0-c);
1992fe8fb19SBen Gras 
2002fe8fb19SBen Gras 			return  scalb(1.+(hi-(lo - c)), k);
2012fe8fb19SBen Gras 		}
2022fe8fb19SBen Gras 		/* end of x > lntiny */
2032fe8fb19SBen Gras 
2042fe8fb19SBen Gras 		else
2052fe8fb19SBen Gras 		     /* exp(-big#) underflows to zero */
2062fe8fb19SBen Gras 		     if(finite(x))  return(scalb(1.0,-5000));
2072fe8fb19SBen Gras 
2082fe8fb19SBen Gras 		     /* exp(-INF) is zero */
2092fe8fb19SBen Gras 		     else return(0.0);
2102fe8fb19SBen Gras 	}
2112fe8fb19SBen Gras 	/* end of x < lnhuge */
2122fe8fb19SBen Gras 
2132fe8fb19SBen Gras 	else
2142fe8fb19SBen Gras 	/* exp(INF) is INF, exp(+big#) overflows to INF */
2152fe8fb19SBen Gras 	    return( finite(x) ?  scalb(1.0,5000)  : x);
2162fe8fb19SBen Gras }
217