1 /* Common code for fixed-size types in 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
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
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
31 /* ------------------------------------------------------------------ */
32 /* decCommon.c -- common code for all three fixed-size types */
33 /* ------------------------------------------------------------------ */
34 /* This module comprises code that is shared between all the formats */
35 /* (decSingle, decDouble, and decQuad); it includes set and extract */
36 /* of format components, widening, narrowing, and string conversions. */
38 /* Unlike decNumber, parameterization takes place at compile time */
39 /* rather than at runtime. The parameters are set in the decDouble.c */
40 /* (etc.) files, which then include this one to produce the compiled */
41 /* code. The functions here, therefore, are code shared between */
42 /* multiple formats. */
43 /* ------------------------------------------------------------------ */
44 /* Names here refer to decFloat rather than to decDouble, etc., and */
45 /* the functions are in strict alphabetical order. */
46 /* Constants, tables, and debug function(s) are included only for QUAD */
47 /* (which will always be compiled if DOUBLE or SINGLE are used). */
49 /* Whenever a decContext is used, only the status may be set (using */
50 /* OR) or the rounding mode read; all other fields are ignored and */
53 /* names for simpler testing and default context */
58 #define DEFCONTEXT DEC_INIT_DECIMAL32
63 #define DEFCONTEXT DEC_INIT_DECIMAL64
68 #define DEFCONTEXT DEC_INIT_DECIMAL128
70 #error Unexpected DECPMAX value
75 #if DECPMAX!=7 && DECPMAX!=16 && DECPMAX!=34
76 #error Unexpected Pmax (DECPMAX) value for this module
79 /* Assert facts about digit characters, etc. */
81 #error This module assumes characters are of the form 0b....nnnn
82 /* where .... are don't care 4 bits and nnnn is 0000 through 1001 */
84 #if ('9'&0xf0)==('.'&0xf0)
85 #error This module assumes '.' has a different mask than a digit
88 /* Assert ToString lay-out conditions */
89 #if DECSTRING<DECPMAX+9
90 #error ToString needs at least 8 characters for lead-in and dot
92 #if DECPMAX+DECEMAXD+5 > DECSTRING
93 #error Exponent form can be too long for ToString to lay out safely
96 #error Exponent form is too long for ToString to lay out
97 /* Note: code for up to 9 digits exists in archives [decOct] */
100 /* Private functions used here and possibly in decBasic.c, etc. */
101 static decFloat
* decFinalize(decFloat
*, bcdnum
*, decContext
*);
102 static Flag
decBiStr(const char *, const char *, const char *);
104 /* Macros and private tables; those which are not format-dependent */
105 /* are only included if decQuad is being built. */
107 /* ------------------------------------------------------------------ */
108 /* Combination field lookup tables (uInts to save measurable work) */
110 /* DECCOMBEXP - 2 most-significant-bits of exponent (00, 01, or */
111 /* 10), shifted left for format, or DECFLOAT_Inf/NaN */
112 /* DECCOMBWEXP - The same, for the next-wider format (unless QUAD) */
113 /* DECCOMBMSD - 4-bit most-significant-digit */
114 /* [0 if the index is a special (Infinity or NaN)] */
115 /* DECCOMBFROM - 5-bit combination field from EXP top bits and MSD */
116 /* (placed in uInt so no shift is needed) */
118 /* DECCOMBEXP, DECCOMBWEXP, and DECCOMBMSD are indexed by the sign */
119 /* and 5-bit combination field (0-63, the second half of the table */
120 /* identical to the first half) */
121 /* DECCOMBFROM is indexed by expTopTwoBits*16 + msd */
123 /* DECCOMBMSD and DECCOMBFROM are not format-dependent and so are */
124 /* only included once, when QUAD is being built */
125 /* ------------------------------------------------------------------ */
126 static const uInt DECCOMBEXP
[64]={
127 0, 0, 0, 0, 0, 0, 0, 0,
128 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
,
129 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
,
130 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
,
131 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
,
132 0, 0, 1<<DECECONL
, 1<<DECECONL
,
133 2<<DECECONL
, 2<<DECECONL
, DECFLOAT_Inf
, DECFLOAT_NaN
,
134 0, 0, 0, 0, 0, 0, 0, 0,
135 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
,
136 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
,
137 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
,
138 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
,
139 0, 0, 1<<DECECONL
, 1<<DECECONL
,
140 2<<DECECONL
, 2<<DECECONL
, DECFLOAT_Inf
, DECFLOAT_NaN
};
142 static const uInt DECCOMBWEXP
[64]={
143 0, 0, 0, 0, 0, 0, 0, 0,
144 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
,
145 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
,
146 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
,
147 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
,
148 0, 0, 1<<DECWECONL
, 1<<DECWECONL
,
149 2<<DECWECONL
, 2<<DECWECONL
, DECFLOAT_Inf
, DECFLOAT_NaN
,
150 0, 0, 0, 0, 0, 0, 0, 0,
151 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
,
152 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
,
153 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
,
154 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
,
155 0, 0, 1<<DECWECONL
, 1<<DECWECONL
,
156 2<<DECWECONL
, 2<<DECWECONL
, DECFLOAT_Inf
, DECFLOAT_NaN
};
160 const uInt DECCOMBMSD
[64]={
161 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
162 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 1,
163 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
164 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0};
166 const uInt DECCOMBFROM
[48]={
167 0x00000000, 0x04000000, 0x08000000, 0x0C000000, 0x10000000, 0x14000000,
168 0x18000000, 0x1C000000, 0x60000000, 0x64000000, 0x00000000, 0x00000000,
169 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000, 0x24000000,
170 0x28000000, 0x2C000000, 0x30000000, 0x34000000, 0x38000000, 0x3C000000,
171 0x68000000, 0x6C000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
172 0x00000000, 0x00000000, 0x40000000, 0x44000000, 0x48000000, 0x4C000000,
173 0x50000000, 0x54000000, 0x58000000, 0x5C000000, 0x70000000, 0x74000000,
174 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000};
176 /* ------------------------------------------------------------------ */
177 /* Request and include the tables to use for conversions */
178 /* ------------------------------------------------------------------ */
179 #define DEC_BCD2DPD 1 /* 0-0x999 -> DPD */
180 #define DEC_BIN2DPD 1 /* 0-999 -> DPD */
181 #define DEC_BIN2BCD8 1 /* 0-999 -> ddd, len */
182 #define DEC_DPD2BCD8 1 /* DPD -> ddd, len */
183 #define DEC_DPD2BIN 1 /* DPD -> 0-999 */
184 #define DEC_DPD2BINK 1 /* DPD -> 0-999000 */
185 #define DEC_DPD2BINM 1 /* DPD -> 0-999000000 */
186 #include "decDPD.h" /* source of the lookup tables */
190 /* ----------------------------------------------------------------- */
191 /* decBiStr -- compare string with pairwise options */
193 /* targ is the string to compare */
194 /* str1 is one of the strings to compare against (length may be 0) */
195 /* str2 is the other; it must be the same length as str1 */
197 /* returns 1 if strings compare equal, (that is, targ is the same */
198 /* length as str1 and str2, and each character of targ is in one */
199 /* of str1 or str2 in the corresponding position), or 0 otherwise */
201 /* This is used for generic caseless compare, including the awkward */
202 /* case of the Turkish dotted and dotless Is. Use as (for example): */
203 /* if (decBiStr(test, "mike", "MIKE")) ... */
204 /* ----------------------------------------------------------------- */
205 static Flag
decBiStr(const char *targ
, const char *str1
, const char *str2
) {
206 for (;;targ
++, str1
++, str2
++) {
207 if (*targ
!=*str1
&& *targ
!=*str2
) return 0;
208 /* *targ has a match in one (or both, if terminator) */
209 if (*targ
=='\0') break;
214 /* ------------------------------------------------------------------ */
215 /* decFinalize -- adjust and store a final result */
217 /* df is the decFloat format number which gets the final result */
218 /* num is the descriptor of the number to be checked and encoded */
219 /* [its values, including the coefficient, may be modified] */
220 /* set is the context to use */
223 /* The num descriptor may point to a bcd8 string of any length; this */
224 /* string may have leading insignificant zeros. If it has more than */
225 /* DECPMAX digits then the final digit can be a round-for-reround */
226 /* digit (i.e., it may include a sticky bit residue). */
228 /* The exponent (q) may be one of the codes for a special value and */
229 /* can be up to 999999999 for conversion from string. */
231 /* No error is possible, but Inexact, Underflow, and/or Overflow may */
233 /* ------------------------------------------------------------------ */
234 /* Constant whose size varies with format; also the check for surprises */
235 static uByte allnines
[DECPMAX
]=
237 {9, 9, 9, 9, 9, 9, 9};
239 {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
241 {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
242 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
245 static decFloat
* decFinalize(decFloat
*df
, bcdnum
*num
,
247 uByte
*ub
; /* work */
249 uByte
*umsd
=num
->msd
; /* local copy */
250 uByte
*ulsd
=num
->lsd
; /* .. */
251 uInt encode
; /* encoding accumulator */
252 Int length
; /* coefficient length */
255 Int clen
=ulsd
-umsd
+1;
257 #define COEXTRA 2 /* extra-long coefficent */
261 if (clen
<1 || clen
>DECPMAX
*3+2+COEXTRA
)
262 printf("decFinalize: suspect coefficient [length=%ld]\n", (LI
)clen
);
263 if (num
->sign
!=0 && num
->sign
!=DECFLOAT_Sign
)
264 printf("decFinalize: bad sign [%08lx]\n", (LI
)num
->sign
);
265 if (!EXPISSPECIAL(num
->exponent
)
266 && (num
->exponent
>1999999999 || num
->exponent
<-1999999999))
267 printf("decFinalize: improbable exponent [%ld]\n", (LI
)num
->exponent
);
268 /* decShowNum(num, "final"); */
271 /* A special will have an 'exponent' which is very positive and a */
272 /* coefficient < DECPMAX */
273 length
=(uInt
)(ulsd
-umsd
+1); /* coefficient length */
275 if (!NUMISSPECIAL(num
)) {
276 Int drop
; /* digits to be dropped */
277 /* skip leading insignificant zeros to calculate an exact length */
278 /* [this is quite expensive] */
280 for (; UINTAT(umsd
)==0 && umsd
+3<ulsd
;) umsd
+=4;
281 for (; *umsd
==0 && umsd
<ulsd
;) umsd
++;
282 length
=ulsd
-umsd
+1; /* recalculate */
284 drop
=MAXI(length
-DECPMAX
, DECQTINY
-num
->exponent
);
285 /* drop can now be > digits for bottom-clamp (subnormal) cases */
286 if (drop
>0) { /* rounding needed */
287 /* (decFloatQuantize has very similar code to this, so any */
288 /* changes may need to be made there, too) */
289 uByte
*roundat
; /* -> re-round digit */
290 uByte reround
; /* reround value */
291 /* printf("Rounding; drop=%ld\n", (LI)drop); */
293 num
->exponent
+=drop
; /* always update exponent */
295 /* Three cases here: */
296 /* 1. new LSD is in coefficient (almost always) */
297 /* 2. new LSD is digit to left of coefficient (so MSD is */
298 /* round-for-reround digit) */
299 /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */
300 /* [duplicate check-stickies code to save a test] */
301 /* [by-digit check for stickies as runs of zeros are rare] */
302 if (drop
<length
) { /* NB lengths not addresses */
303 roundat
=umsd
+length
-drop
;
305 for (ub
=roundat
+1; ub
<=ulsd
; ub
++) {
306 if (*ub
!=0) { /* non-zero to be discarded */
307 reround
=DECSTICKYTAB
[reround
]; /* apply sticky bit */
308 break; /* [remainder don't-care] */
310 } /* check stickies */
311 ulsd
=roundat
-1; /* new LSD */
313 else { /* edge case */
322 for (ub
=roundat
+1; ub
<=ulsd
; ub
++) {
323 if (*ub
!=0) { /* non-zero to be discarded */
324 reround
=DECSTICKYTAB
[reround
]; /* apply sticky bit */
325 break; /* [remainder don't-care] */
327 } /* check stickies */
328 *umsd
=0; /* coefficient is a 0 */
332 if (reround
!=0) { /* discarding non-zero */
334 set
->status
|=DEC_Inexact
;
335 /* if adjusted exponent [exp+digits-1] is < EMIN then num is */
336 /* subnormal -- so raise Underflow */
337 if (num
->exponent
<DECEMIN
&& (num
->exponent
+(ulsd
-umsd
+1)-1)<DECEMIN
)
338 set
->status
|=DEC_Underflow
;
340 /* next decide whether increment of the coefficient is needed */
341 if (set
->round
==DEC_ROUND_HALF_EVEN
) { /* fastpath slowest case */
342 if (reround
>5) bump
=1; /* >0.5 goes up */
343 else if (reround
==5) /* exactly 0.5000 .. */
344 bump
=*ulsd
& 0x01; /* .. up iff [new] lsd is odd */
346 else switch (set
->round
) {
347 case DEC_ROUND_DOWN
: {
350 case DEC_ROUND_HALF_DOWN
: {
351 if (reround
>5) bump
=1;
353 case DEC_ROUND_HALF_UP
: {
354 if (reround
>=5) bump
=1;
357 if (reround
>0) bump
=1;
359 case DEC_ROUND_CEILING
: {
360 /* same as _UP for positive numbers, and as _DOWN for negatives */
361 if (!num
->sign
&& reround
>0) bump
=1;
363 case DEC_ROUND_FLOOR
: {
364 /* same as _UP for negative numbers, and as _DOWN for positive */
365 /* [negative reround cannot occur on 0] */
366 if (num
->sign
&& reround
>0) bump
=1;
368 case DEC_ROUND_05UP
: {
369 if (reround
>0) { /* anything out there is 'sticky' */
370 /* bump iff lsd=0 or 5; this cannot carry so it could be */
371 /* effected immediately with no bump -- but the code */
372 /* is clearer if this is done the same way as the others */
373 if (*ulsd
==0 || *ulsd
==5) bump
=1;
376 default: { /* e.g., DEC_ROUND_MAX */
377 set
->status
|=DEC_Invalid_context
;
379 printf("Unknown rounding mode: %ld\n", (LI
)set
->round
);
382 } /* switch (not r-h-e) */
383 /* printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump); */
385 if (bump
!=0) { /* need increment */
386 /* increment the coefficient; this might end up with 1000... */
387 /* (after the all nines case) */
389 for(; ub
-3>=umsd
&& UINTAT(ub
-3)==0x09090909; ub
-=4) UINTAT(ub
-3)=0;
390 /* [note ub could now be to left of msd, and it is not safe */
391 /* to write to the the left of the msd] */
392 /* now at most 3 digits left to non-9 (usually just the one) */
393 for (; ub
>=umsd
; *ub
=0, ub
--) {
394 if (*ub
==9) continue; /* carry */
398 if (ub
<umsd
) { /* had all-nines */
399 *umsd
=1; /* coefficient to 1000... */
400 /* usually the 1000... coefficient can be used as-is */
401 if ((ulsd
-umsd
+1)==DECPMAX
) {
405 /* if coefficient is shorter than Pmax then num is */
406 /* subnormal, so extend it; this is safe as drop>0 */
407 /* (or, if the coefficient was supplied above, it could */
408 /* not be 9); this may make the result normal. */
411 /* [exponent unchanged] */
413 if (num
->exponent
!=DECQTINY
) /* sanity check */
414 printf("decFinalize: bad all-nines extend [^%ld, %ld]\n",
415 (LI
)num
->exponent
, (LI
)(ulsd
-umsd
+1));
417 } /* subnormal extend */
418 } /* had all-nines */
420 } /* inexact rounding */
422 length
=ulsd
-umsd
+1; /* recalculate (may be <DECPMAX) */
423 } /* need round (drop>0) */
425 /* The coefficient will now fit and has final length unless overflow */
426 /* decShowNum(num, "rounded"); */
428 /* if exponent is >=emax may have to clamp, overflow, or fold-down */
429 if (num
->exponent
>DECEMAX
-(DECPMAX
-1)) { /* is edge case */
430 /* printf("overflow checks...\n"); */
431 if (*ulsd
==0 && ulsd
==umsd
) { /* have zero */
432 num
->exponent
=DECEMAX
-(DECPMAX
-1); /* clamp to max */
434 else if ((num
->exponent
+length
-1)>DECEMAX
) { /* > Nmax */
435 /* Overflow -- these could go straight to encoding, here, but */
436 /* instead num is adjusted to keep the code cleaner */
437 Flag needmax
=0; /* 1 for finite result */
438 set
->status
|=(DEC_Overflow
| DEC_Inexact
);
439 switch (set
->round
) {
440 case DEC_ROUND_DOWN
: {
441 needmax
=1; /* never Infinity */
443 case DEC_ROUND_05UP
: {
444 needmax
=1; /* never Infinity */
446 case DEC_ROUND_CEILING
: {
447 if (num
->sign
) needmax
=1; /* Infinity iff non-negative */
449 case DEC_ROUND_FLOOR
: {
450 if (!num
->sign
) needmax
=1; /* Infinity iff negative */
452 default: break; /* Infinity in all other cases */
454 if (!needmax
) { /* easy .. set Infinity */
455 num
->exponent
=DECFLOAT_Inf
;
456 *umsd
=0; /* be clean: coefficient to 0 */
459 else { /* return Nmax */
460 umsd
=allnines
; /* use constant array */
461 ulsd
=allnines
+DECPMAX
-1;
462 num
->exponent
=DECEMAX
-(DECPMAX
-1);
465 else { /* no overflow but non-zero and may have to fold-down */
466 Int shift
=num
->exponent
-(DECEMAX
-(DECPMAX
-1));
467 if (shift
>0) { /* fold-down needed */
468 /* fold down needed; must copy to buffer in order to pad */
469 /* with zeros safely; fortunately this is not the worst case */
470 /* path because cannot have had a round */
471 uByte buffer
[ROUNDUP(DECPMAX
+3, 4)]; /* [+3 allows uInt padding] */
472 uByte
*s
=umsd
; /* source */
473 uByte
*t
=buffer
; /* safe target */
474 uByte
*tlsd
=buffer
+(ulsd
-umsd
)+shift
; /* target LSD */
475 /* printf("folddown shift=%ld\n", (LI)shift); */
476 for (; s
<=ulsd
; s
+=4, t
+=4) UINTAT(t
)=UINTAT(s
);
477 for (t
=tlsd
-shift
+1; t
<=tlsd
; t
+=4) UINTAT(t
)=0; /* pad */
478 num
->exponent
-=shift
;
483 length
=ulsd
-umsd
+1; /* recalculate length */
484 } /* high-end edge case */
485 } /* finite number */
487 /*------------------------------------------------------------------*/
488 /* At this point the result will properly fit the decFloat */
489 /* encoding, and it can be encoded with no possibility of error */
490 /*------------------------------------------------------------------*/
491 /* Following code does not alter coefficient (could be allnines array) */
493 if (length
==DECPMAX
) {
494 return decFloatFromBCD(df
, num
->exponent
, umsd
, num
->sign
);
497 /* Here when length is short */
498 if (!NUMISSPECIAL(num
)) { /* is still finite */
499 /* encode the combination field and exponent continuation */
500 uInt uexp
=(uInt
)(num
->exponent
+DECBIAS
); /* biased exponent */
501 uInt code
=(uexp
>>DECECONL
)<<4; /* top two bits of exp */
503 /* look up the combination field and make high word */
504 encode
=DECCOMBFROM
[code
]; /* indexed by (0-2)*16+msd */
505 encode
|=(uexp
<<(32-6-DECECONL
)) & 0x03ffffff; /* exponent continuation */
507 else encode
=num
->exponent
; /* special [already in word] */
508 /* [coefficient length here will be < DECPMAX] */
510 encode
|=num
->sign
; /* add sign */
512 /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */
513 /* refers to the declet from the least significant three digits) */
514 /* and put the corresponding DPD code into dpd. Access to umsd and */
515 /* ulsd (pointers to the most and least significant digit of the */
516 /* variable-length coefficient) is assumed, along with use of a */
517 /* working pointer, uInt *ub. */
518 /* As not full-length then chances are there are many leading zeros */
519 /* [and there may be a partial triad] */
520 #define getDPD(dpd, n) ub=ulsd-(3*(n))-2; \
521 if (ub<umsd-2) dpd=0; \
522 else if (ub>=umsd) dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)]; \
523 else {dpd=*(ub+2); if (ub+1==umsd) dpd+=*(ub+1)*16; dpd=BCD2DPD[dpd];}
525 /* place the declets in the encoding words and copy to result (df), */
526 /* according to endianness; in all cases complete the sign word */
533 DFWORD(df
, 0)=encode
; /* just the one word */
536 getDPD(dpd
, 4); encode
|=dpd
<<8;
537 getDPD(dpd
, 3); encode
|=dpd
>>2;
538 DFWORD(df
, 0)=encode
;
540 getDPD(dpd
, 2); encode
|=dpd
<<20;
541 getDPD(dpd
, 1); encode
|=dpd
<<10;
542 getDPD(dpd
, 0); encode
|=dpd
;
543 DFWORD(df
, 1)=encode
;
546 getDPD(dpd
,10); encode
|=dpd
<<4;
547 getDPD(dpd
, 9); encode
|=dpd
>>6;
548 DFWORD(df
, 0)=encode
;
551 getDPD(dpd
, 8); encode
|=dpd
<<16;
552 getDPD(dpd
, 7); encode
|=dpd
<<6;
553 getDPD(dpd
, 6); encode
|=dpd
>>4;
554 DFWORD(df
, 1)=encode
;
557 getDPD(dpd
, 5); encode
|=dpd
<<18;
558 getDPD(dpd
, 4); encode
|=dpd
<<8;
559 getDPD(dpd
, 3); encode
|=dpd
>>2;
560 DFWORD(df
, 2)=encode
;
563 getDPD(dpd
, 2); encode
|=dpd
<<20;
564 getDPD(dpd
, 1); encode
|=dpd
<<10;
565 getDPD(dpd
, 0); encode
|=dpd
;
566 DFWORD(df
, 3)=encode
;
569 /* printf("Status: %08lx\n", (LI)set->status); */
570 /* decFloatShow(df, "final"); */
574 /* ------------------------------------------------------------------ */
575 /* decFloatFromBCD -- set decFloat from exponent, BCD8, and sign */
577 /* df is the target decFloat */
578 /* exp is the in-range unbiased exponent, q, or a special value in */
579 /* the form returned by decFloatGetExponent */
580 /* bcdar holds DECPMAX digits to set the coefficient from, one */
581 /* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
582 /* if df is a NaN; all are ignored if df is infinite. */
583 /* All bytes must be in 0-9; results undefined otherwise. */
584 /* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
585 /* returns df, which will be canonical */
587 /* No error is possible, and no status will be set. */
588 /* ------------------------------------------------------------------ */
589 decFloat
* decFloatFromBCD(decFloat
*df
, Int exp
, const uByte
*bcdar
,
591 uInt encode
, dpd
; /* work */
592 const uByte
*ub
; /* .. */
594 if (EXPISSPECIAL(exp
)) encode
=exp
|sig
;/* specials already encoded */
595 else { /* is finite */
596 /* encode the combination field and exponent continuation */
597 uInt uexp
=(uInt
)(exp
+DECBIAS
); /* biased exponent */
598 uInt code
=(uexp
>>DECECONL
)<<4; /* top two bits of exp */
599 code
+=bcdar
[0]; /* add msd */
600 /* look up the combination field and make high word */
601 encode
=DECCOMBFROM
[code
]|sig
; /* indexed by (0-2)*16+msd */
602 encode
|=(uexp
<<(32-6-DECECONL
)) & 0x03ffffff; /* exponent continuation */
605 /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */
606 /* refers to the declet from the least significant three digits) */
607 /* and put the corresponding DPD code into dpd. */
608 /* Use of a working pointer, uInt *ub, is assumed. */
610 #define getDPDf(dpd, n) ub=bcdar+DECPMAX-1-(3*(n))-2; \
611 dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)];
613 /* place the declets in the encoding words and copy to result (df), */
614 /* according to endianness; in all cases complete the sign word */
621 DFWORD(df
, 0)=encode
; /* just the one word */
624 getDPDf(dpd
, 4); encode
|=dpd
<<8;
625 getDPDf(dpd
, 3); encode
|=dpd
>>2;
626 DFWORD(df
, 0)=encode
;
628 getDPDf(dpd
, 2); encode
|=dpd
<<20;
629 getDPDf(dpd
, 1); encode
|=dpd
<<10;
630 getDPDf(dpd
, 0); encode
|=dpd
;
631 DFWORD(df
, 1)=encode
;
634 getDPDf(dpd
,10); encode
|=dpd
<<4;
635 getDPDf(dpd
, 9); encode
|=dpd
>>6;
636 DFWORD(df
, 0)=encode
;
639 getDPDf(dpd
, 8); encode
|=dpd
<<16;
640 getDPDf(dpd
, 7); encode
|=dpd
<<6;
641 getDPDf(dpd
, 6); encode
|=dpd
>>4;
642 DFWORD(df
, 1)=encode
;
645 getDPDf(dpd
, 5); encode
|=dpd
<<18;
646 getDPDf(dpd
, 4); encode
|=dpd
<<8;
647 getDPDf(dpd
, 3); encode
|=dpd
>>2;
648 DFWORD(df
, 2)=encode
;
651 getDPDf(dpd
, 2); encode
|=dpd
<<20;
652 getDPDf(dpd
, 1); encode
|=dpd
<<10;
653 getDPDf(dpd
, 0); encode
|=dpd
;
654 DFWORD(df
, 3)=encode
;
656 /* decFloatShow(df, "final"); */
658 } /* decFloatFromBCD */
660 /* ------------------------------------------------------------------ */
661 /* decFloatFromPacked -- set decFloat from exponent and packed BCD */
663 /* df is the target decFloat */
664 /* exp is the in-range unbiased exponent, q, or a special value in */
665 /* the form returned by decFloatGetExponent */
666 /* packed holds DECPMAX packed decimal digits plus a sign nibble */
667 /* (all 6 codes are OK); the first (MSD) is ignored if df is a NaN */
668 /* and all except sign are ignored if df is infinite. For DOUBLE */
669 /* and QUAD the first (pad) nibble is also ignored in all cases. */
670 /* All coefficient nibbles must be in 0-9 and sign in A-F; results */
671 /* are undefined otherwise. */
672 /* returns df, which will be canonical */
674 /* No error is possible, and no status will be set. */
675 /* ------------------------------------------------------------------ */
676 decFloat
* decFloatFromPacked(decFloat
*df
, Int exp
, const uByte
*packed
) {
677 uByte bcdar
[DECPMAX
+2]; /* work [+1 for pad, +1 for sign] */
678 const uByte
*ip
; /* .. */
680 Int sig
=0; /* sign */
682 /* expand coefficient and sign to BCDAR */
684 op
=bcdar
+1; /* no pad digit */
686 op
=bcdar
; /* first (pad) digit ignored */
688 for (ip
=packed
; ip
<packed
+((DECPMAX
+2)/2); ip
++) {
690 *op
++=(uByte
)(*ip
&0x0f); /* [final nibble is sign] */
692 op
--; /* -> sign byte */
693 if (*op
==DECPMINUS
|| *op
==DECPMINUSALT
) sig
=DECFLOAT_Sign
;
695 if (EXPISSPECIAL(exp
)) { /* Infinity or NaN */
696 if (!EXPISINF(exp
)) bcdar
[1]=0; /* a NaN: ignore MSD */
697 else memset(bcdar
+1, 0, DECPMAX
); /* Infinite: coefficient to 0 */
699 return decFloatFromBCD(df
, exp
, bcdar
+1, sig
);
700 } /* decFloatFromPacked */
702 /* ------------------------------------------------------------------ */
703 /* decFloatFromString -- conversion from numeric string */
705 /* result is the decFloat format number which gets the result of */
707 /* *string is the character string which should contain a valid */
708 /* number (which may be a special value), \0-terminated */
709 /* If there are too many significant digits in the */
710 /* coefficient it will be rounded. */
711 /* set is the context */
714 /* The length of the coefficient and the size of the exponent are */
715 /* checked by this routine, so the correct error (Underflow or */
716 /* Overflow) can be reported or rounding applied, as necessary. */
718 /* There is no limit to the coefficient length for finite inputs; */
719 /* NaN payloads must be integers with no more than DECPMAX-1 digits. */
720 /* Exponents may have up to nine significant digits. */
722 /* If bad syntax is detected, the result will be a quiet NaN. */
723 /* ------------------------------------------------------------------ */
724 decFloat
* decFloatFromString(decFloat
*result
, const char *string
,
726 Int digits
; /* count of digits in coefficient */
727 const char *dotchar
=NULL
; /* where dot was found [NULL if none] */
728 const char *cfirst
=string
; /* -> first character of decimal part */
729 const char *c
; /* work */
731 bcdnum num
; /* collects data for finishing */
732 uInt error
=DEC_Conversion_syntax
; /* assume the worst */
733 uByte buffer
[ROUNDUP(DECSTRING
+11, 8)]; /* room for most coefficents, */
734 /* some common rounding, +3, & pad */
736 /* printf("FromString %s ...\n", string); */
739 for(;;) { /* once-only 'loop' */
740 num
.sign
=0; /* assume non-negative */
741 num
.msd
=buffer
; /* MSD is here always */
743 /* detect and validate the coefficient, including any leading, */
744 /* trailing, or embedded '.' */
745 /* [could test four-at-a-time here (saving 10% for decQuads), */
746 /* but that risks storage violation because the position of the */
747 /* terminator is unknown] */
748 for (c
=string
;; c
++) { /* -> input character */
749 if (((unsigned)(*c
-'0'))<=9) continue; /* '0' through '9' is good */
750 if (*c
=='\0') break; /* most common non-digit */
752 if (dotchar
!=NULL
) break; /* not first '.' */
753 dotchar
=c
; /* record offset into decimal part */
755 if (c
==string
) { /* first in string... */
756 if (*c
=='-') { /* valid - sign */
758 num
.sign
=DECFLOAT_Sign
;
760 if (*c
=='+') { /* valid + sign */
764 /* *c is not a digit, terminator, or a valid +, -, or '.' */
768 digits
=(uInt
)(c
-cfirst
); /* digits (+1 if a dot) */
770 if (digits
>0) { /* had digits and/or dot */
771 const char *clast
=c
-1; /* note last coefficient char position */
772 Int exp
=0; /* exponent accumulator */
773 if (*c
!='\0') { /* something follows the coefficient */
774 uInt edig
; /* unsigned work */
775 /* had some digits and more to come; expect E[+|-]nnn now */
776 const char *firstexp
; /* exponent first non-zero */
777 if (*c
!='E' && *c
!='e') break;
778 c
++; /* to (optional) sign */
779 if (*c
=='-' || *c
=='+') c
++; /* step over sign (c=clast+2) */
780 if (*c
=='\0') break; /* no digits! (e.g., '1.2E') */
781 for (; *c
=='0';) c
++; /* skip leading zeros [even last] */
782 firstexp
=c
; /* remember start [maybe '\0'] */
783 /* gather exponent digits */
784 edig
=(uInt
)*c
-(uInt
)'0';
785 if (edig
<=9) { /* [check not bad or terminator] */
786 exp
+=edig
; /* avoid initial X10 */
789 edig
=(uInt
)*c
-(uInt
)'0';
794 /* if not now on the '\0', *c must not be a digit */
797 /* (this next test must be after the syntax checks) */
798 /* if definitely more than the possible digits for format then */
799 /* the exponent may have wrapped, so simply set it to a certain */
800 /* over/underflow value */
801 if (c
>firstexp
+DECEMAXD
) exp
=DECEMAX
*2;
802 if (*(clast
+2)=='-') exp
=-exp
; /* was negative */
805 if (dotchar
!=NULL
) { /* had a '.' */
806 digits
--; /* remove from digits count */
807 if (digits
==0) break; /* was dot alone: bad syntax */
808 exp
-=(Int
)(clast
-dotchar
); /* adjust exponent */
809 /* [the '.' can now be ignored] */
811 num
.exponent
=exp
; /* exponent is good; store it */
813 /* Here when whole string has been inspected and syntax is good */
814 /* cfirst->first digit or dot, clast->last digit or dot */
815 error
=0; /* no error possible now */
817 /* if the number of digits in the coefficient will fit in buffer */
818 /* then it can simply be converted to bcd8 and copied -- decFinalize */
819 /* will take care of leading zeros and rounding; the buffer is big */
820 /* enough for all canonical coefficients, including 0.00000nn... */
822 if (digits
<=(Int
)(sizeof(buffer
)-3)) { /* [-3 allows by-4s copy] */
824 if (dotchar
!=NULL
) { /* a dot to worry about */
825 if (*(c
+1)=='.') { /* common canonical case */
826 *ub
++=(uByte
)(*c
-'0'); /* copy leading digit */
827 c
+=2; /* prepare to handle rest */
829 else for (; c
<=clast
;) { /* '.' could be anywhere */
830 /* as usual, go by fours when safe; NB it has been asserted */
831 /* that a '.' does not have the same mask as a digit */
832 if (c
<=clast
-3 /* safe for four */
833 && (UINTAT(c
)&0xf0f0f0f0)==CHARMASK
) { /* test four */
834 UINTAT(ub
)=UINTAT(c
)&0x0f0f0f0f; /* to BCD8 */
839 if (*c
=='.') { /* found the dot */
840 c
++; /* step over it .. */
841 break; /* .. and handle the rest */
843 *ub
++=(uByte
)(*c
++-'0');
846 /* Now no dot; do this by fours (where safe) */
847 for (; c
<=clast
-3; c
+=4, ub
+=4) UINTAT(ub
)=UINTAT(c
)&0x0f0f0f0f;
848 for (; c
<=clast
; c
++, ub
++) *ub
=(uByte
)(*c
-'0');
849 num
.lsd
=buffer
+digits
-1; /* record new LSD */
852 else { /* too long for buffer */
853 /* [This is a rare and unusual case; arbitrary-length input] */
854 /* strip leading zeros [but leave final 0 if all 0's] */
855 if (*cfirst
=='.') cfirst
++; /* step past dot at start */
856 if (*cfirst
=='0') { /* [cfirst always -> digit] */
857 for (; cfirst
<clast
; cfirst
++) {
858 if (*cfirst
!='0') { /* non-zero found */
859 if (*cfirst
=='.') continue; /* [ignore] */
862 digits
--; /* 0 stripped */
864 } /* at least one leading 0 */
866 /* the coefficient is now as short as possible, but may still */
867 /* be too long; copy up to Pmax+1 digits to the buffer, then */
868 /* just record any non-zeros (set round-for-reround digit) */
869 for (c
=cfirst
; c
<=clast
&& ub
<=buffer
+DECPMAX
; c
++) {
870 /* (see commentary just above) */
871 if (c
<=clast
-3 /* safe for four */
872 && (UINTAT(c
)&0xf0f0f0f0)==CHARMASK
) { /* four digits */
873 UINTAT(ub
)=UINTAT(c
)&0x0f0f0f0f; /* to BCD8 */
875 c
+=3; /* [will become 4] */
878 if (*c
=='.') continue; /* [ignore] */
879 *ub
++=(uByte
)(*c
-'0');
882 for (; c
<=clast
; c
++) { /* inspect remaining chars */
883 if (*c
!='0') { /* sticky bit needed */
884 if (*c
=='.') continue; /* [ignore] */
885 *ub
=DECSTICKYTAB
[*ub
]; /* update round-for-reround */
886 break; /* no need to look at more */
889 num
.lsd
=ub
; /* record LSD */
890 /* adjust exponent for dropped digits */
891 num
.exponent
+=digits
-(Int
)(ub
-buffer
+1);
892 } /* too long for buffer */
893 } /* digits or dot */
895 else { /* no digits or dot were found */
896 if (*c
=='\0') break; /* nothing to come is bad */
897 /* only Infinities and NaNs are allowed, here */
898 buffer
[0]=0; /* default a coefficient of 0 */
899 num
.lsd
=buffer
; /* .. */
900 if (decBiStr(c
, "infinity", "INFINITY")
901 || decBiStr(c
, "inf", "INF")) num
.exponent
=DECFLOAT_Inf
;
902 else { /* should be a NaN */
903 num
.exponent
=DECFLOAT_qNaN
; /* assume quiet NaN */
904 if (*c
=='s' || *c
=='S') { /* probably an sNaN */
906 num
.exponent
=DECFLOAT_sNaN
; /* assume is in fact sNaN */
908 if (*c
!='N' && *c
!='n') break; /* check caseless "NaN" */
910 if (*c
!='a' && *c
!='A') break; /* .. */
912 if (*c
!='N' && *c
!='n') break; /* .. */
914 /* now either nothing, or nnnn payload (no dots), expected */
915 /* -> start of integer, and skip leading 0s [including plain 0] */
916 for (cfirst
=c
; *cfirst
=='0';) cfirst
++;
917 if (*cfirst
!='\0') { /* not empty or all-0, payload */
918 /* payload found; check all valid digits and copy to buffer as bcd8 */
920 for (c
=cfirst
;; c
++, ub
++) {
921 if ((unsigned)(*c
-'0')>9) break; /* quit if not 0-9 */
922 if (c
-cfirst
==DECPMAX
-1) break; /* too many digits */
923 *ub
=(uByte
)(*c
-'0'); /* good bcd8 */
925 if (*c
!='\0') break; /* not all digits, or too many */
926 num
.lsd
=ub
-1; /* record new LSD */
929 error
=0; /* syntax is OK */
930 break; /* done with specials */
931 } /* digits=0 (special expected) */
933 } /* [for(;;) break] */
935 /* decShowNum(&num, "fromStr"); */
939 num
.exponent
=DECFLOAT_qNaN
; /* set up quiet NaN */
940 num
.sign
=0; /* .. with 0 sign */
941 buffer
[0]=0; /* .. and coefficient */
942 num
.lsd
=buffer
; /* .. */
943 /* decShowNum(&num, "oops"); */
946 /* decShowNum(&num, "dffs"); */
947 decFinalize(result
, &num
, set
); /* round, check, and lay out */
948 /* decFloatShow(result, "fromString"); */
950 } /* decFloatFromString */
952 /* ------------------------------------------------------------------ */
953 /* decFloatFromWider -- conversion from next-wider format */
955 /* result is the decFloat format number which gets the result of */
957 /* wider is the decFloatWider format number which will be narrowed */
958 /* set is the context */
961 /* Narrowing can cause rounding, overflow, etc., but not Invalid */
962 /* operation (sNaNs are copied and do not signal). */
963 /* ------------------------------------------------------------------ */
964 /* narrow-to is not possible for decQuad format numbers; simply omit */
966 decFloat
* decFloatFromWider(decFloat
*result
, const decFloatWider
*wider
,
968 bcdnum num
; /* collects data for finishing */
969 uByte bcdar
[DECWPMAX
]; /* room for wider coefficient */
970 uInt widerhi
=DFWWORD(wider
, 0); /* top word */
973 GETWCOEFF(wider
, bcdar
);
975 num
.msd
=bcdar
; /* MSD is here always */
976 num
.lsd
=bcdar
+DECWPMAX
-1; /* LSD is here always */
977 num
.sign
=widerhi
&0x80000000; /* extract sign [DECFLOAT_Sign=Neg] */
979 /* decode the wider combination field to exponent */
980 exp
=DECCOMBWEXP
[widerhi
>>26]; /* decode from wider combination field */
981 /* if it is a special there's nothing to do unless sNaN; if it's */
982 /* finite then add the (wider) exponent continuation and unbias */
983 if (EXPISSPECIAL(exp
)) exp
=widerhi
&0x7e000000; /* include sNaN selector */
984 else exp
+=GETWECON(wider
)-DECWBIAS
;
987 /* decShowNum(&num, "dffw"); */
988 return decFinalize(result
, &num
, set
);/* round, check, and lay out */
989 } /* decFloatFromWider */
992 /* ------------------------------------------------------------------ */
993 /* decFloatGetCoefficient -- get coefficient as BCD8 */
995 /* df is the decFloat from which to extract the coefficient */
996 /* bcdar is where DECPMAX bytes will be written, one BCD digit in */
997 /* each byte (BCD8 encoding); if df is a NaN the first byte will */
998 /* be zero, and if it is infinite they will all be zero */
999 /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
1002 /* No error is possible, and no status will be set. If df is a */
1003 /* special value the array is set to zeros (for Infinity) or to the */
1004 /* payload of a qNaN or sNaN. */
1005 /* ------------------------------------------------------------------ */
1006 Int
decFloatGetCoefficient(const decFloat
*df
, uByte
*bcdar
) {
1007 if (DFISINF(df
)) memset(bcdar
, 0, DECPMAX
);
1009 GETCOEFF(df
, bcdar
); /* use macro */
1010 if (DFISNAN(df
)) bcdar
[0]=0; /* MSD needs correcting */
1012 return DFISSIGNED(df
);
1013 } /* decFloatGetCoefficient */
1015 /* ------------------------------------------------------------------ */
1016 /* decFloatGetExponent -- get unbiased exponent */
1018 /* df is the decFloat from which to extract the exponent */
1019 /* returns the exponent, q. */
1021 /* No error is possible, and no status will be set. If df is a */
1022 /* special value the first seven bits of the decFloat are returned, */
1023 /* left adjusted and with the first (sign) bit set to 0 (followed by */
1024 /* 25 0 bits). e.g., -sNaN would return 0x7e000000 (DECFLOAT_sNaN). */
1025 /* ------------------------------------------------------------------ */
1026 Int
decFloatGetExponent(const decFloat
*df
) {
1027 if (DFISSPECIAL(df
)) return DFWORD(df
, 0)&0x7e000000;
1028 return GETEXPUN(df
);
1029 } /* decFloatGetExponent */
1031 /* ------------------------------------------------------------------ */
1032 /* decFloatSetCoefficient -- set coefficient from BCD8 */
1034 /* df is the target decFloat (and source of exponent/special value) */
1035 /* bcdar holds DECPMAX digits to set the coefficient from, one */
1036 /* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
1037 /* if df is a NaN; all are ignored if df is infinite. */
1038 /* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
1039 /* returns df, which will be canonical */
1041 /* No error is possible, and no status will be set. */
1042 /* ------------------------------------------------------------------ */
1043 decFloat
* decFloatSetCoefficient(decFloat
*df
, const uByte
*bcdar
,
1045 uInt exp
; /* for exponent */
1046 uByte bcdzero
[DECPMAX
]; /* for infinities */
1048 /* Exponent/special code is extracted from df */
1049 if (DFISSPECIAL(df
)) {
1050 exp
=DFWORD(df
, 0)&0x7e000000;
1052 memset(bcdzero
, 0, DECPMAX
);
1053 return decFloatFromBCD(df
, exp
, bcdzero
, sig
);
1056 else exp
=GETEXPUN(df
);
1057 return decFloatFromBCD(df
, exp
, bcdar
, sig
);
1058 } /* decFloatSetCoefficient */
1060 /* ------------------------------------------------------------------ */
1061 /* decFloatSetExponent -- set exponent or special value */
1063 /* df is the target decFloat (and source of coefficient/payload) */
1064 /* set is the context for reporting status */
1065 /* exp is the unbiased exponent, q, or a special value in the form */
1066 /* returned by decFloatGetExponent */
1067 /* returns df, which will be canonical */
1069 /* No error is possible, but Overflow or Underflow might occur. */
1070 /* ------------------------------------------------------------------ */
1071 decFloat
* decFloatSetExponent(decFloat
*df
, decContext
*set
, Int exp
) {
1072 uByte bcdcopy
[DECPMAX
]; /* for coefficient */
1073 bcdnum num
; /* work */
1075 num
.sign
=decFloatGetCoefficient(df
, bcdcopy
); /* extract coefficient */
1076 if (DFISSPECIAL(df
)) { /* MSD or more needs correcting */
1077 if (DFISINF(df
)) memset(bcdcopy
, 0, DECPMAX
);
1081 num
.lsd
=bcdcopy
+DECPMAX
-1;
1082 return decFinalize(df
, &num
, set
);
1083 } /* decFloatSetExponent */
1085 /* ------------------------------------------------------------------ */
1086 /* decFloatRadix -- returns the base (10) */
1088 /* df is any decFloat of this format */
1089 /* ------------------------------------------------------------------ */
1090 uInt
decFloatRadix(const decFloat
*df
) {
1091 if (df
) return 10; /* to placate compiler */
1093 } /* decFloatRadix */
1095 /* ------------------------------------------------------------------ */
1096 /* decFloatShow -- printf a decFloat in hexadecimal and decimal */
1097 /* df is the decFloat to show */
1098 /* tag is a tag string displayed with the number */
1100 /* This is a debug aid; the precise format of the string may change. */
1101 /* ------------------------------------------------------------------ */
1102 void decFloatShow(const decFloat
*df
, const char *tag
) {
1103 char hexbuf
[DECBYTES
*2+DECBYTES
/4+1]; /* NB blank after every fourth */
1104 char buff
[DECSTRING
]; /* for value in decimal */
1107 for (i
=0; i
<DECBYTES
; i
++) {
1109 sprintf(&hexbuf
[j
], "%02x", df
->bytes
[DECBYTES
-1-i
]);
1111 sprintf(&hexbuf
[j
], "%02x", df
->bytes
[i
]);
1114 /* the next line adds blank (and terminator) after final pair, too */
1115 if ((i
+1)%4==0) {strcpy(&hexbuf
[j
], " "); j
++;}
1117 decFloatToString(df
, buff
);
1118 printf(">%s> %s [big-endian] %s\n", tag
, hexbuf
, buff
);
1120 } /* decFloatShow */
1122 /* ------------------------------------------------------------------ */
1123 /* decFloatToBCD -- get sign, exponent, and BCD8 from a decFloat */
1125 /* df is the source decFloat */
1126 /* exp will be set to the unbiased exponent, q, or to a special */
1127 /* value in the form returned by decFloatGetExponent */
1128 /* bcdar is where DECPMAX bytes will be written, one BCD digit in */
1129 /* each byte (BCD8 encoding); if df is a NaN the first byte will */
1130 /* be zero, and if it is infinite they will all be zero */
1131 /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
1134 /* No error is possible, and no status will be set. */
1135 /* ------------------------------------------------------------------ */
1136 Int
decFloatToBCD(const decFloat
*df
, Int
*exp
, uByte
*bcdar
) {
1138 memset(bcdar
, 0, DECPMAX
);
1139 *exp
=DFWORD(df
, 0)&0x7e000000;
1142 GETCOEFF(df
, bcdar
); /* use macro */
1144 bcdar
[0]=0; /* MSD needs correcting */
1145 *exp
=DFWORD(df
, 0)&0x7e000000;
1151 return DFISSIGNED(df
);
1152 } /* decFloatToBCD */
1154 /* ------------------------------------------------------------------ */
1155 /* decFloatToEngString -- conversion to numeric string, engineering */
1157 /* df is the decFloat format number to convert */
1158 /* string is the string where the result will be laid out */
1160 /* string must be at least DECPMAX+9 characters (the worst case is */
1161 /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
1162 /* DECEMAXD<=4); this condition is asserted above */
1164 /* No error is possible, and no status will be set */
1165 /* ------------------------------------------------------------------ */
1166 char * decFloatToEngString(const decFloat
*df
, char *string
){
1167 uInt msd
; /* coefficient MSD */
1168 Int exp
; /* exponent top two bits or full */
1169 uInt comb
; /* combination field */
1170 char *cstart
; /* coefficient start */
1171 char *c
; /* output pointer in string */
1172 char *s
, *t
; /* .. (source, target) */
1173 Int pre
, e
; /* work */
1174 const uByte
*u
; /* .. */
1176 /* Source words; macro handles endianness */
1177 uInt sourhi
=DFWORD(df
, 0); /* word with sign */
1179 uInt sourlo
=DFWORD(df
, 1);
1181 uInt sourmh
=DFWORD(df
, 1);
1182 uInt sourml
=DFWORD(df
, 2);
1183 uInt sourlo
=DFWORD(df
, 3);
1186 c
=string
; /* where result will go */
1187 if (((Int
)sourhi
)<0) *c
++='-'; /* handle sign */
1188 comb
=sourhi
>>26; /* sign+combination field */
1189 msd
=DECCOMBMSD
[comb
]; /* decode the combination field */
1190 exp
=DECCOMBEXP
[comb
]; /* .. */
1192 if (EXPISSPECIAL(exp
)) { /* special */
1193 if (exp
==DECFLOAT_Inf
) { /* infinity */
1195 strcpy(c
+3, "inity");
1196 return string
; /* easy */
1198 if (sourhi
&0x02000000) *c
++='s'; /* sNaN */
1199 strcpy(c
, "NaN"); /* complete word */
1200 c
+=3; /* step past */
1201 /* quick exit if the payload is zero */
1203 if ((sourhi
&0x000fffff)==0) return string
;
1205 if (sourlo
==0 && (sourhi
&0x0003ffff)==0) return string
;
1207 if (sourlo
==0 && sourml
==0 && sourmh
==0
1208 && (sourhi
&0x00003fff)==0) return string
;
1210 /* otherwise drop through to add integer; set correct exp etc. */
1211 exp
=0; msd
=0; /* setup for following code */
1213 else { /* complete exponent; top two bits are in place */
1214 exp
+=GETECON(df
)-DECBIAS
; /* .. + continuation and unbias */
1217 /* convert the digits of the significand to characters */
1218 cstart
=c
; /* save start of coefficient */
1219 if (msd
) *c
++=(char)('0'+(char)msd
); /* non-zero most significant digit */
1221 /* Decode the declets. After extracting each declet, it is */
1222 /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
1223 /* are the three encoded BCD8 digits followed by a 1-byte length */
1224 /* (significant digits, except that 000 has length 0). This allows */
1225 /* us to left-align the first declet with non-zero content, then */
1226 /* the remaining ones are full 3-char length. Fixed-length copies */
1227 /* are used because variable-length memcpy causes a subroutine call */
1228 /* in at least two compilers. (The copies are length 4 for speed */
1229 /* and are safe because the last item in the array is of length */
1230 /* three and has the length byte following.) */
1231 #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
1232 if (c!=cstart) {UINTAT(c)=UINTAT(u)|CHARMASK; c+=3;} \
1233 else if (*(u+3)) { \
1234 UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; c+=*(u+3);}
1237 dpd2char(sourhi
>>10); /* declet 1 */
1238 dpd2char(sourhi
); /* declet 2 */
1241 dpd2char(sourhi
>>8); /* declet 1 */
1242 dpd2char((sourhi
<<2) | (sourlo
>>30)); /* declet 2 */
1243 dpd2char(sourlo
>>20); /* declet 3 */
1244 dpd2char(sourlo
>>10); /* declet 4 */
1245 dpd2char(sourlo
); /* declet 5 */
1248 dpd2char(sourhi
>>4); /* declet 1 */
1249 dpd2char((sourhi
<<6) | (sourmh
>>26)); /* declet 2 */
1250 dpd2char(sourmh
>>16); /* declet 3 */
1251 dpd2char(sourmh
>>6); /* declet 4 */
1252 dpd2char((sourmh
<<4) | (sourml
>>28)); /* declet 5 */
1253 dpd2char(sourml
>>18); /* declet 6 */
1254 dpd2char(sourml
>>8); /* declet 7 */
1255 dpd2char((sourml
<<2) | (sourlo
>>30)); /* declet 8 */
1256 dpd2char(sourlo
>>20); /* declet 9 */
1257 dpd2char(sourlo
>>10); /* declet 10 */
1258 dpd2char(sourlo
); /* declet 11 */
1261 if (c
==cstart
) *c
++='0'; /* all zeros, empty -- make "0" */
1263 if (exp
==0) { /* integer or NaN case -- easy */
1264 *c
='\0'; /* terminate */
1267 /* non-0 exponent */
1269 e
=0; /* assume no E */
1270 pre
=(Int
)(c
-cstart
)+exp
; /* length+exp [c->LSD+1] */
1271 /* [here, pre-exp is the digits count (==1 for zero)] */
1273 if (exp
>0 || pre
<-5) { /* need exponential form */
1274 e
=pre
-1; /* calculate E value */
1275 pre
=1; /* assume one digit before '.' */
1276 if (e
!=0) { /* engineering: may need to adjust */
1277 Int adj
; /* adjustment */
1278 /* The C remainder operator is undefined for negative numbers, so */
1279 /* a positive remainder calculation must be used here */
1282 if (adj
!=0) adj
=3-adj
;
1288 /* if dealing with zero still produce an exponent which is a */
1289 /* multiple of three, as expected, but there will only be the */
1290 /* one zero before the E, still. Otherwise note the padding. */
1291 if (!DFISZERO(df
)) pre
+=adj
;
1292 else { /* is zero */
1293 if (adj
!=0) { /* 0.00Esnn needed */
1298 } /* engineering adjustment */
1299 } /* exponential form */
1300 /* printf("e=%ld pre=%ld exp=%ld\n", (LI)e, (LI)pre, (LI)exp); */
1302 /* modify the coefficient, adding 0s, '.', and E+nn as needed */
1303 if (pre
>0) { /* ddd.ddd (plain), perhaps with E */
1304 /* or dd00 padding for engineering */
1305 char *dotat
=cstart
+pre
;
1306 if (dotat
<c
) { /* if embedded dot needed... */
1307 /* move by fours; there must be space for junk at the end */
1308 /* because there is still space for exponent */
1309 s
=dotat
+ROUNDDOWN4(c
-dotat
); /* source */
1312 for (; s
>=dotat
; s
-=4, t
-=4) UINTAT(t
)=UINTAT(s
);
1314 c
++; /* length increased by one */
1316 else for (; c
<dotat
; c
++) *c
='0'; /* pad for engineering */
1319 /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (may have
1320 E, but only for 0.00E+3 kind of case -- with plenty of spare
1321 space in this case */
1322 pre
=-pre
+2; /* gap width, including "0." */
1323 t
=cstart
+ROUNDDOWN4(c
-cstart
)+pre
; /* preferred first target point */
1324 /* backoff if too far to the right */
1325 if (t
>string
+DECSTRING
-5) t
=string
+DECSTRING
-5; /* adjust to fit */
1326 /* now shift the entire coefficient to the right, being careful not */
1327 /* to access to the left of string */
1328 for (s
=t
-pre
; s
>=string
; s
-=4, t
-=4) UINTAT(t
)=UINTAT(s
);
1329 /* for Quads and Singles there may be a character or two left... */
1330 s
+=3; /* where next would come from */
1331 for(; s
>=cstart
; s
--, t
--) *(t
+3)=*(s
);
1332 /* now have fill 0. through 0.00000; use overlaps to avoid tests */
1334 UINTAT(cstart
+pre
-4)=UINTAT("0000");
1335 UINTAT(cstart
)=UINTAT("0.00");
1338 *(cstart
+pre
-1)='0';
1339 USHORTAT(cstart
)=USHORTAT("0.");
1341 c
+=pre
; /* to end */
1344 /* finally add the E-part, if needed; it will never be 0, and has */
1345 /* a maximum length of 3 or 4 digits (asserted above) */
1347 USHORTAT(c
)=USHORTAT("E+"); /* starts with E, assume + */
1350 *c
='-'; /* oops, need '-' */
1351 e
=-e
; /* uInt, please */
1354 /* Three-character exponents are easy; 4-character a little trickier */
1356 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1357 /* copy fixed 4 characters [is safe], starting at non-zero */
1358 /* and with character mask to convert BCD to char */
1359 UINTAT(c
)=UINTAT(u
+3-*(u
+3))|CHARMASK
;
1360 c
+=*(u
+3); /* bump pointer appropriately */
1362 if (e
<1000) { /* 3 (or fewer) digits case */
1363 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1364 UINTAT(c
)=UINTAT(u
+3-*(u
+3))|CHARMASK
; /* [as above] */
1365 c
+=*(u
+3); /* bump pointer appropriately */
1367 else { /* 4-digits */
1368 Int thou
=((e
>>3)*1049)>>17; /* e/1000 */
1369 Int rem
=e
-(1000*thou
); /* e%1000 */
1370 *c
++=(char)('0'+(char)thou
); /* the thousands digit */
1371 u
=&BIN2BCD8
[rem
*4]; /* -> 3 digits + length byte */
1372 UINTAT(c
)=UINTAT(u
)|CHARMASK
; /* copy fixed 3+1 characters [is safe] */
1373 c
+=3; /* bump pointer, always 3 digits */
1377 *c
='\0'; /* terminate */
1378 /*printf("res %s\n", string); */
1380 } /* decFloatToEngString */
1382 /* ------------------------------------------------------------------ */
1383 /* decFloatToPacked -- convert decFloat to Packed decimal + exponent */
1385 /* df is the source decFloat */
1386 /* exp will be set to the unbiased exponent, q, or to a special */
1387 /* value in the form returned by decFloatGetExponent */
1388 /* packed is where DECPMAX nibbles will be written with the sign as */
1389 /* final nibble (0x0c for +, 0x0d for -); a NaN has a first nibble */
1390 /* of zero, and an infinity is all zeros. decDouble and decQuad */
1391 /* have a additional leading zero nibble, leading to result */
1392 /* lengths of 4, 9, and 18 bytes. */
1393 /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
1396 /* No error is possible, and no status will be set. */
1397 /* ------------------------------------------------------------------ */
1398 Int
decFloatToPacked(const decFloat
*df
, Int
*exp
, uByte
*packed
) {
1399 uByte bcdar
[DECPMAX
+2]; /* work buffer */
1400 uByte
*ip
=bcdar
, *op
=packed
; /* work pointers */
1402 memset(bcdar
, 0, DECPMAX
+2);
1406 GETCOEFF(df
, bcdar
+1); /* use macro */
1408 bcdar
[1]=0; /* MSD needs clearing */
1409 *exp
=DFWORD(df
, 0)&0x7e000000;
1415 /* now pack; coefficient currently at bcdar+1 */
1417 ip
++; /* ignore first byte */
1419 *ip
=0; /* need leading zero */
1421 /* set final byte to Packed BCD sign value */
1422 bcdar
[DECPMAX
+1]=(DFISSIGNED(df
) ? DECPMINUS
: DECPPLUS
);
1423 /* pack an even number of bytes... */
1424 for (; op
<packed
+((DECPMAX
+2)/2); op
++, ip
+=2) {
1425 *op
=(uByte
)((*ip
<<4)+*(ip
+1));
1427 return (bcdar
[DECPMAX
+1]==DECPMINUS
? DECFLOAT_Sign
: 0);
1428 } /* decFloatToPacked */
1430 /* ------------------------------------------------------------------ */
1431 /* decFloatToString -- conversion to numeric string */
1433 /* df is the decFloat format number to convert */
1434 /* string is the string where the result will be laid out */
1436 /* string must be at least DECPMAX+9 characters (the worst case is */
1437 /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
1438 /* DECEMAXD<=4); this condition is asserted above */
1440 /* No error is possible, and no status will be set */
1441 /* ------------------------------------------------------------------ */
1442 char * decFloatToString(const decFloat
*df
, char *string
){
1443 uInt msd
; /* coefficient MSD */
1444 Int exp
; /* exponent top two bits or full */
1445 uInt comb
; /* combination field */
1446 char *cstart
; /* coefficient start */
1447 char *c
; /* output pointer in string */
1448 char *s
, *t
; /* .. (source, target) */
1449 Int pre
, e
; /* work */
1450 const uByte
*u
; /* .. */
1452 /* Source words; macro handles endianness */
1453 uInt sourhi
=DFWORD(df
, 0); /* word with sign */
1455 uInt sourlo
=DFWORD(df
, 1);
1457 uInt sourmh
=DFWORD(df
, 1);
1458 uInt sourml
=DFWORD(df
, 2);
1459 uInt sourlo
=DFWORD(df
, 3);
1462 c
=string
; /* where result will go */
1463 if (((Int
)sourhi
)<0) *c
++='-'; /* handle sign */
1464 comb
=sourhi
>>26; /* sign+combination field */
1465 msd
=DECCOMBMSD
[comb
]; /* decode the combination field */
1466 exp
=DECCOMBEXP
[comb
]; /* .. */
1468 if (EXPISSPECIAL(exp
)) { /* special */
1469 if (exp
==DECFLOAT_Inf
) { /* infinity */
1470 strcpy(c
, "Infinity");
1471 return string
; /* easy */
1473 if (sourhi
&0x02000000) *c
++='s'; /* sNaN */
1474 strcpy(c
, "NaN"); /* complete word */
1475 c
+=3; /* step past */
1476 /* quick exit if the payload is zero */
1478 if ((sourhi
&0x000fffff)==0) return string
;
1480 if (sourlo
==0 && (sourhi
&0x0003ffff)==0) return string
;
1482 if (sourlo
==0 && sourml
==0 && sourmh
==0
1483 && (sourhi
&0x00003fff)==0) return string
;
1485 /* otherwise drop through to add integer; set correct exp etc. */
1486 exp
=0; msd
=0; /* setup for following code */
1488 else { /* complete exponent; top two bits are in place */
1489 exp
+=GETECON(df
)-DECBIAS
; /* .. + continuation and unbias */
1492 /* convert the digits of the significand to characters */
1493 cstart
=c
; /* save start of coefficient */
1494 if (msd
) *c
++=(char)('0'+(char)msd
); /* non-zero most significant digit */
1496 /* Decode the declets. After extracting each declet, it is */
1497 /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
1498 /* are the three encoded BCD8 digits followed by a 1-byte length */
1499 /* (significant digits, except that 000 has length 0). This allows */
1500 /* us to left-align the first declet with non-zero content, then */
1501 /* the remaining ones are full 3-char length. Fixed-length copies */
1502 /* are used because variable-length memcpy causes a subroutine call */
1503 /* in at least two compilers. (The copies are length 4 for speed */
1504 /* and are safe because the last item in the array is of length */
1505 /* three and has the length byte following.) */
1506 #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
1507 if (c!=cstart) {UINTAT(c)=UINTAT(u)|CHARMASK; c+=3;} \
1508 else if (*(u+3)) { \
1509 UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; c+=*(u+3);}
1512 dpd2char(sourhi
>>10); /* declet 1 */
1513 dpd2char(sourhi
); /* declet 2 */
1516 dpd2char(sourhi
>>8); /* declet 1 */
1517 dpd2char((sourhi
<<2) | (sourlo
>>30)); /* declet 2 */
1518 dpd2char(sourlo
>>20); /* declet 3 */
1519 dpd2char(sourlo
>>10); /* declet 4 */
1520 dpd2char(sourlo
); /* declet 5 */
1523 dpd2char(sourhi
>>4); /* declet 1 */
1524 dpd2char((sourhi
<<6) | (sourmh
>>26)); /* declet 2 */
1525 dpd2char(sourmh
>>16); /* declet 3 */
1526 dpd2char(sourmh
>>6); /* declet 4 */
1527 dpd2char((sourmh
<<4) | (sourml
>>28)); /* declet 5 */
1528 dpd2char(sourml
>>18); /* declet 6 */
1529 dpd2char(sourml
>>8); /* declet 7 */
1530 dpd2char((sourml
<<2) | (sourlo
>>30)); /* declet 8 */
1531 dpd2char(sourlo
>>20); /* declet 9 */
1532 dpd2char(sourlo
>>10); /* declet 10 */
1533 dpd2char(sourlo
); /* declet 11 */
1536 if (c
==cstart
) *c
++='0'; /* all zeros, empty -- make "0" */
1538 /*[This fast path is valid but adds 3-5 cycles to worst case length] */
1539 /*if (exp==0) { // integer or NaN case -- easy */
1540 /* *c='\0'; // terminate */
1541 /* return string; */
1544 e
=0; /* assume no E */
1545 pre
=(Int
)(c
-cstart
)+exp
; /* length+exp [c->LSD+1] */
1546 /* [here, pre-exp is the digits count (==1 for zero)] */
1548 if (exp
>0 || pre
<-5) { /* need exponential form */
1549 e
=pre
-1; /* calculate E value */
1550 pre
=1; /* assume one digit before '.' */
1551 } /* exponential form */
1553 /* modify the coefficient, adding 0s, '.', and E+nn as needed */
1554 if (pre
>0) { /* ddd.ddd (plain), perhaps with E */
1555 char *dotat
=cstart
+pre
;
1556 if (dotat
<c
) { /* if embedded dot needed... */
1557 /* move by fours; there must be space for junk at the end */
1558 /* because there is still space for exponent */
1559 s
=dotat
+ROUNDDOWN4(c
-dotat
); /* source */
1562 for (; s
>=dotat
; s
-=4, t
-=4) UINTAT(t
)=UINTAT(s
);
1564 c
++; /* length increased by one */
1567 /* finally add the E-part, if needed; it will never be 0, and has */
1568 /* a maximum length of 3 or 4 digits (asserted above) */
1570 USHORTAT(c
)=USHORTAT("E+"); /* starts with E, assume + */
1573 *c
='-'; /* oops, need '-' */
1574 e
=-e
; /* uInt, please */
1577 /* Three-character exponents are easy; 4-character a little trickier */
1579 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1580 /* copy fixed 4 characters [is safe], starting at non-zero */
1581 /* and with character mask to convert BCD to char */
1582 UINTAT(c
)=UINTAT(u
+3-*(u
+3))|CHARMASK
;
1583 c
+=*(u
+3); /* bump pointer appropriately */
1585 if (e
<1000) { /* 3 (or fewer) digits case */
1586 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1587 UINTAT(c
)=UINTAT(u
+3-*(u
+3))|CHARMASK
; /* [as above] */
1588 c
+=*(u
+3); /* bump pointer appropriately */
1590 else { /* 4-digits */
1591 Int thou
=((e
>>3)*1049)>>17; /* e/1000 */
1592 Int rem
=e
-(1000*thou
); /* e%1000 */
1593 *c
++=(char)('0'+(char)thou
); /* the thousands digit */
1594 u
=&BIN2BCD8
[rem
*4]; /* -> 3 digits + length byte */
1595 UINTAT(c
)=UINTAT(u
)|CHARMASK
; /* copy fixed 3+1 characters [is safe] */
1596 c
+=3; /* bump pointer, always 3 digits */
1600 *c
='\0'; /* add terminator */
1601 /*printf("res %s\n", string); */
1605 /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
1606 /* Surprisingly, this is close to being the worst-case path, so the */
1607 /* shift is done by fours; this is a little tricky because the */
1608 /* rightmost character to be written must not be beyond where the */
1609 /* rightmost terminator could be -- so backoff to not touch */
1610 /* terminator position if need be (this can make exact alignments */
1611 /* for full Doubles, but in some cases needs care not to access too */
1612 /* far to the left) */
1614 pre
=-pre
+2; /* gap width, including "0." */
1615 t
=cstart
+ROUNDDOWN4(c
-cstart
)+pre
; /* preferred first target point */
1616 /* backoff if too far to the right */
1617 if (t
>string
+DECSTRING
-5) t
=string
+DECSTRING
-5; /* adjust to fit */
1618 /* now shift the entire coefficient to the right, being careful not */
1619 /* to access to the left of string */
1620 for (s
=t
-pre
; s
>=string
; s
-=4, t
-=4) UINTAT(t
)=UINTAT(s
);
1621 /* for Quads and Singles there may be a character or two left... */
1622 s
+=3; /* where next would come from */
1623 for(; s
>=cstart
; s
--, t
--) *(t
+3)=*(s
);
1624 /* now have fill 0. through 0.00000; use overlaps to avoid tests */
1626 UINTAT(cstart
+pre
-4)=UINTAT("0000");
1627 UINTAT(cstart
)=UINTAT("0.00");
1630 *(cstart
+pre
-1)='0';
1631 USHORTAT(cstart
)=USHORTAT("0.");
1633 *(c
+pre
)='\0'; /* terminate */
1635 } /* decFloatToString */
1637 /* ------------------------------------------------------------------ */
1638 /* decFloatToWider -- conversion to next-wider format */
1640 /* source is the decFloat format number which gets the result of */
1641 /* the conversion */
1642 /* wider is the decFloatWider format number which will be narrowed */
1645 /* Widening is always exact; no status is set (sNaNs are copied and */
1646 /* do not signal). The result will be canonical if the source is, */
1647 /* and may or may not be if the source is not. */
1648 /* ------------------------------------------------------------------ */
1649 /* widening is not possible for decQuad format numbers; simply omit */
1651 decFloatWider
* decFloatToWider(const decFloat
*source
, decFloatWider
*wider
) {
1654 /* Construct and copy the sign word */
1655 if (DFISSPECIAL(source
)) {
1656 /* copy sign, combination, and first bit of exponent (sNaN selector) */
1657 DFWWORD(wider
, 0)=DFWORD(source
, 0)&0xfe000000;
1660 else { /* is finite number */
1661 uInt exp
=GETEXPUN(source
)+DECWBIAS
; /* get unbiased exponent and rebias */
1662 uInt code
=(exp
>>DECWECONL
)<<29; /* set two bits of exp [msd=0] */
1663 code
|=(exp
<<(32-6-DECWECONL
)) & 0x03ffffff; /* add exponent continuation */
1664 code
|=DFWORD(source
, 0)&0x80000000; /* add sign */
1665 DFWWORD(wider
, 0)=code
; /* .. and place top word in wider */
1666 msd
=GETMSD(source
); /* get source coefficient MSD [0-9] */
1668 /* Copy the coefficient and clear any 'unused' words to left */
1670 DFWWORD(wider
, 1)=(DFWORD(source
, 0)&0x000fffff)|(msd
<<20);
1672 DFWWORD(wider
, 2)=(DFWORD(source
, 0)&0x0003ffff)|(msd
<<18);
1673 DFWWORD(wider
, 3)=DFWORD(source
, 1);
1674 DFWWORD(wider
, 1)=0;
1677 } /* decFloatToWider */
1680 /* ------------------------------------------------------------------ */
1681 /* decFloatVersion -- return package version string */
1683 /* returns a constant string describing this package */
1684 /* ------------------------------------------------------------------ */
1685 const char *decFloatVersion(void) {
1687 } /* decFloatVersion */
1689 /* ------------------------------------------------------------------ */
1690 /* decFloatZero -- set to canonical (integer) zero */
1692 /* df is the decFloat format number to integer +0 (q=0, c=+0) */
1695 /* No error is possible, and no status can be set. */
1696 /* ------------------------------------------------------------------ */
1697 decFloat
* decFloatZero(decFloat
*df
){
1698 DFWORD(df
, 0)=ZEROWORD
; /* set appropriate top word */
1706 /* decFloatShow(df, "zero"); */
1708 } /* decFloatZero */
1710 /* ------------------------------------------------------------------ */
1711 /* Private generic function (not format-specific) for development use */
1712 /* ------------------------------------------------------------------ */
1713 /* This is included once only, for all to use */
1714 #if QUAD && (DECCHECK || DECTRACE)
1715 /* ---------------------------------------------------------------- */
1716 /* decShowNum -- display bcd8 number in debug form */
1718 /* num is the bcdnum to display */
1719 /* tag is a string to label the display */
1720 /* ---------------------------------------------------------------- */
1721 void decShowNum(const bcdnum
*num
, const char *tag
) {
1722 const char *csign
="+"; /* sign character */
1723 uByte
*ub
; /* work */
1724 if (num
->sign
==DECFLOAT_Sign
) csign
="-";
1726 printf(">%s> ", tag
);
1727 if (num
->exponent
==DECFLOAT_Inf
) printf("%sInfinity", csign
);
1728 else if (num
->exponent
==DECFLOAT_qNaN
) printf("%sqNaN", csign
);
1729 else if (num
->exponent
==DECFLOAT_sNaN
) printf("%ssNaN", csign
);
1731 char qbuf
[10]; /* for right-aligned q */
1733 const uByte
*u
; /* .. */
1734 Int e
=num
->exponent
; /* .. exponent */
1736 c
=&qbuf
[2]; /* where exponent will go */
1737 /* lay out the exponent */
1739 *c
++='-'; /* add '-' */
1740 e
=-e
; /* uInt, please */
1743 #error Exponent form is too long for ShowNum to lay out
1745 if (e
==0) *c
++='0'; /* 0-length case */
1746 else if (e
<1000) { /* 3 (or fewer) digits case */
1747 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1748 UINTAT(c
)=UINTAT(u
+3-*(u
+3))|CHARMASK
; /* [as above] */
1749 c
+=*(u
+3); /* bump pointer appropriately */
1751 else { /* 4-digits */
1752 Int thou
=((e
>>3)*1049)>>17; /* e/1000 */
1753 Int rem
=e
-(1000*thou
); /* e%1000 */
1754 *c
++=(char)('0'+(char)thou
); /* the thousands digit */
1755 u
=&BIN2BCD8
[rem
*4]; /* -> 3 digits + length byte */
1756 UINTAT(c
)=UINTAT(u
)|CHARMASK
; /* copy fixed 3+1 characters [is safe] */
1757 c
+=3; /* bump pointer, always 3 digits */
1759 *c
='\0'; /* add terminator */
1760 printf("%7s c=%s", qbuf
, csign
);
1763 if (!EXPISSPECIAL(num
->exponent
) || num
->msd
!=num
->lsd
|| *num
->lsd
!=0) {
1764 for (ub
=num
->msd
; ub
<=num
->lsd
; ub
++) { /* coefficient... */
1766 if ((num
->lsd
-ub
)%3==0 && ub
!=num
->lsd
) printf(" "); /* 4-space */
This page took 0.084037 seconds and 4 git commands to generate.