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