1*a898920cSchristos /* $NetBSD: b_exp.c,v 1.1 2012/05/05 17:54:14 christos Exp $ */
2*a898920cSchristos
3*a898920cSchristos /*
4*a898920cSchristos * Copyright (c) 1985, 1993
5*a898920cSchristos * The Regents of the University of California. All rights reserved.
6*a898920cSchristos *
7*a898920cSchristos * Redistribution and use in source and binary forms, with or without
8*a898920cSchristos * modification, are permitted provided that the following conditions
9*a898920cSchristos * are met:
10*a898920cSchristos * 1. Redistributions of source code must retain the above copyright
11*a898920cSchristos * notice, this list of conditions and the following disclaimer.
12*a898920cSchristos * 2. Redistributions in binary form must reproduce the above copyright
13*a898920cSchristos * notice, this list of conditions and the following disclaimer in the
14*a898920cSchristos * documentation and/or other materials provided with the distribution.
15*a898920cSchristos * 3. All advertising materials mentioning features or use of this software
16*a898920cSchristos * must display the following acknowledgement:
17*a898920cSchristos * This product includes software developed by the University of
18*a898920cSchristos * California, Berkeley and its contributors.
19*a898920cSchristos * 4. Neither the name of the University nor the names of its contributors
20*a898920cSchristos * may be used to endorse or promote products derived from this software
21*a898920cSchristos * without specific prior written permission.
22*a898920cSchristos *
23*a898920cSchristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*a898920cSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*a898920cSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*a898920cSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*a898920cSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*a898920cSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*a898920cSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*a898920cSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*a898920cSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*a898920cSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*a898920cSchristos * SUCH DAMAGE.
34*a898920cSchristos */
35*a898920cSchristos
36*a898920cSchristos /* @(#)exp.c 8.1 (Berkeley) 6/4/93 */
37*a898920cSchristos #include <sys/cdefs.h>
38*a898920cSchristos #if 0
39*a898920cSchristos __FBSDID("$FreeBSD: release/9.0.0/lib/msun/bsdsrc/b_exp.c 176449 2008-02-22 02:26:51Z das $");
40*a898920cSchristos #else
41*a898920cSchristos __RCSID("$NetBSD: b_exp.c,v 1.1 2012/05/05 17:54:14 christos Exp $");
42*a898920cSchristos #endif
43*a898920cSchristos
44*a898920cSchristos
45*a898920cSchristos /* EXP(X)
46*a898920cSchristos * RETURN THE EXPONENTIAL OF X
47*a898920cSchristos * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
48*a898920cSchristos * CODED IN C BY K.C. NG, 1/19/85;
49*a898920cSchristos * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85, 6/14/86.
50*a898920cSchristos *
51*a898920cSchristos * Required system supported functions:
52*a898920cSchristos * scalb(x,n)
53*a898920cSchristos * copysign(x,y)
54*a898920cSchristos * finite(x)
55*a898920cSchristos *
56*a898920cSchristos * Method:
57*a898920cSchristos * 1. Argument Reduction: given the input x, find r and integer k such
58*a898920cSchristos * that
59*a898920cSchristos * x = k*ln2 + r, |r| <= 0.5*ln2 .
60*a898920cSchristos * r will be represented as r := z+c for better accuracy.
61*a898920cSchristos *
62*a898920cSchristos * 2. Compute exp(r) by
63*a898920cSchristos *
64*a898920cSchristos * exp(r) = 1 + r + r*R1/(2-R1),
65*a898920cSchristos * where
66*a898920cSchristos * R1 = x - x^2*(p1+x^2*(p2+x^2*(p3+x^2*(p4+p5*x^2)))).
67*a898920cSchristos *
68*a898920cSchristos * 3. exp(x) = 2^k * exp(r) .
69*a898920cSchristos *
70*a898920cSchristos * Special cases:
71*a898920cSchristos * exp(INF) is INF, exp(NaN) is NaN;
72*a898920cSchristos * exp(-INF)= 0;
73*a898920cSchristos * for finite argument, only exp(0)=1 is exact.
74*a898920cSchristos *
75*a898920cSchristos * Accuracy:
76*a898920cSchristos * exp(x) returns the exponential of x nearly rounded. In a test run
77*a898920cSchristos * with 1,156,000 random arguments on a VAX, the maximum observed
78*a898920cSchristos * error was 0.869 ulps (units in the last place).
79*a898920cSchristos */
80*a898920cSchristos
81*a898920cSchristos #include "math.h"
82*a898920cSchristos #include "math_private.h"
83*a898920cSchristos
84*a898920cSchristos static const double p1 = 0x1.555555555553ep-3;
85*a898920cSchristos static const double p2 = -0x1.6c16c16bebd93p-9;
86*a898920cSchristos static const double p3 = 0x1.1566aaf25de2cp-14;
87*a898920cSchristos static const double p4 = -0x1.bbd41c5d26bf1p-20;
88*a898920cSchristos static const double p5 = 0x1.6376972bea4d0p-25;
89*a898920cSchristos static const double ln2hi = 0x1.62e42fee00000p-1;
90*a898920cSchristos static const double ln2lo = 0x1.a39ef35793c76p-33;
91*a898920cSchristos static const double lnhuge = 0x1.6602b15b7ecf2p9;
92*a898920cSchristos static const double lntiny = -0x1.77af8ebeae354p9;
93*a898920cSchristos static const double invln2 = 0x1.71547652b82fep0;
94*a898920cSchristos
95*a898920cSchristos /* returns exp(r = x + c) for |c| < |x| with no overlap. */
96*a898920cSchristos
97*a898920cSchristos double
__exp__D(double x,double c)98*a898920cSchristos __exp__D(double x, double c)
99*a898920cSchristos {
100*a898920cSchristos double z,hi,lo;
101*a898920cSchristos int k;
102*a898920cSchristos
103*a898920cSchristos if (x != x) /* x is NaN */
104*a898920cSchristos return(x);
105*a898920cSchristos if ( x <= lnhuge ) {
106*a898920cSchristos if ( x >= lntiny ) {
107*a898920cSchristos
108*a898920cSchristos /* argument reduction : x --> x - k*ln2 */
109*a898920cSchristos z = invln2*x;
110*a898920cSchristos k = z + copysign(.5, x);
111*a898920cSchristos
112*a898920cSchristos /* express (x+c)-k*ln2 as hi-lo and let x=hi-lo rounded */
113*a898920cSchristos
114*a898920cSchristos hi=(x-k*ln2hi); /* Exact. */
115*a898920cSchristos x= hi - (lo = k*ln2lo-c);
116*a898920cSchristos /* return 2^k*[1+x+x*c/(2+c)] */
117*a898920cSchristos z=x*x;
118*a898920cSchristos c= x - z*(p1+z*(p2+z*(p3+z*(p4+z*p5))));
119*a898920cSchristos c = (x*c)/(2.0-c);
120*a898920cSchristos
121*a898920cSchristos return scalb(1.+(hi-(lo - c)), k);
122*a898920cSchristos }
123*a898920cSchristos /* end of x > lntiny */
124*a898920cSchristos
125*a898920cSchristos else
126*a898920cSchristos /* exp(-big#) underflows to zero */
127*a898920cSchristos if(finite(x)) return(scalb(1.0,-5000));
128*a898920cSchristos
129*a898920cSchristos /* exp(-INF) is zero */
130*a898920cSchristos else return(0.0);
131*a898920cSchristos }
132*a898920cSchristos /* end of x < lnhuge */
133*a898920cSchristos
134*a898920cSchristos else
135*a898920cSchristos /* exp(INF) is INF, exp(+big#) overflows to INF */
136*a898920cSchristos return( finite(x) ? scalb(1.0,5000) : x);
137*a898920cSchristos }
138