/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: t_ie10.c,v 1.2 1997/09/25 18:16:56 ae59 Exp $ */
/*
$Log: t_ie10.c,v $
 * Revision 1.2  1997/09/25  18:16:56  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:25:31  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : t_ie10.c                              */
/*                                                              */
/*      Interval exponential function 10**x                     */
/*                                                              */
/*      Date            : 1992-01-21                            */
/*      1992-01-21 : new routine                                */
/****************************************************************/
#ifdef AIX
#include "/u/p88c/runtime/tbyte/t_defs.h"
#else
#include "t_defs.h"
#endif
extern a_real *r_pinf;
extern a_real *r_zero;
extern a_real *r_one_;
extern a_real *r_eps_;
 
/*--------------------------------------------------------------*
 | exp10rd   exp10 with rounding mode                           |
 *--------------------------------------------------------------*/
#ifdef ANSI_C
#ifdef LINT_ARGS
static int exp10rd(const ExtReal *arg, ExtReal *res, int rnd)
#else
static int exp10rd(arg, res, rnd)
const ExtReal *arg;
      ExtReal *res;
      int rnd;
#endif /* LINT_ARGS */
#else  /* NOT ANSI_C */
#ifdef LINT_ARGS
static int exp10rd(ExtReal *arg, ExtReal *res, int rnd)
#else
static int exp10rd(arg, res, rnd)
ExtReal *arg;
ExtReal *res;
int rnd;
#endif /* LINT_ARGS */
#endif /* ANSI_C */
{
   ExtReal     r;          /* Ergebnis vor Rundung              */
   int         ret;        /* Rueckgabe                         */
   int         check;      /* Rueckgabe von Makro ArgCheck      */
 
   /* --- pruefe Argument, dann Pruefung aus --- */
   ArgCheck1(Exp, arg, res);
   /* arg_check = Off; */
 
   /* --- exp --- */
   ret = t_10ex(arg, &r);
 
   /* --- Abbruch bei Fehler --- */
   if(ret!=NoErr) {
      copyee(&r, res);
      arg_check = On;
      return (ret);
   }
 
   /* nicht runden, falls Argument Null ist */
   if (!cmpee(arg, &Zero)) ret |= LB_Exact;
 
   /* --- Rundungs-Fehler --- */
   if ( rnd == UP )
      {
      if(!(ret&LB_Exact)) round_rel(UP,  &r, &EpsExp, res);
      else copyee(&r, res);
      }
   else
      {
      if(!(ret&LB_Exact)) round_rel(DOWN,  &r, &EpsExp, res);
      else copyee(&r, res);
      }
   /* Verbesserung des Ergebnis' in Spezialfaellen */
#if INT_HPREC
   { int vz;
     vz = SGNE(arg);
     if (vz==POS&&-1==cmpee(res, &One)) /* exp10 x >= 1 */
        copyee(&One, res);
   }
#endif /* INT_HPREC */
 
   /* Unterlaufbehandlung */
   if(0==cmpee(res,&Zero))
      copyee(&LongRealDenormMin,res);
   if (rnd == UP)
      {
      /* --- ArgPruefung wieder an --- */
      arg_check = On;
      }
 
   /* --- kein Fehler mehr moeglich --- */
   return NoErr;
} /* exp10rd() */


#ifdef LINT_ARGS
a_intv t_ie10(a_intv ai)
#else
a_intv t_ie10(ai)
 
a_intv ai;
#endif
   {
   a_intv   res;
   int      i, rnd, rc;
   IExtReal  a, r;
   a_real half, maxx, min, c10, c23;

   E_SPUSH("t_ie10")
 
   ((a_btyp *)&min)[B_HPART] = 0xC090C800;   /* -1074   */
   ((a_btyp *)&min)[B_LPART] = 0;
   ((a_btyp *)&maxx)[B_HPART] = 0x40900000;   /*  1024   */
   ((a_btyp *)&maxx)[B_LPART] = 0;
   ((a_btyp *)&half)[B_HPART] = 0x3FE00000;  /* 0.5     */
   ((a_btyp *)&half)[B_LPART] = 0;
   ((a_btyp *)&c23)[B_HPART] = 0x40370000;   /*   23    */
   ((a_btyp *)&c23)[B_LPART] = 0;
   ((a_btyp *)&c10)[B_HPART] = 0x40240000;   /*   10    */
   ((a_btyp *)&c10)[B_LPART] = 0;
 
   if (r_ge(ai.INF, maxx) == TRUE)
      {
      e_trap(INV_ARG, 6, E_TMSG, 48,
             E_TDBL+E_TEXT(5), &ai.INF, E_TDBL+E_TEXT(6), &ai.SUP);
      R_ASSIGN(res.INF, *r_pinf);
      R_ASSIGN(res.SUP, *r_pinf);
      }
   else if (r_le(ai.INF, min) == TRUE)
      {
      R_ASSIGN(res.INF, *r_zero); /* underflow */
      }
   else if ( (r_sign(r_frac(ai.INF)) == 0) &&
            (r_ge(ai.INF,*r_zero) == TRUE) &&
            (r_lt(ai.INF, c23) == TRUE))
      {
      a_real val;
      R_ASSIGN(val,*r_one_);
      for (i=0; i < r_trun(ai.INF); i++)
         {
         R_ASSIGN(val, r_muld(val, c10));
         }
      R_ASSIGN(res.INF,val); /* exact result */
      }
   else
      {
      rnd = getrndmode();
      longreal_to_extreal((LongReal *)&ai.INF, &a.l);
 
      if ((rc = exp10rd(&a.l, &r.l, DOWN))!=0)
         ieee_aborti1(rc, &ai);
 
      setrndmode(DOWN);
      if ((rc = extreal_to_longreal(&r.l, (LongReal *)&res.INF))!=0)
        ieee_aborti1(rc, &ai);
      setrndmode(rnd);  /* restore rounding mode */
      }    /* at this point: lower bound of result has been computed */
 
   if (r_ge(ai.SUP, maxx) == TRUE)   /* consider upper bound */
      {
      e_trap(INV_ARG, 6, E_TMSG, 48,
             E_TDBL+E_TEXT(5), &ai.INF, E_TDBL+E_TEXT(6), &ai.SUP);
      R_ASSIGN(res.INF, *r_pinf);
      R_ASSIGN(res.SUP, *r_pinf);
      }
   else if (r_le(ai.SUP, min) == TRUE)
      {
      R_ASSIGN(res.SUP, *r_eps_); /* underflow */
      }
   else if ( (r_sign(r_frac(ai.SUP)) == 0)  &&
           (r_ge(ai.SUP,*r_zero) == TRUE) &&
           (r_lt(ai.SUP, c23) == TRUE) )
      {
      a_real val;
      R_ASSIGN(val,*r_one_);
      for (i=0; i < r_trun(ai.SUP); i++)
         {
         R_ASSIGN(val, r_muld(val, c10));
         }
      R_ASSIGN(res.SUP,val); /* exact result */
      }
   else
      {
      rnd = getrndmode();
      longreal_to_extreal((LongReal *)&ai.SUP, &a.u);
 
      if ((rc = exp10rd(&a.u, &r.u, UP))!=0)
         ieee_aborti1(rc, &ai);
 
      setrndmode(UP);
      if ((rc = extreal_to_longreal(&r.u, (LongReal *)&res.SUP))!=0)
         ieee_aborti1(rc, &ai);
      setrndmode(rnd);
      }  /* upper bound */
 
      E_SPOPP("t_ie10")
   return res;
   }
 
/* ------------------------------------------------------------ */
 
