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

Packit 6c4009
/*
Packit 6c4009
 * IBM Accurate Mathematical Library
Packit 6c4009
 * written by International Business Machines Corp.
Packit 6c4009
 * Copyright (C) 2001-2018 Free Software Foundation, Inc.
Packit 6c4009
 *
Packit 6c4009
 * This program is free software; you can redistribute it and/or modify
Packit 6c4009
 * it under the terms of the GNU Lesser General Public License as published by
Packit 6c4009
 * the Free Software Foundation; either version 2.1 of the License, or
Packit 6c4009
 * (at your option) any later version.
Packit 6c4009
 *
Packit 6c4009
 * This program is distributed in the hope that it will be useful,
Packit 6c4009
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
 * GNU Lesser General Public License for more details.
Packit 6c4009
 *
Packit 6c4009
 * You should have received a copy of the GNU Lesser General Public License
Packit 6c4009
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
 */
Packit 6c4009
/***************************************************************************/
Packit 6c4009
/*  MODULE_NAME:uexp.c                                                     */
Packit 6c4009
/*                                                                         */
Packit 6c4009
/*  FUNCTION:uexp                                                          */
Packit 6c4009
/*           exp1                                                          */
Packit 6c4009
/*                                                                         */
Packit 6c4009
/* FILES NEEDED:dla.h endian.h mpa.h mydefs.h uexp.h                       */
Packit 6c4009
/*                                                                         */
Packit 6c4009
/* An ultimate exp routine. Given an IEEE double machine number x          */
Packit 6c4009
/* it computes an almost correctly rounded (to nearest) value of e^x       */
Packit 6c4009
/* Assumption: Machine arithmetic operations are performed in              */
Packit 6c4009
/* round to nearest mode of IEEE 754 standard.                             */
Packit 6c4009
/*                                                                         */
Packit 6c4009
/***************************************************************************/
Packit 6c4009
Packit 6c4009
#include <math.h>
Packit 6c4009
#include "endian.h"
Packit 6c4009
#include "uexp.h"
Packit 6c4009
#include "mydefs.h"
Packit 6c4009
#include "MathLib.h"
Packit 6c4009
#include "uexp.tbl"
Packit 6c4009
#include <math-barriers.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <float.h>
Packit 6c4009
#include "eexp.tbl"
Packit 6c4009
Packit 6c4009
#ifndef SECTION
Packit 6c4009
# define SECTION
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
double
Packit 6c4009
SECTION
Packit 6c4009
__ieee754_exp (double x)
Packit 6c4009
{
Packit 6c4009
  double bexp, t, eps, del, base, y, al, bet, res, rem, cor;
Packit 6c4009
  double z;
Packit 6c4009
  mynumber junk1, junk2, binexp = {{0, 0}};
Packit 6c4009
  int4 i, j, m, n, ex;
Packit 6c4009
  int4 k;
Packit 6c4009
  double retval;
Packit 6c4009
Packit 6c4009
  {
Packit 6c4009
    SET_RESTORE_ROUND (FE_TONEAREST);
Packit 6c4009
Packit 6c4009
    junk1.x = x;
Packit 6c4009
    m = junk1.i[HIGH_HALF];
Packit 6c4009
    n = m & hugeint;
Packit 6c4009
Packit 6c4009
    if (n < 0x3ff0a2b2)		/* |x| < 1.03972053527832 */
Packit 6c4009
      {
Packit 6c4009
	if (n < 0x3f862e42)	/* |x| < 3/2 ln 2 */
Packit 6c4009
	  {
Packit 6c4009
	    if (n < 0x3ed00000)	/* |x| < 1/64 ln 2 */
Packit 6c4009
	      {
Packit 6c4009
		if (n < 0x3e300000)	/* |x| < 2^18 */
Packit 6c4009
		  {
Packit 6c4009
		    retval = one + junk1.x;
Packit 6c4009
		    goto ret;
Packit 6c4009
		  }
Packit 6c4009
		retval = one + junk1.x * (one + half * junk1.x);
Packit 6c4009
		goto ret;
Packit 6c4009
	      }
Packit 6c4009
	    t = junk1.x * junk1.x;
Packit 6c4009
	    retval = junk1.x + (t * (half + junk1.x * t2) +
Packit 6c4009
				(t * t) * (t3 + junk1.x * t4 + t * t5));
Packit 6c4009
	    retval = one + retval;
Packit 6c4009
	    goto ret;
Packit 6c4009
	  }
Packit 6c4009
Packit 6c4009
	/* Find the multiple of 2^-6 nearest x.  */
Packit 6c4009
	k = n >> 20;
Packit 6c4009
	j = (0x00100000 | (n & 0x000fffff)) >> (0x40c - k);
Packit 6c4009
	j = (j - 1) & ~1;
Packit 6c4009
	if (m < 0)
Packit 6c4009
	  j += 134;
Packit 6c4009
	z = junk1.x - TBL2[j];
Packit 6c4009
	t = z * z;
Packit 6c4009
	retval = z + (t * (half + (z * t2))
Packit 6c4009
		      + (t * t) * (t3 + z * t4 + t * t5));
Packit 6c4009
	retval = TBL2[j + 1] + TBL2[j + 1] * retval;
Packit 6c4009
	goto ret;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    if (n < bigint)		/* && |x| >= 1.03972053527832 */
Packit 6c4009
      {
Packit 6c4009
	y = x * log2e.x + three51.x;
Packit 6c4009
	bexp = y - three51.x;	/*  multiply the result by 2**bexp        */
Packit 6c4009
Packit 6c4009
	junk1.x = y;
Packit 6c4009
Packit 6c4009
	eps = bexp * ln_two2.x;	/* x = bexp*ln(2) + t - eps               */
Packit 6c4009
	t = x - bexp * ln_two1.x;
Packit 6c4009
Packit 6c4009
	y = t + three33.x;
Packit 6c4009
	base = y - three33.x;	/* t rounded to a multiple of 2**-18      */
Packit 6c4009
	junk2.x = y;
Packit 6c4009
	del = (t - base) - eps;	/*  x = bexp*ln(2) + base + del           */
Packit 6c4009
	eps = del + del * del * (p3.x * del + p2.x);
Packit 6c4009
Packit 6c4009
	binexp.i[HIGH_HALF] = (junk1.i[LOW_HALF] + 1023) << 20;
Packit 6c4009
Packit 6c4009
	i = ((junk2.i[LOW_HALF] >> 8) & 0xfffffffe) + 356;
Packit 6c4009
	j = (junk2.i[LOW_HALF] & 511) << 1;
Packit 6c4009
Packit 6c4009
	al = coar.x[i] * fine.x[j];
Packit 6c4009
	bet = ((coar.x[i] * fine.x[j + 1] + coar.x[i + 1] * fine.x[j])
Packit 6c4009
	       + coar.x[i + 1] * fine.x[j + 1]);
Packit 6c4009
Packit 6c4009
	rem = (bet + bet * eps) + al * eps;
Packit 6c4009
	res = al + rem;
Packit 6c4009
	/* Maximum relative error is 7.8e-22 (70.1 bits).
Packit 6c4009
	   Maximum ULP error is 0.500007.  */
Packit 6c4009
	retval = res * binexp.x;
Packit 6c4009
	goto ret;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    if (n >= badint)
Packit 6c4009
      {
Packit 6c4009
	if (n > infint)
Packit 6c4009
	  {
Packit 6c4009
	    retval = x + x;
Packit 6c4009
	    goto ret;
Packit 6c4009
	  }			/* x is NaN */
Packit 6c4009
	if (n < infint)
Packit 6c4009
	  {
Packit 6c4009
	    if (x > 0)
Packit 6c4009
	      goto ret_huge;
Packit 6c4009
	    else
Packit 6c4009
	      goto ret_tiny;
Packit 6c4009
	  }
Packit 6c4009
	/* x is finite,  cause either overflow or underflow  */
Packit 6c4009
	if (junk1.i[LOW_HALF] != 0)
Packit 6c4009
	  {
Packit 6c4009
	    retval = x + x;
Packit 6c4009
	    goto ret;
Packit 6c4009
	  }			/*  x is NaN  */
Packit 6c4009
	retval = (x > 0) ? inf.x : zero;	/* |x| = inf;  return either inf or 0 */
Packit 6c4009
	goto ret;
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    y = x * log2e.x + three51.x;
Packit 6c4009
    bexp = y - three51.x;
Packit 6c4009
    junk1.x = y;
Packit 6c4009
    eps = bexp * ln_two2.x;
Packit 6c4009
    t = x - bexp * ln_two1.x;
Packit 6c4009
    y = t + three33.x;
Packit 6c4009
    base = y - three33.x;
Packit 6c4009
    junk2.x = y;
Packit 6c4009
    del = (t - base) - eps;
Packit 6c4009
    eps = del + del * del * (p3.x * del + p2.x);
Packit 6c4009
    i = ((junk2.i[LOW_HALF] >> 8) & 0xfffffffe) + 356;
Packit 6c4009
    j = (junk2.i[LOW_HALF] & 511) << 1;
Packit 6c4009
    al = coar.x[i] * fine.x[j];
Packit 6c4009
    bet = ((coar.x[i] * fine.x[j + 1] + coar.x[i + 1] * fine.x[j])
Packit 6c4009
	   + coar.x[i + 1] * fine.x[j + 1]);
Packit 6c4009
    rem = (bet + bet * eps) + al * eps;
Packit 6c4009
    res = al + rem;
Packit 6c4009
    cor = (al - res) + rem;
Packit 6c4009
    if (m >> 31)
Packit 6c4009
      {
Packit 6c4009
	ex = junk1.i[LOW_HALF];
Packit 6c4009
	if (res < 1.0)
Packit 6c4009
	  {
Packit 6c4009
	    res += res;
Packit 6c4009
	    cor += cor;
Packit 6c4009
	    ex -= 1;
Packit 6c4009
	  }
Packit 6c4009
	if (ex >= -1022)
Packit 6c4009
	  {
Packit 6c4009
	    binexp.i[HIGH_HALF] = (1023 + ex) << 20;
Packit 6c4009
	    /* Does not underflow: res >= 1.0, binexp >= 0x1p-1022
Packit 6c4009
	       Maximum relative error is 7.8e-22 (70.1 bits).
Packit 6c4009
	       Maximum ULP error is 0.500007.  */
Packit 6c4009
	    retval = res * binexp.x;
Packit 6c4009
	    goto ret;
Packit 6c4009
	  }
Packit 6c4009
	ex = -(1022 + ex);
Packit 6c4009
	binexp.i[HIGH_HALF] = (1023 - ex) << 20;
Packit 6c4009
	res *= binexp.x;
Packit 6c4009
	cor *= binexp.x;
Packit 6c4009
	t = 1.0 + res;
Packit 6c4009
	y = ((1.0 - t) + res) + cor;
Packit 6c4009
	res = t + y;
Packit 6c4009
	/* Maximum ULP error is 0.5000035.  */
Packit 6c4009
	binexp.i[HIGH_HALF] = 0x00100000;
Packit 6c4009
	retval = (res - 1.0) * binexp.x;
Packit 6c4009
	if (retval < DBL_MIN)
Packit 6c4009
	  {
Packit 6c4009
	    double force_underflow = tiny * tiny;
Packit 6c4009
	    math_force_eval (force_underflow);
Packit 6c4009
	  }
Packit 6c4009
	if (retval == 0)
Packit 6c4009
	  goto ret_tiny;
Packit 6c4009
	goto ret;
Packit 6c4009
      }
Packit 6c4009
    else
Packit 6c4009
      {
Packit 6c4009
	binexp.i[HIGH_HALF] = (junk1.i[LOW_HALF] + 767) << 20;
Packit 6c4009
	/* Maximum relative error is 7.8e-22 (70.1 bits).
Packit 6c4009
	   Maximum ULP error is 0.500007.  */
Packit 6c4009
	retval = res * binexp.x * t256.x;
Packit 6c4009
	if (isinf (retval))
Packit 6c4009
	  goto ret_huge;
Packit 6c4009
	else
Packit 6c4009
	  goto ret;
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
ret:
Packit 6c4009
  return retval;
Packit 6c4009
Packit 6c4009
 ret_huge:
Packit 6c4009
  return hhuge * hhuge;
Packit 6c4009
Packit 6c4009
 ret_tiny:
Packit 6c4009
  return tiny * tiny;
Packit 6c4009
}
Packit 6c4009
#ifndef __ieee754_exp
Packit 6c4009
strong_alias (__ieee754_exp, __exp_finite)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Compute e^(x+xx).  */
Packit 6c4009
double
Packit 6c4009
SECTION
Packit 6c4009
__exp1 (double x, double xx)
Packit 6c4009
{
Packit 6c4009
  double bexp, t, eps, del, base, y, al, bet, res, rem, cor;
Packit 6c4009
  mynumber junk1, junk2, binexp = {{0, 0}};
Packit 6c4009
  int4 i, j, m, n, ex;
Packit 6c4009
Packit 6c4009
  junk1.x = x;
Packit 6c4009
  m = junk1.i[HIGH_HALF];
Packit 6c4009
  n = m & hugeint;		/* no sign */
Packit 6c4009
Packit 6c4009
  /* fabs (x) > 5.551112e-17 and fabs (x) < 7.080010e+02.  */
Packit 6c4009
  if (n > smallint && n < bigint)
Packit 6c4009
    {
Packit 6c4009
      y = x * log2e.x + three51.x;
Packit 6c4009
      bexp = y - three51.x;	/*  multiply the result by 2**bexp        */
Packit 6c4009
Packit 6c4009
      junk1.x = y;
Packit 6c4009
Packit 6c4009
      eps = bexp * ln_two2.x;	/* x = bexp*ln(2) + t - eps               */
Packit 6c4009
      t = x - bexp * ln_two1.x;
Packit 6c4009
Packit 6c4009
      y = t + three33.x;
Packit 6c4009
      base = y - three33.x;	/* t rounded to a multiple of 2**-18      */
Packit 6c4009
      junk2.x = y;
Packit 6c4009
      del = (t - base) + (xx - eps);	/*  x = bexp*ln(2) + base + del      */
Packit 6c4009
      eps = del + del * del * (p3.x * del + p2.x);
Packit 6c4009
Packit 6c4009
      binexp.i[HIGH_HALF] = (junk1.i[LOW_HALF] + 1023) << 20;
Packit 6c4009
Packit 6c4009
      i = ((junk2.i[LOW_HALF] >> 8) & 0xfffffffe) + 356;
Packit 6c4009
      j = (junk2.i[LOW_HALF] & 511) << 1;
Packit 6c4009
Packit 6c4009
      al = coar.x[i] * fine.x[j];
Packit 6c4009
      bet = ((coar.x[i] * fine.x[j + 1] + coar.x[i + 1] * fine.x[j])
Packit 6c4009
	     + coar.x[i + 1] * fine.x[j + 1]);
Packit 6c4009
Packit 6c4009
      rem = (bet + bet * eps) + al * eps;
Packit 6c4009
      res = al + rem;
Packit 6c4009
      /* Maximum relative error before rounding is 8.8e-22 (69.9 bits).
Packit 6c4009
	 Maximum ULP error is 0.500008.  */
Packit 6c4009
      return res * binexp.x;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (n <= smallint)
Packit 6c4009
    return 1.0;			/*  if x->0 e^x=1 */
Packit 6c4009
Packit 6c4009
  if (n >= badint)
Packit 6c4009
    {
Packit 6c4009
      if (n > infint)
Packit 6c4009
	return (zero / zero);	/* x is NaN,  return invalid */
Packit 6c4009
      if (n < infint)
Packit 6c4009
	return ((x > 0) ? (hhuge * hhuge) : (tiny * tiny));
Packit 6c4009
      /* x is finite,  cause either overflow or underflow  */
Packit 6c4009
      if (junk1.i[LOW_HALF] != 0)
Packit 6c4009
	return (zero / zero);	/*  x is NaN  */
Packit 6c4009
      return ((x > 0) ? inf.x : zero);	/* |x| = inf;  return either inf or 0 */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  y = x * log2e.x + three51.x;
Packit 6c4009
  bexp = y - three51.x;
Packit 6c4009
  junk1.x = y;
Packit 6c4009
  eps = bexp * ln_two2.x;
Packit 6c4009
  t = x - bexp * ln_two1.x;
Packit 6c4009
  y = t + three33.x;
Packit 6c4009
  base = y - three33.x;
Packit 6c4009
  junk2.x = y;
Packit 6c4009
  del = (t - base) + (xx - eps);
Packit 6c4009
  eps = del + del * del * (p3.x * del + p2.x);
Packit 6c4009
  i = ((junk2.i[LOW_HALF] >> 8) & 0xfffffffe) + 356;
Packit 6c4009
  j = (junk2.i[LOW_HALF] & 511) << 1;
Packit 6c4009
  al = coar.x[i] * fine.x[j];
Packit 6c4009
  bet = ((coar.x[i] * fine.x[j + 1] + coar.x[i + 1] * fine.x[j])
Packit 6c4009
	 + coar.x[i + 1] * fine.x[j + 1]);
Packit 6c4009
  rem = (bet + bet * eps) + al * eps;
Packit 6c4009
  res = al + rem;
Packit 6c4009
  cor = (al - res) + rem;
Packit 6c4009
  if (m >> 31)
Packit 6c4009
    {
Packit 6c4009
      /* x < 0.  */
Packit 6c4009
      ex = junk1.i[LOW_HALF];
Packit 6c4009
      if (res < 1.0)
Packit 6c4009
	{
Packit 6c4009
	  res += res;
Packit 6c4009
	  cor += cor;
Packit 6c4009
	  ex -= 1;
Packit 6c4009
	}
Packit 6c4009
      if (ex >= -1022)
Packit 6c4009
	{
Packit 6c4009
	  binexp.i[HIGH_HALF] = (1023 + ex) << 20;
Packit 6c4009
	  /* Maximum ULP error is 0.500008.  */
Packit 6c4009
	  return res * binexp.x;
Packit 6c4009
	}
Packit 6c4009
      /* Denormal case - ex < -1022.  */
Packit 6c4009
      ex = -(1022 + ex);
Packit 6c4009
      binexp.i[HIGH_HALF] = (1023 - ex) << 20;
Packit 6c4009
      res *= binexp.x;
Packit 6c4009
      cor *= binexp.x;
Packit 6c4009
      t = 1.0 + res;
Packit 6c4009
      y = ((1.0 - t) + res) + cor;
Packit 6c4009
      res = t + y;
Packit 6c4009
      binexp.i[HIGH_HALF] = 0x00100000;
Packit 6c4009
      /* Maximum ULP error is 0.500004.  */
Packit 6c4009
      return (res - 1.0) * binexp.x;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      binexp.i[HIGH_HALF] = (junk1.i[LOW_HALF] + 767) << 20;
Packit 6c4009
      /* Maximum ULP error is 0.500008.  */
Packit 6c4009
      return res * binexp.x * t256.x;
Packit 6c4009
    }
Packit 6c4009
}