/**************************************************************************/
/*                                                                        */
/*                                 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: b_ltod.c,v 1.2 1997/09/25 17:04:12 ae59 Exp $ */
/*
$Log: b_ltod.c,v $
 * Revision 1.2  1997/09/25  17:04:12  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:31  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_ltod.c                              */
/*                                                              */
/*      Entries         : a_btyp b_ltod(i,d,rnd)                */
/*                        multiprecision i;                     */
/*                        d_otpr *d;                            */
/*                        a_intg rnd;                           */
/*                                                              */
/*      Arguments       : i    = long value                     */
/*                        d    = dotprecision variable          */
/*                        rnd  = rounding                       */
/*                               <0 : downwards                 */
/*                               =0 : nearest                   */
/*                               >0 : upwards                   */
/*                                                              */
/*      Function value  : error code                            */
/*                                                              */
/*      Description     : convert long to dotprecision.         */
/*                                                              */
/*      Date            : 1991-12-03                            */
/*      1991-12-03 : new routine                                */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
extern a_btyp b_maxl;
#endif

#ifdef LINT_ARGS
local a_btyp b_ltod(multiprecision i,d_otpr *d,a_intg rnd)
#else
local a_btyp b_ltod(i,d,rnd)

multiprecision i;
d_otpr *d;
a_intg rnd;
#endif
        {
        register a_btyp *rega,k;

        E_TPUSH("b_ltod")

        rega = *d;
        B_CLEAR(rega,A_LENGTH) 

        /* number is zero */
        if (i->z)
           {
           E_TPOPP("b_ltod")
           return(NO_ERROR);
           }

        /* sign and exponent of real value */
        rega[A_SIGN] = i->s;
        rega[A_BEGIN] = A_D_P-i->e;
        rega[A_END] = A_D_P-i->e+i->l-1;
        rega[A_STATUS] |= A_MZERO | A_PZERO;
 
        /* overflow occurred */
        if (rega[A_BEGIN]<A_START)
           {
           e_trap(OVERFLOW,2,E_TMSG,48); 
           rega[A_STATUS] |= (i->s) ? A_MINFINITY : A_PINFINITY;

           E_TPOPP("b_ltod")
           return(OVERFLOW);
           }
        /* underflow occurred */
        else if (rega[A_BEGIN]>=A_LENGTH)
           {
           e_trap(UNDERFLOW,0);
           if ((i->s && rnd<0) || (!i->s && rnd>0))
              {
              rega[A_BEGIN] = rega[A_END] = A_LENGTH-1;
              rega[A_LENGTH-1] = LSB;
              }
           else
              {
              rega[A_BEGIN] = rega[A_END] = ZERO;
              }
           }
        /* copy mantissa of multiprecision value */
        else
           {
           /* chop length of mantissa */
           if (rega[A_END]>=A_LENGTH)
              {
              /* rnd holds rounding information */
              rnd = ((i->s && rnd<0) || (!i->s && rnd>0));
              rega[A_END] = A_LENGTH-1;
              }
           else
              rnd = 0;
       
           for (k=rega[A_BEGIN];k<=rega[A_END];k++) 
              rega[k] = i->m[k-rega[A_BEGIN]]; 

           /* propagate rounding carry */
           if (rnd)
              {
              if (b_bcad(rega[A_END]-rega[A_BEGIN]+1,
                         &rega[rega[A_END]]))
                 {
                 rega[A_END] = ++rega[A_BEGIN];
                 /* overflow is not considered */
                 }
              }

           /* eliminate trailing zeros */
           while (rega[rega[A_END]]==ZERO) rega[A_END]--;
           }

        E_TPOPP("b_ltod")
        return(NO_ERROR);
        }
