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