/**************************************************************************/
/*                                                                        */
/*                                 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: r_rond.c,v 1.2 1997/09/25 17:49:50 ae59 Exp $ */
/*
$Log: r_rond.c,v $
 * Revision 1.2  1997/09/25  17:49:50  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:24:10  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : r_rond.c                              */
/*                                                              */
/*      Entries         : a_intg r_rond(a)                      */
/*                        a_real a;                             */
/*                                                              */
/*      Arguments       : a = IEEE value                        */
/*                                                              */
/*      Description     : Round real value to next integer      */
/*                        number.                               */
/*                                                              */
/*      Date            : 1992-11-23                            */
/*      1992-11-23 : add E_ieee at e_trap(INV_op ... =b=        */
/*      1991-09-06 : reduce maximum line length                 */
/*      1991-05-20 : detect OVERFLOW                            */
/*      1991-04-23 : MANT_INFINITY() used                       */
/*      1991-02-27 : e_trap() arguments changed for E_TMSG 13/14*/
/*      1990-11-20 : E_TPUSH+E_TPOPP                            */
/*      1990-11-12 : INV_OP                                     */
/****************************************************************/

#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 a_intg r_rond(a_real a)
#else
local a_intg r_rond(a)

a_real a;
#endif
        {
        a_intg expoa;
        a_intg res = 0;
        a_btyp manta[BSIZE];
        a_bool vza;

        E_TPUSH("r_rond")

        B_CLEAR(manta, BSIZE);

        if (b_deko(a,&expoa,manta,&vza))
           {
           }
        else if (expoa<-1)
           {
           }
        else
           {
           if (expoa>EXPO_MAX)
              {
              if (MANT_INFINITY(manta))
                 e_trap(INV_ARG,4,E_TMSG,13,E_TDBL+E_TEXT(7),&a);
              else if (SIGNALING(manta[0]))
                 e_trap(INV_OP+E_IEEE,4,E_TMSG,5,E_TDBL+E_TEXT(7),&a);
              else
                 e_trap(INV_ARG,4,E_TMSG,14,E_TDBL+E_TEXT(7),&a);
              }
           else
              {

              /* shift to 1 position before decimal point */
              b_shru(manta,BSIZE,(MANTL-1-1)-expoa);

              /* perform rounding by incrementing */
              b_addc(manta+(D_U_RATIO-1));

              /* shift mantissa to decimal point */
              manta[1] =
                 (manta[1]>>1) | (manta[0]<<(B_LENGTH-1));
              manta[0] >>= 1;

              if (expoa>=I_LENGTH-1)
                 {
                 if (vza && expoa==I_LENGTH-1 &&
                     manta[D_U_RATIO-1]<MSB)
                    e_trap(OVERFLOW,2,E_TDBL+E_TEXT(7),&a);
                 else
                    res = -manta[D_U_RATIO-1];
                 }
              else if (vza)
                 res = -manta[D_U_RATIO-1];
              else
                 res = manta[D_U_RATIO-1];
              }
           }

        E_TPOPP("r_rond")
        return(res);
        }
