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