xref: /openbsd-src/lib/libm/src/b_exp__D.c (revision eab01a26e008dd07c2bc46ce67ddd6fd3ca35f93)
1*eab01a26Sguenther /*	$OpenBSD: b_exp__D.c,v 1.6 2016/09/12 04:39:47 guenther Exp $	*/
25b14c9c6Smartynas /*
35b14c9c6Smartynas  * Copyright (c) 1985, 1993
45b14c9c6Smartynas  *	The Regents of the University of California.  All rights reserved.
55b14c9c6Smartynas  *
65b14c9c6Smartynas  * Redistribution and use in source and binary forms, with or without
75b14c9c6Smartynas  * modification, are permitted provided that the following conditions
85b14c9c6Smartynas  * are met:
95b14c9c6Smartynas  * 1. Redistributions of source code must retain the above copyright
105b14c9c6Smartynas  *    notice, this list of conditions and the following disclaimer.
115b14c9c6Smartynas  * 2. Redistributions in binary form must reproduce the above copyright
125b14c9c6Smartynas  *    notice, this list of conditions and the following disclaimer in the
135b14c9c6Smartynas  *    documentation and/or other materials provided with the distribution.
145b14c9c6Smartynas  * 3. Neither the name of the University nor the names of its contributors
155b14c9c6Smartynas  *    may be used to endorse or promote products derived from this software
165b14c9c6Smartynas  *    without specific prior written permission.
175b14c9c6Smartynas  *
185b14c9c6Smartynas  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
195b14c9c6Smartynas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
205b14c9c6Smartynas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
215b14c9c6Smartynas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
225b14c9c6Smartynas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
235b14c9c6Smartynas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
245b14c9c6Smartynas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
255b14c9c6Smartynas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
265b14c9c6Smartynas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
275b14c9c6Smartynas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
285b14c9c6Smartynas  * SUCH DAMAGE.
295b14c9c6Smartynas  */
305b14c9c6Smartynas 
315b14c9c6Smartynas /* EXP(X)
325b14c9c6Smartynas  * RETURN THE EXPONENTIAL OF X
335b14c9c6Smartynas  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
345b14c9c6Smartynas  * CODED IN C BY K.C. NG, 1/19/85;
355b14c9c6Smartynas  * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85, 6/14/86.
365b14c9c6Smartynas  *
375b14c9c6Smartynas  * Required system supported functions:
385b14c9c6Smartynas  *	scalb(x,n)
395b14c9c6Smartynas  *	copysign(x,y)
40*eab01a26Sguenther  *	isfinite(x)
415b14c9c6Smartynas  *
425b14c9c6Smartynas  * Method:
435b14c9c6Smartynas  *	1. Argument Reduction: given the input x, find r and integer k such
445b14c9c6Smartynas  *	   that
455b14c9c6Smartynas  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
465b14c9c6Smartynas  *	   r will be represented as r := z+c for better accuracy.
475b14c9c6Smartynas  *
485b14c9c6Smartynas  *	2. Compute exp(r) by
495b14c9c6Smartynas  *
505b14c9c6Smartynas  *		exp(r) = 1 + r + r*R1/(2-R1),
515b14c9c6Smartynas  *	   where
525b14c9c6Smartynas  *		R1 = x - x^2*(p1+x^2*(p2+x^2*(p3+x^2*(p4+p5*x^2)))).
535b14c9c6Smartynas  *
545b14c9c6Smartynas  *	3. exp(x) = 2^k * exp(r) .
555b14c9c6Smartynas  *
565b14c9c6Smartynas  * Special cases:
575b14c9c6Smartynas  *	exp(INF) is INF, exp(NaN) is NaN;
585b14c9c6Smartynas  *	exp(-INF)=  0;
595b14c9c6Smartynas  *	for finite argument, only exp(0)=1 is exact.
605b14c9c6Smartynas  *
615b14c9c6Smartynas  * Accuracy:
625b14c9c6Smartynas  *	exp(x) returns the exponential of x nearly rounded. In a test run
635b14c9c6Smartynas  *	with 1,156,000 random arguments on a VAX, the maximum observed
645b14c9c6Smartynas  *	error was 0.869 ulps (units in the last place).
655b14c9c6Smartynas  */
665b14c9c6Smartynas 
675b14c9c6Smartynas #include "math.h"
685b14c9c6Smartynas #include "math_private.h"
695b14c9c6Smartynas 
70fda4b60dSmartynas static const double p1 = 0x1.555555555553ep-3;
71fda4b60dSmartynas static const double p2 = -0x1.6c16c16bebd93p-9;
72fda4b60dSmartynas static const double p3 = 0x1.1566aaf25de2cp-14;
73fda4b60dSmartynas static const double p4 = -0x1.bbd41c5d26bf1p-20;
74fda4b60dSmartynas static const double p5 = 0x1.6376972bea4d0p-25;
75fda4b60dSmartynas static const double ln2hi = 0x1.62e42fee00000p-1;
76fda4b60dSmartynas static const double ln2lo = 0x1.a39ef35793c76p-33;
77fda4b60dSmartynas static const double lnhuge = 0x1.6602b15b7ecf2p9;
78fda4b60dSmartynas static const double lntiny = -0x1.77af8ebeae354p9;
79fda4b60dSmartynas static const double invln2 = 0x1.71547652b82fep0;
805b14c9c6Smartynas 
815b14c9c6Smartynas /* returns exp(r = x + c) for |c| < |x| with no overlap.  */
825b14c9c6Smartynas 
83044349e2Smartynas double
__exp__D(double x,double c)84044349e2Smartynas __exp__D(double x, double c)
855b14c9c6Smartynas {
865b14c9c6Smartynas 	double z, hi, lo;
875b14c9c6Smartynas 	int k;
885b14c9c6Smartynas 
899ba807bfSmartynas 	if (isnan(x))	/* x is NaN */
905b14c9c6Smartynas 		return(x);
915b14c9c6Smartynas 	if ( x <= lnhuge ) {
925b14c9c6Smartynas 		if ( x >= lntiny ) {
935b14c9c6Smartynas 
945b14c9c6Smartynas 		    /* argument reduction : x --> x - k*ln2 */
955b14c9c6Smartynas 			z = invln2*x;
965b14c9c6Smartynas 			k = z + copysign(.5, x);
975b14c9c6Smartynas 
985b14c9c6Smartynas 		    /* express (x+c)-k*ln2 as hi-lo and let x=hi-lo rounded */
995b14c9c6Smartynas 
1005b14c9c6Smartynas 			hi=(x-k*ln2hi);			/* Exact. */
1015b14c9c6Smartynas 			x= hi - (lo = k*ln2lo-c);
1025b14c9c6Smartynas 		    /* return 2^k*[1+x+x*c/(2+c)]  */
1035b14c9c6Smartynas 			z=x*x;
1045b14c9c6Smartynas 			c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5))));
1055b14c9c6Smartynas 			c = (x*c)/(2.0-c);
1065b14c9c6Smartynas 
1075b14c9c6Smartynas 			return  scalb(1.+(hi-(lo - c)), k);
1085b14c9c6Smartynas 		}
1095b14c9c6Smartynas 		/* end of x > lntiny */
1105b14c9c6Smartynas 
1115b14c9c6Smartynas 		else
1125b14c9c6Smartynas 		     /* exp(-big#) underflows to zero */
113*eab01a26Sguenther 		     if(isfinite(x))  return(scalb(1.0,-5000));
1145b14c9c6Smartynas 
1155b14c9c6Smartynas 		     /* exp(-INF) is zero */
1165b14c9c6Smartynas 		     else return(0.0);
1175b14c9c6Smartynas 	}
1185b14c9c6Smartynas 	/* end of x < lnhuge */
1195b14c9c6Smartynas 
1205b14c9c6Smartynas 	else
1215b14c9c6Smartynas 	/* exp(INF) is INF, exp(+big#) overflows to INF */
122*eab01a26Sguenther 	    return( isfinite(x) ?  scalb(1.0,5000)  : x);
1235b14c9c6Smartynas }
124