Home | History | Annotate | Download | only in i18n
      1 /* ------------------------------------------------------------------ */
      2 /* decNumber package local type, tuning, and macro definitions        */
      3 /* ------------------------------------------------------------------ */
      4 /* Copyright (c) IBM Corporation, 2000-2010.   All rights reserved.   */
      5 /*                                                                    */
      6 /* This software is made available under the terms of the             */
      7 /* ICU License -- ICU 1.8.1 and later.                                */
      8 /*                                                                    */
      9 /* The description and User's Guide ("The decNumber C Library") for   */
     10 /* this software is called decNumber.pdf.  This document is           */
     11 /* available, together with arithmetic and format specifications,     */
     12 /* testcases, and Web links, on the General Decimal Arithmetic page.  */
     13 /*                                                                    */
     14 /* Please send comments, suggestions, and corrections to the author:  */
     15 /*   mfc (at) uk.ibm.com                                                   */
     16 /*   Mike Cowlishaw, IBM Fellow                                       */
     17 /*   IBM UK, PO Box 31, Birmingham Road, Warwick CV34 5JL, UK         */
     18 /* ------------------------------------------------------------------ */
     19 /* This header file is included by all modules in the decNumber       */
     20 /* library, and contains local type definitions, tuning parameters,   */
     21 /* etc.  It should not need to be used by application programs.       */
     22 /* decNumber.h or one of decDouble (etc.) must be included first.     */
     23 /* ------------------------------------------------------------------ */
     24 
     25 #if !defined(DECNUMBERLOC)
     26   #define DECNUMBERLOC
     27   #define DECVERSION    "decNumber 3.61" /* Package Version [16 max.] */
     28   #define DECNLAUTHOR   "Mike Cowlishaw"              /* Who to blame */
     29 
     30   #include <stdlib.h>         /* for abs                              */
     31   #include <string.h>         /* for memset, strcpy                   */
     32 
     33   /* Conditional code flag -- set this to match hardware platform     */
     34   #if !defined(DECLITEND)
     35   #define DECLITEND 1         /* 1=little-endian, 0=big-endian        */
     36   #endif
     37 
     38   /* Conditional code flag -- set this to 1 for best performance      */
     39   #if !defined(DECUSE64)
     40   #define DECUSE64  1         /* 1=use int64s, 0=int32 & smaller only */
     41   #endif
     42 
     43   /* Conditional check flags -- set these to 0 for best performance   */
     44   #if !defined(DECCHECK)
     45   #define DECCHECK  0         /* 1 to enable robust checking          */
     46   #endif
     47   #if !defined(DECALLOC)
     48   #define DECALLOC  0         /* 1 to enable memory accounting        */
     49   #endif
     50   #if !defined(DECTRACE)
     51   #define DECTRACE  0         /* 1 to trace certain internals, etc.   */
     52   #endif
     53 
     54   /* Tuning parameter for decNumber (arbitrary precision) module      */
     55   #if !defined(DECBUFFER)
     56   #define DECBUFFER 36        /* Size basis for local buffers.  This  */
     57                               /* should be a common maximum precision */
     58                               /* rounded up to a multiple of 4; must  */
     59                               /* be zero or positive.                 */
     60   #endif
     61 
     62   /* ---------------------------------------------------------------- */
     63   /* Definitions for all modules (general-purpose)                    */
     64   /* ---------------------------------------------------------------- */
     65 
     66   /* Local names for common types -- for safety, decNumber modules do */
     67   /* not use int or long directly.                                    */
     68   #define Flag   uint8_t
     69   #define Byte   int8_t
     70   #define uByte  uint8_t
     71   #define Short  int16_t
     72   #define uShort uint16_t
     73   #define Int    int32_t
     74   #define uInt   uint32_t
     75   #define Unit   decNumberUnit
     76   #if DECUSE64
     77   #define Long   int64_t
     78   #define uLong  uint64_t
     79   #endif
     80 
     81   /* Development-use definitions                                      */
     82   typedef long int LI;        /* for printf arguments only            */
     83   #define DECNOINT  0         /* 1 to check no internal use of 'int'  */
     84                               /*   or stdint types                    */
     85   #if DECNOINT
     86     /* if these interfere with your C includes, do not set DECNOINT   */
     87     #define int     ?         /* enable to ensure that plain C 'int'  */
     88     #define long    ??        /* .. or 'long' types are not used      */
     89   #endif
     90 
     91   /* Shared lookup tables                                             */
     92   extern const uByte  DECSTICKYTAB[10]; /* re-round digits if sticky  */
     93   extern const uInt   DECPOWERS[10];    /* powers of ten table        */
     94   /* The following are included from decDPD.h                         */
     95   extern const uShort DPD2BIN[1024];    /* DPD -> 0-999               */
     96   extern const uShort BIN2DPD[1000];    /* 0-999 -> DPD               */
     97   extern const uInt   DPD2BINK[1024];   /* DPD -> 0-999000            */
     98   extern const uInt   DPD2BINM[1024];   /* DPD -> 0-999000000         */
     99   extern const uByte  DPD2BCD8[4096];   /* DPD -> ddd + len           */
    100   extern const uByte  BIN2BCD8[4000];   /* 0-999 -> ddd + len         */
    101   extern const uShort BCD2DPD[2458];    /* 0-0x999 -> DPD (0x999=2457)*/
    102 
    103   /* LONGMUL32HI -- set w=(u*v)>>32, where w, u, and v are uInts      */
    104   /* (that is, sets w to be the high-order word of the 64-bit result; */
    105   /* the low-order word is simply u*v.)                               */
    106   /* This version is derived from Knuth via Hacker's Delight;         */
    107   /* it seems to optimize better than some others tried               */
    108   #define LONGMUL32HI(w, u, v) {             \
    109     uInt u0, u1, v0, v1, w0, w1, w2, t;      \
    110     u0=u & 0xffff; u1=u>>16;                 \
    111     v0=v & 0xffff; v1=v>>16;                 \
    112     w0=u0*v0;                                \
    113     t=u1*v0 + (w0>>16);                      \
    114     w1=t & 0xffff; w2=t>>16;                 \
    115     w1=u0*v1 + w1;                           \
    116     (w)=u1*v1 + w2 + (w1>>16);}
    117 
    118   /* ROUNDUP -- round an integer up to a multiple of n                */
    119   #define ROUNDUP(i, n) ((((i)+(n)-1)/n)*n)
    120   #define ROUNDUP4(i)   (((i)+3)&~3)    /* special for n=4            */
    121 
    122   /* ROUNDDOWN -- round an integer down to a multiple of n            */
    123   #define ROUNDDOWN(i, n) (((i)/n)*n)
    124   #define ROUNDDOWN4(i)   ((i)&~3)      /* special for n=4            */
    125 
    126   /* References to multi-byte sequences under different sizes; these  */
    127   /* require locally declared variables, but do not violate strict    */
    128   /* aliasing or alignment (as did the UINTAT simple cast to uInt).   */
    129   /* Variables needed are uswork, uiwork, etc. [so do not use at same */
    130   /* level in an expression, e.g., UBTOUI(x)==UBTOUI(y) may fail].    */
    131 
    132   /* Return a uInt, etc., from bytes starting at a char* or uByte*    */
    133   #define UBTOUS(b)  (memcpy((void *)&uswork, b, 2), uswork)
    134   #define UBTOUI(b)  (memcpy((void *)&uiwork, b, 4), uiwork)
    135 
    136   /* Store a uInt, etc., into bytes starting at a char* or uByte*.    */
    137   /* Returns i, evaluated, for convenience; has to use uiwork because */
    138   /* i may be an expression.                                          */
    139   #define UBFROMUS(b, i)  (uswork=(i), memcpy(b, (void *)&uswork, 2), uswork)
    140   #define UBFROMUI(b, i)  (uiwork=(i), memcpy(b, (void *)&uiwork, 4), uiwork)
    141 
    142   /* X10 and X100 -- multiply integer i by 10 or 100                  */
    143   /* [shifts are usually faster than multiply; could be conditional]  */
    144   #define X10(i)  (((i)<<1)+((i)<<3))
    145   #define X100(i) (((i)<<2)+((i)<<5)+((i)<<6))
    146 
    147   /* MAXI and MINI -- general max & min (not in ANSI) for integers    */
    148   #define MAXI(x,y) ((x)<(y)?(y):(x))
    149   #define MINI(x,y) ((x)>(y)?(y):(x))
    150 
    151   /* Useful constants                                                 */
    152   #define BILLION      1000000000            /* 10**9                 */
    153   /* CHARMASK: 0x30303030 for ASCII/UTF8; 0xF0F0F0F0 for EBCDIC       */
    154   #define CHARMASK ((((((((uInt)'0')<<8)+'0')<<8)+'0')<<8)+'0')
    155 
    156 
    157   /* ---------------------------------------------------------------- */
    158   /* Definitions for arbitary-precision modules (only valid after     */
    159   /* decNumber.h has been included)                                   */
    160   /* ---------------------------------------------------------------- */
    161 
    162   /* Limits and constants                                             */
    163   #define DECNUMMAXP 999999999  /* maximum precision code can handle  */
    164   #define DECNUMMAXE 999999999  /* maximum adjusted exponent ditto    */
    165   #define DECNUMMINE -999999999 /* minimum adjusted exponent ditto    */
    166   #if (DECNUMMAXP != DEC_MAX_DIGITS)
    167     #error Maximum digits mismatch
    168   #endif
    169   #if (DECNUMMAXE != DEC_MAX_EMAX)
    170     #error Maximum exponent mismatch
    171   #endif
    172   #if (DECNUMMINE != DEC_MIN_EMIN)
    173     #error Minimum exponent mismatch
    174   #endif
    175 
    176   /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN       */
    177   /* digits, and D2UTABLE -- the initializer for the D2U table        */
    178   #if   DECDPUN==1
    179     #define DECDPUNMAX 9
    180     #define D2UTABLE {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,  \
    181                       18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, \
    182                       33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, \
    183                       48,49}
    184   #elif DECDPUN==2
    185     #define DECDPUNMAX 99
    186     #define D2UTABLE {0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,  \
    187                       11,11,12,12,13,13,14,14,15,15,16,16,17,17,18, \
    188                       18,19,19,20,20,21,21,22,22,23,23,24,24,25}
    189   #elif DECDPUN==3
    190     #define DECDPUNMAX 999
    191     #define D2UTABLE {0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,  \
    192                       8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13, \
    193                       13,14,14,14,15,15,15,16,16,16,17}
    194   #elif DECDPUN==4
    195     #define DECDPUNMAX 9999
    196     #define D2UTABLE {0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,  \
    197                       6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11, \
    198                       11,11,11,12,12,12,12,13}
    199   #elif DECDPUN==5
    200     #define DECDPUNMAX 99999
    201     #define D2UTABLE {0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,  \
    202                       5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9,  \
    203                       9,9,10,10,10,10}
    204   #elif DECDPUN==6
    205     #define DECDPUNMAX 999999
    206     #define D2UTABLE {0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,  \
    207                       4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8,  \
    208                       8,8,8,8,8,9}
    209   #elif DECDPUN==7
    210     #define DECDPUNMAX 9999999
    211     #define D2UTABLE {0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3,  \
    212                       4,4,4,4,4,4,4,5,5,5,5,5,5,5,6,6,6,6,6,6,6,7,  \
    213                       7,7,7,7,7,7}
    214   #elif DECDPUN==8
    215     #define DECDPUNMAX 99999999
    216     #define D2UTABLE {0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,  \
    217                       3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,  \
    218                       6,6,6,6,6,7}
    219   #elif DECDPUN==9
    220     #define DECDPUNMAX 999999999
    221     #define D2UTABLE {0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,  \
    222                       3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,  \
    223                       5,5,6,6,6,6}
    224   #elif defined(DECDPUN)
    225     #error DECDPUN must be in the range 1-9
    226   #endif
    227 
    228   /* ----- Shared data (in decNumber.c) ----- */
    229   /* Public lookup table used by the D2U macro (see below)            */
    230   #define DECMAXD2U 49
    231   extern const uByte d2utable[DECMAXD2U+1];
    232 
    233   /* ----- Macros ----- */
    234   /* ISZERO -- return true if decNumber dn is a zero                  */
    235   /* [performance-critical in some situations]                        */
    236   #define ISZERO(dn) decNumberIsZero(dn)     /* now just a local name */
    237 
    238   /* D2U -- return the number of Units needed to hold d digits        */
    239   /* (runtime version, with table lookaside for small d)              */
    240   #if DECDPUN==8
    241     #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+7)>>3))
    242   #elif DECDPUN==4
    243     #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+3)>>2))
    244   #else
    245     #define D2U(d) ((d)<=DECMAXD2U?d2utable[d]:((d)+DECDPUN-1)/DECDPUN)
    246   #endif
    247   /* SD2U -- static D2U macro (for compile-time calculation)          */
    248   #define SD2U(d) (((d)+DECDPUN-1)/DECDPUN)
    249 
    250   /* MSUDIGITS -- returns digits in msu, from digits, calculated      */
    251   /* using D2U                                                        */
    252   #define MSUDIGITS(d) ((d)-(D2U(d)-1)*DECDPUN)
    253 
    254   /* D2N -- return the number of decNumber structs that would be      */
    255   /* needed to contain that number of digits (and the initial         */
    256   /* decNumber struct) safely.  Note that one Unit is included in the */
    257   /* initial structure.  Used for allocating space that is aligned on */
    258   /* a decNumber struct boundary. */
    259   #define D2N(d) \
    260     ((((SD2U(d)-1)*sizeof(Unit))+sizeof(decNumber)*2-1)/sizeof(decNumber))
    261 
    262   /* TODIGIT -- macro to remove the leading digit from the unsigned   */
    263   /* integer u at column cut (counting from the right, LSD=0) and     */
    264   /* place it as an ASCII character into the character pointed to by  */
    265   /* c.  Note that cut must be <= 9, and the maximum value for u is   */
    266   /* 2,000,000,000 (as is needed for negative exponents of            */
    267   /* subnormals).  The unsigned integer pow is used as a temporary    */
    268   /* variable. */
    269   #define TODIGIT(u, cut, c, pow) {       \
    270     *(c)='0';                             \
    271     pow=DECPOWERS[cut]*2;                 \
    272     if ((u)>pow) {                        \
    273       pow*=4;                             \
    274       if ((u)>=pow) {(u)-=pow; *(c)+=8;}  \
    275       pow/=2;                             \
    276       if ((u)>=pow) {(u)-=pow; *(c)+=4;}  \
    277       pow/=2;                             \
    278       }                                   \
    279     if ((u)>=pow) {(u)-=pow; *(c)+=2;}    \
    280     pow/=2;                               \
    281     if ((u)>=pow) {(u)-=pow; *(c)+=1;}    \
    282     }
    283 
    284   /* ---------------------------------------------------------------- */
    285   /* Definitions for fixed-precision modules (only valid after        */
    286   /* decSingle.h, decDouble.h, or decQuad.h has been included)        */
    287   /* ---------------------------------------------------------------- */
    288 
    289   /* bcdnum -- a structure describing a format-independent finite     */
    290   /* number, whose coefficient is a string of bcd8 uBytes             */
    291   typedef struct {
    292     uByte   *msd;             /* -> most significant digit            */
    293     uByte   *lsd;             /* -> least ditto                       */
    294     uInt     sign;            /* 0=positive, DECFLOAT_Sign=negative   */
    295     Int      exponent;        /* Unadjusted signed exponent (q), or   */
    296                               /* DECFLOAT_NaN etc. for a special      */
    297     } bcdnum;
    298 
    299   /* Test if exponent or bcdnum exponent must be a special, etc.      */
    300   #define EXPISSPECIAL(exp) ((exp)>=DECFLOAT_MinSp)
    301   #define EXPISINF(exp) (exp==DECFLOAT_Inf)
    302   #define EXPISNAN(exp) (exp==DECFLOAT_qNaN || exp==DECFLOAT_sNaN)
    303   #define NUMISSPECIAL(num) (EXPISSPECIAL((num)->exponent))
    304 
    305   /* Refer to a 32-bit word or byte in a decFloat (df) by big-endian  */
    306   /* (array) notation (the 0 word or byte contains the sign bit),     */
    307   /* automatically adjusting for endianness; similarly address a word */
    308   /* in the next-wider format (decFloatWider, or dfw)                 */
    309   #define DECWORDS  (DECBYTES/4)
    310   #define DECWWORDS (DECWBYTES/4)
    311   #if DECLITEND
    312     #define DFBYTE(df, off)   ((df)->bytes[DECBYTES-1-(off)])
    313     #define DFWORD(df, off)   ((df)->words[DECWORDS-1-(off)])
    314     #define DFWWORD(dfw, off) ((dfw)->words[DECWWORDS-1-(off)])
    315   #else
    316     #define DFBYTE(df, off)   ((df)->bytes[off])
    317     #define DFWORD(df, off)   ((df)->words[off])
    318     #define DFWWORD(dfw, off) ((dfw)->words[off])
    319   #endif
    320 
    321   /* Tests for sign or specials, directly on DECFLOATs                */
    322   #define DFISSIGNED(df)   (DFWORD(df, 0)&0x80000000)
    323   #define DFISSPECIAL(df) ((DFWORD(df, 0)&0x78000000)==0x78000000)
    324   #define DFISINF(df)     ((DFWORD(df, 0)&0x7c000000)==0x78000000)
    325   #define DFISNAN(df)     ((DFWORD(df, 0)&0x7c000000)==0x7c000000)
    326   #define DFISQNAN(df)    ((DFWORD(df, 0)&0x7e000000)==0x7c000000)
    327   #define DFISSNAN(df)    ((DFWORD(df, 0)&0x7e000000)==0x7e000000)
    328 
    329   /* Shared lookup tables                                             */
    330   extern const uInt   DECCOMBMSD[64];   /* Combination field -> MSD   */
    331   extern const uInt   DECCOMBFROM[48];  /* exp+msd -> Combination     */
    332 
    333   /* Private generic (utility) routine                                */
    334   #if DECCHECK || DECTRACE
    335     extern void decShowNum(const bcdnum *, const char *);
    336   #endif
    337 
    338   /* Format-dependent macros and constants                            */
    339   #if defined(DECPMAX)
    340 
    341     /* Useful constants                                               */
    342     #define DECPMAX9  (ROUNDUP(DECPMAX, 9)/9)  /* 'Pmax' in 10**9s    */
    343     /* Top words for a zero                                           */
    344     #define SINGLEZERO   0x22500000
    345     #define DOUBLEZERO   0x22380000
    346     #define QUADZERO     0x22080000
    347     /* [ZEROWORD is defined to be one of these in the DFISZERO macro] */
    348 
    349     /* Format-dependent common tests:                                 */
    350     /*   DFISZERO   -- test for (any) zero                            */
    351     /*   DFISCCZERO -- test for coefficient continuation being zero   */
    352     /*   DFISCC01   -- test for coefficient contains only 0s and 1s   */
    353     /*   DFISINT    -- test for finite and exponent q=0               */
    354     /*   DFISUINT01 -- test for sign=0, finite, exponent q=0, and     */
    355     /*                 MSD=0 or 1                                     */
    356     /*   ZEROWORD is also defined here.                               */
    357     /* In DFISZERO the first test checks the least-significant word   */
    358     /* (most likely to be non-zero); the penultimate tests MSD and    */
    359     /* DPDs in the signword, and the final test excludes specials and */
    360     /* MSD>7.  DFISINT similarly has to allow for the two forms of    */
    361     /* MSD codes.  DFISUINT01 only has to allow for one form of MSD   */
    362     /* code.                                                          */
    363     #if DECPMAX==7
    364       #define ZEROWORD SINGLEZERO
    365       /* [test macros not needed except for Zero]                     */
    366       #define DFISZERO(df)  ((DFWORD(df, 0)&0x1c0fffff)==0         \
    367                           && (DFWORD(df, 0)&0x60000000)!=0x60000000)
    368     #elif DECPMAX==16
    369       #define ZEROWORD DOUBLEZERO
    370       #define DFISZERO(df)  ((DFWORD(df, 1)==0                     \
    371                           && (DFWORD(df, 0)&0x1c03ffff)==0         \
    372                           && (DFWORD(df, 0)&0x60000000)!=0x60000000))
    373       #define DFISINT(df) ((DFWORD(df, 0)&0x63fc0000)==0x22380000  \
    374                          ||(DFWORD(df, 0)&0x7bfc0000)==0x6a380000)
    375       #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbfc0000)==0x22380000)
    376       #define DFISCCZERO(df) (DFWORD(df, 1)==0                     \
    377                           && (DFWORD(df, 0)&0x0003ffff)==0)
    378       #define DFISCC01(df)  ((DFWORD(df, 0)&~0xfffc9124)==0        \
    379                           && (DFWORD(df, 1)&~0x49124491)==0)
    380     #elif DECPMAX==34
    381       #define ZEROWORD QUADZERO
    382       #define DFISZERO(df)  ((DFWORD(df, 3)==0                     \
    383                           &&  DFWORD(df, 2)==0                     \
    384                           &&  DFWORD(df, 1)==0                     \
    385                           && (DFWORD(df, 0)&0x1c003fff)==0         \
    386                           && (DFWORD(df, 0)&0x60000000)!=0x60000000))
    387       #define DFISINT(df) ((DFWORD(df, 0)&0x63ffc000)==0x22080000  \
    388                          ||(DFWORD(df, 0)&0x7bffc000)==0x6a080000)
    389       #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbffc000)==0x22080000)
    390       #define DFISCCZERO(df) (DFWORD(df, 3)==0                     \
    391                           &&  DFWORD(df, 2)==0                     \
    392                           &&  DFWORD(df, 1)==0                     \
    393                           && (DFWORD(df, 0)&0x00003fff)==0)
    394 
    395       #define DFISCC01(df)   ((DFWORD(df, 0)&~0xffffc912)==0       \
    396                           &&  (DFWORD(df, 1)&~0x44912449)==0       \
    397                           &&  (DFWORD(df, 2)&~0x12449124)==0       \
    398                           &&  (DFWORD(df, 3)&~0x49124491)==0)
    399     #endif
    400 
    401     /* Macros to test if a certain 10 bits of a uInt or pair of uInts */
    402     /* are a canonical declet [higher or lower bits are ignored].     */
    403     /* declet is at offset 0 (from the right) in a uInt:              */
    404     #define CANONDPD(dpd) (((dpd)&0x300)==0 || ((dpd)&0x6e)!=0x6e)
    405     /* declet is at offset k (a multiple of 2) in a uInt:             */
    406     #define CANONDPDOFF(dpd, k) (((dpd)&(0x300<<(k)))==0            \
    407       || ((dpd)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
    408     /* declet is at offset k (a multiple of 2) in a pair of uInts:    */
    409     /* [the top 2 bits will always be in the more-significant uInt]   */
    410     #define CANONDPDTWO(hi, lo, k) (((hi)&(0x300>>(32-(k))))==0     \
    411       || ((hi)&(0x6e>>(32-(k))))!=(0x6e>>(32-(k)))                  \
    412       || ((lo)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
    413 
    414     /* Macro to test whether a full-length (length DECPMAX) BCD8      */
    415     /* coefficient, starting at uByte u, is all zeros                 */
    416     /* Test just the LSWord first, then the remainder as a sequence   */
    417     /* of tests in order to avoid same-level use of UBTOUI            */
    418     #if DECPMAX==7
    419       #define ISCOEFFZERO(u) (                                      \
    420            UBTOUI((u)+DECPMAX-4)==0                                 \
    421         && UBTOUS((u)+DECPMAX-6)==0                                 \
    422         && *(u)==0)
    423     #elif DECPMAX==16
    424       #define ISCOEFFZERO(u) (                                      \
    425            UBTOUI((u)+DECPMAX-4)==0                                 \
    426         && UBTOUI((u)+DECPMAX-8)==0                                 \
    427         && UBTOUI((u)+DECPMAX-12)==0                                \
    428         && UBTOUI(u)==0)
    429     #elif DECPMAX==34
    430       #define ISCOEFFZERO(u) (                                      \
    431            UBTOUI((u)+DECPMAX-4)==0                                 \
    432         && UBTOUI((u)+DECPMAX-8)==0                                 \
    433         && UBTOUI((u)+DECPMAX-12)==0                                \
    434         && UBTOUI((u)+DECPMAX-16)==0                                \
    435         && UBTOUI((u)+DECPMAX-20)==0                                \
    436         && UBTOUI((u)+DECPMAX-24)==0                                \
    437         && UBTOUI((u)+DECPMAX-28)==0                                \
    438         && UBTOUI((u)+DECPMAX-32)==0                                \
    439         && UBTOUS(u)==0)
    440     #endif
    441 
    442     /* Macros and masks for the exponent continuation field and MSD   */
    443     /* Get the exponent continuation from a decFloat *df as an Int    */
    444     #define GETECON(df) ((Int)((DFWORD((df), 0)&0x03ffffff)>>(32-6-DECECONL)))
    445     /* Ditto, from the next-wider format                              */
    446     #define GETWECON(df) ((Int)((DFWWORD((df), 0)&0x03ffffff)>>(32-6-DECWECONL)))
    447     /* Get the biased exponent similarly                              */
    448     #define GETEXP(df)  ((Int)(DECCOMBEXP[DFWORD((df), 0)>>26]+GETECON(df)))
    449     /* Get the unbiased exponent similarly                            */
    450     #define GETEXPUN(df) ((Int)GETEXP(df)-DECBIAS)
    451     /* Get the MSD similarly (as uInt)                                */
    452     #define GETMSD(df)   (DECCOMBMSD[DFWORD((df), 0)>>26])
    453 
    454     /* Compile-time computes of the exponent continuation field masks */
    455     /* full exponent continuation field:                              */
    456     #define ECONMASK ((0x03ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
    457     /* same, not including its first digit (the qNaN/sNaN selector):  */
    458     #define ECONNANMASK ((0x01ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
    459 
    460     /* Macros to decode the coefficient in a finite decFloat *df into */
    461     /* a BCD string (uByte *bcdin) of length DECPMAX uBytes.          */
    462 
    463     /* In-line sequence to convert least significant 10 bits of uInt  */
    464     /* dpd to three BCD8 digits starting at uByte u.  Note that an    */
    465     /* extra byte is written to the right of the three digits because */
    466     /* four bytes are moved at a time for speed; the alternative      */
    467     /* macro moves exactly three bytes (usually slower).              */
    468     #define dpd2bcd8(u, dpd)  memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 4)
    469     #define dpd2bcd83(u, dpd) memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 3)
    470 
    471     /* Decode the declets.  After extracting each one, it is decoded  */
    472     /* to BCD8 using a table lookup (also used for variable-length    */
    473     /* decode).  Each DPD decode is 3 bytes BCD8 plus a one-byte      */
    474     /* length which is not used, here).  Fixed-length 4-byte moves    */
    475     /* are fast, however, almost everywhere, and so are used except   */
    476     /* for the final three bytes (to avoid overrun).  The code below  */
    477     /* is 36 instructions for Doubles and about 70 for Quads, even    */
    478     /* on IA32.                                                       */
    479 
    480     /* Two macros are defined for each format:                        */
    481     /*   GETCOEFF extracts the coefficient of the current format      */
    482     /*   GETWCOEFF extracts the coefficient of the next-wider format. */
    483     /* The latter is a copy of the next-wider GETCOEFF using DFWWORD. */
    484 
    485     #if DECPMAX==7
    486     #define GETCOEFF(df, bcd) {                          \
    487       uInt sourhi=DFWORD(df, 0);                         \
    488       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
    489       dpd2bcd8(bcd+1, sourhi>>10);                       \
    490       dpd2bcd83(bcd+4, sourhi);}
    491     #define GETWCOEFF(df, bcd) {                         \
    492       uInt sourhi=DFWWORD(df, 0);                        \
    493       uInt sourlo=DFWWORD(df, 1);                        \
    494       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
    495       dpd2bcd8(bcd+1, sourhi>>8);                        \
    496       dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30));       \
    497       dpd2bcd8(bcd+7, sourlo>>20);                       \
    498       dpd2bcd8(bcd+10, sourlo>>10);                      \
    499       dpd2bcd83(bcd+13, sourlo);}
    500 
    501     #elif DECPMAX==16
    502     #define GETCOEFF(df, bcd) {                          \
    503       uInt sourhi=DFWORD(df, 0);                         \
    504       uInt sourlo=DFWORD(df, 1);                         \
    505       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
    506       dpd2bcd8(bcd+1, sourhi>>8);                        \
    507       dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30));       \
    508       dpd2bcd8(bcd+7, sourlo>>20);                       \
    509       dpd2bcd8(bcd+10, sourlo>>10);                      \
    510       dpd2bcd83(bcd+13, sourlo);}
    511     #define GETWCOEFF(df, bcd) {                         \
    512       uInt sourhi=DFWWORD(df, 0);                        \
    513       uInt sourmh=DFWWORD(df, 1);                        \
    514       uInt sourml=DFWWORD(df, 2);                        \
    515       uInt sourlo=DFWWORD(df, 3);                        \
    516       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
    517       dpd2bcd8(bcd+1, sourhi>>4);                        \
    518       dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26));     \
    519       dpd2bcd8(bcd+7, sourmh>>16);                       \
    520       dpd2bcd8(bcd+10, sourmh>>6);                       \
    521       dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28));    \
    522       dpd2bcd8(bcd+16, sourml>>18);                      \
    523       dpd2bcd8(bcd+19, sourml>>8);                       \
    524       dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30));    \
    525       dpd2bcd8(bcd+25, sourlo>>20);                      \
    526       dpd2bcd8(bcd+28, sourlo>>10);                      \
    527       dpd2bcd83(bcd+31, sourlo);}
    528 
    529     #elif DECPMAX==34
    530     #define GETCOEFF(df, bcd) {                          \
    531       uInt sourhi=DFWORD(df, 0);                         \
    532       uInt sourmh=DFWORD(df, 1);                         \
    533       uInt sourml=DFWORD(df, 2);                         \
    534       uInt sourlo=DFWORD(df, 3);                         \
    535       *(bcd)=(uByte)DECCOMBMSD[sourhi>>26];              \
    536       dpd2bcd8(bcd+1, sourhi>>4);                        \
    537       dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26));     \
    538       dpd2bcd8(bcd+7, sourmh>>16);                       \
    539       dpd2bcd8(bcd+10, sourmh>>6);                       \
    540       dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28));    \
    541       dpd2bcd8(bcd+16, sourml>>18);                      \
    542       dpd2bcd8(bcd+19, sourml>>8);                       \
    543       dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30));    \
    544       dpd2bcd8(bcd+25, sourlo>>20);                      \
    545       dpd2bcd8(bcd+28, sourlo>>10);                      \
    546       dpd2bcd83(bcd+31, sourlo);}
    547 
    548       #define GETWCOEFF(df, bcd) {??} /* [should never be used]       */
    549     #endif
    550 
    551     /* Macros to decode the coefficient in a finite decFloat *df into */
    552     /* a base-billion uInt array, with the least-significant          */
    553     /* 0-999999999 'digit' at offset 0.                               */
    554 
    555     /* Decode the declets.  After extracting each one, it is decoded  */
    556     /* to binary using a table lookup.  Three tables are used; one    */
    557     /* the usual DPD to binary, the other two pre-multiplied by 1000  */
    558     /* and 1000000 to avoid multiplication during decode.  These      */
    559     /* tables can also be used for multiplying up the MSD as the DPD  */
    560     /* code for 0 through 9 is the identity.                          */
    561     #define DPD2BIN0 DPD2BIN         /* for prettier code             */
    562 
    563     #if DECPMAX==7
    564     #define GETCOEFFBILL(df, buf) {                           \
    565       uInt sourhi=DFWORD(df, 0);                              \
    566       (buf)[0]=DPD2BIN0[sourhi&0x3ff]                         \
    567               +DPD2BINK[(sourhi>>10)&0x3ff]                   \
    568               +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
    569 
    570     #elif DECPMAX==16
    571     #define GETCOEFFBILL(df, buf) {                           \
    572       uInt sourhi, sourlo;                                    \
    573       sourlo=DFWORD(df, 1);                                   \
    574       (buf)[0]=DPD2BIN0[sourlo&0x3ff]                         \
    575               +DPD2BINK[(sourlo>>10)&0x3ff]                   \
    576               +DPD2BINM[(sourlo>>20)&0x3ff];                  \
    577       sourhi=DFWORD(df, 0);                                   \
    578       (buf)[1]=DPD2BIN0[((sourhi<<2) | (sourlo>>30))&0x3ff]   \
    579               +DPD2BINK[(sourhi>>8)&0x3ff]                    \
    580               +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
    581 
    582     #elif DECPMAX==34
    583     #define GETCOEFFBILL(df, buf) {                           \
    584       uInt sourhi, sourmh, sourml, sourlo;                    \
    585       sourlo=DFWORD(df, 3);                                   \
    586       (buf)[0]=DPD2BIN0[sourlo&0x3ff]                         \
    587               +DPD2BINK[(sourlo>>10)&0x3ff]                   \
    588               +DPD2BINM[(sourlo>>20)&0x3ff];                  \
    589       sourml=DFWORD(df, 2);                                   \
    590       (buf)[1]=DPD2BIN0[((sourml<<2) | (sourlo>>30))&0x3ff]   \
    591               +DPD2BINK[(sourml>>8)&0x3ff]                    \
    592               +DPD2BINM[(sourml>>18)&0x3ff];                  \
    593       sourmh=DFWORD(df, 1);                                   \
    594       (buf)[2]=DPD2BIN0[((sourmh<<4) | (sourml>>28))&0x3ff]   \
    595               +DPD2BINK[(sourmh>>6)&0x3ff]                    \
    596               +DPD2BINM[(sourmh>>16)&0x3ff];                  \
    597       sourhi=DFWORD(df, 0);                                   \
    598       (buf)[3]=DPD2BIN0[((sourhi<<6) | (sourmh>>26))&0x3ff]   \
    599               +DPD2BINK[(sourhi>>4)&0x3ff]                    \
    600               +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
    601 
    602     #endif
    603 
    604     /* Macros to decode the coefficient in a finite decFloat *df into */
    605     /* a base-thousand uInt array (of size DECLETS+1, to allow for    */
    606     /* the MSD), with the least-significant 0-999 'digit' at offset 0.*/
    607 
    608     /* Decode the declets.  After extracting each one, it is decoded  */
    609     /* to binary using a table lookup.                                */
    610     #if DECPMAX==7
    611     #define GETCOEFFTHOU(df, buf) {                           \
    612       uInt sourhi=DFWORD(df, 0);                              \
    613       (buf)[0]=DPD2BIN[sourhi&0x3ff];                         \
    614       (buf)[1]=DPD2BIN[(sourhi>>10)&0x3ff];                   \
    615       (buf)[2]=DECCOMBMSD[sourhi>>26];}
    616 
    617     #elif DECPMAX==16
    618     #define GETCOEFFTHOU(df, buf) {                           \
    619       uInt sourhi, sourlo;                                    \
    620       sourlo=DFWORD(df, 1);                                   \
    621       (buf)[0]=DPD2BIN[sourlo&0x3ff];                         \
    622       (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff];                   \
    623       (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff];                   \
    624       sourhi=DFWORD(df, 0);                                   \
    625       (buf)[3]=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff];   \
    626       (buf)[4]=DPD2BIN[(sourhi>>8)&0x3ff];                    \
    627       (buf)[5]=DECCOMBMSD[sourhi>>26];}
    628 
    629     #elif DECPMAX==34
    630     #define GETCOEFFTHOU(df, buf) {                           \
    631       uInt sourhi, sourmh, sourml, sourlo;                    \
    632       sourlo=DFWORD(df, 3);                                   \
    633       (buf)[0]=DPD2BIN[sourlo&0x3ff];                         \
    634       (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff];                   \
    635       (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff];                   \
    636       sourml=DFWORD(df, 2);                                   \
    637       (buf)[3]=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff];   \
    638       (buf)[4]=DPD2BIN[(sourml>>8)&0x3ff];                    \
    639       (buf)[5]=DPD2BIN[(sourml>>18)&0x3ff];                   \
    640       sourmh=DFWORD(df, 1);                                   \
    641       (buf)[6]=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff];   \
    642       (buf)[7]=DPD2BIN[(sourmh>>6)&0x3ff];                    \
    643       (buf)[8]=DPD2BIN[(sourmh>>16)&0x3ff];                   \
    644       sourhi=DFWORD(df, 0);                                   \
    645       (buf)[9]=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff];   \
    646       (buf)[10]=DPD2BIN[(sourhi>>4)&0x3ff];                   \
    647       (buf)[11]=DECCOMBMSD[sourhi>>26];}
    648     #endif
    649 
    650 
    651     /* Macros to decode the coefficient in a finite decFloat *df and  */
    652     /* add to a base-thousand uInt array (as for GETCOEFFTHOU).       */
    653     /* After the addition then most significant 'digit' in the array  */
    654     /* might have a value larger then 10 (with a maximum of 19).      */
    655     #if DECPMAX==7
    656     #define ADDCOEFFTHOU(df, buf) {                           \
    657       uInt sourhi=DFWORD(df, 0);                              \
    658       (buf)[0]+=DPD2BIN[sourhi&0x3ff];                        \
    659       if (buf[0]>999) {buf[0]-=1000; buf[1]++;}               \
    660       (buf)[1]+=DPD2BIN[(sourhi>>10)&0x3ff];                  \
    661       if (buf[1]>999) {buf[1]-=1000; buf[2]++;}               \
    662       (buf)[2]+=DECCOMBMSD[sourhi>>26];}
    663 
    664     #elif DECPMAX==16
    665     #define ADDCOEFFTHOU(df, buf) {                           \
    666       uInt sourhi, sourlo;                                    \
    667       sourlo=DFWORD(df, 1);                                   \
    668       (buf)[0]+=DPD2BIN[sourlo&0x3ff];                        \
    669       if (buf[0]>999) {buf[0]-=1000; buf[1]++;}               \
    670       (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff];                  \
    671       if (buf[1]>999) {buf[1]-=1000; buf[2]++;}               \
    672       (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff];                  \
    673       if (buf[2]>999) {buf[2]-=1000; buf[3]++;}               \
    674       sourhi=DFWORD(df, 0);                                   \
    675       (buf)[3]+=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff];  \
    676       if (buf[3]>999) {buf[3]-=1000; buf[4]++;}               \
    677       (buf)[4]+=DPD2BIN[(sourhi>>8)&0x3ff];                   \
    678       if (buf[4]>999) {buf[4]-=1000; buf[5]++;}               \
    679       (buf)[5]+=DECCOMBMSD[sourhi>>26];}
    680 
    681     #elif DECPMAX==34
    682     #define ADDCOEFFTHOU(df, buf) {                           \
    683       uInt sourhi, sourmh, sourml, sourlo;                    \
    684       sourlo=DFWORD(df, 3);                                   \
    685       (buf)[0]+=DPD2BIN[sourlo&0x3ff];                        \
    686       if (buf[0]>999) {buf[0]-=1000; buf[1]++;}               \
    687       (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff];                  \
    688       if (buf[1]>999) {buf[1]-=1000; buf[2]++;}               \
    689       (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff];                  \
    690       if (buf[2]>999) {buf[2]-=1000; buf[3]++;}               \
    691       sourml=DFWORD(df, 2);                                   \
    692       (buf)[3]+=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff];  \
    693       if (buf[3]>999) {buf[3]-=1000; buf[4]++;}               \
    694       (buf)[4]+=DPD2BIN[(sourml>>8)&0x3ff];                   \
    695       if (buf[4]>999) {buf[4]-=1000; buf[5]++;}               \
    696       (buf)[5]+=DPD2BIN[(sourml>>18)&0x3ff];                  \
    697       if (buf[5]>999) {buf[5]-=1000; buf[6]++;}               \
    698       sourmh=DFWORD(df, 1);                                   \
    699       (buf)[6]+=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff];  \
    700       if (buf[6]>999) {buf[6]-=1000; buf[7]++;}               \
    701       (buf)[7]+=DPD2BIN[(sourmh>>6)&0x3ff];                   \
    702       if (buf[7]>999) {buf[7]-=1000; buf[8]++;}               \
    703       (buf)[8]+=DPD2BIN[(sourmh>>16)&0x3ff];                  \
    704       if (buf[8]>999) {buf[8]-=1000; buf[9]++;}               \
    705       sourhi=DFWORD(df, 0);                                   \
    706       (buf)[9]+=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff];  \
    707       if (buf[9]>999) {buf[9]-=1000; buf[10]++;}              \
    708       (buf)[10]+=DPD2BIN[(sourhi>>4)&0x3ff];                  \
    709       if (buf[10]>999) {buf[10]-=1000; buf[11]++;}            \
    710       (buf)[11]+=DECCOMBMSD[sourhi>>26];}
    711     #endif
    712 
    713 
    714     /* Set a decFloat to the maximum positive finite number (Nmax)    */
    715     #if DECPMAX==7
    716     #define DFSETNMAX(df)            \
    717       {DFWORD(df, 0)=0x77f3fcff;}
    718     #elif DECPMAX==16
    719     #define DFSETNMAX(df)            \
    720       {DFWORD(df, 0)=0x77fcff3f;     \
    721        DFWORD(df, 1)=0xcff3fcff;}
    722     #elif DECPMAX==34
    723     #define DFSETNMAX(df)            \
    724       {DFWORD(df, 0)=0x77ffcff3;     \
    725        DFWORD(df, 1)=0xfcff3fcf;     \
    726        DFWORD(df, 2)=0xf3fcff3f;     \
    727        DFWORD(df, 3)=0xcff3fcff;}
    728     #endif
    729 
    730   /* [end of format-dependent macros and constants]                   */
    731   #endif
    732 
    733 #else
    734   #error decNumberLocal included more than once
    735 #endif
    736