/**************************************************************************/
/*                                                                        */
/*                                 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_outf.c,v 1.2 1997/09/25 17:04:55 ae59 Exp $ */
/*
$Log: b_outf.c,v $
 * Revision 1.2  1997/09/25  17:04:55  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:34  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_outf.c                              */
/*                                                              */
/*      Entries         : void b_outf(digits,buffer,bdp,dexpo,c)*/
/*                        char *buffer;                         */
/*                        dotprecision c;                       */
/*                        a_intg *digits,*bdp,*dexpo;           */
/*                                                              */
/*      Arguments       : digits  - number of required digits   */
/*                        buffer  - output buffer               */
/*                        bdp - position of decimal point       */
/*                        dexpo   - exponent of first non-zero  */
/*                                  digit                       */
/*                        c       - accu holding IEEE value     */
/*                                                              */
/*      Description     : Convert fractional part of an IEEE    */
/*                        value to a character string           */
/*                                                              */
/*      Date            : 1992-07-22                            */
/*      1992-07-22 : cast to char in storage of digits =b=      */
/*      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_outf(a_intg *digits,char *buffer,a_intg *bdp,a_intg *dexpo,
                  dotprecision c)
#else
local void b_outf(digits,buffer,bdp,dexpo,c)

a_intg *digits;
char *buffer;
a_intg *bdp;
a_intg *dexpo;
dotprecision c;
#endif
        {
        a_intg i,carry,cont,dg;
        a_btyp *p,*s,h,hh;
        char *q;

        /* initialize                                   */
        dg = *digits;
        cont = *dexpo;
        q = buffer+(*bdp+1);
        s = &c[c[A_END]];
        while (s>&c[A_D_P] && *s==ZERO) s--;

        /* convert by repeated multiplication           */
        while (dg>0 && s>&c[A_D_P])
           {
           carry = 0;
           for (p=s;p>&c[A_D_P];p--)
                {
                hh = GETLOW(*p)*B2D_POWER+carry;
                h = GETHIGH(*p)*B2D_POWER+GETHIGH(hh);
                carry = GETHIGH(h);
                *p = MOVEHIGH(h) | GETLOW(hh);
                }

           /* store decimal digits                              */
           for (i=B2D_LOG10-1;i>0;i--) {
               q[i] = (char) (carry%10+'0');
               carry /= 10;
               }
           *q = (char) (carry+'0');

           /* adjust dexpo and digits                           */
           for (i=0;i<B2D_LOG10;i++) {
               if (cont<0) {
                   if (*q!='0') {
                       cont = 0;
                       *dexpo = (buffer+*bdp)-q;
                       dg--;
                       }
                   }
               else dg--;
               q++;
               }

           if (*s==ZERO) s--;
           }

        /* pad with zeros if fraction part is zero remainder */
        if (dg>0) {
           while (dg--) {
              *q++ = '0';
              }
           }

        /* set least significant digit to 1 if non-zero remainder */
        else {
           if (s!=&c[A_D_P]) q[dg-1] = '1';                 /*(1)*/
           }

   return;
   }
