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

source code of libdecnumber/decNumberLocal.h