s_expm1.c revision 256281
1239310Sdim/* @(#)s_expm1.c 5.1 93/09/24 */ 2239310Sdim/* 3239310Sdim * ==================================================== 4239310Sdim * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5239310Sdim * 6239310Sdim * Developed at SunPro, a Sun Microsystems, Inc. business. 7239310Sdim * Permission to use, copy, modify, and distribute this 8239310Sdim * software is freely granted, provided that this notice 9239310Sdim * is preserved. 10239310Sdim * ==================================================== 11239310Sdim */ 12239310Sdim 13239310Sdim#include <sys/cdefs.h> 14239310Sdim__FBSDID("$FreeBSD: stable/10/lib/msun/src/s_expm1.c 251343 2013-06-03 19:51:32Z kargl $"); 15239310Sdim 16239310Sdim/* expm1(x) 17239310Sdim * Returns exp(x)-1, the exponential of x minus 1. 18239310Sdim * 19239310Sdim * Method 20239310Sdim * 1. Argument reduction: 21239310Sdim * Given x, find r and integer k such that 22239310Sdim * 23239310Sdim * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658 24239310Sdim * 25239310Sdim * Here a correction term c will be computed to compensate 26239310Sdim * the error in r when rounded to a floating-point number. 27239310Sdim * 28239310Sdim * 2. Approximating expm1(r) by a special rational function on 29239310Sdim * the interval [0,0.34658]: 30239310Sdim * Since 31239310Sdim * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ... 32239310Sdim * we define R1(r*r) by 33239310Sdim * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r) 34239310Sdim * That is, 35239310Sdim * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r) 36239310Sdim * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r)) 37239310Sdim * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ... 38239310Sdim * We use a special Reme algorithm on [0,0.347] to generate 39239310Sdim * a polynomial of degree 5 in r*r to approximate R1. The 40239310Sdim * maximum error of this polynomial approximation is bounded 41239310Sdim * by 2**-61. In other words, 42239310Sdim * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5 43239310Sdim * where Q1 = -1.6666666666666567384E-2, 44239310Sdim * Q2 = 3.9682539681370365873E-4, 45239310Sdim * Q3 = -9.9206344733435987357E-6, 46239310Sdim * Q4 = 2.5051361420808517002E-7, 47239310Sdim * Q5 = -6.2843505682382617102E-9; 48239310Sdim * z = r*r, 49239310Sdim * with error bounded by 50239310Sdim * | 5 | -61 51239310Sdim * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2 52239310Sdim * | | 53239310Sdim * 54239310Sdim * expm1(r) = exp(r)-1 is then computed by the following 55239310Sdim * specific way which minimize the accumulation rounding error: 56239310Sdim * 2 3 57239310Sdim * r r [ 3 - (R1 + R1*r/2) ] 58239310Sdim * expm1(r) = r + --- + --- * [--------------------] 59239310Sdim * 2 2 [ 6 - r*(3 - R1*r/2) ] 60239310Sdim * 61239310Sdim * To compensate the error in the argument reduction, we use 62239310Sdim * expm1(r+c) = expm1(r) + c + expm1(r)*c 63239310Sdim * ~ expm1(r) + c + r*c 64239310Sdim * Thus c+r*c will be added in as the correction terms for 65239310Sdim * expm1(r+c). Now rearrange the term to avoid optimization 66239310Sdim * screw up: 67239310Sdim * ( 2 2 ) 68239310Sdim * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r ) 69239310Sdim * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- ) 70239310Sdim * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 ) 71239310Sdim * ( ) 72239310Sdim * 73239310Sdim * = r - E 74239310Sdim * 3. Scale back to obtain expm1(x): 75239310Sdim * From step 1, we have 76239310Sdim * expm1(x) = either 2^k*[expm1(r)+1] - 1 77239310Sdim * = or 2^k*[expm1(r) + (1-2^-k)] 78239310Sdim * 4. Implementation notes: 79239310Sdim * (A). To save one multiplication, we scale the coefficient Qi 80239310Sdim * to Qi*2^i, and replace z by (x^2)/2. 81239310Sdim * (B). To achieve maximum accuracy, we compute expm1(x) by 82239310Sdim * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf) 83239310Sdim * (ii) if k=0, return r-E 84239310Sdim * (iii) if k=-1, return 0.5*(r-E)-0.5 85239310Sdim * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E) 86239310Sdim * else return 1.0+2.0*(r-E); 87239310Sdim * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1) 88239310Sdim * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else 89239310Sdim * (vii) return 2^k(1-((E+2^-k)-r)) 90239310Sdim * 91239310Sdim * Special cases: 92239310Sdim * expm1(INF) is INF, expm1(NaN) is NaN; 93239310Sdim * expm1(-INF) is -1, and 94239310Sdim * for finite argument, only expm1(0)=0 is exact. 95239310Sdim * 96239310Sdim * Accuracy: 97239310Sdim * according to an error analysis, the error is always less than 98239310Sdim * 1 ulp (unit in the last place). 99239310Sdim * 100239310Sdim * Misc. info. 101239310Sdim * For IEEE double 102239310Sdim * if x > 7.09782712893383973096e+02 then expm1(x) overflow 103239310Sdim * 104239310Sdim * Constants: 105239310Sdim * The hexadecimal values are the intended ones for the following 106239310Sdim * constants. The decimal values may be used, provided that the 107239310Sdim * compiler will convert from decimal to binary accurately enough 108239310Sdim * to produce the hexadecimal values shown. 109239310Sdim */ 110239310Sdim 111239310Sdim#include <float.h> 112239310Sdim 113239310Sdim#include "math.h" 114239310Sdim#include "math_private.h" 115239310Sdim 116239310Sdimstatic const double 117239310Sdimone = 1.0, 118239310Sdimtiny = 1.0e-300, 119239310Sdimo_threshold = 7.09782712893383973096e+02,/* 0x40862E42, 0xFEFA39EF */ 120239310Sdimln2_hi = 6.93147180369123816490e-01,/* 0x3fe62e42, 0xfee00000 */ 121239310Sdimln2_lo = 1.90821492927058770002e-10,/* 0x3dea39ef, 0x35793c76 */ 122239310Sdiminvln2 = 1.44269504088896338700e+00,/* 0x3ff71547, 0x652b82fe */ 123239310Sdim/* Scaled Q's: Qn_here = 2**n * Qn_above, for R(2*z) where z = hxs = x*x/2: */ 124239310SdimQ1 = -3.33333333333331316428e-02, /* BFA11111 111110F4 */ 125239310SdimQ2 = 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */ 126239310SdimQ3 = -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */ 127239310SdimQ4 = 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */ 128239310SdimQ5 = -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */ 129239310Sdim 130239310Sdimstatic volatile double huge = 1.0e+300; 131239310Sdim 132239310Sdimdouble 133239310Sdimexpm1(double x) 134239310Sdim{ 135239310Sdim double y,hi,lo,c,t,e,hxs,hfx,r1,twopk; 136239310Sdim int32_t k,xsb; 137239310Sdim u_int32_t hx; 138239310Sdim 139239310Sdim GET_HIGH_WORD(hx,x); 140239310Sdim xsb = hx&0x80000000; /* sign bit of x */ 141239310Sdim hx &= 0x7fffffff; /* high word of |x| */ 142239310Sdim 143239310Sdim /* filter out huge and non-finite argument */ 144239310Sdim if(hx >= 0x4043687A) { /* if |x|>=56*ln2 */ 145239310Sdim if(hx >= 0x40862E42) { /* if |x|>=709.78... */ 146239310Sdim if(hx>=0x7ff00000) { 147239310Sdim u_int32_t low; 148239310Sdim GET_LOW_WORD(low,x); 149239310Sdim if(((hx&0xfffff)|low)!=0) 150239310Sdim return x+x; /* NaN */ 151239310Sdim else return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */ 152239310Sdim } 153239310Sdim if(x > o_threshold) return huge*huge; /* overflow */ 154239310Sdim } 155239310Sdim if(xsb!=0) { /* x < -56*ln2, return -1.0 with inexact */ 156239310Sdim if(x+tiny<0.0) /* raise inexact */ 157239310Sdim return tiny-one; /* return -1 */ 158239310Sdim } 159239310Sdim } 160239310Sdim 161239310Sdim /* argument reduction */ 162239310Sdim if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 163239310Sdim if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ 164239310Sdim if(xsb==0) 165239310Sdim {hi = x - ln2_hi; lo = ln2_lo; k = 1;} 166239310Sdim else 167239310Sdim {hi = x + ln2_hi; lo = -ln2_lo; k = -1;} 168239310Sdim } else { 169239310Sdim k = invln2*x+((xsb==0)?0.5:-0.5); 170239310Sdim t = k; 171239310Sdim hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ 172239310Sdim lo = t*ln2_lo; 173239310Sdim } 174239310Sdim STRICT_ASSIGN(double, x, hi - lo); 175239310Sdim c = (hi-x)-lo; 176239310Sdim } 177239310Sdim else if(hx < 0x3c900000) { /* when |x|<2**-54, return x */ 178239310Sdim t = huge+x; /* return x with inexact flags when x!=0 */ 179239310Sdim return x - (t-(huge+x)); 180239310Sdim } 181239310Sdim else k = 0; 182239310Sdim 183239310Sdim /* x is now in primary range */ 184239310Sdim hfx = 0.5*x; 185239310Sdim hxs = x*hfx; 186239310Sdim r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5)))); 187239310Sdim t = 3.0-r1*hfx; 188239310Sdim e = hxs*((r1-t)/(6.0 - x*t)); 189239310Sdim if(k==0) return x - (x*e-hxs); /* c is 0 */ 190239310Sdim else { 191239310Sdim INSERT_WORDS(twopk,0x3ff00000+(k<<20),0); /* 2^k */ 192239310Sdim e = (x*(e-c)-c); 193239310Sdim e -= hxs; 194239310Sdim if(k== -1) return 0.5*(x-e)-0.5; 195239310Sdim if(k==1) { 196239310Sdim if(x < -0.25) return -2.0*(e-(x+0.5)); 197239310Sdim else return one+2.0*(x-e); 198239310Sdim } 199239310Sdim if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */ 200239310Sdim y = one-(e-x); 201239310Sdim if (k == 1024) y = y*2.0*0x1p1023; 202239310Sdim else y = y*twopk; 203239310Sdim return y-one; 204239310Sdim } 205239310Sdim t = one; 206239310Sdim if(k<20) { 207239310Sdim SET_HIGH_WORD(t,0x3ff00000 - (0x200000>>k)); /* t=1-2^-k */ 208239310Sdim y = t-(e-x); 209239310Sdim y = y*twopk; 210239310Sdim } else { 211239310Sdim SET_HIGH_WORD(t,((0x3ff-k)<<20)); /* 2^-k */ 212239310Sdim y = x-(e+t); 213239310Sdim y += one; 214239310Sdim y = y*twopk; 215239310Sdim } 216239310Sdim } 217239310Sdim return y; 218239310Sdim} 219239310Sdim 220239310Sdim#if (LDBL_MANT_DIG == 53) 221239310Sdim__weak_reference(expm1, expm1l); 222239310Sdim#endif 223239310Sdim