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