P:/wx_LIB/Dcml/decNumberLocal.h

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

Généré le Mon Jul 20 18:22:14 2009 pour Decimal par  doxygen 1.5.1-p1