HPGCC3 Documentation 3.0 R003

decNumberLocal.h

Go to the documentation of this file.
00001 /* ------------------------------------------------------------------ */
00002 /* decNumber package local type, tuning, and macro definitions        */
00003 /* ------------------------------------------------------------------ */
00004 /* Copyright (c) IBM Corporation, 2000, 2007.  All rights reserved.   */
00005 /*                                                                    */
00006 /* This software is made available under the terms of the             */
00007 /* ICU License -- ICU 1.8.1 and later.                                */
00008 /*                                                                    */
00009 /* The description and User's Guide ("The decNumber C Library") for   */
00010 /* this software is called decNumber.pdf.  This document is           */
00011 /* available, together with arithmetic and format specifications,     */
00012 /* testcases, and Web links, at: http://www2.hursley.ibm.com/decimal  */
00013 /*                                                                    */
00014 /* Please send comments, suggestions, and corrections to the author:  */
00015 /*   mfc@uk.ibm.com                                                   */
00016 /*   Mike Cowlishaw, IBM Fellow                                       */
00017 /*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
00018 /* ------------------------------------------------------------------ */
00019 /* This header file is included by all modules in the decNumber       */
00020 /* library, and contains local type definitions, tuning parameters,   */
00021 /* etc.  It should not need to be used by application programs.       */
00022 /* decNumber.h or one of decDouble (etc.) must be included first.     */
00023 /* ------------------------------------------------------------------ */
00024 
00025 #if !defined(DECNUMBERLOC)
00026   #define DECNUMBERLOC
00027   #define DECVERSION    "decNumber 3.50" /* Package Version [16 max.] */
00028   #define DECNLAUTHOR   "Mike Cowlishaw"              /* Who to blame */
00029 
00030 #include <stdlib.h>
00031   //#include <stdlib.h>         /* for abs                              */
00032 #include <string.h>
00033   //#include <string.h>         /* for memset, strcpy                   */
00034 
00035   /* Conditional code flag -- set this to match hardware platform     */
00036   #define DECLITEND 1         /* 1=little-endian, 0=big-endian        */
00037 
00038   /* Conditional code flag -- set this to 1 for best performance      */
00039   #define DECUSE64  0         /* 1=use int64s, 0=int32 & smaller only */
00040 
00041   /* Conditional check flags -- set these to 0 for best performance   */
00042   #define DECCHECK  0         /* 1 to enable robust checking          */
00043   #define DECALLOC  0         /* 1 to enable memory accounting        */
00044   #define DECTRACE  0         /* 1 to trace certain internals, etc.   */
00045 
00046   /* Tuning parameter for decNumber (arbitrary precision) module      */
00047   #define DECBUFFER 36        /* Size basis for local buffers.  This  */
00048                               /* should be a common maximum precision */
00049                               /* rounded up to a multiple of 4; must  */
00050                               /* be zero or positive.                 */
00051 
00052   /* ---------------------------------------------------------------- */
00053   /* Definitions for all modules (general-purpose)                    */
00054   /* ---------------------------------------------------------------- */
00055 
00056   /* Local names for common types -- for safety, decNumber modules do */
00057   /* not use int or long directly.                                    */
00058   #define Flag   uint8_t
00059   #define Byte   int8_t
00060   #define uByte  uint8_t
00061   #define Short  int16_t
00062   #define uShort uint16_t
00063   #define Int    int32_t
00064   #define uInt   uint32_t
00065   #define Unit   decNumberUnit
00066   #if DECUSE64
00067   #define Long   int64_t
00068   #define uLong  uint64_t
00069   #endif
00070 
00071   /* Development-use definitions                                      */
00072   typedef long int LI;        /* for printf arguments only            */
00073   #define DECNOINT  0         /* 1 to check no internal use of 'int'  */
00074   #if DECNOINT
00075     /* if these interfere with your C includes, do not set DECNOINT   */
00076     #define  int ?            /* enable to ensure that plain C 'int'  */
00077     #define  long ??          /* .. or 'long' types are not used      */
00078   #endif
00079 
00080   /* Shared lookup tables                                             */
00081   extern const uByte  DECSTICKYTAB[10]; /* re-round digits if sticky  */
00082   extern const uInt   DECPOWERS[10];    /* powers of ten table        */
00083   /* The following are included from decDPD.h                         */
00084   extern const uShort DPD2BIN[1024];    /* DPD -> 0-999               */
00085   extern const uShort BIN2DPD[1000];    /* 0-999 -> DPD               */
00086   extern const uInt   DPD2BINK[1024];   /* DPD -> 0-999000            */
00087   extern const uInt   DPD2BINM[1024];   /* DPD -> 0-999000000         */
00088   extern const uByte  DPD2BCD8[4096];   /* DPD -> ddd + len           */
00089   extern const uByte  BIN2BCD8[4000];   /* 0-999 -> ddd + len         */
00090   extern const uShort BCD2DPD[2458];    /* 0-0x999 -> DPD (0x999=2457)*/
00091 
00092   /* LONGMUL32HI -- set w=(u*v)>>32, where w, u, and v are uInts      */
00093   /* (that is, sets w to be the high-order word of the 64-bit result; */
00094   /* the low-order word is simply u*v.)                               */
00095   /* This version is derived from Knuth via Hacker's Delight;         */
00096   /* it seems to optimize better than some others tried               */
00097   #define LONGMUL32HI(w, u, v) {             \
00098     uInt u0, u1, v0, v1, w0, w1, w2, t;      \
00099     u0=u & 0xffff; u1=u>>16;                 \
00100     v0=v & 0xffff; v1=v>>16;                 \
00101     w0=u0*v0;                                \
00102     t=u1*v0 + (w0>>16);                      \
00103     w1=t & 0xffff; w2=t>>16;                 \
00104     w1=u0*v1 + w1;                           \
00105     (w)=u1*v1 + w2 + (w1>>16);}
00106 
00107   /* ROUNDUP -- round an integer up to a multiple of n                */
00108   #define ROUNDUP(i, n) ((((i)+(n)-1)/n)*n)
00109 
00110   /* ROUNDDOWN -- round an integer down to a multiple of n            */
00111   #define ROUNDDOWN(i, n) (((i)/n)*n)
00112   #define ROUNDDOWN4(i)   ((i)&~3)      /* special for n=4            */
00113 
00114   /* References to multi-byte sequences under different sizes         */
00115   /* Refer to a uInt from four bytes starting at a char* or uByte*,   */
00116   /* etc.                                                             */
00117   #define UINTAT(b)   (*((uInt   *)(b)))
00118   #define USHORTAT(b) (*((uShort *)(b)))
00119   #define UBYTEAT(b)  (*((uByte  *)(b)))
00120 
00121   /* X10 and X100 -- multiply integer i by 10 or 100                  */
00122   /* [shifts are usually faster than multiply; could be conditional]  */
00123   #define X10(i)  (((i)<<1)+((i)<<3))
00124   #define X100(i) (((i)<<2)+((i)<<5)+((i)<<6))
00125 
00126   /* MAXI and MINI -- general max & min (not in ANSI) for integers    */
00127   #define MAXI(x,y) ((x)<(y)?(y):(x))
00128   #define MINI(x,y) ((x)>(y)?(y):(x))
00129 
00130   /* Useful constants                                                 */
00131   #define BILLION      1000000000            /* 10**9                 */
00132   /* CHARMASK: 0x30303030 for ASCII/UTF8; 0xF0F0F0F0 for EBCDIC       */
00133   #define CHARMASK ((((((((uInt)'0')<<8)+'0')<<8)+'0')<<8)+'0')
00134 
00135 
00136   /* ---------------------------------------------------------------- */
00137   /* Definitions for arbitary-precision modules (only valid after     */
00138   /* decNumber.h has been included)                                   */
00139   /* ---------------------------------------------------------------- */
00140 
00141   /* Limits and constants                                             */
00142   #define DECNUMMAXP 999999999  /* maximum precision code can handle  */
00143   #define DECNUMMAXE 999999999  /* maximum adjusted exponent ditto    */
00144   #define DECNUMMINE -999999999 /* minimum adjusted exponent ditto    */
00145   #if (DECNUMMAXP != DEC_MAX_DIGITS)
00146     #error Maximum digits mismatch
00147   #endif
00148   #if (DECNUMMAXE != DEC_MAX_EMAX)
00149     #error Maximum exponent mismatch
00150   #endif
00151   #if (DECNUMMINE != DEC_MIN_EMIN)
00152     #error Minimum exponent mismatch
00153   #endif
00154 
00155   /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN       */
00156   /* digits, and D2UTABLE -- the initializer for the D2U table        */
00157   #if   DECDPUN==1
00158     #define DECDPUNMAX 9
00159     #define D2UTABLE {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,  \
00160                       18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, \
00161                       33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, \
00162                       48,49}
00163   #elif DECDPUN==2
00164     #define DECDPUNMAX 99
00165     #define D2UTABLE {0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,  \
00166                       11,11,12,12,13,13,14,14,15,15,16,16,17,17,18, \
00167                       18,19,19,20,20,21,21,22,22,23,23,24,24,25}
00168   #elif DECDPUN==3
00169     #define DECDPUNMAX 999
00170     #define D2UTABLE {0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,  \
00171                       8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13, \
00172                       13,14,14,14,15,15,15,16,16,16,17}
00173   #elif DECDPUN==4
00174     #define DECDPUNMAX 9999
00175     #define D2UTABLE {0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,  \
00176                       6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11, \
00177                       11,11,11,12,12,12,12,13}
00178   #elif DECDPUN==5
00179     #define DECDPUNMAX 99999
00180     #define D2UTABLE {0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,  \
00181                       5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,  \
00182                       9,9,10,10,10,10}
00183   #elif DECDPUN==6
00184     #define DECDPUNMAX 999999
00185     #define D2UTABLE {0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,  \
00186                       4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8,  \
00187                       8,8,8,8,8,9}
00188   #elif DECDPUN==7
00189     #define DECDPUNMAX 9999999
00190     #define D2UTABLE {0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,  \
00191                       4,4,4,4,4,4,4,5,5,5,5,5,5,5,6,6,6,6,6,6,6,7,  \
00192                       7,7,7,7,7,7}
00193   #elif DECDPUN==8
00194     #define DECDPUNMAX 99999999
00195     #define D2UTABLE {0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,  \
00196                       3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,  \
00197                       6,6,6,6,6,7}
00198   #elif DECDPUN==9
00199     #define DECDPUNMAX 999999999
00200     #define D2UTABLE {0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,  \
00201                       3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,  \
00202                       5,5,6,6,6,6}
00203   #elif defined(DECDPUN)
00204     #error DECDPUN must be in the range 1-9
00205   #endif
00206 
00207   /* ----- Shared data (in decNumber.c) ----- */
00208   /* Public lookup table used by the D2U macro (see below)            */
00209   #define DECMAXD2U 49
00210   extern const uByte d2utable[DECMAXD2U+1];
00211 
00212   /* ----- Macros ----- */
00213   /* ISZERO -- return true if decNumber dn is a zero                  */
00214   /* [performance-critical in some situations]                        */
00215   #define ISZERO(dn) decNumberIsZero(dn)     /* now just a local name */
00216 
00217   /* D2U -- return the number of Units needed to hold d digits        */
00218   /* (runtime version, with table lookaside for small d)              */
00219   #if DECDPUN==8
00220     #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+7)>>3))
00221   #elif DECDPUN==4
00222     #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+3)>>2))
00223   #else
00224     #define D2U(d) ((d)<=DECMAXD2U?d2utable[d]:((d)+DECDPUN-1)/DECDPUN)
00225   #endif
00226   /* SD2U -- static D2U macro (for compile-time calculation)          */
00227   #define SD2U(d) (((d)+DECDPUN-1)/DECDPUN)
00228 
00229   /* MSUDIGITS -- returns digits in msu, from digits, calculated      */
00230   /* using D2U                                                        */
00231   #define MSUDIGITS(d) ((d)-(D2U(d)-1)*DECDPUN)
00232 
00233   /* D2N -- return the number of decNumber structs that would be      */
00234   /* needed to contain that number of digits (and the initial         */
00235   /* decNumber struct) safely.  Note that one Unit is included in the */
00236   /* initial structure.  Used for allocating space that is aligned on */
00237   /* a decNumber struct boundary. */
00238   #define D2N(d) \
00239     ((((SD2U(d)-1)*sizeof(Unit))+sizeof(decNumber)*2-1)/sizeof(decNumber))
00240 
00241   /* TODIGIT -- macro to remove the leading digit from the unsigned   */
00242   /* integer u at column cut (counting from the right, LSD=0) and     */
00243   /* place it as an ASCII character into the character pointed to by  */
00244   /* c.  Note that cut must be <= 9, and the maximum value for u is   */
00245   /* 2,000,000,000 (as is needed for negative exponents of            */
00246   /* subnormals).  The unsigned integer pow is used as a temporary    */
00247   /* variable. */
00248   #define TODIGIT(u, cut, c, pow) {       \
00249     *(c)='0';                             \
00250     pow=DECPOWERS[cut]*2;                 \
00251     if ((u)>pow) {                        \
00252       pow*=4;                             \
00253       if ((u)>=pow) {(u)-=pow; *(c)+=8;}  \
00254       pow/=2;                             \
00255       if ((u)>=pow) {(u)-=pow; *(c)+=4;}  \
00256       pow/=2;                             \
00257       }                                   \
00258     if ((u)>=pow) {(u)-=pow; *(c)+=2;}    \
00259     pow/=2;                               \
00260     if ((u)>=pow) {(u)-=pow; *(c)+=1;}    \
00261     }
00262 
00263   /* ---------------------------------------------------------------- */
00264   /* Definitions for fixed-precision modules (only valid after        */
00265   /* decSingle.h, decDouble.h, or decQuad.h has been included)        */
00266   /* ---------------------------------------------------------------- */
00267 
00268   /* bcdnum -- a structure describing a format-independent finite     */
00269   /* number, whose coefficient is a string of bcd8 uBytes             */
00270   typedef struct {
00271     uByte   *msd;             /* -> most significant digit            */
00272     uByte   *lsd;             /* -> least ditto                       */
00273     uInt     sign;            /* 0=positive, DECFLOAT_Sign=negative   */
00274     Int      exponent;        /* Unadjusted signed exponent (q), or   */
00275                               /* DECFLOAT_NaN etc. for a special      */
00276     } bcdnum;
00277 
00278   /* Test if exponent or bcdnum exponent must be a special, etc.      */
00279   #define EXPISSPECIAL(exp) ((exp)>=DECFLOAT_MinSp)
00280   #define EXPISINF(exp) (exp==DECFLOAT_Inf)
00281   #define EXPISNAN(exp) (exp==DECFLOAT_qNaN || exp==DECFLOAT_sNaN)
00282   #define NUMISSPECIAL(num) (EXPISSPECIAL((num)->exponent))
00283 
00284   /* Refer to a 32-bit word or byte in a decFloat (df) by big-endian  */
00285   /* (array) notation (the 0 word or byte contains the sign bit),     */
00286   /* automatically adjusting for endianness; similarly address a word */
00287   /* in the next-wider format (decFloatWider, or dfw)                 */
00288   #define DECWORDS  (DECBYTES/4)
00289   #define DECWWORDS (DECWBYTES/4)
00290   #if DECLITEND
00291     #define DFWORD(df, off) UINTAT((df)->bytes+(DECWORDS-1-(off))*4)
00292     #define DFBYTE(df, off) UBYTEAT((df)->bytes+(DECBYTES-1-(off)))
00293     #define DFWWORD(dfw, off) UINTAT((dfw)->bytes+(DECWWORDS-1-(off))*4)
00294   #else
00295     #define DFWORD(df, off) UINTAT((df)->bytes+(off)*4)
00296     #define DFBYTE(df, off) UBYTEAT((df)->bytes+(off))
00297     #define DFWWORD(dfw, off) UINTAT((dfw)->bytes+(off)*4)
00298   #endif
00299 
00300   /* Tests for sign or specials, directly on DECFLOATs                */
00301   #define DFISSIGNED(df)   (DFWORD(df, 0)&0x80000000)
00302   #define DFISSPECIAL(df) ((DFWORD(df, 0)&0x78000000)==0x78000000)
00303   #define DFISINF(df)     ((DFWORD(df, 0)&0x7c000000)==0x78000000)
00304   #define DFISNAN(df)     ((DFWORD(df, 0)&0x7c000000)==0x7c000000)
00305   #define DFISQNAN(df)    ((DFWORD(df, 0)&0x7e000000)==0x7c000000)
00306   #define DFISSNAN(df)    ((DFWORD(df, 0)&0x7e000000)==0x7e000000)
00307 
00308   /* Shared lookup tables                                             */
00309   extern const uInt   DECCOMBMSD[64];   /* Combination field -> MSD   */
00310   extern const uInt   DECCOMBFROM[48];  /* exp+msd -> Combination     */
00311 
00312   /* Private generic (utility) routine                                */
00313   #if DECCHECK || DECTRACE
00314     extern void decShowNum(const bcdnum *, const char *);
00315   #endif
00316 
00317   /* Format-dependent macros and constants                            */
00318   #if defined(DECPMAX)
00319 
00320     /* Useful constants                                               */
00321     #define DECPMAX9  (ROUNDUP(DECPMAX, 9)/9)  /* 'Pmax' in 10**9s    */
00322     /* Top words for a zero                                           */
00323     #define SINGLEZERO   0x22500000
00324     #define DOUBLEZERO   0x22380000
00325     #define QUADZERO     0x22080000
00326     /* [ZEROWORD is defined to be one of these in the DFISZERO macro] */
00327 
00328     /* Format-dependent common tests:                                 */
00329     /*   DFISZERO   -- test for (any) zero                            */
00330     /*   DFISCCZERO -- test for coefficient continuation being zero   */
00331     /*   DFISCC01   -- test for coefficient contains only 0s and 1s   */
00332     /*   DFISINT    -- test for finite and exponent q=0               */
00333     /*   DFISUINT01 -- test for sign=0, finite, exponent q=0, and     */
00334     /*                 MSD=0 or 1                                     */
00335     /*   ZEROWORD is also defined here.                               */
00336     /* In DFISZERO the first test checks the least-significant word   */
00337     /* (most likely to be non-zero); the penultimate tests MSD and    */
00338     /* DPDs in the signword, and the final test excludes specials and */
00339     /* MSD>7.  DFISINT similarly has to allow for the two forms of    */
00340     /* MSD codes.  DFISUINT01 only has to allow for one form of MSD   */
00341     /* code.                                                          */
00342     #if DECPMAX==7
00343       #define ZEROWORD SINGLEZERO
00344       /* [test macros not needed except for Zero]                     */
00345       #define DFISZERO(df)  ((DFWORD(df, 0)&0x1c0fffff)==0         \
00346                           && (DFWORD(df, 0)&0x60000000)!=0x60000000)
00347     #elif DECPMAX==16
00348       #define ZEROWORD DOUBLEZERO
00349       #define DFISZERO(df)  ((DFWORD(df, 1)==0                     \
00350                           && (DFWORD(df, 0)&0x1c03ffff)==0         \
00351                           && (DFWORD(df, 0)&0x60000000)!=0x60000000))
00352       #define DFISINT(df) ((DFWORD(df, 0)&0x63fc0000)==0x22380000  \
00353                          ||(DFWORD(df, 0)&0x7bfc0000)==0x6a380000)
00354       #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbfc0000)==0x22380000)
00355       #define DFISCCZERO(df) (DFWORD(df, 1)==0                     \
00356                           && (DFWORD(df, 0)&0x0003ffff)==0)
00357       #define DFISCC01(df)  ((DFWORD(df, 0)&~0xfffc9124)==0        \
00358                           && (DFWORD(df, 1)&~0x49124491)==0)
00359     #elif DECPMAX==34
00360       #define ZEROWORD QUADZERO
00361       #define DFISZERO(df)  ((DFWORD(df, 3)==0                     \
00362                           &&  DFWORD(df, 2)==0                     \
00363                           &&  DFWORD(df, 1)==0                     \
00364                           && (DFWORD(df, 0)&0x1c003fff)==0         \
00365                           && (DFWORD(df, 0)&0x60000000)!=0x60000000))
00366       #define DFISINT(df) ((DFWORD(df, 0)&0x63ffc000)==0x22080000  \
00367                          ||(DFWORD(df, 0)&0x7bffc000)==0x6a080000)
00368       #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbffc000)==0x22080000)
00369       #define DFISCCZERO(df) (DFWORD(df, 3)==0                     \
00370                           &&  DFWORD(df, 2)==0                     \
00371                           &&  DFWORD(df, 1)==0                     \
00372                           && (DFWORD(df, 0)&0x00003fff)==0)
00373 
00374       #define DFISCC01(df)   ((DFWORD(df, 0)&~0xffffc912)==0       \
00375                           &&  (DFWORD(df, 1)&~0x44912449)==0       \
00376                           &&  (DFWORD(df, 2)&~0x12449124)==0       \
00377                           &&  (DFWORD(df, 3)&~0x49124491)==0)
00378     #endif
00379 
00380     /* Macros to test if a certain 10 bits of a uInt or pair of uInts */
00381     /* are a canonical declet [higher or lower bits are ignored].     */
00382     /* declet is at offset 0 (from the right) in a uInt:              */
00383     #define CANONDPD(dpd) (((dpd)&0x300)==0 || ((dpd)&0x6e)!=0x6e)
00384     /* declet is at offset k (a multiple of 2) in a uInt:             */
00385     #define CANONDPDOFF(dpd, k) (((dpd)&(0x300<<(k)))==0            \
00386       || ((dpd)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
00387     /* declet is at offset k (a multiple of 2) in a pair of uInts:    */
00388     /* [the top 2 bits will always be in the more-significant uInt]   */
00389     #define CANONDPDTWO(hi, lo, k) (((hi)&(0x300>>(32-(k))))==0     \
00390       || ((hi)&(0x6e>>(32-(k))))!=(0x6e>>(32-(k)))                  \
00391       || ((lo)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
00392 
00393     /* Macro to test whether a full-length (length DECPMAX) BCD8      */
00394     /* coefficient is zero                                            */
00395     /* test just the LSWord first, then the remainder                 */
00396     #if DECPMAX==7
00397       #define ISCOEFFZERO(u) (UINTAT((u)+DECPMAX-4)==0              \
00398         && UINTAT((u)+DECPMAX-7)==0)
00399     #elif DECPMAX==16
00400       #define ISCOEFFZERO(u) (UINTAT((u)+DECPMAX-4)==0              \
00401         && (UINTAT((u)+DECPMAX-8)+UINTAT((u)+DECPMAX-12)            \
00402            +UINTAT((u)+DECPMAX-16))==0)
00403     #elif DECPMAX==34
00404       #define ISCOEFFZERO(u) (UINTAT((u)+DECPMAX-4)==0              \
00405         && (UINTAT((u)+DECPMAX-8) +UINTAT((u)+DECPMAX-12)           \
00406            +UINTAT((u)+DECPMAX-16)+UINTAT((u)+DECPMAX-20)           \
00407            +UINTAT((u)+DECPMAX-24)+UINTAT((u)+DECPMAX-28)           \
00408            +UINTAT((u)+DECPMAX-32)+USHORTAT((u)+DECPMAX-34))==0)
00409     #endif
00410 
00411     /* Macros and masks for the exponent continuation field and MSD   */
00412     /* Get the exponent continuation from a decFloat *df as an Int    */
00413     #define GETECON(df) ((Int)((DFWORD((df), 0)&0x03ffffff)>>(32-6-DECECONL)))
00414     /* Ditto, from the next-wider format                              */
00415     #define GETWECON(df) ((Int)((DFWWORD((df), 0)&0x03ffffff)>>(32-6-DECWECONL)))
00416     /* Get the biased exponent similarly                              */
00417     #define GETEXP(df)  ((Int)(DECCOMBEXP[DFWORD((df), 0)>>26]+GETECON(df)))
00418     /* Get the unbiased exponent similarly                            */
00419     #define GETEXPUN(df) ((Int)GETEXP(df)-DECBIAS)
00420     /* Get the MSD similarly (as uInt)                                */
00421     #define GETMSD(df)   (DECCOMBMSD[DFWORD((df), 0)>>26])
00422 
00423     /* Compile-time computes of the exponent continuation field masks */
00424     /* full exponent continuation field:                              */
00425     #define ECONMASK ((0x03ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
00426     /* same, not including its first digit (the qNaN/sNaN selector):  */
00427     #define ECONNANMASK ((0x01ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
00428 
00429     /* Macros to decode the coefficient in a finite decFloat *df into */
00430     /* a BCD string (uByte *bcdin) of length DECPMAX uBytes           */
00431 
00432     /* In-line sequence to convert 10 bits at right end of uInt dpd   */
00433     /* to three BCD8 digits starting at uByte u.  Note that an extra  */
00434     /* byte is written to the right of the three digits because this  */
00435     /* moves four at a time for speed; the alternative macro moves    */
00436     /* exactly three bytes                                            */
00437     #define dpd2bcd8(u, dpd) {                           \
00438       UINTAT(u)=UINTAT(&DPD2BCD8[((dpd)&0x3ff)*4]);}
00439 
00440     #define dpd2bcd83(u, dpd) {                          \
00441       *(u)=DPD2BCD8[((dpd)&0x3ff)*4];                    \
00442       *(u+1)=DPD2BCD8[((dpd)&0x3ff)*4+1];                \
00443       *(u+2)=DPD2BCD8[((dpd)&0x3ff)*4+2];}
00444 
00445     /* Decode the declets.  After extracting each one, it is decoded  */
00446     /* to BCD8 using a table lookup (also used for variable-length    */
00447     /* decode).  Each DPD decode is 3 bytes BCD8 plus a one-byte      */
00448     /* length which is not used, here).  Fixed-length 4-byte moves    */
00449     /* are fast, however, almost everywhere, and so are used except   */
00450     /* for the final three bytes (to avoid overrun).  The code below  */
00451     /* is 36 instructions for Doubles and about 70 for Quads, even    */
00452     /* on IA32.                                                       */
00453 
00454     /* Two macros are defined for each format:                        */
00455     /*   GETCOEFF extracts the coefficient of the current format      */
00456     /*   GETWCOEFF extracts the coefficient of the next-wider format. */
00457     /* The latter is a copy of the next-wider GETCOEFF using DFWWORD. */
00458 
00459     #if DECPMAX==7
00460     #define GETCOEFF(df, bcd) {                          \
00461       uInt sourhi=DFWORD(df, 0);                         \
00462       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
00463       dpd2bcd8(bcd+1, sourhi>>10);                       \
00464       dpd2bcd83(bcd+4, sourhi);}
00465     #define GETWCOEFF(df, bcd) {                         \
00466       uInt sourhi=DFWWORD(df, 0);                        \
00467       uInt sourlo=DFWWORD(df, 1);                        \
00468       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
00469       dpd2bcd8(bcd+1, sourhi>>8);                        \
00470       dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30));       \
00471       dpd2bcd8(bcd+7, sourlo>>20);                       \
00472       dpd2bcd8(bcd+10, sourlo>>10);                      \
00473       dpd2bcd83(bcd+13, sourlo);}
00474 
00475     #elif DECPMAX==16
00476     #define GETCOEFF(df, bcd) {                          \
00477       uInt sourhi=DFWORD(df, 0);                         \
00478       uInt sourlo=DFWORD(df, 1);                         \
00479       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
00480       dpd2bcd8(bcd+1, sourhi>>8);                        \
00481       dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30));       \
00482       dpd2bcd8(bcd+7, sourlo>>20);                       \
00483       dpd2bcd8(bcd+10, sourlo>>10);                      \
00484       dpd2bcd83(bcd+13, sourlo);}
00485     #define GETWCOEFF(df, bcd) {                         \
00486       uInt sourhi=DFWWORD(df, 0);                        \
00487       uInt sourmh=DFWWORD(df, 1);                        \
00488       uInt sourml=DFWWORD(df, 2);                        \
00489       uInt sourlo=DFWWORD(df, 3);                        \
00490       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
00491       dpd2bcd8(bcd+1, sourhi>>4);                        \
00492       dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26));     \
00493       dpd2bcd8(bcd+7, sourmh>>16);                       \
00494       dpd2bcd8(bcd+10, sourmh>>6);                       \
00495       dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28));    \
00496       dpd2bcd8(bcd+16, sourml>>18);                      \
00497       dpd2bcd8(bcd+19, sourml>>8);                       \
00498       dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30));    \
00499       dpd2bcd8(bcd+25, sourlo>>20);                      \
00500       dpd2bcd8(bcd+28, sourlo>>10);                      \
00501       dpd2bcd83(bcd+31, sourlo);}
00502 
00503     #elif DECPMAX==34
00504     #define GETCOEFF(df, bcd) {                          \
00505       uInt sourhi=DFWORD(df, 0);                         \
00506       uInt sourmh=DFWORD(df, 1);                         \
00507       uInt sourml=DFWORD(df, 2);                         \
00508       uInt sourlo=DFWORD(df, 3);                         \
00509       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
00510       dpd2bcd8(bcd+1, sourhi>>4);                        \
00511       dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26));     \
00512       dpd2bcd8(bcd+7, sourmh>>16);                       \
00513       dpd2bcd8(bcd+10, sourmh>>6);                       \
00514       dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28));    \
00515       dpd2bcd8(bcd+16, sourml>>18);                      \
00516       dpd2bcd8(bcd+19, sourml>>8);                       \
00517       dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30));    \
00518       dpd2bcd8(bcd+25, sourlo>>20);                      \
00519       dpd2bcd8(bcd+28, sourlo>>10);                      \
00520       dpd2bcd83(bcd+31, sourlo);}
00521 
00522       #define GETWCOEFF(df, bcd) {??} /* [should never be used]       */
00523     #endif
00524 
00525     /* Macros to decode the coefficient in a finite decFloat *df into */
00526     /* a base-billion uInt array, with the least-significant          */
00527     /* 0-999999999 'digit' at offset 0.                               */
00528 
00529     /* Decode the declets.  After extracting each one, it is decoded  */
00530     /* to binary using a table lookup.  Three tables are used; one    */
00531     /* the usual DPD to binary, the other two pre-multiplied by 1000  */
00532     /* and 1000000 to avoid multiplication during decode.  These      */
00533     /* tables can also be used for multiplying up the MSD as the DPD  */
00534     /* code for 0 through 9 is the identity.                          */
00535     #define DPD2BIN0 DPD2BIN         /* for prettier code             */
00536 
00537     #if DECPMAX==7
00538     #define GETCOEFFBILL(df, buf) {                           \
00539       uInt sourhi=DFWORD(df, 0);                              \
00540       (buf)[0]=DPD2BIN0[sourhi&0x3ff]                         \
00541               +DPD2BINK[(sourhi>>10)&0x3ff]                   \
00542               +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
00543 
00544     #elif DECPMAX==16
00545     #define GETCOEFFBILL(df, buf) {                           \
00546       uInt sourhi, sourlo;                                    \
00547       sourlo=DFWORD(df, 1);                                   \
00548       (buf)[0]=DPD2BIN0[sourlo&0x3ff]                         \
00549               +DPD2BINK[(sourlo>>10)&0x3ff]                   \
00550               +DPD2BINM[(sourlo>>20)&0x3ff];                  \
00551       sourhi=DFWORD(df, 0);                                   \
00552       (buf)[1]=DPD2BIN0[((sourhi<<2) | (sourlo>>30))&0x3ff]   \
00553               +DPD2BINK[(sourhi>>8)&0x3ff]                    \
00554               +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
00555 
00556     #elif DECPMAX==34
00557     #define GETCOEFFBILL(df, buf) {                           \
00558       uInt sourhi, sourmh, sourml, sourlo;                    \
00559       sourlo=DFWORD(df, 3);                                   \
00560       (buf)[0]=DPD2BIN0[sourlo&0x3ff]                         \
00561               +DPD2BINK[(sourlo>>10)&0x3ff]                   \
00562               +DPD2BINM[(sourlo>>20)&0x3ff];                  \
00563       sourml=DFWORD(df, 2);                                   \
00564       (buf)[1]=DPD2BIN0[((sourml<<2) | (sourlo>>30))&0x3ff]   \
00565               +DPD2BINK[(sourml>>8)&0x3ff]                    \
00566               +DPD2BINM[(sourml>>18)&0x3ff];                  \
00567       sourmh=DFWORD(df, 1);                                   \
00568       (buf)[2]=DPD2BIN0[((sourmh<<4) | (sourml>>28))&0x3ff]   \
00569               +DPD2BINK[(sourmh>>6)&0x3ff]                    \
00570               +DPD2BINM[(sourmh>>16)&0x3ff];                  \
00571       sourhi=DFWORD(df, 0);                                   \
00572       (buf)[3]=DPD2BIN0[((sourhi<<6) | (sourmh>>26))&0x3ff]   \
00573               +DPD2BINK[(sourhi>>4)&0x3ff]                    \
00574               +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
00575 
00576     #endif
00577 
00578     /* Macros to decode the coefficient in a finite decFloat *df into */
00579     /* a base-thousand uInt array, with the least-significant 0-999   */
00580     /* 'digit' at offset 0.                                           */
00581 
00582     /* Decode the declets.  After extracting each one, it is decoded  */
00583     /* to binary using a table lookup.                                */
00584     #if DECPMAX==7
00585     #define GETCOEFFTHOU(df, buf) {                           \
00586       uInt sourhi=DFWORD(df, 0);                              \
00587       (buf)[0]=DPD2BIN[sourhi&0x3ff];                         \
00588       (buf)[1]=DPD2BIN[(sourhi>>10)&0x3ff];                   \
00589       (buf)[2]=DECCOMBMSD[sourhi>>26];}
00590 
00591     #elif DECPMAX==16
00592     #define GETCOEFFTHOU(df, buf) {                           \
00593       uInt sourhi, sourlo;                                    \
00594       sourlo=DFWORD(df, 1);                                   \
00595       (buf)[0]=DPD2BIN[sourlo&0x3ff];                         \
00596       (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff];                   \
00597       (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff];                   \
00598       sourhi=DFWORD(df, 0);                                   \
00599       (buf)[3]=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff];   \
00600       (buf)[4]=DPD2BIN[(sourhi>>8)&0x3ff];                    \
00601       (buf)[5]=DECCOMBMSD[sourhi>>26];}
00602 
00603     #elif DECPMAX==34
00604     #define GETCOEFFTHOU(df, buf) {                           \
00605       uInt sourhi, sourmh, sourml, sourlo;                    \
00606       sourlo=DFWORD(df, 3);                                   \
00607       (buf)[0]=DPD2BIN[sourlo&0x3ff];                         \
00608       (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff];                   \
00609       (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff];                   \
00610       sourml=DFWORD(df, 2);                                   \
00611       (buf)[3]=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff];   \
00612       (buf)[4]=DPD2BIN[(sourml>>8)&0x3ff];                    \
00613       (buf)[5]=DPD2BIN[(sourml>>18)&0x3ff];                   \
00614       sourmh=DFWORD(df, 1);                                   \
00615       (buf)[6]=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff];   \
00616       (buf)[7]=DPD2BIN[(sourmh>>6)&0x3ff];                    \
00617       (buf)[8]=DPD2BIN[(sourmh>>16)&0x3ff];                   \
00618       sourhi=DFWORD(df, 0);                                   \
00619       (buf)[9]=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff];   \
00620       (buf)[10]=DPD2BIN[(sourhi>>4)&0x3ff];                   \
00621       (buf)[11]=DECCOMBMSD[sourhi>>26];}
00622 
00623     #endif
00624 
00625     /* Set a decFloat to the maximum positive finite number (Nmax)    */
00626     #if DECPMAX==7
00627     #define DFSETNMAX(df)            \
00628       {DFWORD(df, 0)=0x77f3fcff;}
00629     #elif DECPMAX==16
00630     #define DFSETNMAX(df)            \
00631       {DFWORD(df, 0)=0x77fcff3f;     \
00632        DFWORD(df, 1)=0xcff3fcff;}
00633     #elif DECPMAX==34
00634     #define DFSETNMAX(df)            \
00635       {DFWORD(df, 0)=0x77ffcff3;     \
00636        DFWORD(df, 1)=0xfcff3fcf;     \
00637        DFWORD(df, 2)=0xf3fcff3f;     \
00638        DFWORD(df, 3)=0xcff3fcff;}
00639     #endif
00640 
00641   /* [end of format-dependent macros and constants]                   */
00642   #endif
00643 
00644 #else
00645   #error decNumberLocal included more than once
00646 #endif