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