expm1.c revision 25c28e83beb90e7c80452a7c818c5e6f73a07dc8
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER START
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The contents of this file are subject to the terms of the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Common Development and Distribution License (the "License").
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You may not use this file except in compliance with the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * See the License for the specific language governing permissions
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and limitations under the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER END
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Use is subject to license terms.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Returns exp(x)-1, the exponential of x minus 1.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1. Arugment reduction:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Given x, find r and integer k such that
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Here a correction term c will be computed to compensate
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * the error in r when rounded to a floating-point number.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2. Approximating expm1(r) by a special rational function on
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * the interval [0,0.34658]:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * we define R1(r*r) by
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * We use a special Reme algorithm on [0,0.347] to generate
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * a polynomial of degree 5 in r*r to approximate R1. The
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * maximum error of this polynomial approximation is bounded
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * by 2**-61. In other words,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * where Q1 = -1.6666666666666567384E-2,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Q2 = 3.9682539681370365873E-4,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Q3 = -9.9206344733435987357E-6,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Q4 = 2.5051361420808517002E-7,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Q5 = -6.2843505682382617102E-9;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (where z=r*r, and the values of Q1 to Q5 are listed below)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * with error bounded by
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1(r) = exp(r)-1 is then computed by the following
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * specific way which minimize the accumulation rounding error:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * r r [ 3 - (R1 + R1*r/2) ]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1(r) = r + --- + --- * [--------------------]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2 2 [ 6 - r*(3 - R1*r/2) ]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * To compensate the error in the argument reduction, we use
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1(r+c) = expm1(r) + c + expm1(r)*c
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ~ expm1(r) + c + r*c
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Thus c+r*c will be added in as the correction terms for
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1(r+c). Now rearrange the term to avoid optimization
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r )
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 )
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 3. Scale back to obtain expm1(x):
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * From step 1, we have
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1(x) = either 2^k*[expm1(r)+1] - 1
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * = or 2^k*[expm1(r) + (1-2^-k)]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 4. Implementation notes:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (A). To save one multiplication, we scale the coefficient Qi
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * to Qi*2^i, and replace z by (x^2)/2.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (B). To achieve maximum accuracy, we compute expm1(x) by
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (i) if x < -56*ln2, return -1.0, (raise inexact if x != inf)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (ii) if k=0, return r-E
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (iii) if k=-1, return 0.5*(r-E)-0.5
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * else return 1.0+2.0*(r-E);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * (vii) return 2^k(1-((E+2^-k)-r))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Special cases:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1(INF) is INF, expm1(NaN) is NaN;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1(-INF) is -1, and
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * for finite argument, only expm1(0)=0 is exact.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * according to an error analysis, the error is always less than
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1 ulp (unit in the last place).
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * For IEEE double
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * if x > 7.09782712893383973096e+02 then expm1(x) overflow
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The hexadecimal values are the intended ones for the following
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * constants. The decimal values may be used, provided that the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * compiler will convert from decimal to binary accurately enough
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * to produce the hexadecimal values shown.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisstatic const double xxx[] = {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* o_threshold */ 7.09782712893383973096e+02, /* 40862E42 FEFA39EF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* ln2_hi */ 6.93147180369123816490e-01, /* 3FE62E42 FEE00000 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* ln2_lo */ 1.90821492927058770002e-10, /* 3DEA39EF 35793C76 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* invln2 */ 1.44269504088896338700e+00, /* 3FF71547 652B82FE */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* scaled coefficients related to expm1 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* Q1 */ -3.33333333333331316428e-02, /* BFA11111 111110F4 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* Q2 */ 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* Q3 */ -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* Q4 */ 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* Q5 */ -2.01099218183624371326e-07 /* BE8AFDB7 6E09C32D */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis double y, hi, lo, c = 0.0L, t, e, hxs, hfx, r1;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis hx = ((unsigned *) &x)[HIWORD]; /* high word of x */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis xsb = hx & 0x80000000; /* sign bit of x */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = -x; /* y = |x| */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* filter out huge and non-finite argument */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* for example exp(38)-1 is approximately 3.1855932e+16 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* if |x|>=56*ln2 (~38.8162...) */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx >= 0x40862E42) { /* if |x|>=709.78... -> inf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (((hx & 0xfffff) | ((int *) &x)[LOWORD])
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (x * x); /* + -> * for Cheetah */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* exp(+-inf)={inf,-1} */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (xsb != 0) { /* x < -56*ln2, return -1.0 w/inexact */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* argument reduction */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* negative number */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* |x| > 1.5 ln2 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis k = (int) (invln2 * x + (xsb == 0 ? 0.5 : -0.5));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis hi = x - t * ln2_hi; /* t*ln2_hi is exact here */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis c = (hi - x) - lo; /* still at |x| > 0.5 ln2 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* when |x|<2**-54, return x */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis t = huge + x; /* return x w/inexact when x != 0 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (x - (t - (huge + x)));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* |x| <= 0.5 ln2 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* x is now in primary range */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis r1 = one + hxs * (Q1 + hxs * (Q2 + hxs * (Q3 + hxs * (Q4 + hxs * Q5))));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (k == 0) /* |x| <= 0.5 ln2 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (x - (x * e - hxs));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else { /* |x| > 0.5 ln2 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis e = (x * (e - c) - c);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (x < -0.25)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (k <= -2 || k > 56) { /* suffice to return exp(x)-1 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = one - (e - x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (y - one);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ((int *) &t)[HIWORD] = 0x3ff00000 - (0x200000 >> k);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* t = 1 - 2^-k */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = t - (e - x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ((int *) &t)[HIWORD] = (0x3ff - k) << 20; /* 2^-k */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = x - (e + t);