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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [libdecnumber/] [decPacked.c] - Diff between revs 834 and 842

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

Rev 834 Rev 842
/* Packed decimal conversion module for the decNumber C Library.
/* Packed decimal conversion module for the decNumber C Library.
   Copyright (C) 2007, 2009 Free Software Foundation, Inc.
   Copyright (C) 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/>.  */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* Packed Decimal conversion module                                   */
/* Packed Decimal conversion module                                   */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* This module comprises the routines for Packed Decimal format       */
/* This module comprises the routines for Packed Decimal format       */
/* numbers.  Conversions are supplied to and from decNumber, which in */
/* numbers.  Conversions are supplied to and from decNumber, which in */
/* turn supports:                                                     */
/* turn supports:                                                     */
/*   conversions to and from string                                   */
/*   conversions to and from string                                   */
/*   arithmetic routines                                              */
/*   arithmetic routines                                              */
/*   utilities.                                                       */
/*   utilities.                                                       */
/* Conversions from decNumber to and from densely packed decimal      */
/* Conversions from decNumber to and from densely packed decimal      */
/* formats are provided by the decimal32 through decimal128 modules.  */
/* formats are provided by the decimal32 through decimal128 modules.  */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
 
 
#include <string.h>           /* for NULL */
#include <string.h>           /* for NULL */
#include "decNumber.h"        /* base number library */
#include "decNumber.h"        /* base number library */
#include "decPacked.h"        /* packed decimal */
#include "decPacked.h"        /* packed decimal */
#include "decNumberLocal.h"   /* decNumber local types, etc. */
#include "decNumberLocal.h"   /* decNumber local types, etc. */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decPackedFromNumber -- convert decNumber to BCD Packed Decimal     */
/* decPackedFromNumber -- convert decNumber to BCD Packed Decimal     */
/*                                                                    */
/*                                                                    */
/*   bcd    is the BCD bytes                                          */
/*   bcd    is the BCD bytes                                          */
/*   length is the length of the BCD array                            */
/*   length is the length of the BCD array                            */
/*   scale  is the scale result                                       */
/*   scale  is the scale result                                       */
/*   dn     is the decNumber                                          */
/*   dn     is the decNumber                                          */
/*   returns bcd, or NULL if error                                    */
/*   returns bcd, or NULL if error                                    */
/*                                                                    */
/*                                                                    */
/* The number is converted to a BCD packed decimal byte array,        */
/* The number is converted to a BCD packed decimal byte array,        */
/* right aligned in the bcd array, whose length is indicated by the   */
/* right aligned in the bcd array, whose length is indicated by the   */
/* second parameter.  The final 4-bit nibble in the array will be a   */
/* second parameter.  The final 4-bit nibble in the array will be a   */
/* sign nibble, C (1100) for + and D (1101) for -.  Unused bytes and  */
/* sign nibble, C (1100) for + and D (1101) for -.  Unused bytes and  */
/* nibbles to the left of the number are set to 0.                    */
/* nibbles to the left of the number are set to 0.                    */
/*                                                                    */
/*                                                                    */
/* scale is set to the scale of the number (this is the exponent,     */
/* scale is set to the scale of the number (this is the exponent,     */
/* negated).  To force the number to a specified scale, first use the */
/* negated).  To force the number to a specified scale, first use the */
/* decNumberRescale routine, which will round and change the exponent */
/* decNumberRescale routine, which will round and change the exponent */
/* as necessary.                                                      */
/* as necessary.                                                      */
/*                                                                    */
/*                                                                    */
/* If there is an error (that is, the decNumber has too many digits   */
/* If there is an error (that is, the decNumber has too many digits   */
/* to fit in length bytes, or it is a NaN or Infinity), NULL is       */
/* to fit in length bytes, or it is a NaN or Infinity), NULL is       */
/* returned and the bcd and scale results are unchanged.  Otherwise   */
/* returned and the bcd and scale results are unchanged.  Otherwise   */
/* bcd is returned.                                                   */
/* bcd is returned.                                                   */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
uByte * decPackedFromNumber(uByte *bcd, Int length, Int *scale,
uByte * decPackedFromNumber(uByte *bcd, Int length, Int *scale,
                            const decNumber *dn) {
                            const decNumber *dn) {
  const Unit *up=dn->lsu;     /* Unit array pointer */
  const Unit *up=dn->lsu;     /* Unit array pointer */
  uByte obyte, *out;          /* current output byte, and where it goes */
  uByte obyte, *out;          /* current output byte, and where it goes */
  Int indigs=dn->digits;      /* digits processed */
  Int indigs=dn->digits;      /* digits processed */
  uInt cut=DECDPUN;           /* downcounter per Unit */
  uInt cut=DECDPUN;           /* downcounter per Unit */
  uInt u=*up;                 /* work */
  uInt u=*up;                 /* work */
  uInt nib;                   /* .. */
  uInt nib;                   /* .. */
  #if DECDPUN<=4
  #if DECDPUN<=4
  uInt temp;                  /* .. */
  uInt temp;                  /* .. */
  #endif
  #endif
 
 
  if (dn->digits>length*2-1                  /* too long .. */
  if (dn->digits>length*2-1                  /* too long .. */
   ||(dn->bits & DECSPECIAL)) return NULL;   /* .. or special -- hopeless */
   ||(dn->bits & DECSPECIAL)) return NULL;   /* .. or special -- hopeless */
 
 
  if (dn->bits&DECNEG) obyte=DECPMINUS;      /* set the sign .. */
  if (dn->bits&DECNEG) obyte=DECPMINUS;      /* set the sign .. */
   else                obyte=DECPPLUS;
   else                obyte=DECPPLUS;
  *scale=-dn->exponent;                      /* .. and scale */
  *scale=-dn->exponent;                      /* .. and scale */
 
 
  /* loop from lowest (rightmost) byte */
  /* loop from lowest (rightmost) byte */
  out=bcd+length-1;                          /* -> final byte */
  out=bcd+length-1;                          /* -> final byte */
  for (; out>=bcd; out--) {
  for (; out>=bcd; out--) {
    if (indigs>0) {
    if (indigs>0) {
      if (cut==0) {
      if (cut==0) {
        up++;
        up++;
        u=*up;
        u=*up;
        cut=DECDPUN;
        cut=DECDPUN;
        }
        }
      #if DECDPUN<=4
      #if DECDPUN<=4
        temp=(u*6554)>>16;         /* fast /10 */
        temp=(u*6554)>>16;         /* fast /10 */
        nib=u-X10(temp);
        nib=u-X10(temp);
        u=temp;
        u=temp;
      #else
      #else
        nib=u%10;                  /* cannot use *6554 trick :-( */
        nib=u%10;                  /* cannot use *6554 trick :-( */
        u=u/10;
        u=u/10;
      #endif
      #endif
      obyte|=(nib<<4);
      obyte|=(nib<<4);
      indigs--;
      indigs--;
      cut--;
      cut--;
      }
      }
    *out=obyte;
    *out=obyte;
    obyte=0;                        /* assume 0 */
    obyte=0;                        /* assume 0 */
    if (indigs>0) {
    if (indigs>0) {
      if (cut==0) {
      if (cut==0) {
        up++;
        up++;
        u=*up;
        u=*up;
        cut=DECDPUN;
        cut=DECDPUN;
        }
        }
      #if DECDPUN<=4
      #if DECDPUN<=4
        temp=(u*6554)>>16;         /* as above */
        temp=(u*6554)>>16;         /* as above */
        obyte=(uByte)(u-X10(temp));
        obyte=(uByte)(u-X10(temp));
        u=temp;
        u=temp;
      #else
      #else
        obyte=(uByte)(u%10);
        obyte=(uByte)(u%10);
        u=u/10;
        u=u/10;
      #endif
      #endif
      indigs--;
      indigs--;
      cut--;
      cut--;
      }
      }
    } /* loop */
    } /* loop */
 
 
  return bcd;
  return bcd;
  } /* decPackedFromNumber */
  } /* decPackedFromNumber */
 
 
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
/* decPackedToNumber -- convert BCD Packed Decimal to a decNumber     */
/* decPackedToNumber -- convert BCD Packed Decimal to a decNumber     */
/*                                                                    */
/*                                                                    */
/*   bcd    is the BCD bytes                                          */
/*   bcd    is the BCD bytes                                          */
/*   length is the length of the BCD array                            */
/*   length is the length of the BCD array                            */
/*   scale  is the scale associated with the BCD integer              */
/*   scale  is the scale associated with the BCD integer              */
/*   dn     is the decNumber [with space for length*2 digits]         */
/*   dn     is the decNumber [with space for length*2 digits]         */
/*   returns dn, or NULL if error                                     */
/*   returns dn, or NULL if error                                     */
/*                                                                    */
/*                                                                    */
/* The BCD packed decimal byte array, together with an associated     */
/* The BCD packed decimal byte array, together with an associated     */
/* scale, is converted to a decNumber.  The BCD array is assumed full */
/* scale, is converted to a decNumber.  The BCD array is assumed full */
/* of digits, and must be ended by a 4-bit sign nibble in the least   */
/* of digits, and must be ended by a 4-bit sign nibble in the least   */
/* significant four bits of the final byte.                           */
/* significant four bits of the final byte.                           */
/*                                                                    */
/*                                                                    */
/* The scale is used (negated) as the exponent of the decNumber.      */
/* The scale is used (negated) as the exponent of the decNumber.      */
/* Note that zeros may have a sign and/or a scale.                    */
/* Note that zeros may have a sign and/or a scale.                    */
/*                                                                    */
/*                                                                    */
/* The decNumber structure is assumed to have sufficient space to     */
/* The decNumber structure is assumed to have sufficient space to     */
/* hold the converted number (that is, up to length*2-1 digits), so   */
/* hold the converted number (that is, up to length*2-1 digits), so   */
/* no error is possible unless the adjusted exponent is out of range, */
/* no error is possible unless the adjusted exponent is out of range, */
/* no sign nibble was found, or a sign nibble was found before the    */
/* no sign nibble was found, or a sign nibble was found before the    */
/* final nibble.  In these error cases, NULL is returned and the      */
/* final nibble.  In these error cases, NULL is returned and the      */
/* decNumber will be 0.                                               */
/* decNumber will be 0.                                               */
/* ------------------------------------------------------------------ */
/* ------------------------------------------------------------------ */
decNumber * decPackedToNumber(const uByte *bcd, Int length,
decNumber * decPackedToNumber(const uByte *bcd, Int length,
                              const Int *scale, decNumber *dn) {
                              const Int *scale, decNumber *dn) {
  const uByte *last=bcd+length-1;  /* -> last byte */
  const uByte *last=bcd+length-1;  /* -> last byte */
  const uByte *first;              /* -> first non-zero byte */
  const uByte *first;              /* -> first non-zero byte */
  uInt  nib;                       /* work nibble */
  uInt  nib;                       /* work nibble */
  Unit  *up=dn->lsu;               /* output pointer */
  Unit  *up=dn->lsu;               /* output pointer */
  Int   digits;                    /* digits count */
  Int   digits;                    /* digits count */
  Int   cut=0;                      /* phase of output */
  Int   cut=0;                      /* phase of output */
 
 
  decNumberZero(dn);               /* default result */
  decNumberZero(dn);               /* default result */
  last=&bcd[length-1];
  last=&bcd[length-1];
  nib=*last & 0x0f;                /* get the sign */
  nib=*last & 0x0f;                /* get the sign */
  if (nib==DECPMINUS || nib==DECPMINUSALT) dn->bits=DECNEG;
  if (nib==DECPMINUS || nib==DECPMINUSALT) dn->bits=DECNEG;
   else if (nib<=9) return NULL;   /* not a sign nibble */
   else if (nib<=9) return NULL;   /* not a sign nibble */
 
 
  /* skip leading zero bytes [final byte is always non-zero, due to sign] */
  /* skip leading zero bytes [final byte is always non-zero, due to sign] */
  for (first=bcd; *first==0;) first++;
  for (first=bcd; *first==0;) first++;
  digits=(last-first)*2+1;              /* calculate digits .. */
  digits=(last-first)*2+1;              /* calculate digits .. */
  if ((*first & 0xf0)==0) digits--;      /* adjust for leading zero nibble */
  if ((*first & 0xf0)==0) digits--;      /* adjust for leading zero nibble */
  if (digits!=0) dn->digits=digits;      /* count of actual digits [if 0, */
  if (digits!=0) dn->digits=digits;      /* count of actual digits [if 0, */
                                        /* leave as 1] */
                                        /* leave as 1] */
 
 
  /* check the adjusted exponent; note that scale could be unbounded */
  /* check the adjusted exponent; note that scale could be unbounded */
  dn->exponent=-*scale;                 /* set the exponent */
  dn->exponent=-*scale;                 /* set the exponent */
  if (*scale>=0) {                       /* usual case */
  if (*scale>=0) {                       /* usual case */
    if ((dn->digits-*scale-1)<-DECNUMMAXE) {      /* underflow */
    if ((dn->digits-*scale-1)<-DECNUMMAXE) {      /* underflow */
      decNumberZero(dn);
      decNumberZero(dn);
      return NULL;}
      return NULL;}
    }
    }
   else { /* -ve scale; +ve exponent */
   else { /* -ve scale; +ve exponent */
    /* need to be careful to avoid wrap, here, also BADINT case */
    /* need to be careful to avoid wrap, here, also BADINT case */
    if ((*scale<-DECNUMMAXE)            /* overflow even without digits */
    if ((*scale<-DECNUMMAXE)            /* overflow even without digits */
         || ((dn->digits-*scale-1)>DECNUMMAXE)) { /* overflow */
         || ((dn->digits-*scale-1)>DECNUMMAXE)) { /* overflow */
      decNumberZero(dn);
      decNumberZero(dn);
      return NULL;}
      return NULL;}
    }
    }
  if (digits==0) return dn;              /* result was zero */
  if (digits==0) return dn;              /* result was zero */
 
 
  /* copy the digits to the number's units, starting at the lsu */
  /* copy the digits to the number's units, starting at the lsu */
  /* [unrolled] */
  /* [unrolled] */
  for (;;) {                            /* forever */
  for (;;) {                            /* forever */
    /* left nibble first */
    /* left nibble first */
    nib=(unsigned)(*last & 0xf0)>>4;
    nib=(unsigned)(*last & 0xf0)>>4;
    /* got a digit, in nib */
    /* got a digit, in nib */
    if (nib>9) {decNumberZero(dn); return NULL;}
    if (nib>9) {decNumberZero(dn); return NULL;}
 
 
    if (cut==0) *up=(Unit)nib;
    if (cut==0) *up=(Unit)nib;
     else *up=(Unit)(*up+nib*DECPOWERS[cut]);
     else *up=(Unit)(*up+nib*DECPOWERS[cut]);
    digits--;
    digits--;
    if (digits==0) break;                /* got them all */
    if (digits==0) break;                /* got them all */
    cut++;
    cut++;
    if (cut==DECDPUN) {
    if (cut==DECDPUN) {
      up++;
      up++;
      cut=0;
      cut=0;
      }
      }
    last--;                             /* ready for next */
    last--;                             /* ready for next */
    nib=*last & 0x0f;                   /* get right nibble */
    nib=*last & 0x0f;                   /* get right nibble */
    if (nib>9) {decNumberZero(dn); return NULL;}
    if (nib>9) {decNumberZero(dn); return NULL;}
 
 
    /* got a digit, in nib */
    /* got a digit, in nib */
    if (cut==0) *up=(Unit)nib;
    if (cut==0) *up=(Unit)nib;
     else *up=(Unit)(*up+nib*DECPOWERS[cut]);
     else *up=(Unit)(*up+nib*DECPOWERS[cut]);
    digits--;
    digits--;
    if (digits==0) break;                /* got them all */
    if (digits==0) break;                /* got them all */
    cut++;
    cut++;
    if (cut==DECDPUN) {
    if (cut==DECDPUN) {
      up++;
      up++;
      cut=0;
      cut=0;
      }
      }
    } /* forever */
    } /* forever */
 
 
  return dn;
  return dn;
  } /* decPackedToNumber */
  } /* decPackedToNumber */
 
 
 
 

powered by: WebSVN 2.1.0

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