/**************************************************************************/
/*                                                                        */
/*                                 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_dtol.c,v 1.2 1997/09/25 17:01:59 ae59 Exp $ */
/*
$Log: b_dtol.c,v $
 * Revision 1.2  1997/09/25  17:01:59  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:21  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_dtol.c                              */
/*                                                              */
/*      Entries         : a_btyp b_dtol(d,i,rnd)                */
/*                        d_otpr d;                             */
/*                        multiprecision *i;                    */
/*                        a_intg rnd;                           */
/*                                                              */
/*      Arguments       : i    = long variable                  */
/*                        d    = dotprecision value             */
/*                        rnd  = rounding mode                  */
/*                                                              */
/*      Function value  : ALLOCATION - allocation error         */
/*                                                              */
/*      Description     : convert dotprecision to long.         */
/*                                                              */
/*      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_dtol(d_otpr d,multiprecision *i,a_intg rnd)
#else
local a_btyp b_dtol(d,i,rnd)

d_otpr d;
multiprecision *i;
a_intg rnd;
#endif
        {
        a_btyp j,l;

        E_TPUSH("b_dtol")

        (*i)->r = 0;
        (*i)->f = 0;

        if (d[A_BEGIN]==ZERO)
           {
           (*i)->z = TRUE;
           }
        else
           {
           (*i)->z = FALSE;
           (*i)->s = (d[A_SIGN]) ? TRUE : FALSE;

           /* required length of long value mantissa = l+1         */
           if ((l = d[A_END]-d[A_BEGIN])>=b_maxl) 
              {
              l = b_maxl-1;
              (*i)->r = 1;

              /* remove trailing zeros */
              while (d[d[A_BEGIN]+l]==ZERO) l--;
              }

           /* allocate and copy mantissa */
           if (l+1!=(*i)->l)
              {
              if ((*i)->l)
                 {
                 (*i)->l = 0;
#ifdef HEAP_CHECK
b_freh((a_char *)&(*i)->m,(a_char *)(*i)->m,(a_char *)"b_dtol");
#endif
                 B_FREE((*i)->m)
                 }
              if (b_ball(l+1,&(*i)->m)) return(ALLOCATION);
              (*i)->l = l+1;
              }
           for (j=0;j<=l;j++) (*i)->m[j] = d[d[A_BEGIN]+j];

           (*i)->e = A_D_P-d[A_BEGIN];
           }

        return(NO_ERROR);
        }
