#include "SDL_internal.h"1/*2* ====================================================3* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.4*5* Developed at SunPro, a Sun Microsystems, Inc. business.6* Permission to use, copy, modify, and distribute this7* software is freely granted, provided that this notice8* is preserved.9* ====================================================10*/1112/* __ieee754_exp(x)13* Returns the exponential of x.14*15* Method16* 1. Argument reduction:17* Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.18* Given x, find r and integer k such that19*20* x = k*ln2 + r, |r| <= 0.5*ln2.21*22* Here r will be represented as r = hi-lo for better23* accuracy.24*25* 2. Approximation of exp(r) by a special rational function on26* the interval [0,0.34658]:27* Write28* R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...29* We use a special Reme algorithm on [0,0.34658] to generate30* a polynomial of degree 5 to approximate R. The maximum error31* of this polynomial approximation is bounded by 2**-59. In32* other words,33* R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**534* (where z=r*r, and the values of P1 to P5 are listed below)35* and36* | 5 | -5937* | 2.0+P1*z+...+P5*z - R(z) | <= 238* | |39* The computation of exp(r) thus becomes40* 2*r41* exp(r) = 1 + -------42* R - r43* r*R1(r)44* = 1 + r + ----------- (for better accuracy)45* 2 - R1(r)46* where47* 2 4 1048* R1(r) = r - (P1*r + P2*r + ... + P5*r ).49*50* 3. Scale back to obtain exp(x):51* From step 1, we have52* exp(x) = 2^k * exp(r)53*54* Special cases:55* exp(INF) is INF, exp(NaN) is NaN;56* exp(-INF) is 0, and57* for finite argument, only exp(0)=1 is exact.58*59* Accuracy:60* according to an error analysis, the error is always less than61* 1 ulp (unit in the last place).62*63* Misc. info.64* For IEEE double65* if x > 7.09782712893383973096e+02 then exp(x) overflow66* if x < -7.45133219101941108420e+02 then exp(x) underflow67*68* Constants:69* The hexadecimal values are the intended ones for the following70* constants. The decimal values may be used, provided that the71* compiler will convert from decimal to binary accurately enough72* to produce the hexadecimal values shown.73*/7475#include "math_libm.h"76#include "math_private.h"7778#ifdef __WATCOMC__ /* Watcom defines huge=__huge */79#undef huge80#endif8182static const double83one = 1.0,84halF[2] = {0.5,-0.5,},85huge = 1.0e+300,86twom1000= 9.33263618503218878990e-302, /* 2**-1000=0x01700000,0*/87o_threshold= 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */88u_threshold= -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */89ln2HI[2] ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */90-6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */91ln2LO[2] ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */92-1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */93invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */94P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */95P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */96P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */97P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */98P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */99100union {101Uint64 u64;102double d;103} inf_union = {104SDL_UINT64_C(0x7ff0000000000000) /* Binary representation of a 64-bit infinite double (sign=0, exponent=2047, mantissa=0) */105};106107double __ieee754_exp(double x) /* default IEEE double exp */108{109double y;110double hi = 0.0;111double lo = 0.0;112double c;113double t;114int32_t k=0;115int32_t xsb;116u_int32_t hx;117118GET_HIGH_WORD(hx,x);119xsb = (hx>>31)&1; /* sign bit of x */120hx &= 0x7fffffff; /* high word of |x| */121122/* filter out non-finite argument */123if(hx >= 0x40862E42) { /* if |x|>=709.78... */124if(hx>=0x7ff00000) {125u_int32_t lx;126GET_LOW_WORD(lx,x);127if(((hx&0xfffff)|lx)!=0)128return x+x; /* NaN */129else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */130}131#if 1132if(x > o_threshold) return inf_union.d; /* overflow */133#elif 1134if(x > o_threshold) return huge*huge; /* overflow */135#else /* !!! FIXME: check this: "huge * huge" is a compiler warning, maybe they wanted +Inf? */136if(x > o_threshold) return INFINITY; /* overflow */137#endif138139if(x < u_threshold) return twom1000*twom1000; /* underflow */140}141142/* argument reduction */143if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */144if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */145hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;146} else {147k = (int32_t) (invln2*x+halF[xsb]);148t = k;149hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */150lo = t*ln2LO[0];151}152x = hi - lo;153}154else if(hx < 0x3e300000) { /* when |x|<2**-28 */155if(huge+x>one) return one+x;/* trigger inexact */156}157else k = 0;158159/* x is now in primary range */160t = x*x;161c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));162if(k==0) return one-((x*c)/(c-2.0)-x);163else y = one-((lo-(x*c)/(2.0-c))-hi);164if(k >= -1021) {165u_int32_t hy;166GET_HIGH_WORD(hy,y);167SET_HIGH_WORD(y,hy+(k<<20)); /* add k to y's exponent */168return y;169} else {170u_int32_t hy;171GET_HIGH_WORD(hy,y);172SET_HIGH_WORD(y,hy+((k+1000)<<20)); /* add k to y's exponent */173return y*twom1000;174}175}176177/*178* wrapper exp(x)179*/180#ifndef _IEEE_LIBM181double exp(double x)182{183static const double o_threshold = 7.09782712893383973096e+02; /* 0x40862E42, 0xFEFA39EF */184static const double u_threshold = -7.45133219101941108420e+02; /* 0xc0874910, 0xD52D3051 */185186double z = __ieee754_exp(x);187if (_LIB_VERSION == _IEEE_)188return z;189if (isfinite(x)) {190if (x > o_threshold)191return __kernel_standard(x, x, 6); /* exp overflow */192if (x < u_threshold)193return __kernel_standard(x, x, 7); /* exp underflow */194}195return z;196}197#else198strong_alias(__ieee754_exp, exp)199#endif200libm_hidden_def(exp)201202203