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