/**************************************************************************/
/*                                                                        */
/*                                 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_trnd.c,v 1.2 1997/09/25 17:09:50 ae59 Exp $ */
/*
$Log: b_trnd.c,v $
 * Revision 1.2  1997/09/25  17:09:50  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:51  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_trnd.c                              */
/*                                                              */
/*      Entries         : void b_trnd(mant,expo,vz)             */
/*                        a_btyp *mant;                         */
/*                        a_intg *expo;                         */
/*                        a_bool  vz;                           */
/*                                                              */
/*      Arguments       : mant = mantissa to be rounded         */
/*                        expo = exponent                       */
/*                        vz = sign                             */
/*                                                              */
/*      Description     : Round mantissa according to           */
/*                        flag b_rflg.                          */
/*                                                              */
/*      Date            : 1991-06-20                            */
/*      1991-06-20 : new routine                                */
/****************************************************************/

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

#ifdef LINT_ARGS
local void b_trnd(a_btyp *mant,a_intg *expo,a_bool  vz)
#else
local void b_trnd(mant,expo,vz)

a_btyp *mant;
a_intg *expo;
a_bool  vz;
#endif
        {
        a_intg i;

        E_TPUSH("b_trnd")

        if (b_rflg==NEAREST)
           {

           /* msb of mant[2] is set                     */
           if (mant[2] & MSB)
              {
              e_sieo();

              /* test trailing mantissa to be zero      */
              if ((mant[2] & NOT_MSB)==ZERO)
                 {
                 for (i=3;i<BSIZE;i++) if (mant[i]) break;

                 /* return if lsb of mant[1] is zero    */
                 if (i>=BSIZE)
                 if (!(mant[1] & LSB))
                    {
                    E_TPOPP("b_trnd")
                    return;
                    }
                 }
              }
           else
              {
              for (i=2;i<BSIZE;i++)
                 if (mant[i])
                    {
                    e_sieo();
                    break;
                    }

              E_TPOPP("b_trnd")
              return;
              }
           }
        else if (b_rflg==CHOP)
           {
           for (i=2;i<BSIZE;i++) if (mant[i]) break;
           if (i==BSIZE)
              {
              E_TPOPP("b_trnd")
              return;
              }
           e_sieo();

           E_TPOPP("b_trnd")
           return;
           }
        else
           {
           for (i=2;i<BSIZE;i++) if (mant[i]) break;
           if (i==BSIZE)
              {
              E_TPOPP("b_trnd")
              return;
              }
           e_sieo();

           /* add correction if trailing mantissa is non-zero   */
           if (!((b_rflg==DOWN && vz==TRUE) || (b_rflg==UP && vz==FALSE)))
              {
              E_TPOPP("b_trnd")
              return;
              }
           }

        /* propagate carry                              */
        mant[1]++;
        if (mant[1]==ZERO)
           {
           mant[0]++;

           /* normalization (only msb is set in mantissa)       */
           if (mant[0]==ZERO)
              {
              mant[0] = MSB;
              (*expo)++;

              /* overflow occurred                      */
              if (*expo>tEXPO_MAX)
                 {
                 if (e_of_e()) *expo -= tEXPO_ADJUST;
                 }
              }
           }

        E_TPOPP("b_trnd")
        return;
        }
