Skip to content
Myr

decNumberLocal.h

This file

Path

vendor/decNumber/decNumberLocal.h

757 lines · 38.9 KiB · C

Last changed by

Not known.

History of this file

Take it

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