In gcc/: 2011-03-21 Nicola Pero <nicola.pero@meta-innovation.com>
[official-gcc.git] / libdecnumber / decNumberLocal.h
blobdca79e51db70fef6ea7615437c99f41b8e1afe71
1 /* Local definitions for the decNumber C Library.
2 Copyright (C) 2007, 2009 Free Software Foundation, Inc.
3 Contributed by IBM Corporation. Author Mike Cowlishaw.
5 This file is part of GCC.
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.
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.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
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 /* ------------------------------------------------------------------ */
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 */
40 #include <stdlib.h> /* for abs */
41 #include <string.h> /* for memset, strcpy */
42 #include "dconfig.h" /* for WORDS_BIGENDIAN */
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
52 #if !defined(DECLITEND)
53 #define DECLITEND 1 /* 1=little-endian, 0=big-endian */
54 #endif
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
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
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
80 /* ---------------------------------------------------------------- */
81 /* Definitions for all modules (general-purpose) */
82 /* ---------------------------------------------------------------- */
84 /* Local names for common types -- for safety, decNumber modules do */
85 /* not use int or long directly. */
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
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
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)*/
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);}
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 */
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 */
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]. */
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)
155 /* Store a uInt, etc., into bytes starting at a char* or uByte*. */
156 /* Returns i, evaluated, for convenience; has to use uiwork because */
157 /* i may be an expression. */
158 #define UBFROMUS(b, i) (uswork=(i), memcpy(b, (void *)&uswork, 2), uswork)
159 #define UBFROMUI(b, i) (uiwork=(i), memcpy(b, (void *)&uiwork, 4), uiwork)
161 /* X10 and X100 -- multiply integer i by 10 or 100 */
162 /* [shifts are usually faster than multiply; could be conditional] */
163 #define X10(i) (((i)<<1)+((i)<<3))
164 #define X100(i) (((i)<<2)+((i)<<5)+((i)<<6))
166 /* MAXI and MINI -- general max & min (not in ANSI) for integers */
167 #define MAXI(x,y) ((x)<(y)?(y):(x))
168 #define MINI(x,y) ((x)>(y)?(y):(x))
170 /* Useful constants */
171 #define BILLION 1000000000 /* 10**9 */
172 /* CHARMASK: 0x30303030 for ASCII/UTF8; 0xF0F0F0F0 for EBCDIC */
173 #define CHARMASK ((((((((uInt)'0')<<8)+'0')<<8)+'0')<<8)+'0')
176 /* ---------------------------------------------------------------- */
177 /* Definitions for arbitary-precision modules (only valid after */
178 /* decNumber.h has been included) */
179 /* ---------------------------------------------------------------- */
181 /* Limits and constants */
182 #define DECNUMMAXP 999999999 /* maximum precision code can handle */
183 #define DECNUMMAXE 999999999 /* maximum adjusted exponent ditto */
184 #define DECNUMMINE -999999999 /* minimum adjusted exponent ditto */
185 #if (DECNUMMAXP != DEC_MAX_DIGITS)
186 #error Maximum digits mismatch
187 #endif
188 #if (DECNUMMAXE != DEC_MAX_EMAX)
189 #error Maximum exponent mismatch
190 #endif
191 #if (DECNUMMINE != DEC_MIN_EMIN)
192 #error Minimum exponent mismatch
193 #endif
195 /* Set DECDPUNMAX -- the maximum integer that fits in DECDPUN */
196 /* digits, and D2UTABLE -- the initializer for the D2U table */
197 #if DECDPUN==1
198 #define DECDPUNMAX 9
199 #define D2UTABLE {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17, \
200 18,19,20,21,22,23,24,25,26,27,28,29,30,31,32, \
201 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47, \
202 48,49}
203 #elif DECDPUN==2
204 #define DECDPUNMAX 99
205 #define D2UTABLE {0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10, \
206 11,11,12,12,13,13,14,14,15,15,16,16,17,17,18, \
207 18,19,19,20,20,21,21,22,22,23,23,24,24,25}
208 #elif DECDPUN==3
209 #define DECDPUNMAX 999
210 #define D2UTABLE {0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7, \
211 8,8,8,9,9,9,10,10,10,11,11,11,12,12,12,13,13, \
212 13,14,14,14,15,15,15,16,16,16,17}
213 #elif DECDPUN==4
214 #define DECDPUNMAX 9999
215 #define D2UTABLE {0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6, \
216 6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11, \
217 11,11,11,12,12,12,12,13}
218 #elif DECDPUN==5
219 #define DECDPUNMAX 99999
220 #define D2UTABLE {0,1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5, \
221 5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8,9,9,9, \
222 9,9,10,10,10,10}
223 #elif DECDPUN==6
224 #define DECDPUNMAX 999999
225 #define D2UTABLE {0,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4, \
226 4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8, \
227 8,8,8,8,8,9}
228 #elif DECDPUN==7
229 #define DECDPUNMAX 9999999
230 #define D2UTABLE {0,1,1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3, \
231 4,4,4,4,4,4,4,5,5,5,5,5,5,5,6,6,6,6,6,6,6,7, \
232 7,7,7,7,7,7}
233 #elif DECDPUN==8
234 #define DECDPUNMAX 99999999
235 #define D2UTABLE {0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3, \
236 3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6, \
237 6,6,6,6,6,7}
238 #elif DECDPUN==9
239 #define DECDPUNMAX 999999999
240 #define D2UTABLE {0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3, \
241 3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5, \
242 5,5,6,6,6,6}
243 #elif defined(DECDPUN)
244 #error DECDPUN must be in the range 1-9
245 #endif
247 /* ----- Shared data (in decNumber.c) ----- */
248 /* Public lookup table used by the D2U macro (see below) */
249 #define DECMAXD2U 49
250 extern const uByte d2utable[DECMAXD2U+1];
252 /* ----- Macros ----- */
253 /* ISZERO -- return true if decNumber dn is a zero */
254 /* [performance-critical in some situations] */
255 #define ISZERO(dn) decNumberIsZero(dn) /* now just a local name */
257 /* D2U -- return the number of Units needed to hold d digits */
258 /* (runtime version, with table lookaside for small d) */
259 #if DECDPUN==8
260 #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+7)>>3))
261 #elif DECDPUN==4
262 #define D2U(d) ((unsigned)((d)<=DECMAXD2U?d2utable[d]:((d)+3)>>2))
263 #else
264 #define D2U(d) ((d)<=DECMAXD2U?d2utable[d]:((d)+DECDPUN-1)/DECDPUN)
265 #endif
266 /* SD2U -- static D2U macro (for compile-time calculation) */
267 #define SD2U(d) (((d)+DECDPUN-1)/DECDPUN)
269 /* MSUDIGITS -- returns digits in msu, from digits, calculated */
270 /* using D2U */
271 #define MSUDIGITS(d) ((d)-(D2U(d)-1)*DECDPUN)
273 /* D2N -- return the number of decNumber structs that would be */
274 /* needed to contain that number of digits (and the initial */
275 /* decNumber struct) safely. Note that one Unit is included in the */
276 /* initial structure. Used for allocating space that is aligned on */
277 /* a decNumber struct boundary. */
278 #define D2N(d) \
279 ((((SD2U(d)-1)*sizeof(Unit))+sizeof(decNumber)*2-1)/sizeof(decNumber))
281 /* TODIGIT -- macro to remove the leading digit from the unsigned */
282 /* integer u at column cut (counting from the right, LSD=0) and */
283 /* place it as an ASCII character into the character pointed to by */
284 /* c. Note that cut must be <= 9, and the maximum value for u is */
285 /* 2,000,000,000 (as is needed for negative exponents of */
286 /* subnormals). The unsigned integer pow is used as a temporary */
287 /* variable. */
288 #define TODIGIT(u, cut, c, pow) { \
289 *(c)='0'; \
290 pow=DECPOWERS[cut]*2; \
291 if ((u)>pow) { \
292 pow*=4; \
293 if ((u)>=pow) {(u)-=pow; *(c)+=8;} \
294 pow/=2; \
295 if ((u)>=pow) {(u)-=pow; *(c)+=4;} \
296 pow/=2; \
298 if ((u)>=pow) {(u)-=pow; *(c)+=2;} \
299 pow/=2; \
300 if ((u)>=pow) {(u)-=pow; *(c)+=1;} \
303 /* ---------------------------------------------------------------- */
304 /* Definitions for fixed-precision modules (only valid after */
305 /* decSingle.h, decDouble.h, or decQuad.h has been included) */
306 /* ---------------------------------------------------------------- */
308 /* bcdnum -- a structure describing a format-independent finite */
309 /* number, whose coefficient is a string of bcd8 uBytes */
310 typedef struct {
311 uByte *msd; /* -> most significant digit */
312 uByte *lsd; /* -> least ditto */
313 uInt sign; /* 0=positive, DECFLOAT_Sign=negative */
314 Int exponent; /* Unadjusted signed exponent (q), or */
315 /* DECFLOAT_NaN etc. for a special */
316 } bcdnum;
318 /* Test if exponent or bcdnum exponent must be a special, etc. */
319 #define EXPISSPECIAL(exp) ((exp)>=DECFLOAT_MinSp)
320 #define EXPISINF(exp) (exp==DECFLOAT_Inf)
321 #define EXPISNAN(exp) (exp==DECFLOAT_qNaN || exp==DECFLOAT_sNaN)
322 #define NUMISSPECIAL(num) (EXPISSPECIAL((num)->exponent))
324 /* Refer to a 32-bit word or byte in a decFloat (df) by big-endian */
325 /* (array) notation (the 0 word or byte contains the sign bit), */
326 /* automatically adjusting for endianness; similarly address a word */
327 /* in the next-wider format (decFloatWider, or dfw) */
328 #define DECWORDS (DECBYTES/4)
329 #define DECWWORDS (DECWBYTES/4)
330 #if DECLITEND
331 #define DFBYTE(df, off) ((df)->bytes[DECBYTES-1-(off)])
332 #define DFWORD(df, off) ((df)->words[DECWORDS-1-(off)])
333 #define DFWWORD(dfw, off) ((dfw)->words[DECWWORDS-1-(off)])
334 #else
335 #define DFBYTE(df, off) ((df)->bytes[off])
336 #define DFWORD(df, off) ((df)->words[off])
337 #define DFWWORD(dfw, off) ((dfw)->words[off])
338 #endif
340 /* Tests for sign or specials, directly on DECFLOATs */
341 #define DFISSIGNED(df) (DFWORD(df, 0)&0x80000000)
342 #define DFISSPECIAL(df) ((DFWORD(df, 0)&0x78000000)==0x78000000)
343 #define DFISINF(df) ((DFWORD(df, 0)&0x7c000000)==0x78000000)
344 #define DFISNAN(df) ((DFWORD(df, 0)&0x7c000000)==0x7c000000)
345 #define DFISQNAN(df) ((DFWORD(df, 0)&0x7e000000)==0x7c000000)
346 #define DFISSNAN(df) ((DFWORD(df, 0)&0x7e000000)==0x7e000000)
348 /* Shared lookup tables */
349 #include "decCommonSymbols.h"
350 extern const uInt DECCOMBMSD[64]; /* Combination field -> MSD */
351 extern const uInt DECCOMBFROM[48]; /* exp+msd -> Combination */
353 /* Private generic (utility) routine */
354 #if DECCHECK || DECTRACE
355 extern void decShowNum(const bcdnum *, const char *);
356 #endif
358 /* Format-dependent macros and constants */
359 #if defined(DECPMAX)
361 /* Useful constants */
362 #define DECPMAX9 (ROUNDUP(DECPMAX, 9)/9) /* 'Pmax' in 10**9s */
363 /* Top words for a zero */
364 #define SINGLEZERO 0x22500000
365 #define DOUBLEZERO 0x22380000
366 #define QUADZERO 0x22080000
367 /* [ZEROWORD is defined to be one of these in the DFISZERO macro] */
369 /* Format-dependent common tests: */
370 /* DFISZERO -- test for (any) zero */
371 /* DFISCCZERO -- test for coefficient continuation being zero */
372 /* DFISCC01 -- test for coefficient contains only 0s and 1s */
373 /* DFISINT -- test for finite and exponent q=0 */
374 /* DFISUINT01 -- test for sign=0, finite, exponent q=0, and */
375 /* MSD=0 or 1 */
376 /* ZEROWORD is also defined here. */
377 /* In DFISZERO the first test checks the least-significant word */
378 /* (most likely to be non-zero); the penultimate tests MSD and */
379 /* DPDs in the signword, and the final test excludes specials and */
380 /* MSD>7. DFISINT similarly has to allow for the two forms of */
381 /* MSD codes. DFISUINT01 only has to allow for one form of MSD */
382 /* code. */
383 #if DECPMAX==7
384 #define ZEROWORD SINGLEZERO
385 /* [test macros not needed except for Zero] */
386 #define DFISZERO(df) ((DFWORD(df, 0)&0x1c0fffff)==0 \
387 && (DFWORD(df, 0)&0x60000000)!=0x60000000)
388 #elif DECPMAX==16
389 #define ZEROWORD DOUBLEZERO
390 #define DFISZERO(df) ((DFWORD(df, 1)==0 \
391 && (DFWORD(df, 0)&0x1c03ffff)==0 \
392 && (DFWORD(df, 0)&0x60000000)!=0x60000000))
393 #define DFISINT(df) ((DFWORD(df, 0)&0x63fc0000)==0x22380000 \
394 ||(DFWORD(df, 0)&0x7bfc0000)==0x6a380000)
395 #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbfc0000)==0x22380000)
396 #define DFISCCZERO(df) (DFWORD(df, 1)==0 \
397 && (DFWORD(df, 0)&0x0003ffff)==0)
398 #define DFISCC01(df) ((DFWORD(df, 0)&~0xfffc9124)==0 \
399 && (DFWORD(df, 1)&~0x49124491)==0)
400 #elif DECPMAX==34
401 #define ZEROWORD QUADZERO
402 #define DFISZERO(df) ((DFWORD(df, 3)==0 \
403 && DFWORD(df, 2)==0 \
404 && DFWORD(df, 1)==0 \
405 && (DFWORD(df, 0)&0x1c003fff)==0 \
406 && (DFWORD(df, 0)&0x60000000)!=0x60000000))
407 #define DFISINT(df) ((DFWORD(df, 0)&0x63ffc000)==0x22080000 \
408 ||(DFWORD(df, 0)&0x7bffc000)==0x6a080000)
409 #define DFISUINT01(df) ((DFWORD(df, 0)&0xfbffc000)==0x22080000)
410 #define DFISCCZERO(df) (DFWORD(df, 3)==0 \
411 && DFWORD(df, 2)==0 \
412 && DFWORD(df, 1)==0 \
413 && (DFWORD(df, 0)&0x00003fff)==0)
415 #define DFISCC01(df) ((DFWORD(df, 0)&~0xffffc912)==0 \
416 && (DFWORD(df, 1)&~0x44912449)==0 \
417 && (DFWORD(df, 2)&~0x12449124)==0 \
418 && (DFWORD(df, 3)&~0x49124491)==0)
419 #endif
421 /* Macros to test if a certain 10 bits of a uInt or pair of uInts */
422 /* are a canonical declet [higher or lower bits are ignored]. */
423 /* declet is at offset 0 (from the right) in a uInt: */
424 #define CANONDPD(dpd) (((dpd)&0x300)==0 || ((dpd)&0x6e)!=0x6e)
425 /* declet is at offset k (a multiple of 2) in a uInt: */
426 #define CANONDPDOFF(dpd, k) (((dpd)&(0x300<<(k)))==0 \
427 || ((dpd)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
428 /* declet is at offset k (a multiple of 2) in a pair of uInts: */
429 /* [the top 2 bits will always be in the more-significant uInt] */
430 #define CANONDPDTWO(hi, lo, k) (((hi)&(0x300>>(32-(k))))==0 \
431 || ((hi)&(0x6e>>(32-(k))))!=(0x6e>>(32-(k))) \
432 || ((lo)&(((uInt)0x6e)<<(k)))!=(((uInt)0x6e)<<(k)))
434 /* Macro to test whether a full-length (length DECPMAX) BCD8 */
435 /* coefficient, starting at uByte u, is all zeros */
436 /* Test just the LSWord first, then the remainder as a sequence */
437 /* of tests in order to avoid same-level use of UBTOUI */
438 #if DECPMAX==7
439 #define ISCOEFFZERO(u) ( \
440 UBTOUI((u)+DECPMAX-4)==0 \
441 && UBTOUS((u)+DECPMAX-6)==0 \
442 && *(u)==0)
443 #elif DECPMAX==16
444 #define ISCOEFFZERO(u) ( \
445 UBTOUI((u)+DECPMAX-4)==0 \
446 && UBTOUI((u)+DECPMAX-8)==0 \
447 && UBTOUI((u)+DECPMAX-12)==0 \
448 && UBTOUI(u)==0)
449 #elif DECPMAX==34
450 #define ISCOEFFZERO(u) ( \
451 UBTOUI((u)+DECPMAX-4)==0 \
452 && UBTOUI((u)+DECPMAX-8)==0 \
453 && UBTOUI((u)+DECPMAX-12)==0 \
454 && UBTOUI((u)+DECPMAX-16)==0 \
455 && UBTOUI((u)+DECPMAX-20)==0 \
456 && UBTOUI((u)+DECPMAX-24)==0 \
457 && UBTOUI((u)+DECPMAX-28)==0 \
458 && UBTOUI((u)+DECPMAX-32)==0 \
459 && UBTOUS(u)==0)
460 #endif
462 /* Macros and masks for the exponent continuation field and MSD */
463 /* Get the exponent continuation from a decFloat *df as an Int */
464 #define GETECON(df) ((Int)((DFWORD((df), 0)&0x03ffffff)>>(32-6-DECECONL)))
465 /* Ditto, from the next-wider format */
466 #define GETWECON(df) ((Int)((DFWWORD((df), 0)&0x03ffffff)>>(32-6-DECWECONL)))
467 /* Get the biased exponent similarly */
468 #define GETEXP(df) ((Int)(DECCOMBEXP[DFWORD((df), 0)>>26]+GETECON(df)))
469 /* Get the unbiased exponent similarly */
470 #define GETEXPUN(df) ((Int)GETEXP(df)-DECBIAS)
471 /* Get the MSD similarly (as uInt) */
472 #define GETMSD(df) (DECCOMBMSD[DFWORD((df), 0)>>26])
474 /* Compile-time computes of the exponent continuation field masks */
475 /* full exponent continuation field: */
476 #define ECONMASK ((0x03ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
477 /* same, not including its first digit (the qNaN/sNaN selector): */
478 #define ECONNANMASK ((0x01ffffff>>(32-6-DECECONL))<<(32-6-DECECONL))
480 /* Macros to decode the coefficient in a finite decFloat *df into */
481 /* a BCD string (uByte *bcdin) of length DECPMAX uBytes. */
483 /* In-line sequence to convert least significant 10 bits of uInt */
484 /* dpd to three BCD8 digits starting at uByte u. Note that an */
485 /* extra byte is written to the right of the three digits because */
486 /* four bytes are moved at a time for speed; the alternative */
487 /* macro moves exactly three bytes (usually slower). */
488 #define dpd2bcd8(u, dpd) memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 4)
489 #define dpd2bcd83(u, dpd) memcpy(u, &DPD2BCD8[((dpd)&0x3ff)*4], 3)
491 /* Decode the declets. After extracting each one, it is decoded */
492 /* to BCD8 using a table lookup (also used for variable-length */
493 /* decode). Each DPD decode is 3 bytes BCD8 plus a one-byte */
494 /* length which is not used, here). Fixed-length 4-byte moves */
495 /* are fast, however, almost everywhere, and so are used except */
496 /* for the final three bytes (to avoid overrun). The code below */
497 /* is 36 instructions for Doubles and about 70 for Quads, even */
498 /* on IA32. */
500 /* Two macros are defined for each format: */
501 /* GETCOEFF extracts the coefficient of the current format */
502 /* GETWCOEFF extracts the coefficient of the next-wider format. */
503 /* The latter is a copy of the next-wider GETCOEFF using DFWWORD. */
505 #if DECPMAX==7
506 #define GETCOEFF(df, bcd) { \
507 uInt sourhi=DFWORD(df, 0); \
508 *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
509 dpd2bcd8(bcd+1, sourhi>>10); \
510 dpd2bcd83(bcd+4, sourhi);}
511 #define GETWCOEFF(df, bcd) { \
512 uInt sourhi=DFWWORD(df, 0); \
513 uInt sourlo=DFWWORD(df, 1); \
514 *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
515 dpd2bcd8(bcd+1, sourhi>>8); \
516 dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30)); \
517 dpd2bcd8(bcd+7, sourlo>>20); \
518 dpd2bcd8(bcd+10, sourlo>>10); \
519 dpd2bcd83(bcd+13, sourlo);}
521 #elif DECPMAX==16
522 #define GETCOEFF(df, bcd) { \
523 uInt sourhi=DFWORD(df, 0); \
524 uInt sourlo=DFWORD(df, 1); \
525 *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
526 dpd2bcd8(bcd+1, sourhi>>8); \
527 dpd2bcd8(bcd+4, (sourhi<<2) | (sourlo>>30)); \
528 dpd2bcd8(bcd+7, sourlo>>20); \
529 dpd2bcd8(bcd+10, sourlo>>10); \
530 dpd2bcd83(bcd+13, sourlo);}
531 #define GETWCOEFF(df, bcd) { \
532 uInt sourhi=DFWWORD(df, 0); \
533 uInt sourmh=DFWWORD(df, 1); \
534 uInt sourml=DFWWORD(df, 2); \
535 uInt sourlo=DFWWORD(df, 3); \
536 *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
537 dpd2bcd8(bcd+1, sourhi>>4); \
538 dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26)); \
539 dpd2bcd8(bcd+7, sourmh>>16); \
540 dpd2bcd8(bcd+10, sourmh>>6); \
541 dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28)); \
542 dpd2bcd8(bcd+16, sourml>>18); \
543 dpd2bcd8(bcd+19, sourml>>8); \
544 dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30)); \
545 dpd2bcd8(bcd+25, sourlo>>20); \
546 dpd2bcd8(bcd+28, sourlo>>10); \
547 dpd2bcd83(bcd+31, sourlo);}
549 #elif DECPMAX==34
550 #define GETCOEFF(df, bcd) { \
551 uInt sourhi=DFWORD(df, 0); \
552 uInt sourmh=DFWORD(df, 1); \
553 uInt sourml=DFWORD(df, 2); \
554 uInt sourlo=DFWORD(df, 3); \
555 *(bcd)=(uByte)DECCOMBMSD[sourhi>>26]; \
556 dpd2bcd8(bcd+1, sourhi>>4); \
557 dpd2bcd8(bcd+4, ((sourhi)<<6) | (sourmh>>26)); \
558 dpd2bcd8(bcd+7, sourmh>>16); \
559 dpd2bcd8(bcd+10, sourmh>>6); \
560 dpd2bcd8(bcd+13, ((sourmh)<<4) | (sourml>>28)); \
561 dpd2bcd8(bcd+16, sourml>>18); \
562 dpd2bcd8(bcd+19, sourml>>8); \
563 dpd2bcd8(bcd+22, ((sourml)<<2) | (sourlo>>30)); \
564 dpd2bcd8(bcd+25, sourlo>>20); \
565 dpd2bcd8(bcd+28, sourlo>>10); \
566 dpd2bcd83(bcd+31, sourlo);}
568 #define GETWCOEFF(df, bcd) {??} /* [should never be used] */
569 #endif
571 /* Macros to decode the coefficient in a finite decFloat *df into */
572 /* a base-billion uInt array, with the least-significant */
573 /* 0-999999999 'digit' at offset 0. */
575 /* Decode the declets. After extracting each one, it is decoded */
576 /* to binary using a table lookup. Three tables are used; one */
577 /* the usual DPD to binary, the other two pre-multiplied by 1000 */
578 /* and 1000000 to avoid multiplication during decode. These */
579 /* tables can also be used for multiplying up the MSD as the DPD */
580 /* code for 0 through 9 is the identity. */
581 #define DPD2BIN0 DPD2BIN /* for prettier code */
583 #if DECPMAX==7
584 #define GETCOEFFBILL(df, buf) { \
585 uInt sourhi=DFWORD(df, 0); \
586 (buf)[0]=DPD2BIN0[sourhi&0x3ff] \
587 +DPD2BINK[(sourhi>>10)&0x3ff] \
588 +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
590 #elif DECPMAX==16
591 #define GETCOEFFBILL(df, buf) { \
592 uInt sourhi, sourlo; \
593 sourlo=DFWORD(df, 1); \
594 (buf)[0]=DPD2BIN0[sourlo&0x3ff] \
595 +DPD2BINK[(sourlo>>10)&0x3ff] \
596 +DPD2BINM[(sourlo>>20)&0x3ff]; \
597 sourhi=DFWORD(df, 0); \
598 (buf)[1]=DPD2BIN0[((sourhi<<2) | (sourlo>>30))&0x3ff] \
599 +DPD2BINK[(sourhi>>8)&0x3ff] \
600 +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
602 #elif DECPMAX==34
603 #define GETCOEFFBILL(df, buf) { \
604 uInt sourhi, sourmh, sourml, sourlo; \
605 sourlo=DFWORD(df, 3); \
606 (buf)[0]=DPD2BIN0[sourlo&0x3ff] \
607 +DPD2BINK[(sourlo>>10)&0x3ff] \
608 +DPD2BINM[(sourlo>>20)&0x3ff]; \
609 sourml=DFWORD(df, 2); \
610 (buf)[1]=DPD2BIN0[((sourml<<2) | (sourlo>>30))&0x3ff] \
611 +DPD2BINK[(sourml>>8)&0x3ff] \
612 +DPD2BINM[(sourml>>18)&0x3ff]; \
613 sourmh=DFWORD(df, 1); \
614 (buf)[2]=DPD2BIN0[((sourmh<<4) | (sourml>>28))&0x3ff] \
615 +DPD2BINK[(sourmh>>6)&0x3ff] \
616 +DPD2BINM[(sourmh>>16)&0x3ff]; \
617 sourhi=DFWORD(df, 0); \
618 (buf)[3]=DPD2BIN0[((sourhi<<6) | (sourmh>>26))&0x3ff] \
619 +DPD2BINK[(sourhi>>4)&0x3ff] \
620 +DPD2BINM[DECCOMBMSD[sourhi>>26]];}
622 #endif
624 /* Macros to decode the coefficient in a finite decFloat *df into */
625 /* a base-thousand uInt array (of size DECLETS+1, to allow for */
626 /* the MSD), with the least-significant 0-999 'digit' at offset 0.*/
628 /* Decode the declets. After extracting each one, it is decoded */
629 /* to binary using a table lookup. */
630 #if DECPMAX==7
631 #define GETCOEFFTHOU(df, buf) { \
632 uInt sourhi=DFWORD(df, 0); \
633 (buf)[0]=DPD2BIN[sourhi&0x3ff]; \
634 (buf)[1]=DPD2BIN[(sourhi>>10)&0x3ff]; \
635 (buf)[2]=DECCOMBMSD[sourhi>>26];}
637 #elif DECPMAX==16
638 #define GETCOEFFTHOU(df, buf) { \
639 uInt sourhi, sourlo; \
640 sourlo=DFWORD(df, 1); \
641 (buf)[0]=DPD2BIN[sourlo&0x3ff]; \
642 (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff]; \
643 (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff]; \
644 sourhi=DFWORD(df, 0); \
645 (buf)[3]=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff]; \
646 (buf)[4]=DPD2BIN[(sourhi>>8)&0x3ff]; \
647 (buf)[5]=DECCOMBMSD[sourhi>>26];}
649 #elif DECPMAX==34
650 #define GETCOEFFTHOU(df, buf) { \
651 uInt sourhi, sourmh, sourml, sourlo; \
652 sourlo=DFWORD(df, 3); \
653 (buf)[0]=DPD2BIN[sourlo&0x3ff]; \
654 (buf)[1]=DPD2BIN[(sourlo>>10)&0x3ff]; \
655 (buf)[2]=DPD2BIN[(sourlo>>20)&0x3ff]; \
656 sourml=DFWORD(df, 2); \
657 (buf)[3]=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff]; \
658 (buf)[4]=DPD2BIN[(sourml>>8)&0x3ff]; \
659 (buf)[5]=DPD2BIN[(sourml>>18)&0x3ff]; \
660 sourmh=DFWORD(df, 1); \
661 (buf)[6]=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff]; \
662 (buf)[7]=DPD2BIN[(sourmh>>6)&0x3ff]; \
663 (buf)[8]=DPD2BIN[(sourmh>>16)&0x3ff]; \
664 sourhi=DFWORD(df, 0); \
665 (buf)[9]=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff]; \
666 (buf)[10]=DPD2BIN[(sourhi>>4)&0x3ff]; \
667 (buf)[11]=DECCOMBMSD[sourhi>>26];}
668 #endif
671 /* Macros to decode the coefficient in a finite decFloat *df and */
672 /* add to a base-thousand uInt array (as for GETCOEFFTHOU). */
673 /* After the addition then most significant 'digit' in the array */
674 /* might have a value larger then 10 (with a maximum of 19). */
675 #if DECPMAX==7
676 #define ADDCOEFFTHOU(df, buf) { \
677 uInt sourhi=DFWORD(df, 0); \
678 (buf)[0]+=DPD2BIN[sourhi&0x3ff]; \
679 if (buf[0]>999) {buf[0]-=1000; buf[1]++;} \
680 (buf)[1]+=DPD2BIN[(sourhi>>10)&0x3ff]; \
681 if (buf[1]>999) {buf[1]-=1000; buf[2]++;} \
682 (buf)[2]+=DECCOMBMSD[sourhi>>26];}
684 #elif DECPMAX==16
685 #define ADDCOEFFTHOU(df, buf) { \
686 uInt sourhi, sourlo; \
687 sourlo=DFWORD(df, 1); \
688 (buf)[0]+=DPD2BIN[sourlo&0x3ff]; \
689 if (buf[0]>999) {buf[0]-=1000; buf[1]++;} \
690 (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff]; \
691 if (buf[1]>999) {buf[1]-=1000; buf[2]++;} \
692 (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff]; \
693 if (buf[2]>999) {buf[2]-=1000; buf[3]++;} \
694 sourhi=DFWORD(df, 0); \
695 (buf)[3]+=DPD2BIN[((sourhi<<2) | (sourlo>>30))&0x3ff]; \
696 if (buf[3]>999) {buf[3]-=1000; buf[4]++;} \
697 (buf)[4]+=DPD2BIN[(sourhi>>8)&0x3ff]; \
698 if (buf[4]>999) {buf[4]-=1000; buf[5]++;} \
699 (buf)[5]+=DECCOMBMSD[sourhi>>26];}
701 #elif DECPMAX==34
702 #define ADDCOEFFTHOU(df, buf) { \
703 uInt sourhi, sourmh, sourml, sourlo; \
704 sourlo=DFWORD(df, 3); \
705 (buf)[0]+=DPD2BIN[sourlo&0x3ff]; \
706 if (buf[0]>999) {buf[0]-=1000; buf[1]++;} \
707 (buf)[1]+=DPD2BIN[(sourlo>>10)&0x3ff]; \
708 if (buf[1]>999) {buf[1]-=1000; buf[2]++;} \
709 (buf)[2]+=DPD2BIN[(sourlo>>20)&0x3ff]; \
710 if (buf[2]>999) {buf[2]-=1000; buf[3]++;} \
711 sourml=DFWORD(df, 2); \
712 (buf)[3]+=DPD2BIN[((sourml<<2) | (sourlo>>30))&0x3ff]; \
713 if (buf[3]>999) {buf[3]-=1000; buf[4]++;} \
714 (buf)[4]+=DPD2BIN[(sourml>>8)&0x3ff]; \
715 if (buf[4]>999) {buf[4]-=1000; buf[5]++;} \
716 (buf)[5]+=DPD2BIN[(sourml>>18)&0x3ff]; \
717 if (buf[5]>999) {buf[5]-=1000; buf[6]++;} \
718 sourmh=DFWORD(df, 1); \
719 (buf)[6]+=DPD2BIN[((sourmh<<4) | (sourml>>28))&0x3ff]; \
720 if (buf[6]>999) {buf[6]-=1000; buf[7]++;} \
721 (buf)[7]+=DPD2BIN[(sourmh>>6)&0x3ff]; \
722 if (buf[7]>999) {buf[7]-=1000; buf[8]++;} \
723 (buf)[8]+=DPD2BIN[(sourmh>>16)&0x3ff]; \
724 if (buf[8]>999) {buf[8]-=1000; buf[9]++;} \
725 sourhi=DFWORD(df, 0); \
726 (buf)[9]+=DPD2BIN[((sourhi<<6) | (sourmh>>26))&0x3ff]; \
727 if (buf[9]>999) {buf[9]-=1000; buf[10]++;} \
728 (buf)[10]+=DPD2BIN[(sourhi>>4)&0x3ff]; \
729 if (buf[10]>999) {buf[10]-=1000; buf[11]++;} \
730 (buf)[11]+=DECCOMBMSD[sourhi>>26];}
731 #endif
734 /* Set a decFloat to the maximum positive finite number (Nmax) */
735 #if DECPMAX==7
736 #define DFSETNMAX(df) \
737 {DFWORD(df, 0)=0x77f3fcff;}
738 #elif DECPMAX==16
739 #define DFSETNMAX(df) \
740 {DFWORD(df, 0)=0x77fcff3f; \
741 DFWORD(df, 1)=0xcff3fcff;}
742 #elif DECPMAX==34
743 #define DFSETNMAX(df) \
744 {DFWORD(df, 0)=0x77ffcff3; \
745 DFWORD(df, 1)=0xfcff3fcf; \
746 DFWORD(df, 2)=0xf3fcff3f; \
747 DFWORD(df, 3)=0xcff3fcff;}
748 #endif
750 /* [end of format-dependent macros and constants] */
751 #endif
753 #else
754 #error decNumberLocal included more than once
755 #endif