1*72ac97cdSTom Musta /* Decimal 64-bit format module for the decNumber C Library. 2*72ac97cdSTom Musta Copyright (C) 2005, 2007 Free Software Foundation, Inc. 3*72ac97cdSTom Musta Contributed by IBM Corporation. Author Mike Cowlishaw. 4*72ac97cdSTom Musta 5*72ac97cdSTom Musta This file is part of GCC. 6*72ac97cdSTom Musta 7*72ac97cdSTom Musta GCC is free software; you can redistribute it and/or modify it under 8*72ac97cdSTom Musta the terms of the GNU General Public License as published by the Free 9*72ac97cdSTom Musta Software Foundation; either version 2, or (at your option) any later 10*72ac97cdSTom Musta version. 11*72ac97cdSTom Musta 12*72ac97cdSTom Musta In addition to the permissions in the GNU General Public License, 13*72ac97cdSTom Musta the Free Software Foundation gives you unlimited permission to link 14*72ac97cdSTom Musta the compiled version of this file into combinations with other 15*72ac97cdSTom Musta programs, and to distribute those combinations without any 16*72ac97cdSTom Musta restriction coming from the use of this file. (The General Public 17*72ac97cdSTom Musta License restrictions do apply in other respects; for example, they 18*72ac97cdSTom Musta cover modification of the file, and distribution when not linked 19*72ac97cdSTom Musta into a combine executable.) 20*72ac97cdSTom Musta 21*72ac97cdSTom Musta GCC is distributed in the hope that it will be useful, but WITHOUT ANY 22*72ac97cdSTom Musta WARRANTY; without even the implied warranty of MERCHANTABILITY or 23*72ac97cdSTom Musta FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 24*72ac97cdSTom Musta for more details. 25*72ac97cdSTom Musta 26*72ac97cdSTom Musta You should have received a copy of the GNU General Public License 27*72ac97cdSTom Musta along with GCC; see the file COPYING. If not, write to the Free 28*72ac97cdSTom Musta Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 29*72ac97cdSTom Musta 02110-1301, USA. */ 30*72ac97cdSTom Musta 31*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 32*72ac97cdSTom Musta /* Decimal 64-bit format module */ 33*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 34*72ac97cdSTom Musta /* This module comprises the routines for decimal64 format numbers. */ 35*72ac97cdSTom Musta /* Conversions are supplied to and from decNumber and String. */ 36*72ac97cdSTom Musta /* */ 37*72ac97cdSTom Musta /* This is used when decNumber provides operations, either for all */ 38*72ac97cdSTom Musta /* operations or as a proxy between decNumber and decSingle. */ 39*72ac97cdSTom Musta /* */ 40*72ac97cdSTom Musta /* Error handling is the same as decNumber (qv.). */ 41*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 42*72ac97cdSTom Musta #include <string.h> /* [for memset/memcpy] */ 43*72ac97cdSTom Musta #include <stdio.h> /* [for printf] */ 44*72ac97cdSTom Musta 45*72ac97cdSTom Musta #include "dconfig.h" /* GCC definitions */ 46*72ac97cdSTom Musta #define DECNUMDIGITS 16 /* make decNumbers with space for 16 */ 47*72ac97cdSTom Musta #include "decNumber.h" /* base number library */ 48*72ac97cdSTom Musta #include "decNumberLocal.h" /* decNumber local types, etc. */ 49*72ac97cdSTom Musta #include "decimal64.h" /* our primary include */ 50*72ac97cdSTom Musta 51*72ac97cdSTom Musta /* Utility routines and tables [in decimal64.c]; externs for C++ */ 52*72ac97cdSTom Musta extern const uInt COMBEXP[32], COMBMSD[32]; 53*72ac97cdSTom Musta extern const uShort DPD2BIN[1024]; 54*72ac97cdSTom Musta extern const uShort BIN2DPD[1000]; 55*72ac97cdSTom Musta extern const uByte BIN2CHAR[4001]; 56*72ac97cdSTom Musta 57*72ac97cdSTom Musta extern void decDigitsFromDPD(decNumber *, const uInt *, Int); 58*72ac97cdSTom Musta extern void decDigitsToDPD(const decNumber *, uInt *, Int); 59*72ac97cdSTom Musta 60*72ac97cdSTom Musta #if DECTRACE || DECCHECK 61*72ac97cdSTom Musta void decimal64Show(const decimal64 *); /* for debug */ 62*72ac97cdSTom Musta extern void decNumberShow(const decNumber *); /* .. */ 63*72ac97cdSTom Musta #endif 64*72ac97cdSTom Musta 65*72ac97cdSTom Musta /* Useful macro */ 66*72ac97cdSTom Musta /* Clear a structure (e.g., a decNumber) */ 67*72ac97cdSTom Musta #define DEC_clear(d) memset(d, 0, sizeof(*d)) 68*72ac97cdSTom Musta 69*72ac97cdSTom Musta /* define and include the tables to use for conversions */ 70*72ac97cdSTom Musta #define DEC_BIN2CHAR 1 71*72ac97cdSTom Musta #define DEC_DPD2BIN 1 72*72ac97cdSTom Musta #define DEC_BIN2DPD 1 /* used for all sizes */ 73*72ac97cdSTom Musta #include "decDPD.h" /* lookup tables */ 74*72ac97cdSTom Musta 75*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 76*72ac97cdSTom Musta /* decimal64FromNumber -- convert decNumber to decimal64 */ 77*72ac97cdSTom Musta /* */ 78*72ac97cdSTom Musta /* ds is the target decimal64 */ 79*72ac97cdSTom Musta /* dn is the source number (assumed valid) */ 80*72ac97cdSTom Musta /* set is the context, used only for reporting errors */ 81*72ac97cdSTom Musta /* */ 82*72ac97cdSTom Musta /* The set argument is used only for status reporting and for the */ 83*72ac97cdSTom Musta /* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */ 84*72ac97cdSTom Musta /* digits or an overflow is detected). If the exponent is out of the */ 85*72ac97cdSTom Musta /* valid range then Overflow or Underflow will be raised. */ 86*72ac97cdSTom Musta /* After Underflow a subnormal result is possible. */ 87*72ac97cdSTom Musta /* */ 88*72ac97cdSTom Musta /* DEC_Clamped is set if the number has to be 'folded down' to fit, */ 89*72ac97cdSTom Musta /* by reducing its exponent and multiplying the coefficient by a */ 90*72ac97cdSTom Musta /* power of ten, or if the exponent on a zero had to be clamped. */ 91*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 92*72ac97cdSTom Musta decimal64 * decimal64FromNumber(decimal64 *d64, const decNumber *dn, 93*72ac97cdSTom Musta decContext *set) { 94*72ac97cdSTom Musta uInt status=0; /* status accumulator */ 95*72ac97cdSTom Musta Int ae; /* adjusted exponent */ 96*72ac97cdSTom Musta decNumber dw; /* work */ 97*72ac97cdSTom Musta decContext dc; /* .. */ 98*72ac97cdSTom Musta uInt *pu; /* .. */ 99*72ac97cdSTom Musta uInt comb, exp; /* .. */ 100*72ac97cdSTom Musta uInt targar[2]={0, 0}; /* target 64-bit */ 101*72ac97cdSTom Musta #define targhi targar[1] /* name the word with the sign */ 102*72ac97cdSTom Musta #define targlo targar[0] /* and the other */ 103*72ac97cdSTom Musta 104*72ac97cdSTom Musta /* If the number has too many digits, or the exponent could be */ 105*72ac97cdSTom Musta /* out of range then reduce the number under the appropriate */ 106*72ac97cdSTom Musta /* constraints. This could push the number to Infinity or zero, */ 107*72ac97cdSTom Musta /* so this check and rounding must be done before generating the */ 108*72ac97cdSTom Musta /* decimal64] */ 109*72ac97cdSTom Musta ae=dn->exponent+dn->digits-1; /* [0 if special] */ 110*72ac97cdSTom Musta if (dn->digits>DECIMAL64_Pmax /* too many digits */ 111*72ac97cdSTom Musta || ae>DECIMAL64_Emax /* likely overflow */ 112*72ac97cdSTom Musta || ae<DECIMAL64_Emin) { /* likely underflow */ 113*72ac97cdSTom Musta decContextDefault(&dc, DEC_INIT_DECIMAL64); /* [no traps] */ 114*72ac97cdSTom Musta dc.round=set->round; /* use supplied rounding */ 115*72ac97cdSTom Musta decNumberPlus(&dw, dn, &dc); /* (round and check) */ 116*72ac97cdSTom Musta /* [this changes -0 to 0, so enforce the sign...] */ 117*72ac97cdSTom Musta dw.bits|=dn->bits&DECNEG; 118*72ac97cdSTom Musta status=dc.status; /* save status */ 119*72ac97cdSTom Musta dn=&dw; /* use the work number */ 120*72ac97cdSTom Musta } /* maybe out of range */ 121*72ac97cdSTom Musta 122*72ac97cdSTom Musta if (dn->bits&DECSPECIAL) { /* a special value */ 123*72ac97cdSTom Musta if (dn->bits&DECINF) targhi=DECIMAL_Inf<<24; 124*72ac97cdSTom Musta else { /* sNaN or qNaN */ 125*72ac97cdSTom Musta if ((*dn->lsu!=0 || dn->digits>1) /* non-zero coefficient */ 126*72ac97cdSTom Musta && (dn->digits<DECIMAL64_Pmax)) { /* coefficient fits */ 127*72ac97cdSTom Musta decDigitsToDPD(dn, targar, 0); 128*72ac97cdSTom Musta } 129*72ac97cdSTom Musta if (dn->bits&DECNAN) targhi|=DECIMAL_NaN<<24; 130*72ac97cdSTom Musta else targhi|=DECIMAL_sNaN<<24; 131*72ac97cdSTom Musta } /* a NaN */ 132*72ac97cdSTom Musta } /* special */ 133*72ac97cdSTom Musta 134*72ac97cdSTom Musta else { /* is finite */ 135*72ac97cdSTom Musta if (decNumberIsZero(dn)) { /* is a zero */ 136*72ac97cdSTom Musta /* set and clamp exponent */ 137*72ac97cdSTom Musta if (dn->exponent<-DECIMAL64_Bias) { 138*72ac97cdSTom Musta exp=0; /* low clamp */ 139*72ac97cdSTom Musta status|=DEC_Clamped; 140*72ac97cdSTom Musta } 141*72ac97cdSTom Musta else { 142*72ac97cdSTom Musta exp=dn->exponent+DECIMAL64_Bias; /* bias exponent */ 143*72ac97cdSTom Musta if (exp>DECIMAL64_Ehigh) { /* top clamp */ 144*72ac97cdSTom Musta exp=DECIMAL64_Ehigh; 145*72ac97cdSTom Musta status|=DEC_Clamped; 146*72ac97cdSTom Musta } 147*72ac97cdSTom Musta } 148*72ac97cdSTom Musta comb=(exp>>5) & 0x18; /* msd=0, exp top 2 bits .. */ 149*72ac97cdSTom Musta } 150*72ac97cdSTom Musta else { /* non-zero finite number */ 151*72ac97cdSTom Musta uInt msd; /* work */ 152*72ac97cdSTom Musta Int pad=0; /* coefficient pad digits */ 153*72ac97cdSTom Musta 154*72ac97cdSTom Musta /* the dn is known to fit, but it may need to be padded */ 155*72ac97cdSTom Musta exp=(uInt)(dn->exponent+DECIMAL64_Bias); /* bias exponent */ 156*72ac97cdSTom Musta if (exp>DECIMAL64_Ehigh) { /* fold-down case */ 157*72ac97cdSTom Musta pad=exp-DECIMAL64_Ehigh; 158*72ac97cdSTom Musta exp=DECIMAL64_Ehigh; /* [to maximum] */ 159*72ac97cdSTom Musta status|=DEC_Clamped; 160*72ac97cdSTom Musta } 161*72ac97cdSTom Musta 162*72ac97cdSTom Musta /* fastpath common case */ 163*72ac97cdSTom Musta if (DECDPUN==3 && pad==0) { 164*72ac97cdSTom Musta uInt dpd[6]={0,0,0,0,0,0}; 165*72ac97cdSTom Musta uInt i; 166*72ac97cdSTom Musta Int d=dn->digits; 167*72ac97cdSTom Musta for (i=0; d>0; i++, d-=3) dpd[i]=BIN2DPD[dn->lsu[i]]; 168*72ac97cdSTom Musta targlo =dpd[0]; 169*72ac97cdSTom Musta targlo|=dpd[1]<<10; 170*72ac97cdSTom Musta targlo|=dpd[2]<<20; 171*72ac97cdSTom Musta if (dn->digits>6) { 172*72ac97cdSTom Musta targlo|=dpd[3]<<30; 173*72ac97cdSTom Musta targhi =dpd[3]>>2; 174*72ac97cdSTom Musta targhi|=dpd[4]<<8; 175*72ac97cdSTom Musta } 176*72ac97cdSTom Musta msd=dpd[5]; /* [did not really need conversion] */ 177*72ac97cdSTom Musta } 178*72ac97cdSTom Musta else { /* general case */ 179*72ac97cdSTom Musta decDigitsToDPD(dn, targar, pad); 180*72ac97cdSTom Musta /* save and clear the top digit */ 181*72ac97cdSTom Musta msd=targhi>>18; 182*72ac97cdSTom Musta targhi&=0x0003ffff; 183*72ac97cdSTom Musta } 184*72ac97cdSTom Musta 185*72ac97cdSTom Musta /* create the combination field */ 186*72ac97cdSTom Musta if (msd>=8) comb=0x18 | ((exp>>7) & 0x06) | (msd & 0x01); 187*72ac97cdSTom Musta else comb=((exp>>5) & 0x18) | msd; 188*72ac97cdSTom Musta } 189*72ac97cdSTom Musta targhi|=comb<<26; /* add combination field .. */ 190*72ac97cdSTom Musta targhi|=(exp&0xff)<<18; /* .. and exponent continuation */ 191*72ac97cdSTom Musta } /* finite */ 192*72ac97cdSTom Musta 193*72ac97cdSTom Musta if (dn->bits&DECNEG) targhi|=0x80000000; /* add sign bit */ 194*72ac97cdSTom Musta 195*72ac97cdSTom Musta /* now write to storage; this is now always endian */ 196*72ac97cdSTom Musta pu=(uInt *)d64->bytes; /* overlay */ 197*72ac97cdSTom Musta if (DECLITEND) { 198*72ac97cdSTom Musta pu[0]=targar[0]; /* directly store the low int */ 199*72ac97cdSTom Musta pu[1]=targar[1]; /* then the high int */ 200*72ac97cdSTom Musta } 201*72ac97cdSTom Musta else { 202*72ac97cdSTom Musta pu[0]=targar[1]; /* directly store the high int */ 203*72ac97cdSTom Musta pu[1]=targar[0]; /* then the low int */ 204*72ac97cdSTom Musta } 205*72ac97cdSTom Musta 206*72ac97cdSTom Musta if (status!=0) decContextSetStatus(set, status); /* pass on status */ 207*72ac97cdSTom Musta /* decimal64Show(d64); */ 208*72ac97cdSTom Musta return d64; 209*72ac97cdSTom Musta } /* decimal64FromNumber */ 210*72ac97cdSTom Musta 211*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 212*72ac97cdSTom Musta /* decimal64ToNumber -- convert decimal64 to decNumber */ 213*72ac97cdSTom Musta /* d64 is the source decimal64 */ 214*72ac97cdSTom Musta /* dn is the target number, with appropriate space */ 215*72ac97cdSTom Musta /* No error is possible. */ 216*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 217*72ac97cdSTom Musta decNumber * decimal64ToNumber(const decimal64 *d64, decNumber *dn) { 218*72ac97cdSTom Musta uInt msd; /* coefficient MSD */ 219*72ac97cdSTom Musta uInt exp; /* exponent top two bits */ 220*72ac97cdSTom Musta uInt comb; /* combination field */ 221*72ac97cdSTom Musta const uInt *pu; /* work */ 222*72ac97cdSTom Musta Int need; /* .. */ 223*72ac97cdSTom Musta uInt sourar[2]; /* source 64-bit */ 224*72ac97cdSTom Musta #define sourhi sourar[1] /* name the word with the sign */ 225*72ac97cdSTom Musta #define sourlo sourar[0] /* and the lower word */ 226*72ac97cdSTom Musta 227*72ac97cdSTom Musta /* load source from storage; this is endian */ 228*72ac97cdSTom Musta pu=(const uInt *)d64->bytes; /* overlay */ 229*72ac97cdSTom Musta if (DECLITEND) { 230*72ac97cdSTom Musta sourlo=pu[0]; /* directly load the low int */ 231*72ac97cdSTom Musta sourhi=pu[1]; /* then the high int */ 232*72ac97cdSTom Musta } 233*72ac97cdSTom Musta else { 234*72ac97cdSTom Musta sourhi=pu[0]; /* directly load the high int */ 235*72ac97cdSTom Musta sourlo=pu[1]; /* then the low int */ 236*72ac97cdSTom Musta } 237*72ac97cdSTom Musta 238*72ac97cdSTom Musta comb=(sourhi>>26)&0x1f; /* combination field */ 239*72ac97cdSTom Musta 240*72ac97cdSTom Musta decNumberZero(dn); /* clean number */ 241*72ac97cdSTom Musta if (sourhi&0x80000000) dn->bits=DECNEG; /* set sign if negative */ 242*72ac97cdSTom Musta 243*72ac97cdSTom Musta msd=COMBMSD[comb]; /* decode the combination field */ 244*72ac97cdSTom Musta exp=COMBEXP[comb]; /* .. */ 245*72ac97cdSTom Musta 246*72ac97cdSTom Musta if (exp==3) { /* is a special */ 247*72ac97cdSTom Musta if (msd==0) { 248*72ac97cdSTom Musta dn->bits|=DECINF; 249*72ac97cdSTom Musta return dn; /* no coefficient needed */ 250*72ac97cdSTom Musta } 251*72ac97cdSTom Musta else if (sourhi&0x02000000) dn->bits|=DECSNAN; 252*72ac97cdSTom Musta else dn->bits|=DECNAN; 253*72ac97cdSTom Musta msd=0; /* no top digit */ 254*72ac97cdSTom Musta } 255*72ac97cdSTom Musta else { /* is a finite number */ 256*72ac97cdSTom Musta dn->exponent=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias; /* unbiased */ 257*72ac97cdSTom Musta } 258*72ac97cdSTom Musta 259*72ac97cdSTom Musta /* get the coefficient */ 260*72ac97cdSTom Musta sourhi&=0x0003ffff; /* clean coefficient continuation */ 261*72ac97cdSTom Musta if (msd) { /* non-zero msd */ 262*72ac97cdSTom Musta sourhi|=msd<<18; /* prefix to coefficient */ 263*72ac97cdSTom Musta need=6; /* process 6 declets */ 264*72ac97cdSTom Musta } 265*72ac97cdSTom Musta else { /* msd=0 */ 266*72ac97cdSTom Musta if (!sourhi) { /* top word 0 */ 267*72ac97cdSTom Musta if (!sourlo) return dn; /* easy: coefficient is 0 */ 268*72ac97cdSTom Musta need=3; /* process at least 3 declets */ 269*72ac97cdSTom Musta if (sourlo&0xc0000000) need++; /* process 4 declets */ 270*72ac97cdSTom Musta /* [could reduce some more, here] */ 271*72ac97cdSTom Musta } 272*72ac97cdSTom Musta else { /* some bits in top word, msd=0 */ 273*72ac97cdSTom Musta need=4; /* process at least 4 declets */ 274*72ac97cdSTom Musta if (sourhi&0x0003ff00) need++; /* top declet!=0, process 5 */ 275*72ac97cdSTom Musta } 276*72ac97cdSTom Musta } /*msd=0 */ 277*72ac97cdSTom Musta 278*72ac97cdSTom Musta decDigitsFromDPD(dn, sourar, need); /* process declets */ 279*72ac97cdSTom Musta return dn; 280*72ac97cdSTom Musta } /* decimal64ToNumber */ 281*72ac97cdSTom Musta 282*72ac97cdSTom Musta 283*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 284*72ac97cdSTom Musta /* to-scientific-string -- conversion to numeric string */ 285*72ac97cdSTom Musta /* to-engineering-string -- conversion to numeric string */ 286*72ac97cdSTom Musta /* */ 287*72ac97cdSTom Musta /* decimal64ToString(d64, string); */ 288*72ac97cdSTom Musta /* decimal64ToEngString(d64, string); */ 289*72ac97cdSTom Musta /* */ 290*72ac97cdSTom Musta /* d64 is the decimal64 format number to convert */ 291*72ac97cdSTom Musta /* string is the string where the result will be laid out */ 292*72ac97cdSTom Musta /* */ 293*72ac97cdSTom Musta /* string must be at least 24 characters */ 294*72ac97cdSTom Musta /* */ 295*72ac97cdSTom Musta /* No error is possible, and no status can be set. */ 296*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 297*72ac97cdSTom Musta char * decimal64ToEngString(const decimal64 *d64, char *string){ 298*72ac97cdSTom Musta decNumber dn; /* work */ 299*72ac97cdSTom Musta decimal64ToNumber(d64, &dn); 300*72ac97cdSTom Musta decNumberToEngString(&dn, string); 301*72ac97cdSTom Musta return string; 302*72ac97cdSTom Musta } /* decimal64ToEngString */ 303*72ac97cdSTom Musta 304*72ac97cdSTom Musta char * decimal64ToString(const decimal64 *d64, char *string){ 305*72ac97cdSTom Musta uInt msd; /* coefficient MSD */ 306*72ac97cdSTom Musta Int exp; /* exponent top two bits or full */ 307*72ac97cdSTom Musta uInt comb; /* combination field */ 308*72ac97cdSTom Musta char *cstart; /* coefficient start */ 309*72ac97cdSTom Musta char *c; /* output pointer in string */ 310*72ac97cdSTom Musta const uInt *pu; /* work */ 311*72ac97cdSTom Musta char *s, *t; /* .. (source, target) */ 312*72ac97cdSTom Musta Int dpd; /* .. */ 313*72ac97cdSTom Musta Int pre, e; /* .. */ 314*72ac97cdSTom Musta const uByte *u; /* .. */ 315*72ac97cdSTom Musta 316*72ac97cdSTom Musta uInt sourar[2]; /* source 64-bit */ 317*72ac97cdSTom Musta #define sourhi sourar[1] /* name the word with the sign */ 318*72ac97cdSTom Musta #define sourlo sourar[0] /* and the lower word */ 319*72ac97cdSTom Musta 320*72ac97cdSTom Musta /* load source from storage; this is endian */ 321*72ac97cdSTom Musta pu=(const uInt *)d64->bytes; /* overlay */ 322*72ac97cdSTom Musta if (DECLITEND) { 323*72ac97cdSTom Musta sourlo=pu[0]; /* directly load the low int */ 324*72ac97cdSTom Musta sourhi=pu[1]; /* then the high int */ 325*72ac97cdSTom Musta } 326*72ac97cdSTom Musta else { 327*72ac97cdSTom Musta sourhi=pu[0]; /* directly load the high int */ 328*72ac97cdSTom Musta sourlo=pu[1]; /* then the low int */ 329*72ac97cdSTom Musta } 330*72ac97cdSTom Musta 331*72ac97cdSTom Musta c=string; /* where result will go */ 332*72ac97cdSTom Musta if (((Int)sourhi)<0) *c++='-'; /* handle sign */ 333*72ac97cdSTom Musta 334*72ac97cdSTom Musta comb=(sourhi>>26)&0x1f; /* combination field */ 335*72ac97cdSTom Musta msd=COMBMSD[comb]; /* decode the combination field */ 336*72ac97cdSTom Musta exp=COMBEXP[comb]; /* .. */ 337*72ac97cdSTom Musta 338*72ac97cdSTom Musta if (exp==3) { 339*72ac97cdSTom Musta if (msd==0) { /* infinity */ 340*72ac97cdSTom Musta strcpy(c, "Inf"); 341*72ac97cdSTom Musta strcpy(c+3, "inity"); 342*72ac97cdSTom Musta return string; /* easy */ 343*72ac97cdSTom Musta } 344*72ac97cdSTom Musta if (sourhi&0x02000000) *c++='s'; /* sNaN */ 345*72ac97cdSTom Musta strcpy(c, "NaN"); /* complete word */ 346*72ac97cdSTom Musta c+=3; /* step past */ 347*72ac97cdSTom Musta if (sourlo==0 && (sourhi&0x0003ffff)==0) return string; /* zero payload */ 348*72ac97cdSTom Musta /* otherwise drop through to add integer; set correct exp */ 349*72ac97cdSTom Musta exp=0; msd=0; /* setup for following code */ 350*72ac97cdSTom Musta } 351*72ac97cdSTom Musta else exp=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias; 352*72ac97cdSTom Musta 353*72ac97cdSTom Musta /* convert 16 digits of significand to characters */ 354*72ac97cdSTom Musta cstart=c; /* save start of coefficient */ 355*72ac97cdSTom Musta if (msd) *c++='0'+(char)msd; /* non-zero most significant digit */ 356*72ac97cdSTom Musta 357*72ac97cdSTom Musta /* Now decode the declets. After extracting each one, it is */ 358*72ac97cdSTom Musta /* decoded to binary and then to a 4-char sequence by table lookup; */ 359*72ac97cdSTom Musta /* the 4-chars are a 1-char length (significant digits, except 000 */ 360*72ac97cdSTom Musta /* has length 0). This allows us to left-align the first declet */ 361*72ac97cdSTom Musta /* with non-zero content, then remaining ones are full 3-char */ 362*72ac97cdSTom Musta /* length. We use fixed-length memcpys because variable-length */ 363*72ac97cdSTom Musta /* causes a subroutine call in GCC. (These are length 4 for speed */ 364*72ac97cdSTom Musta /* and are safe because the array has an extra terminator byte.) */ 365*72ac97cdSTom Musta #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \ 366*72ac97cdSTom Musta if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \ 367*72ac97cdSTom Musta else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;} 368*72ac97cdSTom Musta 369*72ac97cdSTom Musta dpd=(sourhi>>8)&0x3ff; /* declet 1 */ 370*72ac97cdSTom Musta dpd2char; 371*72ac97cdSTom Musta dpd=((sourhi&0xff)<<2) | (sourlo>>30); /* declet 2 */ 372*72ac97cdSTom Musta dpd2char; 373*72ac97cdSTom Musta dpd=(sourlo>>20)&0x3ff; /* declet 3 */ 374*72ac97cdSTom Musta dpd2char; 375*72ac97cdSTom Musta dpd=(sourlo>>10)&0x3ff; /* declet 4 */ 376*72ac97cdSTom Musta dpd2char; 377*72ac97cdSTom Musta dpd=(sourlo)&0x3ff; /* declet 5 */ 378*72ac97cdSTom Musta dpd2char; 379*72ac97cdSTom Musta 380*72ac97cdSTom Musta if (c==cstart) *c++='0'; /* all zeros -- make 0 */ 381*72ac97cdSTom Musta 382*72ac97cdSTom Musta if (exp==0) { /* integer or NaN case -- easy */ 383*72ac97cdSTom Musta *c='\0'; /* terminate */ 384*72ac97cdSTom Musta return string; 385*72ac97cdSTom Musta } 386*72ac97cdSTom Musta 387*72ac97cdSTom Musta /* non-0 exponent */ 388*72ac97cdSTom Musta e=0; /* assume no E */ 389*72ac97cdSTom Musta pre=c-cstart+exp; 390*72ac97cdSTom Musta /* [here, pre-exp is the digits count (==1 for zero)] */ 391*72ac97cdSTom Musta if (exp>0 || pre<-5) { /* need exponential form */ 392*72ac97cdSTom Musta e=pre-1; /* calculate E value */ 393*72ac97cdSTom Musta pre=1; /* assume one digit before '.' */ 394*72ac97cdSTom Musta } /* exponential form */ 395*72ac97cdSTom Musta 396*72ac97cdSTom Musta /* modify the coefficient, adding 0s, '.', and E+nn as needed */ 397*72ac97cdSTom Musta s=c-1; /* source (LSD) */ 398*72ac97cdSTom Musta if (pre>0) { /* ddd.ddd (plain), perhaps with E */ 399*72ac97cdSTom Musta char *dotat=cstart+pre; 400*72ac97cdSTom Musta if (dotat<c) { /* if embedded dot needed... */ 401*72ac97cdSTom Musta t=c; /* target */ 402*72ac97cdSTom Musta for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */ 403*72ac97cdSTom Musta *t='.'; /* insert the dot */ 404*72ac97cdSTom Musta c++; /* length increased by one */ 405*72ac97cdSTom Musta } 406*72ac97cdSTom Musta 407*72ac97cdSTom Musta /* finally add the E-part, if needed; it will never be 0, and has */ 408*72ac97cdSTom Musta /* a maximum length of 3 digits */ 409*72ac97cdSTom Musta if (e!=0) { 410*72ac97cdSTom Musta *c++='E'; /* starts with E */ 411*72ac97cdSTom Musta *c++='+'; /* assume positive */ 412*72ac97cdSTom Musta if (e<0) { 413*72ac97cdSTom Musta *(c-1)='-'; /* oops, need '-' */ 414*72ac97cdSTom Musta e=-e; /* uInt, please */ 415*72ac97cdSTom Musta } 416*72ac97cdSTom Musta u=&BIN2CHAR[e*4]; /* -> length byte */ 417*72ac97cdSTom Musta memcpy(c, u+4-*u, 4); /* copy fixed 4 characters [is safe] */ 418*72ac97cdSTom Musta c+=*u; /* bump pointer appropriately */ 419*72ac97cdSTom Musta } 420*72ac97cdSTom Musta *c='\0'; /* add terminator */ 421*72ac97cdSTom Musta /*printf("res %s\n", string); */ 422*72ac97cdSTom Musta return string; 423*72ac97cdSTom Musta } /* pre>0 */ 424*72ac97cdSTom Musta 425*72ac97cdSTom Musta /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */ 426*72ac97cdSTom Musta t=c+1-pre; 427*72ac97cdSTom Musta *(t+1)='\0'; /* can add terminator now */ 428*72ac97cdSTom Musta for (; s>=cstart; s--, t--) *t=*s; /* shift whole coefficient right */ 429*72ac97cdSTom Musta c=cstart; 430*72ac97cdSTom Musta *c++='0'; /* always starts with 0. */ 431*72ac97cdSTom Musta *c++='.'; 432*72ac97cdSTom Musta for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */ 433*72ac97cdSTom Musta /*printf("res %s\n", string); */ 434*72ac97cdSTom Musta return string; 435*72ac97cdSTom Musta } /* decimal64ToString */ 436*72ac97cdSTom Musta 437*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 438*72ac97cdSTom Musta /* to-number -- conversion from numeric string */ 439*72ac97cdSTom Musta /* */ 440*72ac97cdSTom Musta /* decimal64FromString(result, string, set); */ 441*72ac97cdSTom Musta /* */ 442*72ac97cdSTom Musta /* result is the decimal64 format number which gets the result of */ 443*72ac97cdSTom Musta /* the conversion */ 444*72ac97cdSTom Musta /* *string is the character string which should contain a valid */ 445*72ac97cdSTom Musta /* number (which may be a special value) */ 446*72ac97cdSTom Musta /* set is the context */ 447*72ac97cdSTom Musta /* */ 448*72ac97cdSTom Musta /* The context is supplied to this routine is used for error handling */ 449*72ac97cdSTom Musta /* (setting of status and traps) and for the rounding mode, only. */ 450*72ac97cdSTom Musta /* If an error occurs, the result will be a valid decimal64 NaN. */ 451*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 452*72ac97cdSTom Musta decimal64 * decimal64FromString(decimal64 *result, const char *string, 453*72ac97cdSTom Musta decContext *set) { 454*72ac97cdSTom Musta decContext dc; /* work */ 455*72ac97cdSTom Musta decNumber dn; /* .. */ 456*72ac97cdSTom Musta 457*72ac97cdSTom Musta decContextDefault(&dc, DEC_INIT_DECIMAL64); /* no traps, please */ 458*72ac97cdSTom Musta dc.round=set->round; /* use supplied rounding */ 459*72ac97cdSTom Musta 460*72ac97cdSTom Musta decNumberFromString(&dn, string, &dc); /* will round if needed */ 461*72ac97cdSTom Musta 462*72ac97cdSTom Musta decimal64FromNumber(result, &dn, &dc); 463*72ac97cdSTom Musta if (dc.status!=0) { /* something happened */ 464*72ac97cdSTom Musta decContextSetStatus(set, dc.status); /* .. pass it on */ 465*72ac97cdSTom Musta } 466*72ac97cdSTom Musta return result; 467*72ac97cdSTom Musta } /* decimal64FromString */ 468*72ac97cdSTom Musta 469*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 470*72ac97cdSTom Musta /* decimal64IsCanonical -- test whether encoding is canonical */ 471*72ac97cdSTom Musta /* d64 is the source decimal64 */ 472*72ac97cdSTom Musta /* returns 1 if the encoding of d64 is canonical, 0 otherwise */ 473*72ac97cdSTom Musta /* No error is possible. */ 474*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 475*72ac97cdSTom Musta uint32_t decimal64IsCanonical(const decimal64 *d64) { 476*72ac97cdSTom Musta decNumber dn; /* work */ 477*72ac97cdSTom Musta decimal64 canon; /* .. */ 478*72ac97cdSTom Musta decContext dc; /* .. */ 479*72ac97cdSTom Musta decContextDefault(&dc, DEC_INIT_DECIMAL64); 480*72ac97cdSTom Musta decimal64ToNumber(d64, &dn); 481*72ac97cdSTom Musta decimal64FromNumber(&canon, &dn, &dc);/* canon will now be canonical */ 482*72ac97cdSTom Musta return memcmp(d64, &canon, DECIMAL64_Bytes)==0; 483*72ac97cdSTom Musta } /* decimal64IsCanonical */ 484*72ac97cdSTom Musta 485*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 486*72ac97cdSTom Musta /* decimal64Canonical -- copy an encoding, ensuring it is canonical */ 487*72ac97cdSTom Musta /* d64 is the source decimal64 */ 488*72ac97cdSTom Musta /* result is the target (may be the same decimal64) */ 489*72ac97cdSTom Musta /* returns result */ 490*72ac97cdSTom Musta /* No error is possible. */ 491*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 492*72ac97cdSTom Musta decimal64 * decimal64Canonical(decimal64 *result, const decimal64 *d64) { 493*72ac97cdSTom Musta decNumber dn; /* work */ 494*72ac97cdSTom Musta decContext dc; /* .. */ 495*72ac97cdSTom Musta decContextDefault(&dc, DEC_INIT_DECIMAL64); 496*72ac97cdSTom Musta decimal64ToNumber(d64, &dn); 497*72ac97cdSTom Musta decimal64FromNumber(result, &dn, &dc);/* result will now be canonical */ 498*72ac97cdSTom Musta return result; 499*72ac97cdSTom Musta } /* decimal64Canonical */ 500*72ac97cdSTom Musta 501*72ac97cdSTom Musta #if DECTRACE || DECCHECK 502*72ac97cdSTom Musta /* Macros for accessing decimal64 fields. These assume the 503*72ac97cdSTom Musta argument is a reference (pointer) to the decimal64 structure, 504*72ac97cdSTom Musta and the decimal64 is in network byte order (big-endian) */ 505*72ac97cdSTom Musta /* Get sign */ 506*72ac97cdSTom Musta #define decimal64Sign(d) ((unsigned)(d)->bytes[0]>>7) 507*72ac97cdSTom Musta 508*72ac97cdSTom Musta /* Get combination field */ 509*72ac97cdSTom Musta #define decimal64Comb(d) (((d)->bytes[0] & 0x7c)>>2) 510*72ac97cdSTom Musta 511*72ac97cdSTom Musta /* Get exponent continuation [does not remove bias] */ 512*72ac97cdSTom Musta #define decimal64ExpCon(d) ((((d)->bytes[0] & 0x03)<<6) \ 513*72ac97cdSTom Musta | ((unsigned)(d)->bytes[1]>>2)) 514*72ac97cdSTom Musta 515*72ac97cdSTom Musta /* Set sign [this assumes sign previously 0] */ 516*72ac97cdSTom Musta #define decimal64SetSign(d, b) { \ 517*72ac97cdSTom Musta (d)->bytes[0]|=((unsigned)(b)<<7);} 518*72ac97cdSTom Musta 519*72ac97cdSTom Musta /* Set exponent continuation [does not apply bias] */ 520*72ac97cdSTom Musta /* This assumes range has been checked and exponent previously 0; */ 521*72ac97cdSTom Musta /* type of exponent must be unsigned */ 522*72ac97cdSTom Musta #define decimal64SetExpCon(d, e) { \ 523*72ac97cdSTom Musta (d)->bytes[0]|=(uint8_t)((e)>>6); \ 524*72ac97cdSTom Musta (d)->bytes[1]|=(uint8_t)(((e)&0x3F)<<2);} 525*72ac97cdSTom Musta 526*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 527*72ac97cdSTom Musta /* decimal64Show -- display a decimal64 in hexadecimal [debug aid] */ 528*72ac97cdSTom Musta /* d64 -- the number to show */ 529*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 530*72ac97cdSTom Musta /* Also shows sign/cob/expconfields extracted */ 531*72ac97cdSTom Musta void decimal64Show(const decimal64 *d64) { 532*72ac97cdSTom Musta char buf[DECIMAL64_Bytes*2+1]; 533*72ac97cdSTom Musta Int i, j=0; 534*72ac97cdSTom Musta 535*72ac97cdSTom Musta if (DECLITEND) { 536*72ac97cdSTom Musta for (i=0; i<DECIMAL64_Bytes; i++, j+=2) { 537*72ac97cdSTom Musta sprintf(&buf[j], "%02x", d64->bytes[7-i]); 538*72ac97cdSTom Musta } 539*72ac97cdSTom Musta printf(" D64> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf, 540*72ac97cdSTom Musta d64->bytes[7]>>7, (d64->bytes[7]>>2)&0x1f, 541*72ac97cdSTom Musta ((d64->bytes[7]&0x3)<<6)| (d64->bytes[6]>>2)); 542*72ac97cdSTom Musta } 543*72ac97cdSTom Musta else { /* big-endian */ 544*72ac97cdSTom Musta for (i=0; i<DECIMAL64_Bytes; i++, j+=2) { 545*72ac97cdSTom Musta sprintf(&buf[j], "%02x", d64->bytes[i]); 546*72ac97cdSTom Musta } 547*72ac97cdSTom Musta printf(" D64> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf, 548*72ac97cdSTom Musta decimal64Sign(d64), decimal64Comb(d64), decimal64ExpCon(d64)); 549*72ac97cdSTom Musta } 550*72ac97cdSTom Musta } /* decimal64Show */ 551*72ac97cdSTom Musta #endif 552*72ac97cdSTom Musta 553*72ac97cdSTom Musta /* ================================================================== */ 554*72ac97cdSTom Musta /* Shared utility routines and tables */ 555*72ac97cdSTom Musta /* ================================================================== */ 556*72ac97cdSTom Musta /* define and include the conversion tables to use for shared code */ 557*72ac97cdSTom Musta #if DECDPUN==3 558*72ac97cdSTom Musta #define DEC_DPD2BIN 1 559*72ac97cdSTom Musta #else 560*72ac97cdSTom Musta #define DEC_DPD2BCD 1 561*72ac97cdSTom Musta #endif 562*72ac97cdSTom Musta #include "decDPD.h" /* lookup tables */ 563*72ac97cdSTom Musta 564*72ac97cdSTom Musta /* The maximum number of decNumberUnits needed for a working copy of */ 565*72ac97cdSTom Musta /* the units array is the ceiling of digits/DECDPUN, where digits is */ 566*72ac97cdSTom Musta /* the maximum number of digits in any of the formats for which this */ 567*72ac97cdSTom Musta /* is used. decimal128.h must not be included in this module, so, as */ 568*72ac97cdSTom Musta /* a very special case, that number is defined as a literal here. */ 569*72ac97cdSTom Musta #define DECMAX754 34 570*72ac97cdSTom Musta #define DECMAXUNITS ((DECMAX754+DECDPUN-1)/DECDPUN) 571*72ac97cdSTom Musta 572*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 573*72ac97cdSTom Musta /* Combination field lookup tables (uInts to save measurable work) */ 574*72ac97cdSTom Musta /* */ 575*72ac97cdSTom Musta /* COMBEXP - 2-bit most-significant-bits of exponent */ 576*72ac97cdSTom Musta /* [11 if an Infinity or NaN] */ 577*72ac97cdSTom Musta /* COMBMSD - 4-bit most-significant-digit */ 578*72ac97cdSTom Musta /* [0=Infinity, 1=NaN if COMBEXP=11] */ 579*72ac97cdSTom Musta /* */ 580*72ac97cdSTom Musta /* Both are indexed by the 5-bit combination field (0-31) */ 581*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 582*72ac97cdSTom Musta const uInt COMBEXP[32]={0, 0, 0, 0, 0, 0, 0, 0, 583*72ac97cdSTom Musta 1, 1, 1, 1, 1, 1, 1, 1, 584*72ac97cdSTom Musta 2, 2, 2, 2, 2, 2, 2, 2, 585*72ac97cdSTom Musta 0, 0, 1, 1, 2, 2, 3, 3}; 586*72ac97cdSTom Musta const uInt COMBMSD[32]={0, 1, 2, 3, 4, 5, 6, 7, 587*72ac97cdSTom Musta 0, 1, 2, 3, 4, 5, 6, 7, 588*72ac97cdSTom Musta 0, 1, 2, 3, 4, 5, 6, 7, 589*72ac97cdSTom Musta 8, 9, 8, 9, 8, 9, 0, 1}; 590*72ac97cdSTom Musta 591*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 592*72ac97cdSTom Musta /* decDigitsToDPD -- pack coefficient into DPD form */ 593*72ac97cdSTom Musta /* */ 594*72ac97cdSTom Musta /* dn is the source number (assumed valid, max DECMAX754 digits) */ 595*72ac97cdSTom Musta /* targ is 1, 2, or 4-element uInt array, which the caller must */ 596*72ac97cdSTom Musta /* have cleared to zeros */ 597*72ac97cdSTom Musta /* shift is the number of 0 digits to add on the right (normally 0) */ 598*72ac97cdSTom Musta /* */ 599*72ac97cdSTom Musta /* The coefficient must be known small enough to fit. The full */ 600*72ac97cdSTom Musta /* coefficient is copied, including the leading 'odd' digit. This */ 601*72ac97cdSTom Musta /* digit is retrieved and packed into the combination field by the */ 602*72ac97cdSTom Musta /* caller. */ 603*72ac97cdSTom Musta /* */ 604*72ac97cdSTom Musta /* The target uInts are altered only as necessary to receive the */ 605*72ac97cdSTom Musta /* digits of the decNumber. When more than one uInt is needed, they */ 606*72ac97cdSTom Musta /* are filled from left to right (that is, the uInt at offset 0 will */ 607*72ac97cdSTom Musta /* end up with the least-significant digits). */ 608*72ac97cdSTom Musta /* */ 609*72ac97cdSTom Musta /* shift is used for 'fold-down' padding. */ 610*72ac97cdSTom Musta /* */ 611*72ac97cdSTom Musta /* No error is possible. */ 612*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 613*72ac97cdSTom Musta #if DECDPUN<=4 614*72ac97cdSTom Musta /* Constant multipliers for divide-by-power-of five using reciprocal */ 615*72ac97cdSTom Musta /* multiply, after removing powers of 2 by shifting, and final shift */ 616*72ac97cdSTom Musta /* of 17 [we only need up to **4] */ 617*72ac97cdSTom Musta static const uInt multies[]={131073, 26215, 5243, 1049, 210}; 618*72ac97cdSTom Musta /* QUOT10 -- macro to return the quotient of unit u divided by 10**n */ 619*72ac97cdSTom Musta #define QUOT10(u, n) ((((uInt)(u)>>(n))*multies[n])>>17) 620*72ac97cdSTom Musta #endif 621*72ac97cdSTom Musta void decDigitsToDPD(const decNumber *dn, uInt *targ, Int shift) { 622*72ac97cdSTom Musta Int cut; /* work */ 623*72ac97cdSTom Musta Int n; /* output bunch counter */ 624*72ac97cdSTom Musta Int digits=dn->digits; /* digit countdown */ 625*72ac97cdSTom Musta uInt dpd; /* densely packed decimal value */ 626*72ac97cdSTom Musta uInt bin; /* binary value 0-999 */ 627*72ac97cdSTom Musta uInt *uout=targ; /* -> current output uInt */ 628*72ac97cdSTom Musta uInt uoff=0; /* -> current output offset [from right] */ 629*72ac97cdSTom Musta const Unit *inu=dn->lsu; /* -> current input unit */ 630*72ac97cdSTom Musta Unit uar[DECMAXUNITS]; /* working copy of units, iff shifted */ 631*72ac97cdSTom Musta #if DECDPUN!=3 /* not fast path */ 632*72ac97cdSTom Musta Unit in; /* current unit */ 633*72ac97cdSTom Musta #endif 634*72ac97cdSTom Musta 635*72ac97cdSTom Musta if (shift!=0) { /* shift towards most significant required */ 636*72ac97cdSTom Musta /* shift the units array to the left by pad digits and copy */ 637*72ac97cdSTom Musta /* [this code is a special case of decShiftToMost, which could */ 638*72ac97cdSTom Musta /* be used instead if exposed and the array were copied first] */ 639*72ac97cdSTom Musta const Unit *source; /* .. */ 640*72ac97cdSTom Musta Unit *target, *first; /* .. */ 641*72ac97cdSTom Musta uInt next=0; /* work */ 642*72ac97cdSTom Musta 643*72ac97cdSTom Musta source=dn->lsu+D2U(digits)-1; /* where msu comes from */ 644*72ac97cdSTom Musta target=uar+D2U(digits)-1+D2U(shift);/* where upper part of first cut goes */ 645*72ac97cdSTom Musta cut=DECDPUN-MSUDIGITS(shift); /* where to slice */ 646*72ac97cdSTom Musta if (cut==0) { /* unit-boundary case */ 647*72ac97cdSTom Musta for (; source>=dn->lsu; source--, target--) *target=*source; 648*72ac97cdSTom Musta } 649*72ac97cdSTom Musta else { 650*72ac97cdSTom Musta first=uar+D2U(digits+shift)-1; /* where msu will end up */ 651*72ac97cdSTom Musta for (; source>=dn->lsu; source--, target--) { 652*72ac97cdSTom Musta /* split the source Unit and accumulate remainder for next */ 653*72ac97cdSTom Musta #if DECDPUN<=4 654*72ac97cdSTom Musta uInt quot=QUOT10(*source, cut); 655*72ac97cdSTom Musta uInt rem=*source-quot*DECPOWERS[cut]; 656*72ac97cdSTom Musta next+=quot; 657*72ac97cdSTom Musta #else 658*72ac97cdSTom Musta uInt rem=*source%DECPOWERS[cut]; 659*72ac97cdSTom Musta next+=*source/DECPOWERS[cut]; 660*72ac97cdSTom Musta #endif 661*72ac97cdSTom Musta if (target<=first) *target=(Unit)next; /* write to target iff valid */ 662*72ac97cdSTom Musta next=rem*DECPOWERS[DECDPUN-cut]; /* save remainder for next Unit */ 663*72ac97cdSTom Musta } 664*72ac97cdSTom Musta } /* shift-move */ 665*72ac97cdSTom Musta /* propagate remainder to one below and clear the rest */ 666*72ac97cdSTom Musta for (; target>=uar; target--) { 667*72ac97cdSTom Musta *target=(Unit)next; 668*72ac97cdSTom Musta next=0; 669*72ac97cdSTom Musta } 670*72ac97cdSTom Musta digits+=shift; /* add count (shift) of zeros added */ 671*72ac97cdSTom Musta inu=uar; /* use units in working array */ 672*72ac97cdSTom Musta } 673*72ac97cdSTom Musta 674*72ac97cdSTom Musta /* now densely pack the coefficient into DPD declets */ 675*72ac97cdSTom Musta 676*72ac97cdSTom Musta #if DECDPUN!=3 /* not fast path */ 677*72ac97cdSTom Musta in=*inu; /* current unit */ 678*72ac97cdSTom Musta cut=0; /* at lowest digit */ 679*72ac97cdSTom Musta bin=0; /* [keep compiler quiet] */ 680*72ac97cdSTom Musta #endif 681*72ac97cdSTom Musta 682*72ac97cdSTom Musta for(n=0; digits>0; n++) { /* each output bunch */ 683*72ac97cdSTom Musta #if DECDPUN==3 /* fast path, 3-at-a-time */ 684*72ac97cdSTom Musta bin=*inu; /* 3 digits ready for convert */ 685*72ac97cdSTom Musta digits-=3; /* [may go negative] */ 686*72ac97cdSTom Musta inu++; /* may need another */ 687*72ac97cdSTom Musta 688*72ac97cdSTom Musta #else /* must collect digit-by-digit */ 689*72ac97cdSTom Musta Unit dig; /* current digit */ 690*72ac97cdSTom Musta Int j; /* digit-in-declet count */ 691*72ac97cdSTom Musta for (j=0; j<3; j++) { 692*72ac97cdSTom Musta #if DECDPUN<=4 693*72ac97cdSTom Musta Unit temp=(Unit)((uInt)(in*6554)>>16); 694*72ac97cdSTom Musta dig=(Unit)(in-X10(temp)); 695*72ac97cdSTom Musta in=temp; 696*72ac97cdSTom Musta #else 697*72ac97cdSTom Musta dig=in%10; 698*72ac97cdSTom Musta in=in/10; 699*72ac97cdSTom Musta #endif 700*72ac97cdSTom Musta if (j==0) bin=dig; 701*72ac97cdSTom Musta else if (j==1) bin+=X10(dig); 702*72ac97cdSTom Musta else /* j==2 */ bin+=X100(dig); 703*72ac97cdSTom Musta digits--; 704*72ac97cdSTom Musta if (digits==0) break; /* [also protects *inu below] */ 705*72ac97cdSTom Musta cut++; 706*72ac97cdSTom Musta if (cut==DECDPUN) {inu++; in=*inu; cut=0;} 707*72ac97cdSTom Musta } 708*72ac97cdSTom Musta #endif 709*72ac97cdSTom Musta /* here there are 3 digits in bin, or have used all input digits */ 710*72ac97cdSTom Musta 711*72ac97cdSTom Musta dpd=BIN2DPD[bin]; 712*72ac97cdSTom Musta 713*72ac97cdSTom Musta /* write declet to uInt array */ 714*72ac97cdSTom Musta *uout|=dpd<<uoff; 715*72ac97cdSTom Musta uoff+=10; 716*72ac97cdSTom Musta if (uoff<32) continue; /* no uInt boundary cross */ 717*72ac97cdSTom Musta uout++; 718*72ac97cdSTom Musta uoff-=32; 719*72ac97cdSTom Musta *uout|=dpd>>(10-uoff); /* collect top bits */ 720*72ac97cdSTom Musta } /* n declets */ 721*72ac97cdSTom Musta return; 722*72ac97cdSTom Musta } /* decDigitsToDPD */ 723*72ac97cdSTom Musta 724*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 725*72ac97cdSTom Musta /* decDigitsFromDPD -- unpack a format's coefficient */ 726*72ac97cdSTom Musta /* */ 727*72ac97cdSTom Musta /* dn is the target number, with 7, 16, or 34-digit space. */ 728*72ac97cdSTom Musta /* sour is a 1, 2, or 4-element uInt array containing only declets */ 729*72ac97cdSTom Musta /* declets is the number of (right-aligned) declets in sour to */ 730*72ac97cdSTom Musta /* be processed. This may be 1 more than the obvious number in */ 731*72ac97cdSTom Musta /* a format, as any top digit is prefixed to the coefficient */ 732*72ac97cdSTom Musta /* continuation field. It also may be as small as 1, as the */ 733*72ac97cdSTom Musta /* caller may pre-process leading zero declets. */ 734*72ac97cdSTom Musta /* */ 735*72ac97cdSTom Musta /* When doing the 'extra declet' case care is taken to avoid writing */ 736*72ac97cdSTom Musta /* extra digits when there are leading zeros, as these could overflow */ 737*72ac97cdSTom Musta /* the units array when DECDPUN is not 3. */ 738*72ac97cdSTom Musta /* */ 739*72ac97cdSTom Musta /* The target uInts are used only as necessary to process declets */ 740*72ac97cdSTom Musta /* declets into the decNumber. When more than one uInt is needed, */ 741*72ac97cdSTom Musta /* they are used from left to right (that is, the uInt at offset 0 */ 742*72ac97cdSTom Musta /* provides the least-significant digits). */ 743*72ac97cdSTom Musta /* */ 744*72ac97cdSTom Musta /* dn->digits is set, but not the sign or exponent. */ 745*72ac97cdSTom Musta /* No error is possible [the redundant 888 codes are allowed]. */ 746*72ac97cdSTom Musta /* ------------------------------------------------------------------ */ 747*72ac97cdSTom Musta void decDigitsFromDPD(decNumber *dn, const uInt *sour, Int declets) { 748*72ac97cdSTom Musta 749*72ac97cdSTom Musta uInt dpd; /* collector for 10 bits */ 750*72ac97cdSTom Musta Int n; /* counter */ 751*72ac97cdSTom Musta Unit *uout=dn->lsu; /* -> current output unit */ 752*72ac97cdSTom Musta Unit *last=uout; /* will be unit containing msd */ 753*72ac97cdSTom Musta const uInt *uin=sour; /* -> current input uInt */ 754*72ac97cdSTom Musta uInt uoff=0; /* -> current input offset [from right] */ 755*72ac97cdSTom Musta 756*72ac97cdSTom Musta #if DECDPUN!=3 757*72ac97cdSTom Musta uInt bcd; /* BCD result */ 758*72ac97cdSTom Musta uInt nibble; /* work */ 759*72ac97cdSTom Musta Unit out=0; /* accumulator */ 760*72ac97cdSTom Musta Int cut=0; /* power of ten in current unit */ 761*72ac97cdSTom Musta #endif 762*72ac97cdSTom Musta #if DECDPUN>4 763*72ac97cdSTom Musta uInt const *pow; /* work */ 764*72ac97cdSTom Musta #endif 765*72ac97cdSTom Musta 766*72ac97cdSTom Musta /* Expand the densely-packed integer, right to left */ 767*72ac97cdSTom Musta for (n=declets-1; n>=0; n--) { /* count down declets of 10 bits */ 768*72ac97cdSTom Musta dpd=*uin>>uoff; 769*72ac97cdSTom Musta uoff+=10; 770*72ac97cdSTom Musta if (uoff>32) { /* crossed uInt boundary */ 771*72ac97cdSTom Musta uin++; 772*72ac97cdSTom Musta uoff-=32; 773*72ac97cdSTom Musta dpd|=*uin<<(10-uoff); /* get waiting bits */ 774*72ac97cdSTom Musta } 775*72ac97cdSTom Musta dpd&=0x3ff; /* clear uninteresting bits */ 776*72ac97cdSTom Musta 777*72ac97cdSTom Musta #if DECDPUN==3 778*72ac97cdSTom Musta if (dpd==0) *uout=0; 779*72ac97cdSTom Musta else { 780*72ac97cdSTom Musta *uout=DPD2BIN[dpd]; /* convert 10 bits to binary 0-999 */ 781*72ac97cdSTom Musta last=uout; /* record most significant unit */ 782*72ac97cdSTom Musta } 783*72ac97cdSTom Musta uout++; 784*72ac97cdSTom Musta } /* n */ 785*72ac97cdSTom Musta 786*72ac97cdSTom Musta #else /* DECDPUN!=3 */ 787*72ac97cdSTom Musta if (dpd==0) { /* fastpath [e.g., leading zeros] */ 788*72ac97cdSTom Musta /* write out three 0 digits (nibbles); out may have digit(s) */ 789*72ac97cdSTom Musta cut++; 790*72ac97cdSTom Musta if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;} 791*72ac97cdSTom Musta if (n==0) break; /* [as below, works even if MSD=0] */ 792*72ac97cdSTom Musta cut++; 793*72ac97cdSTom Musta if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;} 794*72ac97cdSTom Musta cut++; 795*72ac97cdSTom Musta if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;} 796*72ac97cdSTom Musta continue; 797*72ac97cdSTom Musta } 798*72ac97cdSTom Musta 799*72ac97cdSTom Musta bcd=DPD2BCD[dpd]; /* convert 10 bits to 12 bits BCD */ 800*72ac97cdSTom Musta 801*72ac97cdSTom Musta /* now accumulate the 3 BCD nibbles into units */ 802*72ac97cdSTom Musta nibble=bcd & 0x00f; 803*72ac97cdSTom Musta if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]); 804*72ac97cdSTom Musta cut++; 805*72ac97cdSTom Musta if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;} 806*72ac97cdSTom Musta bcd>>=4; 807*72ac97cdSTom Musta 808*72ac97cdSTom Musta /* if this is the last declet and the remaining nibbles in bcd */ 809*72ac97cdSTom Musta /* are 00 then process no more nibbles, because this could be */ 810*72ac97cdSTom Musta /* the 'odd' MSD declet and writing any more Units would then */ 811*72ac97cdSTom Musta /* overflow the unit array */ 812*72ac97cdSTom Musta if (n==0 && !bcd) break; 813*72ac97cdSTom Musta 814*72ac97cdSTom Musta nibble=bcd & 0x00f; 815*72ac97cdSTom Musta if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]); 816*72ac97cdSTom Musta cut++; 817*72ac97cdSTom Musta if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;} 818*72ac97cdSTom Musta bcd>>=4; 819*72ac97cdSTom Musta 820*72ac97cdSTom Musta nibble=bcd & 0x00f; 821*72ac97cdSTom Musta if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]); 822*72ac97cdSTom Musta cut++; 823*72ac97cdSTom Musta if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;} 824*72ac97cdSTom Musta } /* n */ 825*72ac97cdSTom Musta if (cut!=0) { /* some more left over */ 826*72ac97cdSTom Musta *uout=out; /* write out final unit */ 827*72ac97cdSTom Musta if (out) last=uout; /* and note if non-zero */ 828*72ac97cdSTom Musta } 829*72ac97cdSTom Musta #endif 830*72ac97cdSTom Musta 831*72ac97cdSTom Musta /* here, last points to the most significant unit with digits; */ 832*72ac97cdSTom Musta /* inspect it to get the final digits count -- this is essentially */ 833*72ac97cdSTom Musta /* the same code as decGetDigits in decNumber.c */ 834*72ac97cdSTom Musta dn->digits=(last-dn->lsu)*DECDPUN+1; /* floor of digits, plus */ 835*72ac97cdSTom Musta /* must be at least 1 digit */ 836*72ac97cdSTom Musta #if DECDPUN>1 837*72ac97cdSTom Musta if (*last<10) return; /* common odd digit or 0 */ 838*72ac97cdSTom Musta dn->digits++; /* must be 2 at least */ 839*72ac97cdSTom Musta #if DECDPUN>2 840*72ac97cdSTom Musta if (*last<100) return; /* 10-99 */ 841*72ac97cdSTom Musta dn->digits++; /* must be 3 at least */ 842*72ac97cdSTom Musta #if DECDPUN>3 843*72ac97cdSTom Musta if (*last<1000) return; /* 100-999 */ 844*72ac97cdSTom Musta dn->digits++; /* must be 4 at least */ 845*72ac97cdSTom Musta #if DECDPUN>4 846*72ac97cdSTom Musta for (pow=&DECPOWERS[4]; *last>=*pow; pow++) dn->digits++; 847*72ac97cdSTom Musta #endif 848*72ac97cdSTom Musta #endif 849*72ac97cdSTom Musta #endif 850*72ac97cdSTom Musta #endif 851*72ac97cdSTom Musta return; 852*72ac97cdSTom Musta } /*decDigitsFromDPD */ 853