/**************************************************************************/
/*                                                                        */
/*                                 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_outi.c,v 1.2 1997/09/25 17:05:02 ae59 Exp $ */
/*
$Log: b_outi.c,v $
 * Revision 1.2  1997/09/25  17:05:02  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:34  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_outi.c                              */
/*                                                              */
/*      Entries         : void b_outi(digits,buffer,bdp,dexpo,c)*/
/*                        char *buffer;                         */
/*                        a_intg *digits,*bdp,*dexpo;           */
/*                        dotprecision c;                       */
/*                                                              */
/*      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 integer part of IEEE value    */
/*                        to a character string                 */
/*                                                              */
/*      Date            : 1992-07-22                            */
/*      1992-07-22 : cast to char in storage of digits =b=      */
/*      1991-04-09 : avoid loop without body                    */
/*      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_outi(a_intg *digits,char *buffer,a_intg *bdp,a_intg *dexpo,
                  dotprecision c)
#else
local void b_outi(digits,buffer,bdp,dexpo,c)

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

        /* initialize                                   */
        q = buffer+*bdp;
        s = &c[c[A_BEGIN]];

        /* convert by repeated division                 */
        while (s<=&c[A_D_P]) {
           mod = 0;
           for (p=s;p<=&c[A_D_P];p++)
               {
               h = GETHIGH(*p) | MOVEHIGH(mod);
               hh = GETLOW(*p) | MOVEHIGH(h%B2D_POWER);
               mod = hh%B2D_POWER;
               *p = MOVEHIGH(h/B2D_POWER) | (hh/B2D_POWER);
               }

           /* store decimal digits                              */
           for (i=0;i<B2D_LOG10-1;i++) {
               *q-- = (char) (mod%10+'0');
               mod /= 10;
               }
           *q-- = (char) (mod+'0');

           /* reduce length of binary mantissa                  */
           if (*s==ZERO) s++;
           }

        /* ignore leading zeros */
        while (*(++q)=='0') { /* empty */ }

        /* decimal exponent of first non-zero digit     */
        *dexpo = (buffer+*bdp)-q;

        if (*digits>*dexpo+1) {
           *digits -= (*dexpo+1);
           }
        else
           {                                                 /*(1)*/
           *digits = 0;
           if (!b_test((a_intg)c[A_END]-A_D_P,c+(A_D_P+1))) {/*(1)*/
                 buffer[*bdp] = '1';                         /*(1)*/
                 }                                           /*(1)*/
           }                                                 /*(1)*/

        return;
        }
