Print this page




 143 /* Q2 */                 1.58730158725481460165e-03,    /* 3F5A01A0 19FE5585 */
 144 /* Q3 */                -7.93650757867487942473e-05,    /* BF14CE19 9EAADBB7 */
 145 /* Q4 */                 4.00821782732936239552e-06,    /* 3ED0CFCA 86E65239 */
 146 /* Q5 */                -2.01099218183624371326e-07     /* BE8AFDB7 6E09C32D */
 147 };
 148 #define one             xxx[0]
 149 #define huge            xxx[1]
 150 #define tiny            xxx[2]
 151 #define o_threshold     xxx[3]
 152 #define ln2_hi          xxx[4]
 153 #define ln2_lo          xxx[5]
 154 #define invln2          xxx[6]
 155 #define Q1              xxx[7]
 156 #define Q2              xxx[8]
 157 #define Q3              xxx[9]
 158 #define Q4              xxx[10]
 159 #define Q5              xxx[11]
 160 
 161 double
 162 expm1(double x) {
 163         double y, hi, lo, c, t, e, hxs, hfx, r1;
 164         int k, xsb;
 165         unsigned hx;
 166 
 167         hx = ((unsigned *) &x)[HIWORD];             /* high word of x */
 168         xsb = hx & 0x80000000;                      /* sign bit of x */
 169         if (xsb == 0)
 170                 y = x;
 171         else
 172                 y = -x;                         /* y = |x| */
 173         hx &= 0x7fffffff;                   /* high word of |x| */
 174 
 175         /* filter out huge and non-finite arugment */
 176         if (hx >= 0x4043687A) {                      /* if |x|>=56*ln2 */
 177                 if (hx >= 0x40862E42) {              /* if |x|>=709.78... */

 178                         if (hx >= 0x7ff00000) {
 179                                 if (((hx & 0xfffff) | ((int *) &x)[LOWORD])
 180                                         != 0)
 181                                         return x * x;   /* + -> * for Cheetah */
 182                                 else
 183                                         return xsb == 0 ? x : -1.0;     /* exp(+-inf)={inf,-1} */
 184                         }
 185                         if (x > o_threshold)
 186                                 return huge * huge;     /* overflow */
 187                 }
 188                 if (xsb != 0) {         /* x < -56*ln2, return -1.0 w/inexact */
 189                         if (x + tiny < 0.0)          /* raise inexact */
 190                                 return tiny - one;      /* return -1 */
 191                 }
 192         }
 193 
 194         /* argument reduction */
 195         if (hx > 0x3fd62e42) {                       /* if  |x| > 0.5 ln2 */
 196                 if (hx < 0x3FF0A2B2) {               /* and |x| < 1.5 ln2 */
 197                         if (xsb == 0) {
 198                                 hi = x - ln2_hi;
 199                                 lo = ln2_lo;
 200                                 k = 1;
 201                         }
 202                         else {
 203                                 hi = x + ln2_hi;
 204                                 lo = -ln2_lo;
 205                                 k = -1;
 206                         }
 207                 }
 208                 else {
 209                         k = (int) (invln2 * x + (xsb == 0 ? 0.5 : -0.5));
 210                         t = k;
 211                         hi = x - t * ln2_hi;    /* t*ln2_hi is exact here */
 212                         lo = t * ln2_lo;
 213                 }
 214                 x = hi - lo;
 215                 c = (hi - x) - lo;
 216         }
 217         else if (hx < 0x3c900000) {          /* when |x|<2**-54, return x */
 218                 t = huge + x;           /* return x w/inexact when x != 0 */
 219                 return x - (t - (huge + x));
 220         }
 221         else
 222                 k = 0;
 223 
 224         /* x is now in primary range */
 225         hfx = 0.5 * x;
 226         hxs = x * hfx;
 227         r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
 228         t = 3.0 - r1 * hfx;
 229         e = hxs * ((r1 - t) / (6.0 - x * t));
 230         if (k == 0)
 231                 return x - (x * e - hxs);       /* c is 0 */
 232         else {
 233                 e = (x * (e - c) - c);
 234                 e -= hxs;
 235                 if (k == -1)
 236                         return 0.5 * (x - e) - 0.5;
 237                 if (k == 1)
 238                         if (x < -0.25)
 239                                 return -2.0 * (e - (x + 0.5));
 240                         else
 241                                 return one + 2.0 * (x - e);

 242                 if (k <= -2 || k > 56) {  /* suffice to return exp(x)-1 */
 243                         y = one - (e - x);
 244                         ((int *) &y)[HIWORD] += k << 20;
 245                         return y - one;
 246                 }
 247                 t = one;
 248                 if (k < 20) {
 249                         ((int *) &t)[HIWORD] = 0x3ff00000 - (0x200000 >> k);
 250                                                         /* t = 1 - 2^-k */
 251                         y = t - (e - x);
 252                         ((int *) &y)[HIWORD] += k << 20;
 253                 }
 254                 else {
 255                         ((int *) &t)[HIWORD] = (0x3ff - k) << 20; /* 2^-k */
 256                         y = x - (e + t);
 257                         y += one;
 258                         ((int *) &y)[HIWORD] += k << 20;
 259                 }
 260         }
 261         return y;


 143 /* Q2 */                 1.58730158725481460165e-03,    /* 3F5A01A0 19FE5585 */
 144 /* Q3 */                -7.93650757867487942473e-05,    /* BF14CE19 9EAADBB7 */
 145 /* Q4 */                 4.00821782732936239552e-06,    /* 3ED0CFCA 86E65239 */
 146 /* Q5 */                -2.01099218183624371326e-07     /* BE8AFDB7 6E09C32D */
 147 };
 148 #define one             xxx[0]
 149 #define huge            xxx[1]
 150 #define tiny            xxx[2]
 151 #define o_threshold     xxx[3]
 152 #define ln2_hi          xxx[4]
 153 #define ln2_lo          xxx[5]
 154 #define invln2          xxx[6]
 155 #define Q1              xxx[7]
 156 #define Q2              xxx[8]
 157 #define Q3              xxx[9]
 158 #define Q4              xxx[10]
 159 #define Q5              xxx[11]
 160 
 161 double
 162 expm1(double x) {
 163         double y, hi, lo, c = 0.0L, t, e, hxs, hfx, r1;
 164         int k, xsb;
 165         unsigned hx;
 166 
 167         hx = ((unsigned *) &x)[HIWORD];             /* high word of x */
 168         xsb = hx & 0x80000000;                      /* sign bit of x */
 169         if (xsb == 0)
 170                 y = x;
 171         else
 172                 y = -x;                         /* y = |x| */
 173         hx &= 0x7fffffff;                   /* high word of |x| */
 174 
 175         /* filter out huge and non-finite argument */
 176         /* for example exp(38)-1 is approximately 3.1855932e+16 */
 177         if (hx >= 0x4043687A) {                      /* if |x|>=56*ln2  (~38.8162...)*/
 178                 if (hx >= 0x40862E42) {              /* if |x|>=709.78... -> inf */
 179                         if (hx >= 0x7ff00000) {
 180                                 if (((hx & 0xfffff) | ((int *) &x)[LOWORD])
 181                                         != 0)
 182                                         return x * x;   /* + -> * for Cheetah */
 183                                 else
 184                                         return xsb == 0 ? x : -1.0;     /* exp(+-inf)={inf,-1} */
 185                         }
 186                         if (x > o_threshold)
 187                                 return huge * huge;     /* overflow */
 188                 }
 189                 if (xsb != 0) {         /* x < -56*ln2, return -1.0 w/inexact */
 190                         if (x + tiny < 0.0)          /* raise inexact */
 191                                 return tiny - one;      /* return -1 */
 192                 }
 193         }
 194 
 195         /* argument reduction */
 196         if (hx > 0x3fd62e42) {                       /* if  |x| > 0.5 ln2 */
 197                 if (hx < 0x3FF0A2B2) {               /* and |x| < 1.5 ln2 */
 198                         if (xsb == 0) {         /* positive number */
 199                                 hi = x - ln2_hi;
 200                                 lo = ln2_lo;
 201                                 k = 1;
 202                         }
 203                         else { /* negative number */
 204                                 hi = x + ln2_hi;
 205                                 lo = -ln2_lo;
 206                                 k = -1;
 207                         }
 208                 }
 209                 else {  /* |x| > 1.5 ln2 */
 210                         k = (int) (invln2 * x + (xsb == 0 ? 0.5 : -0.5));
 211                         t = k;
 212                         hi = x - t * ln2_hi;    /* t*ln2_hi is exact here */
 213                         lo = t * ln2_lo;
 214                 }
 215                 x = hi - lo;
 216                 c = (hi - x) - lo; /* still at |x| > 0.5 ln2 */
 217         }
 218         else if (hx < 0x3c900000) {          /* when |x|<2**-54, return x */
 219                 t = huge + x;           /* return x w/inexact when x != 0 */
 220                 return x - (t - (huge + x));
 221         }
 222         else    /* |x| <= 0.5 ln2 */
 223                 k = 0;
 224 
 225         /* x is now in primary range */
 226         hfx = 0.5 * x;
 227         hxs = x * hfx;
 228         r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
 229         t = 3.0 - r1 * hfx;
 230         e = hxs * ((r1 - t) / (6.0 - x * t));
 231         if (k == 0) /* |x| <= 0.5 ln2 */
 232                 return x - (x * e - hxs);
 233         else {      /* |x| > 0.5 ln2 */
 234                 e = (x * (e - c) - c);
 235                 e -= hxs;
 236                 if (k == -1)
 237                         return 0.5 * (x - e) - 0.5;
 238                 if (k == 1) {
 239                         if (x < -0.25)
 240                                 return -2.0 * (e - (x + 0.5));
 241                         else
 242                                 return one + 2.0 * (x - e);
 243                 }
 244                 if (k <= -2 || k > 56) {  /* suffice to return exp(x)-1 */
 245                         y = one - (e - x);
 246                         ((int *) &y)[HIWORD] += k << 20;
 247                         return y - one;
 248                 }
 249                 t = one;
 250                 if (k < 20) {
 251                         ((int *) &t)[HIWORD] = 0x3ff00000 - (0x200000 >> k);
 252                                                         /* t = 1 - 2^-k */
 253                         y = t - (e - x);
 254                         ((int *) &y)[HIWORD] += k << 20;
 255                 }
 256                 else {
 257                         ((int *) &t)[HIWORD] = (0x3ff - k) << 20; /* 2^-k */
 258                         y = x - (e + t);
 259                         y += one;
 260                         ((int *) &y)[HIWORD] += k << 20;
 261                 }
 262         }
 263         return y;