/**************************************************************************/
/*                                                                        */
/*                                 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_adpp.c,v 1.2 1997/09/25 16:55:13 ae59 Exp $ */
/*
$Log: b_adpp.c,v $
 * Revision 1.2  1997/09/25  16:55:13  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:20:55  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_adpp.c                              */
/*                                                              */
/*      Entries         : a_btyp b_adpp                         */
/*                           (dc,size,expo,dp,length,           */
/*                            p_start,p_dp,p_length)            */
/*                        a_btyp **dc;                          */
/*                        a_intg *size;                         */
/*                        a_intg expo;                          */
/*                        a_intg dp;                            */
/*                        a_intg length;                        */
/*                        a_intg *p_start;                      */
/*                        a_intg *p_dp;                         */
/*                        a_intg *p_length;                     */
/*                                                              */
/*      Arguments       : dc     - decimal digits               */
/*                        size   - size of dc                   */
/*                        expo   - converted exponent           */
/*                        dp     - position of decimal point    */
/*                        length - number of decimal digits     */
/*                        p_start - start of packed mantissa    */
/*                        p_dp   - packed decimal point         */
/*                        p_length - length of packed decimals  */
/*                                                              */
/*      Function value  : error indicator                       */
/*                                                              */
/*      Description     : Adjust decimal point and pack decimal */
/*                        digits.                               */
/*                                                              */
/*      Date            : 1993-09-16                            */
/*      1993-09-16 : cast to char* in malloc             =b=    */
/*      1993-05-28 : 64 bit base type considered (cray)  =b=    */
/*      1992-07-22 : cast length to size_t in mem...     =b=    */
/*      1991-02-18 : 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 a_btyp b_adpp(a_btyp **dc,a_intg *size,a_intg expo,a_intg dp,
                    a_intg length,a_intg *p_start,
                    a_intg *p_dp,a_intg *p_length)
#else
local a_btyp b_adpp(dc,size,expo,dp,length,p_start,p_dp,p_length)

a_btyp **dc;
a_intg *size;
a_intg expo;
a_intg dp;
a_intg length;
a_intg *p_start;
a_intg *p_dp;
a_intg *p_length;
#endif
        {
        a_intg i,j,k,m;
        char *buffer,*ptr;

        E_TPUSH("b_adpp")

        *p_start = *p_dp = *p_length = 0;

        /* number is non-zero   */
        if (dp!=0 || length!=0)
           {
           buffer = (char *)(*dc);

           /* move decimal point left to first digit    */
           if (0<(i = -dp-expo))
              {
              if (i+length>*size)
                 {
                 if ((ptr = (char*) malloc((size_t) (i+length)))==NULL)
                    return(ALLOCATION);

                 memcpy(ptr,buffer,(size_t)length);

                 /* if *size==BUFFERSIZE then static object assumed */
                 if (*size!=BUFFERSIZE)
                    {
#ifdef HEAP_CHECK
b_freh((a_char *)dc,(a_char *)buffer,(a_char *)"b_adpp");
#endif
                    free(buffer);
                    }
#ifdef HEAP_CHECK
b_geth((a_char *)dc,(a_char *)ptr,(a_char *)"b_adpp");
#endif
                 buffer = ptr;
                 *dc = (a_btyp *)ptr;
                 *size = i+length;
                 }
              for (k=length-1;k>=0;k--) buffer[k+i] = buffer[k];
              for (k=0;k<i;k++) buffer[k] = 0x00;
              length += i;
              dp = 0;
              }

           /* move decimal point right to last digit    */
           else if (-i>length)
              {
              if (-i>*size)
                 {
                 if ((ptr = (char*) malloc((size_t) -i))==NULL)
                    return(ALLOCATION);

                 memcpy(ptr,buffer,(size_t) *size);

                 /* if *size==BUFFERSIZE then static object assumed */
                 if (*size!=BUFFERSIZE)
                    {
#ifdef HEAP_CHECK
b_freh((a_char *)dc,(a_char *)buffer,(a_char *)"b_adpp");
#endif
                    free(buffer);
                    }
#ifdef HEAP_CHECK
b_geth((a_char *)dc,(a_char *)ptr,(a_char *)"b_adpp");
#endif
                 buffer = ptr;
                 *dc = (a_btyp *)ptr;
                 *size = -i;
                 }
              while (length<-i) buffer[length++] = 0x00;
              dp = length;
              }

           /* move decimal point within mantissa digits */
           else
              dp += expo;

           /* p_length = maximum number of a_btyp for packed number */
#if CRAY_UNIX_C
           k = (dp) ? 2*dp/sizeof(a_btyp)+2 : 1;
           k += ((length!=dp) ? 2*(length-dp)/sizeof(a_btyp)+2 : 1);
#else
           k = (dp) ? dp/sizeof(a_btyp)+2 : 1;
           k += ((length!=dp) ? (length-dp)/sizeof(a_btyp)+2 : 1);
#endif

           /* check for sufficient buffer space */
           if (k*sizeof(a_btyp)>*size)
              {
              if ((ptr = (char*) malloc((size_t) k*sizeof(a_btyp)))==NULL)
                 return(ALLOCATION);

              memcpy(ptr,buffer,(size_t) length);

              /* if *size==BUFFERSIZE then static object assumed */
              if (*size!=BUFFERSIZE)
                 {
#ifdef HEAP_CHECK
b_freh((a_char *)dc,(a_char *)buffer,(a_char *)"b_adpp");
#endif
                 free(buffer);
                 }
#ifdef HEAP_CHECK
b_geth((a_char *)dc,(a_char *)ptr,(a_char *)"b_adpp");
#endif
              buffer = ptr;
              *dc = (a_btyp *)ptr;
              *size = k*sizeof(a_btyp);
              }
           *p_length = k;

           /* pack digits of fraction part */
           if ((i = j = (length-dp) % DEC_PACK)!=0)
              {
              (*dc)[--k] = buffer[length-j];
              while (--i>0)
                 (*dc)[k] = (*dc)[k]*10+buffer[length-i];
              i = j;
              while (i++<DEC_PACK) (*dc)[k] *= 10;
              }
           for (i=length-j;i>dp;i -= DEC_PACK)
              {
              (*dc)[--k] = buffer[i-DEC_PACK];
              for (m=DEC_PACK-1;m>0;m--)
                 (*dc)[k] = (*dc)[k]*10+buffer[i-m];
              }

           /* position of packed decimal point */
           *p_dp = k;

           /* pack digits of integer part */
           j = dp % DEC_PACK;
           for (i=dp;i>j;i -= DEC_PACK)
              {
              (*dc)[--k] = buffer[i-DEC_PACK];
              for (m=DEC_PACK-1;m>0;m--)
                 (*dc)[k] = (*dc)[k]*10+buffer[i-m];
              }
           if (j)
              {
              (*dc)[--k] = buffer[0];
              for (i=1;i<j;i++)
                 (*dc)[k] = (*dc)[k]*10+buffer[i];
              }

           *p_start = k;
           }

        E_TPOPP("b_adpp")
        return(NO_ERROR);
        }
