Print this page
11210 libm should be cstyle(1ONBLD) clean


   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */

  25 /*
  26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29 
  30 #pragma weak __expm1 = expm1
  31 
  32 /* INDENT OFF */
  33 /*
  34  * expm1(x)
  35  * Returns exp(x)-1, the exponential of x minus 1.
  36  *
  37  * Method
  38  *   1. Arugment reduction:
  39  *      Given x, find r and integer k such that
  40  *
  41  *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
  42  *
  43  *      Here a correction term c will be computed to compensate
  44  *      the error in r when rounded to a floating-point number.
  45  *
  46  *   2. Approximating expm1(r) by a special rational function on
  47  *      the interval [0,0.34658]:
  48  *      Since
  49  *          r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
  50  *      we define R1(r*r) by
  51  *          r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
  52  *      That is,


 108  *
 109  * Special cases:
 110  *      expm1(INF) is INF, expm1(NaN) is NaN;
 111  *      expm1(-INF) is -1, and
 112  *      for finite argument, only expm1(0)=0 is exact.
 113  *
 114  * Accuracy:
 115  *      according to an error analysis, the error is always less than
 116  *      1 ulp (unit in the last place).
 117  *
 118  * Misc. info.
 119  *      For IEEE double
 120  *          if x >  7.09782712893383973096e+02 then expm1(x) overflow
 121  *
 122  * Constants:
 123  * The hexadecimal values are the intended ones for the following
 124  * constants. The decimal values may be used, provided that the
 125  * compiler will convert from decimal to binary accurately enough
 126  * to produce the hexadecimal values shown.
 127  */
 128 /* INDENT ON */
 129 
 130 #include "libm_macros.h"
 131 #include <math.h>
 132 
 133 static const double xxx[] = {
 134 /* one */                1.0,

 135 /* huge */               1.0e+300,
 136 /* tiny */               1.0e-300,
 137 /* o_threshold */        7.09782712893383973096e+02,    /* 40862E42 FEFA39EF */
 138 /* ln2_hi */             6.93147180369123816490e-01,    /* 3FE62E42 FEE00000 */
 139 /* ln2_lo */             1.90821492927058770002e-10,    /* 3DEA39EF 35793C76 */
 140 /* invln2 */             1.44269504088896338700e+00,    /* 3FF71547 652B82FE */
 141 /* scaled coefficients related to expm1 */
 142 /* Q1 */                -3.33333333333331316428e-02,    /* BFA11111 111110F4 */




 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) {
 178                 /* if |x|>=56*ln2 (~38.8162...) */
 179                 if (hx >= 0x40862E42) {              /* if |x|>=709.78... -> inf */
 180                         if (hx >= 0x7ff00000) {
 181                                 if (((hx & 0xfffff) | ((int *) &x)[LOWORD])
 182                                         != 0)
 183                                         return (x * x); /* + -> * for Cheetah */
 184                                 else
 185                                         /* exp(+-inf)={inf,-1} */
 186                                         return (xsb == 0 ? x : -1.0);
 187                         }

 188                         if (x > o_threshold)
 189                                 return (huge * huge);   /* overflow */
 190                 }

 191                 if (xsb != 0) {         /* x < -56*ln2, return -1.0 w/inexact */
 192                         if (x + tiny < 0.0)          /* raise inexact */
 193                                 return (tiny - one);    /* return -1 */
 194                 }
 195         }
 196 
 197         /* argument reduction */
 198         if (hx > 0x3fd62e42) {                       /* if  |x| > 0.5 ln2 */
 199                 if (hx < 0x3FF0A2B2) {               /* and |x| < 1.5 ln2 */
 200                         if (xsb == 0) {         /* positive number */
 201                                 hi = x - ln2_hi;
 202                                 lo = ln2_lo;
 203                                 k = 1;
 204                         } else {
 205                                 /* negative number */
 206                                 hi = x + ln2_hi;
 207                                 lo = -ln2_lo;
 208                                 k = -1;
 209                         }
 210                 } else {
 211                         /* |x| > 1.5 ln2 */
 212                         k = (int) (invln2 * x + (xsb == 0 ? 0.5 : -0.5));
 213                         t = k;
 214                         hi = x - t * ln2_hi;    /* t*ln2_hi is exact here */
 215                         lo = t * ln2_lo;
 216                 }

 217                 x = hi - lo;
 218                 c = (hi - x) - lo; /* still at |x| > 0.5 ln2 */
 219         } else if (hx < 0x3c900000) {
 220                 /* when |x|<2**-54, return x */
 221                 t = huge + x;           /* return x w/inexact when x != 0 */
 222                 return (x - (t - (huge + x)));
 223         } else
 224                 /* |x| <= 0.5 ln2 */
 225                 k = 0;

 226 
 227         /* x is now in primary range */
 228         hfx = 0.5 * x;
 229         hxs = x * hfx;
 230         r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
 231         t = 3.0 - r1 * hfx;
 232         e = hxs * ((r1 - t) / (6.0 - x * t));
 233         if (k == 0) /* |x| <= 0.5 ln2 */

 234                 return (x - (x * e - hxs));
 235         else {          /* |x| > 0.5 ln2 */
 236                 e = (x * (e - c) - c);
 237                 e -= hxs;

 238                 if (k == -1)
 239                         return (0.5 * (x - e) - 0.5);

 240                 if (k == 1) {
 241                         if (x < -0.25)
 242                                 return (-2.0 * (e - (x + 0.5)));
 243                         else
 244                                 return (one + 2.0 * (x - e));
 245                 }

 246                 if (k <= -2 || k > 56) {  /* suffice to return exp(x)-1 */
 247                         y = one - (e - x);
 248                         ((int *) &y)[HIWORD] += k << 20;
 249                         return (y - one);
 250                 }

 251                 t = one;

 252                 if (k < 20) {
 253                         ((int *) &t)[HIWORD] = 0x3ff00000 - (0x200000 >> k);
 254                                                         /* t = 1 - 2^-k */
 255                         y = t - (e - x);
 256                         ((int *) &y)[HIWORD] += k << 20;
 257                 } else {
 258                         ((int *) &t)[HIWORD] = (0x3ff - k) << 20; /* 2^-k */
 259                         y = x - (e + t);
 260                         y += one;
 261                         ((int *) &y)[HIWORD] += k << 20;
 262                 }
 263         }

 264         return (y);
 265 }


   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak __expm1 = expm1
  32 
  33 
  34 /*
  35  * expm1(x)
  36  * Returns exp(x)-1, the exponential of x minus 1.
  37  *
  38  * Method
  39  *   1. Arugment reduction:
  40  *      Given x, find r and integer k such that
  41  *
  42  *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
  43  *
  44  *      Here a correction term c will be computed to compensate
  45  *      the error in r when rounded to a floating-point number.
  46  *
  47  *   2. Approximating expm1(r) by a special rational function on
  48  *      the interval [0,0.34658]:
  49  *      Since
  50  *          r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
  51  *      we define R1(r*r) by
  52  *          r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
  53  *      That is,


 109  *
 110  * Special cases:
 111  *      expm1(INF) is INF, expm1(NaN) is NaN;
 112  *      expm1(-INF) is -1, and
 113  *      for finite argument, only expm1(0)=0 is exact.
 114  *
 115  * Accuracy:
 116  *      according to an error analysis, the error is always less than
 117  *      1 ulp (unit in the last place).
 118  *
 119  * Misc. info.
 120  *      For IEEE double
 121  *          if x >  7.09782712893383973096e+02 then expm1(x) overflow
 122  *
 123  * Constants:
 124  * The hexadecimal values are the intended ones for the following
 125  * constants. The decimal values may be used, provided that the
 126  * compiler will convert from decimal to binary accurately enough
 127  * to produce the hexadecimal values shown.
 128  */

 129 
 130 #include "libm_macros.h"
 131 #include <math.h>
 132 
 133 static const double xxx[] = {
 134 /* one */
 135         1.0,
 136 /* huge */ 1.0e+300,
 137 /* tiny */ 1.0e-300,
 138 /* o_threshold */ 7.09782712893383973096e+02,   /* 40862E42 FEFA39EF */
 139 /* ln2_hi */ 6.93147180369123816490e-01,        /* 3FE62E42 FEE00000 */
 140 /* ln2_lo */ 1.90821492927058770002e-10,        /* 3DEA39EF 35793C76 */
 141 /* invln2 */ 1.44269504088896338700e+00,        /* 3FF71547 652B82FE */
 142 
 143 /*
 144  * scaled coefficients related to expm1
 145  * Q1
 146  */
 147         -3.33333333333331316428e-02,            /* BFA11111 111110F4 */
 148 /* Q2 */ 1.58730158725481460165e-03,            /* 3F5A01A0 19FE5585 */
 149 /* Q3 */ -7.93650757867487942473e-05,           /* BF14CE19 9EAADBB7 */
 150 /* Q4 */ 4.00821782732936239552e-06,            /* 3ED0CFCA 86E65239 */
 151 /* Q5 */ -2.01099218183624371326e-07            /* BE8AFDB7 6E09C32D */
 152 };
 153 
 154 #define one                     xxx[0]
 155 #define huge                    xxx[1]
 156 #define tiny                    xxx[2]
 157 #define o_threshold             xxx[3]
 158 #define ln2_hi                  xxx[4]
 159 #define ln2_lo                  xxx[5]
 160 #define invln2                  xxx[6]
 161 #define Q1                      xxx[7]
 162 #define Q2                      xxx[8]
 163 #define Q3                      xxx[9]
 164 #define Q4                      xxx[10]
 165 #define Q5                      xxx[11]
 166 
 167 double
 168 expm1(double x)
 169 {
 170         double y, hi, lo, c = 0.0L, t, e, hxs, hfx, r1;
 171         int k, xsb;
 172         unsigned hx;
 173 
 174         hx = ((unsigned *)&x)[HIWORD];      /* high word of x */
 175         xsb = hx & 0x80000000;              /* sign bit of x */
 176 
 177         if (xsb == 0)
 178                 y = x;
 179         else
 180                 y = -x;                 /* y = |x| */
 181 
 182         hx &= 0x7fffffff;           /* high word of |x| */
 183 
 184         /*
 185          * filter out huge and non-finite argument
 186          * for example exp(38)-1 is approximately 3.1855932e+16
 187          */
 188         if (hx >= 0x4043687A) {
 189                 /* if |x|>=56*ln2 (~38.8162...) */
 190                 if (hx >= 0x40862E42) {      /* if |x|>=709.78... -> inf */
 191                         if (hx >= 0x7ff00000) {
 192                                 if (((hx & 0xfffff) | ((int *)&x)[LOWORD]) != 0)

 193                                         return (x * x); /* + -> * for Cheetah */
 194                                 else
 195                                         /* exp(+-inf)={inf,-1} */
 196                                         return (xsb == 0 ? x : -1.0);
 197                         }
 198 
 199                         if (x > o_threshold)
 200                                 return (huge * huge);   /* overflow */
 201                 }
 202 
 203                 if (xsb != 0) { /* x < -56*ln2, return -1.0 w/inexact */
 204                         if (x + tiny < 0.0)          /* raise inexact */
 205                                 return (tiny - one);    /* return -1 */
 206                 }
 207         }
 208 
 209         /* argument reduction */
 210         if (hx > 0x3fd62e42) {               /* if  |x| > 0.5 ln2 */
 211                 if (hx < 0x3FF0A2B2) {       /* and |x| < 1.5 ln2 */
 212                         if (xsb == 0) { /* positive number */
 213                                 hi = x - ln2_hi;
 214                                 lo = ln2_lo;
 215                                 k = 1;
 216                         } else {
 217                                 /* negative number */
 218                                 hi = x + ln2_hi;
 219                                 lo = -ln2_lo;
 220                                 k = -1;
 221                         }
 222                 } else {
 223                         /* |x| > 1.5 ln2 */
 224                         k = (int)(invln2 * x + (xsb == 0 ? 0.5 : -0.5));
 225                         t = k;
 226                         hi = x - t * ln2_hi;    /* t*ln2_hi is exact here */
 227                         lo = t * ln2_lo;
 228                 }
 229 
 230                 x = hi - lo;
 231                 c = (hi - x) - lo;      /* still at |x| > 0.5 ln2 */
 232         } else if (hx < 0x3c900000) {
 233                 /* when |x|<2**-54, return x */
 234                 t = huge + x;           /* return x w/inexact when x != 0 */
 235                 return (x - (t - (huge + x)));
 236         } else {
 237                 /* |x| <= 0.5 ln2 */
 238                 k = 0;
 239         }
 240 
 241         /* x is now in primary range */
 242         hfx = 0.5 * x;
 243         hxs = x * hfx;
 244         r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
 245         t = 3.0 - r1 * hfx;
 246         e = hxs * ((r1 - t) / (6.0 - x * t));
 247 
 248         if (k == 0) {                   /* |x| <= 0.5 ln2 */
 249                 return (x - (x * e - hxs));
 250         } else {                        /* |x| > 0.5 ln2 */
 251                 e = (x * (e - c) - c);
 252                 e -= hxs;
 253 
 254                 if (k == -1)
 255                         return (0.5 * (x - e) - 0.5);
 256 
 257                 if (k == 1) {
 258                         if (x < -0.25)
 259                                 return (-2.0 * (e - (x + 0.5)));
 260                         else
 261                                 return (one + 2.0 * (x - e));
 262                 }
 263 
 264                 if (k <= -2 || k > 56) {  /* suffice to return exp(x)-1 */
 265                         y = one - (e - x);
 266                         ((int *)&y)[HIWORD] += k << 20;
 267                         return (y - one);
 268                 }
 269 
 270                 t = one;
 271 
 272                 if (k < 20) {
 273                         ((int *)&t)[HIWORD] = 0x3ff00000 - (0x200000 >> k);
 274                         /* t = 1 - 2^-k */
 275                         y = t - (e - x);
 276                         ((int *)&y)[HIWORD] += k << 20;
 277                 } else {
 278                         ((int *)&t)[HIWORD] = (0x3ff - k) << 20; /* 2^-k */
 279                         y = x - (e + t);
 280                         y += one;
 281                         ((int *)&y)[HIWORD] += k << 20;
 282                 }
 283         }
 284 
 285         return (y);
 286 }