aboutsummaryrefslogtreecommitdiff
path: root/libdecnumber/dpd
diff options
context:
space:
mode:
authorTom Musta <tommusta@gmail.com>2014-04-21 15:54:45 -0500
committerAlexander Graf <agraf@suse.de>2014-06-16 13:24:28 +0200
commit72ac97cdfc0592b567cb62582300c0d707701bb1 (patch)
tree79b91c722c95e526dab7dc8cf11f6a246dd5c849 /libdecnumber/dpd
parent9d1c128341df7a303571f172d986291b4f3ed9ee (diff)
libdecnumber: Introduce libdecnumber Code
Add files from the libdecnumber decimal floating point library to QEMU. The libdecnumber library was originally part of GCC and contains code that is useful in emulating the PowerPC decimal floating point (DFP) instructions. This particular copy of the source comes from GCC 4.3 and is licensed at GPLv2+. Signed-off-by: Tom Musta <tommusta@gmail.com> Signed-off-by: Alexander Graf <agraf@suse.de>
Diffstat (limited to 'libdecnumber/dpd')
-rw-r--r--libdecnumber/dpd/decimal128.c566
-rw-r--r--libdecnumber/dpd/decimal128Local.h42
-rw-r--r--libdecnumber/dpd/decimal32.c491
-rw-r--r--libdecnumber/dpd/decimal64.c852
4 files changed, 1951 insertions, 0 deletions
diff --git a/libdecnumber/dpd/decimal128.c b/libdecnumber/dpd/decimal128.c
new file mode 100644
index 0000000000..54191aab5c
--- /dev/null
+++ b/libdecnumber/dpd/decimal128.c
@@ -0,0 +1,566 @@
+/* Decimal 128-bit format module for the decNumber C Library.
+ Copyright (C) 2005, 2007 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. */
+
+/* ------------------------------------------------------------------ */
+/* Decimal 128-bit format module */
+/* ------------------------------------------------------------------ */
+/* This module comprises the routines for decimal128 format numbers. */
+/* Conversions are supplied to and from decNumber and String. */
+/* */
+/* This is used when decNumber provides operations, either for all */
+/* operations or as a proxy between decNumber and decSingle. */
+/* */
+/* Error handling is the same as decNumber (qv.). */
+/* ------------------------------------------------------------------ */
+#include <string.h> /* [for memset/memcpy] */
+#include <stdio.h> /* [for printf] */
+
+#include "dconfig.h" /* GCC definitions */
+#define DECNUMDIGITS 34 /* make decNumbers with space for 34 */
+#include "decNumber.h" /* base number library */
+#include "decNumberLocal.h" /* decNumber local types, etc. */
+#include "decimal128.h" /* our primary include */
+
+/* Utility routines and tables [in decimal64.c] */
+extern const uInt COMBEXP[32], COMBMSD[32];
+extern const uShort DPD2BIN[1024];
+extern const uShort BIN2DPD[1000]; /* [not used] */
+extern const uByte BIN2CHAR[4001];
+
+extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
+extern void decDigitsToDPD(const decNumber *, uInt *, Int);
+
+#if DECTRACE || DECCHECK
+void decimal128Show(const decimal128 *); /* for debug */
+extern 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 ae; /* adjusted exponent */
+ decNumber dw; /* work */
+ decContext dc; /* .. */
+ uInt *pu; /* .. */
+ uInt comb, exp; /* .. */
+ uInt targar[4]={0,0,0,0}; /* target 128-bit */
+ #define targhi targar[3] /* name the word with the sign */
+ #define targmh targar[2] /* name the words */
+ #define targml targar[1] /* .. */
+ #define targlo targar[0] /* .. */
+
+ /* If the number has too many digits, or the exponent could be */
+ /* out of range then reduce the number under the appropriate */
+ /* constraints. This could push the number to Infinity or zero, */
+ /* so this check and rounding must be done before generating the */
+ /* decimal128] */
+ ae=dn->exponent+dn->digits-1; /* [0 if special] */
+ 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, so enforce the sign...] */
+ dw.bits|=dn->bits&DECNEG;
+ status=dc.status; /* save status */
+ dn=&dw; /* use the work number */
+ } /* maybe out of range */
+
+ if (dn->bits&DECSPECIAL) { /* a special value */
+ if (dn->bits&DECINF) targhi=DECIMAL_Inf<<24;
+ else { /* sNaN or qNaN */
+ if ((*dn->lsu!=0 || dn->digits>1) /* non-zero coefficient */
+ && (dn->digits<DECIMAL128_Pmax)) { /* coefficient fits */
+ decDigitsToDPD(dn, targar, 0);
+ }
+ if (dn->bits&DECNAN) targhi|=DECIMAL_NaN<<24;
+ else targhi|=DECIMAL_sNaN<<24;
+ } /* a NaN */
+ } /* special */
+
+ else { /* is finite */
+ if (decNumberIsZero(dn)) { /* is a zero */
+ /* set and clamp exponent */
+ if (dn->exponent<-DECIMAL128_Bias) {
+ exp=0; /* low clamp */
+ 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; /* msd=0, exp top 2 bits .. */
+ }
+ else { /* non-zero finite number */
+ uInt msd; /* work */
+ Int pad=0; /* coefficient pad digits */
+
+ /* the dn is known to fit, 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;
+ }
+
+ /* [fastpath for common case is not a win, here] */
+ decDigitsToDPD(dn, targar, pad);
+ /* save and clear the top digit */
+ msd=targhi>>14;
+ targhi&=0x00003fff;
+
+ /* create the combination field */
+ if (msd>=8) comb=0x18 | ((exp>>11) & 0x06) | (msd & 0x01);
+ else comb=((exp>>9) & 0x18) | msd;
+ }
+ targhi|=comb<<26; /* add combination field .. */
+ targhi|=(exp&0xfff)<<14; /* .. and exponent continuation */
+ } /* finite */
+
+ if (dn->bits&DECNEG) targhi|=0x80000000; /* add sign bit */
+
+ /* now write to storage; this is endian */
+ pu=(uInt *)d128->bytes; /* overlay */
+ if (DECLITEND) {
+ pu[0]=targlo; /* directly store the low int */
+ pu[1]=targml; /* then the mid-low */
+ pu[2]=targmh; /* then the mid-high */
+ pu[3]=targhi; /* then the high int */
+ }
+ else {
+ pu[0]=targhi; /* directly store the high int */
+ pu[1]=targmh; /* then the mid-high */
+ pu[2]=targml; /* then the mid-low */
+ pu[3]=targlo; /* then the low int */
+ }
+
+ if (status!=0) decContextSetStatus(set, status); /* pass on status */
+ /* decimal128Show(d128); */
+ return d128;
+ } /* decimal128FromNumber */
+
+/* ------------------------------------------------------------------ */
+/* 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 */
+ uInt exp; /* exponent top two bits */
+ uInt comb; /* combination field */
+ const uInt *pu; /* work */
+ Int need; /* .. */
+ uInt sourar[4]; /* source 128-bit */
+ #define sourhi sourar[3] /* name the word with the sign */
+ #define sourmh sourar[2] /* and the mid-high word */
+ #define sourml sourar[1] /* and the mod-low word */
+ #define sourlo sourar[0] /* and the lowest word */
+
+ /* load source from storage; this is endian */
+ pu=(const uInt *)d128->bytes; /* overlay */
+ if (DECLITEND) {
+ sourlo=pu[0]; /* directly load the low int */
+ sourml=pu[1]; /* then the mid-low */
+ sourmh=pu[2]; /* then the mid-high */
+ sourhi=pu[3]; /* then the high int */
+ }
+ else {
+ sourhi=pu[0]; /* directly load the high int */
+ sourmh=pu[1]; /* then the mid-high */
+ sourml=pu[2]; /* then the mid-low */
+ sourlo=pu[3]; /* then the low int */
+ }
+
+ comb=(sourhi>>26)&0x1f; /* combination field */
+
+ decNumberZero(dn); /* clean number */
+ if (sourhi&0x80000000) dn->bits=DECNEG; /* set sign if negative */
+
+ msd=COMBMSD[comb]; /* decode the combination field */
+ exp=COMBEXP[comb]; /* .. */
+
+ if (exp==3) { /* is a special */
+ if (msd==0) {
+ dn->bits|=DECINF;
+ return dn; /* no coefficient needed */
+ }
+ else if (sourhi&0x02000000) dn->bits|=DECSNAN;
+ else dn->bits|=DECNAN;
+ msd=0; /* no top digit */
+ }
+ else { /* is a finite number */
+ dn->exponent=(exp<<12)+((sourhi>>14)&0xfff)-DECIMAL128_Bias; /* unbiased */
+ }
+
+ /* get the coefficient */
+ sourhi&=0x00003fff; /* clean coefficient continuation */
+ if (msd) { /* non-zero msd */
+ sourhi|=msd<<14; /* prefix to coefficient */
+ need=12; /* process 12 declets */
+ }
+ else { /* msd=0 */
+ if (sourhi) need=11; /* declets to process */
+ else if (sourmh) need=10;
+ else if (sourml) need=7;
+ else if (sourlo) need=4;
+ else return dn; /* easy: coefficient is 0 */
+ } /*msd=0 */
+
+ decDigitsFromDPD(dn, sourar, need); /* process declets */
+ /* decNumberShow(dn); */
+ return dn;
+ } /* decimal128ToNumber */
+
+/* ------------------------------------------------------------------ */
+/* 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 * decimal128ToEngString(const decimal128 *d128, char *string){
+ decNumber dn; /* work */
+ decimal128ToNumber(d128, &dn);
+ decNumberToEngString(&dn, string);
+ return string;
+ } /* decimal128ToEngString */
+
+char * decimal128ToString(const decimal128 *d128, char *string){
+ uInt msd; /* coefficient MSD */
+ Int exp; /* exponent top two bits or full */
+ uInt comb; /* combination field */
+ char *cstart; /* coefficient start */
+ char *c; /* output pointer in string */
+ const uInt *pu; /* work */
+ char *s, *t; /* .. (source, target) */
+ Int dpd; /* .. */
+ Int pre, e; /* .. */
+ const uByte *u; /* .. */
+
+ uInt sourar[4]; /* source 128-bit */
+ #define sourhi sourar[3] /* name the word with the sign */
+ #define sourmh sourar[2] /* and the mid-high word */
+ #define sourml sourar[1] /* and the mod-low word */
+ #define sourlo sourar[0] /* and the lowest word */
+
+ /* load source from storage; this is endian */
+ pu=(const uInt *)d128->bytes; /* overlay */
+ if (DECLITEND) {
+ sourlo=pu[0]; /* directly load the low int */
+ sourml=pu[1]; /* then the mid-low */
+ sourmh=pu[2]; /* then the mid-high */
+ sourhi=pu[3]; /* then the high int */
+ }
+ else {
+ sourhi=pu[0]; /* directly load the high int */
+ sourmh=pu[1]; /* then the mid-high */
+ sourml=pu[2]; /* then the mid-low */
+ sourlo=pu[3]; /* then the low int */
+ }
+
+ c=string; /* where result will go */
+ if (((Int)sourhi)<0) *c++='-'; /* handle sign */
+
+ comb=(sourhi>>26)&0x1f; /* combination field */
+ msd=COMBMSD[comb]; /* decode the combination field */
+ exp=COMBEXP[comb]; /* .. */
+
+ if (exp==3) {
+ if (msd==0) { /* infinity */
+ strcpy(c, "Inf");
+ strcpy(c+3, "inity");
+ return string; /* easy */
+ }
+ if (sourhi&0x02000000) *c++='s'; /* sNaN */
+ strcpy(c, "NaN"); /* complete word */
+ c+=3; /* step past */
+ if (sourlo==0 && sourml==0 && sourmh==0
+ && (sourhi&0x0003ffff)==0) return string; /* zero payload */
+ /* otherwise drop through to add integer; set correct exp */
+ exp=0; msd=0; /* setup for following code */
+ }
+ else exp=(exp<<12)+((sourhi>>14)&0xfff)-DECIMAL128_Bias; /* unbiased */
+
+ /* convert 34 digits of significand to characters */
+ cstart=c; /* save start of coefficient */
+ if (msd) *c++='0'+(char)msd; /* non-zero most significant digit */
+
+ /* Now decode the declets. After extracting each one, it is */
+ /* decoded to binary and then to a 4-char sequence by table lookup; */
+ /* the 4-chars are a 1-char length (significant digits, except 000 */
+ /* has length 0). This allows us to left-align the first declet */
+ /* with non-zero content, then remaining ones are full 3-char */
+ /* length. We use fixed-length memcpys because variable-length */
+ /* causes a subroutine call in GCC. (These are length 4 for speed */
+ /* and are safe because the array has an extra terminator byte.) */
+ #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \
+ if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \
+ else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;}
+ dpd=(sourhi>>4)&0x3ff; /* declet 1 */
+ dpd2char;
+ dpd=((sourhi&0xf)<<6) | (sourmh>>26); /* declet 2 */
+ dpd2char;
+ dpd=(sourmh>>16)&0x3ff; /* declet 3 */
+ dpd2char;
+ dpd=(sourmh>>6)&0x3ff; /* declet 4 */
+ dpd2char;
+ dpd=((sourmh&0x3f)<<4) | (sourml>>28); /* declet 5 */
+ dpd2char;
+ dpd=(sourml>>18)&0x3ff; /* declet 6 */
+ dpd2char;
+ dpd=(sourml>>8)&0x3ff; /* declet 7 */
+ dpd2char;
+ dpd=((sourml&0xff)<<2) | (sourlo>>30); /* declet 8 */
+ dpd2char;
+ dpd=(sourlo>>20)&0x3ff; /* declet 9 */
+ dpd2char;
+ dpd=(sourlo>>10)&0x3ff; /* declet 10 */
+ dpd2char;
+ dpd=(sourlo)&0x3ff; /* declet 11 */
+ dpd2char;
+
+ if (c==cstart) *c++='0'; /* all zeros -- make 0 */
+
+ if (exp==0) { /* integer or NaN case -- easy */
+ *c='\0'; /* terminate */
+ return string;
+ }
+
+ /* non-0 exponent */
+ e=0; /* assume no E */
+ pre=c-cstart+exp;
+ /* [here, pre-exp is the digits count (==1 for zero)] */
+ if (exp>0 || pre<-5) { /* need exponential form */
+ e=pre-1; /* calculate E value */
+ pre=1; /* assume one digit before '.' */
+ } /* exponential form */
+
+ /* modify the coefficient, adding 0s, '.', and E+nn as needed */
+ s=c-1; /* source (LSD) */
+ if (pre>0) { /* ddd.ddd (plain), perhaps with E */
+ char *dotat=cstart+pre;
+ if (dotat<c) { /* if embedded dot needed... */
+ t=c; /* target */
+ for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */
+ *t='.'; /* insert the dot */
+ c++; /* length increased by one */
+ }
+
+ /* finally add the E-part, if needed; it will never be 0, and has */
+ /* a maximum length of 4 digits */
+ if (e!=0) {
+ *c++='E'; /* starts with E */
+ *c++='+'; /* assume positive */
+ if (e<0) {
+ *(c-1)='-'; /* oops, need '-' */
+ e=-e; /* uInt, please */
+ }
+ if (e<1000) { /* 3 (or fewer) digits case */
+ u=&BIN2CHAR[e*4]; /* -> length byte */
+ memcpy(c, u+4-*u, 4); /* copy fixed 4 characters [is safe] */
+ c+=*u; /* bump pointer appropriately */
+ }
+ else { /* 4-digits */
+ Int thou=((e>>3)*1049)>>17; /* e/1000 */
+ Int rem=e-(1000*thou); /* e%1000 */
+ *c++='0'+(char)thou;
+ u=&BIN2CHAR[rem*4]; /* -> length byte */
+ memcpy(c, u+1, 4); /* copy fixed 3+1 characters [is safe] */
+ c+=3; /* bump pointer, always 3 digits */
+ }
+ }
+ *c='\0'; /* add terminator */
+ /*printf("res %s\n", string); */
+ return string;
+ } /* pre>0 */
+
+ /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
+ t=c+1-pre;
+ *(t+1)='\0'; /* can add terminator now */
+ for (; s>=cstart; s--, t--) *t=*s; /* shift whole coefficient right */
+ c=cstart;
+ *c++='0'; /* always starts with 0. */
+ *c++='.';
+ for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */
+ /*printf("res %s\n", string); */
+ return string;
+ } /* decimal128ToString */
+
+/* ------------------------------------------------------------------ */
+/* 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;
+ } /* decimal128FromString */
+
+/* ------------------------------------------------------------------ */
+/* decimal128IsCanonical -- test whether encoding is canonical */
+/* d128 is the source decimal128 */
+/* returns 1 if the encoding of d128 is canonical, 0 otherwise */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+uint32_t decimal128IsCanonical(const decimal128 *d128) {
+ decNumber dn; /* work */
+ decimal128 canon; /* .. */
+ decContext dc; /* .. */
+ decContextDefault(&dc, DEC_INIT_DECIMAL128);
+ decimal128ToNumber(d128, &dn);
+ decimal128FromNumber(&canon, &dn, &dc);/* canon will now be canonical */
+ return memcmp(d128, &canon, DECIMAL128_Bytes)==0;
+ } /* decimal128IsCanonical */
+
+/* ------------------------------------------------------------------ */
+/* decimal128Canonical -- copy an encoding, ensuring it is canonical */
+/* d128 is the source decimal128 */
+/* result is the target (may be the same decimal128) */
+/* returns result */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+decimal128 * decimal128Canonical(decimal128 *result, const decimal128 *d128) {
+ decNumber dn; /* work */
+ decContext dc; /* .. */
+ decContextDefault(&dc, DEC_INIT_DECIMAL128);
+ decimal128ToNumber(d128, &dn);
+ decimal128FromNumber(result, &dn, &dc);/* result will now be canonical */
+ return result;
+ } /* decimal128Canonical */
+
+#if DECTRACE || DECCHECK
+/* Macros for accessing decimal128 fields. These assume the argument
+ is a reference (pointer) to the decimal128 structure, and the
+ decimal128 is in network byte order (big-endian) */
+/* 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);}
+
+/* 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);}
+
+/* ------------------------------------------------------------------ */
+/* decimal128Show -- display a decimal128 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=0;
+
+ if (DECLITEND) {
+ for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
+ sprintf(&buf[j], "%02x", d128->bytes[15-i]);
+ }
+ printf(" D128> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
+ d128->bytes[15]>>7, (d128->bytes[15]>>2)&0x1f,
+ ((d128->bytes[15]&0x3)<<10)|(d128->bytes[14]<<2)|
+ (d128->bytes[13]>>6));
+ }
+ else {
+ for (i=0; i<DECIMAL128_Bytes; i++, j+=2) {
+ sprintf(&buf[j], "%02x", d128->bytes[i]);
+ }
+ printf(" D128> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
+ decimal128Sign(d128), decimal128Comb(d128),
+ decimal128ExpCon(d128));
+ }
+ } /* decimal128Show */
+#endif
diff --git a/libdecnumber/dpd/decimal128Local.h b/libdecnumber/dpd/decimal128Local.h
new file mode 100644
index 0000000000..1963678cdd
--- /dev/null
+++ b/libdecnumber/dpd/decimal128Local.h
@@ -0,0 +1,42 @@
+/* Local definitions for use with the decNumber C Library.
+ Copyright (C) 2007, 2009 Free Software Foundation, Inc.
+
+ 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 3, or (at your option) any later
+ version.
+
+ 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.
+
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+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;
+see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+<http://www.gnu.org/licenses/>. */
+
+#if !defined(DECIMAL128LOCAL)
+
+/* The compiler needs sign manipulation functions for decimal128 which
+ are not part of the decNumber package. */
+
+/* Set sign; this assumes the sign was previously zero. */
+#define decimal128SetSign(d,b) \
+ { (d)->bytes[WORDS_BIGENDIAN ? 0 : 15] |= ((unsigned) (b) << 7); }
+
+/* Clear sign. */
+#define decimal128ClearSign(d) \
+ { (d)->bytes[WORDS_BIGENDIAN ? 0 : 15] &= ~0x80; }
+
+/* Flip sign. */
+#define decimal128FlipSign(d) \
+ { (d)->bytes[WORDS_BIGENDIAN ? 0 : 15] ^= 0x80; }
+
+#endif
diff --git a/libdecnumber/dpd/decimal32.c b/libdecnumber/dpd/decimal32.c
new file mode 100644
index 0000000000..d8e3f59781
--- /dev/null
+++ b/libdecnumber/dpd/decimal32.c
@@ -0,0 +1,491 @@
+/* Decimal 32-bit format module for the decNumber C Library.
+ Copyright (C) 2005, 2007 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. */
+
+/* ------------------------------------------------------------------ */
+/* Decimal 32-bit format module */
+/* ------------------------------------------------------------------ */
+/* This module comprises the routines for decimal32 format numbers. */
+/* Conversions are supplied to and from decNumber and String. */
+/* */
+/* This is used when decNumber provides operations, either for all */
+/* operations or as a proxy between decNumber and decSingle. */
+/* */
+/* Error handling is the same as decNumber (qv.). */
+/* ------------------------------------------------------------------ */
+#include <string.h> /* [for memset/memcpy] */
+#include <stdio.h> /* [for printf] */
+
+#include "dconfig.h" /* GCC definitions */
+#define DECNUMDIGITS 7 /* make decNumbers with space for 7 */
+#include "decNumber.h" /* base number library */
+#include "decNumberLocal.h" /* decNumber local types, etc. */
+#include "decimal32.h" /* our primary include */
+
+/* Utility tables and routines [in decimal64.c] */
+extern const uInt COMBEXP[32], COMBMSD[32];
+extern const uShort DPD2BIN[1024];
+extern const uShort BIN2DPD[1000];
+extern const uByte BIN2CHAR[4001];
+
+extern void decDigitsToDPD(const decNumber *, uInt *, Int);
+extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
+
+#if DECTRACE || DECCHECK
+void decimal32Show(const decimal32 *); /* for debug */
+extern 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 ae; /* adjusted exponent */
+ decNumber dw; /* work */
+ decContext dc; /* .. */
+ uInt *pu; /* .. */
+ uInt comb, exp; /* .. */
+ uInt targ=0; /* target 32-bit */
+
+ /* If the number has too many digits, or the exponent could be */
+ /* out of range then reduce the number under the appropriate */
+ /* constraints. This could push the number to Infinity or zero, */
+ /* so this check and rounding must be done before generating the */
+ /* decimal32] */
+ ae=dn->exponent+dn->digits-1; /* [0 if special] */
+ 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, so enforce the sign...] */
+ dw.bits|=dn->bits&DECNEG;
+ status=dc.status; /* save status */
+ dn=&dw; /* use the work number */
+ } /* maybe out of range */
+
+ if (dn->bits&DECSPECIAL) { /* a special value */
+ if (dn->bits&DECINF) targ=DECIMAL_Inf<<24;
+ else { /* sNaN or qNaN */
+ if ((*dn->lsu!=0 || dn->digits>1) /* non-zero coefficient */
+ && (dn->digits<DECIMAL32_Pmax)) { /* coefficient fits */
+ decDigitsToDPD(dn, &targ, 0);
+ }
+ if (dn->bits&DECNAN) targ|=DECIMAL_NaN<<24;
+ else targ|=DECIMAL_sNaN<<24;
+ } /* a NaN */
+ } /* special */
+
+ else { /* is finite */
+ if (decNumberIsZero(dn)) { /* is a zero */
+ /* set and clamp exponent */
+ if (dn->exponent<-DECIMAL32_Bias) {
+ exp=0; /* low clamp */
+ 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; /* msd=0, exp top 2 bits .. */
+ }
+ else { /* non-zero finite number */
+ uInt msd; /* work */
+ Int pad=0; /* coefficient pad digits */
+
+ /* the dn is known to fit, 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;
+ }
+
+ /* fastpath common case */
+ if (DECDPUN==3 && pad==0) {
+ targ=BIN2DPD[dn->lsu[0]];
+ if (dn->digits>3) targ|=(uInt)(BIN2DPD[dn->lsu[1]])<<10;
+ msd=(dn->digits==7 ? dn->lsu[2] : 0);
+ }
+ else { /* general case */
+ decDigitsToDPD(dn, &targ, pad);
+ /* save and clear the top digit */
+ msd=targ>>20;
+ targ&=0x000fffff;
+ }
+
+ /* create the combination field */
+ if (msd>=8) comb=0x18 | ((exp>>5) & 0x06) | (msd & 0x01);
+ else comb=((exp>>3) & 0x18) | msd;
+ }
+ targ|=comb<<26; /* add combination field .. */
+ targ|=(exp&0x3f)<<20; /* .. and exponent continuation */
+ } /* finite */
+
+ if (dn->bits&DECNEG) targ|=0x80000000; /* add sign bit */
+
+ /* now write to storage; this is endian */
+ pu=(uInt *)d32->bytes; /* overlay */
+ *pu=targ; /* directly store the int */
+
+ if (status!=0) decContextSetStatus(set, status); /* pass on status */
+ /* decimal32Show(d32); */
+ return d32;
+ } /* decimal32FromNumber */
+
+/* ------------------------------------------------------------------ */
+/* 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 */
+ uInt exp; /* exponent top two bits */
+ uInt comb; /* combination field */
+ uInt sour; /* source 32-bit */
+ const uInt *pu; /* work */
+
+ /* load source from storage; this is endian */
+ pu=(const uInt *)d32->bytes; /* overlay */
+ sour=*pu; /* directly load the int */
+
+ comb=(sour>>26)&0x1f; /* combination field */
+
+ decNumberZero(dn); /* clean number */
+ if (sour&0x80000000) dn->bits=DECNEG; /* set sign if negative */
+
+ msd=COMBMSD[comb]; /* decode the combination field */
+ exp=COMBEXP[comb]; /* .. */
+
+ if (exp==3) { /* is a special */
+ if (msd==0) {
+ dn->bits|=DECINF;
+ return dn; /* no coefficient needed */
+ }
+ else if (sour&0x02000000) dn->bits|=DECSNAN;
+ else dn->bits|=DECNAN;
+ msd=0; /* no top digit */
+ }
+ else { /* is a finite number */
+ dn->exponent=(exp<<6)+((sour>>20)&0x3f)-DECIMAL32_Bias; /* unbiased */
+ }
+
+ /* get the coefficient */
+ sour&=0x000fffff; /* clean coefficient continuation */
+ if (msd) { /* non-zero msd */
+ sour|=msd<<20; /* prefix to coefficient */
+ decDigitsFromDPD(dn, &sour, 3); /* process 3 declets */
+ return dn;
+ }
+ /* msd=0 */
+ if (!sour) return dn; /* easy: coefficient is 0 */
+ if (sour&0x000ffc00) /* need 2 declets? */
+ decDigitsFromDPD(dn, &sour, 2); /* process 2 declets */
+ else
+ decDigitsFromDPD(dn, &sour, 1); /* process 1 declet */
+ return dn;
+ } /* decimal32ToNumber */
+
+/* ------------------------------------------------------------------ */
+/* 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 * decimal32ToEngString(const decimal32 *d32, char *string){
+ decNumber dn; /* work */
+ decimal32ToNumber(d32, &dn);
+ decNumberToEngString(&dn, string);
+ return string;
+ } /* decimal32ToEngString */
+
+char * decimal32ToString(const decimal32 *d32, char *string){
+ uInt msd; /* coefficient MSD */
+ Int exp; /* exponent top two bits or full */
+ uInt comb; /* combination field */
+ char *cstart; /* coefficient start */
+ char *c; /* output pointer in string */
+ const uInt *pu; /* work */
+ const uByte *u; /* .. */
+ char *s, *t; /* .. (source, target) */
+ Int dpd; /* .. */
+ Int pre, e; /* .. */
+ uInt sour; /* source 32-bit */
+
+ /* load source from storage; this is endian */
+ pu=(const uInt *)d32->bytes; /* overlay */
+ sour=*pu; /* directly load the int */
+
+ c=string; /* where result will go */
+ if (((Int)sour)<0) *c++='-'; /* handle sign */
+
+ comb=(sour>>26)&0x1f; /* combination field */
+ msd=COMBMSD[comb]; /* decode the combination field */
+ exp=COMBEXP[comb]; /* .. */
+
+ if (exp==3) {
+ if (msd==0) { /* infinity */
+ strcpy(c, "Inf");
+ strcpy(c+3, "inity");
+ return string; /* easy */
+ }
+ if (sour&0x02000000) *c++='s'; /* sNaN */
+ strcpy(c, "NaN"); /* complete word */
+ c+=3; /* step past */
+ if ((sour&0x000fffff)==0) return string; /* zero payload */
+ /* otherwise drop through to add integer; set correct exp */
+ exp=0; msd=0; /* setup for following code */
+ }
+ else exp=(exp<<6)+((sour>>20)&0x3f)-DECIMAL32_Bias; /* unbiased */
+
+ /* convert 7 digits of significand to characters */
+ cstart=c; /* save start of coefficient */
+ if (msd) *c++='0'+(char)msd; /* non-zero most significant digit */
+
+ /* Now decode the declets. After extracting each one, it is */
+ /* decoded to binary and then to a 4-char sequence by table lookup; */
+ /* the 4-chars are a 1-char length (significant digits, except 000 */
+ /* has length 0). This allows us to left-align the first declet */
+ /* with non-zero content, then remaining ones are full 3-char */
+ /* length. We use fixed-length memcpys because variable-length */
+ /* causes a subroutine call in GCC. (These are length 4 for speed */
+ /* and are safe because the array has an extra terminator byte.) */
+ #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \
+ if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \
+ else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;}
+
+ dpd=(sour>>10)&0x3ff; /* declet 1 */
+ dpd2char;
+ dpd=(sour)&0x3ff; /* declet 2 */
+ dpd2char;
+
+ if (c==cstart) *c++='0'; /* all zeros -- make 0 */
+
+ if (exp==0) { /* integer or NaN case -- easy */
+ *c='\0'; /* terminate */
+ return string;
+ }
+
+ /* non-0 exponent */
+ e=0; /* assume no E */
+ pre=c-cstart+exp;
+ /* [here, pre-exp is the digits count (==1 for zero)] */
+ if (exp>0 || pre<-5) { /* need exponential form */
+ e=pre-1; /* calculate E value */
+ pre=1; /* assume one digit before '.' */
+ } /* exponential form */
+
+ /* modify the coefficient, adding 0s, '.', and E+nn as needed */
+ s=c-1; /* source (LSD) */
+ if (pre>0) { /* ddd.ddd (plain), perhaps with E */
+ char *dotat=cstart+pre;
+ if (dotat<c) { /* if embedded dot needed... */
+ t=c; /* target */
+ for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */
+ *t='.'; /* insert the dot */
+ c++; /* length increased by one */
+ }
+
+ /* finally add the E-part, if needed; it will never be 0, and has */
+ /* a maximum length of 3 digits (E-101 case) */
+ if (e!=0) {
+ *c++='E'; /* starts with E */
+ *c++='+'; /* assume positive */
+ if (e<0) {
+ *(c-1)='-'; /* oops, need '-' */
+ e=-e; /* uInt, please */
+ }
+ u=&BIN2CHAR[e*4]; /* -> length byte */
+ memcpy(c, u+4-*u, 4); /* copy fixed 4 characters [is safe] */
+ c+=*u; /* bump pointer appropriately */
+ }
+ *c='\0'; /* add terminator */
+ /*printf("res %s\n", string); */
+ return string;
+ } /* pre>0 */
+
+ /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
+ t=c+1-pre;
+ *(t+1)='\0'; /* can add terminator now */
+ for (; s>=cstart; s--, t--) *t=*s; /* shift whole coefficient right */
+ c=cstart;
+ *c++='0'; /* always starts with 0. */
+ *c++='.';
+ for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */
+ /*printf("res %s\n", string); */
+ return string;
+ } /* decimal32ToString */
+
+/* ------------------------------------------------------------------ */
+/* 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;
+ } /* decimal32FromString */
+
+/* ------------------------------------------------------------------ */
+/* decimal32IsCanonical -- test whether encoding is canonical */
+/* d32 is the source decimal32 */
+/* returns 1 if the encoding of d32 is canonical, 0 otherwise */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+uint32_t decimal32IsCanonical(const decimal32 *d32) {
+ decNumber dn; /* work */
+ decimal32 canon; /* .. */
+ decContext dc; /* .. */
+ decContextDefault(&dc, DEC_INIT_DECIMAL32);
+ decimal32ToNumber(d32, &dn);
+ decimal32FromNumber(&canon, &dn, &dc);/* canon will now be canonical */
+ return memcmp(d32, &canon, DECIMAL32_Bytes)==0;
+ } /* decimal32IsCanonical */
+
+/* ------------------------------------------------------------------ */
+/* decimal32Canonical -- copy an encoding, ensuring it is canonical */
+/* d32 is the source decimal32 */
+/* result is the target (may be the same decimal32) */
+/* returns result */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+decimal32 * decimal32Canonical(decimal32 *result, const decimal32 *d32) {
+ decNumber dn; /* work */
+ decContext dc; /* .. */
+ decContextDefault(&dc, DEC_INIT_DECIMAL32);
+ decimal32ToNumber(d32, &dn);
+ decimal32FromNumber(result, &dn, &dc);/* result will now be canonical */
+ return result;
+ } /* decimal32Canonical */
+
+#if DECTRACE || DECCHECK
+/* Macros for accessing decimal32 fields. These assume the argument
+ is a reference (pointer) to the decimal32 structure, and the
+ decimal32 is in network byte order (big-endian) */
+/* 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);}
+
+/* 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);}
+
+/* ------------------------------------------------------------------ */
+/* decimal32Show -- display a decimal32 in hexadecimal [debug aid] */
+/* d32 -- the number to show */
+/* ------------------------------------------------------------------ */
+/* Also shows sign/cob/expconfields extracted - valid bigendian only */
+void decimal32Show(const decimal32 *d32) {
+ char buf[DECIMAL32_Bytes*2+1];
+ Int i, j=0;
+
+ if (DECLITEND) {
+ for (i=0; i<DECIMAL32_Bytes; i++, j+=2) {
+ sprintf(&buf[j], "%02x", d32->bytes[3-i]);
+ }
+ printf(" D32> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
+ d32->bytes[3]>>7, (d32->bytes[3]>>2)&0x1f,
+ ((d32->bytes[3]&0x3)<<4)| (d32->bytes[2]>>4));
+ }
+ else {
+ for (i=0; i<DECIMAL32_Bytes; i++, j+=2) {
+ sprintf(&buf[j], "%02x", d32->bytes[i]);
+ }
+ printf(" D32> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
+ decimal32Sign(d32), decimal32Comb(d32), decimal32ExpCon(d32));
+ }
+ } /* decimal32Show */
+#endif
diff --git a/libdecnumber/dpd/decimal64.c b/libdecnumber/dpd/decimal64.c
new file mode 100644
index 0000000000..474eb7cf8a
--- /dev/null
+++ b/libdecnumber/dpd/decimal64.c
@@ -0,0 +1,852 @@
+/* Decimal 64-bit format module for the decNumber C Library.
+ Copyright (C) 2005, 2007 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. */
+
+/* ------------------------------------------------------------------ */
+/* Decimal 64-bit format module */
+/* ------------------------------------------------------------------ */
+/* This module comprises the routines for decimal64 format numbers. */
+/* Conversions are supplied to and from decNumber and String. */
+/* */
+/* This is used when decNumber provides operations, either for all */
+/* operations or as a proxy between decNumber and decSingle. */
+/* */
+/* Error handling is the same as decNumber (qv.). */
+/* ------------------------------------------------------------------ */
+#include <string.h> /* [for memset/memcpy] */
+#include <stdio.h> /* [for printf] */
+
+#include "dconfig.h" /* GCC definitions */
+#define DECNUMDIGITS 16 /* make decNumbers with space for 16 */
+#include "decNumber.h" /* base number library */
+#include "decNumberLocal.h" /* decNumber local types, etc. */
+#include "decimal64.h" /* our primary include */
+
+/* Utility routines and tables [in decimal64.c]; externs for C++ */
+extern const uInt COMBEXP[32], COMBMSD[32];
+extern const uShort DPD2BIN[1024];
+extern const uShort BIN2DPD[1000];
+extern const uByte BIN2CHAR[4001];
+
+extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
+extern void decDigitsToDPD(const decNumber *, uInt *, Int);
+
+#if DECTRACE || DECCHECK
+void decimal64Show(const decimal64 *); /* for debug */
+extern void decNumberShow(const decNumber *); /* .. */
+#endif
+
+/* Useful macro */
+/* Clear a structure (e.g., a decNumber) */
+#define DEC_clear(d) memset(d, 0, sizeof(*d))
+
+/* define and include the tables to use for conversions */
+#define DEC_BIN2CHAR 1
+#define DEC_DPD2BIN 1
+#define DEC_BIN2DPD 1 /* used for all sizes */
+#include "decDPD.h" /* lookup tables */
+
+/* ------------------------------------------------------------------ */
+/* 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 ae; /* adjusted exponent */
+ decNumber dw; /* work */
+ decContext dc; /* .. */
+ uInt *pu; /* .. */
+ uInt comb, exp; /* .. */
+ uInt targar[2]={0, 0}; /* target 64-bit */
+ #define targhi targar[1] /* name the word with the sign */
+ #define targlo targar[0] /* and the other */
+
+ /* If the number has too many digits, or the exponent could be */
+ /* out of range then reduce the number under the appropriate */
+ /* constraints. This could push the number to Infinity or zero, */
+ /* so this check and rounding must be done before generating the */
+ /* decimal64] */
+ ae=dn->exponent+dn->digits-1; /* [0 if special] */
+ 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, so enforce the sign...] */
+ dw.bits|=dn->bits&DECNEG;
+ status=dc.status; /* save status */
+ dn=&dw; /* use the work number */
+ } /* maybe out of range */
+
+ if (dn->bits&DECSPECIAL) { /* a special value */
+ if (dn->bits&DECINF) targhi=DECIMAL_Inf<<24;
+ else { /* sNaN or qNaN */
+ if ((*dn->lsu!=0 || dn->digits>1) /* non-zero coefficient */
+ && (dn->digits<DECIMAL64_Pmax)) { /* coefficient fits */
+ decDigitsToDPD(dn, targar, 0);
+ }
+ if (dn->bits&DECNAN) targhi|=DECIMAL_NaN<<24;
+ else targhi|=DECIMAL_sNaN<<24;
+ } /* a NaN */
+ } /* special */
+
+ else { /* is finite */
+ if (decNumberIsZero(dn)) { /* is a zero */
+ /* set and clamp exponent */
+ if (dn->exponent<-DECIMAL64_Bias) {
+ exp=0; /* low clamp */
+ 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; /* msd=0, exp top 2 bits .. */
+ }
+ else { /* non-zero finite number */
+ uInt msd; /* work */
+ Int pad=0; /* coefficient pad digits */
+
+ /* the dn is known to fit, 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;
+ }
+
+ /* fastpath common case */
+ if (DECDPUN==3 && pad==0) {
+ uInt dpd[6]={0,0,0,0,0,0};
+ uInt i;
+ Int d=dn->digits;
+ for (i=0; d>0; i++, d-=3) dpd[i]=BIN2DPD[dn->lsu[i]];
+ targlo =dpd[0];
+ targlo|=dpd[1]<<10;
+ targlo|=dpd[2]<<20;
+ if (dn->digits>6) {
+ targlo|=dpd[3]<<30;
+ targhi =dpd[3]>>2;
+ targhi|=dpd[4]<<8;
+ }
+ msd=dpd[5]; /* [did not really need conversion] */
+ }
+ else { /* general case */
+ decDigitsToDPD(dn, targar, pad);
+ /* save and clear the top digit */
+ msd=targhi>>18;
+ targhi&=0x0003ffff;
+ }
+
+ /* create the combination field */
+ if (msd>=8) comb=0x18 | ((exp>>7) & 0x06) | (msd & 0x01);
+ else comb=((exp>>5) & 0x18) | msd;
+ }
+ targhi|=comb<<26; /* add combination field .. */
+ targhi|=(exp&0xff)<<18; /* .. and exponent continuation */
+ } /* finite */
+
+ if (dn->bits&DECNEG) targhi|=0x80000000; /* add sign bit */
+
+ /* now write to storage; this is now always endian */
+ pu=(uInt *)d64->bytes; /* overlay */
+ if (DECLITEND) {
+ pu[0]=targar[0]; /* directly store the low int */
+ pu[1]=targar[1]; /* then the high int */
+ }
+ else {
+ pu[0]=targar[1]; /* directly store the high int */
+ pu[1]=targar[0]; /* then the low int */
+ }
+
+ if (status!=0) decContextSetStatus(set, status); /* pass on status */
+ /* decimal64Show(d64); */
+ return d64;
+ } /* decimal64FromNumber */
+
+/* ------------------------------------------------------------------ */
+/* 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 */
+ uInt exp; /* exponent top two bits */
+ uInt comb; /* combination field */
+ const uInt *pu; /* work */
+ Int need; /* .. */
+ uInt sourar[2]; /* source 64-bit */
+ #define sourhi sourar[1] /* name the word with the sign */
+ #define sourlo sourar[0] /* and the lower word */
+
+ /* load source from storage; this is endian */
+ pu=(const uInt *)d64->bytes; /* overlay */
+ if (DECLITEND) {
+ sourlo=pu[0]; /* directly load the low int */
+ sourhi=pu[1]; /* then the high int */
+ }
+ else {
+ sourhi=pu[0]; /* directly load the high int */
+ sourlo=pu[1]; /* then the low int */
+ }
+
+ comb=(sourhi>>26)&0x1f; /* combination field */
+
+ decNumberZero(dn); /* clean number */
+ if (sourhi&0x80000000) dn->bits=DECNEG; /* set sign if negative */
+
+ msd=COMBMSD[comb]; /* decode the combination field */
+ exp=COMBEXP[comb]; /* .. */
+
+ if (exp==3) { /* is a special */
+ if (msd==0) {
+ dn->bits|=DECINF;
+ return dn; /* no coefficient needed */
+ }
+ else if (sourhi&0x02000000) dn->bits|=DECSNAN;
+ else dn->bits|=DECNAN;
+ msd=0; /* no top digit */
+ }
+ else { /* is a finite number */
+ dn->exponent=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias; /* unbiased */
+ }
+
+ /* get the coefficient */
+ sourhi&=0x0003ffff; /* clean coefficient continuation */
+ if (msd) { /* non-zero msd */
+ sourhi|=msd<<18; /* prefix to coefficient */
+ need=6; /* process 6 declets */
+ }
+ else { /* msd=0 */
+ if (!sourhi) { /* top word 0 */
+ if (!sourlo) return dn; /* easy: coefficient is 0 */
+ need=3; /* process at least 3 declets */
+ if (sourlo&0xc0000000) need++; /* process 4 declets */
+ /* [could reduce some more, here] */
+ }
+ else { /* some bits in top word, msd=0 */
+ need=4; /* process at least 4 declets */
+ if (sourhi&0x0003ff00) need++; /* top declet!=0, process 5 */
+ }
+ } /*msd=0 */
+
+ decDigitsFromDPD(dn, sourar, need); /* process declets */
+ return dn;
+ } /* decimal64ToNumber */
+
+
+/* ------------------------------------------------------------------ */
+/* 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 * decimal64ToEngString(const decimal64 *d64, char *string){
+ decNumber dn; /* work */
+ decimal64ToNumber(d64, &dn);
+ decNumberToEngString(&dn, string);
+ return string;
+ } /* decimal64ToEngString */
+
+char * decimal64ToString(const decimal64 *d64, char *string){
+ uInt msd; /* coefficient MSD */
+ Int exp; /* exponent top two bits or full */
+ uInt comb; /* combination field */
+ char *cstart; /* coefficient start */
+ char *c; /* output pointer in string */
+ const uInt *pu; /* work */
+ char *s, *t; /* .. (source, target) */
+ Int dpd; /* .. */
+ Int pre, e; /* .. */
+ const uByte *u; /* .. */
+
+ uInt sourar[2]; /* source 64-bit */
+ #define sourhi sourar[1] /* name the word with the sign */
+ #define sourlo sourar[0] /* and the lower word */
+
+ /* load source from storage; this is endian */
+ pu=(const uInt *)d64->bytes; /* overlay */
+ if (DECLITEND) {
+ sourlo=pu[0]; /* directly load the low int */
+ sourhi=pu[1]; /* then the high int */
+ }
+ else {
+ sourhi=pu[0]; /* directly load the high int */
+ sourlo=pu[1]; /* then the low int */
+ }
+
+ c=string; /* where result will go */
+ if (((Int)sourhi)<0) *c++='-'; /* handle sign */
+
+ comb=(sourhi>>26)&0x1f; /* combination field */
+ msd=COMBMSD[comb]; /* decode the combination field */
+ exp=COMBEXP[comb]; /* .. */
+
+ if (exp==3) {
+ if (msd==0) { /* infinity */
+ strcpy(c, "Inf");
+ strcpy(c+3, "inity");
+ return string; /* easy */
+ }
+ if (sourhi&0x02000000) *c++='s'; /* sNaN */
+ strcpy(c, "NaN"); /* complete word */
+ c+=3; /* step past */
+ if (sourlo==0 && (sourhi&0x0003ffff)==0) return string; /* zero payload */
+ /* otherwise drop through to add integer; set correct exp */
+ exp=0; msd=0; /* setup for following code */
+ }
+ else exp=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias;
+
+ /* convert 16 digits of significand to characters */
+ cstart=c; /* save start of coefficient */
+ if (msd) *c++='0'+(char)msd; /* non-zero most significant digit */
+
+ /* Now decode the declets. After extracting each one, it is */
+ /* decoded to binary and then to a 4-char sequence by table lookup; */
+ /* the 4-chars are a 1-char length (significant digits, except 000 */
+ /* has length 0). This allows us to left-align the first declet */
+ /* with non-zero content, then remaining ones are full 3-char */
+ /* length. We use fixed-length memcpys because variable-length */
+ /* causes a subroutine call in GCC. (These are length 4 for speed */
+ /* and are safe because the array has an extra terminator byte.) */
+ #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \
+ if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \
+ else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;}
+
+ dpd=(sourhi>>8)&0x3ff; /* declet 1 */
+ dpd2char;
+ dpd=((sourhi&0xff)<<2) | (sourlo>>30); /* declet 2 */
+ dpd2char;
+ dpd=(sourlo>>20)&0x3ff; /* declet 3 */
+ dpd2char;
+ dpd=(sourlo>>10)&0x3ff; /* declet 4 */
+ dpd2char;
+ dpd=(sourlo)&0x3ff; /* declet 5 */
+ dpd2char;
+
+ if (c==cstart) *c++='0'; /* all zeros -- make 0 */
+
+ if (exp==0) { /* integer or NaN case -- easy */
+ *c='\0'; /* terminate */
+ return string;
+ }
+
+ /* non-0 exponent */
+ e=0; /* assume no E */
+ pre=c-cstart+exp;
+ /* [here, pre-exp is the digits count (==1 for zero)] */
+ if (exp>0 || pre<-5) { /* need exponential form */
+ e=pre-1; /* calculate E value */
+ pre=1; /* assume one digit before '.' */
+ } /* exponential form */
+
+ /* modify the coefficient, adding 0s, '.', and E+nn as needed */
+ s=c-1; /* source (LSD) */
+ if (pre>0) { /* ddd.ddd (plain), perhaps with E */
+ char *dotat=cstart+pre;
+ if (dotat<c) { /* if embedded dot needed... */
+ t=c; /* target */
+ for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */
+ *t='.'; /* insert the dot */
+ c++; /* length increased by one */
+ }
+
+ /* finally add the E-part, if needed; it will never be 0, and has */
+ /* a maximum length of 3 digits */
+ if (e!=0) {
+ *c++='E'; /* starts with E */
+ *c++='+'; /* assume positive */
+ if (e<0) {
+ *(c-1)='-'; /* oops, need '-' */
+ e=-e; /* uInt, please */
+ }
+ u=&BIN2CHAR[e*4]; /* -> length byte */
+ memcpy(c, u+4-*u, 4); /* copy fixed 4 characters [is safe] */
+ c+=*u; /* bump pointer appropriately */
+ }
+ *c='\0'; /* add terminator */
+ /*printf("res %s\n", string); */
+ return string;
+ } /* pre>0 */
+
+ /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
+ t=c+1-pre;
+ *(t+1)='\0'; /* can add terminator now */
+ for (; s>=cstart; s--, t--) *t=*s; /* shift whole coefficient right */
+ c=cstart;
+ *c++='0'; /* always starts with 0. */
+ *c++='.';
+ for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */
+ /*printf("res %s\n", string); */
+ return string;
+ } /* decimal64ToString */
+
+/* ------------------------------------------------------------------ */
+/* 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;
+ } /* decimal64FromString */
+
+/* ------------------------------------------------------------------ */
+/* decimal64IsCanonical -- test whether encoding is canonical */
+/* d64 is the source decimal64 */
+/* returns 1 if the encoding of d64 is canonical, 0 otherwise */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+uint32_t decimal64IsCanonical(const decimal64 *d64) {
+ decNumber dn; /* work */
+ decimal64 canon; /* .. */
+ decContext dc; /* .. */
+ decContextDefault(&dc, DEC_INIT_DECIMAL64);
+ decimal64ToNumber(d64, &dn);
+ decimal64FromNumber(&canon, &dn, &dc);/* canon will now be canonical */
+ return memcmp(d64, &canon, DECIMAL64_Bytes)==0;
+ } /* decimal64IsCanonical */
+
+/* ------------------------------------------------------------------ */
+/* decimal64Canonical -- copy an encoding, ensuring it is canonical */
+/* d64 is the source decimal64 */
+/* result is the target (may be the same decimal64) */
+/* returns result */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+decimal64 * decimal64Canonical(decimal64 *result, const decimal64 *d64) {
+ decNumber dn; /* work */
+ decContext dc; /* .. */
+ decContextDefault(&dc, DEC_INIT_DECIMAL64);
+ decimal64ToNumber(d64, &dn);
+ decimal64FromNumber(result, &dn, &dc);/* result will now be canonical */
+ return result;
+ } /* decimal64Canonical */
+
+#if DECTRACE || DECCHECK
+/* Macros for accessing decimal64 fields. These assume the
+ argument is a reference (pointer) to the decimal64 structure,
+ and the decimal64 is in network byte order (big-endian) */
+/* 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);}
+
+/* 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);}
+
+/* ------------------------------------------------------------------ */
+/* decimal64Show -- display a decimal64 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=0;
+
+ if (DECLITEND) {
+ for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
+ sprintf(&buf[j], "%02x", d64->bytes[7-i]);
+ }
+ printf(" D64> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
+ d64->bytes[7]>>7, (d64->bytes[7]>>2)&0x1f,
+ ((d64->bytes[7]&0x3)<<6)| (d64->bytes[6]>>2));
+ }
+ else { /* big-endian */
+ for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
+ sprintf(&buf[j], "%02x", d64->bytes[i]);
+ }
+ printf(" D64> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
+ decimal64Sign(d64), decimal64Comb(d64), decimal64ExpCon(d64));
+ }
+ } /* decimal64Show */
+#endif
+
+/* ================================================================== */
+/* Shared utility routines and tables */
+/* ================================================================== */
+/* define and include the conversion tables to use for shared code */
+#if DECDPUN==3
+ #define DEC_DPD2BIN 1
+#else
+ #define DEC_DPD2BCD 1
+#endif
+#include "decDPD.h" /* lookup tables */
+
+/* The maximum number of decNumberUnits needed for a working copy of */
+/* 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 */
+/* is used. decimal128.h must not be included in this module, so, as */
+/* a very special case, that number is defined as a literal here. */
+#define DECMAX754 34
+#define DECMAXUNITS ((DECMAX754+DECDPUN-1)/DECDPUN)
+
+/* ------------------------------------------------------------------ */
+/* Combination field lookup tables (uInts to save measurable work) */
+/* */
+/* COMBEXP - 2-bit most-significant-bits of exponent */
+/* [11 if an Infinity or NaN] */
+/* COMBMSD - 4-bit most-significant-digit */
+/* [0=Infinity, 1=NaN if COMBEXP=11] */
+/* */
+/* Both are indexed by the 5-bit combination field (0-31) */
+/* ------------------------------------------------------------------ */
+const uInt COMBEXP[32]={0, 0, 0, 0, 0, 0, 0, 0,
+ 1, 1, 1, 1, 1, 1, 1, 1,
+ 2, 2, 2, 2, 2, 2, 2, 2,
+ 0, 0, 1, 1, 2, 2, 3, 3};
+const uInt COMBMSD[32]={0, 1, 2, 3, 4, 5, 6, 7,
+ 0, 1, 2, 3, 4, 5, 6, 7,
+ 0, 1, 2, 3, 4, 5, 6, 7,
+ 8, 9, 8, 9, 8, 9, 0, 1};
+
+/* ------------------------------------------------------------------ */
+/* decDigitsToDPD -- pack coefficient into DPD form */
+/* */
+/* dn is the source number (assumed valid, max DECMAX754 digits) */
+/* targ is 1, 2, or 4-element uInt array, which the caller must */
+/* have cleared to zeros */
+/* shift is the number of 0 digits to add on the right (normally 0) */
+/* */
+/* The coefficient must be known small enough to fit. The full */
+/* coefficient is copied, including the leading 'odd' digit. This */
+/* digit is retrieved and packed into the combination field by the */
+/* caller. */
+/* */
+/* The target uInts are altered only as necessary to receive the */
+/* digits of the decNumber. When more than one uInt is needed, they */
+/* are filled from left to right (that is, the uInt at offset 0 will */
+/* end up with the least-significant digits). */
+/* */
+/* shift is used for 'fold-down' padding. */
+/* */
+/* No error is possible. */
+/* ------------------------------------------------------------------ */
+#if DECDPUN<=4
+/* Constant multipliers for divide-by-power-of five using reciprocal */
+/* multiply, after removing powers of 2 by shifting, and final shift */
+/* of 17 [we only need up to **4] */
+static const uInt multies[]={131073, 26215, 5243, 1049, 210};
+/* QUOT10 -- macro to return the quotient of unit u divided by 10**n */
+#define QUOT10(u, n) ((((uInt)(u)>>(n))*multies[n])>>17)
+#endif
+void decDigitsToDPD(const decNumber *dn, uInt *targ, Int shift) {
+ Int cut; /* work */
+ Int n; /* output bunch counter */
+ Int digits=dn->digits; /* digit countdown */
+ uInt dpd; /* densely packed decimal value */
+ uInt bin; /* binary value 0-999 */
+ uInt *uout=targ; /* -> current output uInt */
+ uInt uoff=0; /* -> current output offset [from right] */
+ const Unit *inu=dn->lsu; /* -> current input unit */
+ Unit uar[DECMAXUNITS]; /* working copy of units, iff shifted */
+ #if DECDPUN!=3 /* not fast path */
+ Unit in; /* current unit */
+ #endif
+
+ if (shift!=0) { /* shift towards most significant required */
+ /* shift the units array to the left by pad digits and copy */
+ /* [this code is a special case of decShiftToMost, which could */
+ /* be used instead if exposed and the array were copied first] */
+ const Unit *source; /* .. */
+ Unit *target, *first; /* .. */
+ uInt next=0; /* work */
+
+ source=dn->lsu+D2U(digits)-1; /* where msu comes from */
+ target=uar+D2U(digits)-1+D2U(shift);/* where upper part of first cut goes */
+ cut=DECDPUN-MSUDIGITS(shift); /* where to slice */
+ if (cut==0) { /* unit-boundary case */
+ for (; source>=dn->lsu; source--, target--) *target=*source;
+ }
+ else {
+ first=uar+D2U(digits+shift)-1; /* where msu will end up */
+ for (; source>=dn->lsu; source--, target--) {
+ /* split the source Unit and accumulate remainder for next */
+ #if DECDPUN<=4
+ uInt quot=QUOT10(*source, cut);
+ uInt rem=*source-quot*DECPOWERS[cut];
+ next+=quot;
+ #else
+ uInt rem=*source%DECPOWERS[cut];
+ next+=*source/DECPOWERS[cut];
+ #endif
+ if (target<=first) *target=(Unit)next; /* write to target iff valid */
+ next=rem*DECPOWERS[DECDPUN-cut]; /* save remainder for next Unit */
+ }
+ } /* shift-move */
+ /* propagate remainder to one below and clear the rest */
+ for (; target>=uar; target--) {
+ *target=(Unit)next;
+ next=0;
+ }
+ digits+=shift; /* add count (shift) of zeros added */
+ inu=uar; /* use units in working array */
+ }
+
+ /* now densely pack the coefficient into DPD declets */
+
+ #if DECDPUN!=3 /* not fast path */
+ in=*inu; /* current unit */
+ cut=0; /* at lowest digit */
+ bin=0; /* [keep compiler quiet] */
+ #endif
+
+ for(n=0; digits>0; n++) { /* each output bunch */
+ #if DECDPUN==3 /* fast path, 3-at-a-time */
+ bin=*inu; /* 3 digits ready for convert */
+ digits-=3; /* [may go negative] */
+ inu++; /* may need another */
+
+ #else /* must collect digit-by-digit */
+ Unit dig; /* current digit */
+ Int j; /* digit-in-declet count */
+ for (j=0; j<3; j++) {
+ #if DECDPUN<=4
+ Unit temp=(Unit)((uInt)(in*6554)>>16);
+ dig=(Unit)(in-X10(temp));
+ in=temp;
+ #else
+ dig=in%10;
+ in=in/10;
+ #endif
+ if (j==0) bin=dig;
+ else if (j==1) bin+=X10(dig);
+ else /* j==2 */ bin+=X100(dig);
+ digits--;
+ if (digits==0) break; /* [also protects *inu below] */
+ cut++;
+ if (cut==DECDPUN) {inu++; in=*inu; cut=0;}
+ }
+ #endif
+ /* here there are 3 digits in bin, or have used all input digits */
+
+ dpd=BIN2DPD[bin];
+
+ /* write declet to uInt array */
+ *uout|=dpd<<uoff;
+ uoff+=10;
+ if (uoff<32) continue; /* no uInt boundary cross */
+ uout++;
+ uoff-=32;
+ *uout|=dpd>>(10-uoff); /* collect top bits */
+ } /* n declets */
+ return;
+ } /* decDigitsToDPD */
+
+/* ------------------------------------------------------------------ */
+/* decDigitsFromDPD -- unpack a format's coefficient */
+/* */
+/* dn is the target number, with 7, 16, or 34-digit space. */
+/* sour is a 1, 2, or 4-element uInt array containing only declets */
+/* declets is the number of (right-aligned) declets in sour to */
+/* be processed. This may be 1 more than the obvious number in */
+/* a format, as any top digit is prefixed to the coefficient */
+/* continuation field. It also may be as small as 1, as the */
+/* caller may pre-process leading zero declets. */
+/* */
+/* When doing the 'extra declet' case care is taken to avoid writing */
+/* extra digits when there are leading zeros, as these could overflow */
+/* the units array when DECDPUN is not 3. */
+/* */
+/* The target uInts are used only as necessary to process declets */
+/* declets into the decNumber. When more than one uInt is needed, */
+/* they are used from left to right (that is, the uInt at offset 0 */
+/* provides the least-significant digits). */
+/* */
+/* dn->digits is set, but not the sign or exponent. */
+/* No error is possible [the redundant 888 codes are allowed]. */
+/* ------------------------------------------------------------------ */
+void decDigitsFromDPD(decNumber *dn, const uInt *sour, Int declets) {
+
+ uInt dpd; /* collector for 10 bits */
+ Int n; /* counter */
+ Unit *uout=dn->lsu; /* -> current output unit */
+ Unit *last=uout; /* will be unit containing msd */
+ const uInt *uin=sour; /* -> current input uInt */
+ uInt uoff=0; /* -> current input offset [from right] */
+
+ #if DECDPUN!=3
+ uInt bcd; /* BCD result */
+ uInt nibble; /* work */
+ Unit out=0; /* accumulator */
+ Int cut=0; /* power of ten in current unit */
+ #endif
+ #if DECDPUN>4
+ uInt const *pow; /* work */
+ #endif
+
+ /* Expand the densely-packed integer, right to left */
+ for (n=declets-1; n>=0; n--) { /* count down declets of 10 bits */
+ dpd=*uin>>uoff;
+ uoff+=10;
+ if (uoff>32) { /* crossed uInt boundary */
+ uin++;
+ uoff-=32;
+ dpd|=*uin<<(10-uoff); /* get waiting bits */
+ }
+ dpd&=0x3ff; /* clear uninteresting bits */
+
+ #if DECDPUN==3
+ if (dpd==0) *uout=0;
+ else {
+ *uout=DPD2BIN[dpd]; /* convert 10 bits to binary 0-999 */
+ last=uout; /* record most significant unit */
+ }
+ uout++;
+ } /* n */
+
+ #else /* DECDPUN!=3 */
+ if (dpd==0) { /* fastpath [e.g., leading zeros] */
+ /* write out three 0 digits (nibbles); out may have digit(s) */
+ cut++;
+ if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
+ if (n==0) break; /* [as below, works even if MSD=0] */
+ cut++;
+ if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
+ cut++;
+ if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
+ continue;
+ }
+
+ bcd=DPD2BCD[dpd]; /* convert 10 bits to 12 bits BCD */
+
+ /* now accumulate the 3 BCD nibbles into units */
+ nibble=bcd & 0x00f;
+ if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
+ cut++;
+ if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
+ bcd>>=4;
+
+ /* if this is the last declet and the remaining nibbles in bcd */
+ /* are 00 then process no more nibbles, because this could be */
+ /* the 'odd' MSD declet and writing any more Units would then */
+ /* overflow the unit array */
+ if (n==0 && !bcd) break;
+
+ nibble=bcd & 0x00f;
+ if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
+ cut++;
+ if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
+ bcd>>=4;
+
+ nibble=bcd & 0x00f;
+ if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
+ cut++;
+ if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
+ } /* n */
+ if (cut!=0) { /* some more left over */
+ *uout=out; /* write out final unit */
+ if (out) last=uout; /* and note if non-zero */
+ }
+ #endif
+
+ /* here, last points to the most significant unit with digits; */
+ /* inspect it to get the final digits count -- this is essentially */
+ /* the same code as decGetDigits in decNumber.c */
+ dn->digits=(last-dn->lsu)*DECDPUN+1; /* floor of digits, plus */
+ /* must be at least 1 digit */
+ #if DECDPUN>1
+ if (*last<10) return; /* common odd digit or 0 */
+ dn->digits++; /* must be 2 at least */
+ #if DECDPUN>2
+ if (*last<100) return; /* 10-99 */
+ dn->digits++; /* must be 3 at least */
+ #if DECDPUN>3
+ if (*last<1000) return; /* 100-999 */
+ dn->digits++; /* must be 4 at least */
+ #if DECDPUN>4
+ for (pow=&DECPOWERS[4]; *last>=*pow; pow++) dn->digits++;
+ #endif
+ #endif
+ #endif
+ #endif
+ return;
+ } /*decDigitsFromDPD */