0
|
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.
|
|
4
|
|
5 This file is part of GCC.
|
|
6
|
|
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
|
|
10 version.
|
|
11
|
|
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
|
|
15 for more details.
|
|
16
|
|
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.
|
|
20
|
|
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/>. */
|
|
25
|
|
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. */
|
|
32 /* */
|
|
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). */
|
|
43 /* */
|
|
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 */
|
|
46 /* untouched. */
|
|
47
|
|
48 #include "decCommonSymbols.h"
|
|
49
|
|
50 /* names for simpler testing and default context */
|
|
51 #if DECPMAX==7
|
|
52 #define SINGLE 1
|
|
53 #define DOUBLE 0
|
|
54 #define QUAD 0
|
|
55 #define DEFCONTEXT DEC_INIT_DECIMAL32
|
|
56 #elif DECPMAX==16
|
|
57 #define SINGLE 0
|
|
58 #define DOUBLE 1
|
|
59 #define QUAD 0
|
|
60 #define DEFCONTEXT DEC_INIT_DECIMAL64
|
|
61 #elif DECPMAX==34
|
|
62 #define SINGLE 0
|
|
63 #define DOUBLE 0
|
|
64 #define QUAD 1
|
|
65 #define DEFCONTEXT DEC_INIT_DECIMAL128
|
|
66 #else
|
|
67 #error Unexpected DECPMAX value
|
|
68 #endif
|
|
69
|
|
70 /* Assertions */
|
|
71
|
|
72 #if DECPMAX!=7 && DECPMAX!=16 && DECPMAX!=34
|
|
73 #error Unexpected Pmax (DECPMAX) value for this module
|
|
74 #endif
|
|
75
|
|
76 /* Assert facts about digit characters, etc. */
|
|
77 #if ('9'&0x0f)!=9
|
|
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 */
|
|
80 #endif
|
|
81 #if ('9'&0xf0)==('.'&0xf0)
|
|
82 #error This module assumes '.' has a different mask than a digit
|
|
83 #endif
|
|
84
|
|
85 /* Assert ToString lay-out conditions */
|
|
86 #if DECSTRING<DECPMAX+9
|
|
87 #error ToString needs at least 8 characters for lead-in and dot
|
|
88 #endif
|
|
89 #if DECPMAX+DECEMAXD+5 > DECSTRING
|
|
90 #error Exponent form can be too long for ToString to lay out safely
|
|
91 #endif
|
|
92 #if DECEMAXD > 4
|
|
93 #error Exponent form is too long for ToString to lay out
|
|
94 /* Note: code for up to 9 digits exists in archives [decOct] */
|
|
95 #endif
|
|
96
|
|
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 *);
|
|
100
|
|
101 /* Macros and private tables; those which are not format-dependent */
|
|
102 /* are only included if decQuad is being built. */
|
|
103
|
|
104 /* ------------------------------------------------------------------ */
|
|
105 /* Combination field lookup tables (uInts to save measurable work) */
|
|
106 /* */
|
|
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) */
|
|
114 /* */
|
|
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 */
|
|
119 /* */
|
|
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};
|
|
138 #if !QUAD
|
|
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};
|
|
154 #endif
|
|
155
|
|
156 #if QUAD
|
|
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, 1,
|
|
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};
|
|
162
|
|
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};
|
|
172
|
|
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 */
|
|
184
|
|
185 #endif
|
|
186
|
|
187 /* ----------------------------------------------------------------- */
|
|
188 /* decBiStr -- compare string with pairwise options */
|
|
189 /* */
|
|
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 */
|
|
193 /* */
|
|
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 */
|
|
197 /* */
|
|
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;
|
|
207 } /* forever */
|
|
208 return 1;
|
|
209 } /* decBiStr */
|
|
210
|
|
211 /* ------------------------------------------------------------------ */
|
|
212 /* decFinalize -- adjust and store a final result */
|
|
213 /* */
|
|
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 */
|
|
218 /* returns df */
|
|
219 /* */
|
|
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). */
|
|
224 /* */
|
|
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. */
|
|
227 /* */
|
|
228 /* No error is possible, but Inexact, Underflow, and/or Overflow may */
|
|
229 /* be set. */
|
|
230 /* ------------------------------------------------------------------ */
|
|
231 /* Constant whose size varies with format; also the check for surprises */
|
|
232 static uByte allnines[DECPMAX]=
|
|
233 #if SINGLE
|
|
234 {9, 9, 9, 9, 9, 9, 9};
|
|
235 #elif DOUBLE
|
|
236 {9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9};
|
|
237 #elif QUAD
|
|
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};
|
|
240 #endif
|
|
241
|
|
242 static decFloat * decFinalize(decFloat *df, bcdnum *num,
|
|
243 decContext *set) {
|
|
244 uByte *ub; /* work */
|
|
245 uInt dpd; /* .. */
|
|
246 uByte *umsd=num->msd; /* local copy */
|
|
247 uByte *ulsd=num->lsd; /* .. */
|
|
248 uInt encode; /* encoding accumulator */
|
|
249 Int length; /* coefficient length */
|
|
250
|
|
251 #if DECCHECK
|
|
252 Int clen=ulsd-umsd+1;
|
|
253 #if QUAD
|
|
254 #define COEXTRA 2 /* extra-long coefficent */
|
|
255 #else
|
|
256 #define COEXTRA 0
|
|
257 #endif
|
|
258 if (clen<1 || clen>DECPMAX*3+2+COEXTRA)
|
|
259 printf("decFinalize: suspect coefficient [length=%ld]\n", (LI)clen);
|
|
260 if (num->sign!=0 && num->sign!=DECFLOAT_Sign)
|
|
261 printf("decFinalize: bad sign [%08lx]\n", (LI)num->sign);
|
|
262 if (!EXPISSPECIAL(num->exponent)
|
|
263 && (num->exponent>1999999999 || num->exponent<-1999999999))
|
|
264 printf("decFinalize: improbable exponent [%ld]\n", (LI)num->exponent);
|
|
265 /* decShowNum(num, "final"); */
|
|
266 #endif
|
|
267
|
|
268 /* A special will have an 'exponent' which is very positive and a */
|
|
269 /* coefficient < DECPMAX */
|
|
270 length=(uInt)(ulsd-umsd+1); /* coefficient length */
|
|
271
|
|
272 if (!NUMISSPECIAL(num)) {
|
|
273 Int drop; /* digits to be dropped */
|
|
274 /* skip leading insignificant zeros to calculate an exact length */
|
|
275 /* [this is quite expensive] */
|
|
276 if (*umsd==0) {
|
|
277 for (; UINTAT(umsd)==0 && umsd+3<ulsd;) umsd+=4;
|
|
278 for (; *umsd==0 && umsd<ulsd;) umsd++;
|
|
279 length=ulsd-umsd+1; /* recalculate */
|
|
280 }
|
|
281 drop=MAXI(length-DECPMAX, DECQTINY-num->exponent);
|
|
282 /* drop can now be > digits for bottom-clamp (subnormal) cases */
|
|
283 if (drop>0) { /* rounding needed */
|
|
284 /* (decFloatQuantize has very similar code to this, so any */
|
|
285 /* changes may need to be made there, too) */
|
|
286 uByte *roundat; /* -> re-round digit */
|
|
287 uByte reround; /* reround value */
|
|
288 /* printf("Rounding; drop=%ld\n", (LI)drop); */
|
|
289
|
|
290 num->exponent+=drop; /* always update exponent */
|
|
291
|
|
292 /* Three cases here: */
|
|
293 /* 1. new LSD is in coefficient (almost always) */
|
|
294 /* 2. new LSD is digit to left of coefficient (so MSD is */
|
|
295 /* round-for-reround digit) */
|
|
296 /* 3. new LSD is to left of case 2 (whole coefficient is sticky) */
|
|
297 /* [duplicate check-stickies code to save a test] */
|
|
298 /* [by-digit check for stickies as runs of zeros are rare] */
|
|
299 if (drop<length) { /* NB lengths not addresses */
|
|
300 roundat=umsd+length-drop;
|
|
301 reround=*roundat;
|
|
302 for (ub=roundat+1; ub<=ulsd; ub++) {
|
|
303 if (*ub!=0) { /* non-zero to be discarded */
|
|
304 reround=DECSTICKYTAB[reround]; /* apply sticky bit */
|
|
305 break; /* [remainder don't-care] */
|
|
306 }
|
|
307 } /* check stickies */
|
|
308 ulsd=roundat-1; /* new LSD */
|
|
309 }
|
|
310 else { /* edge case */
|
|
311 if (drop==length) {
|
|
312 roundat=umsd;
|
|
313 reround=*roundat;
|
|
314 }
|
|
315 else {
|
|
316 roundat=umsd-1;
|
|
317 reround=0;
|
|
318 }
|
|
319 for (ub=roundat+1; ub<=ulsd; ub++) {
|
|
320 if (*ub!=0) { /* non-zero to be discarded */
|
|
321 reround=DECSTICKYTAB[reround]; /* apply sticky bit */
|
|
322 break; /* [remainder don't-care] */
|
|
323 }
|
|
324 } /* check stickies */
|
|
325 *umsd=0; /* coefficient is a 0 */
|
|
326 ulsd=umsd; /* .. */
|
|
327 }
|
|
328
|
|
329 if (reround!=0) { /* discarding non-zero */
|
|
330 uInt bump=0;
|
|
331 set->status|=DEC_Inexact;
|
|
332 /* if adjusted exponent [exp+digits-1] is < EMIN then num is */
|
|
333 /* subnormal -- so raise Underflow */
|
|
334 if (num->exponent<DECEMIN && (num->exponent+(ulsd-umsd+1)-1)<DECEMIN)
|
|
335 set->status|=DEC_Underflow;
|
|
336
|
|
337 /* next decide whether increment of the coefficient is needed */
|
|
338 if (set->round==DEC_ROUND_HALF_EVEN) { /* fastpath slowest case */
|
|
339 if (reround>5) bump=1; /* >0.5 goes up */
|
|
340 else if (reround==5) /* exactly 0.5000 .. */
|
|
341 bump=*ulsd & 0x01; /* .. up iff [new] lsd is odd */
|
|
342 } /* r-h-e */
|
|
343 else switch (set->round) {
|
|
344 case DEC_ROUND_DOWN: {
|
|
345 /* no change */
|
|
346 break;} /* r-d */
|
|
347 case DEC_ROUND_HALF_DOWN: {
|
|
348 if (reround>5) bump=1;
|
|
349 break;} /* r-h-d */
|
|
350 case DEC_ROUND_HALF_UP: {
|
|
351 if (reround>=5) bump=1;
|
|
352 break;} /* r-h-u */
|
|
353 case DEC_ROUND_UP: {
|
|
354 if (reround>0) bump=1;
|
|
355 break;} /* r-u */
|
|
356 case DEC_ROUND_CEILING: {
|
|
357 /* same as _UP for positive numbers, and as _DOWN for negatives */
|
|
358 if (!num->sign && reround>0) bump=1;
|
|
359 break;} /* r-c */
|
|
360 case DEC_ROUND_FLOOR: {
|
|
361 /* same as _UP for negative numbers, and as _DOWN for positive */
|
|
362 /* [negative reround cannot occur on 0] */
|
|
363 if (num->sign && reround>0) bump=1;
|
|
364 break;} /* r-f */
|
|
365 case DEC_ROUND_05UP: {
|
|
366 if (reround>0) { /* anything out there is 'sticky' */
|
|
367 /* bump iff lsd=0 or 5; this cannot carry so it could be */
|
|
368 /* effected immediately with no bump -- but the code */
|
|
369 /* is clearer if this is done the same way as the others */
|
|
370 if (*ulsd==0 || *ulsd==5) bump=1;
|
|
371 }
|
|
372 break;} /* r-r */
|
|
373 default: { /* e.g., DEC_ROUND_MAX */
|
|
374 set->status|=DEC_Invalid_context;
|
|
375 #if DECCHECK
|
|
376 printf("Unknown rounding mode: %ld\n", (LI)set->round);
|
|
377 #endif
|
|
378 break;}
|
|
379 } /* switch (not r-h-e) */
|
|
380 /* printf("ReRound: %ld bump: %ld\n", (LI)reround, (LI)bump); */
|
|
381
|
|
382 if (bump!=0) { /* need increment */
|
|
383 /* increment the coefficient; this might end up with 1000... */
|
|
384 /* (after the all nines case) */
|
|
385 ub=ulsd;
|
|
386 for(; ub-3>=umsd && UINTAT(ub-3)==0x09090909; ub-=4) UINTAT(ub-3)=0;
|
|
387 /* [note ub could now be to left of msd, and it is not safe */
|
|
388 /* to write to the the left of the msd] */
|
|
389 /* now at most 3 digits left to non-9 (usually just the one) */
|
|
390 for (; ub>=umsd; *ub=0, ub--) {
|
|
391 if (*ub==9) continue; /* carry */
|
|
392 *ub+=1;
|
|
393 break;
|
|
394 }
|
|
395 if (ub<umsd) { /* had all-nines */
|
|
396 *umsd=1; /* coefficient to 1000... */
|
|
397 /* usually the 1000... coefficient can be used as-is */
|
|
398 if ((ulsd-umsd+1)==DECPMAX) {
|
|
399 num->exponent++;
|
|
400 }
|
|
401 else {
|
|
402 /* if coefficient is shorter than Pmax then num is */
|
|
403 /* subnormal, so extend it; this is safe as drop>0 */
|
|
404 /* (or, if the coefficient was supplied above, it could */
|
|
405 /* not be 9); this may make the result normal. */
|
|
406 ulsd++;
|
|
407 *ulsd=0;
|
|
408 /* [exponent unchanged] */
|
|
409 #if DECCHECK
|
|
410 if (num->exponent!=DECQTINY) /* sanity check */
|
|
411 printf("decFinalize: bad all-nines extend [^%ld, %ld]\n",
|
|
412 (LI)num->exponent, (LI)(ulsd-umsd+1));
|
|
413 #endif
|
|
414 } /* subnormal extend */
|
|
415 } /* had all-nines */
|
|
416 } /* bump needed */
|
|
417 } /* inexact rounding */
|
|
418
|
|
419 length=ulsd-umsd+1; /* recalculate (may be <DECPMAX) */
|
|
420 } /* need round (drop>0) */
|
|
421
|
|
422 /* The coefficient will now fit and has final length unless overflow */
|
|
423 /* decShowNum(num, "rounded"); */
|
|
424
|
|
425 /* if exponent is >=emax may have to clamp, overflow, or fold-down */
|
|
426 if (num->exponent>DECEMAX-(DECPMAX-1)) { /* is edge case */
|
|
427 /* printf("overflow checks...\n"); */
|
|
428 if (*ulsd==0 && ulsd==umsd) { /* have zero */
|
|
429 num->exponent=DECEMAX-(DECPMAX-1); /* clamp to max */
|
|
430 }
|
|
431 else if ((num->exponent+length-1)>DECEMAX) { /* > Nmax */
|
|
432 /* Overflow -- these could go straight to encoding, here, but */
|
|
433 /* instead num is adjusted to keep the code cleaner */
|
|
434 Flag needmax=0; /* 1 for finite result */
|
|
435 set->status|=(DEC_Overflow | DEC_Inexact);
|
|
436 switch (set->round) {
|
|
437 case DEC_ROUND_DOWN: {
|
|
438 needmax=1; /* never Infinity */
|
|
439 break;} /* r-d */
|
|
440 case DEC_ROUND_05UP: {
|
|
441 needmax=1; /* never Infinity */
|
|
442 break;} /* r-05 */
|
|
443 case DEC_ROUND_CEILING: {
|
|
444 if (num->sign) needmax=1; /* Infinity iff non-negative */
|
|
445 break;} /* r-c */
|
|
446 case DEC_ROUND_FLOOR: {
|
|
447 if (!num->sign) needmax=1; /* Infinity iff negative */
|
|
448 break;} /* r-f */
|
|
449 default: break; /* Infinity in all other cases */
|
|
450 }
|
|
451 if (!needmax) { /* easy .. set Infinity */
|
|
452 num->exponent=DECFLOAT_Inf;
|
|
453 *umsd=0; /* be clean: coefficient to 0 */
|
|
454 ulsd=umsd; /* .. */
|
|
455 }
|
|
456 else { /* return Nmax */
|
|
457 umsd=allnines; /* use constant array */
|
|
458 ulsd=allnines+DECPMAX-1;
|
|
459 num->exponent=DECEMAX-(DECPMAX-1);
|
|
460 }
|
|
461 }
|
|
462 else { /* no overflow but non-zero and may have to fold-down */
|
|
463 Int shift=num->exponent-(DECEMAX-(DECPMAX-1));
|
|
464 if (shift>0) { /* fold-down needed */
|
|
465 /* fold down needed; must copy to buffer in order to pad */
|
|
466 /* with zeros safely; fortunately this is not the worst case */
|
|
467 /* path because cannot have had a round */
|
|
468 uByte buffer[ROUNDUP(DECPMAX+3, 4)]; /* [+3 allows uInt padding] */
|
|
469 uByte *s=umsd; /* source */
|
|
470 uByte *t=buffer; /* safe target */
|
|
471 uByte *tlsd=buffer+(ulsd-umsd)+shift; /* target LSD */
|
|
472 /* printf("folddown shift=%ld\n", (LI)shift); */
|
|
473 for (; s<=ulsd; s+=4, t+=4) UINTAT(t)=UINTAT(s);
|
|
474 for (t=tlsd-shift+1; t<=tlsd; t+=4) UINTAT(t)=0; /* pad */
|
|
475 num->exponent-=shift;
|
|
476 umsd=buffer;
|
|
477 ulsd=tlsd;
|
|
478 }
|
|
479 } /* fold-down? */
|
|
480 length=ulsd-umsd+1; /* recalculate length */
|
|
481 } /* high-end edge case */
|
|
482 } /* finite number */
|
|
483
|
|
484 /*------------------------------------------------------------------*/
|
|
485 /* At this point the result will properly fit the decFloat */
|
|
486 /* encoding, and it can be encoded with no possibility of error */
|
|
487 /*------------------------------------------------------------------*/
|
|
488 /* Following code does not alter coefficient (could be allnines array) */
|
|
489
|
|
490 if (length==DECPMAX) {
|
|
491 return decFloatFromBCD(df, num->exponent, umsd, num->sign);
|
|
492 }
|
|
493
|
|
494 /* Here when length is short */
|
|
495 if (!NUMISSPECIAL(num)) { /* is still finite */
|
|
496 /* encode the combination field and exponent continuation */
|
|
497 uInt uexp=(uInt)(num->exponent+DECBIAS); /* biased exponent */
|
|
498 uInt code=(uexp>>DECECONL)<<4; /* top two bits of exp */
|
|
499 /* [msd=0] */
|
|
500 /* look up the combination field and make high word */
|
|
501 encode=DECCOMBFROM[code]; /* indexed by (0-2)*16+msd */
|
|
502 encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; /* exponent continuation */
|
|
503 }
|
|
504 else encode=num->exponent; /* special [already in word] */
|
|
505 /* [coefficient length here will be < DECPMAX] */
|
|
506
|
|
507 encode|=num->sign; /* add sign */
|
|
508
|
|
509 /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */
|
|
510 /* refers to the declet from the least significant three digits) */
|
|
511 /* and put the corresponding DPD code into dpd. Access to umsd and */
|
|
512 /* ulsd (pointers to the most and least significant digit of the */
|
|
513 /* variable-length coefficient) is assumed, along with use of a */
|
|
514 /* working pointer, uInt *ub. */
|
|
515 /* As not full-length then chances are there are many leading zeros */
|
|
516 /* [and there may be a partial triad] */
|
|
517 #define getDPD(dpd, n) ub=ulsd-(3*(n))-2; \
|
|
518 if (ub<umsd-2) dpd=0; \
|
|
519 else if (ub>=umsd) dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)]; \
|
|
520 else {dpd=*(ub+2); if (ub+1==umsd) dpd+=*(ub+1)*16; dpd=BCD2DPD[dpd];}
|
|
521
|
|
522 /* place the declets in the encoding words and copy to result (df), */
|
|
523 /* according to endianness; in all cases complete the sign word */
|
|
524 /* first */
|
|
525 #if DECPMAX==7
|
|
526 getDPD(dpd, 1);
|
|
527 encode|=dpd<<10;
|
|
528 getDPD(dpd, 0);
|
|
529 encode|=dpd;
|
|
530 DFWORD(df, 0)=encode; /* just the one word */
|
|
531
|
|
532 #elif DECPMAX==16
|
|
533 getDPD(dpd, 4); encode|=dpd<<8;
|
|
534 getDPD(dpd, 3); encode|=dpd>>2;
|
|
535 DFWORD(df, 0)=encode;
|
|
536 encode=dpd<<30;
|
|
537 getDPD(dpd, 2); encode|=dpd<<20;
|
|
538 getDPD(dpd, 1); encode|=dpd<<10;
|
|
539 getDPD(dpd, 0); encode|=dpd;
|
|
540 DFWORD(df, 1)=encode;
|
|
541
|
|
542 #elif DECPMAX==34
|
|
543 getDPD(dpd,10); encode|=dpd<<4;
|
|
544 getDPD(dpd, 9); encode|=dpd>>6;
|
|
545 DFWORD(df, 0)=encode;
|
|
546
|
|
547 encode=dpd<<26;
|
|
548 getDPD(dpd, 8); encode|=dpd<<16;
|
|
549 getDPD(dpd, 7); encode|=dpd<<6;
|
|
550 getDPD(dpd, 6); encode|=dpd>>4;
|
|
551 DFWORD(df, 1)=encode;
|
|
552
|
|
553 encode=dpd<<28;
|
|
554 getDPD(dpd, 5); encode|=dpd<<18;
|
|
555 getDPD(dpd, 4); encode|=dpd<<8;
|
|
556 getDPD(dpd, 3); encode|=dpd>>2;
|
|
557 DFWORD(df, 2)=encode;
|
|
558
|
|
559 encode=dpd<<30;
|
|
560 getDPD(dpd, 2); encode|=dpd<<20;
|
|
561 getDPD(dpd, 1); encode|=dpd<<10;
|
|
562 getDPD(dpd, 0); encode|=dpd;
|
|
563 DFWORD(df, 3)=encode;
|
|
564 #endif
|
|
565
|
|
566 /* printf("Status: %08lx\n", (LI)set->status); */
|
|
567 /* decFloatShow(df, "final"); */
|
|
568 return df;
|
|
569 } /* decFinalize */
|
|
570
|
|
571 /* ------------------------------------------------------------------ */
|
|
572 /* decFloatFromBCD -- set decFloat from exponent, BCD8, and sign */
|
|
573 /* */
|
|
574 /* df is the target decFloat */
|
|
575 /* exp is the in-range unbiased exponent, q, or a special value in */
|
|
576 /* the form returned by decFloatGetExponent */
|
|
577 /* bcdar holds DECPMAX digits to set the coefficient from, one */
|
|
578 /* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
|
|
579 /* if df is a NaN; all are ignored if df is infinite. */
|
|
580 /* All bytes must be in 0-9; results undefined otherwise. */
|
|
581 /* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
|
|
582 /* returns df, which will be canonical */
|
|
583 /* */
|
|
584 /* No error is possible, and no status will be set. */
|
|
585 /* ------------------------------------------------------------------ */
|
|
586 decFloat * decFloatFromBCD(decFloat *df, Int exp, const uByte *bcdar,
|
|
587 Int sig) {
|
|
588 uInt encode, dpd; /* work */
|
|
589 const uByte *ub; /* .. */
|
|
590
|
|
591 if (EXPISSPECIAL(exp)) encode=exp|sig;/* specials already encoded */
|
|
592 else { /* is finite */
|
|
593 /* encode the combination field and exponent continuation */
|
|
594 uInt uexp=(uInt)(exp+DECBIAS); /* biased exponent */
|
|
595 uInt code=(uexp>>DECECONL)<<4; /* top two bits of exp */
|
|
596 code+=bcdar[0]; /* add msd */
|
|
597 /* look up the combination field and make high word */
|
|
598 encode=DECCOMBFROM[code]|sig; /* indexed by (0-2)*16+msd */
|
|
599 encode|=(uexp<<(32-6-DECECONL)) & 0x03ffffff; /* exponent continuation */
|
|
600 }
|
|
601
|
|
602 /* private macro to extract a declet, n (where 0<=n<DECLETS and 0 */
|
|
603 /* refers to the declet from the least significant three digits) */
|
|
604 /* and put the corresponding DPD code into dpd. */
|
|
605 /* Use of a working pointer, uInt *ub, is assumed. */
|
|
606
|
|
607 #define getDPDf(dpd, n) ub=bcdar+DECPMAX-1-(3*(n))-2; \
|
|
608 dpd=BCD2DPD[(*ub*256)+(*(ub+1)*16)+*(ub+2)];
|
|
609
|
|
610 /* place the declets in the encoding words and copy to result (df), */
|
|
611 /* according to endianness; in all cases complete the sign word */
|
|
612 /* first */
|
|
613 #if DECPMAX==7
|
|
614 getDPDf(dpd, 1);
|
|
615 encode|=dpd<<10;
|
|
616 getDPDf(dpd, 0);
|
|
617 encode|=dpd;
|
|
618 DFWORD(df, 0)=encode; /* just the one word */
|
|
619
|
|
620 #elif DECPMAX==16
|
|
621 getDPDf(dpd, 4); encode|=dpd<<8;
|
|
622 getDPDf(dpd, 3); encode|=dpd>>2;
|
|
623 DFWORD(df, 0)=encode;
|
|
624 encode=dpd<<30;
|
|
625 getDPDf(dpd, 2); encode|=dpd<<20;
|
|
626 getDPDf(dpd, 1); encode|=dpd<<10;
|
|
627 getDPDf(dpd, 0); encode|=dpd;
|
|
628 DFWORD(df, 1)=encode;
|
|
629
|
|
630 #elif DECPMAX==34
|
|
631 getDPDf(dpd,10); encode|=dpd<<4;
|
|
632 getDPDf(dpd, 9); encode|=dpd>>6;
|
|
633 DFWORD(df, 0)=encode;
|
|
634
|
|
635 encode=dpd<<26;
|
|
636 getDPDf(dpd, 8); encode|=dpd<<16;
|
|
637 getDPDf(dpd, 7); encode|=dpd<<6;
|
|
638 getDPDf(dpd, 6); encode|=dpd>>4;
|
|
639 DFWORD(df, 1)=encode;
|
|
640
|
|
641 encode=dpd<<28;
|
|
642 getDPDf(dpd, 5); encode|=dpd<<18;
|
|
643 getDPDf(dpd, 4); encode|=dpd<<8;
|
|
644 getDPDf(dpd, 3); encode|=dpd>>2;
|
|
645 DFWORD(df, 2)=encode;
|
|
646
|
|
647 encode=dpd<<30;
|
|
648 getDPDf(dpd, 2); encode|=dpd<<20;
|
|
649 getDPDf(dpd, 1); encode|=dpd<<10;
|
|
650 getDPDf(dpd, 0); encode|=dpd;
|
|
651 DFWORD(df, 3)=encode;
|
|
652 #endif
|
|
653 /* decFloatShow(df, "final"); */
|
|
654 return df;
|
|
655 } /* decFloatFromBCD */
|
|
656
|
|
657 /* ------------------------------------------------------------------ */
|
|
658 /* decFloatFromPacked -- set decFloat from exponent and packed BCD */
|
|
659 /* */
|
|
660 /* df is the target decFloat */
|
|
661 /* exp is the in-range unbiased exponent, q, or a special value in */
|
|
662 /* the form returned by decFloatGetExponent */
|
|
663 /* packed holds DECPMAX packed decimal digits plus a sign nibble */
|
|
664 /* (all 6 codes are OK); the first (MSD) is ignored if df is a NaN */
|
|
665 /* and all except sign are ignored if df is infinite. For DOUBLE */
|
|
666 /* and QUAD the first (pad) nibble is also ignored in all cases. */
|
|
667 /* All coefficient nibbles must be in 0-9 and sign in A-F; results */
|
|
668 /* are undefined otherwise. */
|
|
669 /* returns df, which will be canonical */
|
|
670 /* */
|
|
671 /* No error is possible, and no status will be set. */
|
|
672 /* ------------------------------------------------------------------ */
|
|
673 decFloat * decFloatFromPacked(decFloat *df, Int exp, const uByte *packed) {
|
|
674 uByte bcdar[DECPMAX+2]; /* work [+1 for pad, +1 for sign] */
|
|
675 const uByte *ip; /* .. */
|
|
676 uByte *op; /* .. */
|
|
677 Int sig=0; /* sign */
|
|
678
|
|
679 /* expand coefficient and sign to BCDAR */
|
|
680 #if SINGLE
|
|
681 op=bcdar+1; /* no pad digit */
|
|
682 #else
|
|
683 op=bcdar; /* first (pad) digit ignored */
|
|
684 #endif
|
|
685 for (ip=packed; ip<packed+((DECPMAX+2)/2); ip++) {
|
|
686 *op++=*ip>>4;
|
|
687 *op++=(uByte)(*ip&0x0f); /* [final nibble is sign] */
|
|
688 }
|
|
689 op--; /* -> sign byte */
|
|
690 if (*op==DECPMINUS || *op==DECPMINUSALT) sig=DECFLOAT_Sign;
|
|
691
|
|
692 if (EXPISSPECIAL(exp)) { /* Infinity or NaN */
|
|
693 if (!EXPISINF(exp)) bcdar[1]=0; /* a NaN: ignore MSD */
|
|
694 else memset(bcdar+1, 0, DECPMAX); /* Infinite: coefficient to 0 */
|
|
695 }
|
|
696 return decFloatFromBCD(df, exp, bcdar+1, sig);
|
|
697 } /* decFloatFromPacked */
|
|
698
|
|
699 /* ------------------------------------------------------------------ */
|
|
700 /* decFloatFromString -- conversion from numeric string */
|
|
701 /* */
|
|
702 /* result is the decFloat format number which gets the result of */
|
|
703 /* the conversion */
|
|
704 /* *string is the character string which should contain a valid */
|
|
705 /* number (which may be a special value), \0-terminated */
|
|
706 /* If there are too many significant digits in the */
|
|
707 /* coefficient it will be rounded. */
|
|
708 /* set is the context */
|
|
709 /* returns result */
|
|
710 /* */
|
|
711 /* The length of the coefficient and the size of the exponent are */
|
|
712 /* checked by this routine, so the correct error (Underflow or */
|
|
713 /* Overflow) can be reported or rounding applied, as necessary. */
|
|
714 /* */
|
|
715 /* There is no limit to the coefficient length for finite inputs; */
|
|
716 /* NaN payloads must be integers with no more than DECPMAX-1 digits. */
|
|
717 /* Exponents may have up to nine significant digits. */
|
|
718 /* */
|
|
719 /* If bad syntax is detected, the result will be a quiet NaN. */
|
|
720 /* ------------------------------------------------------------------ */
|
|
721 decFloat * decFloatFromString(decFloat *result, const char *string,
|
|
722 decContext *set) {
|
|
723 Int digits; /* count of digits in coefficient */
|
|
724 const char *dotchar=NULL; /* where dot was found [NULL if none] */
|
|
725 const char *cfirst=string; /* -> first character of decimal part */
|
|
726 const char *c; /* work */
|
|
727 uByte *ub; /* .. */
|
|
728 bcdnum num; /* collects data for finishing */
|
|
729 uInt error=DEC_Conversion_syntax; /* assume the worst */
|
|
730 uByte buffer[ROUNDUP(DECSTRING+11, 8)]; /* room for most coefficents, */
|
|
731 /* some common rounding, +3, & pad */
|
|
732 #if DECTRACE
|
|
733 /* printf("FromString %s ...\n", string); */
|
|
734 #endif
|
|
735
|
|
736 for(;;) { /* once-only 'loop' */
|
|
737 num.sign=0; /* assume non-negative */
|
|
738 num.msd=buffer; /* MSD is here always */
|
|
739
|
|
740 /* detect and validate the coefficient, including any leading, */
|
|
741 /* trailing, or embedded '.' */
|
|
742 /* [could test four-at-a-time here (saving 10% for decQuads), */
|
|
743 /* but that risks storage violation because the position of the */
|
|
744 /* terminator is unknown] */
|
|
745 for (c=string;; c++) { /* -> input character */
|
|
746 if (((unsigned)(*c-'0'))<=9) continue; /* '0' through '9' is good */
|
|
747 if (*c=='\0') break; /* most common non-digit */
|
|
748 if (*c=='.') {
|
|
749 if (dotchar!=NULL) break; /* not first '.' */
|
|
750 dotchar=c; /* record offset into decimal part */
|
|
751 continue;}
|
|
752 if (c==string) { /* first in string... */
|
|
753 if (*c=='-') { /* valid - sign */
|
|
754 cfirst++;
|
|
755 num.sign=DECFLOAT_Sign;
|
|
756 continue;}
|
|
757 if (*c=='+') { /* valid + sign */
|
|
758 cfirst++;
|
|
759 continue;}
|
|
760 }
|
|
761 /* *c is not a digit, terminator, or a valid +, -, or '.' */
|
|
762 break;
|
|
763 } /* c loop */
|
|
764
|
|
765 digits=(uInt)(c-cfirst); /* digits (+1 if a dot) */
|
|
766
|
|
767 if (digits>0) { /* had digits and/or dot */
|
|
768 const char *clast=c-1; /* note last coefficient char position */
|
|
769 Int exp=0; /* exponent accumulator */
|
|
770 if (*c!='\0') { /* something follows the coefficient */
|
|
771 uInt edig; /* unsigned work */
|
|
772 /* had some digits and more to come; expect E[+|-]nnn now */
|
|
773 const char *firstexp; /* exponent first non-zero */
|
|
774 if (*c!='E' && *c!='e') break;
|
|
775 c++; /* to (optional) sign */
|
|
776 if (*c=='-' || *c=='+') c++; /* step over sign (c=clast+2) */
|
|
777 if (*c=='\0') break; /* no digits! (e.g., '1.2E') */
|
|
778 for (; *c=='0';) c++; /* skip leading zeros [even last] */
|
|
779 firstexp=c; /* remember start [maybe '\0'] */
|
|
780 /* gather exponent digits */
|
|
781 edig=(uInt)*c-(uInt)'0';
|
|
782 if (edig<=9) { /* [check not bad or terminator] */
|
|
783 exp+=edig; /* avoid initial X10 */
|
|
784 c++;
|
|
785 for (;; c++) {
|
|
786 edig=(uInt)*c-(uInt)'0';
|
|
787 if (edig>9) break;
|
|
788 exp=exp*10+edig;
|
|
789 }
|
|
790 }
|
|
791 /* if not now on the '\0', *c must not be a digit */
|
|
792 if (*c!='\0') break;
|
|
793
|
|
794 /* (this next test must be after the syntax checks) */
|
|
795 /* if definitely more than the possible digits for format then */
|
|
796 /* the exponent may have wrapped, so simply set it to a certain */
|
|
797 /* over/underflow value */
|
|
798 if (c>firstexp+DECEMAXD) exp=DECEMAX*2;
|
|
799 if (*(clast+2)=='-') exp=-exp; /* was negative */
|
|
800 } /* digits>0 */
|
|
801
|
|
802 if (dotchar!=NULL) { /* had a '.' */
|
|
803 digits--; /* remove from digits count */
|
|
804 if (digits==0) break; /* was dot alone: bad syntax */
|
|
805 exp-=(Int)(clast-dotchar); /* adjust exponent */
|
|
806 /* [the '.' can now be ignored] */
|
|
807 }
|
|
808 num.exponent=exp; /* exponent is good; store it */
|
|
809
|
|
810 /* Here when whole string has been inspected and syntax is good */
|
|
811 /* cfirst->first digit or dot, clast->last digit or dot */
|
|
812 error=0; /* no error possible now */
|
|
813
|
|
814 /* if the number of digits in the coefficient will fit in buffer */
|
|
815 /* then it can simply be converted to bcd8 and copied -- decFinalize */
|
|
816 /* will take care of leading zeros and rounding; the buffer is big */
|
|
817 /* enough for all canonical coefficients, including 0.00000nn... */
|
|
818 ub=buffer;
|
|
819 if (digits<=(Int)(sizeof(buffer)-3)) { /* [-3 allows by-4s copy] */
|
|
820 c=cfirst;
|
|
821 if (dotchar!=NULL) { /* a dot to worry about */
|
|
822 if (*(c+1)=='.') { /* common canonical case */
|
|
823 *ub++=(uByte)(*c-'0'); /* copy leading digit */
|
|
824 c+=2; /* prepare to handle rest */
|
|
825 }
|
|
826 else for (; c<=clast;) { /* '.' could be anywhere */
|
|
827 /* as usual, go by fours when safe; NB it has been asserted */
|
|
828 /* that a '.' does not have the same mask as a digit */
|
|
829 if (c<=clast-3 /* safe for four */
|
|
830 && (UINTAT(c)&0xf0f0f0f0)==CHARMASK) { /* test four */
|
|
831 UINTAT(ub)=UINTAT(c)&0x0f0f0f0f; /* to BCD8 */
|
|
832 ub+=4;
|
|
833 c+=4;
|
|
834 continue;
|
|
835 }
|
|
836 if (*c=='.') { /* found the dot */
|
|
837 c++; /* step over it .. */
|
|
838 break; /* .. and handle the rest */
|
|
839 }
|
|
840 *ub++=(uByte)(*c++-'0');
|
|
841 }
|
|
842 } /* had dot */
|
|
843 /* Now no dot; do this by fours (where safe) */
|
|
844 for (; c<=clast-3; c+=4, ub+=4) UINTAT(ub)=UINTAT(c)&0x0f0f0f0f;
|
|
845 for (; c<=clast; c++, ub++) *ub=(uByte)(*c-'0');
|
|
846 num.lsd=buffer+digits-1; /* record new LSD */
|
|
847 } /* fits */
|
|
848
|
|
849 else { /* too long for buffer */
|
|
850 /* [This is a rare and unusual case; arbitrary-length input] */
|
|
851 /* strip leading zeros [but leave final 0 if all 0's] */
|
|
852 if (*cfirst=='.') cfirst++; /* step past dot at start */
|
|
853 if (*cfirst=='0') { /* [cfirst always -> digit] */
|
|
854 for (; cfirst<clast; cfirst++) {
|
|
855 if (*cfirst!='0') { /* non-zero found */
|
|
856 if (*cfirst=='.') continue; /* [ignore] */
|
|
857 break; /* done */
|
|
858 }
|
|
859 digits--; /* 0 stripped */
|
|
860 } /* cfirst */
|
|
861 } /* at least one leading 0 */
|
|
862
|
|
863 /* the coefficient is now as short as possible, but may still */
|
|
864 /* be too long; copy up to Pmax+1 digits to the buffer, then */
|
|
865 /* just record any non-zeros (set round-for-reround digit) */
|
|
866 for (c=cfirst; c<=clast && ub<=buffer+DECPMAX; c++) {
|
|
867 /* (see commentary just above) */
|
|
868 if (c<=clast-3 /* safe for four */
|
|
869 && (UINTAT(c)&0xf0f0f0f0)==CHARMASK) { /* four digits */
|
|
870 UINTAT(ub)=UINTAT(c)&0x0f0f0f0f; /* to BCD8 */
|
|
871 ub+=4;
|
|
872 c+=3; /* [will become 4] */
|
|
873 continue;
|
|
874 }
|
|
875 if (*c=='.') continue; /* [ignore] */
|
|
876 *ub++=(uByte)(*c-'0');
|
|
877 }
|
|
878 ub--; /* -> LSD */
|
|
879 for (; c<=clast; c++) { /* inspect remaining chars */
|
|
880 if (*c!='0') { /* sticky bit needed */
|
|
881 if (*c=='.') continue; /* [ignore] */
|
|
882 *ub=DECSTICKYTAB[*ub]; /* update round-for-reround */
|
|
883 break; /* no need to look at more */
|
|
884 }
|
|
885 }
|
|
886 num.lsd=ub; /* record LSD */
|
|
887 /* adjust exponent for dropped digits */
|
|
888 num.exponent+=digits-(Int)(ub-buffer+1);
|
|
889 } /* too long for buffer */
|
|
890 } /* digits or dot */
|
|
891
|
|
892 else { /* no digits or dot were found */
|
|
893 if (*c=='\0') break; /* nothing to come is bad */
|
|
894 /* only Infinities and NaNs are allowed, here */
|
|
895 buffer[0]=0; /* default a coefficient of 0 */
|
|
896 num.lsd=buffer; /* .. */
|
|
897 if (decBiStr(c, "infinity", "INFINITY")
|
|
898 || decBiStr(c, "inf", "INF")) num.exponent=DECFLOAT_Inf;
|
|
899 else { /* should be a NaN */
|
|
900 num.exponent=DECFLOAT_qNaN; /* assume quiet NaN */
|
|
901 if (*c=='s' || *c=='S') { /* probably an sNaN */
|
|
902 c++;
|
|
903 num.exponent=DECFLOAT_sNaN; /* assume is in fact sNaN */
|
|
904 }
|
|
905 if (*c!='N' && *c!='n') break; /* check caseless "NaN" */
|
|
906 c++;
|
|
907 if (*c!='a' && *c!='A') break; /* .. */
|
|
908 c++;
|
|
909 if (*c!='N' && *c!='n') break; /* .. */
|
|
910 c++;
|
|
911 /* now either nothing, or nnnn payload (no dots), expected */
|
|
912 /* -> start of integer, and skip leading 0s [including plain 0] */
|
|
913 for (cfirst=c; *cfirst=='0';) cfirst++;
|
|
914 if (*cfirst!='\0') { /* not empty or all-0, payload */
|
|
915 /* payload found; check all valid digits and copy to buffer as bcd8 */
|
|
916 ub=buffer;
|
|
917 for (c=cfirst;; c++, ub++) {
|
|
918 if ((unsigned)(*c-'0')>9) break; /* quit if not 0-9 */
|
|
919 if (c-cfirst==DECPMAX-1) break; /* too many digits */
|
|
920 *ub=(uByte)(*c-'0'); /* good bcd8 */
|
|
921 }
|
|
922 if (*c!='\0') break; /* not all digits, or too many */
|
|
923 num.lsd=ub-1; /* record new LSD */
|
|
924 }
|
|
925 } /* NaN or sNaN */
|
|
926 error=0; /* syntax is OK */
|
|
927 break; /* done with specials */
|
|
928 } /* digits=0 (special expected) */
|
|
929 break;
|
|
930 } /* [for(;;) break] */
|
|
931
|
|
932 /* decShowNum(&num, "fromStr"); */
|
|
933
|
|
934 if (error!=0) {
|
|
935 set->status|=error;
|
|
936 num.exponent=DECFLOAT_qNaN; /* set up quiet NaN */
|
|
937 num.sign=0; /* .. with 0 sign */
|
|
938 buffer[0]=0; /* .. and coefficient */
|
|
939 num.lsd=buffer; /* .. */
|
|
940 /* decShowNum(&num, "oops"); */
|
|
941 }
|
|
942
|
|
943 /* decShowNum(&num, "dffs"); */
|
|
944 decFinalize(result, &num, set); /* round, check, and lay out */
|
|
945 /* decFloatShow(result, "fromString"); */
|
|
946 return result;
|
|
947 } /* decFloatFromString */
|
|
948
|
|
949 /* ------------------------------------------------------------------ */
|
|
950 /* decFloatFromWider -- conversion from next-wider format */
|
|
951 /* */
|
|
952 /* result is the decFloat format number which gets the result of */
|
|
953 /* the conversion */
|
|
954 /* wider is the decFloatWider format number which will be narrowed */
|
|
955 /* set is the context */
|
|
956 /* returns result */
|
|
957 /* */
|
|
958 /* Narrowing can cause rounding, overflow, etc., but not Invalid */
|
|
959 /* operation (sNaNs are copied and do not signal). */
|
|
960 /* ------------------------------------------------------------------ */
|
|
961 /* narrow-to is not possible for decQuad format numbers; simply omit */
|
|
962 #if !QUAD
|
|
963 decFloat * decFloatFromWider(decFloat *result, const decFloatWider *wider,
|
|
964 decContext *set) {
|
|
965 bcdnum num; /* collects data for finishing */
|
|
966 uByte bcdar[DECWPMAX]; /* room for wider coefficient */
|
|
967 uInt widerhi=DFWWORD(wider, 0); /* top word */
|
|
968 Int exp;
|
|
969
|
|
970 GETWCOEFF(wider, bcdar);
|
|
971
|
|
972 num.msd=bcdar; /* MSD is here always */
|
|
973 num.lsd=bcdar+DECWPMAX-1; /* LSD is here always */
|
|
974 num.sign=widerhi&0x80000000; /* extract sign [DECFLOAT_Sign=Neg] */
|
|
975
|
|
976 /* decode the wider combination field to exponent */
|
|
977 exp=DECCOMBWEXP[widerhi>>26]; /* decode from wider combination field */
|
|
978 /* if it is a special there's nothing to do unless sNaN; if it's */
|
|
979 /* finite then add the (wider) exponent continuation and unbias */
|
|
980 if (EXPISSPECIAL(exp)) exp=widerhi&0x7e000000; /* include sNaN selector */
|
|
981 else exp+=GETWECON(wider)-DECWBIAS;
|
|
982 num.exponent=exp;
|
|
983
|
|
984 /* decShowNum(&num, "dffw"); */
|
|
985 return decFinalize(result, &num, set);/* round, check, and lay out */
|
|
986 } /* decFloatFromWider */
|
|
987 #endif
|
|
988
|
|
989 /* ------------------------------------------------------------------ */
|
|
990 /* decFloatGetCoefficient -- get coefficient as BCD8 */
|
|
991 /* */
|
|
992 /* df is the decFloat from which to extract the coefficient */
|
|
993 /* bcdar is where DECPMAX bytes will be written, one BCD digit in */
|
|
994 /* each byte (BCD8 encoding); if df is a NaN the first byte will */
|
|
995 /* be zero, and if it is infinite they will all be zero */
|
|
996 /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
|
|
997 /* 0 otherwise) */
|
|
998 /* */
|
|
999 /* No error is possible, and no status will be set. If df is a */
|
|
1000 /* special value the array is set to zeros (for Infinity) or to the */
|
|
1001 /* payload of a qNaN or sNaN. */
|
|
1002 /* ------------------------------------------------------------------ */
|
|
1003 Int decFloatGetCoefficient(const decFloat *df, uByte *bcdar) {
|
|
1004 if (DFISINF(df)) memset(bcdar, 0, DECPMAX);
|
|
1005 else {
|
|
1006 GETCOEFF(df, bcdar); /* use macro */
|
|
1007 if (DFISNAN(df)) bcdar[0]=0; /* MSD needs correcting */
|
|
1008 }
|
|
1009 return DFISSIGNED(df);
|
|
1010 } /* decFloatGetCoefficient */
|
|
1011
|
|
1012 /* ------------------------------------------------------------------ */
|
|
1013 /* decFloatGetExponent -- get unbiased exponent */
|
|
1014 /* */
|
|
1015 /* df is the decFloat from which to extract the exponent */
|
|
1016 /* returns the exponent, q. */
|
|
1017 /* */
|
|
1018 /* No error is possible, and no status will be set. If df is a */
|
|
1019 /* special value the first seven bits of the decFloat are returned, */
|
|
1020 /* left adjusted and with the first (sign) bit set to 0 (followed by */
|
|
1021 /* 25 0 bits). e.g., -sNaN would return 0x7e000000 (DECFLOAT_sNaN). */
|
|
1022 /* ------------------------------------------------------------------ */
|
|
1023 Int decFloatGetExponent(const decFloat *df) {
|
|
1024 if (DFISSPECIAL(df)) return DFWORD(df, 0)&0x7e000000;
|
|
1025 return GETEXPUN(df);
|
|
1026 } /* decFloatGetExponent */
|
|
1027
|
|
1028 /* ------------------------------------------------------------------ */
|
|
1029 /* decFloatSetCoefficient -- set coefficient from BCD8 */
|
|
1030 /* */
|
|
1031 /* df is the target decFloat (and source of exponent/special value) */
|
|
1032 /* bcdar holds DECPMAX digits to set the coefficient from, one */
|
|
1033 /* digit in each byte (BCD8 encoding); the first (MSD) is ignored */
|
|
1034 /* if df is a NaN; all are ignored if df is infinite. */
|
|
1035 /* sig is DECFLOAT_Sign to set the sign bit, 0 otherwise */
|
|
1036 /* returns df, which will be canonical */
|
|
1037 /* */
|
|
1038 /* No error is possible, and no status will be set. */
|
|
1039 /* ------------------------------------------------------------------ */
|
|
1040 decFloat * decFloatSetCoefficient(decFloat *df, const uByte *bcdar,
|
|
1041 Int sig) {
|
|
1042 uInt exp; /* for exponent */
|
|
1043 uByte bcdzero[DECPMAX]; /* for infinities */
|
|
1044
|
|
1045 /* Exponent/special code is extracted from df */
|
|
1046 if (DFISSPECIAL(df)) {
|
|
1047 exp=DFWORD(df, 0)&0x7e000000;
|
|
1048 if (DFISINF(df)) {
|
|
1049 memset(bcdzero, 0, DECPMAX);
|
|
1050 return decFloatFromBCD(df, exp, bcdzero, sig);
|
|
1051 }
|
|
1052 }
|
|
1053 else exp=GETEXPUN(df);
|
|
1054 return decFloatFromBCD(df, exp, bcdar, sig);
|
|
1055 } /* decFloatSetCoefficient */
|
|
1056
|
|
1057 /* ------------------------------------------------------------------ */
|
|
1058 /* decFloatSetExponent -- set exponent or special value */
|
|
1059 /* */
|
|
1060 /* df is the target decFloat (and source of coefficient/payload) */
|
|
1061 /* set is the context for reporting status */
|
|
1062 /* exp is the unbiased exponent, q, or a special value in the form */
|
|
1063 /* returned by decFloatGetExponent */
|
|
1064 /* returns df, which will be canonical */
|
|
1065 /* */
|
|
1066 /* No error is possible, but Overflow or Underflow might occur. */
|
|
1067 /* ------------------------------------------------------------------ */
|
|
1068 decFloat * decFloatSetExponent(decFloat *df, decContext *set, Int exp) {
|
|
1069 uByte bcdcopy[DECPMAX]; /* for coefficient */
|
|
1070 bcdnum num; /* work */
|
|
1071 num.exponent=exp;
|
|
1072 num.sign=decFloatGetCoefficient(df, bcdcopy); /* extract coefficient */
|
|
1073 if (DFISSPECIAL(df)) { /* MSD or more needs correcting */
|
|
1074 if (DFISINF(df)) memset(bcdcopy, 0, DECPMAX);
|
|
1075 bcdcopy[0]=0;
|
|
1076 }
|
|
1077 num.msd=bcdcopy;
|
|
1078 num.lsd=bcdcopy+DECPMAX-1;
|
|
1079 return decFinalize(df, &num, set);
|
|
1080 } /* decFloatSetExponent */
|
|
1081
|
|
1082 /* ------------------------------------------------------------------ */
|
|
1083 /* decFloatRadix -- returns the base (10) */
|
|
1084 /* */
|
|
1085 /* df is any decFloat of this format */
|
|
1086 /* ------------------------------------------------------------------ */
|
|
1087 uInt decFloatRadix(const decFloat *df) {
|
|
1088 if (df) return 10; /* to placate compiler */
|
|
1089 return 10;
|
|
1090 } /* decFloatRadix */
|
|
1091
|
36
|
1092 #if (DECCHECK || DECTRACE)
|
0
|
1093 /* ------------------------------------------------------------------ */
|
|
1094 /* decFloatShow -- printf a decFloat in hexadecimal and decimal */
|
|
1095 /* df is the decFloat to show */
|
|
1096 /* tag is a tag string displayed with the number */
|
|
1097 /* */
|
|
1098 /* This is a debug aid; the precise format of the string may change. */
|
|
1099 /* ------------------------------------------------------------------ */
|
|
1100 void decFloatShow(const decFloat *df, const char *tag) {
|
|
1101 char hexbuf[DECBYTES*2+DECBYTES/4+1]; /* NB blank after every fourth */
|
|
1102 char buff[DECSTRING]; /* for value in decimal */
|
|
1103 Int i, j=0;
|
|
1104
|
|
1105 for (i=0; i<DECBYTES; i++) {
|
|
1106 #if DECLITEND
|
|
1107 sprintf(&hexbuf[j], "%02x", df->bytes[DECBYTES-1-i]);
|
|
1108 #else
|
|
1109 sprintf(&hexbuf[j], "%02x", df->bytes[i]);
|
|
1110 #endif
|
|
1111 j+=2;
|
|
1112 /* the next line adds blank (and terminator) after final pair, too */
|
|
1113 if ((i+1)%4==0) {strcpy(&hexbuf[j], " "); j++;}
|
|
1114 }
|
|
1115 decFloatToString(df, buff);
|
|
1116 printf(">%s> %s [big-endian] %s\n", tag, hexbuf, buff);
|
|
1117 return;
|
|
1118 } /* decFloatShow */
|
36
|
1119 #endif
|
0
|
1120
|
|
1121 /* ------------------------------------------------------------------ */
|
|
1122 /* decFloatToBCD -- get sign, exponent, and BCD8 from a decFloat */
|
|
1123 /* */
|
|
1124 /* df is the source decFloat */
|
|
1125 /* exp will be set to the unbiased exponent, q, or to a special */
|
|
1126 /* value in the form returned by decFloatGetExponent */
|
|
1127 /* bcdar is where DECPMAX bytes will be written, one BCD digit in */
|
|
1128 /* each byte (BCD8 encoding); if df is a NaN the first byte will */
|
|
1129 /* be zero, and if it is infinite they will all be zero */
|
|
1130 /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
|
|
1131 /* 0 otherwise) */
|
|
1132 /* */
|
|
1133 /* No error is possible, and no status will be set. */
|
|
1134 /* ------------------------------------------------------------------ */
|
|
1135 Int decFloatToBCD(const decFloat *df, Int *exp, uByte *bcdar) {
|
|
1136 if (DFISINF(df)) {
|
|
1137 memset(bcdar, 0, DECPMAX);
|
|
1138 *exp=DFWORD(df, 0)&0x7e000000;
|
|
1139 }
|
|
1140 else {
|
|
1141 GETCOEFF(df, bcdar); /* use macro */
|
|
1142 if (DFISNAN(df)) {
|
|
1143 bcdar[0]=0; /* MSD needs correcting */
|
|
1144 *exp=DFWORD(df, 0)&0x7e000000;
|
|
1145 }
|
|
1146 else { /* finite */
|
|
1147 *exp=GETEXPUN(df);
|
|
1148 }
|
|
1149 }
|
|
1150 return DFISSIGNED(df);
|
|
1151 } /* decFloatToBCD */
|
|
1152
|
|
1153 /* ------------------------------------------------------------------ */
|
|
1154 /* decFloatToEngString -- conversion to numeric string, engineering */
|
|
1155 /* */
|
|
1156 /* df is the decFloat format number to convert */
|
|
1157 /* string is the string where the result will be laid out */
|
|
1158 /* */
|
|
1159 /* string must be at least DECPMAX+9 characters (the worst case is */
|
|
1160 /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
|
|
1161 /* DECEMAXD<=4); this condition is asserted above */
|
|
1162 /* */
|
|
1163 /* No error is possible, and no status will be set */
|
|
1164 /* ------------------------------------------------------------------ */
|
|
1165 char * decFloatToEngString(const decFloat *df, char *string){
|
|
1166 uInt msd; /* coefficient MSD */
|
|
1167 Int exp; /* exponent top two bits or full */
|
|
1168 uInt comb; /* combination field */
|
|
1169 char *cstart; /* coefficient start */
|
|
1170 char *c; /* output pointer in string */
|
|
1171 char *s, *t; /* .. (source, target) */
|
|
1172 Int pre, e; /* work */
|
|
1173 const uByte *u; /* .. */
|
|
1174
|
|
1175 /* Source words; macro handles endianness */
|
|
1176 uInt sourhi=DFWORD(df, 0); /* word with sign */
|
|
1177 #if DECPMAX==16
|
|
1178 uInt sourlo=DFWORD(df, 1);
|
|
1179 #elif DECPMAX==34
|
|
1180 uInt sourmh=DFWORD(df, 1);
|
|
1181 uInt sourml=DFWORD(df, 2);
|
|
1182 uInt sourlo=DFWORD(df, 3);
|
|
1183 #endif
|
|
1184
|
|
1185 c=string; /* where result will go */
|
|
1186 if (((Int)sourhi)<0) *c++='-'; /* handle sign */
|
|
1187 comb=sourhi>>26; /* sign+combination field */
|
|
1188 msd=DECCOMBMSD[comb]; /* decode the combination field */
|
|
1189 exp=DECCOMBEXP[comb]; /* .. */
|
|
1190
|
|
1191 if (EXPISSPECIAL(exp)) { /* special */
|
|
1192 if (exp==DECFLOAT_Inf) { /* infinity */
|
|
1193 strcpy(c, "Inf");
|
|
1194 strcpy(c+3, "inity");
|
|
1195 return string; /* easy */
|
|
1196 }
|
|
1197 if (sourhi&0x02000000) *c++='s'; /* sNaN */
|
|
1198 strcpy(c, "NaN"); /* complete word */
|
|
1199 c+=3; /* step past */
|
|
1200 /* quick exit if the payload is zero */
|
|
1201 #if DECPMAX==7
|
|
1202 if ((sourhi&0x000fffff)==0) return string;
|
|
1203 #elif DECPMAX==16
|
|
1204 if (sourlo==0 && (sourhi&0x0003ffff)==0) return string;
|
|
1205 #elif DECPMAX==34
|
|
1206 if (sourlo==0 && sourml==0 && sourmh==0
|
|
1207 && (sourhi&0x00003fff)==0) return string;
|
|
1208 #endif
|
|
1209 /* otherwise drop through to add integer; set correct exp etc. */
|
|
1210 exp=0; msd=0; /* setup for following code */
|
|
1211 }
|
|
1212 else { /* complete exponent; top two bits are in place */
|
|
1213 exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */
|
|
1214 }
|
|
1215
|
|
1216 /* convert the digits of the significand to characters */
|
|
1217 cstart=c; /* save start of coefficient */
|
|
1218 if (msd) *c++=(char)('0'+(char)msd); /* non-zero most significant digit */
|
|
1219
|
|
1220 /* Decode the declets. After extracting each declet, it is */
|
|
1221 /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
|
|
1222 /* are the three encoded BCD8 digits followed by a 1-byte length */
|
|
1223 /* (significant digits, except that 000 has length 0). This allows */
|
|
1224 /* us to left-align the first declet with non-zero content, then */
|
|
1225 /* the remaining ones are full 3-char length. Fixed-length copies */
|
|
1226 /* are used because variable-length memcpy causes a subroutine call */
|
|
1227 /* in at least two compilers. (The copies are length 4 for speed */
|
|
1228 /* and are safe because the last item in the array is of length */
|
|
1229 /* three and has the length byte following.) */
|
|
1230 #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
|
|
1231 if (c!=cstart) {UINTAT(c)=UINTAT(u)|CHARMASK; c+=3;} \
|
|
1232 else if (*(u+3)) { \
|
|
1233 UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; c+=*(u+3);}
|
|
1234
|
|
1235 #if DECPMAX==7
|
|
1236 dpd2char(sourhi>>10); /* declet 1 */
|
|
1237 dpd2char(sourhi); /* declet 2 */
|
|
1238
|
|
1239 #elif DECPMAX==16
|
|
1240 dpd2char(sourhi>>8); /* declet 1 */
|
|
1241 dpd2char((sourhi<<2) | (sourlo>>30)); /* declet 2 */
|
|
1242 dpd2char(sourlo>>20); /* declet 3 */
|
|
1243 dpd2char(sourlo>>10); /* declet 4 */
|
|
1244 dpd2char(sourlo); /* declet 5 */
|
|
1245
|
|
1246 #elif DECPMAX==34
|
|
1247 dpd2char(sourhi>>4); /* declet 1 */
|
|
1248 dpd2char((sourhi<<6) | (sourmh>>26)); /* declet 2 */
|
|
1249 dpd2char(sourmh>>16); /* declet 3 */
|
|
1250 dpd2char(sourmh>>6); /* declet 4 */
|
|
1251 dpd2char((sourmh<<4) | (sourml>>28)); /* declet 5 */
|
|
1252 dpd2char(sourml>>18); /* declet 6 */
|
|
1253 dpd2char(sourml>>8); /* declet 7 */
|
|
1254 dpd2char((sourml<<2) | (sourlo>>30)); /* declet 8 */
|
|
1255 dpd2char(sourlo>>20); /* declet 9 */
|
|
1256 dpd2char(sourlo>>10); /* declet 10 */
|
|
1257 dpd2char(sourlo); /* declet 11 */
|
|
1258 #endif
|
|
1259
|
|
1260 if (c==cstart) *c++='0'; /* all zeros, empty -- make "0" */
|
|
1261
|
|
1262 if (exp==0) { /* integer or NaN case -- easy */
|
|
1263 *c='\0'; /* terminate */
|
|
1264 return string;
|
|
1265 }
|
|
1266 /* non-0 exponent */
|
|
1267
|
|
1268 e=0; /* assume no E */
|
|
1269 pre=(Int)(c-cstart)+exp; /* length+exp [c->LSD+1] */
|
|
1270 /* [here, pre-exp is the digits count (==1 for zero)] */
|
|
1271
|
|
1272 if (exp>0 || pre<-5) { /* need exponential form */
|
|
1273 e=pre-1; /* calculate E value */
|
|
1274 pre=1; /* assume one digit before '.' */
|
|
1275 if (e!=0) { /* engineering: may need to adjust */
|
|
1276 Int adj; /* adjustment */
|
|
1277 /* The C remainder operator is undefined for negative numbers, so */
|
|
1278 /* a positive remainder calculation must be used here */
|
|
1279 if (e<0) {
|
|
1280 adj=(-e)%3;
|
|
1281 if (adj!=0) adj=3-adj;
|
|
1282 }
|
|
1283 else { /* e>0 */
|
|
1284 adj=e%3;
|
|
1285 }
|
|
1286 e=e-adj;
|
|
1287 /* if dealing with zero still produce an exponent which is a */
|
|
1288 /* multiple of three, as expected, but there will only be the */
|
|
1289 /* one zero before the E, still. Otherwise note the padding. */
|
|
1290 if (!DFISZERO(df)) pre+=adj;
|
|
1291 else { /* is zero */
|
|
1292 if (adj!=0) { /* 0.00Esnn needed */
|
|
1293 e=e+3;
|
|
1294 pre=-(2-adj);
|
|
1295 }
|
|
1296 } /* zero */
|
|
1297 } /* engineering adjustment */
|
|
1298 } /* exponential form */
|
|
1299 /* printf("e=%ld pre=%ld exp=%ld\n", (LI)e, (LI)pre, (LI)exp); */
|
|
1300
|
|
1301 /* modify the coefficient, adding 0s, '.', and E+nn as needed */
|
|
1302 if (pre>0) { /* ddd.ddd (plain), perhaps with E */
|
|
1303 /* or dd00 padding for engineering */
|
|
1304 char *dotat=cstart+pre;
|
|
1305 if (dotat<c) { /* if embedded dot needed... */
|
|
1306 /* move by fours; there must be space for junk at the end */
|
|
1307 /* because there is still space for exponent */
|
|
1308 s=dotat+ROUNDDOWN4(c-dotat); /* source */
|
|
1309 t=s+1; /* target */
|
|
1310 /* open the gap */
|
|
1311 for (; s>=dotat; s-=4, t-=4) UINTAT(t)=UINTAT(s);
|
|
1312 *dotat='.';
|
|
1313 c++; /* length increased by one */
|
|
1314 } /* need dot? */
|
|
1315 else for (; c<dotat; c++) *c='0'; /* pad for engineering */
|
|
1316 } /* pre>0 */
|
|
1317 else {
|
|
1318 /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (may have
|
|
1319 E, but only for 0.00E+3 kind of case -- with plenty of spare
|
|
1320 space in this case */
|
|
1321 pre=-pre+2; /* gap width, including "0." */
|
|
1322 t=cstart+ROUNDDOWN4(c-cstart)+pre; /* preferred first target point */
|
|
1323 /* backoff if too far to the right */
|
|
1324 if (t>string+DECSTRING-5) t=string+DECSTRING-5; /* adjust to fit */
|
|
1325 /* now shift the entire coefficient to the right, being careful not */
|
|
1326 /* to access to the left of string */
|
|
1327 for (s=t-pre; s>=string; s-=4, t-=4) UINTAT(t)=UINTAT(s);
|
|
1328 /* for Quads and Singles there may be a character or two left... */
|
|
1329 s+=3; /* where next would come from */
|
|
1330 for(; s>=cstart; s--, t--) *(t+3)=*(s);
|
|
1331 /* now have fill 0. through 0.00000; use overlaps to avoid tests */
|
|
1332 if (pre>=4) {
|
|
1333 UINTAT(cstart+pre-4)=UINTAT("0000");
|
|
1334 UINTAT(cstart)=UINTAT("0.00");
|
|
1335 }
|
|
1336 else { /* 2 or 3 */
|
|
1337 *(cstart+pre-1)='0';
|
|
1338 USHORTAT(cstart)=USHORTAT("0.");
|
|
1339 }
|
|
1340 c+=pre; /* to end */
|
|
1341 }
|
|
1342
|
|
1343 /* finally add the E-part, if needed; it will never be 0, and has */
|
|
1344 /* a maximum length of 3 or 4 digits (asserted above) */
|
|
1345 if (e!=0) {
|
|
1346 USHORTAT(c)=USHORTAT("E+"); /* starts with E, assume + */
|
|
1347 c++;
|
|
1348 if (e<0) {
|
|
1349 *c='-'; /* oops, need '-' */
|
|
1350 e=-e; /* uInt, please */
|
|
1351 }
|
|
1352 c++;
|
|
1353 /* Three-character exponents are easy; 4-character a little trickier */
|
|
1354 #if DECEMAXD<=3
|
|
1355 u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
|
|
1356 /* copy fixed 4 characters [is safe], starting at non-zero */
|
|
1357 /* and with character mask to convert BCD to char */
|
|
1358 UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK;
|
|
1359 c+=*(u+3); /* bump pointer appropriately */
|
|
1360 #elif DECEMAXD==4
|
|
1361 if (e<1000) { /* 3 (or fewer) digits case */
|
|
1362 u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
|
|
1363 UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; /* [as above] */
|
|
1364 c+=*(u+3); /* bump pointer appropriately */
|
|
1365 }
|
|
1366 else { /* 4-digits */
|
|
1367 Int thou=((e>>3)*1049)>>17; /* e/1000 */
|
|
1368 Int rem=e-(1000*thou); /* e%1000 */
|
|
1369 *c++=(char)('0'+(char)thou); /* the thousands digit */
|
|
1370 u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */
|
|
1371 UINTAT(c)=UINTAT(u)|CHARMASK; /* copy fixed 3+1 characters [is safe] */
|
|
1372 c+=3; /* bump pointer, always 3 digits */
|
|
1373 }
|
|
1374 #endif
|
|
1375 }
|
|
1376 *c='\0'; /* terminate */
|
|
1377 /*printf("res %s\n", string); */
|
|
1378 return string;
|
|
1379 } /* decFloatToEngString */
|
|
1380
|
|
1381 /* ------------------------------------------------------------------ */
|
|
1382 /* decFloatToPacked -- convert decFloat to Packed decimal + exponent */
|
|
1383 /* */
|
|
1384 /* df is the source decFloat */
|
|
1385 /* exp will be set to the unbiased exponent, q, or to a special */
|
|
1386 /* value in the form returned by decFloatGetExponent */
|
|
1387 /* packed is where DECPMAX nibbles will be written with the sign as */
|
|
1388 /* final nibble (0x0c for +, 0x0d for -); a NaN has a first nibble */
|
|
1389 /* of zero, and an infinity is all zeros. decDouble and decQuad */
|
|
1390 /* have a additional leading zero nibble, leading to result */
|
|
1391 /* lengths of 4, 9, and 18 bytes. */
|
|
1392 /* returns the sign of the coefficient (DECFLOAT_Sign if negative, */
|
|
1393 /* 0 otherwise) */
|
|
1394 /* */
|
|
1395 /* No error is possible, and no status will be set. */
|
|
1396 /* ------------------------------------------------------------------ */
|
|
1397 Int decFloatToPacked(const decFloat *df, Int *exp, uByte *packed) {
|
|
1398 uByte bcdar[DECPMAX+2]; /* work buffer */
|
|
1399 uByte *ip=bcdar, *op=packed; /* work pointers */
|
|
1400 if (DFISINF(df)) {
|
|
1401 memset(bcdar, 0, DECPMAX+2);
|
|
1402 *exp=DECFLOAT_Inf;
|
|
1403 }
|
|
1404 else {
|
|
1405 GETCOEFF(df, bcdar+1); /* use macro */
|
|
1406 if (DFISNAN(df)) {
|
|
1407 bcdar[1]=0; /* MSD needs clearing */
|
|
1408 *exp=DFWORD(df, 0)&0x7e000000;
|
|
1409 }
|
|
1410 else { /* finite */
|
|
1411 *exp=GETEXPUN(df);
|
|
1412 }
|
|
1413 }
|
|
1414 /* now pack; coefficient currently at bcdar+1 */
|
|
1415 #if SINGLE
|
|
1416 ip++; /* ignore first byte */
|
|
1417 #else
|
|
1418 *ip=0; /* need leading zero */
|
|
1419 #endif
|
|
1420 /* set final byte to Packed BCD sign value */
|
|
1421 bcdar[DECPMAX+1]=(DFISSIGNED(df) ? DECPMINUS : DECPPLUS);
|
|
1422 /* pack an even number of bytes... */
|
|
1423 for (; op<packed+((DECPMAX+2)/2); op++, ip+=2) {
|
|
1424 *op=(uByte)((*ip<<4)+*(ip+1));
|
|
1425 }
|
|
1426 return (bcdar[DECPMAX+1]==DECPMINUS ? DECFLOAT_Sign : 0);
|
|
1427 } /* decFloatToPacked */
|
|
1428
|
|
1429 /* ------------------------------------------------------------------ */
|
|
1430 /* decFloatToString -- conversion to numeric string */
|
|
1431 /* */
|
|
1432 /* df is the decFloat format number to convert */
|
|
1433 /* string is the string where the result will be laid out */
|
|
1434 /* */
|
|
1435 /* string must be at least DECPMAX+9 characters (the worst case is */
|
|
1436 /* "-0.00000nnn...nnn\0", which is as long as the exponent form when */
|
|
1437 /* DECEMAXD<=4); this condition is asserted above */
|
|
1438 /* */
|
|
1439 /* No error is possible, and no status will be set */
|
|
1440 /* ------------------------------------------------------------------ */
|
|
1441 char * decFloatToString(const decFloat *df, char *string){
|
|
1442 uInt msd; /* coefficient MSD */
|
|
1443 Int exp; /* exponent top two bits or full */
|
|
1444 uInt comb; /* combination field */
|
|
1445 char *cstart; /* coefficient start */
|
|
1446 char *c; /* output pointer in string */
|
|
1447 char *s, *t; /* .. (source, target) */
|
|
1448 Int pre, e; /* work */
|
|
1449 const uByte *u; /* .. */
|
|
1450
|
|
1451 /* Source words; macro handles endianness */
|
|
1452 uInt sourhi=DFWORD(df, 0); /* word with sign */
|
|
1453 #if DECPMAX==16
|
|
1454 uInt sourlo=DFWORD(df, 1);
|
|
1455 #elif DECPMAX==34
|
|
1456 uInt sourmh=DFWORD(df, 1);
|
|
1457 uInt sourml=DFWORD(df, 2);
|
|
1458 uInt sourlo=DFWORD(df, 3);
|
|
1459 #endif
|
|
1460
|
|
1461 c=string; /* where result will go */
|
|
1462 if (((Int)sourhi)<0) *c++='-'; /* handle sign */
|
|
1463 comb=sourhi>>26; /* sign+combination field */
|
|
1464 msd=DECCOMBMSD[comb]; /* decode the combination field */
|
|
1465 exp=DECCOMBEXP[comb]; /* .. */
|
|
1466
|
|
1467 if (EXPISSPECIAL(exp)) { /* special */
|
|
1468 if (exp==DECFLOAT_Inf) { /* infinity */
|
|
1469 strcpy(c, "Infinity");
|
|
1470 return string; /* easy */
|
|
1471 }
|
|
1472 if (sourhi&0x02000000) *c++='s'; /* sNaN */
|
|
1473 strcpy(c, "NaN"); /* complete word */
|
|
1474 c+=3; /* step past */
|
|
1475 /* quick exit if the payload is zero */
|
|
1476 #if DECPMAX==7
|
|
1477 if ((sourhi&0x000fffff)==0) return string;
|
|
1478 #elif DECPMAX==16
|
|
1479 if (sourlo==0 && (sourhi&0x0003ffff)==0) return string;
|
|
1480 #elif DECPMAX==34
|
|
1481 if (sourlo==0 && sourml==0 && sourmh==0
|
|
1482 && (sourhi&0x00003fff)==0) return string;
|
|
1483 #endif
|
|
1484 /* otherwise drop through to add integer; set correct exp etc. */
|
|
1485 exp=0; msd=0; /* setup for following code */
|
|
1486 }
|
|
1487 else { /* complete exponent; top two bits are in place */
|
|
1488 exp+=GETECON(df)-DECBIAS; /* .. + continuation and unbias */
|
|
1489 }
|
|
1490
|
|
1491 /* convert the digits of the significand to characters */
|
|
1492 cstart=c; /* save start of coefficient */
|
|
1493 if (msd) *c++=(char)('0'+(char)msd); /* non-zero most significant digit */
|
|
1494
|
|
1495 /* Decode the declets. After extracting each declet, it is */
|
|
1496 /* decoded to a 4-uByte sequence by table lookup; the four uBytes */
|
|
1497 /* are the three encoded BCD8 digits followed by a 1-byte length */
|
|
1498 /* (significant digits, except that 000 has length 0). This allows */
|
|
1499 /* us to left-align the first declet with non-zero content, then */
|
|
1500 /* the remaining ones are full 3-char length. Fixed-length copies */
|
|
1501 /* are used because variable-length memcpy causes a subroutine call */
|
|
1502 /* in at least two compilers. (The copies are length 4 for speed */
|
|
1503 /* and are safe because the last item in the array is of length */
|
|
1504 /* three and has the length byte following.) */
|
|
1505 #define dpd2char(dpdin) u=&DPD2BCD8[((dpdin)&0x3ff)*4]; \
|
|
1506 if (c!=cstart) {UINTAT(c)=UINTAT(u)|CHARMASK; c+=3;} \
|
|
1507 else if (*(u+3)) { \
|
|
1508 UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; c+=*(u+3);}
|
|
1509
|
|
1510 #if DECPMAX==7
|
|
1511 dpd2char(sourhi>>10); /* declet 1 */
|
|
1512 dpd2char(sourhi); /* declet 2 */
|
|
1513
|
|
1514 #elif DECPMAX==16
|
|
1515 dpd2char(sourhi>>8); /* declet 1 */
|
|
1516 dpd2char((sourhi<<2) | (sourlo>>30)); /* declet 2 */
|
|
1517 dpd2char(sourlo>>20); /* declet 3 */
|
|
1518 dpd2char(sourlo>>10); /* declet 4 */
|
|
1519 dpd2char(sourlo); /* declet 5 */
|
|
1520
|
|
1521 #elif DECPMAX==34
|
|
1522 dpd2char(sourhi>>4); /* declet 1 */
|
|
1523 dpd2char((sourhi<<6) | (sourmh>>26)); /* declet 2 */
|
|
1524 dpd2char(sourmh>>16); /* declet 3 */
|
|
1525 dpd2char(sourmh>>6); /* declet 4 */
|
|
1526 dpd2char((sourmh<<4) | (sourml>>28)); /* declet 5 */
|
|
1527 dpd2char(sourml>>18); /* declet 6 */
|
|
1528 dpd2char(sourml>>8); /* declet 7 */
|
|
1529 dpd2char((sourml<<2) | (sourlo>>30)); /* declet 8 */
|
|
1530 dpd2char(sourlo>>20); /* declet 9 */
|
|
1531 dpd2char(sourlo>>10); /* declet 10 */
|
|
1532 dpd2char(sourlo); /* declet 11 */
|
|
1533 #endif
|
|
1534
|
|
1535 if (c==cstart) *c++='0'; /* all zeros, empty -- make "0" */
|
|
1536
|
|
1537 /*[This fast path is valid but adds 3-5 cycles to worst case length] */
|
|
1538 /*if (exp==0) { // integer or NaN case -- easy */
|
|
1539 /* *c='\0'; // terminate */
|
|
1540 /* return string; */
|
|
1541 /* } */
|
|
1542
|
|
1543 e=0; /* assume no E */
|
|
1544 pre=(Int)(c-cstart)+exp; /* length+exp [c->LSD+1] */
|
|
1545 /* [here, pre-exp is the digits count (==1 for zero)] */
|
|
1546
|
|
1547 if (exp>0 || pre<-5) { /* need exponential form */
|
|
1548 e=pre-1; /* calculate E value */
|
|
1549 pre=1; /* assume one digit before '.' */
|
|
1550 } /* exponential form */
|
|
1551
|
|
1552 /* modify the coefficient, adding 0s, '.', and E+nn as needed */
|
|
1553 if (pre>0) { /* ddd.ddd (plain), perhaps with E */
|
|
1554 char *dotat=cstart+pre;
|
|
1555 if (dotat<c) { /* if embedded dot needed... */
|
|
1556 /* move by fours; there must be space for junk at the end */
|
|
1557 /* because there is still space for exponent */
|
|
1558 s=dotat+ROUNDDOWN4(c-dotat); /* source */
|
|
1559 t=s+1; /* target */
|
|
1560 /* open the gap */
|
|
1561 for (; s>=dotat; s-=4, t-=4) UINTAT(t)=UINTAT(s);
|
|
1562 *dotat='.';
|
|
1563 c++; /* length increased by one */
|
|
1564 } /* need dot? */
|
|
1565
|
|
1566 /* finally add the E-part, if needed; it will never be 0, and has */
|
|
1567 /* a maximum length of 3 or 4 digits (asserted above) */
|
|
1568 if (e!=0) {
|
|
1569 USHORTAT(c)=USHORTAT("E+"); /* starts with E, assume + */
|
|
1570 c++;
|
|
1571 if (e<0) {
|
|
1572 *c='-'; /* oops, need '-' */
|
|
1573 e=-e; /* uInt, please */
|
|
1574 }
|
|
1575 c++;
|
|
1576 /* Three-character exponents are easy; 4-character a little trickier */
|
|
1577 #if DECEMAXD<=3
|
|
1578 u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
|
|
1579 /* copy fixed 4 characters [is safe], starting at non-zero */
|
|
1580 /* and with character mask to convert BCD to char */
|
|
1581 UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK;
|
|
1582 c+=*(u+3); /* bump pointer appropriately */
|
|
1583 #elif DECEMAXD==4
|
|
1584 if (e<1000) { /* 3 (or fewer) digits case */
|
|
1585 u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
|
|
1586 UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; /* [as above] */
|
|
1587 c+=*(u+3); /* bump pointer appropriately */
|
|
1588 }
|
|
1589 else { /* 4-digits */
|
|
1590 Int thou=((e>>3)*1049)>>17; /* e/1000 */
|
|
1591 Int rem=e-(1000*thou); /* e%1000 */
|
|
1592 *c++=(char)('0'+(char)thou); /* the thousands digit */
|
|
1593 u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */
|
|
1594 UINTAT(c)=UINTAT(u)|CHARMASK; /* copy fixed 3+1 characters [is safe] */
|
|
1595 c+=3; /* bump pointer, always 3 digits */
|
|
1596 }
|
|
1597 #endif
|
|
1598 }
|
|
1599 *c='\0'; /* add terminator */
|
|
1600 /*printf("res %s\n", string); */
|
|
1601 return string;
|
|
1602 } /* pre>0 */
|
|
1603
|
|
1604 /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
|
|
1605 /* Surprisingly, this is close to being the worst-case path, so the */
|
|
1606 /* shift is done by fours; this is a little tricky because the */
|
|
1607 /* rightmost character to be written must not be beyond where the */
|
|
1608 /* rightmost terminator could be -- so backoff to not touch */
|
|
1609 /* terminator position if need be (this can make exact alignments */
|
|
1610 /* for full Doubles, but in some cases needs care not to access too */
|
|
1611 /* far to the left) */
|
|
1612
|
|
1613 pre=-pre+2; /* gap width, including "0." */
|
|
1614 t=cstart+ROUNDDOWN4(c-cstart)+pre; /* preferred first target point */
|
|
1615 /* backoff if too far to the right */
|
|
1616 if (t>string+DECSTRING-5) t=string+DECSTRING-5; /* adjust to fit */
|
|
1617 /* now shift the entire coefficient to the right, being careful not */
|
|
1618 /* to access to the left of string */
|
|
1619 for (s=t-pre; s>=string; s-=4, t-=4) UINTAT(t)=UINTAT(s);
|
|
1620 /* for Quads and Singles there may be a character or two left... */
|
|
1621 s+=3; /* where next would come from */
|
|
1622 for(; s>=cstart; s--, t--) *(t+3)=*(s);
|
|
1623 /* now have fill 0. through 0.00000; use overlaps to avoid tests */
|
|
1624 if (pre>=4) {
|
|
1625 UINTAT(cstart+pre-4)=UINTAT("0000");
|
|
1626 UINTAT(cstart)=UINTAT("0.00");
|
|
1627 }
|
|
1628 else { /* 2 or 3 */
|
|
1629 *(cstart+pre-1)='0';
|
|
1630 USHORTAT(cstart)=USHORTAT("0.");
|
|
1631 }
|
|
1632 *(c+pre)='\0'; /* terminate */
|
|
1633 return string;
|
|
1634 } /* decFloatToString */
|
|
1635
|
|
1636 /* ------------------------------------------------------------------ */
|
|
1637 /* decFloatToWider -- conversion to next-wider format */
|
|
1638 /* */
|
|
1639 /* source is the decFloat format number which gets the result of */
|
|
1640 /* the conversion */
|
|
1641 /* wider is the decFloatWider format number which will be narrowed */
|
|
1642 /* returns wider */
|
|
1643 /* */
|
|
1644 /* Widening is always exact; no status is set (sNaNs are copied and */
|
|
1645 /* do not signal). The result will be canonical if the source is, */
|
|
1646 /* and may or may not be if the source is not. */
|
|
1647 /* ------------------------------------------------------------------ */
|
|
1648 /* widening is not possible for decQuad format numbers; simply omit */
|
|
1649 #if !QUAD
|
|
1650 decFloatWider * decFloatToWider(const decFloat *source, decFloatWider *wider) {
|
|
1651 uInt msd;
|
|
1652
|
|
1653 /* Construct and copy the sign word */
|
|
1654 if (DFISSPECIAL(source)) {
|
|
1655 /* copy sign, combination, and first bit of exponent (sNaN selector) */
|
|
1656 DFWWORD(wider, 0)=DFWORD(source, 0)&0xfe000000;
|
|
1657 msd=0;
|
|
1658 }
|
|
1659 else { /* is finite number */
|
|
1660 uInt exp=GETEXPUN(source)+DECWBIAS; /* get unbiased exponent and rebias */
|
|
1661 uInt code=(exp>>DECWECONL)<<29; /* set two bits of exp [msd=0] */
|
|
1662 code|=(exp<<(32-6-DECWECONL)) & 0x03ffffff; /* add exponent continuation */
|
|
1663 code|=DFWORD(source, 0)&0x80000000; /* add sign */
|
|
1664 DFWWORD(wider, 0)=code; /* .. and place top word in wider */
|
|
1665 msd=GETMSD(source); /* get source coefficient MSD [0-9] */
|
|
1666 }
|
|
1667 /* Copy the coefficient and clear any 'unused' words to left */
|
|
1668 #if SINGLE
|
|
1669 DFWWORD(wider, 1)=(DFWORD(source, 0)&0x000fffff)|(msd<<20);
|
|
1670 #elif DOUBLE
|
|
1671 DFWWORD(wider, 2)=(DFWORD(source, 0)&0x0003ffff)|(msd<<18);
|
|
1672 DFWWORD(wider, 3)=DFWORD(source, 1);
|
|
1673 DFWWORD(wider, 1)=0;
|
|
1674 #endif
|
|
1675 return wider;
|
|
1676 } /* decFloatToWider */
|
|
1677 #endif
|
|
1678
|
|
1679 /* ------------------------------------------------------------------ */
|
|
1680 /* decFloatVersion -- return package version string */
|
|
1681 /* */
|
|
1682 /* returns a constant string describing this package */
|
|
1683 /* ------------------------------------------------------------------ */
|
|
1684 const char *decFloatVersion(void) {
|
|
1685 return DECVERSION;
|
|
1686 } /* decFloatVersion */
|
|
1687
|
|
1688 /* ------------------------------------------------------------------ */
|
|
1689 /* decFloatZero -- set to canonical (integer) zero */
|
|
1690 /* */
|
|
1691 /* df is the decFloat format number to integer +0 (q=0, c=+0) */
|
|
1692 /* returns df */
|
|
1693 /* */
|
|
1694 /* No error is possible, and no status can be set. */
|
|
1695 /* ------------------------------------------------------------------ */
|
|
1696 decFloat * decFloatZero(decFloat *df){
|
|
1697 DFWORD(df, 0)=ZEROWORD; /* set appropriate top word */
|
|
1698 #if DOUBLE || QUAD
|
|
1699 DFWORD(df, 1)=0;
|
|
1700 #if QUAD
|
|
1701 DFWORD(df, 2)=0;
|
|
1702 DFWORD(df, 3)=0;
|
|
1703 #endif
|
|
1704 #endif
|
|
1705 /* decFloatShow(df, "zero"); */
|
|
1706 return df;
|
|
1707 } /* decFloatZero */
|
|
1708
|
|
1709 /* ------------------------------------------------------------------ */
|
|
1710 /* Private generic function (not format-specific) for development use */
|
|
1711 /* ------------------------------------------------------------------ */
|
|
1712 /* This is included once only, for all to use */
|
|
1713 #if QUAD && (DECCHECK || DECTRACE)
|
|
1714 /* ---------------------------------------------------------------- */
|
|
1715 /* decShowNum -- display bcd8 number in debug form */
|
|
1716 /* */
|
|
1717 /* num is the bcdnum to display */
|
|
1718 /* tag is a string to label the display */
|
|
1719 /* ---------------------------------------------------------------- */
|
|
1720 void decShowNum(const bcdnum *num, const char *tag) {
|
|
1721 const char *csign="+"; /* sign character */
|
|
1722 uByte *ub; /* work */
|
|
1723 if (num->sign==DECFLOAT_Sign) csign="-";
|
|
1724
|
|
1725 printf(">%s> ", tag);
|
|
1726 if (num->exponent==DECFLOAT_Inf) printf("%sInfinity", csign);
|
|
1727 else if (num->exponent==DECFLOAT_qNaN) printf("%sqNaN", csign);
|
|
1728 else if (num->exponent==DECFLOAT_sNaN) printf("%ssNaN", csign);
|
|
1729 else { /* finite */
|
|
1730 char qbuf[10]; /* for right-aligned q */
|
|
1731 char *c; /* work */
|
|
1732 const uByte *u; /* .. */
|
|
1733 Int e=num->exponent; /* .. exponent */
|
|
1734 strcpy(qbuf, "q=");
|
|
1735 c=&qbuf[2]; /* where exponent will go */
|
|
1736 /* lay out the exponent */
|
|
1737 if (e<0) {
|
|
1738 *c++='-'; /* add '-' */
|
|
1739 e=-e; /* uInt, please */
|
|
1740 }
|
|
1741 #if DECEMAXD>4
|
|
1742 #error Exponent form is too long for ShowNum to lay out
|
|
1743 #endif
|
|
1744 if (e==0) *c++='0'; /* 0-length case */
|
|
1745 else if (e<1000) { /* 3 (or fewer) digits case */
|
|
1746 u=&BIN2BCD8[e*4]; /* -> 3 digits + length byte */
|
|
1747 UINTAT(c)=UINTAT(u+3-*(u+3))|CHARMASK; /* [as above] */
|
|
1748 c+=*(u+3); /* bump pointer appropriately */
|
|
1749 }
|
|
1750 else { /* 4-digits */
|
|
1751 Int thou=((e>>3)*1049)>>17; /* e/1000 */
|
|
1752 Int rem=e-(1000*thou); /* e%1000 */
|
|
1753 *c++=(char)('0'+(char)thou); /* the thousands digit */
|
|
1754 u=&BIN2BCD8[rem*4]; /* -> 3 digits + length byte */
|
|
1755 UINTAT(c)=UINTAT(u)|CHARMASK; /* copy fixed 3+1 characters [is safe] */
|
|
1756 c+=3; /* bump pointer, always 3 digits */
|
|
1757 }
|
|
1758 *c='\0'; /* add terminator */
|
|
1759 printf("%7s c=%s", qbuf, csign);
|
|
1760 }
|
|
1761
|
|
1762 if (!EXPISSPECIAL(num->exponent) || num->msd!=num->lsd || *num->lsd!=0) {
|
|
1763 for (ub=num->msd; ub<=num->lsd; ub++) { /* coefficient... */
|
|
1764 printf("%1x", *ub);
|
|
1765 if ((num->lsd-ub)%3==0 && ub!=num->lsd) printf(" "); /* 4-space */
|
|
1766 }
|
|
1767 }
|
|
1768 printf("\n");
|
|
1769 } /* decShowNum */
|
|
1770 #endif
|