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