/**************************************************************************/
/*                                                                        */
/*                                 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_rnd.c,v 1.2 1997/09/25 17:05:58 ae59 Exp $ */
/*
$Log: b_rnd.c,v $
 * Revision 1.2  1997/09/25  17:05:58  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:38  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_rnd.c                               */
/*                                                              */
/*      Entries         : void b_rnd                            */
/*                         (rnd,buffer,digits,pos,bdp,dexpo)    */
/*                        a_intg *dexpo,rnd,digits,pos,*bdp;    */
/*                        char *buffer;                         */
/*                                                              */
/*      Arguments       : rnd - rounding mode for unsigned      */
/*                        buffer - mantissa                     */
/*                        digits - number of mantissa digits    */
/*                        pos - position where to round         */
/*                        bdp - position of decimal point       */
/*                        dexpo - exponent of result            */
/*                                                              */
/*      Description     : Round decimal representation of number*/
/*                                                              */
/*      Date            : 1991-01-28                            */
/*      1991-01-28 : round to even digit in case of a tie       */
/*      1990-07-16 : LINT_ARGS prototype                        */
/****************************************************************/

#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_rnd(a_intg rnd,char *buffer,a_intg digits,a_intg pos,
                 a_intg *bdp,a_intg *dexpo)
#else
local void b_rnd(rnd,buffer,digits,pos,bdp,dexpo)

a_intg rnd;
char *buffer;
a_intg digits;
a_intg pos;
a_intg *bdp;
a_intg *dexpo;
#endif
        {
        char *s,*q;

        s = buffer+(*bdp-*dexpo);

        if (rnd>0) {
                for (q=s+pos;q<s+digits;q++)
                   if (*q!='0') break;
                if (q==s+digits) return;
                }
        else if (rnd==0) {
                if (s[pos]<'5') return;
                if (s[pos]=='5')
                   {
                   for (q=s+pos+1;q<s+digits;q++)
                      if (*q!='0') break;
                   if (q>=s+digits && (s[pos-1] & 1)==0) return;
                   }
                }
        else return;

        /* advance to next decimal number                       */
        for (q=s+pos-1;q>=s;q--)
                if (*q=='9') *q = '0';
                else {
                    (*q)++;
                    break;
                    }

        /* carry occurs                                         */
        if (q<s) {
                (*dexpo)++;
                *q = '1';
                }

        return;
        }
