/**************************************************************************/
/*                                                                        */
/*                                 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_tadj.c,v 1.2 1997/09/25 17:08:29 ae59 Exp $ */
/*
$Log: b_tadj.c,v $
 * Revision 1.2  1997/09/25  17:08:29  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:46  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_tadj.c                              */
/*                                                              */
/*      Entries         : void b_tadj(mant,expo)                */
/*                        a_btyp mant[BSIZE];                   */
/*                        a_intg *expo;                         */
/*                                                              */
/*      Arguments       : mant = mantossa of tenbyte value      */
/*                        expo = exponent of tenbyte value      */
/*                                                              */
/*      Description     : Adjust denormalized tenbyte,          */
/*                        overflow and underflow.               */
/*                                                              */
/*      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
#endif

#ifdef LINT_ARGS
local void b_tadj(a_btyp *mant,a_intg *expo)
#else
local void b_tadj(mant,expo)

a_btyp mant[BSIZE];
a_intg *expo;
#endif
        {
        a_intg i;
        a_btyp rc;

        E_TPUSH("b_tadj")

        if (*expo>tEXPO_MAX) {

           /* if overflow trap enabled                          */
           if (e_of_e()) {
              *expo -= tEXPO_ADJUST;

              E_TPOPP("b_tadj")
              return;
              }
           else    {

              /* set all sgnificant mantissa bits               */
              mant[0] = MAX_BASETYPE;
              mant[1] = MAX_BASETYPE;
              mant[2] = MSB;

              *expo = tEXPO_MAX;

              e_sieo();

              E_TPOPP("b_tadj")
              return;
              }
           }
        else if (*expo<tEXPO_MIN) {

           /* if underflow trap enabled                         */
           if (e_uf_e()) {
              *expo += tEXPO_ADJUST;

              E_TPOPP("b_tadj")
              return;
              }
           else    {
              rc = NO_ERROR;
              for (i=2; i<BSIZE; i++ )
              if (mant[i]) {
                    e_sufo();
                    rc = UNDERFLOW;
                    break;
                    }

              /* denormalized number will result or zero        */
              if (*expo<tEXPO_MIN-tMANTL)
                 b_shru( mant, BSIZE, tMANTL+1 );
              else
                 b_shru( mant, BSIZE, tEXPO_MIN-*expo );

              /* exponent set to denormalized exponent          */
              *expo = tEXPO_MIN-1;

              if (rc==NO_ERROR)
              for ( i=2; i<BSIZE; i++ )
              if (mant[i]) {
                       e_sufo();
                       break;
                       }
              }
           }

        E_TPOPP("b_tadj")
        return;
        }
