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