aboutsummaryrefslogtreecommitdiff
path: root/libdecnumber/dpd
diff options
context:
space:
mode:
authorMichael Meissner <michael.meissner@amd.com>2007-03-24 17:04:47 +0000
committerMichael Meissner <meissner@gcc.gnu.org>2007-03-24 17:04:47 +0000
commit79b87c74d764bd42703818289685e48996b54eb8 (patch)
treeec50c8ab7786788a6da7d4f06b0cd93e9555d487 /libdecnumber/dpd
parentcca643862ddb1f61f200b567c667576d39961fb2 (diff)
downloadgcc-79b87c74d764bd42703818289685e48996b54eb8.zip
gcc-79b87c74d764bd42703818289685e48996b54eb8.tar.gz
gcc-79b87c74d764bd42703818289685e48996b54eb8.tar.bz2
Add BID decimal support
Co-Authored-By: H.J. Lu <hongjiu.lu@intel.com> Co-Authored-By: Marius Cornea <marius.cornea@intel.com> From-SVN: r123185
Diffstat (limited to 'libdecnumber/dpd')
-rw-r--r--libdecnumber/dpd/decimal128.c347
-rw-r--r--libdecnumber/dpd/decimal128.h130
-rw-r--r--libdecnumber/dpd/decimal32.c337
-rw-r--r--libdecnumber/dpd/decimal32.h120
-rw-r--r--libdecnumber/dpd/decimal64.c337
-rw-r--r--libdecnumber/dpd/decimal64.h124
6 files changed, 1395 insertions, 0 deletions
diff --git a/libdecnumber/dpd/decimal128.c b/libdecnumber/dpd/decimal128.c
new file mode 100644
index 0000000..92b4f61
--- /dev/null
+++ b/libdecnumber/dpd/decimal128.c
@@ -0,0 +1,347 @@
+/* Decimal 128-bit format module from the decNumber C Library.
+ Copyright (C) 2005 Free Software Foundation, Inc.
+ Contributed by IBM Corporation. Author Mike Cowlishaw.
+
+ This file is part of GCC.
+
+ 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
+ Software Foundation; either version 2, or (at your option) any later
+ version.
+
+ In addition to the permissions in the GNU General Public License,
+ the Free Software Foundation gives you unlimited permission to link
+ the compiled version of this file into combinations with other
+ programs, and to distribute those combinations without any
+ restriction coming from the use of this file. (The General Public
+ License restrictions do apply in other respects; for example, they
+ cover modification of the file, and distribution when not linked
+ into a combine executable.)
+
+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ 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
+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
+
+/* ------------------------------------------------------------------ */
+/* This module comprises the routines for decimal128 format numbers. */
+/* Conversions are supplied to and from decNumber and String. */
+/* */
+/* No arithmetic routines are included; decNumber provides these. */
+/* */
+/* Error handling is the same as decNumber (qv.). */
+/* ------------------------------------------------------------------ */
+#include <string.h> /* [for memset/memcpy] */
+#include <stdio.h> /* [for printf] */
+
+#define DECNUMDIGITS 34 /* we need decNumbers with space for 34 */
+#include "config.h"
+#include "decNumber.h" /* base number library */
+#include "decNumberLocal.h" /* decNumber local types, etc. */
+#include "decimal128.h" /* our primary include */
+#include "decUtility.h" /* utility routines */
+
+#if DECTRACE || DECCHECK
+void decimal128Show (const decimal128 *); /* for debug */
+void decNumberShow (const decNumber *); /* .. */
+#endif
+
+/* Useful macro */
+/* Clear a structure (e.g., a decNumber) */
+#define DEC_clear(d) memset(d, 0, sizeof(*d))
+
+/* ------------------------------------------------------------------ */
+/* decimal128FromNumber -- convert decNumber to decimal128 */
+/* */
+/* ds is the target decimal128 */
+/* dn is the source number (assumed valid) */
+/* set is the context, used only for reporting errors */
+/* */
+/* The set argument is used only for status reporting and for the */
+/* rounding mode (used if the coefficient is more than DECIMAL128_Pmax*/
+/* digits or an overflow is detected). If the exponent is out of the */
+/* valid range then Overflow or Underflow will be raised. */
+/* After Underflow a subnormal result is possible. */
+/* */
+/* DEC_Clamped is set if the number has to be 'folded down' to fit, */
+/* by reducing its exponent and multiplying the coefficient by a */
+/* power of ten, or if the exponent on a zero had to be clamped. */
+/* ------------------------------------------------------------------ */
+decimal128 *
+decimal128FromNumber (decimal128 * d128, const decNumber * dn, decContext * set)
+{
+ uInt status = 0; /* status accumulator */
+ Int pad = 0; /* coefficient pad digits */
+ decNumber dw; /* work */
+ decContext dc; /* .. */
+ uByte isneg = dn->bits & DECNEG; /* non-0 if original sign set */
+ uInt comb, exp; /* work */
+
+ /* If the number is finite, and has too many digits, or the exponent */
+ /* could be out of range then we reduce the number under the */
+ /* appropriate constraints */
+ if (!(dn->bits & DECSPECIAL))
+ { /* not a special value */
+ Int ae = dn->exponent + dn->digits - 1; /* adjusted exponent */
+ if (dn->digits > DECIMAL128_Pmax /* too many digits */
+ || ae > DECIMAL128_Emax /* likely overflow */
+ || ae < DECIMAL128_Emin)
+ { /* likely underflow */
+ decContextDefault (&dc, DEC_INIT_DECIMAL128); /* [no traps] */
+ dc.round = set->round; /* use supplied rounding */
+ decNumberPlus (&dw, dn, &dc); /* (round and check) */
+ /* [this changes -0 to 0, but it will be restored below] */
+ status |= dc.status; /* save status */
+ dn = &dw; /* use the work number */
+ }
+ /* [this could have pushed number to Infinity or zero, so this */
+ /* rounding must be done before we generate the decimal128] */
+ }
+
+ DEC_clear (d128); /* clean the target */
+ if (dn->bits & DECSPECIAL)
+ { /* a special value */
+ uByte top; /* work */
+ if (dn->bits & DECINF)
+ top = DECIMAL_Inf;
+ else
+ { /* sNaN or qNaN */
+ if ((*dn->lsu != 0 || dn->digits > 1) /* non-zero coefficient */
+ && (dn->digits < DECIMAL128_Pmax))
+ { /* coefficient fits */
+ decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), 0);
+ }
+ if (dn->bits & DECNAN)
+ top = DECIMAL_NaN;
+ else
+ top = DECIMAL_sNaN;
+ }
+ d128->bytes[0] = top;
+ }
+ else if (decNumberIsZero (dn))
+ { /* a zero */
+ /* set and clamp exponent */
+ if (dn->exponent < -DECIMAL128_Bias)
+ {
+ exp = 0;
+ status |= DEC_Clamped;
+ }
+ else
+ {
+ exp = dn->exponent + DECIMAL128_Bias; /* bias exponent */
+ if (exp > DECIMAL128_Ehigh)
+ { /* top clamp */
+ exp = DECIMAL128_Ehigh;
+ status |= DEC_Clamped;
+ }
+ }
+ comb = (exp >> 9) & 0x18; /* combination field */
+ d128->bytes[0] = (uByte) (comb << 2);
+ exp &= 0xfff; /* remaining exponent bits */
+ decimal128SetExpCon (d128, exp);
+ }
+ else
+ { /* non-zero finite number */
+ uInt msd; /* work */
+
+ /* we have a dn that fits, but it may need to be padded */
+ exp = (uInt) (dn->exponent + DECIMAL128_Bias); /* bias exponent */
+
+ if (exp > DECIMAL128_Ehigh)
+ { /* fold-down case */
+ pad = exp - DECIMAL128_Ehigh;
+ exp = DECIMAL128_Ehigh; /* [to maximum] */
+ status |= DEC_Clamped;
+ }
+
+ decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), pad);
+
+ /* save and clear the top digit */
+ msd = ((unsigned) d128->bytes[1] << 2) & 0x0c; /* top 2 bits */
+ msd |= ((unsigned) d128->bytes[2] >> 6); /* low 2 bits */
+ d128->bytes[1] &= 0xfc;
+ d128->bytes[2] &= 0x3f;
+
+ /* create the combination field */
+ if (msd >= 8)
+ comb = 0x18 | (msd & 0x01) | ((exp >> 11) & 0x06);
+ else
+ comb = (msd & 0x07) | ((exp >> 9) & 0x18);
+ d128->bytes[0] = (uByte) (comb << 2);
+ exp &= 0xfff; /* remaining exponent bits */
+ decimal128SetExpCon (d128, exp);
+ }
+
+ if (isneg)
+ decimal128SetSign (d128, 1);
+ if (status != 0)
+ decContextSetStatus (set, status); /* pass on status */
+
+ /* decimal128Show(d128); */
+ return d128;
+}
+
+/* ------------------------------------------------------------------ */
+/* decimal128ToNumber -- convert decimal128 to decNumber */
+/* d128 is the source decimal128 */
+/* dn is the target number, with appropriate space */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+decNumber *
+decimal128ToNumber (const decimal128 * d128, decNumber * dn)
+{
+ uInt msd; /* coefficient MSD */
+ decimal128 wk; /* working copy, if needed */
+ uInt top = d128->bytes[0] & 0x7f; /* top byte, less sign bit */
+ decNumberZero (dn); /* clean target */
+ /* set the sign if negative */
+ if (decimal128Sign (d128))
+ dn->bits = DECNEG;
+
+ if (top >= 0x78)
+ { /* is a special */
+ if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
+ dn->bits |= DECINF;
+ else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
+ dn->bits |= DECNAN;
+ else
+ dn->bits |= DECSNAN;
+ msd = 0; /* no top digit */
+ }
+ else
+ { /* have a finite number */
+ uInt comb = top >> 2; /* combination field */
+ uInt exp; /* exponent */
+
+ if (comb >= 0x18)
+ {
+ msd = 8 + (comb & 0x01);
+ exp = (comb & 0x06) << 11; /* MSBs */
+ }
+ else
+ {
+ msd = comb & 0x07;
+ exp = (comb & 0x18) << 9;
+ }
+ dn->exponent = exp + decimal128ExpCon (d128) - DECIMAL128_Bias; /* remove bias */
+ }
+
+ /* get the coefficient, unless infinite */
+ if (!(dn->bits & DECINF))
+ {
+ Int bunches = DECIMAL128_Pmax / 3; /* coefficient full bunches to convert */
+ Int odd = 0; /* assume MSD is 0 (no odd bunch) */
+ if (msd != 0)
+ { /* coefficient has leading non-0 digit */
+ /* make a copy of the decimal128, with an extra bunch which has */
+ /* the top digit ready for conversion */
+ wk = *d128; /* take a copy */
+ wk.bytes[0] = 0; /* clear all but coecon */
+ wk.bytes[1] = 0; /* .. */
+ wk.bytes[2] &= 0x3f; /* .. */
+ wk.bytes[1] |= (msd >> 2); /* and prefix MSD */
+ wk.bytes[2] |= (msd << 6); /* .. */
+ odd++; /* indicate the extra */
+ d128 = &wk; /* use the work copy */
+ }
+ decDenseUnpackCoeff (d128->bytes, sizeof (d128->bytes), dn, bunches,
+ odd);
+ }
+
+ /* decNumberShow(dn); */
+ return dn;
+}
+
+/* ------------------------------------------------------------------ */
+/* to-scientific-string -- conversion to numeric string */
+/* to-engineering-string -- conversion to numeric string */
+/* */
+/* decimal128ToString(d128, string); */
+/* decimal128ToEngString(d128, string); */
+/* */
+/* d128 is the decimal128 format number to convert */
+/* string is the string where the result will be laid out */
+/* */
+/* string must be at least 24 characters */
+/* */
+/* No error is possible, and no status can be set. */
+/* ------------------------------------------------------------------ */
+char *
+decimal128ToString (const decimal128 * d128, char *string)
+{
+ decNumber dn; /* work */
+ decimal128ToNumber (d128, &dn);
+ decNumberToString (&dn, string);
+ return string;
+}
+
+char *
+decimal128ToEngString (const decimal128 * d128, char *string)
+{
+ decNumber dn; /* work */
+ decimal128ToNumber (d128, &dn);
+ decNumberToEngString (&dn, string);
+ return string;
+}
+
+/* ------------------------------------------------------------------ */
+/* to-number -- conversion from numeric string */
+/* */
+/* decimal128FromString(result, string, set); */
+/* */
+/* result is the decimal128 format number which gets the result of */
+/* the conversion */
+/* *string is the character string which should contain a valid */
+/* number (which may be a special value) */
+/* set is the context */
+/* */
+/* The context is supplied to this routine is used for error handling */
+/* (setting of status and traps) and for the rounding mode, only. */
+/* If an error occurs, the result will be a valid decimal128 NaN. */
+/* ------------------------------------------------------------------ */
+decimal128 *
+decimal128FromString (decimal128 * result, const char *string, decContext * set)
+{
+ decContext dc; /* work */
+ decNumber dn; /* .. */
+
+ decContextDefault (&dc, DEC_INIT_DECIMAL128); /* no traps, please */
+ dc.round = set->round; /* use supplied rounding */
+
+ decNumberFromString (&dn, string, &dc); /* will round if needed */
+ decimal128FromNumber (result, &dn, &dc);
+ if (dc.status != 0)
+ { /* something happened */
+ decContextSetStatus (set, dc.status); /* .. pass it on */
+ }
+ return result;
+}
+
+
+#if DECTRACE || DECCHECK
+/* ------------------------------------------------------------------ */
+/* decimal128Show -- display a single in hexadecimal [debug aid] */
+/* d128 -- the number to show */
+/* ------------------------------------------------------------------ */
+/* Also shows sign/cob/expconfields extracted */
+void
+decimal128Show (const decimal128 * d128)
+{
+ char buf[DECIMAL128_Bytes * 2 + 1];
+ Int i, j;
+ j = 0;
+ for (i = 0; i < DECIMAL128_Bytes; i++)
+ {
+ sprintf (&buf[j], "%02x", d128->bytes[i]);
+ j = j + 2;
+ }
+ printf (" D128> %s [S:%d Cb:%02x E:%d]\n", buf,
+ decimal128Sign (d128), decimal128Comb (d128),
+ decimal128ExpCon (d128));
+}
+#endif
diff --git a/libdecnumber/dpd/decimal128.h b/libdecnumber/dpd/decimal128.h
new file mode 100644
index 0000000..a6bc87b
--- /dev/null
+++ b/libdecnumber/dpd/decimal128.h
@@ -0,0 +1,130 @@
+/* Decimal 128-bit format module header for the decNumber C Library
+ Copyright (C) 2005 Free Software Foundation, Inc.
+ Contributed by IBM Corporation. Author Mike Cowlishaw.
+
+ This file is part of GCC.
+
+ 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
+ Software Foundation; either version 2, or (at your option) any later
+ version.
+
+ In addition to the permissions in the GNU General Public License,
+ the Free Software Foundation gives you unlimited permission to link
+ the compiled version of this file into combinations with other
+ programs, and to distribute those combinations without any
+ restriction coming from the use of this file. (The General Public
+ License restrictions do apply in other respects; for example, they
+ cover modification of the file, and distribution when not linked
+ into a combine executable.)
+
+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ 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
+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
+
+#if !defined(DECIMAL128)
+#define DECIMAL128
+#define DEC128NAME "decimal128" /* Short name */
+#define DEC128FULLNAME "Decimal 128-bit Number" /* Verbose name */
+#define DEC128AUTHOR "Mike Cowlishaw" /* Who to blame */
+
+#if defined(DECIMAL32)
+#error decimal128.h must precede decimal32.h for correct DECNUMDIGITS
+#else
+#if defined(DECIMAL64)
+#error decimal128.h must precede decimal64.h for correct DECNUMDIGITS
+#endif
+#endif
+
+ /* parameters for decimal128s */
+#define DECIMAL128_Bytes 16 /* length */
+#define DECIMAL128_Pmax 34 /* maximum precision (digits) */
+#define DECIMAL128_Emax 6144 /* maximum adjusted exponent */
+#define DECIMAL128_Emin -6143 /* minimum adjusted exponent */
+#define DECIMAL128_Bias 6176 /* bias for the exponent */
+#define DECIMAL128_String 43 /* maximum string length, +1 */
+ /* highest biased exponent (Elimit-1) */
+#define DECIMAL128_Ehigh (DECIMAL128_Emax+DECIMAL128_Bias-DECIMAL128_Pmax+1)
+
+#ifndef DECNUMDIGITS
+#define DECNUMDIGITS DECIMAL128_Pmax /* size if not already defined */
+#endif
+#ifndef DECNUMBER
+#include "decNumber.h" /* context and number library */
+#endif
+
+ /* Decimal 128-bit type, accessible by bytes */
+typedef struct
+{
+ uint8_t bytes[DECIMAL128_Bytes]; /* decimal128: 1, 5, 12, 110 bits */
+} decimal128;
+
+ /* special values [top byte excluding sign bit; last two bits are
+ don't-care for Infinity on input, last bit don't-care for NaN] */
+#if !defined(DECIMAL_NaN)
+#define DECIMAL_NaN 0x7c /* 0 11111 00 NaN */
+#define DECIMAL_sNaN 0x7e /* 0 11111 10 sNaN */
+#define DECIMAL_Inf 0x78 /* 0 11110 00 Infinity */
+#endif
+
+ /* Macros for accessing decimal128 fields. These assume the argument
+ is a reference (pointer) to the decimal128 structure */
+ /* Get sign */
+#define decimal128Sign(d) ((unsigned)(d)->bytes[0]>>7)
+
+ /* Get combination field */
+#define decimal128Comb(d) (((d)->bytes[0] & 0x7c)>>2)
+
+ /* Get exponent continuation [does not remove bias] */
+#define decimal128ExpCon(d) ((((d)->bytes[0] & 0x03)<<10) \
+ | ((unsigned)(d)->bytes[1]<<2) \
+ | ((unsigned)(d)->bytes[2]>>6))
+
+ /* Set sign [this assumes sign previously 0] */
+#define decimal128SetSign(d, b) { \
+ (d)->bytes[0]|=((unsigned)(b)<<7);}
+
+ /* Clear sign */
+#define decimal128ClearSign(d) {(d)->bytes[0]&=~0x80;}
+
+ /* Flip sign */
+#define decimal128FlipSign(d) {(d)->bytes[0]^=0x80;}
+
+ /* Set exponent continuation [does not apply bias] */
+ /* This assumes range has been checked and exponent previously 0; */
+ /* type of exponent must be unsigned */
+#define decimal128SetExpCon(d, e) { \
+ (d)->bytes[0]|=(uint8_t)((e)>>10); \
+ (d)->bytes[1] =(uint8_t)(((e)&0x3fc)>>2); \
+ (d)->bytes[2]|=(uint8_t)(((e)&0x03)<<6);}
+
+ /* ------------------------------------------------------------------ */
+ /* Routines */
+ /* ------------------------------------------------------------------ */
+
+#ifdef IN_LIBGCC2
+#ifndef decimal128FromString
+#define decimal128FromString __decimal128FromString
+#define decimal128ToString __decimal128ToString
+#define decimal128ToEngString __decimal128ToEngString
+#define decimal128FromNumber __decimal128FromNumber
+#define decimal128ToNumber __decimal128ToNumber
+#endif
+#endif
+
+ /* String conversions */
+decimal128 *decimal128FromString (decimal128 *, const char *, decContext *);
+char *decimal128ToString (const decimal128 *, char *);
+char *decimal128ToEngString (const decimal128 *, char *);
+
+ /* decNumber conversions */
+decimal128 *decimal128FromNumber (decimal128 *, const decNumber *, decContext *);
+decNumber *decimal128ToNumber (const decimal128 *, decNumber *);
+
+#endif
diff --git a/libdecnumber/dpd/decimal32.c b/libdecnumber/dpd/decimal32.c
new file mode 100644
index 0000000..8691286
--- /dev/null
+++ b/libdecnumber/dpd/decimal32.c
@@ -0,0 +1,337 @@
+/* Decimal 32-bit format module for the decNumber C Library
+ Copyright (C) 2005 Free Software Foundation, Inc.
+ Contributed by IBM Corporation. Author Mike Cowlishaw.
+
+ This file is part of GCC.
+
+ 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
+ Software Foundation; either version 2, or (at your option) any later
+ version.
+
+ In addition to the permissions in the GNU General Public License,
+ the Free Software Foundation gives you unlimited permission to link
+ the compiled version of this file into combinations with other
+ programs, and to distribute those combinations without any
+ restriction coming from the use of this file. (The General Public
+ License restrictions do apply in other respects; for example, they
+ cover modification of the file, and distribution when not linked
+ into a combine executable.)
+
+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ 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
+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
+
+/* ------------------------------------------------------------------ */
+/* This module comprises the routines for decimal32 format numbers. */
+/* Conversions are supplied to and from decNumber and String. */
+/* */
+/* No arithmetic routines are included; decNumber provides these. */
+/* */
+/* Error handling is the same as decNumber (qv.). */
+/* ------------------------------------------------------------------ */
+#include <string.h> /* [for memset/memcpy] */
+#include <stdio.h> /* [for printf] */
+
+#define DECNUMDIGITS 7 /* we need decNumbers with space for 7 */
+#include "config.h"
+#include "decNumber.h" /* base number library */
+#include "decNumberLocal.h" /* decNumber local types, etc. */
+#include "decimal32.h" /* our primary include */
+#include "decUtility.h" /* utility routines */
+
+#if DECTRACE || DECCHECK
+void decimal32Show (const decimal32 *); /* for debug */
+void decNumberShow (const decNumber *); /* .. */
+#endif
+
+/* Useful macro */
+/* Clear a structure (e.g., a decNumber) */
+#define DEC_clear(d) memset(d, 0, sizeof(*d))
+
+/* ------------------------------------------------------------------ */
+/* decimal32FromNumber -- convert decNumber to decimal32 */
+/* */
+/* ds is the target decimal32 */
+/* dn is the source number (assumed valid) */
+/* set is the context, used only for reporting errors */
+/* */
+/* The set argument is used only for status reporting and for the */
+/* rounding mode (used if the coefficient is more than DECIMAL32_Pmax */
+/* digits or an overflow is detected). If the exponent is out of the */
+/* valid range then Overflow or Underflow will be raised. */
+/* After Underflow a subnormal result is possible. */
+/* */
+/* DEC_Clamped is set if the number has to be 'folded down' to fit, */
+/* by reducing its exponent and multiplying the coefficient by a */
+/* power of ten, or if the exponent on a zero had to be clamped. */
+/* ------------------------------------------------------------------ */
+decimal32 *
+decimal32FromNumber (decimal32 * d32, const decNumber * dn, decContext * set)
+{
+ uInt status = 0; /* status accumulator */
+ Int pad = 0; /* coefficient pad digits */
+ decNumber dw; /* work */
+ decContext dc; /* .. */
+ uByte isneg = dn->bits & DECNEG; /* non-0 if original sign set */
+ uInt comb, exp; /* work */
+
+ /* If the number is finite, and has too many digits, or the exponent */
+ /* could be out of range then we reduce the number under the */
+ /* appropriate constraints */
+ if (!(dn->bits & DECSPECIAL))
+ { /* not a special value */
+ Int ae = dn->exponent + dn->digits - 1; /* adjusted exponent */
+ if (dn->digits > DECIMAL32_Pmax /* too many digits */
+ || ae > DECIMAL32_Emax /* likely overflow */
+ || ae < DECIMAL32_Emin)
+ { /* likely underflow */
+ decContextDefault (&dc, DEC_INIT_DECIMAL32); /* [no traps] */
+ dc.round = set->round; /* use supplied rounding */
+ decNumberPlus (&dw, dn, &dc); /* (round and check) */
+ /* [this changes -0 to 0, but it will be restored below] */
+ status |= dc.status; /* save status */
+ dn = &dw; /* use the work number */
+ }
+ /* [this could have pushed number to Infinity or zero, so this */
+ /* rounding must be done before we generate the decimal32] */
+ }
+
+ DEC_clear (d32); /* clean the target */
+ if (dn->bits & DECSPECIAL)
+ { /* a special value */
+ uByte top; /* work */
+ if (dn->bits & DECINF)
+ top = DECIMAL_Inf;
+ else
+ { /* sNaN or qNaN */
+ if ((*dn->lsu != 0 || dn->digits > 1) /* non-zero coefficient */
+ && (dn->digits < DECIMAL32_Pmax))
+ { /* coefficient fits */
+ decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), 0);
+ }
+ if (dn->bits & DECNAN)
+ top = DECIMAL_NaN;
+ else
+ top = DECIMAL_sNaN;
+ }
+ d32->bytes[0] = top;
+ }
+ else if (decNumberIsZero (dn))
+ { /* a zero */
+ /* set and clamp exponent */
+ if (dn->exponent < -DECIMAL32_Bias)
+ {
+ exp = 0;
+ status |= DEC_Clamped;
+ }
+ else
+ {
+ exp = dn->exponent + DECIMAL32_Bias; /* bias exponent */
+ if (exp > DECIMAL32_Ehigh)
+ { /* top clamp */
+ exp = DECIMAL32_Ehigh;
+ status |= DEC_Clamped;
+ }
+ }
+ comb = (exp >> 3) & 0x18; /* combination field */
+ d32->bytes[0] = (uByte) (comb << 2);
+ exp &= 0x3f; /* remaining exponent bits */
+ decimal32SetExpCon (d32, exp);
+ }
+ else
+ { /* non-zero finite number */
+ uInt msd; /* work */
+
+ /* we have a dn that fits, but it may need to be padded */
+ exp = (uInt) (dn->exponent + DECIMAL32_Bias); /* bias exponent */
+
+ if (exp > DECIMAL32_Ehigh)
+ { /* fold-down case */
+ pad = exp - DECIMAL32_Ehigh;
+ exp = DECIMAL32_Ehigh; /* [to maximum] */
+ status |= DEC_Clamped;
+ }
+
+ decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), pad);
+
+ /* save and clear the top digit */
+ msd = ((unsigned) d32->bytes[1] >> 4);
+ d32->bytes[1] &= 0x0f;
+ /* create the combination field */
+ if (msd >= 8)
+ comb = 0x18 | (msd & 0x01) | ((exp >> 5) & 0x06);
+ else
+ comb = (msd & 0x07) | ((exp >> 3) & 0x18);
+ d32->bytes[0] = (uByte) (comb << 2);
+ exp &= 0x3f; /* remaining exponent bits */
+ decimal32SetExpCon (d32, exp);
+ }
+
+ if (isneg)
+ decimal32SetSign (d32, 1);
+ if (status != 0)
+ decContextSetStatus (set, status); /* pass on status */
+
+ /*decimal32Show(d32); */
+ return d32;
+}
+
+/* ------------------------------------------------------------------ */
+/* decimal32ToNumber -- convert decimal32 to decNumber */
+/* d32 is the source decimal32 */
+/* dn is the target number, with appropriate space */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+decNumber *
+decimal32ToNumber (const decimal32 * d32, decNumber * dn)
+{
+ uInt msd; /* coefficient MSD */
+ decimal32 wk; /* working copy, if needed */
+ uInt top = d32->bytes[0] & 0x7f; /* top byte, less sign bit */
+ decNumberZero (dn); /* clean target */
+ /* set the sign if negative */
+ if (decimal32Sign (d32))
+ dn->bits = DECNEG;
+
+ if (top >= 0x78)
+ { /* is a special */
+ if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
+ dn->bits |= DECINF;
+ else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
+ dn->bits |= DECNAN;
+ else
+ dn->bits |= DECSNAN;
+ msd = 0; /* no top digit */
+ }
+ else
+ { /* have a finite number */
+ uInt comb = top >> 2; /* combination field */
+ uInt exp; /* working exponent */
+
+ if (comb >= 0x18)
+ {
+ msd = 8 + (comb & 0x01);
+ exp = (comb & 0x06) << 5; /* MSBs */
+ }
+ else
+ {
+ msd = comb & 0x07;
+ exp = (comb & 0x18) << 3;
+ }
+ dn->exponent = exp + decimal32ExpCon (d32) - DECIMAL32_Bias; /* remove bias */
+ }
+
+ /* get the coefficient, unless infinite */
+ if (!(dn->bits & DECINF))
+ {
+ Int bunches = DECIMAL32_Pmax / 3; /* coefficient full bunches to convert */
+ Int odd = 0; /* assume MSD is 0 (no odd bunch) */
+ if (msd != 0)
+ { /* coefficient has leading non-0 digit */
+ /* make a copy of the decimal32, with an extra bunch which has */
+ /* the top digit ready for conversion */
+ wk = *d32; /* take a copy */
+ wk.bytes[0] = 0; /* clear all but coecon */
+ wk.bytes[1] &= 0x0f; /* .. */
+ wk.bytes[1] |= (msd << 4); /* and prefix MSD */
+ odd++; /* indicate the extra */
+ d32 = &wk; /* use the work copy */
+ }
+ decDenseUnpackCoeff (d32->bytes, sizeof (d32->bytes), dn, bunches, odd);
+ }
+ return dn;
+}
+
+/* ------------------------------------------------------------------ */
+/* to-scientific-string -- conversion to numeric string */
+/* to-engineering-string -- conversion to numeric string */
+/* */
+/* decimal32ToString(d32, string); */
+/* decimal32ToEngString(d32, string); */
+/* */
+/* d32 is the decimal32 format number to convert */
+/* string is the string where the result will be laid out */
+/* */
+/* string must be at least 24 characters */
+/* */
+/* No error is possible, and no status can be set. */
+/* ------------------------------------------------------------------ */
+char *
+decimal32ToString (const decimal32 * d32, char *string)
+{
+ decNumber dn; /* work */
+ decimal32ToNumber (d32, &dn);
+ decNumberToString (&dn, string);
+ return string;
+}
+
+char *
+decimal32ToEngString (const decimal32 * d32, char *string)
+{
+ decNumber dn; /* work */
+ decimal32ToNumber (d32, &dn);
+ decNumberToEngString (&dn, string);
+ return string;
+}
+
+/* ------------------------------------------------------------------ */
+/* to-number -- conversion from numeric string */
+/* */
+/* decimal32FromString(result, string, set); */
+/* */
+/* result is the decimal32 format number which gets the result of */
+/* the conversion */
+/* *string is the character string which should contain a valid */
+/* number (which may be a special value) */
+/* set is the context */
+/* */
+/* The context is supplied to this routine is used for error handling */
+/* (setting of status and traps) and for the rounding mode, only. */
+/* If an error occurs, the result will be a valid decimal32 NaN. */
+/* ------------------------------------------------------------------ */
+decimal32 *
+decimal32FromString (decimal32 * result, const char *string, decContext * set)
+{
+ decContext dc; /* work */
+ decNumber dn; /* .. */
+
+ decContextDefault (&dc, DEC_INIT_DECIMAL32); /* no traps, please */
+ dc.round = set->round; /* use supplied rounding */
+
+ decNumberFromString (&dn, string, &dc); /* will round if needed */
+ decimal32FromNumber (result, &dn, &dc);
+ if (dc.status != 0)
+ { /* something happened */
+ decContextSetStatus (set, dc.status); /* .. pass it on */
+ }
+ return result;
+}
+
+#if DECTRACE || DECCHECK
+/* ------------------------------------------------------------------ */
+/* decimal32Show -- display a single in hexadecimal [debug aid] */
+/* d32 -- the number to show */
+/* ------------------------------------------------------------------ */
+/* Also shows sign/cob/expconfields extracted */
+void
+decimal32Show (const decimal32 * d32)
+{
+ char buf[DECIMAL32_Bytes * 2 + 1];
+ Int i, j;
+ j = 0;
+ for (i = 0; i < DECIMAL32_Bytes; i++)
+ {
+ sprintf (&buf[j], "%02x", d32->bytes[i]);
+ j = j + 2;
+ }
+ printf (" D32> %s [S:%d Cb:%02x E:%d]\n", buf,
+ decimal32Sign (d32), decimal32Comb (d32), decimal32ExpCon (d32));
+}
+#endif
diff --git a/libdecnumber/dpd/decimal32.h b/libdecnumber/dpd/decimal32.h
new file mode 100644
index 0000000..cbe8ab4
--- /dev/null
+++ b/libdecnumber/dpd/decimal32.h
@@ -0,0 +1,120 @@
+/* Decimal 32-bit format module header for the decNumber C Library
+ Copyright (C) 2005 Free Software Foundation, Inc.
+ Contributed by IBM Corporation. Author Mike Cowlishaw.
+
+ This file is part of GCC.
+
+ 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
+ Software Foundation; either version 2, or (at your option) any later
+ version.
+
+ In addition to the permissions in the GNU General Public License,
+ the Free Software Foundation gives you unlimited permission to link
+ the compiled version of this file into combinations with other
+ programs, and to distribute those combinations without any
+ restriction coming from the use of this file. (The General Public
+ License restrictions do apply in other respects; for example, they
+ cover modification of the file, and distribution when not linked
+ into a combine executable.)
+
+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ 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
+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
+
+#if !defined(DECIMAL32)
+#define DECIMAL32
+#define DEC32NAME "decimal32" /* Short name */
+#define DEC32FULLNAME "Decimal 32-bit Number" /* Verbose name */
+#define DEC32AUTHOR "Mike Cowlishaw" /* Who to blame */
+
+ /* parameters for decimal32s */
+#define DECIMAL32_Bytes 4 /* length */
+#define DECIMAL32_Pmax 7 /* maximum precision (digits) */
+#define DECIMAL32_Emax 96 /* maximum adjusted exponent */
+#define DECIMAL32_Emin -95 /* minimum adjusted exponent */
+#define DECIMAL32_Bias 101 /* bias for the exponent */
+#define DECIMAL32_String 15 /* maximum string length, +1 */
+ /* highest biased exponent (Elimit-1) */
+#define DECIMAL32_Ehigh (DECIMAL32_Emax+DECIMAL32_Bias-DECIMAL32_Pmax+1)
+
+#ifndef DECNUMDIGITS
+#define DECNUMDIGITS DECIMAL32_Pmax /* size if not already defined */
+#endif
+#ifndef DECNUMBER
+#include "decNumber.h" /* context and number library */
+#endif
+
+ /* Decimal 32-bit type, accessible by bytes */
+typedef struct
+{
+ uint8_t bytes[DECIMAL32_Bytes]; /* decimal32: 1, 5, 6, 20 bits */
+} decimal32;
+
+ /* special values [top byte excluding sign bit; last two bits are
+ don't-care for Infinity on input, last bit don't-care for NaN] */
+#if !defined(DECIMAL_NaN)
+#define DECIMAL_NaN 0x7c /* 0 11111 00 NaN */
+#define DECIMAL_sNaN 0x7e /* 0 11111 10 sNaN */
+#define DECIMAL_Inf 0x78 /* 0 11110 00 Infinity */
+#endif
+
+ /* Macros for accessing decimal32 fields. These assume the argument
+ is a reference (pointer) to the decimal32 structure */
+ /* Get sign */
+#define decimal32Sign(d) ((unsigned)(d)->bytes[0]>>7)
+
+ /* Get combination field */
+#define decimal32Comb(d) (((d)->bytes[0] & 0x7c)>>2)
+
+ /* Get exponent continuation [does not remove bias] */
+#define decimal32ExpCon(d) ((((d)->bytes[0] & 0x03)<<4) \
+ | ((unsigned)(d)->bytes[1]>>4))
+
+ /* Set sign [this assumes sign previously 0] */
+#define decimal32SetSign(d, b) { \
+ (d)->bytes[0]|=((unsigned)(b)<<7);}
+
+ /* Clear sign */
+#define decimal32ClearSign(d) {(d)->bytes[0]&=~0x80;}
+
+ /* Flip sign */
+#define decimal32FlipSign(d) {(d)->bytes[0]^=0x80;}
+
+ /* Set exponent continuation [does not apply bias] */
+ /* This assumes range has been checked and exponent previously 0; */
+ /* type of exponent must be unsigned */
+#define decimal32SetExpCon(d, e) { \
+ (d)->bytes[0]|=(uint8_t)((e)>>4); \
+ (d)->bytes[1]|=(uint8_t)(((e)&0x0F)<<4);}
+
+ /* ------------------------------------------------------------------ */
+ /* Routines */
+ /* ------------------------------------------------------------------ */
+
+#ifdef IN_LIBGCC2
+#ifndef decimal32FromString
+#define decimal32FromString __decimal32FromString
+#define decimal32ToString __decimal32ToString
+#define decimal32ToEngString __decimal32ToEngString
+#define decimal32FromNumber __decimal32FromNumber
+#define decimal32ToNumber __decimal32ToNumber
+#endif
+#endif
+
+/* String conversions. */
+decimal32 *decimal32FromString (decimal32 *, const char *, decContext *);
+char *decimal32ToString (const decimal32 *, char *);
+char *decimal32ToEngString (const decimal32 *, char *);
+
+/* decNumber conversions. */
+decimal32 *decimal32FromNumber (decimal32 *, const decNumber *, decContext *);
+decNumber *decimal32ToNumber (const decimal32 *, decNumber *);
+
+#endif
diff --git a/libdecnumber/dpd/decimal64.c b/libdecnumber/dpd/decimal64.c
new file mode 100644
index 0000000..c1c1c7c
--- /dev/null
+++ b/libdecnumber/dpd/decimal64.c
@@ -0,0 +1,337 @@
+/* Decimal 64-bit format module for the decNumber C Library
+ Copyright (C) 2005 Free Software Foundation, Inc.
+ Contributed by IBM Corporation. Author Mike Cowlishaw.
+
+ This file is part of GCC.
+
+ 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
+ Software Foundation; either version 2, or (at your option) any later
+ version.
+
+ In addition to the permissions in the GNU General Public License,
+ the Free Software Foundation gives you unlimited permission to link
+ the compiled version of this file into combinations with other
+ programs, and to distribute those combinations without any
+ restriction coming from the use of this file. (The General Public
+ License restrictions do apply in other respects; for example, they
+ cover modification of the file, and distribution when not linked
+ into a combine executable.)
+
+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ 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
+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
+
+/* ------------------------------------------------------------------ */
+/* This module comprises the routines for decimal64 format numbers. */
+/* Conversions are supplied to and from decNumber and String. */
+/* */
+/* No arithmetic routines are included; decNumber provides these. */
+/* */
+/* Error handling is the same as decNumber (qv.). */
+/* ------------------------------------------------------------------ */
+#include <string.h> /* [for memset/memcpy] */
+#include <stdio.h> /* [for printf] */
+
+#define DECNUMDIGITS 16 /* we need decNumbers with space for 16 */
+#include "config.h"
+#include "decNumber.h" /* base number library */
+#include "decNumberLocal.h" /* decNumber local types, etc. */
+#include "decimal64.h" /* our primary include */
+#include "decUtility.h" /* utility routines */
+
+#if DECTRACE || DECCHECK
+void decimal64Show (const decimal64 *); /* for debug */
+void decNumberShow (const decNumber *); /* .. */
+#endif
+
+/* Useful macro */
+/* Clear a structure (e.g., a decNumber) */
+#define DEC_clear(d) memset(d, 0, sizeof(*d))
+
+/* ------------------------------------------------------------------ */
+/* decimal64FromNumber -- convert decNumber to decimal64 */
+/* */
+/* ds is the target decimal64 */
+/* dn is the source number (assumed valid) */
+/* set is the context, used only for reporting errors */
+/* */
+/* The set argument is used only for status reporting and for the */
+/* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */
+/* digits or an overflow is detected). If the exponent is out of the */
+/* valid range then Overflow or Underflow will be raised. */
+/* After Underflow a subnormal result is possible. */
+/* */
+/* DEC_Clamped is set if the number has to be 'folded down' to fit, */
+/* by reducing its exponent and multiplying the coefficient by a */
+/* power of ten, or if the exponent on a zero had to be clamped. */
+/* ------------------------------------------------------------------ */
+decimal64 *
+decimal64FromNumber (decimal64 * d64, const decNumber * dn, decContext * set)
+{
+ uInt status = 0; /* status accumulator */
+ Int pad = 0; /* coefficient pad digits */
+ decNumber dw; /* work */
+ decContext dc; /* .. */
+ uByte isneg = dn->bits & DECNEG; /* non-0 if original sign set */
+ uInt comb, exp; /* work */
+
+ /* If the number is finite, and has too many digits, or the exponent */
+ /* could be out of range then we reduce the number under the */
+ /* appropriate constraints */
+ if (!(dn->bits & DECSPECIAL))
+ { /* not a special value */
+ Int ae = dn->exponent + dn->digits - 1; /* adjusted exponent */
+ if (dn->digits > DECIMAL64_Pmax /* too many digits */
+ || ae > DECIMAL64_Emax /* likely overflow */
+ || ae < DECIMAL64_Emin)
+ { /* likely underflow */
+ decContextDefault (&dc, DEC_INIT_DECIMAL64); /* [no traps] */
+ dc.round = set->round; /* use supplied rounding */
+ decNumberPlus (&dw, dn, &dc); /* (round and check) */
+ /* [this changes -0 to 0, but it will be restored below] */
+ status |= dc.status; /* save status */
+ dn = &dw; /* use the work number */
+ }
+ /* [this could have pushed number to Infinity or zero, so this */
+ /* rounding must be done before we generate the decimal64] */
+ }
+
+ DEC_clear (d64); /* clean the target */
+ if (dn->bits & DECSPECIAL)
+ { /* a special value */
+ uByte top; /* work */
+ if (dn->bits & DECINF)
+ top = DECIMAL_Inf;
+ else
+ { /* sNaN or qNaN */
+ if ((*dn->lsu != 0 || dn->digits > 1) /* non-zero coefficient */
+ && (dn->digits < DECIMAL64_Pmax))
+ { /* coefficient fits */
+ decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), 0);
+ }
+ if (dn->bits & DECNAN)
+ top = DECIMAL_NaN;
+ else
+ top = DECIMAL_sNaN;
+ }
+ d64->bytes[0] = top;
+ }
+ else if (decNumberIsZero (dn))
+ { /* a zero */
+ /* set and clamp exponent */
+ if (dn->exponent < -DECIMAL64_Bias)
+ {
+ exp = 0;
+ status |= DEC_Clamped;
+ }
+ else
+ {
+ exp = dn->exponent + DECIMAL64_Bias; /* bias exponent */
+ if (exp > DECIMAL64_Ehigh)
+ { /* top clamp */
+ exp = DECIMAL64_Ehigh;
+ status |= DEC_Clamped;
+ }
+ }
+ comb = (exp >> 5) & 0x18; /* combination field */
+ d64->bytes[0] = (uByte) (comb << 2);
+ exp &= 0xff; /* remaining exponent bits */
+ decimal64SetExpCon (d64, exp);
+ }
+ else
+ { /* non-zero finite number */
+ uInt msd; /* work */
+
+ /* we have a dn that fits, but it may need to be padded */
+ exp = (uInt) (dn->exponent + DECIMAL64_Bias); /* bias exponent */
+ if (exp > DECIMAL64_Ehigh)
+ { /* fold-down case */
+ pad = exp - DECIMAL64_Ehigh;
+ exp = DECIMAL64_Ehigh; /* [to maximum] */
+ status |= DEC_Clamped;
+ }
+
+ decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), pad);
+
+ /* save and clear the top digit */
+ msd = ((unsigned) d64->bytes[1] >> 2) & 0x0f;
+ d64->bytes[1] &= 0x03;
+ /* create the combination field */
+ if (msd >= 8)
+ comb = 0x18 | (msd & 0x01) | ((exp >> 7) & 0x06);
+ else
+ comb = (msd & 0x07) | ((exp >> 5) & 0x18);
+ d64->bytes[0] = (uByte) (comb << 2);
+ exp &= 0xff; /* remaining exponent bits */
+ decimal64SetExpCon (d64, exp);
+ }
+
+ if (isneg)
+ decimal64SetSign (d64, 1);
+ if (status != 0)
+ decContextSetStatus (set, status); /* pass on status */
+
+ /*decimal64Show(d64); */
+ return d64;
+}
+
+/* ------------------------------------------------------------------ */
+/* decimal64ToNumber -- convert decimal64 to decNumber */
+/* d64 is the source decimal64 */
+/* dn is the target number, with appropriate space */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+decNumber *
+decimal64ToNumber (const decimal64 * d64, decNumber * dn)
+{
+ uInt msd; /* coefficient MSD */
+ decimal64 wk; /* working copy, if needed */
+ uInt top = d64->bytes[0] & 0x7f; /* top byte, less sign bit */
+ decNumberZero (dn); /* clean target */
+ /* set the sign if negative */
+ if (decimal64Sign (d64))
+ dn->bits = DECNEG;
+
+ if (top >= 0x78)
+ { /* is a special */
+ if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
+ dn->bits |= DECINF;
+ else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
+ dn->bits |= DECNAN;
+ else
+ dn->bits |= DECSNAN;
+ msd = 0; /* no top digit */
+ }
+ else
+ { /* have a finite number */
+ uInt comb = top >> 2; /* combination field */
+ uInt exp; /* exponent */
+
+ if (comb >= 0x18)
+ {
+ msd = 8 + (comb & 0x01);
+ exp = (comb & 0x06) << 7; /* MSBs */
+ }
+ else
+ {
+ msd = comb & 0x07;
+ exp = (comb & 0x18) << 5;
+ }
+ dn->exponent = exp + decimal64ExpCon (d64) - DECIMAL64_Bias; /* remove bias */
+ }
+
+ /* get the coefficient, unless infinite */
+ if (!(dn->bits & DECINF))
+ {
+ Int bunches = DECIMAL64_Pmax / 3; /* coefficient full bunches to convert */
+ Int odd = 0; /* assume MSD is 0 (no odd bunch) */
+ if (msd != 0)
+ { /* coefficient has leading non-0 digit */
+ /* make a copy of the decimal64, with an extra bunch which has */
+ /* the top digit ready for conversion */
+ wk = *d64; /* take a copy */
+ wk.bytes[0] = 0; /* clear all but coecon */
+ wk.bytes[1] &= 0x03; /* .. */
+ wk.bytes[1] |= (msd << 2); /* and prefix MSD */
+ odd++; /* indicate the extra */
+ d64 = &wk; /* use the work copy */
+ }
+ decDenseUnpackCoeff (d64->bytes, sizeof (d64->bytes), dn, bunches, odd);
+ }
+ return dn;
+}
+
+/* ------------------------------------------------------------------ */
+/* to-scientific-string -- conversion to numeric string */
+/* to-engineering-string -- conversion to numeric string */
+/* */
+/* decimal64ToString(d64, string); */
+/* decimal64ToEngString(d64, string); */
+/* */
+/* d64 is the decimal64 format number to convert */
+/* string is the string where the result will be laid out */
+/* */
+/* string must be at least 24 characters */
+/* */
+/* No error is possible, and no status can be set. */
+/* ------------------------------------------------------------------ */
+char *
+decimal64ToString (const decimal64 * d64, char *string)
+{
+ decNumber dn; /* work */
+ decimal64ToNumber (d64, &dn);
+ decNumberToString (&dn, string);
+ return string;
+}
+
+char *
+decimal64ToEngString (const decimal64 * d64, char *string)
+{
+ decNumber dn; /* work */
+ decimal64ToNumber (d64, &dn);
+ decNumberToEngString (&dn, string);
+ return string;
+}
+
+/* ------------------------------------------------------------------ */
+/* to-number -- conversion from numeric string */
+/* */
+/* decimal64FromString(result, string, set); */
+/* */
+/* result is the decimal64 format number which gets the result of */
+/* the conversion */
+/* *string is the character string which should contain a valid */
+/* number (which may be a special value) */
+/* set is the context */
+/* */
+/* The context is supplied to this routine is used for error handling */
+/* (setting of status and traps) and for the rounding mode, only. */
+/* If an error occurs, the result will be a valid decimal64 NaN. */
+/* ------------------------------------------------------------------ */
+decimal64 *
+decimal64FromString (decimal64 * result, const char *string, decContext * set)
+{
+ decContext dc; /* work */
+ decNumber dn; /* .. */
+
+ decContextDefault (&dc, DEC_INIT_DECIMAL64); /* no traps, please */
+ dc.round = set->round; /* use supplied rounding */
+
+ decNumberFromString (&dn, string, &dc); /* will round if needed */
+
+ decimal64FromNumber (result, &dn, &dc);
+ if (dc.status != 0)
+ { /* something happened */
+ decContextSetStatus (set, dc.status); /* .. pass it on */
+ }
+ return result;
+}
+
+#if DECTRACE || DECCHECK
+/* ------------------------------------------------------------------ */
+/* decimal64Show -- display a single in hexadecimal [debug aid] */
+/* d64 -- the number to show */
+/* ------------------------------------------------------------------ */
+/* Also shows sign/cob/expconfields extracted */
+void
+decimal64Show (const decimal64 * d64)
+{
+ char buf[DECIMAL64_Bytes * 2 + 1];
+ Int i, j;
+ j = 0;
+ for (i = 0; i < DECIMAL64_Bytes; i++)
+ {
+ sprintf (&buf[j], "%02x", d64->bytes[i]);
+ j = j + 2;
+ }
+ printf (" D64> %s [S:%d Cb:%02x E:%d]\n", buf,
+ decimal64Sign (d64), decimal64Comb (d64), decimal64ExpCon (d64));
+}
+#endif
diff --git a/libdecnumber/dpd/decimal64.h b/libdecnumber/dpd/decimal64.h
new file mode 100644
index 0000000..fb73395
--- /dev/null
+++ b/libdecnumber/dpd/decimal64.h
@@ -0,0 +1,124 @@
+/* Decimal 64-bit format module header for the decNumber C Library
+ Copyright (C) 2005 Free Software Foundation, Inc.
+ Contributed by IBM Corporation. Author Mike Cowlishaw.
+
+ This file is part of GCC.
+
+ 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
+ Software Foundation; either version 2, or (at your option) any later
+ version.
+
+ In addition to the permissions in the GNU General Public License,
+ the Free Software Foundation gives you unlimited permission to link
+ the compiled version of this file into combinations with other
+ programs, and to distribute those combinations without any
+ restriction coming from the use of this file. (The General Public
+ License restrictions do apply in other respects; for example, they
+ cover modification of the file, and distribution when not linked
+ into a combine executable.)
+
+ GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ for more details.
+
+ 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
+ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
+
+#if !defined(DECIMAL64)
+#define DECIMAL64
+#define DEC64NAME "decimal64" /* Short name */
+#define DEC64FULLNAME "Decimal 64-bit Number" /* Verbose name */
+#define DEC64AUTHOR "Mike Cowlishaw" /* Who to blame */
+
+#if defined(DECIMAL32)
+#error decimal64.h must precede decimal32.h for correct DECNUMDIGITS
+#endif
+
+ /* parameters for decimal64s */
+#define DECIMAL64_Bytes 8 /* length */
+#define DECIMAL64_Pmax 16 /* maximum precision (digits) */
+#define DECIMAL64_Emax 384 /* maximum adjusted exponent */
+#define DECIMAL64_Emin -383 /* minimum adjusted exponent */
+#define DECIMAL64_Bias 398 /* bias for the exponent */
+#define DECIMAL64_String 24 /* maximum string length, +1 */
+ /* highest biased exponent (Elimit-1) */
+#define DECIMAL64_Ehigh (DECIMAL64_Emax+DECIMAL64_Bias-DECIMAL64_Pmax+1)
+
+#ifndef DECNUMDIGITS
+#define DECNUMDIGITS DECIMAL64_Pmax /* size if not already defined */
+#endif
+#ifndef DECNUMBER
+#include "decNumber.h" /* context and number library */
+#endif
+
+ /* Decimal 64-bit type, accessible by bytes */
+typedef struct
+{
+ uint8_t bytes[DECIMAL64_Bytes]; /* decimal64: 1, 5, 8, 50 bits */
+} decimal64;
+
+ /* special values [top byte excluding sign bit; last two bits are
+ don't-care for Infinity on input, last bit don't-care for NaN] */
+#if !defined(DECIMAL_NaN)
+#define DECIMAL_NaN 0x7c /* 0 11111 00 NaN */
+#define DECIMAL_sNaN 0x7e /* 0 11111 10 sNaN */
+#define DECIMAL_Inf 0x78 /* 0 11110 00 Infinity */
+#endif
+
+ /* Macros for accessing decimal64 fields. These assume the argument
+ is a reference (pointer) to the decimal64 structure */
+ /* Get sign */
+#define decimal64Sign(d) ((unsigned)(d)->bytes[0]>>7)
+
+ /* Get combination field */
+#define decimal64Comb(d) (((d)->bytes[0] & 0x7c)>>2)
+
+ /* Get exponent continuation [does not remove bias] */
+#define decimal64ExpCon(d) ((((d)->bytes[0] & 0x03)<<6) \
+ | ((unsigned)(d)->bytes[1]>>2))
+
+ /* Set sign [this assumes sign previously 0] */
+#define decimal64SetSign(d, b) { \
+ (d)->bytes[0]|=((unsigned)(b)<<7);}
+
+ /* Clear sign */
+#define decimal64ClearSign(d) {(d)->bytes[0]&=~0x80;}
+
+ /* Flip sign */
+#define decimal64FlipSign(d) {(d)->bytes[0]^=0x80;}
+
+ /* Set exponent continuation [does not apply bias] */
+ /* This assumes range has been checked and exponent previously 0; type */
+ /* of exponent must be unsigned */
+#define decimal64SetExpCon(d, e) { \
+ (d)->bytes[0]|=(uint8_t)((e)>>6); \
+ (d)->bytes[1]|=(uint8_t)(((e)&0x3F)<<2);}
+
+ /* ------------------------------------------------------------------ */
+ /* Routines */
+ /* ------------------------------------------------------------------ */
+
+#ifdef IN_LIBGCC2
+#ifndef decimal64FromString
+#define decimal64FromString __decimal64FromString
+#define decimal64ToString __decimal64ToString
+#define decimal64ToEngString __decimal64ToEngString
+#define decimal64FromNumber __decimal64FromNumber
+#define decimal64ToNumber __decimal64ToNumber
+#endif
+#endif
+
+ /* String conversions */
+decimal64 *decimal64FromString (decimal64 *, const char *, decContext *);
+char *decimal64ToString (const decimal64 *, char *);
+char *decimal64ToEngString (const decimal64 *, char *);
+
+ /* decNumber conversions */
+decimal64 *decimal64FromNumber (decimal64 *, const decNumber *, decContext *);
+decNumber *decimal64ToNumber (const decimal64 *, decNumber *);
+
+#endif