OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gdb-7.2/] [libdecnumber/] [dpd/] [decimal64.c] - Diff between revs 835 and 841

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 835 Rev 841
/* Decimal 64-bit format module for the decNumber C Library.
/* Decimal 64-bit format module for the decNumber C Library.
   Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.
   Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.
   Contributed by IBM Corporation.  Author Mike Cowlishaw.
   Contributed by IBM Corporation.  Author Mike Cowlishaw.
 
 
   This file is part of GCC.
   This file is part of GCC.
 
 
   GCC is free software; you can redistribute it and/or modify it under
   GCC is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3, or (at your option) any later
   Software Foundation; either version 3, or (at your option) any later
   version.
   version.
 
 
   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.
   for more details.
 
 
Under Section 7 of GPL version 3, you are granted additional
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
3.1, as published by the Free Software Foundation.
 
 
You should have received a copy of the GNU General Public License and
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */
<http://www.gnu.org/licenses/>.  */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* Decimal 64-bit format module                                       */
/* Decimal 64-bit format module                                       */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* This module comprises the routines for decimal64 format numbers.   */
/* This module comprises the routines for decimal64 format numbers.   */
/* Conversions are supplied to and from decNumber and String.         */
/* Conversions are supplied to and from decNumber and String.         */
/*                                                                    */
/*                                                                    */
/* This is used when decNumber provides operations, either for all    */
/* This is used when decNumber provides operations, either for all    */
/* operations or as a proxy between decNumber and decSingle.          */
/* operations or as a proxy between decNumber and decSingle.          */
/*                                                                    */
/*                                                                    */
/* Error handling is the same as decNumber (qv.).                     */
/* Error handling is the same as decNumber (qv.).                     */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
#include <string.h>           /* [for memset/memcpy] */
#include <string.h>           /* [for memset/memcpy] */
#include <stdio.h>            /* [for printf] */
#include <stdio.h>            /* [for printf] */
 
 
#include "dconfig.h"          /* GCC definitions */
#include "dconfig.h"          /* GCC definitions */
#define  DECNUMDIGITS 16      /* make decNumbers with space for 16 */
#define  DECNUMDIGITS 16      /* make decNumbers with space for 16 */
#include "decNumber.h"        /* base number library */
#include "decNumber.h"        /* base number library */
#include "decNumberLocal.h"   /* decNumber local types, etc. */
#include "decNumberLocal.h"   /* decNumber local types, etc. */
#include "decimal64.h"        /* our primary include */
#include "decimal64.h"        /* our primary include */
 
 
/* Utility routines and tables [in decimal64.c]; externs for C++ */
/* Utility routines and tables [in decimal64.c]; externs for C++ */
extern const uInt COMBEXP[32], COMBMSD[32];
extern const uInt COMBEXP[32], COMBMSD[32];
extern const uShort DPD2BIN[1024];
extern const uShort DPD2BIN[1024];
extern const uShort BIN2DPD[1000];
extern const uShort BIN2DPD[1000];
extern const uByte  BIN2CHAR[4001];
extern const uByte  BIN2CHAR[4001];
 
 
extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
extern void decDigitsToDPD(const decNumber *, uInt *, Int);
extern void decDigitsToDPD(const decNumber *, uInt *, Int);
 
 
#if DECTRACE || DECCHECK
#if DECTRACE || DECCHECK
void decimal64Show(const decimal64 *);            /* for debug */
void decimal64Show(const decimal64 *);            /* for debug */
extern void decNumberShow(const decNumber *);     /* .. */
extern void decNumberShow(const decNumber *);     /* .. */
#endif
#endif
 
 
/* Useful macro */
/* Useful macro */
/* Clear a structure (e.g., a decNumber) */
/* Clear a structure (e.g., a decNumber) */
#define DEC_clear(d) memset(d, 0, sizeof(*d))
#define DEC_clear(d) memset(d, 0, sizeof(*d))
 
 
/* define and include the tables to use for conversions */
/* define and include the tables to use for conversions */
#define DEC_BIN2CHAR 1
#define DEC_BIN2CHAR 1
#define DEC_DPD2BIN  1
#define DEC_DPD2BIN  1
#define DEC_BIN2DPD  1             /* used for all sizes */
#define DEC_BIN2DPD  1             /* used for all sizes */
#include "decDPD.h"                /* lookup tables */
#include "decDPD.h"                /* lookup tables */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decimal64FromNumber -- convert decNumber to decimal64              */
/* decimal64FromNumber -- convert decNumber to decimal64              */
/*                                                                    */
/*                                                                    */
/*   ds is the target decimal64                                       */
/*   ds is the target decimal64                                       */
/*   dn is the source number (assumed valid)                          */
/*   dn is the source number (assumed valid)                          */
/*   set is the context, used only for reporting errors               */
/*   set is the context, used only for reporting errors               */
/*                                                                    */
/*                                                                    */
/* The set argument is used only for status reporting and for the     */
/* The set argument is used only for status reporting and for the     */
/* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */
/* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */
/* digits or an overflow is detected).  If the exponent is out of the */
/* digits or an overflow is detected).  If the exponent is out of the */
/* valid range then Overflow or Underflow will be raised.             */
/* valid range then Overflow or Underflow will be raised.             */
/* After Underflow a subnormal result is possible.                    */
/* After Underflow a subnormal result is possible.                    */
/*                                                                    */
/*                                                                    */
/* DEC_Clamped is set if the number has to be 'folded down' to fit,   */
/* DEC_Clamped is set if the number has to be 'folded down' to fit,   */
/* by reducing its exponent and multiplying the coefficient by a      */
/* by reducing its exponent and multiplying the coefficient by a      */
/* power of ten, or if the exponent on a zero had to be clamped.      */
/* power of ten, or if the exponent on a zero had to be clamped.      */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
decimal64 * decimal64FromNumber(decimal64 *d64, const decNumber *dn,
decimal64 * decimal64FromNumber(decimal64 *d64, const decNumber *dn,
                                decContext *set) {
                                decContext *set) {
  uInt status=0;            /* status accumulator */
  uInt status=0;            /* status accumulator */
  Int ae;                          /* adjusted exponent */
  Int ae;                          /* adjusted exponent */
  decNumber  dw;                   /* work */
  decNumber  dw;                   /* work */
  decContext dc;                   /* .. */
  decContext dc;                   /* .. */
  uInt comb, exp;                  /* .. */
  uInt comb, exp;                  /* .. */
  uInt uiwork;                     /* for macros */
  uInt uiwork;                     /* for macros */
  uInt targar[2]={0, 0};     /* target 64-bit */
  uInt targar[2]={0, 0};     /* target 64-bit */
  #define targhi targar[1]         /* name the word with the sign */
  #define targhi targar[1]         /* name the word with the sign */
  #define targlo targar[0]         /* and the other */
  #define targlo targar[0]         /* and the other */
 
 
  /* If the number has too many digits, or the exponent could be */
  /* If the number has too many digits, or the exponent could be */
  /* out of range then reduce the number under the appropriate */
  /* out of range then reduce the number under the appropriate */
  /* constraints.  This could push the number to Infinity or zero, */
  /* constraints.  This could push the number to Infinity or zero, */
  /* so this check and rounding must be done before generating the */
  /* so this check and rounding must be done before generating the */
  /* decimal64] */
  /* decimal64] */
  ae=dn->exponent+dn->digits-1;              /* [0 if special] */
  ae=dn->exponent+dn->digits-1;              /* [0 if special] */
  if (dn->digits>DECIMAL64_Pmax              /* too many digits */
  if (dn->digits>DECIMAL64_Pmax              /* too many digits */
   || ae>DECIMAL64_Emax                      /* likely overflow */
   || ae>DECIMAL64_Emax                      /* likely overflow */
   || ae<DECIMAL64_Emin) {                   /* likely underflow */
   || ae<DECIMAL64_Emin) {                   /* likely underflow */
    decContextDefault(&dc, DEC_INIT_DECIMAL64); /* [no traps] */
    decContextDefault(&dc, DEC_INIT_DECIMAL64); /* [no traps] */
    dc.round=set->round;                     /* use supplied rounding */
    dc.round=set->round;                     /* use supplied rounding */
    decNumberPlus(&dw, dn, &dc);             /* (round and check) */
    decNumberPlus(&dw, dn, &dc);             /* (round and check) */
    /* [this changes -0 to 0, so enforce the sign...] */
    /* [this changes -0 to 0, so enforce the sign...] */
    dw.bits|=dn->bits&DECNEG;
    dw.bits|=dn->bits&DECNEG;
    status=dc.status;                        /* save status */
    status=dc.status;                        /* save status */
    dn=&dw;                                  /* use the work number */
    dn=&dw;                                  /* use the work number */
    } /* maybe out of range */
    } /* maybe out of range */
 
 
  if (dn->bits&DECSPECIAL) {                      /* a special value */
  if (dn->bits&DECSPECIAL) {                      /* a special value */
    if (dn->bits&DECINF) targhi=DECIMAL_Inf<<24;
    if (dn->bits&DECINF) targhi=DECIMAL_Inf<<24;
     else {                                       /* sNaN or qNaN */
     else {                                       /* sNaN or qNaN */
      if ((*dn->lsu!=0 || dn->digits>1)    /* non-zero coefficient */
      if ((*dn->lsu!=0 || dn->digits>1)    /* non-zero coefficient */
       && (dn->digits<DECIMAL64_Pmax)) {          /* coefficient fits */
       && (dn->digits<DECIMAL64_Pmax)) {          /* coefficient fits */
        decDigitsToDPD(dn, targar, 0);
        decDigitsToDPD(dn, targar, 0);
        }
        }
      if (dn->bits&DECNAN) targhi|=DECIMAL_NaN<<24;
      if (dn->bits&DECNAN) targhi|=DECIMAL_NaN<<24;
       else targhi|=DECIMAL_sNaN<<24;
       else targhi|=DECIMAL_sNaN<<24;
      } /* a NaN */
      } /* a NaN */
    } /* special */
    } /* special */
 
 
   else { /* is finite */
   else { /* is finite */
    if (decNumberIsZero(dn)) {               /* is a zero */
    if (decNumberIsZero(dn)) {               /* is a zero */
      /* set and clamp exponent */
      /* set and clamp exponent */
      if (dn->exponent<-DECIMAL64_Bias) {
      if (dn->exponent<-DECIMAL64_Bias) {
        exp=0;                                /* low clamp */
        exp=0;                                /* low clamp */
        status|=DEC_Clamped;
        status|=DEC_Clamped;
        }
        }
       else {
       else {
        exp=dn->exponent+DECIMAL64_Bias;     /* bias exponent */
        exp=dn->exponent+DECIMAL64_Bias;     /* bias exponent */
        if (exp>DECIMAL64_Ehigh) {           /* top clamp */
        if (exp>DECIMAL64_Ehigh) {           /* top clamp */
          exp=DECIMAL64_Ehigh;
          exp=DECIMAL64_Ehigh;
          status|=DEC_Clamped;
          status|=DEC_Clamped;
          }
          }
        }
        }
      comb=(exp>>5) & 0x18;             /* msd=0, exp top 2 bits .. */
      comb=(exp>>5) & 0x18;             /* msd=0, exp top 2 bits .. */
      }
      }
     else {                             /* non-zero finite number */
     else {                             /* non-zero finite number */
      uInt msd;                         /* work */
      uInt msd;                         /* work */
      Int pad=0;                 /* coefficient pad digits */
      Int pad=0;                 /* coefficient pad digits */
 
 
      /* the dn is known to fit, but it may need to be padded */
      /* the dn is known to fit, but it may need to be padded */
      exp=(uInt)(dn->exponent+DECIMAL64_Bias);    /* bias exponent */
      exp=(uInt)(dn->exponent+DECIMAL64_Bias);    /* bias exponent */
      if (exp>DECIMAL64_Ehigh) {                  /* fold-down case */
      if (exp>DECIMAL64_Ehigh) {                  /* fold-down case */
        pad=exp-DECIMAL64_Ehigh;
        pad=exp-DECIMAL64_Ehigh;
        exp=DECIMAL64_Ehigh;                      /* [to maximum] */
        exp=DECIMAL64_Ehigh;                      /* [to maximum] */
        status|=DEC_Clamped;
        status|=DEC_Clamped;
        }
        }
 
 
      /* fastpath common case */
      /* fastpath common case */
      if (DECDPUN==3 && pad==0) {
      if (DECDPUN==3 && pad==0) {
        uInt dpd[6]={0,0,0,0,0,0};
        uInt dpd[6]={0,0,0,0,0,0};
        uInt i;
        uInt i;
        Int d=dn->digits;
        Int d=dn->digits;
        for (i=0; d>0; i++, d-=3) dpd[i]=BIN2DPD[dn->lsu[i]];
        for (i=0; d>0; i++, d-=3) dpd[i]=BIN2DPD[dn->lsu[i]];
        targlo =dpd[0];
        targlo =dpd[0];
        targlo|=dpd[1]<<10;
        targlo|=dpd[1]<<10;
        targlo|=dpd[2]<<20;
        targlo|=dpd[2]<<20;
        if (dn->digits>6) {
        if (dn->digits>6) {
          targlo|=dpd[3]<<30;
          targlo|=dpd[3]<<30;
          targhi =dpd[3]>>2;
          targhi =dpd[3]>>2;
          targhi|=dpd[4]<<8;
          targhi|=dpd[4]<<8;
          }
          }
        msd=dpd[5];                /* [did not really need conversion] */
        msd=dpd[5];                /* [did not really need conversion] */
        }
        }
       else { /* general case */
       else { /* general case */
        decDigitsToDPD(dn, targar, pad);
        decDigitsToDPD(dn, targar, pad);
        /* save and clear the top digit */
        /* save and clear the top digit */
        msd=targhi>>18;
        msd=targhi>>18;
        targhi&=0x0003ffff;
        targhi&=0x0003ffff;
        }
        }
 
 
      /* create the combination field */
      /* create the combination field */
      if (msd>=8) comb=0x18 | ((exp>>7) & 0x06) | (msd & 0x01);
      if (msd>=8) comb=0x18 | ((exp>>7) & 0x06) | (msd & 0x01);
             else comb=((exp>>5) & 0x18) | msd;
             else comb=((exp>>5) & 0x18) | msd;
      }
      }
    targhi|=comb<<26;              /* add combination field .. */
    targhi|=comb<<26;              /* add combination field .. */
    targhi|=(exp&0xff)<<18;        /* .. and exponent continuation */
    targhi|=(exp&0xff)<<18;        /* .. and exponent continuation */
    } /* finite */
    } /* finite */
 
 
  if (dn->bits&DECNEG) targhi|=0x80000000; /* add sign bit */
  if (dn->bits&DECNEG) targhi|=0x80000000; /* add sign bit */
 
 
  /* now write to storage; this is now always endian */
  /* now write to storage; this is now always endian */
  if (DECLITEND) {
  if (DECLITEND) {
    /* lo int then hi */
    /* lo int then hi */
    UBFROMUI(d64->bytes,   targar[0]);
    UBFROMUI(d64->bytes,   targar[0]);
    UBFROMUI(d64->bytes+4, targar[1]);
    UBFROMUI(d64->bytes+4, targar[1]);
    }
    }
   else {
   else {
    /* hi int then lo */
    /* hi int then lo */
    UBFROMUI(d64->bytes,   targar[1]);
    UBFROMUI(d64->bytes,   targar[1]);
    UBFROMUI(d64->bytes+4, targar[0]);
    UBFROMUI(d64->bytes+4, targar[0]);
    }
    }
 
 
  if (status!=0) decContextSetStatus(set, status); /* pass on status */
  if (status!=0) decContextSetStatus(set, status); /* pass on status */
  /* decimal64Show(d64); */
  /* decimal64Show(d64); */
  return d64;
  return d64;
  } /* decimal64FromNumber */
  } /* decimal64FromNumber */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decimal64ToNumber -- convert decimal64 to decNumber                */
/* decimal64ToNumber -- convert decimal64 to decNumber                */
/*   d64 is the source decimal64                                      */
/*   d64 is the source decimal64                                      */
/*   dn is the target number, with appropriate space                  */
/*   dn is the target number, with appropriate space                  */
/* No error is possible.                                              */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
decNumber * decimal64ToNumber(const decimal64 *d64, decNumber *dn) {
decNumber * decimal64ToNumber(const decimal64 *d64, decNumber *dn) {
  uInt msd;                        /* coefficient MSD */
  uInt msd;                        /* coefficient MSD */
  uInt exp;                        /* exponent top two bits */
  uInt exp;                        /* exponent top two bits */
  uInt comb;                       /* combination field */
  uInt comb;                       /* combination field */
  Int  need;                       /* work */
  Int  need;                       /* work */
  uInt uiwork;                     /* for macros */
  uInt uiwork;                     /* for macros */
  uInt sourar[2];                  /* source 64-bit */
  uInt sourar[2];                  /* source 64-bit */
  #define sourhi sourar[1]         /* name the word with the sign */
  #define sourhi sourar[1]         /* name the word with the sign */
  #define sourlo sourar[0]         /* and the lower word */
  #define sourlo sourar[0]         /* and the lower word */
 
 
  /* load source from storage; this is endian */
  /* load source from storage; this is endian */
  if (DECLITEND) {
  if (DECLITEND) {
    sourlo=UBTOUI(d64->bytes  );   /* directly load the low int */
    sourlo=UBTOUI(d64->bytes  );   /* directly load the low int */
    sourhi=UBTOUI(d64->bytes+4);   /* then the high int */
    sourhi=UBTOUI(d64->bytes+4);   /* then the high int */
    }
    }
   else {
   else {
    sourhi=UBTOUI(d64->bytes  );   /* directly load the high int */
    sourhi=UBTOUI(d64->bytes  );   /* directly load the high int */
    sourlo=UBTOUI(d64->bytes+4);   /* then the low int */
    sourlo=UBTOUI(d64->bytes+4);   /* then the low int */
    }
    }
 
 
  comb=(sourhi>>26)&0x1f;          /* combination field */
  comb=(sourhi>>26)&0x1f;          /* combination field */
 
 
  decNumberZero(dn);               /* clean number */
  decNumberZero(dn);               /* clean number */
  if (sourhi&0x80000000) dn->bits=DECNEG; /* set sign if negative */
  if (sourhi&0x80000000) dn->bits=DECNEG; /* set sign if negative */
 
 
  msd=COMBMSD[comb];               /* decode the combination field */
  msd=COMBMSD[comb];               /* decode the combination field */
  exp=COMBEXP[comb];               /* .. */
  exp=COMBEXP[comb];               /* .. */
 
 
  if (exp==3) {                    /* is a special */
  if (exp==3) {                    /* is a special */
    if (msd==0) {
    if (msd==0) {
      dn->bits|=DECINF;
      dn->bits|=DECINF;
      return dn;                   /* no coefficient needed */
      return dn;                   /* no coefficient needed */
      }
      }
    else if (sourhi&0x02000000) dn->bits|=DECSNAN;
    else if (sourhi&0x02000000) dn->bits|=DECSNAN;
    else dn->bits|=DECNAN;
    else dn->bits|=DECNAN;
    msd=0;                          /* no top digit */
    msd=0;                          /* no top digit */
    }
    }
   else {                          /* is a finite number */
   else {                          /* is a finite number */
    dn->exponent=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias; /* unbiased */
    dn->exponent=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias; /* unbiased */
    }
    }
 
 
  /* get the coefficient */
  /* get the coefficient */
  sourhi&=0x0003ffff;              /* clean coefficient continuation */
  sourhi&=0x0003ffff;              /* clean coefficient continuation */
  if (msd) {                       /* non-zero msd */
  if (msd) {                       /* non-zero msd */
    sourhi|=msd<<18;               /* prefix to coefficient */
    sourhi|=msd<<18;               /* prefix to coefficient */
    need=6;                        /* process 6 declets */
    need=6;                        /* process 6 declets */
    }
    }
   else { /* msd=0 */
   else { /* msd=0 */
    if (!sourhi) {                 /* top word 0 */
    if (!sourhi) {                 /* top word 0 */
      if (!sourlo) return dn;      /* easy: coefficient is 0 */
      if (!sourlo) return dn;      /* easy: coefficient is 0 */
      need=3;                      /* process at least 3 declets */
      need=3;                      /* process at least 3 declets */
      if (sourlo&0xc0000000) need++; /* process 4 declets */
      if (sourlo&0xc0000000) need++; /* process 4 declets */
      /* [could reduce some more, here] */
      /* [could reduce some more, here] */
      }
      }
     else {                        /* some bits in top word, msd=0 */
     else {                        /* some bits in top word, msd=0 */
      need=4;                      /* process at least 4 declets */
      need=4;                      /* process at least 4 declets */
      if (sourhi&0x0003ff00) need++; /* top declet!=0, process 5 */
      if (sourhi&0x0003ff00) need++; /* top declet!=0, process 5 */
      }
      }
    } /*msd=0 */
    } /*msd=0 */
 
 
  decDigitsFromDPD(dn, sourar, need);   /* process declets */
  decDigitsFromDPD(dn, sourar, need);   /* process declets */
  return dn;
  return dn;
  } /* decimal64ToNumber */
  } /* decimal64ToNumber */
 
 
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* to-scientific-string -- conversion to numeric string               */
/* to-scientific-string -- conversion to numeric string               */
/* to-engineering-string -- conversion to numeric string              */
/* to-engineering-string -- conversion to numeric string              */
/*                                                                    */
/*                                                                    */
/*   decimal64ToString(d64, string);                                  */
/*   decimal64ToString(d64, string);                                  */
/*   decimal64ToEngString(d64, string);                               */
/*   decimal64ToEngString(d64, string);                               */
/*                                                                    */
/*                                                                    */
/*  d64 is the decimal64 format number to convert                     */
/*  d64 is the decimal64 format number to convert                     */
/*  string is the string where the result will be laid out            */
/*  string is the string where the result will be laid out            */
/*                                                                    */
/*                                                                    */
/*  string must be at least 24 characters                             */
/*  string must be at least 24 characters                             */
/*                                                                    */
/*                                                                    */
/*  No error is possible, and no status can be set.                   */
/*  No error is possible, and no status can be set.                   */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
char * decimal64ToEngString(const decimal64 *d64, char *string){
char * decimal64ToEngString(const decimal64 *d64, char *string){
  decNumber dn;                         /* work */
  decNumber dn;                         /* work */
  decimal64ToNumber(d64, &dn);
  decimal64ToNumber(d64, &dn);
  decNumberToEngString(&dn, string);
  decNumberToEngString(&dn, string);
  return string;
  return string;
  } /* decimal64ToEngString */
  } /* decimal64ToEngString */
 
 
char * decimal64ToString(const decimal64 *d64, char *string){
char * decimal64ToString(const decimal64 *d64, char *string){
  uInt msd;                        /* coefficient MSD */
  uInt msd;                        /* coefficient MSD */
  Int  exp;                        /* exponent top two bits or full */
  Int  exp;                        /* exponent top two bits or full */
  uInt comb;                       /* combination field */
  uInt comb;                       /* combination field */
  char *cstart;                    /* coefficient start */
  char *cstart;                    /* coefficient start */
  char *c;                         /* output pointer in string */
  char *c;                         /* output pointer in string */
  const uByte *u;                  /* work */
  const uByte *u;                  /* work */
  char *s, *t;                     /* .. (source, target) */
  char *s, *t;                     /* .. (source, target) */
  Int  dpd;                        /* .. */
  Int  dpd;                        /* .. */
  Int  pre, e;                     /* .. */
  Int  pre, e;                     /* .. */
  uInt uiwork;                     /* for macros */
  uInt uiwork;                     /* for macros */
 
 
  uInt sourar[2];                  /* source 64-bit */
  uInt sourar[2];                  /* source 64-bit */
  #define sourhi sourar[1]         /* name the word with the sign */
  #define sourhi sourar[1]         /* name the word with the sign */
  #define sourlo sourar[0]         /* and the lower word */
  #define sourlo sourar[0]         /* and the lower word */
 
 
  /* load source from storage; this is endian */
  /* load source from storage; this is endian */
  if (DECLITEND) {
  if (DECLITEND) {
    sourlo=UBTOUI(d64->bytes  );   /* directly load the low int */
    sourlo=UBTOUI(d64->bytes  );   /* directly load the low int */
    sourhi=UBTOUI(d64->bytes+4);   /* then the high int */
    sourhi=UBTOUI(d64->bytes+4);   /* then the high int */
    }
    }
   else {
   else {
    sourhi=UBTOUI(d64->bytes  );   /* directly load the high int */
    sourhi=UBTOUI(d64->bytes  );   /* directly load the high int */
    sourlo=UBTOUI(d64->bytes+4);   /* then the low int */
    sourlo=UBTOUI(d64->bytes+4);   /* then the low int */
    }
    }
 
 
  c=string;                        /* where result will go */
  c=string;                        /* where result will go */
  if (((Int)sourhi)<0) *c++='-';   /* handle sign */
  if (((Int)sourhi)<0) *c++='-';   /* handle sign */
 
 
  comb=(sourhi>>26)&0x1f;          /* combination field */
  comb=(sourhi>>26)&0x1f;          /* combination field */
  msd=COMBMSD[comb];               /* decode the combination field */
  msd=COMBMSD[comb];               /* decode the combination field */
  exp=COMBEXP[comb];               /* .. */
  exp=COMBEXP[comb];               /* .. */
 
 
  if (exp==3) {
  if (exp==3) {
    if (msd==0) {                   /* infinity */
    if (msd==0) {                   /* infinity */
      strcpy(c,   "Inf");
      strcpy(c,   "Inf");
      strcpy(c+3, "inity");
      strcpy(c+3, "inity");
      return string;               /* easy */
      return string;               /* easy */
      }
      }
    if (sourhi&0x02000000) *c++='s'; /* sNaN */
    if (sourhi&0x02000000) *c++='s'; /* sNaN */
    strcpy(c, "NaN");              /* complete word */
    strcpy(c, "NaN");              /* complete word */
    c+=3;                          /* step past */
    c+=3;                          /* step past */
    if (sourlo==0 && (sourhi&0x0003ffff)==0) return string; /* zero payload */
    if (sourlo==0 && (sourhi&0x0003ffff)==0) return string; /* zero payload */
    /* otherwise drop through to add integer; set correct exp */
    /* otherwise drop through to add integer; set correct exp */
    exp=0; msd=0;            /* setup for following code */
    exp=0; msd=0;            /* setup for following code */
    }
    }
   else exp=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias;
   else exp=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias;
 
 
  /* convert 16 digits of significand to characters */
  /* convert 16 digits of significand to characters */
  cstart=c;                        /* save start of coefficient */
  cstart=c;                        /* save start of coefficient */
  if (msd) *c++='0'+(char)msd;     /* non-zero most significant digit */
  if (msd) *c++='0'+(char)msd;     /* non-zero most significant digit */
 
 
  /* Now decode the declets.  After extracting each one, it is */
  /* Now decode the declets.  After extracting each one, it is */
  /* decoded to binary and then to a 4-char sequence by table lookup; */
  /* decoded to binary and then to a 4-char sequence by table lookup; */
  /* the 4-chars are a 1-char length (significant digits, except 000 */
  /* the 4-chars are a 1-char length (significant digits, except 000 */
  /* has length 0).  This allows us to left-align the first declet */
  /* has length 0).  This allows us to left-align the first declet */
  /* with non-zero content, then remaining ones are full 3-char */
  /* with non-zero content, then remaining ones are full 3-char */
  /* length.  We use fixed-length memcpys because variable-length */
  /* length.  We use fixed-length memcpys because variable-length */
  /* causes a subroutine call in GCC.  (These are length 4 for speed */
  /* causes a subroutine call in GCC.  (These are length 4 for speed */
  /* and are safe because the array has an extra terminator byte.) */
  /* and are safe because the array has an extra terminator byte.) */
  #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4];                   \
  #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4];                   \
                   if (c!=cstart) {memcpy(c, u+1, 4); c+=3;}      \
                   if (c!=cstart) {memcpy(c, u+1, 4); c+=3;}      \
                    else if (*u)  {memcpy(c, u+4-*u, 4); c+=*u;}
                    else if (*u)  {memcpy(c, u+4-*u, 4); c+=*u;}
 
 
  dpd=(sourhi>>8)&0x3ff;                     /* declet 1 */
  dpd=(sourhi>>8)&0x3ff;                     /* declet 1 */
  dpd2char;
  dpd2char;
  dpd=((sourhi&0xff)<<2) | (sourlo>>30);     /* declet 2 */
  dpd=((sourhi&0xff)<<2) | (sourlo>>30);     /* declet 2 */
  dpd2char;
  dpd2char;
  dpd=(sourlo>>20)&0x3ff;                    /* declet 3 */
  dpd=(sourlo>>20)&0x3ff;                    /* declet 3 */
  dpd2char;
  dpd2char;
  dpd=(sourlo>>10)&0x3ff;                    /* declet 4 */
  dpd=(sourlo>>10)&0x3ff;                    /* declet 4 */
  dpd2char;
  dpd2char;
  dpd=(sourlo)&0x3ff;                        /* declet 5 */
  dpd=(sourlo)&0x3ff;                        /* declet 5 */
  dpd2char;
  dpd2char;
 
 
  if (c==cstart) *c++='0';         /* all zeros -- make 0 */
  if (c==cstart) *c++='0';         /* all zeros -- make 0 */
 
 
  if (exp==0) {             /* integer or NaN case -- easy */
  if (exp==0) {             /* integer or NaN case -- easy */
    *c='\0';                       /* terminate */
    *c='\0';                       /* terminate */
    return string;
    return string;
    }
    }
 
 
  /* non-0 exponent */
  /* non-0 exponent */
  e=0;                              /* assume no E */
  e=0;                              /* assume no E */
  pre=c-cstart+exp;
  pre=c-cstart+exp;
  /* [here, pre-exp is the digits count (==1 for zero)] */
  /* [here, pre-exp is the digits count (==1 for zero)] */
  if (exp>0 || pre<-5) {    /* need exponential form */
  if (exp>0 || pre<-5) {    /* need exponential form */
    e=pre-1;                       /* calculate E value */
    e=pre-1;                       /* calculate E value */
    pre=1;                         /* assume one digit before '.' */
    pre=1;                         /* assume one digit before '.' */
    } /* exponential form */
    } /* exponential form */
 
 
  /* modify the coefficient, adding 0s, '.', and E+nn as needed */
  /* modify the coefficient, adding 0s, '.', and E+nn as needed */
  s=c-1;                           /* source (LSD) */
  s=c-1;                           /* source (LSD) */
  if (pre>0) {                      /* ddd.ddd (plain), perhaps with E */
  if (pre>0) {                      /* ddd.ddd (plain), perhaps with E */
    char *dotat=cstart+pre;
    char *dotat=cstart+pre;
    if (dotat<c) {                 /* if embedded dot needed... */
    if (dotat<c) {                 /* if embedded dot needed... */
      t=c;                              /* target */
      t=c;                              /* target */
      for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */
      for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */
      *t='.';                           /* insert the dot */
      *t='.';                           /* insert the dot */
      c++;                              /* length increased by one */
      c++;                              /* length increased by one */
      }
      }
 
 
    /* finally add the E-part, if needed; it will never be 0, and has */
    /* finally add the E-part, if needed; it will never be 0, and has */
    /* a maximum length of 3 digits */
    /* a maximum length of 3 digits */
    if (e!=0) {
    if (e!=0) {
      *c++='E';                    /* starts with E */
      *c++='E';                    /* starts with E */
      *c++='+';                    /* assume positive */
      *c++='+';                    /* assume positive */
      if (e<0) {
      if (e<0) {
        *(c-1)='-';                /* oops, need '-' */
        *(c-1)='-';                /* oops, need '-' */
        e=-e;                      /* uInt, please */
        e=-e;                      /* uInt, please */
        }
        }
      u=&BIN2CHAR[e*4];            /* -> length byte */
      u=&BIN2CHAR[e*4];            /* -> length byte */
      memcpy(c, u+4-*u, 4);        /* copy fixed 4 characters [is safe] */
      memcpy(c, u+4-*u, 4);        /* copy fixed 4 characters [is safe] */
      c+=*u;                       /* bump pointer appropriately */
      c+=*u;                       /* bump pointer appropriately */
      }
      }
    *c='\0';                       /* add terminator */
    *c='\0';                       /* add terminator */
    /*printf("res %s\n", string); */
    /*printf("res %s\n", string); */
    return string;
    return string;
    } /* pre>0 */
    } /* pre>0 */
 
 
  /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
  /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
  t=c+1-pre;
  t=c+1-pre;
  *(t+1)='\0';                          /* can add terminator now */
  *(t+1)='\0';                          /* can add terminator now */
  for (; s>=cstart; s--, t--) *t=*s;    /* shift whole coefficient right */
  for (; s>=cstart; s--, t--) *t=*s;    /* shift whole coefficient right */
  c=cstart;
  c=cstart;
  *c++='0';                             /* always starts with 0. */
  *c++='0';                             /* always starts with 0. */
  *c++='.';
  *c++='.';
  for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */
  for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */
  /*printf("res %s\n", string); */
  /*printf("res %s\n", string); */
  return string;
  return string;
  } /* decimal64ToString */
  } /* decimal64ToString */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* to-number -- conversion from numeric string                        */
/* to-number -- conversion from numeric string                        */
/*                                                                    */
/*                                                                    */
/*   decimal64FromString(result, string, set);                        */
/*   decimal64FromString(result, string, set);                        */
/*                                                                    */
/*                                                                    */
/*  result  is the decimal64 format number which gets the result of   */
/*  result  is the decimal64 format number which gets the result of   */
/*          the conversion                                            */
/*          the conversion                                            */
/*  *string is the character string which should contain a valid      */
/*  *string is the character string which should contain a valid      */
/*          number (which may be a special value)                     */
/*          number (which may be a special value)                     */
/*  set     is the context                                            */
/*  set     is the context                                            */
/*                                                                    */
/*                                                                    */
/* The context is supplied to this routine is used for error handling */
/* The context is supplied to this routine is used for error handling */
/* (setting of status and traps) and for the rounding mode, only.     */
/* (setting of status and traps) and for the rounding mode, only.     */
/* If an error occurs, the result will be a valid decimal64 NaN.      */
/* If an error occurs, the result will be a valid decimal64 NaN.      */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
decimal64 * decimal64FromString(decimal64 *result, const char *string,
decimal64 * decimal64FromString(decimal64 *result, const char *string,
                                decContext *set) {
                                decContext *set) {
  decContext dc;                             /* work */
  decContext dc;                             /* work */
  decNumber dn;                              /* .. */
  decNumber dn;                              /* .. */
 
 
  decContextDefault(&dc, DEC_INIT_DECIMAL64); /* no traps, please */
  decContextDefault(&dc, DEC_INIT_DECIMAL64); /* no traps, please */
  dc.round=set->round;                        /* use supplied rounding */
  dc.round=set->round;                        /* use supplied rounding */
 
 
  decNumberFromString(&dn, string, &dc);     /* will round if needed */
  decNumberFromString(&dn, string, &dc);     /* will round if needed */
 
 
  decimal64FromNumber(result, &dn, &dc);
  decimal64FromNumber(result, &dn, &dc);
  if (dc.status!=0) {                         /* something happened */
  if (dc.status!=0) {                         /* something happened */
    decContextSetStatus(set, dc.status);     /* .. pass it on */
    decContextSetStatus(set, dc.status);     /* .. pass it on */
    }
    }
  return result;
  return result;
  } /* decimal64FromString */
  } /* decimal64FromString */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decimal64IsCanonical -- test whether encoding is canonical         */
/* decimal64IsCanonical -- test whether encoding is canonical         */
/*   d64 is the source decimal64                                      */
/*   d64 is the source decimal64                                      */
/*   returns 1 if the encoding of d64 is canonical, 0 otherwise       */
/*   returns 1 if the encoding of d64 is canonical, 0 otherwise       */
/* No error is possible.                                              */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
uInt decimal64IsCanonical(const decimal64 *d64) {
uInt decimal64IsCanonical(const decimal64 *d64) {
  decNumber dn;                         /* work */
  decNumber dn;                         /* work */
  decimal64 canon;                      /* .. */
  decimal64 canon;                      /* .. */
  decContext dc;                        /* .. */
  decContext dc;                        /* .. */
  decContextDefault(&dc, DEC_INIT_DECIMAL64);
  decContextDefault(&dc, DEC_INIT_DECIMAL64);
  decimal64ToNumber(d64, &dn);
  decimal64ToNumber(d64, &dn);
  decimal64FromNumber(&canon, &dn, &dc);/* canon will now be canonical */
  decimal64FromNumber(&canon, &dn, &dc);/* canon will now be canonical */
  return memcmp(d64, &canon, DECIMAL64_Bytes)==0;
  return memcmp(d64, &canon, DECIMAL64_Bytes)==0;
  } /* decimal64IsCanonical */
  } /* decimal64IsCanonical */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decimal64Canonical -- copy an encoding, ensuring it is canonical   */
/* decimal64Canonical -- copy an encoding, ensuring it is canonical   */
/*   d64 is the source decimal64                                      */
/*   d64 is the source decimal64                                      */
/*   result is the target (may be the same decimal64)                 */
/*   result is the target (may be the same decimal64)                 */
/*   returns result                                                   */
/*   returns result                                                   */
/* No error is possible.                                              */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
decimal64 * decimal64Canonical(decimal64 *result, const decimal64 *d64) {
decimal64 * decimal64Canonical(decimal64 *result, const decimal64 *d64) {
  decNumber dn;                         /* work */
  decNumber dn;                         /* work */
  decContext dc;                        /* .. */
  decContext dc;                        /* .. */
  decContextDefault(&dc, DEC_INIT_DECIMAL64);
  decContextDefault(&dc, DEC_INIT_DECIMAL64);
  decimal64ToNumber(d64, &dn);
  decimal64ToNumber(d64, &dn);
  decimal64FromNumber(result, &dn, &dc);/* result will now be canonical */
  decimal64FromNumber(result, &dn, &dc);/* result will now be canonical */
  return result;
  return result;
  } /* decimal64Canonical */
  } /* decimal64Canonical */
 
 
#if DECTRACE || DECCHECK
#if DECTRACE || DECCHECK
/* Macros for accessing decimal64 fields.  These assume the
/* Macros for accessing decimal64 fields.  These assume the
   argument is a reference (pointer) to the decimal64 structure,
   argument is a reference (pointer) to the decimal64 structure,
   and the decimal64 is in network byte order (big-endian) */
   and the decimal64 is in network byte order (big-endian) */
/* Get sign */
/* Get sign */
#define decimal64Sign(d)       ((unsigned)(d)->bytes[0]>>7)
#define decimal64Sign(d)       ((unsigned)(d)->bytes[0]>>7)
 
 
/* Get combination field */
/* Get combination field */
#define decimal64Comb(d)       (((d)->bytes[0] & 0x7c)>>2)
#define decimal64Comb(d)       (((d)->bytes[0] & 0x7c)>>2)
 
 
/* Get exponent continuation [does not remove bias] */
/* Get exponent continuation [does not remove bias] */
#define decimal64ExpCon(d)     ((((d)->bytes[0] & 0x03)<<6)           \
#define decimal64ExpCon(d)     ((((d)->bytes[0] & 0x03)<<6)           \
                             | ((unsigned)(d)->bytes[1]>>2))
                             | ((unsigned)(d)->bytes[1]>>2))
 
 
/* Set sign [this assumes sign previously 0] */
/* Set sign [this assumes sign previously 0] */
#define decimal64SetSign(d, b) {                                      \
#define decimal64SetSign(d, b) {                                      \
  (d)->bytes[0]|=((unsigned)(b)<<7);}
  (d)->bytes[0]|=((unsigned)(b)<<7);}
 
 
/* Set exponent continuation [does not apply bias] */
/* Set exponent continuation [does not apply bias] */
/* This assumes range has been checked and exponent previously 0; */
/* This assumes range has been checked and exponent previously 0; */
/* type of exponent must be unsigned */
/* type of exponent must be unsigned */
#define decimal64SetExpCon(d, e) {                                    \
#define decimal64SetExpCon(d, e) {                                    \
  (d)->bytes[0]|=(uByte)((e)>>6);                                      \
  (d)->bytes[0]|=(uByte)((e)>>6);                                      \
  (d)->bytes[1]|=(uByte)(((e)&0x3F)<<2);}
  (d)->bytes[1]|=(uByte)(((e)&0x3F)<<2);}
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decimal64Show -- display a decimal64 in hexadecimal [debug aid]    */
/* decimal64Show -- display a decimal64 in hexadecimal [debug aid]    */
/*   d64 -- the number to show                                        */
/*   d64 -- the number to show                                        */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* Also shows sign/cob/expconfields extracted */
/* Also shows sign/cob/expconfields extracted */
void decimal64Show(const decimal64 *d64) {
void decimal64Show(const decimal64 *d64) {
  char buf[DECIMAL64_Bytes*2+1];
  char buf[DECIMAL64_Bytes*2+1];
  Int i, j=0;
  Int i, j=0;
 
 
  if (DECLITEND) {
  if (DECLITEND) {
    for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
    for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d64->bytes[7-i]);
      sprintf(&buf[j], "%02x", d64->bytes[7-i]);
      }
      }
    printf(" D64> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
    printf(" D64> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
           d64->bytes[7]>>7, (d64->bytes[7]>>2)&0x1f,
           d64->bytes[7]>>7, (d64->bytes[7]>>2)&0x1f,
           ((d64->bytes[7]&0x3)<<6)| (d64->bytes[6]>>2));
           ((d64->bytes[7]&0x3)<<6)| (d64->bytes[6]>>2));
    }
    }
   else { /* big-endian */
   else { /* big-endian */
    for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
    for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
      sprintf(&buf[j], "%02x", d64->bytes[i]);
      sprintf(&buf[j], "%02x", d64->bytes[i]);
      }
      }
    printf(" D64> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
    printf(" D64> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
           decimal64Sign(d64), decimal64Comb(d64), decimal64ExpCon(d64));
           decimal64Sign(d64), decimal64Comb(d64), decimal64ExpCon(d64));
    }
    }
  } /* decimal64Show */
  } /* decimal64Show */
#endif
#endif
 
 
/* ================================================================== */
/* ================================================================== */
/* Shared utility routines and tables                                 */
/* Shared utility routines and tables                                 */
/* ================================================================== */
/* ================================================================== */
/* define and include the conversion tables to use for shared code */
/* define and include the conversion tables to use for shared code */
#if DECDPUN==3
#if DECDPUN==3
  #define DEC_DPD2BIN 1
  #define DEC_DPD2BIN 1
#else
#else
  #define DEC_DPD2BCD 1
  #define DEC_DPD2BCD 1
#endif
#endif
#include "decDPD.h"           /* lookup tables */
#include "decDPD.h"           /* lookup tables */
 
 
/* The maximum number of decNumberUnits needed for a working copy of */
/* The maximum number of decNumberUnits needed for a working copy of */
/* the units array is the ceiling of digits/DECDPUN, where digits is */
/* the units array is the ceiling of digits/DECDPUN, where digits is */
/* the maximum number of digits in any of the formats for which this */
/* the maximum number of digits in any of the formats for which this */
/* is used.  decimal128.h must not be included in this module, so, as */
/* is used.  decimal128.h must not be included in this module, so, as */
/* a very special case, that number is defined as a literal here. */
/* a very special case, that number is defined as a literal here. */
#define DECMAX754   34
#define DECMAX754   34
#define DECMAXUNITS ((DECMAX754+DECDPUN-1)/DECDPUN)
#define DECMAXUNITS ((DECMAX754+DECDPUN-1)/DECDPUN)
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* Combination field lookup tables (uInts to save measurable work)    */
/* Combination field lookup tables (uInts to save measurable work)    */
/*                                                                    */
/*                                                                    */
/*      COMBEXP - 2-bit most-significant-bits of exponent             */
/*      COMBEXP - 2-bit most-significant-bits of exponent             */
/*                [11 if an Infinity or NaN]                          */
/*                [11 if an Infinity or NaN]                          */
/*      COMBMSD - 4-bit most-significant-digit                        */
/*      COMBMSD - 4-bit most-significant-digit                        */
/*                [0=Infinity, 1=NaN if COMBEXP=11]                   */
/*                [0=Infinity, 1=NaN if COMBEXP=11]                   */
/*                                                                    */
/*                                                                    */
/* Both are indexed by the 5-bit combination field (0-31)             */
/* Both are indexed by the 5-bit combination field (0-31)             */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
const uInt COMBEXP[32]={0, 0, 0, 0, 0, 0, 0, 0,
const uInt COMBEXP[32]={0, 0, 0, 0, 0, 0, 0, 0,
                        1, 1, 1, 1, 1, 1, 1, 1,
                        1, 1, 1, 1, 1, 1, 1, 1,
                        2, 2, 2, 2, 2, 2, 2, 2,
                        2, 2, 2, 2, 2, 2, 2, 2,
                        0, 0, 1, 1, 2, 2, 3, 3};
                        0, 0, 1, 1, 2, 2, 3, 3};
const uInt COMBMSD[32]={0, 1, 2, 3, 4, 5, 6, 7,
const uInt COMBMSD[32]={0, 1, 2, 3, 4, 5, 6, 7,
                        0, 1, 2, 3, 4, 5, 6, 7,
                        0, 1, 2, 3, 4, 5, 6, 7,
                        0, 1, 2, 3, 4, 5, 6, 7,
                        0, 1, 2, 3, 4, 5, 6, 7,
                        8, 9, 8, 9, 8, 9, 0, 1};
                        8, 9, 8, 9, 8, 9, 0, 1};
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decDigitsToDPD -- pack coefficient into DPD form                   */
/* decDigitsToDPD -- pack coefficient into DPD form                   */
/*                                                                    */
/*                                                                    */
/*   dn   is the source number (assumed valid, max DECMAX754 digits)  */
/*   dn   is the source number (assumed valid, max DECMAX754 digits)  */
/*   targ is 1, 2, or 4-element uInt array, which the caller must     */
/*   targ is 1, 2, or 4-element uInt array, which the caller must     */
/*        have cleared to zeros                                       */
/*        have cleared to zeros                                       */
/*   shift is the number of 0 digits to add on the right (normally 0) */
/*   shift is the number of 0 digits to add on the right (normally 0) */
/*                                                                    */
/*                                                                    */
/* The coefficient must be known small enough to fit.  The full       */
/* The coefficient must be known small enough to fit.  The full       */
/* coefficient is copied, including the leading 'odd' digit.  This    */
/* coefficient is copied, including the leading 'odd' digit.  This    */
/* digit is retrieved and packed into the combination field by the    */
/* digit is retrieved and packed into the combination field by the    */
/* caller.                                                            */
/* caller.                                                            */
/*                                                                    */
/*                                                                    */
/* The target uInts are altered only as necessary to receive the      */
/* The target uInts are altered only as necessary to receive the      */
/* digits of the decNumber.  When more than one uInt is needed, they  */
/* digits of the decNumber.  When more than one uInt is needed, they  */
/* are filled from left to right (that is, the uInt at offset 0 will  */
/* are filled from left to right (that is, the uInt at offset 0 will  */
/* end up with the least-significant digits).                         */
/* end up with the least-significant digits).                         */
/*                                                                    */
/*                                                                    */
/* shift is used for 'fold-down' padding.                             */
/* shift is used for 'fold-down' padding.                             */
/*                                                                    */
/*                                                                    */
/* No error is possible.                                              */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
#if DECDPUN<=4
#if DECDPUN<=4
/* Constant multipliers for divide-by-power-of five using reciprocal */
/* Constant multipliers for divide-by-power-of five using reciprocal */
/* multiply, after removing powers of 2 by shifting, and final shift */
/* multiply, after removing powers of 2 by shifting, and final shift */
/* of 17 [we only need up to **4] */
/* of 17 [we only need up to **4] */
static const uInt multies[]={131073, 26215, 5243, 1049, 210};
static const uInt multies[]={131073, 26215, 5243, 1049, 210};
/* QUOT10 -- macro to return the quotient of unit u divided by 10**n */
/* QUOT10 -- macro to return the quotient of unit u divided by 10**n */
#define QUOT10(u, n) ((((uInt)(u)>>(n))*multies[n])>>17)
#define QUOT10(u, n) ((((uInt)(u)>>(n))*multies[n])>>17)
#endif
#endif
void decDigitsToDPD(const decNumber *dn, uInt *targ, Int shift) {
void decDigitsToDPD(const decNumber *dn, uInt *targ, Int shift) {
  Int  cut;                   /* work */
  Int  cut;                   /* work */
  Int  n;                     /* output bunch counter */
  Int  n;                     /* output bunch counter */
  Int  digits=dn->digits;     /* digit countdown */
  Int  digits=dn->digits;     /* digit countdown */
  uInt dpd;                   /* densely packed decimal value */
  uInt dpd;                   /* densely packed decimal value */
  uInt bin;                   /* binary value 0-999 */
  uInt bin;                   /* binary value 0-999 */
  uInt *uout=targ;            /* -> current output uInt */
  uInt *uout=targ;            /* -> current output uInt */
  uInt  uoff=0;        /* -> current output offset [from right] */
  uInt  uoff=0;        /* -> current output offset [from right] */
  const Unit *inu=dn->lsu;    /* -> current input unit */
  const Unit *inu=dn->lsu;    /* -> current input unit */
  Unit  uar[DECMAXUNITS];     /* working copy of units, iff shifted */
  Unit  uar[DECMAXUNITS];     /* working copy of units, iff shifted */
  #if DECDPUN!=3              /* not fast path */
  #if DECDPUN!=3              /* not fast path */
    Unit in;                  /* current unit */
    Unit in;                  /* current unit */
  #endif
  #endif
 
 
  if (shift!=0) {              /* shift towards most significant required */
  if (shift!=0) {              /* shift towards most significant required */
    /* shift the units array to the left by pad digits and copy */
    /* shift the units array to the left by pad digits and copy */
    /* [this code is a special case of decShiftToMost, which could */
    /* [this code is a special case of decShiftToMost, which could */
    /* be used instead if exposed and the array were copied first] */
    /* be used instead if exposed and the array were copied first] */
    const Unit *source;                 /* .. */
    const Unit *source;                 /* .. */
    Unit  *target, *first;              /* .. */
    Unit  *target, *first;              /* .. */
    uInt  next=0;                        /* work */
    uInt  next=0;                        /* work */
 
 
    source=dn->lsu+D2U(digits)-1;       /* where msu comes from */
    source=dn->lsu+D2U(digits)-1;       /* where msu comes from */
    target=uar+D2U(digits)-1+D2U(shift);/* where upper part of first cut goes */
    target=uar+D2U(digits)-1+D2U(shift);/* where upper part of first cut goes */
    cut=DECDPUN-MSUDIGITS(shift);       /* where to slice */
    cut=DECDPUN-MSUDIGITS(shift);       /* where to slice */
    if (cut==0) {                        /* unit-boundary case */
    if (cut==0) {                        /* unit-boundary case */
      for (; source>=dn->lsu; source--, target--) *target=*source;
      for (; source>=dn->lsu; source--, target--) *target=*source;
      }
      }
     else {
     else {
      first=uar+D2U(digits+shift)-1;    /* where msu will end up */
      first=uar+D2U(digits+shift)-1;    /* where msu will end up */
      for (; source>=dn->lsu; source--, target--) {
      for (; source>=dn->lsu; source--, target--) {
        /* split the source Unit and accumulate remainder for next */
        /* split the source Unit and accumulate remainder for next */
        #if DECDPUN<=4
        #if DECDPUN<=4
          uInt quot=QUOT10(*source, cut);
          uInt quot=QUOT10(*source, cut);
          uInt rem=*source-quot*DECPOWERS[cut];
          uInt rem=*source-quot*DECPOWERS[cut];
          next+=quot;
          next+=quot;
        #else
        #else
          uInt rem=*source%DECPOWERS[cut];
          uInt rem=*source%DECPOWERS[cut];
          next+=*source/DECPOWERS[cut];
          next+=*source/DECPOWERS[cut];
        #endif
        #endif
        if (target<=first) *target=(Unit)next; /* write to target iff valid */
        if (target<=first) *target=(Unit)next; /* write to target iff valid */
        next=rem*DECPOWERS[DECDPUN-cut];       /* save remainder for next Unit */
        next=rem*DECPOWERS[DECDPUN-cut];       /* save remainder for next Unit */
        }
        }
      } /* shift-move */
      } /* shift-move */
    /* propagate remainder to one below and clear the rest */
    /* propagate remainder to one below and clear the rest */
    for (; target>=uar; target--) {
    for (; target>=uar; target--) {
      *target=(Unit)next;
      *target=(Unit)next;
      next=0;
      next=0;
      }
      }
    digits+=shift;                 /* add count (shift) of zeros added */
    digits+=shift;                 /* add count (shift) of zeros added */
    inu=uar;                       /* use units in working array */
    inu=uar;                       /* use units in working array */
    }
    }
 
 
  /* now densely pack the coefficient into DPD declets */
  /* now densely pack the coefficient into DPD declets */
 
 
  #if DECDPUN!=3                   /* not fast path */
  #if DECDPUN!=3                   /* not fast path */
    in=*inu;                       /* current unit */
    in=*inu;                       /* current unit */
    cut=0;                          /* at lowest digit */
    cut=0;                          /* at lowest digit */
    bin=0;                          /* [keep compiler quiet] */
    bin=0;                          /* [keep compiler quiet] */
  #endif
  #endif
 
 
  for(n=0; digits>0; n++) {          /* each output bunch */
  for(n=0; digits>0; n++) {          /* each output bunch */
    #if DECDPUN==3                 /* fast path, 3-at-a-time */
    #if DECDPUN==3                 /* fast path, 3-at-a-time */
      bin=*inu;                    /* 3 digits ready for convert */
      bin=*inu;                    /* 3 digits ready for convert */
      digits-=3;                   /* [may go negative] */
      digits-=3;                   /* [may go negative] */
      inu++;                       /* may need another */
      inu++;                       /* may need another */
 
 
    #else                          /* must collect digit-by-digit */
    #else                          /* must collect digit-by-digit */
      Unit dig;                    /* current digit */
      Unit dig;                    /* current digit */
      Int j;                       /* digit-in-declet count */
      Int j;                       /* digit-in-declet count */
      for (j=0; j<3; j++) {
      for (j=0; j<3; j++) {
        #if DECDPUN<=4
        #if DECDPUN<=4
          Unit temp=(Unit)((uInt)(in*6554)>>16);
          Unit temp=(Unit)((uInt)(in*6554)>>16);
          dig=(Unit)(in-X10(temp));
          dig=(Unit)(in-X10(temp));
          in=temp;
          in=temp;
        #else
        #else
          dig=in%10;
          dig=in%10;
          in=in/10;
          in=in/10;
        #endif
        #endif
        if (j==0) bin=dig;
        if (j==0) bin=dig;
         else if (j==1)  bin+=X10(dig);
         else if (j==1)  bin+=X10(dig);
         else /* j==2 */ bin+=X100(dig);
         else /* j==2 */ bin+=X100(dig);
        digits--;
        digits--;
        if (digits==0) break;       /* [also protects *inu below] */
        if (digits==0) break;       /* [also protects *inu below] */
        cut++;
        cut++;
        if (cut==DECDPUN) {inu++; in=*inu; cut=0;}
        if (cut==DECDPUN) {inu++; in=*inu; cut=0;}
        }
        }
    #endif
    #endif
    /* here there are 3 digits in bin, or have used all input digits */
    /* here there are 3 digits in bin, or have used all input digits */
 
 
    dpd=BIN2DPD[bin];
    dpd=BIN2DPD[bin];
 
 
    /* write declet to uInt array */
    /* write declet to uInt array */
    *uout|=dpd<<uoff;
    *uout|=dpd<<uoff;
    uoff+=10;
    uoff+=10;
    if (uoff<32) continue;         /* no uInt boundary cross */
    if (uoff<32) continue;         /* no uInt boundary cross */
    uout++;
    uout++;
    uoff-=32;
    uoff-=32;
    *uout|=dpd>>(10-uoff);         /* collect top bits */
    *uout|=dpd>>(10-uoff);         /* collect top bits */
    } /* n declets */
    } /* n declets */
  return;
  return;
  } /* decDigitsToDPD */
  } /* decDigitsToDPD */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decDigitsFromDPD -- unpack a format's coefficient                  */
/* decDigitsFromDPD -- unpack a format's coefficient                  */
/*                                                                    */
/*                                                                    */
/*   dn is the target number, with 7, 16, or 34-digit space.          */
/*   dn is the target number, with 7, 16, or 34-digit space.          */
/*   sour is a 1, 2, or 4-element uInt array containing only declets  */
/*   sour is a 1, 2, or 4-element uInt array containing only declets  */
/*   declets is the number of (right-aligned) declets in sour to      */
/*   declets is the number of (right-aligned) declets in sour to      */
/*     be processed.  This may be 1 more than the obvious number in   */
/*     be processed.  This may be 1 more than the obvious number in   */
/*     a format, as any top digit is prefixed to the coefficient      */
/*     a format, as any top digit is prefixed to the coefficient      */
/*     continuation field.  It also may be as small as 1, as the      */
/*     continuation field.  It also may be as small as 1, as the      */
/*     caller may pre-process leading zero declets.                   */
/*     caller may pre-process leading zero declets.                   */
/*                                                                    */
/*                                                                    */
/* When doing the 'extra declet' case care is taken to avoid writing  */
/* When doing the 'extra declet' case care is taken to avoid writing  */
/* extra digits when there are leading zeros, as these could overflow */
/* extra digits when there are leading zeros, as these could overflow */
/* the units array when DECDPUN is not 3.                             */
/* the units array when DECDPUN is not 3.                             */
/*                                                                    */
/*                                                                    */
/* The target uInts are used only as necessary to process declets     */
/* The target uInts are used only as necessary to process declets     */
/* declets into the decNumber.  When more than one uInt is needed,    */
/* declets into the decNumber.  When more than one uInt is needed,    */
/* they are used from left to right (that is, the uInt at offset 0    */
/* they are used from left to right (that is, the uInt at offset 0    */
/* provides the least-significant digits).                            */
/* provides the least-significant digits).                            */
/*                                                                    */
/*                                                                    */
/* dn->digits is set, but not the sign or exponent.                   */
/* dn->digits is set, but not the sign or exponent.                   */
/* No error is possible [the redundant 888 codes are allowed].        */
/* No error is possible [the redundant 888 codes are allowed].        */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void decDigitsFromDPD(decNumber *dn, const uInt *sour, Int declets) {
void decDigitsFromDPD(decNumber *dn, const uInt *sour, Int declets) {
 
 
  uInt  dpd;                       /* collector for 10 bits */
  uInt  dpd;                       /* collector for 10 bits */
  Int   n;                         /* counter */
  Int   n;                         /* counter */
  Unit  *uout=dn->lsu;             /* -> current output unit */
  Unit  *uout=dn->lsu;             /* -> current output unit */
  Unit  *last=uout;                /* will be unit containing msd */
  Unit  *last=uout;                /* will be unit containing msd */
  const uInt *uin=sour;            /* -> current input uInt */
  const uInt *uin=sour;            /* -> current input uInt */
  uInt  uoff=0;             /* -> current input offset [from right] */
  uInt  uoff=0;             /* -> current input offset [from right] */
 
 
  #if DECDPUN!=3
  #if DECDPUN!=3
  uInt  bcd;                       /* BCD result */
  uInt  bcd;                       /* BCD result */
  uInt  nibble;                    /* work */
  uInt  nibble;                    /* work */
  Unit  out=0;                      /* accumulator */
  Unit  out=0;                      /* accumulator */
  Int   cut=0;                      /* power of ten in current unit */
  Int   cut=0;                      /* power of ten in current unit */
  #endif
  #endif
  #if DECDPUN>4
  #if DECDPUN>4
  uInt const *pow;                 /* work */
  uInt const *pow;                 /* work */
  #endif
  #endif
 
 
  /* Expand the densely-packed integer, right to left */
  /* Expand the densely-packed integer, right to left */
  for (n=declets-1; n>=0; n--) {   /* count down declets of 10 bits */
  for (n=declets-1; n>=0; n--) {   /* count down declets of 10 bits */
    dpd=*uin>>uoff;
    dpd=*uin>>uoff;
    uoff+=10;
    uoff+=10;
    if (uoff>32) {                 /* crossed uInt boundary */
    if (uoff>32) {                 /* crossed uInt boundary */
      uin++;
      uin++;
      uoff-=32;
      uoff-=32;
      dpd|=*uin<<(10-uoff);        /* get waiting bits */
      dpd|=*uin<<(10-uoff);        /* get waiting bits */
      }
      }
    dpd&=0x3ff;                    /* clear uninteresting bits */
    dpd&=0x3ff;                    /* clear uninteresting bits */
 
 
  #if DECDPUN==3
  #if DECDPUN==3
    if (dpd==0) *uout=0;
    if (dpd==0) *uout=0;
     else {
     else {
      *uout=DPD2BIN[dpd];          /* convert 10 bits to binary 0-999 */
      *uout=DPD2BIN[dpd];          /* convert 10 bits to binary 0-999 */
      last=uout;                   /* record most significant unit */
      last=uout;                   /* record most significant unit */
      }
      }
    uout++;
    uout++;
    } /* n */
    } /* n */
 
 
  #else /* DECDPUN!=3 */
  #else /* DECDPUN!=3 */
    if (dpd==0) {                   /* fastpath [e.g., leading zeros] */
    if (dpd==0) {                   /* fastpath [e.g., leading zeros] */
      /* write out three 0 digits (nibbles); out may have digit(s) */
      /* write out three 0 digits (nibbles); out may have digit(s) */
      cut++;
      cut++;
      if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
      if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
      if (n==0) break;              /* [as below, works even if MSD=0] */
      if (n==0) break;              /* [as below, works even if MSD=0] */
      cut++;
      cut++;
      if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
      if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
      cut++;
      cut++;
      if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
      if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
      continue;
      continue;
      }
      }
 
 
    bcd=DPD2BCD[dpd];              /* convert 10 bits to 12 bits BCD */
    bcd=DPD2BCD[dpd];              /* convert 10 bits to 12 bits BCD */
 
 
    /* now accumulate the 3 BCD nibbles into units */
    /* now accumulate the 3 BCD nibbles into units */
    nibble=bcd & 0x00f;
    nibble=bcd & 0x00f;
    if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
    if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
    cut++;
    cut++;
    if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
    if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
    bcd>>=4;
    bcd>>=4;
 
 
    /* if this is the last declet and the remaining nibbles in bcd */
    /* if this is the last declet and the remaining nibbles in bcd */
    /* are 00 then process no more nibbles, because this could be */
    /* are 00 then process no more nibbles, because this could be */
    /* the 'odd' MSD declet and writing any more Units would then */
    /* the 'odd' MSD declet and writing any more Units would then */
    /* overflow the unit array */
    /* overflow the unit array */
    if (n==0 && !bcd) break;
    if (n==0 && !bcd) break;
 
 
    nibble=bcd & 0x00f;
    nibble=bcd & 0x00f;
    if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
    if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
    cut++;
    cut++;
    if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
    if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
    bcd>>=4;
    bcd>>=4;
 
 
    nibble=bcd & 0x00f;
    nibble=bcd & 0x00f;
    if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
    if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
    cut++;
    cut++;
    if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
    if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
    } /* n */
    } /* n */
  if (cut!=0) {                  /* some more left over */
  if (cut!=0) {                  /* some more left over */
    *uout=out;                          /* write out final unit */
    *uout=out;                          /* write out final unit */
    if (out) last=uout;                 /* and note if non-zero */
    if (out) last=uout;                 /* and note if non-zero */
    }
    }
  #endif
  #endif
 
 
  /* here, last points to the most significant unit with digits; */
  /* here, last points to the most significant unit with digits; */
  /* inspect it to get the final digits count -- this is essentially */
  /* inspect it to get the final digits count -- this is essentially */
  /* the same code as decGetDigits in decNumber.c */
  /* the same code as decGetDigits in decNumber.c */
  dn->digits=(last-dn->lsu)*DECDPUN+1;  /* floor of digits, plus */
  dn->digits=(last-dn->lsu)*DECDPUN+1;  /* floor of digits, plus */
                                        /* must be at least 1 digit */
                                        /* must be at least 1 digit */
  #if DECDPUN>1
  #if DECDPUN>1
  if (*last<10) return;                 /* common odd digit or 0 */
  if (*last<10) return;                 /* common odd digit or 0 */
  dn->digits++;                         /* must be 2 at least */
  dn->digits++;                         /* must be 2 at least */
  #if DECDPUN>2
  #if DECDPUN>2
  if (*last<100) return;                /* 10-99 */
  if (*last<100) return;                /* 10-99 */
  dn->digits++;                         /* must be 3 at least */
  dn->digits++;                         /* must be 3 at least */
  #if DECDPUN>3
  #if DECDPUN>3
  if (*last<1000) return;               /* 100-999 */
  if (*last<1000) return;               /* 100-999 */
  dn->digits++;                         /* must be 4 at least */
  dn->digits++;                         /* must be 4 at least */
  #if DECDPUN>4
  #if DECDPUN>4
  for (pow=&DECPOWERS[4]; *last>=*pow; pow++) dn->digits++;
  for (pow=&DECPOWERS[4]; *last>=*pow; pow++) dn->digits++;
  #endif
  #endif
  #endif
  #endif
  #endif
  #endif
  #endif
  #endif
  return;
  return;
  } /*decDigitsFromDPD */
  } /*decDigitsFromDPD */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.