]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Math/e_exp.c
Fix a bug about the iSCSI DHCP dependency issue.
[mirror_edk2.git] / StdLib / LibC / Math / e_exp.c
CommitLineData
2aa62f2b 1/* @(#)e_exp.c 5.1 93/09/24 */\r
2/*\r
3 * ====================================================\r
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\r
5 *\r
6 * Developed at SunPro, a Sun Microsystems, Inc. business.\r
7 * Permission to use, copy, modify, and distribute this\r
8 * software is freely granted, provided that this notice\r
9 * is preserved.\r
10 * ====================================================\r
11 */\r
12#include <LibConfig.h>\r
13#include <sys/EfiCdefs.h>\r
14#if defined(LIBM_SCCS) && !defined(lint)\r
15__RCSID("$NetBSD: e_exp.c,v 1.11 2002/05/26 22:01:49 wiz Exp $");\r
16#endif\r
17\r
18#if defined(_MSC_VER) /* Handle Microsoft VC++ compiler specifics. */\r
19 // C4756: overflow in constant arithmetic\r
20 #pragma warning ( disable : 4756 )\r
21#endif\r
22\r
23/* __ieee754_exp(x)\r
24 * Returns the exponential of x.\r
25 *\r
26 * Method\r
27 * 1. Argument reduction:\r
28 * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.\r
29 * Given x, find r and integer k such that\r
30 *\r
31 * x = k*ln2 + r, |r| <= 0.5*ln2.\r
32 *\r
33 * Here r will be represented as r = hi-lo for better\r
34 * accuracy.\r
35 *\r
36 * 2. Approximation of exp(r) by a special rational function on\r
37 * the interval [0,0.34658]:\r
38 * Write\r
39 * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...\r
40 * We use a special Reme algorithm on [0,0.34658] to generate\r
41 * a polynomial of degree 5 to approximate R. The maximum error\r
42 * of this polynomial approximation is bounded by 2**-59. In\r
43 * other words,\r
44 * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5\r
45 * (where z=r*r, and the values of P1 to P5 are listed below)\r
46 * and\r
47 * | 5 | -59\r
48 * | 2.0+P1*z+...+P5*z - R(z) | <= 2\r
49 * | |\r
50 * The computation of exp(r) thus becomes\r
51 * 2*r\r
52 * exp(r) = 1 + -------\r
53 * R - r\r
54 * r*R1(r)\r
55 * = 1 + r + ----------- (for better accuracy)\r
56 * 2 - R1(r)\r
57 * where\r
58 * 2 4 10\r
59 * R1(r) = r - (P1*r + P2*r + ... + P5*r ).\r
60 *\r
61 * 3. Scale back to obtain exp(x):\r
62 * From step 1, we have\r
63 * exp(x) = 2^k * exp(r)\r
64 *\r
65 * Special cases:\r
66 * exp(INF) is INF, exp(NaN) is NaN;\r
67 * exp(-INF) is 0, and\r
68 * for finite argument, only exp(0)=1 is exact.\r
69 *\r
70 * Accuracy:\r
71 * according to an error analysis, the error is always less than\r
72 * 1 ulp (unit in the last place).\r
73 *\r
74 * Misc. info.\r
75 * For IEEE double\r
76 * if x > 7.09782712893383973096e+02 then exp(x) overflow\r
77 * if x < -7.45133219101941108420e+02 then exp(x) underflow\r
78 *\r
79 * Constants:\r
80 * The hexadecimal values are the intended ones for the following\r
81 * constants. The decimal values may be used, provided that the\r
82 * compiler will convert from decimal to binary accurately enough\r
83 * to produce the hexadecimal values shown.\r
84 */\r
85\r
86#include "math.h"\r
87#include "math_private.h"\r
88\r
89static const double\r
90one = 1.0,\r
91halF[2] = {0.5,-0.5,},\r
92huge = 1.0e+300,\r
93twom1000= 9.33263618503218878990e-302, /* 2**-1000=0x01700000,0*/\r
94o_threshold= 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */\r
95u_threshold= -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */\r
96ln2HI[2] ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */\r
97 -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */\r
98ln2LO[2] ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */\r
99 -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */\r
100invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */\r
101P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */\r
102P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */\r
103P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */\r
104P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */\r
105P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */\r
106\r
107\r
108double\r
109__ieee754_exp(double x) /* default IEEE double exp */\r
110{\r
111 double y,hi,lo,c,t;\r
112 int32_t k,xsb;\r
113 u_int32_t hx;\r
114\r
115 hi = lo = 0;\r
116 k = 0;\r
117 GET_HIGH_WORD(hx,x);\r
118 xsb = (hx>>31)&1; /* sign bit of x */\r
119 hx &= 0x7fffffff; /* high word of |x| */\r
120\r
121 /* filter out non-finite argument */\r
122 if(hx >= 0x40862E42) { /* if |x|>=709.78... */\r
123 if(hx>=0x7ff00000) {\r
124 u_int32_t lx;\r
125 GET_LOW_WORD(lx,x);\r
126 if(((hx&0xfffff)|lx)!=0)\r
127 return x+x; /* NaN */\r
128 else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */\r
129 }\r
130 if(x > o_threshold) return huge*huge; /* overflow */\r
131 if(x < u_threshold) return twom1000*twom1000; /* underflow */\r
132 }\r
133\r
134 /* argument reduction */\r
135 if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */\r
136 if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */\r
137 hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;\r
138 } else {\r
139 k = (int32_t)(invln2*x+halF[xsb]);\r
140 t = k;\r
141 hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */\r
142 lo = t*ln2LO[0];\r
143 }\r
144 x = hi - lo;\r
145 }\r
146 else if(hx < 0x3e300000) { /* when |x|<2**-28 */\r
147 if(huge+x>one) return one+x;/* trigger inexact */\r
148 }\r
149 else k = 0;\r
150\r
151 /* x is now in primary range */\r
152 t = x*x;\r
153 c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));\r
154 if(k==0) return one-((x*c)/(c-2.0)-x);\r
155 else y = one-((lo-(x*c)/(2.0-c))-hi);\r
156 if(k >= -1021) {\r
157 u_int32_t hy;\r
158 GET_HIGH_WORD(hy,y);\r
159 SET_HIGH_WORD(y,hy+(k<<20)); /* add k to y's exponent */\r
160 return y;\r
161 } else {\r
162 u_int32_t hy;\r
163 GET_HIGH_WORD(hy,y);\r
164 SET_HIGH_WORD(y,hy+((k+1000)<<20)); /* add k to y's exponent */\r
165 return y*twom1000;\r
166 }\r
167}\r