OSDN Git Service

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