1 /* Common code for fixed-size types in the decNumber C Library.
2 Copyright (C) 2007, 2009 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 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 /* ------------------------------------------------------------------ */
27 /* decCommon.c -- common code for all three fixed-size types */
28 /* ------------------------------------------------------------------ */
29 /* This module comprises code that is shared between all the formats */
30 /* (decSingle, decDouble, and decQuad); it includes set and extract */
31 /* of format components, widening, narrowing, and string conversions. */
33 /* Unlike decNumber, parameterization takes place at compile time */
34 /* rather than at runtime. The parameters are set in the decDouble.c */
35 /* (etc.) files, which then include this one to produce the compiled */
36 /* code. The functions here, therefore, are code shared between */
37 /* multiple formats. */
38 /* ------------------------------------------------------------------ */
39 /* Names here refer to decFloat rather than to decDouble, etc., and */
40 /* the functions are in strict alphabetical order. */
41 /* Constants, tables, and debug function(s) are included only for QUAD */
42 /* (which will always be compiled if DOUBLE or SINGLE are used). */
44 /* Whenever a decContext is used, only the status may be set (using */
45 /* OR) or the rounding mode read; all other fields are ignored and */
48 #include "decCommonSymbols.h"
50 /* names for simpler testing and default context */
55 #define DEFCONTEXT DEC_INIT_DECIMAL32
60 #define DEFCONTEXT DEC_INIT_DECIMAL64
65 #define DEFCONTEXT DEC_INIT_DECIMAL128
67 #error Unexpected DECPMAX value
72 #if DECPMAX!=7 && DECPMAX!=16 && DECPMAX!=34
73 #error Unexpected Pmax (DECPMAX) value for this module
76 /* Assert facts about digit characters, etc. */
78 #error This module assumes characters are of the form 0b....nnnn
79 /* where .... are don't care 4 bits and nnnn is 0000 through 1001 */
81 #if ('9'&0xf0)==('.'&0xf0)
82 #error This module assumes '.' has a different mask than a digit
85 /* Assert ToString lay-out conditions */
86 #if DECSTRING<DECPMAX+9
87 #error ToString needs at least 8 characters for lead-in and dot
89 #if DECPMAX+DECEMAXD+5 > DECSTRING
90 #error Exponent form can be too long for ToString to lay out safely
93 #error Exponent form is too long for ToString to lay out
94 /* Note: code for up to 9 digits exists in archives [decOct] */
97 /* Private functions used here and possibly in decBasic.c, etc. */
98 static decFloat
* decFinalize(decFloat
*, bcdnum
*, decContext
*);
99 static Flag
decBiStr(const char *, const char *, const char *);
101 /* Macros and private tables; those which are not format-dependent */
102 /* are only included if decQuad is being built. */
104 /* ------------------------------------------------------------------ */
105 /* Combination field lookup tables (uInts to save measurable work) */
107 /* DECCOMBEXP - 2 most-significant-bits of exponent (00, 01, or */
108 /* 10), shifted left for format, or DECFLOAT_Inf/NaN */
109 /* DECCOMBWEXP - The same, for the next-wider format (unless QUAD) */
110 /* DECCOMBMSD - 4-bit most-significant-digit */
111 /* [0 if the index is a special (Infinity or NaN)] */
112 /* DECCOMBFROM - 5-bit combination field from EXP top bits and MSD */
113 /* (placed in uInt so no shift is needed) */
115 /* DECCOMBEXP, DECCOMBWEXP, and DECCOMBMSD are indexed by the sign */
116 /* and 5-bit combination field (0-63, the second half of the table */
117 /* identical to the first half) */
118 /* DECCOMBFROM is indexed by expTopTwoBits*16 + msd */
120 /* DECCOMBMSD and DECCOMBFROM are not format-dependent and so are */
121 /* only included once, when QUAD is being built */
122 /* ------------------------------------------------------------------ */
123 static const uInt DECCOMBEXP
[64]={
124 0, 0, 0, 0, 0, 0, 0, 0,
125 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
,
126 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
,
127 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
,
128 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
,
129 0, 0, 1<<DECECONL
, 1<<DECECONL
,
130 2<<DECECONL
, 2<<DECECONL
, DECFLOAT_Inf
, DECFLOAT_NaN
,
131 0, 0, 0, 0, 0, 0, 0, 0,
132 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
,
133 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
, 1<<DECECONL
,
134 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
,
135 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
, 2<<DECECONL
,
136 0, 0, 1<<DECECONL
, 1<<DECECONL
,
137 2<<DECECONL
, 2<<DECECONL
, DECFLOAT_Inf
, DECFLOAT_NaN
};
139 static const uInt DECCOMBWEXP
[64]={
140 0, 0, 0, 0, 0, 0, 0, 0,
141 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
,
142 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
,
143 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
,
144 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
,
145 0, 0, 1<<DECWECONL
, 1<<DECWECONL
,
146 2<<DECWECONL
, 2<<DECWECONL
, DECFLOAT_Inf
, DECFLOAT_NaN
,
147 0, 0, 0, 0, 0, 0, 0, 0,
148 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
,
149 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
, 1<<DECWECONL
,
150 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
,
151 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
, 2<<DECWECONL
,
152 0, 0, 1<<DECWECONL
, 1<<DECWECONL
,
153 2<<DECWECONL
, 2<<DECWECONL
, DECFLOAT_Inf
, DECFLOAT_NaN
};
157 const uInt DECCOMBMSD
[64]={
158 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
159 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0,
160 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7,
161 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 8, 9, 0, 0};
163 const uInt DECCOMBFROM
[48]={
164 0x00000000, 0x04000000, 0x08000000, 0x0C000000, 0x10000000, 0x14000000,
165 0x18000000, 0x1C000000, 0x60000000, 0x64000000, 0x00000000, 0x00000000,
166 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x20000000, 0x24000000,
167 0x28000000, 0x2C000000, 0x30000000, 0x34000000, 0x38000000, 0x3C000000,
168 0x68000000, 0x6C000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
169 0x00000000, 0x00000000, 0x40000000, 0x44000000, 0x48000000, 0x4C000000,
170 0x50000000, 0x54000000, 0x58000000, 0x5C000000, 0x70000000, 0x74000000,
171 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000};
173 /* ------------------------------------------------------------------ */
174 /* Request and include the tables to use for conversions */
175 /* ------------------------------------------------------------------ */
176 #define DEC_BCD2DPD 1 /* 0-0x999 -> DPD */
177 #define DEC_BIN2DPD 1 /* 0-999 -> DPD */
178 #define DEC_BIN2BCD8 1 /* 0-999 -> ddd, len */
179 #define DEC_DPD2BCD8 1 /* DPD -> ddd, len */
180 #define DEC_DPD2BIN 1 /* DPD -> 0-999 */
181 #define DEC_DPD2BINK 1 /* DPD -> 0-999000 */
182 #define DEC_DPD2BINM 1 /* DPD -> 0-999000000 */
183 #include "decDPD.h" /* source of the lookup tables */
187 /* ----------------------------------------------------------------- */
188 /* decBiStr -- compare string with pairwise options */
190 /* targ is the string to compare */
191 /* str1 is one of the strings to compare against (length may be 0) */
192 /* str2 is the other; it must be the same length as str1 */
194 /* returns 1 if strings compare equal, (that is, targ is the same */
195 /* length as str1 and str2, and each character of targ is in one */
196 /* of str1 or str2 in the corresponding position), or 0 otherwise */
198 /* This is used for generic caseless compare, including the awkward */
199 /* case of the Turkish dotted and dotless Is. Use as (for example): */
200 /* if (decBiStr(test, "mike", "MIKE")) ... */
201 /* ----------------------------------------------------------------- */
202 static Flag
decBiStr(const char *targ
, const char *str1
, const char *str2
) {
203 for (;;targ
++, str1
++, str2
++) {
204 if (*targ
!=*str1
&& *targ
!=*str2
) return 0;
205 /* *targ has a match in one (or both, if terminator) */
206 if (*targ
=='\0') break;
211 /* ------------------------------------------------------------------ */
212 /* decFinalize -- adjust and store a final result */
214 /* df is the decFloat format number which gets the final result */
215 /* num is the descriptor of the number to be checked and encoded */
216 /* [its values, including the coefficient, may be modified] */
217 /* set is the context to use */
220 /* The num descriptor may point to a bcd8 string of any length; this */
221 /* string may have leading insignificant zeros. If it has more than */
222 /* DECPMAX digits then the final digit can be a round-for-reround */
223 /* digit (i.e., it may include a sticky bit residue). */
225 /* The exponent (q) may be one of the codes for a special value and */
226 /* can be up to 999999999 for conversion from string. */
228 /* No error is possible, but Inexact, Underflow, and/or Overflow may */
230 /* ------------------------------------------------------------------ */
231 /* Constant whose size varies with format; also the check for surprises */
232 static uByte allnines
[DECPMAX
]=
234 {9, 9, 9, 9, 9, 9, 9};
236 {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
238 {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
239 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
242 static decFloat
* decFinalize(decFloat
*df
, bcdnum
*num
,
244 uByte
*ub
; /* work */
246 uInt uiwork
; /* for macros */
247 uByte
*umsd
=num
->msd
; /* local copy */
248 uByte
*ulsd
=num
->lsd
; /* .. */
249 uInt encode
; /* encoding accumulator */
250 Int length
; /* coefficient length */
253 Int clen
=ulsd
-umsd
+1;
255 #define COEXTRA 2 /* extra-long coefficent */
259 if (clen
<1 || clen
>DECPMAX
*3+2+COEXTRA
)
260 printf("decFinalize: suspect coefficient [length=%ld]\n", (LI
)clen
);
261 if (num
->sign
!=0 && num
->sign
!=DECFLOAT_Sign
)
262 printf("decFinalize: bad sign [%08lx]\n", (LI
)num
->sign
);
263 if (!EXPISSPECIAL(num
->exponent
)
264 && (num
->exponent
>1999999999 || num
->exponent
<-1999999999))
265 printf("decFinalize: improbable exponent [%ld]\n", (LI
)num
->exponent
);
266 /* decShowNum(num, "final"); */
269 /* A special will have an 'exponent' which is very positive and a */
270 /* coefficient < DECPMAX */
271 length
=(uInt
)(ulsd
-umsd
+1); /* coefficient length */
273 if (!NUMISSPECIAL(num
)) {
274 Int drop
; /* digits to be dropped */
275 /* skip leading insignificant zeros to calculate an exact length */
276 /* [this is quite expensive] */
278 for (; umsd
+3<ulsd
&& UBTOUI(umsd
)==0;) umsd
+=4;
279 for (; *umsd
==0 && umsd
<ulsd
;) umsd
++;
280 length
=ulsd
-umsd
+1; /* recalculate */
282 drop
=MAXI(length
-DECPMAX
, DECQTINY
-num
->exponent
);
283 /* drop can now be > digits for bottom-clamp (subnormal) cases */
284 if (drop
>0) { /* rounding needed */
285 /* (decFloatQuantize has very similar code to this, so any */
286 /* changes may need to be made there, too) */
287 uByte
*roundat
; /* -> re-round digit */
288 uByte reround
; /* reround value */
289 /* printf("Rounding; drop=%ld\n", (LI)drop); */
291 num
->exponent
+=drop
; /* always update exponent */
293 /* Three cases here: */
294 /* 1. new LSD is in coefficient (almost always) */
295 /* 2. new LSD is digit to left of coefficient (so MSD is */
296 /* round-for-reround digit) */
297 /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */
298 /* [duplicate check-stickies code to save a test] */
299 /* [by-digit check for stickies as runs of zeros are rare] */
300 if (drop
<length
) { /* NB lengths not addresses */
301 roundat
=umsd
+length
-drop
;
303 for (ub
=roundat
+1; ub
<=ulsd
; ub
++) {
304 if (*ub
!=0) { /* non-zero to be discarded */
305 reround
=DECSTICKYTAB
[reround
]; /* apply sticky bit */
306 break; /* [remainder don't-care] */
308 } /* check stickies */
309 ulsd
=roundat
-1; /* new LSD */
311 else { /* edge case */
320 for (ub
=roundat
+1; ub
<=ulsd
; ub
++) {
321 if (*ub
!=0) { /* non-zero to be discarded */
322 reround
=DECSTICKYTAB
[reround
]; /* apply sticky bit */
323 break; /* [remainder don't-care] */
325 } /* check stickies */
326 *umsd
=0; /* coefficient is a 0 */
330 if (reround
!=0) { /* discarding non-zero */
332 set
->status
|=DEC_Inexact
;
333 /* if adjusted exponent [exp+digits-1] is < EMIN then num is */
334 /* subnormal -- so raise Underflow */
335 if (num
->exponent
<DECEMIN
&& (num
->exponent
+(ulsd
-umsd
+1)-1)<DECEMIN
)
336 set
->status
|=DEC_Underflow
;
338 /* next decide whether increment of the coefficient is needed */
339 if (set
->round
==DEC_ROUND_HALF_EVEN
) { /* fastpath slowest case */
340 if (reround
>5) bump
=1; /* >0.5 goes up */
341 else if (reround
==5) /* exactly 0.5000 .. */
342 bump
=*ulsd
& 0x01; /* .. up iff [new] lsd is odd */
344 else switch (set
->round
) {
345 case DEC_ROUND_DOWN
: {
348 case DEC_ROUND_HALF_DOWN
: {
349 if (reround
>5) bump
=1;
351 case DEC_ROUND_HALF_UP
: {
352 if (reround
>=5) bump
=1;
355 if (reround
>0) bump
=1;
357 case DEC_ROUND_CEILING
: {
358 /* same as _UP for positive numbers, and as _DOWN for negatives */
359 if (!num
->sign
&& reround
>0) bump
=1;
361 case DEC_ROUND_FLOOR
: {
362 /* same as _UP for negative numbers, and as _DOWN for positive */
363 /* [negative reround cannot occur on 0] */
364 if (num
->sign
&& reround
>0) bump
=1;
366 case DEC_ROUND_05UP
: {
367 if (reround
>0) { /* anything out there is 'sticky' */
368 /* bump iff lsd=0 or 5; this cannot carry so it could be */
369 /* effected immediately with no bump -- but the code */
370 /* is clearer if this is done the same way as the others */
371 if (*ulsd
==0 || *ulsd
==5) bump
=1;
374 default: { /* e.g., DEC_ROUND_MAX */
375 set
->status
|=DEC_Invalid_context
;
377 printf("Unknown rounding mode: %ld\n", (LI
)set
->round
);
380 } /* switch (not r-h-e) */
381 /* printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump); */
383 if (bump
!=0) { /* need increment */
384 /* increment the coefficient; this might end up with 1000... */
385 /* (after the all nines case) */
387 for(; ub
-3>=umsd
&& UBTOUI(ub
-3)==0x09090909; ub
-=4) {
388 UBFROMUI(ub
-3, 0); /* to 00000000 */
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) UBFROMUI(t
, UBTOUI(s
));
477 for (t
=tlsd
-shift
+1; t
<=tlsd
; t
+=4) UBFROMUI(t
, 0); /* pad 0s */
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 /* fast path possible when DECPMAX digits */
494 if (length
==DECPMAX
) {
495 return decFloatFromBCD(df
, num
->exponent
, umsd
, num
->sign
);
498 /* slower path when not a full-length number; must care about length */
499 /* [coefficient length here will be < DECPMAX] */
500 if (!NUMISSPECIAL(num
)) { /* is still finite */
501 /* encode the combination field and exponent continuation */
502 uInt uexp
=(uInt
)(num
->exponent
+DECBIAS
); /* biased exponent */
503 uInt code
=(uexp
>>DECECONL
)<<4; /* top two bits of exp */
505 /* look up the combination field and make high word */
506 encode
=DECCOMBFROM
[code
]; /* indexed by (0-2)*16+msd */
507 encode
|=(uexp
<<(32-6-DECECONL
)) & 0x03ffffff; /* exponent continuation */
509 else encode
=num
->exponent
; /* special [already in word] */
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 getDPDt(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 getDPDt(dpd
, 4); encode
|=dpd
<<8;
537 getDPDt(dpd
, 3); encode
|=dpd
>>2;
538 DFWORD(df
, 0)=encode
;
540 getDPDt(dpd
, 2); encode
|=dpd
<<20;
541 getDPDt(dpd
, 1); encode
|=dpd
<<10;
542 getDPDt(dpd
, 0); encode
|=dpd
;
543 DFWORD(df
, 1)=encode
;
546 getDPDt(dpd
,10); encode
|=dpd
<<4;
547 getDPDt(dpd
, 9); encode
|=dpd
>>6;
548 DFWORD(df
, 0)=encode
;
551 getDPDt(dpd
, 8); encode
|=dpd
<<16;
552 getDPDt(dpd
, 7); encode
|=dpd
<<6;
553 getDPDt(dpd
, 6); encode
|=dpd
>>4;
554 DFWORD(df
, 1)=encode
;
557 getDPDt(dpd
, 5); encode
|=dpd
<<18;
558 getDPDt(dpd
, 4); encode
|=dpd
<<8;
559 getDPDt(dpd
, 3); encode
|=dpd
>>2;
560 DFWORD(df
, 2)=encode
;
563 getDPDt(dpd
, 2); encode
|=dpd
<<20;
564 getDPDt(dpd
, 1); encode
|=dpd
<<10;
565 getDPDt(dpd
, 0); encode
|=dpd
;
566 DFWORD(df
, 3)=encode
;
569 /* printf("Status: %08lx\n", (LI)set->status); */
570 /* decFloatShow(df, "final2"); */
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 are 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 getDPDb(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 getDPDb(dpd
, 4); encode
|=dpd
<<8;
625 getDPDb(dpd
, 3); encode
|=dpd
>>2;
626 DFWORD(df
, 0)=encode
;
628 getDPDb(dpd
, 2); encode
|=dpd
<<20;
629 getDPDb(dpd
, 1); encode
|=dpd
<<10;
630 getDPDb(dpd
, 0); encode
|=dpd
;
631 DFWORD(df
, 1)=encode
;
634 getDPDb(dpd
,10); encode
|=dpd
<<4;
635 getDPDb(dpd
, 9); encode
|=dpd
>>6;
636 DFWORD(df
, 0)=encode
;
639 getDPDb(dpd
, 8); encode
|=dpd
<<16;
640 getDPDb(dpd
, 7); encode
|=dpd
<<6;
641 getDPDb(dpd
, 6); encode
|=dpd
>>4;
642 DFWORD(df
, 1)=encode
;
645 getDPDb(dpd
, 5); encode
|=dpd
<<18;
646 getDPDb(dpd
, 4); encode
|=dpd
<<8;
647 getDPDb(dpd
, 3); encode
|=dpd
>>2;
648 DFWORD(df
, 2)=encode
;
651 getDPDb(dpd
, 2); encode
|=dpd
<<20;
652 getDPDb(dpd
, 1); encode
|=dpd
<<10;
653 getDPDb(dpd
, 0); encode
|=dpd
;
654 DFWORD(df
, 3)=encode
;
656 /* decFloatShow(df, "fromB"); */
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 /* decFloatFromPackedChecked -- set from exponent and packed; checked */
705 /* df is the target decFloat */
706 /* exp is the in-range unbiased exponent, q, or a special value in */
707 /* the form returned by decFloatGetExponent */
708 /* packed holds DECPMAX packed decimal digits plus a sign nibble */
709 /* (all 6 codes are OK); the first (MSD) must be 0 if df is a NaN */
710 /* and all digits must be 0 if df is infinite. For DOUBLE and */
711 /* QUAD the first (pad) nibble must be 0. */
712 /* All coefficient nibbles must be in 0-9 and sign in A-F. */
713 /* returns df, which will be canonical or NULL if any of the */
714 /* requirements are not met (if this case df is unchanged); that */
715 /* is, the input data must be as returned by decFloatToPacked, */
716 /* except that all six sign codes are acccepted. */
718 /* No status will be set. */
719 /* ------------------------------------------------------------------ */
720 decFloat
* decFloatFromPackedChecked(decFloat
*df
, Int exp
,
721 const uByte
*packed
) {
722 uByte bcdar
[DECPMAX
+2]; /* work [+1 for pad, +1 for sign] */
723 const uByte
*ip
; /* .. */
725 Int sig
=0; /* sign */
727 /* expand coefficient and sign to BCDAR */
729 op
=bcdar
+1; /* no pad digit */
731 op
=bcdar
; /* first (pad) digit here */
733 for (ip
=packed
; ip
<packed
+((DECPMAX
+2)/2); ip
++) {
735 if (*op
>9) return NULL
;
737 *op
=(uByte
)(*ip
&0x0f); /* [final nibble is sign] */
738 if (*op
>9 && ip
<packed
+((DECPMAX
+2)/2)-1) return NULL
;
741 op
--; /* -> sign byte */
742 if (*op
<=9) return NULL
; /* bad sign */
743 if (*op
==DECPMINUS
|| *op
==DECPMINUSALT
) sig
=DECFLOAT_Sign
;
746 if (bcdar
[0]!=0) return NULL
; /* bad pad nibble */
749 if (EXPISNAN(exp
)) { /* a NaN */
750 if (bcdar
[1]!=0) return NULL
; /* bad msd */
752 else if (EXPISINF(exp
)) { /* is infinite */
754 for (i
=0; i
<DECPMAX
; i
++) {
755 if (bcdar
[i
+1]!=0) return NULL
; /* should be all zeros */
759 /* check the exponent is in range */
760 if (exp
>DECEMAX
-DECPMAX
+1) return NULL
;
761 if (exp
<DECEMIN
-DECPMAX
+1) return NULL
;
763 return decFloatFromBCD(df
, exp
, bcdar
+1, sig
);
764 } /* decFloatFromPacked */
766 /* ------------------------------------------------------------------ */
767 /* decFloatFromString -- conversion from numeric string */
769 /* result is the decFloat format number which gets the result of */
771 /* *string is the character string which should contain a valid */
772 /* number (which may be a special value), \0-terminated */
773 /* If there are too many significant digits in the */
774 /* coefficient it will be rounded. */
775 /* set is the context */
778 /* The length of the coefficient and the size of the exponent are */
779 /* checked by this routine, so the correct error (Underflow or */
780 /* Overflow) can be reported or rounding applied, as necessary. */
782 /* There is no limit to the coefficient length for finite inputs; */
783 /* NaN payloads must be integers with no more than DECPMAX-1 digits. */
784 /* Exponents may have up to nine significant digits. */
786 /* If bad syntax is detected, the result will be a quiet NaN. */
787 /* ------------------------------------------------------------------ */
788 decFloat
* decFloatFromString(decFloat
*result
, const char *string
,
790 Int digits
; /* count of digits in coefficient */
791 const char *dotchar
=NULL
; /* where dot was found [NULL if none] */
792 const char *cfirst
=string
; /* -> first character of decimal part */
793 const char *c
; /* work */
795 uInt uiwork
; /* for macros */
796 bcdnum num
; /* collects data for finishing */
797 uInt error
=DEC_Conversion_syntax
; /* assume the worst */
798 uByte buffer
[ROUNDUP(DECSTRING
+11, 8)]; /* room for most coefficents, */
799 /* some common rounding, +3, & pad */
801 /* printf("FromString %s ...\n", string); */
804 for(;;) { /* once-only 'loop' */
805 num
.sign
=0; /* assume non-negative */
806 num
.msd
=buffer
; /* MSD is here always */
808 /* detect and validate the coefficient, including any leading, */
809 /* trailing, or embedded '.' */
810 /* [could test four-at-a-time here (saving 10% for decQuads), */
811 /* but that risks storage violation because the position of the */
812 /* terminator is unknown] */
813 for (c
=string
;; c
++) { /* -> input character */
814 if (((unsigned)(*c
-'0'))<=9) continue; /* '0' through '9' is good */
815 if (*c
=='\0') break; /* most common non-digit */
817 if (dotchar
!=NULL
) break; /* not first '.' */
818 dotchar
=c
; /* record offset into decimal part */
820 if (c
==string
) { /* first in string... */
821 if (*c
=='-') { /* valid - sign */
823 num
.sign
=DECFLOAT_Sign
;
825 if (*c
=='+') { /* valid + sign */
829 /* *c is not a digit, terminator, or a valid +, -, or '.' */
833 digits
=(uInt
)(c
-cfirst
); /* digits (+1 if a dot) */
835 if (digits
>0) { /* had digits and/or dot */
836 const char *clast
=c
-1; /* note last coefficient char position */
837 Int exp
=0; /* exponent accumulator */
838 if (*c
!='\0') { /* something follows the coefficient */
839 uInt edig
; /* unsigned work */
840 /* had some digits and more to come; expect E[+|-]nnn now */
841 const char *firstexp
; /* exponent first non-zero */
842 if (*c
!='E' && *c
!='e') break;
843 c
++; /* to (optional) sign */
844 if (*c
=='-' || *c
=='+') c
++; /* step over sign (c=clast+2) */
845 if (*c
=='\0') break; /* no digits! (e.g., '1.2E') */
846 for (; *c
=='0';) c
++; /* skip leading zeros [even last] */
847 firstexp
=c
; /* remember start [maybe '\0'] */
848 /* gather exponent digits */
849 edig
=(uInt
)*c
-(uInt
)'0';
850 if (edig
<=9) { /* [check not bad or terminator] */
851 exp
+=edig
; /* avoid initial X10 */
854 edig
=(uInt
)*c
-(uInt
)'0';
859 /* if not now on the '\0', *c must not be a digit */
862 /* (this next test must be after the syntax checks) */
863 /* if definitely more than the possible digits for format then */
864 /* the exponent may have wrapped, so simply set it to a certain */
865 /* over/underflow value */
866 if (c
>firstexp
+DECEMAXD
) exp
=DECEMAX
*2;
867 if (*(clast
+2)=='-') exp
=-exp
; /* was negative */
870 if (dotchar
!=NULL
) { /* had a '.' */
871 digits
--; /* remove from digits count */
872 if (digits
==0) break; /* was dot alone: bad syntax */
873 exp
-=(Int
)(clast
-dotchar
); /* adjust exponent */
874 /* [the '.' can now be ignored] */
876 num
.exponent
=exp
; /* exponent is good; store it */
878 /* Here when whole string has been inspected and syntax is good */
879 /* cfirst->first digit or dot, clast->last digit or dot */
880 error
=0; /* no error possible now */
882 /* if the number of digits in the coefficient will fit in buffer */
883 /* then it can simply be converted to bcd8 and copied -- decFinalize */
884 /* will take care of leading zeros and rounding; the buffer is big */
885 /* enough for all canonical coefficients, including 0.00000nn... */
887 if (digits
<=(Int
)(sizeof(buffer
)-3)) { /* [-3 allows by-4s copy] */
889 if (dotchar
!=NULL
) { /* a dot to worry about */
890 if (*(c
+1)=='.') { /* common canonical case */
891 *ub
++=(uByte
)(*c
-'0'); /* copy leading digit */
892 c
+=2; /* prepare to handle rest */
894 else for (; c
<=clast
;) { /* '.' could be anywhere */
895 /* as usual, go by fours when safe; NB it has been asserted */
896 /* that a '.' does not have the same mask as a digit */
897 if (c
<=clast
-3 /* safe for four */
898 && (UBTOUI(c
)&0xf0f0f0f0)==CHARMASK
) { /* test four */
899 UBFROMUI(ub
, UBTOUI(c
)&0x0f0f0f0f); /* to BCD8 */
904 if (*c
=='.') { /* found the dot */
905 c
++; /* step over it .. */
906 break; /* .. and handle the rest */
908 *ub
++=(uByte
)(*c
++-'0');
911 /* Now no dot; do this by fours (where safe) */
912 for (; c
<=clast
-3; c
+=4, ub
+=4) UBFROMUI(ub
, UBTOUI(c
)&0x0f0f0f0f);
913 for (; c
<=clast
; c
++, ub
++) *ub
=(uByte
)(*c
-'0');
914 num
.lsd
=buffer
+digits
-1; /* record new LSD */
917 else { /* too long for buffer */
918 /* [This is a rare and unusual case; arbitrary-length input] */
919 /* strip leading zeros [but leave final 0 if all 0's] */
920 if (*cfirst
=='.') cfirst
++; /* step past dot at start */
921 if (*cfirst
=='0') { /* [cfirst always -> digit] */
922 for (; cfirst
<clast
; cfirst
++) {
923 if (*cfirst
!='0') { /* non-zero found */
924 if (*cfirst
=='.') continue; /* [ignore] */
927 digits
--; /* 0 stripped */
929 } /* at least one leading 0 */
931 /* the coefficient is now as short as possible, but may still */
932 /* be too long; copy up to Pmax+1 digits to the buffer, then */
933 /* just record any non-zeros (set round-for-reround digit) */
934 for (c
=cfirst
; c
<=clast
&& ub
<=buffer
+DECPMAX
; c
++) {
935 /* (see commentary just above) */
936 if (c
<=clast
-3 /* safe for four */
937 && (UBTOUI(c
)&0xf0f0f0f0)==CHARMASK
) { /* four digits */
938 UBFROMUI(ub
, UBTOUI(c
)&0x0f0f0f0f); /* to BCD8 */
940 c
+=3; /* [will become 4] */
943 if (*c
=='.') continue; /* [ignore] */
944 *ub
++=(uByte
)(*c
-'0');
947 for (; c
<=clast
; c
++) { /* inspect remaining chars */
948 if (*c
!='0') { /* sticky bit needed */
949 if (*c
=='.') continue; /* [ignore] */
950 *ub
=DECSTICKYTAB
[*ub
]; /* update round-for-reround */
951 break; /* no need to look at more */
954 num
.lsd
=ub
; /* record LSD */
955 /* adjust exponent for dropped digits */
956 num
.exponent
+=digits
-(Int
)(ub
-buffer
+1);
957 } /* too long for buffer */
958 } /* digits or dot */
960 else { /* no digits or dot were found */
961 if (*c
=='\0') break; /* nothing to come is bad */
962 /* only Infinities and NaNs are allowed, here */
963 buffer
[0]=0; /* default a coefficient of 0 */
964 num
.lsd
=buffer
; /* .. */
965 if (decBiStr(c
, "infinity", "INFINITY")
966 || decBiStr(c
, "inf", "INF")) num
.exponent
=DECFLOAT_Inf
;
967 else { /* should be a NaN */
968 num
.exponent
=DECFLOAT_qNaN
; /* assume quiet NaN */
969 if (*c
=='s' || *c
=='S') { /* probably an sNaN */
971 num
.exponent
=DECFLOAT_sNaN
; /* assume is in fact sNaN */
973 if (*c
!='N' && *c
!='n') break; /* check caseless "NaN" */
975 if (*c
!='a' && *c
!='A') break; /* .. */
977 if (*c
!='N' && *c
!='n') break; /* .. */
979 /* now either nothing, or nnnn payload (no dots), expected */
980 /* -> start of integer, and skip leading 0s [including plain 0] */
981 for (cfirst
=c
; *cfirst
=='0';) cfirst
++;
982 if (*cfirst
!='\0') { /* not empty or all-0, payload */
983 /* payload found; check all valid digits and copy to buffer as bcd8 */
985 for (c
=cfirst
;; c
++, ub
++) {
986 if ((unsigned)(*c
-'0')>9) break; /* quit if not 0-9 */
987 if (c
-cfirst
==DECPMAX
-1) break; /* too many digits */
988 *ub
=(uByte
)(*c
-'0'); /* good bcd8 */
990 if (*c
!='\0') break; /* not all digits, or too many */
991 num
.lsd
=ub
-1; /* record new LSD */
994 error
=0; /* syntax is OK */
995 break; /* done with specials */
996 } /* digits=0 (special expected) */
998 } /* [for(;;) break] */
1000 /* decShowNum(&num, "fromStr"); */
1004 num
.exponent
=DECFLOAT_qNaN
; /* set up quiet NaN */
1005 num
.sign
=0; /* .. with 0 sign */
1006 buffer
[0]=0; /* .. and coefficient */
1007 num
.lsd
=buffer
; /* .. */
1008 /* decShowNum(&num, "oops"); */
1011 /* decShowNum(&num, "dffs"); */
1012 decFinalize(result
, &num
, set
); /* round, check, and lay out */
1013 /* decFloatShow(result, "fromString"); */
1015 } /* decFloatFromString */
1017 /* ------------------------------------------------------------------ */
1018 /* decFloatFromWider -- conversion from next-wider format */
1020 /* result is the decFloat format number which gets the result of */
1021 /* the conversion */
1022 /* wider is the decFloatWider format number which will be narrowed */
1023 /* set is the context */
1024 /* returns result */
1026 /* Narrowing can cause rounding, overflow, etc., but not Invalid */
1027 /* operation (sNaNs are copied and do not signal). */
1028 /* ------------------------------------------------------------------ */
1029 /* narrow-to is not possible for decQuad format numbers; simply omit */
1031 decFloat
* decFloatFromWider(decFloat
*result
, const decFloatWider
*wider
,
1033 bcdnum num
; /* collects data for finishing */
1034 uByte bcdar
[DECWPMAX
]; /* room for wider coefficient */
1035 uInt widerhi
=DFWWORD(wider
, 0); /* top word */
1038 GETWCOEFF(wider
, bcdar
);
1040 num
.msd
=bcdar
; /* MSD is here always */
1041 num
.lsd
=bcdar
+DECWPMAX
-1; /* LSD is here always */
1042 num
.sign
=widerhi
&0x80000000; /* extract sign [DECFLOAT_Sign=Neg] */
1044 /* decode the wider combination field to exponent */
1045 exp
=DECCOMBWEXP
[widerhi
>>26]; /* decode from wider combination field */
1046 /* if it is a special there's nothing to do unless sNaN; if it's */
1047 /* finite then add the (wider) exponent continuation and unbias */
1048 if (EXPISSPECIAL(exp
)) exp
=widerhi
&0x7e000000; /* include sNaN selector */
1049 else exp
+=GETWECON(wider
)-DECWBIAS
;
1052 /* decShowNum(&num, "dffw"); */
1053 return decFinalize(result
, &num
, set
);/* round, check, and lay out */
1054 } /* decFloatFromWider */
1057 /* ------------------------------------------------------------------ */
1058 /* decFloatGetCoefficient -- get coefficient as BCD8 */
1060 /* df is the decFloat from which to extract the coefficient */
1061 /* bcdar is where DECPMAX bytes will be written, one BCD digit in */
1062 /* each byte (BCD8 encoding); if df is a NaN the first byte will */
1063 /* be zero, and if it is infinite they will all be zero */
1064 /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
1067 /* No error is possible, and no status will be set. If df is a */
1068 /* special value the array is set to zeros (for Infinity) or to the */
1069 /* payload of a qNaN or sNaN. */
1070 /* ------------------------------------------------------------------ */
1071 Int
decFloatGetCoefficient(const decFloat
*df
, uByte
*bcdar
) {
1072 if (DFISINF(df
)) memset(bcdar
, 0, DECPMAX
);
1074 GETCOEFF(df
, bcdar
); /* use macro */
1075 if (DFISNAN(df
)) bcdar
[0]=0; /* MSD needs correcting */
1077 return DFISSIGNED(df
);
1078 } /* decFloatGetCoefficient */
1080 /* ------------------------------------------------------------------ */
1081 /* decFloatGetExponent -- get unbiased exponent */
1083 /* df is the decFloat from which to extract the exponent */
1084 /* returns the exponent, q. */
1086 /* No error is possible, and no status will be set. If df is a */
1087 /* special value the first seven bits of the decFloat are returned, */
1088 /* left adjusted and with the first (sign) bit set to 0 (followed by */
1089 /* 25 0 bits). e.g., -sNaN would return 0x7e000000 (DECFLOAT_sNaN). */
1090 /* ------------------------------------------------------------------ */
1091 Int
decFloatGetExponent(const decFloat
*df
) {
1092 if (DFISSPECIAL(df
)) return DFWORD(df
, 0)&0x7e000000;
1093 return GETEXPUN(df
);
1094 } /* decFloatGetExponent */
1096 /* ------------------------------------------------------------------ */
1097 /* decFloatSetCoefficient -- set coefficient from BCD8 */
1099 /* df is the target decFloat (and source of exponent/special value) */
1100 /* bcdar holds DECPMAX digits to set the coefficient from, one */
1101 /* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
1102 /* if df is a NaN; all are ignored if df is infinite. */
1103 /* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
1104 /* returns df, which will be canonical */
1106 /* No error is possible, and no status will be set. */
1107 /* ------------------------------------------------------------------ */
1108 decFloat
* decFloatSetCoefficient(decFloat
*df
, const uByte
*bcdar
,
1110 uInt exp
; /* for exponent */
1111 uByte bcdzero
[DECPMAX
]; /* for infinities */
1113 /* Exponent/special code is extracted from df */
1114 if (DFISSPECIAL(df
)) {
1115 exp
=DFWORD(df
, 0)&0x7e000000;
1117 memset(bcdzero
, 0, DECPMAX
);
1118 return decFloatFromBCD(df
, exp
, bcdzero
, sig
);
1121 else exp
=GETEXPUN(df
);
1122 return decFloatFromBCD(df
, exp
, bcdar
, sig
);
1123 } /* decFloatSetCoefficient */
1125 /* ------------------------------------------------------------------ */
1126 /* decFloatSetExponent -- set exponent or special value */
1128 /* df is the target decFloat (and source of coefficient/payload) */
1129 /* set is the context for reporting status */
1130 /* exp is the unbiased exponent, q, or a special value in the form */
1131 /* returned by decFloatGetExponent */
1132 /* returns df, which will be canonical */
1134 /* No error is possible, but Overflow or Underflow might occur. */
1135 /* ------------------------------------------------------------------ */
1136 decFloat
* decFloatSetExponent(decFloat
*df
, decContext
*set
, Int exp
) {
1137 uByte bcdcopy
[DECPMAX
]; /* for coefficient */
1138 bcdnum num
; /* work */
1140 num
.sign
=decFloatGetCoefficient(df
, bcdcopy
); /* extract coefficient */
1141 if (DFISSPECIAL(df
)) { /* MSD or more needs correcting */
1142 if (DFISINF(df
)) memset(bcdcopy
, 0, DECPMAX
);
1146 num
.lsd
=bcdcopy
+DECPMAX
-1;
1147 return decFinalize(df
, &num
, set
);
1148 } /* decFloatSetExponent */
1150 /* ------------------------------------------------------------------ */
1151 /* decFloatRadix -- returns the base (10) */
1153 /* df is any decFloat of this format */
1154 /* ------------------------------------------------------------------ */
1155 uInt
decFloatRadix(const decFloat
*df
) {
1156 if (df
) return 10; /* to placate compiler */
1158 } /* decFloatRadix */
1160 /* ------------------------------------------------------------------ */
1161 /* decFloatShow -- printf a decFloat in hexadecimal and decimal */
1162 /* df is the decFloat to show */
1163 /* tag is a tag string displayed with the number */
1165 /* This is a debug aid; the precise format of the string may change. */
1166 /* ------------------------------------------------------------------ */
1167 void decFloatShow(const decFloat
*df
, const char *tag
) {
1168 char hexbuf
[DECBYTES
*2+DECBYTES
/4+1]; /* NB blank after every fourth */
1169 char buff
[DECSTRING
]; /* for value in decimal */
1172 for (i
=0; i
<DECBYTES
; i
++) {
1174 sprintf(&hexbuf
[j
], "%02x", df
->bytes
[DECBYTES
-1-i
]);
1176 sprintf(&hexbuf
[j
], "%02x", df
->bytes
[i
]);
1179 /* the next line adds blank (and terminator) after final pair, too */
1180 if ((i
+1)%4==0) {strcpy(&hexbuf
[j
], " "); j
++;}
1182 decFloatToString(df
, buff
);
1183 printf(">%s> %s [big-endian] %s\n", tag
, hexbuf
, buff
);
1185 } /* decFloatShow */
1187 /* ------------------------------------------------------------------ */
1188 /* decFloatToBCD -- get sign, exponent, and BCD8 from a decFloat */
1190 /* df is the source decFloat */
1191 /* exp will be set to the unbiased exponent, q, or to a special */
1192 /* value in the form returned by decFloatGetExponent */
1193 /* bcdar is where DECPMAX bytes will be written, one BCD digit in */
1194 /* each byte (BCD8 encoding); if df is a NaN the first byte will */
1195 /* be zero, and if it is infinite they will all be zero */
1196 /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
1199 /* No error is possible, and no status will be set. */
1200 /* ------------------------------------------------------------------ */
1201 Int
decFloatToBCD(const decFloat
*df
, Int
*exp
, uByte
*bcdar
) {
1203 memset(bcdar
, 0, DECPMAX
);
1204 *exp
=DFWORD(df
, 0)&0x7e000000;
1207 GETCOEFF(df
, bcdar
); /* use macro */
1209 bcdar
[0]=0; /* MSD needs correcting */
1210 *exp
=DFWORD(df
, 0)&0x7e000000;
1216 return DFISSIGNED(df
);
1217 } /* decFloatToBCD */
1219 /* ------------------------------------------------------------------ */
1220 /* decFloatToEngString -- conversion to numeric string, engineering */
1222 /* df is the decFloat format number to convert */
1223 /* string is the string where the result will be laid out */
1225 /* string must be at least DECPMAX+9 characters (the worst case is */
1226 /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
1227 /* DECEMAXD<=4); this condition is asserted above */
1229 /* No error is possible, and no status will be set */
1230 /* ------------------------------------------------------------------ */
1231 char * decFloatToEngString(const decFloat
*df
, char *string
){
1232 uInt msd
; /* coefficient MSD */
1233 Int exp
; /* exponent top two bits or full */
1234 uInt comb
; /* combination field */
1235 char *cstart
; /* coefficient start */
1236 char *c
; /* output pointer in string */
1237 char *s
, *t
; /* .. (source, target) */
1238 Int pre
, e
; /* work */
1239 const uByte
*u
; /* .. */
1240 uInt uiwork
; /* for macros [one compiler needs */
1241 /* volatile here to avoid bug, but */
1242 /* that doubles execution time] */
1244 /* Source words; macro handles endianness */
1245 uInt sourhi
=DFWORD(df
, 0); /* word with sign */
1247 uInt sourlo
=DFWORD(df
, 1);
1249 uInt sourmh
=DFWORD(df
, 1);
1250 uInt sourml
=DFWORD(df
, 2);
1251 uInt sourlo
=DFWORD(df
, 3);
1254 c
=string
; /* where result will go */
1255 if (((Int
)sourhi
)<0) *c
++='-'; /* handle sign */
1256 comb
=sourhi
>>26; /* sign+combination field */
1257 msd
=DECCOMBMSD
[comb
]; /* decode the combination field */
1258 exp
=DECCOMBEXP
[comb
]; /* .. */
1260 if (EXPISSPECIAL(exp
)) { /* special */
1261 if (exp
==DECFLOAT_Inf
) { /* infinity */
1263 strcpy(c
+3, "inity");
1264 return string
; /* easy */
1266 if (sourhi
&0x02000000) *c
++='s'; /* sNaN */
1267 strcpy(c
, "NaN"); /* complete word */
1268 c
+=3; /* step past */
1269 /* quick exit if the payload is zero */
1271 if ((sourhi
&0x000fffff)==0) return string
;
1273 if (sourlo
==0 && (sourhi
&0x0003ffff)==0) return string
;
1275 if (sourlo
==0 && sourml
==0 && sourmh
==0
1276 && (sourhi
&0x00003fff)==0) return string
;
1278 /* otherwise drop through to add integer; set correct exp etc. */
1279 exp
=0; msd
=0; /* setup for following code */
1281 else { /* complete exponent; top two bits are in place */
1282 exp
+=GETECON(df
)-DECBIAS
; /* .. + continuation and unbias */
1285 /* convert the digits of the significand to characters */
1286 cstart
=c
; /* save start of coefficient */
1287 if (msd
) *c
++=(char)('0'+(char)msd
); /* non-zero most significant digit */
1289 /* Decode the declets. After extracting each declet, it is */
1290 /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
1291 /* are the three encoded BCD8 digits followed by a 1-byte length */
1292 /* (significant digits, except that 000 has length 0). This allows */
1293 /* us to left-align the first declet with non-zero content, then */
1294 /* the remaining ones are full 3-char length. Fixed-length copies */
1295 /* are used because variable-length memcpy causes a subroutine call */
1296 /* in at least two compilers. (The copies are length 4 for speed */
1297 /* and are safe because the last item in the array is of length */
1298 /* three and has the length byte following.) */
1299 #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
1300 if (c!=cstart) {UBFROMUI(c, UBTOUI(u)|CHARMASK); c+=3;} \
1301 else if (*(u+3)) { \
1302 UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); c+=*(u+3);}
1305 dpd2char(sourhi
>>10); /* declet 1 */
1306 dpd2char(sourhi
); /* declet 2 */
1309 dpd2char(sourhi
>>8); /* declet 1 */
1310 dpd2char((sourhi
<<2) | (sourlo
>>30)); /* declet 2 */
1311 dpd2char(sourlo
>>20); /* declet 3 */
1312 dpd2char(sourlo
>>10); /* declet 4 */
1313 dpd2char(sourlo
); /* declet 5 */
1316 dpd2char(sourhi
>>4); /* declet 1 */
1317 dpd2char((sourhi
<<6) | (sourmh
>>26)); /* declet 2 */
1318 dpd2char(sourmh
>>16); /* declet 3 */
1319 dpd2char(sourmh
>>6); /* declet 4 */
1320 dpd2char((sourmh
<<4) | (sourml
>>28)); /* declet 5 */
1321 dpd2char(sourml
>>18); /* declet 6 */
1322 dpd2char(sourml
>>8); /* declet 7 */
1323 dpd2char((sourml
<<2) | (sourlo
>>30)); /* declet 8 */
1324 dpd2char(sourlo
>>20); /* declet 9 */
1325 dpd2char(sourlo
>>10); /* declet 10 */
1326 dpd2char(sourlo
); /* declet 11 */
1329 if (c
==cstart
) *c
++='0'; /* all zeros, empty -- make "0" */
1331 if (exp
==0) { /* integer or NaN case -- easy */
1332 *c
='\0'; /* terminate */
1335 /* non-0 exponent */
1337 e
=0; /* assume no E */
1338 pre
=(Int
)(c
-cstart
)+exp
; /* length+exp [c->LSD+1] */
1339 /* [here, pre-exp is the digits count (==1 for zero)] */
1341 if (exp
>0 || pre
<-5) { /* need exponential form */
1342 e
=pre
-1; /* calculate E value */
1343 pre
=1; /* assume one digit before '.' */
1344 if (e
!=0) { /* engineering: may need to adjust */
1345 Int adj
; /* adjustment */
1346 /* The C remainder operator is undefined for negative numbers, so */
1347 /* a positive remainder calculation must be used here */
1350 if (adj
!=0) adj
=3-adj
;
1356 /* if dealing with zero still produce an exponent which is a */
1357 /* multiple of three, as expected, but there will only be the */
1358 /* one zero before the E, still. Otherwise note the padding. */
1359 if (!DFISZERO(df
)) pre
+=adj
;
1360 else { /* is zero */
1361 if (adj
!=0) { /* 0.00Esnn needed */
1366 } /* engineering adjustment */
1367 } /* exponential form */
1368 /* printf("e=%ld pre=%ld exp=%ld\n", (LI)e, (LI)pre, (LI)exp); */
1370 /* modify the coefficient, adding 0s, '.', and E+nn as needed */
1371 if (pre
>0) { /* ddd.ddd (plain), perhaps with E */
1372 /* or dd00 padding for engineering */
1373 char *dotat
=cstart
+pre
;
1374 if (dotat
<c
) { /* if embedded dot needed... */
1375 /* move by fours; there must be space for junk at the end */
1376 /* because there is still space for exponent */
1377 s
=dotat
+ROUNDDOWN4(c
-dotat
); /* source */
1379 /* open the gap [cannot use memcpy] */
1380 for (; s
>=dotat
; s
-=4, t
-=4) UBFROMUI(t
, UBTOUI(s
));
1382 c
++; /* length increased by one */
1384 else for (; c
<dotat
; c
++) *c
='0'; /* pad for engineering */
1387 /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (may have
1388 E, but only for 0.00E+3 kind of case -- with plenty of spare
1389 space in this case */
1390 pre
=-pre
+2; /* gap width, including "0." */
1391 t
=cstart
+ROUNDDOWN4(c
-cstart
)+pre
; /* preferred first target point */
1392 /* backoff if too far to the right */
1393 if (t
>string
+DECSTRING
-5) t
=string
+DECSTRING
-5; /* adjust to fit */
1394 /* now shift the entire coefficient to the right, being careful not */
1395 /* to access to the left of string [cannot use memcpy] */
1396 for (s
=t
-pre
; s
>=string
; s
-=4, t
-=4) UBFROMUI(t
, UBTOUI(s
));
1397 /* for Quads and Singles there may be a character or two left... */
1398 s
+=3; /* where next would come from */
1399 for(; s
>=cstart
; s
--, t
--) *(t
+3)=*(s
);
1400 /* now have fill 0. through 0.00000; use overlaps to avoid tests */
1402 memcpy(cstart
+pre
-4, "0000", 4);
1403 memcpy(cstart
, "0.00", 4);
1406 *(cstart
+pre
-1)='0';
1407 memcpy(cstart
, "0.", 2);
1409 c
+=pre
; /* to end */
1412 /* finally add the E-part, if needed; it will never be 0, and has */
1413 /* a maximum length of 3 or 4 digits (asserted above) */
1415 memcpy(c
, "E+", 2); /* starts with E, assume + */
1418 *c
='-'; /* oops, need '-' */
1419 e
=-e
; /* uInt, please */
1422 /* Three-character exponents are easy; 4-character a little trickier */
1424 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1425 /* copy fixed 4 characters [is safe], starting at non-zero */
1426 /* and with character mask to convert BCD to char */
1427 UBFROMUI(c
, UBTOUI(u
+3-*(u
+3))|CHARMASK
);
1428 c
+=*(u
+3); /* bump pointer appropriately */
1430 if (e
<1000) { /* 3 (or fewer) digits case */
1431 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1432 UBFROMUI(c
, UBTOUI(u
+3-*(u
+3))|CHARMASK
); /* [as above] */
1433 c
+=*(u
+3); /* bump pointer appropriately */
1435 else { /* 4-digits */
1436 Int thou
=((e
>>3)*1049)>>17; /* e/1000 */
1437 Int rem
=e
-(1000*thou
); /* e%1000 */
1438 *c
++=(char)('0'+(char)thou
); /* the thousands digit */
1439 u
=&BIN2BCD8
[rem
*4]; /* -> 3 digits + length byte */
1440 UBFROMUI(c
, UBTOUI(u
)|CHARMASK
);/* copy fixed 3+1 characters [is safe] */
1441 c
+=3; /* bump pointer, always 3 digits */
1445 *c
='\0'; /* terminate */
1446 /*printf("res %s\n", string); */
1448 } /* decFloatToEngString */
1450 /* ------------------------------------------------------------------ */
1451 /* decFloatToPacked -- convert decFloat to Packed decimal + exponent */
1453 /* df is the source decFloat */
1454 /* exp will be set to the unbiased exponent, q, or to a special */
1455 /* value in the form returned by decFloatGetExponent */
1456 /* packed is where DECPMAX nibbles will be written with the sign as */
1457 /* final nibble (0x0c for +, 0x0d for -); a NaN has a first nibble */
1458 /* of zero, and an infinity is all zeros. decDouble and decQuad */
1459 /* have a additional leading zero nibble, leading to result */
1460 /* lengths of 4, 9, and 18 bytes. */
1461 /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
1464 /* No error is possible, and no status will be set. */
1465 /* ------------------------------------------------------------------ */
1466 Int
decFloatToPacked(const decFloat
*df
, Int
*exp
, uByte
*packed
) {
1467 uByte bcdar
[DECPMAX
+2]; /* work buffer */
1468 uByte
*ip
=bcdar
, *op
=packed
; /* work pointers */
1470 memset(bcdar
, 0, DECPMAX
+2);
1474 GETCOEFF(df
, bcdar
+1); /* use macro */
1476 bcdar
[1]=0; /* MSD needs clearing */
1477 *exp
=DFWORD(df
, 0)&0x7e000000;
1483 /* now pack; coefficient currently at bcdar+1 */
1485 ip
++; /* ignore first byte */
1487 *ip
=0; /* need leading zero */
1489 /* set final byte to Packed BCD sign value */
1490 bcdar
[DECPMAX
+1]=(DFISSIGNED(df
) ? DECPMINUS
: DECPPLUS
);
1491 /* pack an even number of bytes... */
1492 for (; op
<packed
+((DECPMAX
+2)/2); op
++, ip
+=2) {
1493 *op
=(uByte
)((*ip
<<4)+*(ip
+1));
1495 return (bcdar
[DECPMAX
+1]==DECPMINUS
? DECFLOAT_Sign
: 0);
1496 } /* decFloatToPacked */
1498 /* ------------------------------------------------------------------ */
1499 /* decFloatToString -- conversion to numeric string */
1501 /* df is the decFloat format number to convert */
1502 /* string is the string where the result will be laid out */
1504 /* string must be at least DECPMAX+9 characters (the worst case is */
1505 /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
1506 /* DECEMAXD<=4); this condition is asserted above */
1508 /* No error is possible, and no status will be set */
1509 /* ------------------------------------------------------------------ */
1510 char * decFloatToString(const decFloat
*df
, char *string
){
1511 uInt msd
; /* coefficient MSD */
1512 Int exp
; /* exponent top two bits or full */
1513 uInt comb
; /* combination field */
1514 char *cstart
; /* coefficient start */
1515 char *c
; /* output pointer in string */
1516 char *s
, *t
; /* .. (source, target) */
1517 Int pre
, e
; /* work */
1518 const uByte
*u
; /* .. */
1519 uInt uiwork
; /* for macros [one compiler needs */
1520 /* volatile here to avoid bug, but */
1521 /* that doubles execution time] */
1523 /* Source words; macro handles endianness */
1524 uInt sourhi
=DFWORD(df
, 0); /* word with sign */
1526 uInt sourlo
=DFWORD(df
, 1);
1528 uInt sourmh
=DFWORD(df
, 1);
1529 uInt sourml
=DFWORD(df
, 2);
1530 uInt sourlo
=DFWORD(df
, 3);
1533 c
=string
; /* where result will go */
1534 if (((Int
)sourhi
)<0) *c
++='-'; /* handle sign */
1535 comb
=sourhi
>>26; /* sign+combination field */
1536 msd
=DECCOMBMSD
[comb
]; /* decode the combination field */
1537 exp
=DECCOMBEXP
[comb
]; /* .. */
1539 if (!EXPISSPECIAL(exp
)) { /* finite */
1540 /* complete exponent; top two bits are in place */
1541 exp
+=GETECON(df
)-DECBIAS
; /* .. + continuation and unbias */
1543 else { /* IS special */
1544 if (exp
==DECFLOAT_Inf
) { /* infinity */
1545 strcpy(c
, "Infinity");
1546 return string
; /* easy */
1548 if (sourhi
&0x02000000) *c
++='s'; /* sNaN */
1549 strcpy(c
, "NaN"); /* complete word */
1550 c
+=3; /* step past */
1551 /* quick exit if the payload is zero */
1553 if ((sourhi
&0x000fffff)==0) return string
;
1555 if (sourlo
==0 && (sourhi
&0x0003ffff)==0) return string
;
1557 if (sourlo
==0 && sourml
==0 && sourmh
==0
1558 && (sourhi
&0x00003fff)==0) return string
;
1560 /* otherwise drop through to add integer; set correct exp etc. */
1561 exp
=0; msd
=0; /* setup for following code */
1564 /* convert the digits of the significand to characters */
1565 cstart
=c
; /* save start of coefficient */
1566 if (msd
) *c
++=(char)('0'+(char)msd
); /* non-zero most significant digit */
1568 /* Decode the declets. After extracting each declet, it is */
1569 /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
1570 /* are the three encoded BCD8 digits followed by a 1-byte length */
1571 /* (significant digits, except that 000 has length 0). This allows */
1572 /* us to left-align the first declet with non-zero content, then */
1573 /* the remaining ones are full 3-char length. Fixed-length copies */
1574 /* are used because variable-length memcpy causes a subroutine call */
1575 /* in at least two compilers. (The copies are length 4 for speed */
1576 /* and are safe because the last item in the array is of length */
1577 /* three and has the length byte following.) */
1578 #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
1579 if (c!=cstart) {UBFROMUI(c, UBTOUI(u)|CHARMASK); c+=3;} \
1580 else if (*(u+3)) { \
1581 UBFROMUI(c, UBTOUI(u+3-*(u+3))|CHARMASK); c+=*(u+3);}
1584 dpd2char(sourhi
>>10); /* declet 1 */
1585 dpd2char(sourhi
); /* declet 2 */
1588 dpd2char(sourhi
>>8); /* declet 1 */
1589 dpd2char((sourhi
<<2) | (sourlo
>>30)); /* declet 2 */
1590 dpd2char(sourlo
>>20); /* declet 3 */
1591 dpd2char(sourlo
>>10); /* declet 4 */
1592 dpd2char(sourlo
); /* declet 5 */
1595 dpd2char(sourhi
>>4); /* declet 1 */
1596 dpd2char((sourhi
<<6) | (sourmh
>>26)); /* declet 2 */
1597 dpd2char(sourmh
>>16); /* declet 3 */
1598 dpd2char(sourmh
>>6); /* declet 4 */
1599 dpd2char((sourmh
<<4) | (sourml
>>28)); /* declet 5 */
1600 dpd2char(sourml
>>18); /* declet 6 */
1601 dpd2char(sourml
>>8); /* declet 7 */
1602 dpd2char((sourml
<<2) | (sourlo
>>30)); /* declet 8 */
1603 dpd2char(sourlo
>>20); /* declet 9 */
1604 dpd2char(sourlo
>>10); /* declet 10 */
1605 dpd2char(sourlo
); /* declet 11 */
1608 if (c
==cstart
) *c
++='0'; /* all zeros, empty -- make "0" */
1610 /*[This fast path is valid but adds 3-5 cycles to worst case length] */
1611 /*if (exp==0) { // integer or NaN case -- easy */
1612 /* *c='\0'; // terminate */
1613 /* return string; */
1616 e
=0; /* assume no E */
1617 pre
=(Int
)(c
-cstart
)+exp
; /* length+exp [c->LSD+1] */
1618 /* [here, pre-exp is the digits count (==1 for zero)] */
1620 if (exp
>0 || pre
<-5) { /* need exponential form */
1621 e
=pre
-1; /* calculate E value */
1622 pre
=1; /* assume one digit before '.' */
1623 } /* exponential form */
1625 /* modify the coefficient, adding 0s, '.', and E+nn as needed */
1626 if (pre
>0) { /* ddd.ddd (plain), perhaps with E */
1627 char *dotat
=cstart
+pre
;
1628 if (dotat
<c
) { /* if embedded dot needed... */
1629 /* [memmove is a disaster, here] */
1630 /* move by fours; there must be space for junk at the end */
1631 /* because exponent is still possible */
1632 s
=dotat
+ROUNDDOWN4(c
-dotat
); /* source */
1634 /* open the gap [cannot use memcpy] */
1635 for (; s
>=dotat
; s
-=4, t
-=4) UBFROMUI(t
, UBTOUI(s
));
1637 c
++; /* length increased by one */
1640 /* finally add the E-part, if needed; it will never be 0, and has */
1641 /* a maximum length of 3 or 4 digits (asserted above) */
1643 memcpy(c
, "E+", 2); /* starts with E, assume + */
1646 *c
='-'; /* oops, need '-' */
1647 e
=-e
; /* uInt, please */
1650 /* Three-character exponents are easy; 4-character a little trickier */
1652 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1653 /* copy fixed 4 characters [is safe], starting at non-zero */
1654 /* and with character mask to convert BCD to char */
1655 UBFROMUI(c
, UBTOUI(u
+3-*(u
+3))|CHARMASK
);
1656 c
+=*(u
+3); /* bump pointer appropriately */
1658 if (e
<1000) { /* 3 (or fewer) digits case */
1659 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1660 UBFROMUI(c
, UBTOUI(u
+3-*(u
+3))|CHARMASK
); /* [as above] */
1661 c
+=*(u
+3); /* bump pointer appropriately */
1663 else { /* 4-digits */
1664 Int thou
=((e
>>3)*1049)>>17; /* e/1000 */
1665 Int rem
=e
-(1000*thou
); /* e%1000 */
1666 *c
++=(char)('0'+(char)thou
); /* the thousands digit */
1667 u
=&BIN2BCD8
[rem
*4]; /* -> 3 digits + length byte */
1668 UBFROMUI(c
, UBTOUI(u
)|CHARMASK
); /* copy fixed 3+1 characters [is safe] */
1669 c
+=3; /* bump pointer, always 3 digits */
1673 *c
='\0'; /* add terminator */
1674 /*printf("res %s\n", string); */
1678 /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
1679 /* Surprisingly, this is close to being the worst-case path, so the */
1680 /* shift is done by fours; this is a little tricky because the */
1681 /* rightmost character to be written must not be beyond where the */
1682 /* rightmost terminator could be -- so backoff to not touch */
1683 /* terminator position if need be (this can make exact alignments */
1684 /* for full Doubles, but in some cases needs care not to access too */
1685 /* far to the left) */
1687 pre
=-pre
+2; /* gap width, including "0." */
1688 t
=cstart
+ROUNDDOWN4(c
-cstart
)+pre
; /* preferred first target point */
1689 /* backoff if too far to the right */
1690 if (t
>string
+DECSTRING
-5) t
=string
+DECSTRING
-5; /* adjust to fit */
1691 /* now shift the entire coefficient to the right, being careful not */
1692 /* to access to the left of string [cannot use memcpy] */
1693 for (s
=t
-pre
; s
>=string
; s
-=4, t
-=4) UBFROMUI(t
, UBTOUI(s
));
1694 /* for Quads and Singles there may be a character or two left... */
1695 s
+=3; /* where next would come from */
1696 for(; s
>=cstart
; s
--, t
--) *(t
+3)=*(s
);
1697 /* now have fill 0. through 0.00000; use overlaps to avoid tests */
1699 memcpy(cstart
+pre
-4, "0000", 4);
1700 memcpy(cstart
, "0.00", 4);
1703 *(cstart
+pre
-1)='0';
1704 memcpy(cstart
, "0.", 2);
1706 *(c
+pre
)='\0'; /* terminate */
1708 } /* decFloatToString */
1710 /* ------------------------------------------------------------------ */
1711 /* decFloatToWider -- conversion to next-wider format */
1713 /* source is the decFloat format number which gets the result of */
1714 /* the conversion */
1715 /* wider is the decFloatWider format number which will be narrowed */
1718 /* Widening is always exact; no status is set (sNaNs are copied and */
1719 /* do not signal). The result will be canonical if the source is, */
1720 /* and may or may not be if the source is not. */
1721 /* ------------------------------------------------------------------ */
1722 /* widening is not possible for decQuad format numbers; simply omit */
1724 decFloatWider
* decFloatToWider(const decFloat
*source
, decFloatWider
*wider
) {
1727 /* Construct and copy the sign word */
1728 if (DFISSPECIAL(source
)) {
1729 /* copy sign, combination, and first bit of exponent (sNaN selector) */
1730 DFWWORD(wider
, 0)=DFWORD(source
, 0)&0xfe000000;
1733 else { /* is finite number */
1734 uInt exp
=GETEXPUN(source
)+DECWBIAS
; /* get unbiased exponent and rebias */
1735 uInt code
=(exp
>>DECWECONL
)<<29; /* set two bits of exp [msd=0] */
1736 code
|=(exp
<<(32-6-DECWECONL
)) & 0x03ffffff; /* add exponent continuation */
1737 code
|=DFWORD(source
, 0)&0x80000000; /* add sign */
1738 DFWWORD(wider
, 0)=code
; /* .. and place top word in wider */
1739 msd
=GETMSD(source
); /* get source coefficient MSD [0-9] */
1741 /* Copy the coefficient and clear any 'unused' words to left */
1743 DFWWORD(wider
, 1)=(DFWORD(source
, 0)&0x000fffff)|(msd
<<20);
1745 DFWWORD(wider
, 2)=(DFWORD(source
, 0)&0x0003ffff)|(msd
<<18);
1746 DFWWORD(wider
, 3)=DFWORD(source
, 1);
1747 DFWWORD(wider
, 1)=0;
1750 } /* decFloatToWider */
1753 /* ------------------------------------------------------------------ */
1754 /* decFloatVersion -- return package version string */
1756 /* returns a constant string describing this package */
1757 /* ------------------------------------------------------------------ */
1758 const char *decFloatVersion(void) {
1760 } /* decFloatVersion */
1762 /* ------------------------------------------------------------------ */
1763 /* decFloatZero -- set to canonical (integer) zero */
1765 /* df is the decFloat format number to integer +0 (q=0, c=+0) */
1768 /* No error is possible, and no status can be set. */
1769 /* ------------------------------------------------------------------ */
1770 decFloat
* decFloatZero(decFloat
*df
){
1771 DFWORD(df
, 0)=ZEROWORD
; /* set appropriate top word */
1779 /* decFloatShow(df, "zero"); */
1781 } /* decFloatZero */
1783 /* ------------------------------------------------------------------ */
1784 /* Private generic function (not format-specific) for development use */
1785 /* ------------------------------------------------------------------ */
1786 /* This is included once only, for all to use */
1787 #if QUAD && (DECCHECK || DECTRACE)
1788 /* ---------------------------------------------------------------- */
1789 /* decShowNum -- display bcd8 number in debug form */
1791 /* num is the bcdnum to display */
1792 /* tag is a string to label the display */
1793 /* ---------------------------------------------------------------- */
1794 void decShowNum(const bcdnum
*num
, const char *tag
) {
1795 const char *csign
="+"; /* sign character */
1796 uByte
*ub
; /* work */
1797 uInt uiwork
; /* for macros */
1798 if (num
->sign
==DECFLOAT_Sign
) csign
="-";
1800 printf(">%s> ", tag
);
1801 if (num
->exponent
==DECFLOAT_Inf
) printf("%sInfinity", csign
);
1802 else if (num
->exponent
==DECFLOAT_qNaN
) printf("%sqNaN", csign
);
1803 else if (num
->exponent
==DECFLOAT_sNaN
) printf("%ssNaN", csign
);
1805 char qbuf
[10]; /* for right-aligned q */
1807 const uByte
*u
; /* .. */
1808 Int e
=num
->exponent
; /* .. exponent */
1810 c
=&qbuf
[2]; /* where exponent will go */
1811 /* lay out the exponent */
1813 *c
++='-'; /* add '-' */
1814 e
=-e
; /* uInt, please */
1817 #error Exponent form is too long for ShowNum to lay out
1819 if (e
==0) *c
++='0'; /* 0-length case */
1820 else if (e
<1000) { /* 3 (or fewer) digits case */
1821 u
=&BIN2BCD8
[e
*4]; /* -> 3 digits + length byte */
1822 UBFROMUI(c
, UBTOUI(u
+3-*(u
+3))|CHARMASK
); /* [as above] */
1823 c
+=*(u
+3); /* bump pointer appropriately */
1825 else { /* 4-digits */
1826 Int thou
=((e
>>3)*1049)>>17; /* e/1000 */
1827 Int rem
=e
-(1000*thou
); /* e%1000 */
1828 *c
++=(char)('0'+(char)thou
); /* the thousands digit */
1829 u
=&BIN2BCD8
[rem
*4]; /* -> 3 digits + length byte */
1830 UBFROMUI(c
, UBTOUI(u
)|CHARMASK
); /* copy fixed 3+1 characters [is safe] */
1831 c
+=3; /* bump pointer, always 3 digits */
1833 *c
='\0'; /* add terminator */
1834 printf("%7s c=%s", qbuf
, csign
);
1837 if (!EXPISSPECIAL(num
->exponent
) || num
->msd
!=num
->lsd
|| *num
->lsd
!=0) {
1838 for (ub
=num
->msd
; ub
<=num
->lsd
; ub
++) { /* coefficient... */
1840 if ((num
->lsd
-ub
)%3==0 && ub
!=num
->lsd
) printf(" "); /* 4-space */
This page took 0.118451 seconds and 4 git commands to generate.