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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [libdecnumber/] [decUtility.c] - Diff between revs 154 and 816

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

Rev 154 Rev 816
/* Utility functions for decimal floating point support via decNumber.
/* Utility functions for decimal floating point support via decNumber.
   Copyright (C) 2005 Free Software Foundation, Inc.
   Copyright (C) 2005 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 2, or (at your option) any later
   Software Foundation; either version 2, or (at your option) any later
   version.
   version.
 
 
   In addition to the permissions in the GNU General Public License,
   In addition to the permissions in the GNU General Public License,
   the Free Software Foundation gives you unlimited permission to link
   the Free Software Foundation gives you unlimited permission to link
   the compiled version of this file into combinations with other
   the compiled version of this file into combinations with other
   programs, and to distribute those combinations without any
   programs, and to distribute those combinations without any
   restriction coming from the use of this file.  (The General Public
   restriction coming from the use of this file.  (The General Public
   License restrictions do apply in other respects; for example, they
   License restrictions do apply in other respects; for example, they
   cover modification of the file, and distribution when not linked
   cover modification of the file, and distribution when not linked
   into a combine executable.)
   into a combine executable.)
 
 
   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.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING.  If not, write to the Free
   along with GCC; see the file COPYING.  If not, write to the Free
   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.  */
   02110-1301, USA.  */
 
 
#include "config.h"
#include "config.h"
#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 "decUtility.h"         /* utility routines */
#include "decUtility.h"         /* utility routines */
 
 
/* ================================================================== */
/* ================================================================== */
/* Shared utility routines                                            */
/* Shared utility routines                                            */
/* ================================================================== */
/* ================================================================== */
 
 
/* define and include the conversion tables to use */
/* define and include the conversion tables to use */
#define DEC_BIN2DPD 1           /* used for all sizes */
#define DEC_BIN2DPD 1           /* used for all sizes */
#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 we need for a working copy of */
/* The maximum number of decNumberUnits we need 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.  We do not want to include decimal128.h, so, as a very */
/* is used.  We do not want to include decimal128.h, so, as a very */
/* special case, that number is defined here. */
/* special case, that number is defined here. */
#define DECMAX754   34
#define DECMAX754   34
#define DECMAXUNITS ((DECMAX754+DECDPUN-1)/DECDPUN)
#define DECMAXUNITS ((DECMAX754+DECDPUN-1)/DECDPUN)
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decDensePackCoeff -- densely pack coefficient into DPD form        */
/* decDensePackCoeff -- densely 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)    */
/*   bytes is the target's byte array                                 */
/*   bytes is the target's byte array                                 */
/*   len is length of target format's byte array                      */
/*   len is length of target format's byte array                      */
/*   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, and is filled   */
/* The coefficient must be known small enough to fit, and is filled   */
/* in from the right (least significant first).  Note that the full   */
/* in from the right (least significant first).  Note that 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.                                                            */
/*                                                                    */
/*                                                                    */
/* shift is used for 'fold-down' padding.                             */
/* shift is used for 'fold-down' padding.                             */
/*                                                                    */
/*                                                                    */
/* No error is possible.                                              */
/* No error is possible.                                              */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
void
void
decDensePackCoeff (const decNumber * dn, uByte * bytes, Int len, Int shift)
decDensePackCoeff (const decNumber * dn, uByte * bytes, Int len, 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 */
  uByte *bout;                  /* -> current output byte */
  uByte *bout;                  /* -> current output byte */
  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 input unit */
  Unit in;                      /* current input unit */
#endif
#endif
 
 
  if (shift != 0)
  if (shift != 0)
    {                           /* shift towards most significant required */
    {                           /* 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] */
      Unit *target, *first;     /* work */
      Unit *target, *first;     /* work */
      const Unit *source;       /* work */
      const Unit *source;       /* work */
      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 */
      first = uar + D2U (digits + shift) - 1;   /* where msu will end up */
      first = uar + D2U (digits + shift) - 1;   /* where msu will end up */
      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 - shift % DECDPUN) % DECDPUN;
      cut = (DECDPUN - shift % DECDPUN) % DECDPUN;
      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 */
          uInt rem = *source % powers[cut];
          uInt rem = *source % powers[cut];
          next += *source / powers[cut];
          next += *source / powers[cut];
          if (target <= first)
          if (target <= first)
            *target = (Unit) next;      /* write to target iff valid */
            *target = (Unit) next;      /* write to target iff valid */
          next = rem * powers[DECDPUN - cut];   /* save remainder for next Unit */
          next = rem * powers[DECDPUN - cut];   /* save remainder for next Unit */
        }
        }
      /* 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 */
    }
    }
 
 
  /* densely pack the coefficient into the byte array, starting from
  /* densely pack the coefficient into the byte array, starting from
     the right (optionally padded) */
     the right (optionally padded) */
  bout = &bytes[len - 1];       /* rightmost result byte for phase */
  bout = &bytes[len - 1];       /* rightmost result byte for phase */
 
 
#if DECDPUN!=3                  /* not fast path */
#if DECDPUN!=3                  /* not fast path */
  in = *inu;                    /* prime */
  in = *inu;                    /* prime */
  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++)
  for (n = 0; digits > 0; n++)
    {                           /* each output bunch */
    {                           /* each output bunch */
#if DECDPUN==3                  /* fast path, 3-at-a-time */
#if DECDPUN==3                  /* fast path, 3-at-a-time */
      bin = *inu;               /* 3 ready for convert */
      bin = *inu;               /* 3 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-bunch count */
      Int j;                    /* digit-in-bunch 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)
          if (j == 0)
            bin = dig;
            bin = dig;
          else if (j == 1)
          else if (j == 1)
            bin += X10 (dig);
            bin += X10 (dig);
          else                  /* j==2 */
          else                  /* j==2 */
            bin += X100 (dig);
            bin += X100 (dig);
 
 
          digits--;
          digits--;
          if (digits == 0)
          if (digits == 0)
            break;              /* [also protects *inu below] */
            break;              /* [also protects *inu below] */
          cut++;
          cut++;
          if (cut == DECDPUN)
          if (cut == DECDPUN)
            {
            {
              inu++;
              inu++;
              in = *inu;
              in = *inu;
              cut = 0;
              cut = 0;
            }
            }
        }
        }
#endif
#endif
      /* here we have 3 digits in bin, or have used all input digits */
      /* here we have 3 digits in bin, or have used all input digits */
 
 
      dpd = BIN2DPD[bin];
      dpd = BIN2DPD[bin];
 
 
      /* write bunch (bcd) to byte array */
      /* write bunch (bcd) to byte array */
      switch (n & 0x03)
      switch (n & 0x03)
        {                       /* phase 0-3 */
        {                       /* phase 0-3 */
        case 0:
        case 0:
          *bout = (uByte) dpd;  /* [top 2 bits truncated] */
          *bout = (uByte) dpd;  /* [top 2 bits truncated] */
          bout--;
          bout--;
          *bout = (uByte) (dpd >> 8);
          *bout = (uByte) (dpd >> 8);
          break;
          break;
        case 1:
        case 1:
          *bout |= (uByte) (dpd << 2);
          *bout |= (uByte) (dpd << 2);
          bout--;
          bout--;
          *bout = (uByte) (dpd >> 6);
          *bout = (uByte) (dpd >> 6);
          break;
          break;
        case 2:
        case 2:
          *bout |= (uByte) (dpd << 4);
          *bout |= (uByte) (dpd << 4);
          bout--;
          bout--;
          *bout = (uByte) (dpd >> 4);
          *bout = (uByte) (dpd >> 4);
          break;
          break;
        case 3:
        case 3:
          *bout |= (uByte) (dpd << 6);
          *bout |= (uByte) (dpd << 6);
          bout--;
          bout--;
          *bout = (uByte) (dpd >> 2);
          *bout = (uByte) (dpd >> 2);
          bout--;
          bout--;
          break;
          break;
        }                       /* switch */
        }                       /* switch */
    }                           /* n bunches */
    }                           /* n bunches */
  return;
  return;
}
}
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decDenseUnpackCoeff -- unpack a format's coefficient               */
/* decDenseUnpackCoeff -- unpack a format's coefficient               */
/*                                                                    */
/*                                                                    */
/*   byte is the source's byte array                                  */
/*   byte is the source's byte array                                  */
/*   len is length of the source's byte array                         */
/*   len is length of the source's byte array                         */
/*   dn is the target number, with 7, 16, or 34-digit space.          */
/*   dn is the target number, with 7, 16, or 34-digit space.          */
/*   bunches is the count of DPD groups in the decNumber (2, 5, or 11)*/
/*   bunches is the count of DPD groups in the decNumber (2, 5, or 11)*/
/*   odd is 1 if there is a non-zero leading 10-bit group containing  */
/*   odd is 1 if there is a non-zero leading 10-bit group containing  */
/*     a single digit, 0 otherwise                                    */
/*     a single digit, 0 otherwise                                    */
/*                                                                    */
/*                                                                    */
/* (This routine works on a copy of the number, if necessary, where   */
/* (This routine works on a copy of the number, if necessary, where   */
/* an extra 10-bit group is prefixed to the coefficient continuation  */
/* an extra 10-bit group is prefixed to the coefficient continuation  */
/* to hold the most significant digit if the latter is non-0.)        */
/* to hold the most significant digit if the latter is non-0.)        */
/*                                                                    */
/*                                                                    */
/* 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
void
decDenseUnpackCoeff (const uByte * bytes, Int len, decNumber * dn,
decDenseUnpackCoeff (const uByte * bytes, Int len, decNumber * dn,
                     Int bunches, Int odd)
                     Int bunches, Int odd)
{
{
  uInt dpd = 0;                  /* collector for 10 bits */
  uInt dpd = 0;                  /* collector for 10 bits */
  Int n;                        /* counter */
  Int n;                        /* counter */
  const uByte *bin;             /* -> current input byte */
  const uByte *bin;             /* -> current input byte */
  Unit *uout = dn->lsu;         /* -> current output unit */
  Unit *uout = dn->lsu;         /* -> current output unit */
  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 */
  Unit *last = uout;            /* will be unit containing msd */
  Unit *last = uout;            /* will be unit containing msd */
#if DECDPUN!=3
#if DECDPUN!=3
  uInt bcd;                     /* BCD result */
  uInt bcd;                     /* BCD result */
  uInt nibble;                  /* work */
  uInt nibble;                  /* work */
#endif
#endif
 
 
  /* Expand the densely-packed integer, right to left */
  /* Expand the densely-packed integer, right to left */
  bin = &bytes[len - 1];        /* next input byte to use */
  bin = &bytes[len - 1];        /* next input byte to use */
  for (n = 0; n < bunches + odd; n++)
  for (n = 0; n < bunches + odd; n++)
    {                           /* N bunches of 10 bits */
    {                           /* N bunches of 10 bits */
      /* assemble the 10 bits */
      /* assemble the 10 bits */
      switch (n & 0x03)
      switch (n & 0x03)
        {                       /* phase 0-3 */
        {                       /* phase 0-3 */
        case 0:
        case 0:
          dpd = *bin;
          dpd = *bin;
          bin--;
          bin--;
          dpd |= (*bin & 0x03) << 8;
          dpd |= (*bin & 0x03) << 8;
          break;
          break;
        case 1:
        case 1:
          dpd = (unsigned) *bin >> 2;
          dpd = (unsigned) *bin >> 2;
          bin--;
          bin--;
          dpd |= (*bin & 0x0F) << 6;
          dpd |= (*bin & 0x0F) << 6;
          break;
          break;
        case 2:
        case 2:
          dpd = (unsigned) *bin >> 4;
          dpd = (unsigned) *bin >> 4;
          bin--;
          bin--;
          dpd |= (*bin & 0x3F) << 4;
          dpd |= (*bin & 0x3F) << 4;
          break;
          break;
        case 3:
        case 3:
          dpd = (unsigned) *bin >> 6;
          dpd = (unsigned) *bin >> 6;
          bin--;
          bin--;
          dpd |= (*bin) << 2;
          dpd |= (*bin) << 2;
          bin--;
          bin--;
          break;
          break;
        }                       /*switch */
        }                       /*switch */
 
 
#if DECDPUN==3
#if DECDPUN==3
      if (dpd == 0)
      if (dpd == 0)
        *uout = 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++;
 
 
#else /* DECDPUN!=3 */
#else /* DECDPUN!=3 */
      if (dpd == 0)
      if (dpd == 0)
        {                       /* fastpath [e.g., leading zeros] */
        {                       /* fastpath [e.g., leading zeros] */
          cut += 3;
          cut += 3;
          for (; cut >= DECDPUN;)
          for (; cut >= DECDPUN;)
            {
            {
              cut -= DECDPUN;
              cut -= DECDPUN;
              *uout = out;
              *uout = out;
              uout++;
              uout++;
              out = 0;
              out = 0;
            }
            }
          continue;
          continue;
        }
        }
      bcd = DPD2BCD[dpd];       /* convert 10 bits to 12 bits BCD */
      bcd = DPD2BCD[dpd];       /* convert 10 bits to 12 bits BCD */
      /* now split the 3 BCD nibbles into bytes, and accumulate into units */
      /* now split the 3 BCD nibbles into bytes, and accumulate into units */
      /* If this is the last bunch and it is an odd one, we only have one */
      /* If this is the last bunch and it is an odd one, we only have one */
      /* nibble to handle [extras could overflow a Unit] */
      /* nibble to handle [extras could overflow a Unit] */
      nibble = bcd & 0x000f;
      nibble = bcd & 0x000f;
      if (nibble)
      if (nibble)
        {
        {
          last = uout;
          last = uout;
          out = (Unit) (out + nibble * powers[cut]);
          out = (Unit) (out + nibble * powers[cut]);
        }
        }
      cut++;
      cut++;
      if (cut == DECDPUN)
      if (cut == DECDPUN)
        {
        {
          *uout = out;
          *uout = out;
          uout++;
          uout++;
          cut = 0;
          cut = 0;
          out = 0;
          out = 0;
        }
        }
      if (n < bunches)
      if (n < bunches)
        {
        {
          nibble = bcd & 0x00f0;
          nibble = bcd & 0x00f0;
          if (nibble)
          if (nibble)
            {
            {
              nibble >>= 4;
              nibble >>= 4;
              last = uout;
              last = uout;
              out = (Unit) (out + nibble * powers[cut]);
              out = (Unit) (out + nibble * powers[cut]);
            }
            }
          cut++;
          cut++;
          if (cut == DECDPUN)
          if (cut == DECDPUN)
            {
            {
              *uout = out;
              *uout = out;
              uout++;
              uout++;
              cut = 0;
              cut = 0;
              out = 0;
              out = 0;
            }
            }
          nibble = bcd & 0x0f00;
          nibble = bcd & 0x0f00;
          if (nibble)
          if (nibble)
            {
            {
              nibble >>= 8;
              nibble >>= 8;
              last = uout;
              last = uout;
              out = (Unit) (out + nibble * powers[cut]);
              out = (Unit) (out + nibble * powers[cut]);
            }
            }
          cut++;
          cut++;
          if (cut == DECDPUN)
          if (cut == DECDPUN)
            {
            {
              *uout = out;
              *uout = out;
              uout++;
              uout++;
              cut = 0;
              cut = 0;
              out = 0;
              out = 0;
            }
            }
        }
        }
#endif
#endif
    }                           /* n */
    }                           /* n */
  if (cut != 0)
  if (cut != 0)
    *uout = out;                /* write out final unit */
    *uout = out;                /* write out final unit */
 
 
  /* here, last points to the most significant unit with digits */
  /* here, last points to the most significant unit with digits */
  /* we need to inspect it to get final digits count */
  /* we need to inspect it to get final digits count */
  dn->digits = (last - dn->lsu) * DECDPUN;      /* floor of digits */
  dn->digits = (last - dn->lsu) * DECDPUN;      /* floor of digits */
  for (cut = 0; cut < DECDPUN; cut++)
  for (cut = 0; cut < DECDPUN; cut++)
    {
    {
      if (*last < powers[cut])
      if (*last < powers[cut])
        break;
        break;
      dn->digits++;
      dn->digits++;
    }
    }
  if (dn->digits == 0)
  if (dn->digits == 0)
    dn->digits++;               /* zero has one digit */
    dn->digits++;               /* zero has one digit */
  return;
  return;
}
}
 
 

powered by: WebSVN 2.1.0

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