Blame sysdeps/ieee754/dbl-64/s_expm1.c

Packit 6c4009
/* @(#)s_expm1.c 5.1 93/09/24 */
Packit 6c4009
/*
Packit 6c4009
 * ====================================================
Packit 6c4009
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
Packit 6c4009
 *
Packit 6c4009
 * Developed at SunPro, a Sun Microsystems, Inc. business.
Packit 6c4009
 * Permission to use, copy, modify, and distribute this
Packit 6c4009
 * software is freely granted, provided that this notice
Packit 6c4009
 * is preserved.
Packit 6c4009
 * ====================================================
Packit 6c4009
 */
Packit 6c4009
/* Modified by Naohiko Shimizu/Tokai University, Japan 1997/08/25,
Packit 6c4009
   for performance improvement on pipelined processors.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
/* expm1(x)
Packit 6c4009
 * Returns exp(x)-1, the exponential of x minus 1.
Packit 6c4009
 *
Packit 6c4009
 * Method
Packit 6c4009
 *   1. Argument reduction:
Packit 6c4009
 *	Given x, find r and integer k such that
Packit 6c4009
 *
Packit 6c4009
 *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
Packit 6c4009
 *
Packit 6c4009
 *      Here a correction term c will be computed to compensate
Packit 6c4009
 *	the error in r when rounded to a floating-point number.
Packit 6c4009
 *
Packit 6c4009
 *   2. Approximating expm1(r) by a special rational function on
Packit 6c4009
 *	the interval [0,0.34658]:
Packit 6c4009
 *	Since
Packit 6c4009
 *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
Packit 6c4009
 *	we define R1(r*r) by
Packit 6c4009
 *	    r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
Packit 6c4009
 *	That is,
Packit 6c4009
 *	    R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
Packit 6c4009
 *		     = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
Packit 6c4009
 *		     = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
Packit 6c4009
 *      We use a special Reme algorithm on [0,0.347] to generate
Packit 6c4009
 *	a polynomial of degree 5 in r*r to approximate R1. The
Packit 6c4009
 *	maximum error of this polynomial approximation is bounded
Packit 6c4009
 *	by 2**-61. In other words,
Packit 6c4009
 *	    R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
Packit 6c4009
 *	where	Q1  =  -1.6666666666666567384E-2,
Packit 6c4009
 *		Q2  =   3.9682539681370365873E-4,
Packit 6c4009
 *		Q3  =  -9.9206344733435987357E-6,
Packit 6c4009
 *		Q4  =   2.5051361420808517002E-7,
Packit 6c4009
 *		Q5  =  -6.2843505682382617102E-9;
Packit 6c4009
 *	(where z=r*r, and the values of Q1 to Q5 are listed below)
Packit 6c4009
 *	with error bounded by
Packit 6c4009
 *	    |                  5           |     -61
Packit 6c4009
 *	    | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2
Packit 6c4009
 *	    |                              |
Packit 6c4009
 *
Packit 6c4009
 *	expm1(r) = exp(r)-1 is then computed by the following
Packit 6c4009
 *	specific way which minimize the accumulation rounding error:
Packit 6c4009
 *			       2     3
Packit 6c4009
 *			      r     r    [ 3 - (R1 + R1*r/2)  ]
Packit 6c4009
 *	      expm1(r) = r + --- + --- * [--------------------]
Packit 6c4009
 *			      2     2    [ 6 - r*(3 - R1*r/2) ]
Packit 6c4009
 *
Packit 6c4009
 *	To compensate the error in the argument reduction, we use
Packit 6c4009
 *		expm1(r+c) = expm1(r) + c + expm1(r)*c
Packit 6c4009
 *			   ~ expm1(r) + c + r*c
Packit 6c4009
 *	Thus c+r*c will be added in as the correction terms for
Packit 6c4009
 *	expm1(r+c). Now rearrange the term to avoid optimization
Packit 6c4009
 *	screw up:
Packit 6c4009
 *			(      2                                    2 )
Packit 6c4009
 *			({  ( r    [ R1 -  (3 - R1*r/2) ]  )  }    r  )
Packit 6c4009
 *	 expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
Packit 6c4009
 *			({  ( 2    [ 6 - r*(3 - R1*r/2) ]  )  }    2  )
Packit 6c4009
 *                      (                                             )
Packit 6c4009
 *
Packit 6c4009
 *		   = r - E
Packit 6c4009
 *   3. Scale back to obtain expm1(x):
Packit 6c4009
 *	From step 1, we have
Packit 6c4009
 *	   expm1(x) = either 2^k*[expm1(r)+1] - 1
Packit 6c4009
 *		    = or     2^k*[expm1(r) + (1-2^-k)]
Packit 6c4009
 *   4. Implementation notes:
Packit 6c4009
 *	(A). To save one multiplication, we scale the coefficient Qi
Packit 6c4009
 *	     to Qi*2^i, and replace z by (x^2)/2.
Packit 6c4009
 *	(B). To achieve maximum accuracy, we compute expm1(x) by
Packit 6c4009
 *	  (i)   if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
Packit 6c4009
 *	  (ii)  if k=0, return r-E
Packit 6c4009
 *	  (iii) if k=-1, return 0.5*(r-E)-0.5
Packit 6c4009
 *        (iv)	if k=1 if r < -0.25, return 2*((r+0.5)- E)
Packit 6c4009
 *		       else	     return  1.0+2.0*(r-E);
Packit 6c4009
 *	  (v)   if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
Packit 6c4009
 *	  (vi)  if k <= 20, return 2^k((1-2^-k)-(E-r)), else
Packit 6c4009
 *	  (vii) return 2^k(1-((E+2^-k)-r))
Packit 6c4009
 *
Packit 6c4009
 * Special cases:
Packit 6c4009
 *	expm1(INF) is INF, expm1(NaN) is NaN;
Packit 6c4009
 *	expm1(-INF) is -1, and
Packit 6c4009
 *	for finite argument, only expm1(0)=0 is exact.
Packit 6c4009
 *
Packit 6c4009
 * Accuracy:
Packit 6c4009
 *	according to an error analysis, the error is always less than
Packit 6c4009
 *	1 ulp (unit in the last place).
Packit 6c4009
 *
Packit 6c4009
 * Misc. info.
Packit 6c4009
 *	For IEEE double
Packit 6c4009
 *	    if x >  7.09782712893383973096e+02 then expm1(x) overflow
Packit 6c4009
 *
Packit 6c4009
 * Constants:
Packit 6c4009
 * The hexadecimal values are the intended ones for the following
Packit 6c4009
 * constants. The decimal values may be used, provided that the
Packit 6c4009
 * compiler will convert from decimal to binary accurately enough
Packit 6c4009
 * to produce the hexadecimal values shown.
Packit 6c4009
 */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <float.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math-barriers.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <math-underflow.h>
Packit 6c4009
#include <libm-alias-double.h>
Packit 6c4009
#define one Q[0]
Packit 6c4009
static const double
Packit 6c4009
  huge = 1.0e+300,
Packit 6c4009
  tiny = 1.0e-300,
Packit 6c4009
  o_threshold = 7.09782712893383973096e+02,  /* 0x40862E42, 0xFEFA39EF */
Packit 6c4009
  ln2_hi = 6.93147180369123816490e-01,       /* 0x3fe62e42, 0xfee00000 */
Packit 6c4009
  ln2_lo = 1.90821492927058770002e-10,       /* 0x3dea39ef, 0x35793c76 */
Packit 6c4009
  invln2 = 1.44269504088896338700e+00,       /* 0x3ff71547, 0x652b82fe */
Packit 6c4009
/* scaled coefficients related to expm1 */
Packit 6c4009
  Q[] = { 1.0, -3.33333333333331316428e-02, /* BFA11111 111110F4 */
Packit 6c4009
	  1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
Packit 6c4009
	  -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
Packit 6c4009
	  4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
Packit 6c4009
	  -2.01099218183624371326e-07 }; /* BE8AFDB7 6E09C32D */
Packit 6c4009
Packit 6c4009
double
Packit 6c4009
__expm1 (double x)
Packit 6c4009
{
Packit 6c4009
  double y, hi, lo, c, t, e, hxs, hfx, r1, h2, h4, R1, R2, R3;
Packit 6c4009
  int32_t k, xsb;
Packit 6c4009
  uint32_t hx;
Packit 6c4009
Packit 6c4009
  GET_HIGH_WORD (hx, x);
Packit 6c4009
  xsb = hx & 0x80000000;                /* sign bit of x */
Packit 6c4009
  if (xsb == 0)
Packit 6c4009
    y = x;
Packit 6c4009
  else
Packit 6c4009
    y = -x;                             /* y = |x| */
Packit 6c4009
  hx &= 0x7fffffff;                     /* high word of |x| */
Packit 6c4009
Packit 6c4009
  /* filter out huge and non-finite argument */
Packit 6c4009
  if (hx >= 0x4043687A)                         /* if |x|>=56*ln2 */
Packit 6c4009
    {
Packit 6c4009
      if (hx >= 0x40862E42)                     /* if |x|>=709.78... */
Packit 6c4009
	{
Packit 6c4009
	  if (hx >= 0x7ff00000)
Packit 6c4009
	    {
Packit 6c4009
	      uint32_t low;
Packit 6c4009
	      GET_LOW_WORD (low, x);
Packit 6c4009
	      if (((hx & 0xfffff) | low) != 0)
Packit 6c4009
		return x + x;            /* NaN */
Packit 6c4009
	      else
Packit 6c4009
		return (xsb == 0) ? x : -1.0;    /* exp(+-inf)={inf,-1} */
Packit 6c4009
	    }
Packit 6c4009
	  if (x > o_threshold)
Packit 6c4009
	    {
Packit 6c4009
	      __set_errno (ERANGE);
Packit 6c4009
	      return huge * huge;   /* overflow */
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      if (xsb != 0)      /* x < -56*ln2, return -1.0 with inexact */
Packit 6c4009
	{
Packit 6c4009
	  math_force_eval (x + tiny);           /* raise inexact */
Packit 6c4009
	  return tiny - one;            /* return -1 */
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* argument reduction */
Packit 6c4009
  if (hx > 0x3fd62e42)                  /* if  |x| > 0.5 ln2 */
Packit 6c4009
    {
Packit 6c4009
      if (hx < 0x3FF0A2B2)              /* and |x| < 1.5 ln2 */
Packit 6c4009
	{
Packit 6c4009
	  if (xsb == 0)
Packit 6c4009
	    {
Packit 6c4009
	      hi = x - ln2_hi; lo = ln2_lo;  k = 1;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      hi = x + ln2_hi; lo = -ln2_lo;  k = -1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  k = invln2 * x + ((xsb == 0) ? 0.5 : -0.5);
Packit 6c4009
	  t = k;
Packit 6c4009
	  hi = x - t * ln2_hi;          /* t*ln2_hi is exact here */
Packit 6c4009
	  lo = t * ln2_lo;
Packit 6c4009
	}
Packit 6c4009
      x = hi - lo;
Packit 6c4009
      c = (hi - x) - lo;
Packit 6c4009
    }
Packit 6c4009
  else if (hx < 0x3c900000)             /* when |x|<2**-54, return x */
Packit 6c4009
    {
Packit 6c4009
      math_check_force_underflow (x);
Packit 6c4009
      t = huge + x;     /* return x with inexact flags when x!=0 */
Packit 6c4009
      return x - (t - (huge + x));
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    k = 0;
Packit 6c4009
Packit 6c4009
  /* x is now in primary range */
Packit 6c4009
  hfx = 0.5 * x;
Packit 6c4009
  hxs = x * hfx;
Packit 6c4009
  R1 = one + hxs * Q[1]; h2 = hxs * hxs;
Packit 6c4009
  R2 = Q[2] + hxs * Q[3]; h4 = h2 * h2;
Packit 6c4009
  R3 = Q[4] + hxs * Q[5];
Packit 6c4009
  r1 = R1 + h2 * R2 + h4 * R3;
Packit 6c4009
  t = 3.0 - r1 * hfx;
Packit 6c4009
  e = hxs * ((r1 - t) / (6.0 - x * t));
Packit 6c4009
  if (k == 0)
Packit 6c4009
    return x - (x * e - hxs);                   /* c is 0 */
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      e = (x * (e - c) - c);
Packit 6c4009
      e -= hxs;
Packit 6c4009
      if (k == -1)
Packit 6c4009
	return 0.5 * (x - e) - 0.5;
Packit 6c4009
      if (k == 1)
Packit 6c4009
	{
Packit 6c4009
	  if (x < -0.25)
Packit 6c4009
	    return -2.0 * (e - (x + 0.5));
Packit 6c4009
	  else
Packit 6c4009
	    return one + 2.0 * (x - e);
Packit 6c4009
	}
Packit 6c4009
      if (k <= -2 || k > 56)         /* suffice to return exp(x)-1 */
Packit 6c4009
	{
Packit 6c4009
	  uint32_t high;
Packit 6c4009
	  y = one - (e - x);
Packit 6c4009
	  GET_HIGH_WORD (high, y);
Packit 6c4009
	  SET_HIGH_WORD (y, high + (k << 20));  /* add k to y's exponent */
Packit 6c4009
	  return y - one;
Packit 6c4009
	}
Packit 6c4009
      t = one;
Packit 6c4009
      if (k < 20)
Packit 6c4009
	{
Packit 6c4009
	  uint32_t high;
Packit 6c4009
	  SET_HIGH_WORD (t, 0x3ff00000 - (0x200000 >> k));    /* t=1-2^-k */
Packit 6c4009
	  y = t - (e - x);
Packit 6c4009
	  GET_HIGH_WORD (high, y);
Packit 6c4009
	  SET_HIGH_WORD (y, high + (k << 20));  /* add k to y's exponent */
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  uint32_t high;
Packit 6c4009
	  SET_HIGH_WORD (t, ((0x3ff - k) << 20));       /* 2^-k */
Packit 6c4009
	  y = x - (e + t);
Packit 6c4009
	  y += one;
Packit 6c4009
	  GET_HIGH_WORD (high, y);
Packit 6c4009
	  SET_HIGH_WORD (y, high + (k << 20));  /* add k to y's exponent */
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  return y;
Packit 6c4009
}
Packit 6c4009
libm_alias_double (__expm1, expm1)