/**************************************************************************/
/*                                                                        */
/*                                 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_ifrm.c,v 1.2 1997/09/25 17:03:04 ae59 Exp $ */
/*
$Log: b_ifrm.c,v $
 * Revision 1.2  1997/09/25  17:03:04  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:26  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_ifrm.c                              */
/*                                                              */
/*      Entries         : a_btyp b_ifrm                         */
/*                           (dc,expo,dp,length,sign,s)         */
/*                        a_btyp *dc;                           */
/*                        a_intg expo,dp,length;                */
/*                        a_bool sign;                          */
/*                        a_intv *s;                            */
/*                                                              */
/*      Arguments       : dc - decimal digits                   */
/*                        expo - converted exponent             */
/*                        dp - position of decimal point        */
/*                        length - number of dc                 */
/*                        sign - sign of number                 */
/*                        s - resultant interval                */
/*                                                              */
/*      Function value  : error indicator                       */
/*                                                              */
/*      Description     : Convert a character string of a real  */
/*                        number to the smallest enclosing      */
/*                        interval.                             */
/*                                                              */
/*      Date            : 1991-04-24                            */
/*      1991-04-24 : test for integer and fraction corrected    */
/*      1991-04-22 : real assignment replaced by R_ASSIGN()     */
/*      1991-02-27 : suspicious pointer conversion removed      */
/*      1991-02-19 : use b_adpp()                               */
/*      1991-02-18 : new arguments in b_conf(),b_coni()         */
/*      1990-12-11 : assignment to interval                     */
/*      1990-11-12 : E_TPUSH+E_TPOPP                            */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
extern dotprecision b_cm__;
extern a_real *r_sero;
extern a_real *r_zero;
#endif

#ifdef LINT_ARGS
local a_btyp b_ifrm(a_btyp *dc,a_intg expo,a_intg dp,a_intg length,
                      a_bool sign,a_intv *s)
#else
local a_btyp b_ifrm(dc,expo,dp,length,sign,s)

a_btyp *dc;
a_intg expo;
a_intg dp;
a_intg length;
a_bool sign;
a_intv *s;
#endif
        {
        a_intg k;
        a_intg bits,size;
        a_btyp rc,rc1,mant[BSIZE],mantb[BSIZE];
        a_intg expob,p_dp,p_length;
        char *buffer;

        E_TPUSH("b_ifrm")

        /* number is zero       */
        if (dp==0 && length==0) {
           if (sign)
              {
              R_ASSIGN(s->INF,*r_sero);
              R_ASSIGN(s->SUP,*r_sero);
              }
           else
              {
              R_ASSIGN(s->INF,*r_zero);
              R_ASSIGN(s->SUP,*r_zero);
              }

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

        /* initialize data      */
        B_CLEAR(b_cm__,A_LENGTH)
        B_CLEAR(mant,BSIZE)
        b_cm__[A_SIGN] = (sign) ? LSB : ZERO;
        bits = MANTL+2;
        buffer = (char *)dc;
        size = BUFFERSIZE;

        /* adjust and pack decimal digits */
        /* k = index of start of packed mantissa */
        if (b_adpp((a_btyp **)&buffer,&size,
                   expo,dp,length,&k,&p_dp,&p_length))
           {
           E_TPOPP("b_form")
           return(ALLOCATION);
           }

        /* convert integer part         */
        if (p_dp>k) b_coni(p_dp-k,&dc[k],
                       (a_intg *)&b_cm__[A_BEGIN],
                       (a_intg *)&b_cm__[A_END],b_cm__,&bits);

        /* convert fraction part        */
        if (p_length>p_dp)
           {
           if (bits>0) b_conf(p_length-p_dp,&dc[p_dp],
                              (a_intg *)&b_cm__[A_BEGIN],
                              (a_intg *)&b_cm__[A_END],
                              b_cm__,&bits);
           else b_cm__[b_cm__[A_END]] |= LSB;
           }

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

        /* eliminate leading zeros      */
        while (b_cm__[b_cm__[A_BEGIN]]==ZERO) b_cm__[A_BEGIN]++;

        /* get accu     */
        if (b_geta(b_cm__,mant,&expo,&sign))
           {
           if (sign)
              {
              R_ASSIGN(s->INF,*r_sero);
              R_ASSIGN(s->SUP,*r_sero);
              }
           else
              {
              R_ASSIGN(s->INF,*r_zero);
              R_ASSIGN(s->SUP,*r_zero);
              }
           rc = NO_ERROR;
           }
        else
           {

           /* adjust denormalized number   */
           rc = b_adj(mant,&expo);

           /* round        */
           expob = expo;
           memcpy(mantb,mant,BSIZE*sizeof(a_btyp));
           rc1 = b_rndd(mant,&expo,sign);
           if (rc==NO_ERROR) rc = rc1;
           rc1 = b_rndu(mantb,&expob,sign);
           if (rc==NO_ERROR) rc = rc1;
           if (rc==INEXACT) rc = NO_ERROR;

           /* composition  */
           b_comp(&(s->INF),expo,mant,sign);
           b_comp(&(s->SUP),expob,mantb,sign);
           }

        E_TPOPP("b_ifrm")
        return(rc);
        }
