merge from gcc
[deliverable/binutils-gdb.git] / libdecnumber / dpd / decimal64.c
1 /* Decimal 64-bit format module for the decNumber C Library.
2 Copyright (C) 2005, 2007 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 2, or (at your option) any later
10 version.
11
12 In addition to the permissions in the GNU General Public License,
13 the Free Software Foundation gives you unlimited permission to link
14 the compiled version of this file into combinations with other
15 programs, and to distribute those combinations without any
16 restriction coming from the use of this file. (The General Public
17 License restrictions do apply in other respects; for example, they
18 cover modification of the file, and distribution when not linked
19 into a combine executable.)
20
21 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22 WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with GCC; see the file COPYING. If not, write to the Free
28 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 02110-1301, USA. */
30
31 /* ------------------------------------------------------------------ */
32 /* Decimal 64-bit format module */
33 /* ------------------------------------------------------------------ */
34 /* This module comprises the routines for decimal64 format numbers. */
35 /* Conversions are supplied to and from decNumber and String. */
36 /* */
37 /* This is used when decNumber provides operations, either for all */
38 /* operations or as a proxy between decNumber and decSingle. */
39 /* */
40 /* Error handling is the same as decNumber (qv.). */
41 /* ------------------------------------------------------------------ */
42 #include <string.h> /* [for memset/memcpy] */
43 #include <stdio.h> /* [for printf] */
44
45 #include "dconfig.h" /* GCC definitions */
46 #define DECNUMDIGITS 16 /* make decNumbers with space for 16 */
47 #include "decNumber.h" /* base number library */
48 #include "decNumberLocal.h" /* decNumber local types, etc. */
49 #include "decimal64.h" /* our primary include */
50
51 /* Utility routines and tables [in decimal64.c]; externs for C++ */
52 extern const uInt COMBEXP[32], COMBMSD[32];
53 extern const uShort DPD2BIN[1024];
54 extern const uShort BIN2DPD[1000];
55 extern const uByte BIN2CHAR[4001];
56
57 extern void decDigitsFromDPD(decNumber *, const uInt *, Int);
58 extern void decDigitsToDPD(const decNumber *, uInt *, Int);
59
60 #if DECTRACE || DECCHECK
61 void decimal64Show(const decimal64 *); /* for debug */
62 extern void decNumberShow(const decNumber *); /* .. */
63 #endif
64
65 /* Useful macro */
66 /* Clear a structure (e.g., a decNumber) */
67 #define DEC_clear(d) memset(d, 0, sizeof(*d))
68
69 /* define and include the tables to use for conversions */
70 #define DEC_BIN2CHAR 1
71 #define DEC_DPD2BIN 1
72 #define DEC_BIN2DPD 1 /* used for all sizes */
73 #include "decDPD.h" /* lookup tables */
74
75 /* ------------------------------------------------------------------ */
76 /* decimal64FromNumber -- convert decNumber to decimal64 */
77 /* */
78 /* ds is the target decimal64 */
79 /* dn is the source number (assumed valid) */
80 /* set is the context, used only for reporting errors */
81 /* */
82 /* The set argument is used only for status reporting and for the */
83 /* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */
84 /* digits or an overflow is detected). If the exponent is out of the */
85 /* valid range then Overflow or Underflow will be raised. */
86 /* After Underflow a subnormal result is possible. */
87 /* */
88 /* DEC_Clamped is set if the number has to be 'folded down' to fit, */
89 /* by reducing its exponent and multiplying the coefficient by a */
90 /* power of ten, or if the exponent on a zero had to be clamped. */
91 /* ------------------------------------------------------------------ */
92 decimal64 * decimal64FromNumber(decimal64 *d64, const decNumber *dn,
93 decContext *set) {
94 uInt status=0; /* status accumulator */
95 Int ae; /* adjusted exponent */
96 decNumber dw; /* work */
97 decContext dc; /* .. */
98 uInt comb, exp; /* .. */
99 uInt uiwork; /* for macros */
100 uInt targar[2]={0, 0}; /* target 64-bit */
101 #define targhi targar[1] /* name the word with the sign */
102 #define targlo targar[0] /* and the other */
103
104 /* If the number has too many digits, or the exponent could be */
105 /* out of range then reduce the number under the appropriate */
106 /* constraints. This could push the number to Infinity or zero, */
107 /* so this check and rounding must be done before generating the */
108 /* decimal64] */
109 ae=dn->exponent+dn->digits-1; /* [0 if special] */
110 if (dn->digits>DECIMAL64_Pmax /* too many digits */
111 || ae>DECIMAL64_Emax /* likely overflow */
112 || ae<DECIMAL64_Emin) { /* likely underflow */
113 decContextDefault(&dc, DEC_INIT_DECIMAL64); /* [no traps] */
114 dc.round=set->round; /* use supplied rounding */
115 decNumberPlus(&dw, dn, &dc); /* (round and check) */
116 /* [this changes -0 to 0, so enforce the sign...] */
117 dw.bits|=dn->bits&DECNEG;
118 status=dc.status; /* save status */
119 dn=&dw; /* use the work number */
120 } /* maybe out of range */
121
122 if (dn->bits&DECSPECIAL) { /* a special value */
123 if (dn->bits&DECINF) targhi=DECIMAL_Inf<<24;
124 else { /* sNaN or qNaN */
125 if ((*dn->lsu!=0 || dn->digits>1) /* non-zero coefficient */
126 && (dn->digits<DECIMAL64_Pmax)) { /* coefficient fits */
127 decDigitsToDPD(dn, targar, 0);
128 }
129 if (dn->bits&DECNAN) targhi|=DECIMAL_NaN<<24;
130 else targhi|=DECIMAL_sNaN<<24;
131 } /* a NaN */
132 } /* special */
133
134 else { /* is finite */
135 if (decNumberIsZero(dn)) { /* is a zero */
136 /* set and clamp exponent */
137 if (dn->exponent<-DECIMAL64_Bias) {
138 exp=0; /* low clamp */
139 status|=DEC_Clamped;
140 }
141 else {
142 exp=dn->exponent+DECIMAL64_Bias; /* bias exponent */
143 if (exp>DECIMAL64_Ehigh) { /* top clamp */
144 exp=DECIMAL64_Ehigh;
145 status|=DEC_Clamped;
146 }
147 }
148 comb=(exp>>5) & 0x18; /* msd=0, exp top 2 bits .. */
149 }
150 else { /* non-zero finite number */
151 uInt msd; /* work */
152 Int pad=0; /* coefficient pad digits */
153
154 /* the dn is known to fit, but it may need to be padded */
155 exp=(uInt)(dn->exponent+DECIMAL64_Bias); /* bias exponent */
156 if (exp>DECIMAL64_Ehigh) { /* fold-down case */
157 pad=exp-DECIMAL64_Ehigh;
158 exp=DECIMAL64_Ehigh; /* [to maximum] */
159 status|=DEC_Clamped;
160 }
161
162 /* fastpath common case */
163 if (DECDPUN==3 && pad==0) {
164 uInt dpd[6]={0,0,0,0,0,0};
165 uInt i;
166 Int d=dn->digits;
167 for (i=0; d>0; i++, d-=3) dpd[i]=BIN2DPD[dn->lsu[i]];
168 targlo =dpd[0];
169 targlo|=dpd[1]<<10;
170 targlo|=dpd[2]<<20;
171 if (dn->digits>6) {
172 targlo|=dpd[3]<<30;
173 targhi =dpd[3]>>2;
174 targhi|=dpd[4]<<8;
175 }
176 msd=dpd[5]; /* [did not really need conversion] */
177 }
178 else { /* general case */
179 decDigitsToDPD(dn, targar, pad);
180 /* save and clear the top digit */
181 msd=targhi>>18;
182 targhi&=0x0003ffff;
183 }
184
185 /* create the combination field */
186 if (msd>=8) comb=0x18 | ((exp>>7) & 0x06) | (msd & 0x01);
187 else comb=((exp>>5) & 0x18) | msd;
188 }
189 targhi|=comb<<26; /* add combination field .. */
190 targhi|=(exp&0xff)<<18; /* .. and exponent continuation */
191 } /* finite */
192
193 if (dn->bits&DECNEG) targhi|=0x80000000; /* add sign bit */
194
195 /* now write to storage; this is now always endian */
196 if (DECLITEND) {
197 /* lo int then hi */
198 UBFROMUI(d64->bytes, targar[0]);
199 UBFROMUI(d64->bytes+4, targar[1]);
200 }
201 else {
202 /* hi int then lo */
203 UBFROMUI(d64->bytes, targar[1]);
204 UBFROMUI(d64->bytes+4, targar[0]);
205 }
206
207 if (status!=0) decContextSetStatus(set, status); /* pass on status */
208 /* decimal64Show(d64); */
209 return d64;
210 } /* decimal64FromNumber */
211
212 /* ------------------------------------------------------------------ */
213 /* decimal64ToNumber -- convert decimal64 to decNumber */
214 /* d64 is the source decimal64 */
215 /* dn is the target number, with appropriate space */
216 /* No error is possible. */
217 /* ------------------------------------------------------------------ */
218 decNumber * decimal64ToNumber(const decimal64 *d64, decNumber *dn) {
219 uInt msd; /* coefficient MSD */
220 uInt exp; /* exponent top two bits */
221 uInt comb; /* combination field */
222 Int need; /* work */
223 uInt uiwork; /* for macros */
224 uInt sourar[2]; /* source 64-bit */
225 #define sourhi sourar[1] /* name the word with the sign */
226 #define sourlo sourar[0] /* and the lower word */
227
228 /* load source from storage; this is endian */
229 if (DECLITEND) {
230 sourlo=UBTOUI(d64->bytes ); /* directly load the low int */
231 sourhi=UBTOUI(d64->bytes+4); /* then the high int */
232 }
233 else {
234 sourhi=UBTOUI(d64->bytes ); /* directly load the high int */
235 sourlo=UBTOUI(d64->bytes+4); /* then the low int */
236 }
237
238 comb=(sourhi>>26)&0x1f; /* combination field */
239
240 decNumberZero(dn); /* clean number */
241 if (sourhi&0x80000000) dn->bits=DECNEG; /* set sign if negative */
242
243 msd=COMBMSD[comb]; /* decode the combination field */
244 exp=COMBEXP[comb]; /* .. */
245
246 if (exp==3) { /* is a special */
247 if (msd==0) {
248 dn->bits|=DECINF;
249 return dn; /* no coefficient needed */
250 }
251 else if (sourhi&0x02000000) dn->bits|=DECSNAN;
252 else dn->bits|=DECNAN;
253 msd=0; /* no top digit */
254 }
255 else { /* is a finite number */
256 dn->exponent=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias; /* unbiased */
257 }
258
259 /* get the coefficient */
260 sourhi&=0x0003ffff; /* clean coefficient continuation */
261 if (msd) { /* non-zero msd */
262 sourhi|=msd<<18; /* prefix to coefficient */
263 need=6; /* process 6 declets */
264 }
265 else { /* msd=0 */
266 if (!sourhi) { /* top word 0 */
267 if (!sourlo) return dn; /* easy: coefficient is 0 */
268 need=3; /* process at least 3 declets */
269 if (sourlo&0xc0000000) need++; /* process 4 declets */
270 /* [could reduce some more, here] */
271 }
272 else { /* some bits in top word, msd=0 */
273 need=4; /* process at least 4 declets */
274 if (sourhi&0x0003ff00) need++; /* top declet!=0, process 5 */
275 }
276 } /*msd=0 */
277
278 decDigitsFromDPD(dn, sourar, need); /* process declets */
279 return dn;
280 } /* decimal64ToNumber */
281
282
283 /* ------------------------------------------------------------------ */
284 /* to-scientific-string -- conversion to numeric string */
285 /* to-engineering-string -- conversion to numeric string */
286 /* */
287 /* decimal64ToString(d64, string); */
288 /* decimal64ToEngString(d64, string); */
289 /* */
290 /* d64 is the decimal64 format number to convert */
291 /* string is the string where the result will be laid out */
292 /* */
293 /* string must be at least 24 characters */
294 /* */
295 /* No error is possible, and no status can be set. */
296 /* ------------------------------------------------------------------ */
297 char * decimal64ToEngString(const decimal64 *d64, char *string){
298 decNumber dn; /* work */
299 decimal64ToNumber(d64, &dn);
300 decNumberToEngString(&dn, string);
301 return string;
302 } /* decimal64ToEngString */
303
304 char * decimal64ToString(const decimal64 *d64, char *string){
305 uInt msd; /* coefficient MSD */
306 Int exp; /* exponent top two bits or full */
307 uInt comb; /* combination field */
308 char *cstart; /* coefficient start */
309 char *c; /* output pointer in string */
310 const uByte *u; /* work */
311 char *s, *t; /* .. (source, target) */
312 Int dpd; /* .. */
313 Int pre, e; /* .. */
314 uInt uiwork; /* for macros */
315
316 uInt sourar[2]; /* source 64-bit */
317 #define sourhi sourar[1] /* name the word with the sign */
318 #define sourlo sourar[0] /* and the lower word */
319
320 /* load source from storage; this is endian */
321 if (DECLITEND) {
322 sourlo=UBTOUI(d64->bytes ); /* directly load the low int */
323 sourhi=UBTOUI(d64->bytes+4); /* then the high int */
324 }
325 else {
326 sourhi=UBTOUI(d64->bytes ); /* directly load the high int */
327 sourlo=UBTOUI(d64->bytes+4); /* then the low int */
328 }
329
330 c=string; /* where result will go */
331 if (((Int)sourhi)<0) *c++='-'; /* handle sign */
332
333 comb=(sourhi>>26)&0x1f; /* combination field */
334 msd=COMBMSD[comb]; /* decode the combination field */
335 exp=COMBEXP[comb]; /* .. */
336
337 if (exp==3) {
338 if (msd==0) { /* infinity */
339 strcpy(c, "Inf");
340 strcpy(c+3, "inity");
341 return string; /* easy */
342 }
343 if (sourhi&0x02000000) *c++='s'; /* sNaN */
344 strcpy(c, "NaN"); /* complete word */
345 c+=3; /* step past */
346 if (sourlo==0 && (sourhi&0x0003ffff)==0) return string; /* zero payload */
347 /* otherwise drop through to add integer; set correct exp */
348 exp=0; msd=0; /* setup for following code */
349 }
350 else exp=(exp<<8)+((sourhi>>18)&0xff)-DECIMAL64_Bias;
351
352 /* convert 16 digits of significand to characters */
353 cstart=c; /* save start of coefficient */
354 if (msd) *c++='0'+(char)msd; /* non-zero most significant digit */
355
356 /* Now decode the declets. After extracting each one, it is */
357 /* decoded to binary and then to a 4-char sequence by table lookup; */
358 /* the 4-chars are a 1-char length (significant digits, except 000 */
359 /* has length 0). This allows us to left-align the first declet */
360 /* with non-zero content, then remaining ones are full 3-char */
361 /* length. We use fixed-length memcpys because variable-length */
362 /* causes a subroutine call in GCC. (These are length 4 for speed */
363 /* and are safe because the array has an extra terminator byte.) */
364 #define dpd2char u=&BIN2CHAR[DPD2BIN[dpd]*4]; \
365 if (c!=cstart) {memcpy(c, u+1, 4); c+=3;} \
366 else if (*u) {memcpy(c, u+4-*u, 4); c+=*u;}
367
368 dpd=(sourhi>>8)&0x3ff; /* declet 1 */
369 dpd2char;
370 dpd=((sourhi&0xff)<<2) | (sourlo>>30); /* declet 2 */
371 dpd2char;
372 dpd=(sourlo>>20)&0x3ff; /* declet 3 */
373 dpd2char;
374 dpd=(sourlo>>10)&0x3ff; /* declet 4 */
375 dpd2char;
376 dpd=(sourlo)&0x3ff; /* declet 5 */
377 dpd2char;
378
379 if (c==cstart) *c++='0'; /* all zeros -- make 0 */
380
381 if (exp==0) { /* integer or NaN case -- easy */
382 *c='\0'; /* terminate */
383 return string;
384 }
385
386 /* non-0 exponent */
387 e=0; /* assume no E */
388 pre=c-cstart+exp;
389 /* [here, pre-exp is the digits count (==1 for zero)] */
390 if (exp>0 || pre<-5) { /* need exponential form */
391 e=pre-1; /* calculate E value */
392 pre=1; /* assume one digit before '.' */
393 } /* exponential form */
394
395 /* modify the coefficient, adding 0s, '.', and E+nn as needed */
396 s=c-1; /* source (LSD) */
397 if (pre>0) { /* ddd.ddd (plain), perhaps with E */
398 char *dotat=cstart+pre;
399 if (dotat<c) { /* if embedded dot needed... */
400 t=c; /* target */
401 for (; s>=dotat; s--, t--) *t=*s; /* open the gap; leave t at gap */
402 *t='.'; /* insert the dot */
403 c++; /* length increased by one */
404 }
405
406 /* finally add the E-part, if needed; it will never be 0, and has */
407 /* a maximum length of 3 digits */
408 if (e!=0) {
409 *c++='E'; /* starts with E */
410 *c++='+'; /* assume positive */
411 if (e<0) {
412 *(c-1)='-'; /* oops, need '-' */
413 e=-e; /* uInt, please */
414 }
415 u=&BIN2CHAR[e*4]; /* -> length byte */
416 memcpy(c, u+4-*u, 4); /* copy fixed 4 characters [is safe] */
417 c+=*u; /* bump pointer appropriately */
418 }
419 *c='\0'; /* add terminator */
420 /*printf("res %s\n", string); */
421 return string;
422 } /* pre>0 */
423
424 /* -5<=pre<=0: here for plain 0.ddd or 0.000ddd forms (can never have E) */
425 t=c+1-pre;
426 *(t+1)='\0'; /* can add terminator now */
427 for (; s>=cstart; s--, t--) *t=*s; /* shift whole coefficient right */
428 c=cstart;
429 *c++='0'; /* always starts with 0. */
430 *c++='.';
431 for (; pre<0; pre++) *c++='0'; /* add any 0's after '.' */
432 /*printf("res %s\n", string); */
433 return string;
434 } /* decimal64ToString */
435
436 /* ------------------------------------------------------------------ */
437 /* to-number -- conversion from numeric string */
438 /* */
439 /* decimal64FromString(result, string, set); */
440 /* */
441 /* result is the decimal64 format number which gets the result of */
442 /* the conversion */
443 /* *string is the character string which should contain a valid */
444 /* number (which may be a special value) */
445 /* set is the context */
446 /* */
447 /* The context is supplied to this routine is used for error handling */
448 /* (setting of status and traps) and for the rounding mode, only. */
449 /* If an error occurs, the result will be a valid decimal64 NaN. */
450 /* ------------------------------------------------------------------ */
451 decimal64 * decimal64FromString(decimal64 *result, const char *string,
452 decContext *set) {
453 decContext dc; /* work */
454 decNumber dn; /* .. */
455
456 decContextDefault(&dc, DEC_INIT_DECIMAL64); /* no traps, please */
457 dc.round=set->round; /* use supplied rounding */
458
459 decNumberFromString(&dn, string, &dc); /* will round if needed */
460
461 decimal64FromNumber(result, &dn, &dc);
462 if (dc.status!=0) { /* something happened */
463 decContextSetStatus(set, dc.status); /* .. pass it on */
464 }
465 return result;
466 } /* decimal64FromString */
467
468 /* ------------------------------------------------------------------ */
469 /* decimal64IsCanonical -- test whether encoding is canonical */
470 /* d64 is the source decimal64 */
471 /* returns 1 if the encoding of d64 is canonical, 0 otherwise */
472 /* No error is possible. */
473 /* ------------------------------------------------------------------ */
474 uInt decimal64IsCanonical(const decimal64 *d64) {
475 decNumber dn; /* work */
476 decimal64 canon; /* .. */
477 decContext dc; /* .. */
478 decContextDefault(&dc, DEC_INIT_DECIMAL64);
479 decimal64ToNumber(d64, &dn);
480 decimal64FromNumber(&canon, &dn, &dc);/* canon will now be canonical */
481 return memcmp(d64, &canon, DECIMAL64_Bytes)==0;
482 } /* decimal64IsCanonical */
483
484 /* ------------------------------------------------------------------ */
485 /* decimal64Canonical -- copy an encoding, ensuring it is canonical */
486 /* d64 is the source decimal64 */
487 /* result is the target (may be the same decimal64) */
488 /* returns result */
489 /* No error is possible. */
490 /* ------------------------------------------------------------------ */
491 decimal64 * decimal64Canonical(decimal64 *result, const decimal64 *d64) {
492 decNumber dn; /* work */
493 decContext dc; /* .. */
494 decContextDefault(&dc, DEC_INIT_DECIMAL64);
495 decimal64ToNumber(d64, &dn);
496 decimal64FromNumber(result, &dn, &dc);/* result will now be canonical */
497 return result;
498 } /* decimal64Canonical */
499
500 #if DECTRACE || DECCHECK
501 /* Macros for accessing decimal64 fields. These assume the
502 argument is a reference (pointer) to the decimal64 structure,
503 and the decimal64 is in network byte order (big-endian) */
504 /* Get sign */
505 #define decimal64Sign(d) ((unsigned)(d)->bytes[0]>>7)
506
507 /* Get combination field */
508 #define decimal64Comb(d) (((d)->bytes[0] & 0x7c)>>2)
509
510 /* Get exponent continuation [does not remove bias] */
511 #define decimal64ExpCon(d) ((((d)->bytes[0] & 0x03)<<6) \
512 | ((unsigned)(d)->bytes[1]>>2))
513
514 /* Set sign [this assumes sign previously 0] */
515 #define decimal64SetSign(d, b) { \
516 (d)->bytes[0]|=((unsigned)(b)<<7);}
517
518 /* Set exponent continuation [does not apply bias] */
519 /* This assumes range has been checked and exponent previously 0; */
520 /* type of exponent must be unsigned */
521 #define decimal64SetExpCon(d, e) { \
522 (d)->bytes[0]|=(uByte)((e)>>6); \
523 (d)->bytes[1]|=(uByte)(((e)&0x3F)<<2);}
524
525 /* ------------------------------------------------------------------ */
526 /* decimal64Show -- display a decimal64 in hexadecimal [debug aid] */
527 /* d64 -- the number to show */
528 /* ------------------------------------------------------------------ */
529 /* Also shows sign/cob/expconfields extracted */
530 void decimal64Show(const decimal64 *d64) {
531 char buf[DECIMAL64_Bytes*2+1];
532 Int i, j=0;
533
534 if (DECLITEND) {
535 for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
536 sprintf(&buf[j], "%02x", d64->bytes[7-i]);
537 }
538 printf(" D64> %s [S:%d Cb:%02x Ec:%02x] LittleEndian\n", buf,
539 d64->bytes[7]>>7, (d64->bytes[7]>>2)&0x1f,
540 ((d64->bytes[7]&0x3)<<6)| (d64->bytes[6]>>2));
541 }
542 else { /* big-endian */
543 for (i=0; i<DECIMAL64_Bytes; i++, j+=2) {
544 sprintf(&buf[j], "%02x", d64->bytes[i]);
545 }
546 printf(" D64> %s [S:%d Cb:%02x Ec:%02x] BigEndian\n", buf,
547 decimal64Sign(d64), decimal64Comb(d64), decimal64ExpCon(d64));
548 }
549 } /* decimal64Show */
550 #endif
551
552 /* ================================================================== */
553 /* Shared utility routines and tables */
554 /* ================================================================== */
555 /* define and include the conversion tables to use for shared code */
556 #if DECDPUN==3
557 #define DEC_DPD2BIN 1
558 #else
559 #define DEC_DPD2BCD 1
560 #endif
561 #include "decDPD.h" /* lookup tables */
562
563 /* The maximum number of decNumberUnits needed for a working copy of */
564 /* the units array is the ceiling of digits/DECDPUN, where digits is */
565 /* the maximum number of digits in any of the formats for which this */
566 /* is used. decimal128.h must not be included in this module, so, as */
567 /* a very special case, that number is defined as a literal here. */
568 #define DECMAX754 34
569 #define DECMAXUNITS ((DECMAX754+DECDPUN-1)/DECDPUN)
570
571 /* ------------------------------------------------------------------ */
572 /* Combination field lookup tables (uInts to save measurable work) */
573 /* */
574 /* COMBEXP - 2-bit most-significant-bits of exponent */
575 /* [11 if an Infinity or NaN] */
576 /* COMBMSD - 4-bit most-significant-digit */
577 /* [0=Infinity, 1=NaN if COMBEXP=11] */
578 /* */
579 /* Both are indexed by the 5-bit combination field (0-31) */
580 /* ------------------------------------------------------------------ */
581 const uInt COMBEXP[32]={0, 0, 0, 0, 0, 0, 0, 0,
582 1, 1, 1, 1, 1, 1, 1, 1,
583 2, 2, 2, 2, 2, 2, 2, 2,
584 0, 0, 1, 1, 2, 2, 3, 3};
585 const uInt COMBMSD[32]={0, 1, 2, 3, 4, 5, 6, 7,
586 0, 1, 2, 3, 4, 5, 6, 7,
587 0, 1, 2, 3, 4, 5, 6, 7,
588 8, 9, 8, 9, 8, 9, 0, 1};
589
590 /* ------------------------------------------------------------------ */
591 /* decDigitsToDPD -- pack coefficient into DPD form */
592 /* */
593 /* dn is the source number (assumed valid, max DECMAX754 digits) */
594 /* targ is 1, 2, or 4-element uInt array, which the caller must */
595 /* have cleared to zeros */
596 /* shift is the number of 0 digits to add on the right (normally 0) */
597 /* */
598 /* The coefficient must be known small enough to fit. The full */
599 /* coefficient is copied, including the leading 'odd' digit. This */
600 /* digit is retrieved and packed into the combination field by the */
601 /* caller. */
602 /* */
603 /* The target uInts are altered only as necessary to receive the */
604 /* digits of the decNumber. When more than one uInt is needed, they */
605 /* are filled from left to right (that is, the uInt at offset 0 will */
606 /* end up with the least-significant digits). */
607 /* */
608 /* shift is used for 'fold-down' padding. */
609 /* */
610 /* No error is possible. */
611 /* ------------------------------------------------------------------ */
612 #if DECDPUN<=4
613 /* Constant multipliers for divide-by-power-of five using reciprocal */
614 /* multiply, after removing powers of 2 by shifting, and final shift */
615 /* of 17 [we only need up to **4] */
616 static const uInt multies[]={131073, 26215, 5243, 1049, 210};
617 /* QUOT10 -- macro to return the quotient of unit u divided by 10**n */
618 #define QUOT10(u, n) ((((uInt)(u)>>(n))*multies[n])>>17)
619 #endif
620 void decDigitsToDPD(const decNumber *dn, uInt *targ, Int shift) {
621 Int cut; /* work */
622 Int n; /* output bunch counter */
623 Int digits=dn->digits; /* digit countdown */
624 uInt dpd; /* densely packed decimal value */
625 uInt bin; /* binary value 0-999 */
626 uInt *uout=targ; /* -> current output uInt */
627 uInt uoff=0; /* -> current output offset [from right] */
628 const Unit *inu=dn->lsu; /* -> current input unit */
629 Unit uar[DECMAXUNITS]; /* working copy of units, iff shifted */
630 #if DECDPUN!=3 /* not fast path */
631 Unit in; /* current unit */
632 #endif
633
634 if (shift!=0) { /* shift towards most significant required */
635 /* shift the units array to the left by pad digits and copy */
636 /* [this code is a special case of decShiftToMost, which could */
637 /* be used instead if exposed and the array were copied first] */
638 const Unit *source; /* .. */
639 Unit *target, *first; /* .. */
640 uInt next=0; /* work */
641
642 source=dn->lsu+D2U(digits)-1; /* where msu comes from */
643 target=uar+D2U(digits)-1+D2U(shift);/* where upper part of first cut goes */
644 cut=DECDPUN-MSUDIGITS(shift); /* where to slice */
645 if (cut==0) { /* unit-boundary case */
646 for (; source>=dn->lsu; source--, target--) *target=*source;
647 }
648 else {
649 first=uar+D2U(digits+shift)-1; /* where msu will end up */
650 for (; source>=dn->lsu; source--, target--) {
651 /* split the source Unit and accumulate remainder for next */
652 #if DECDPUN<=4
653 uInt quot=QUOT10(*source, cut);
654 uInt rem=*source-quot*DECPOWERS[cut];
655 next+=quot;
656 #else
657 uInt rem=*source%DECPOWERS[cut];
658 next+=*source/DECPOWERS[cut];
659 #endif
660 if (target<=first) *target=(Unit)next; /* write to target iff valid */
661 next=rem*DECPOWERS[DECDPUN-cut]; /* save remainder for next Unit */
662 }
663 } /* shift-move */
664 /* propagate remainder to one below and clear the rest */
665 for (; target>=uar; target--) {
666 *target=(Unit)next;
667 next=0;
668 }
669 digits+=shift; /* add count (shift) of zeros added */
670 inu=uar; /* use units in working array */
671 }
672
673 /* now densely pack the coefficient into DPD declets */
674
675 #if DECDPUN!=3 /* not fast path */
676 in=*inu; /* current unit */
677 cut=0; /* at lowest digit */
678 bin=0; /* [keep compiler quiet] */
679 #endif
680
681 for(n=0; digits>0; n++) { /* each output bunch */
682 #if DECDPUN==3 /* fast path, 3-at-a-time */
683 bin=*inu; /* 3 digits ready for convert */
684 digits-=3; /* [may go negative] */
685 inu++; /* may need another */
686
687 #else /* must collect digit-by-digit */
688 Unit dig; /* current digit */
689 Int j; /* digit-in-declet count */
690 for (j=0; j<3; j++) {
691 #if DECDPUN<=4
692 Unit temp=(Unit)((uInt)(in*6554)>>16);
693 dig=(Unit)(in-X10(temp));
694 in=temp;
695 #else
696 dig=in%10;
697 in=in/10;
698 #endif
699 if (j==0) bin=dig;
700 else if (j==1) bin+=X10(dig);
701 else /* j==2 */ bin+=X100(dig);
702 digits--;
703 if (digits==0) break; /* [also protects *inu below] */
704 cut++;
705 if (cut==DECDPUN) {inu++; in=*inu; cut=0;}
706 }
707 #endif
708 /* here there are 3 digits in bin, or have used all input digits */
709
710 dpd=BIN2DPD[bin];
711
712 /* write declet to uInt array */
713 *uout|=dpd<<uoff;
714 uoff+=10;
715 if (uoff<32) continue; /* no uInt boundary cross */
716 uout++;
717 uoff-=32;
718 *uout|=dpd>>(10-uoff); /* collect top bits */
719 } /* n declets */
720 return;
721 } /* decDigitsToDPD */
722
723 /* ------------------------------------------------------------------ */
724 /* decDigitsFromDPD -- unpack a format's coefficient */
725 /* */
726 /* dn is the target number, with 7, 16, or 34-digit space. */
727 /* sour is a 1, 2, or 4-element uInt array containing only declets */
728 /* declets is the number of (right-aligned) declets in sour to */
729 /* be processed. This may be 1 more than the obvious number in */
730 /* a format, as any top digit is prefixed to the coefficient */
731 /* continuation field. It also may be as small as 1, as the */
732 /* caller may pre-process leading zero declets. */
733 /* */
734 /* When doing the 'extra declet' case care is taken to avoid writing */
735 /* extra digits when there are leading zeros, as these could overflow */
736 /* the units array when DECDPUN is not 3. */
737 /* */
738 /* The target uInts are used only as necessary to process declets */
739 /* declets into the decNumber. When more than one uInt is needed, */
740 /* they are used from left to right (that is, the uInt at offset 0 */
741 /* provides the least-significant digits). */
742 /* */
743 /* dn->digits is set, but not the sign or exponent. */
744 /* No error is possible [the redundant 888 codes are allowed]. */
745 /* ------------------------------------------------------------------ */
746 void decDigitsFromDPD(decNumber *dn, const uInt *sour, Int declets) {
747
748 uInt dpd; /* collector for 10 bits */
749 Int n; /* counter */
750 Unit *uout=dn->lsu; /* -> current output unit */
751 Unit *last=uout; /* will be unit containing msd */
752 const uInt *uin=sour; /* -> current input uInt */
753 uInt uoff=0; /* -> current input offset [from right] */
754
755 #if DECDPUN!=3
756 uInt bcd; /* BCD result */
757 uInt nibble; /* work */
758 Unit out=0; /* accumulator */
759 Int cut=0; /* power of ten in current unit */
760 #endif
761 #if DECDPUN>4
762 uInt const *pow; /* work */
763 #endif
764
765 /* Expand the densely-packed integer, right to left */
766 for (n=declets-1; n>=0; n--) { /* count down declets of 10 bits */
767 dpd=*uin>>uoff;
768 uoff+=10;
769 if (uoff>32) { /* crossed uInt boundary */
770 uin++;
771 uoff-=32;
772 dpd|=*uin<<(10-uoff); /* get waiting bits */
773 }
774 dpd&=0x3ff; /* clear uninteresting bits */
775
776 #if DECDPUN==3
777 if (dpd==0) *uout=0;
778 else {
779 *uout=DPD2BIN[dpd]; /* convert 10 bits to binary 0-999 */
780 last=uout; /* record most significant unit */
781 }
782 uout++;
783 } /* n */
784
785 #else /* DECDPUN!=3 */
786 if (dpd==0) { /* fastpath [e.g., leading zeros] */
787 /* write out three 0 digits (nibbles); out may have digit(s) */
788 cut++;
789 if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
790 if (n==0) break; /* [as below, works even if MSD=0] */
791 cut++;
792 if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
793 cut++;
794 if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
795 continue;
796 }
797
798 bcd=DPD2BCD[dpd]; /* convert 10 bits to 12 bits BCD */
799
800 /* now accumulate the 3 BCD nibbles into units */
801 nibble=bcd & 0x00f;
802 if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
803 cut++;
804 if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
805 bcd>>=4;
806
807 /* if this is the last declet and the remaining nibbles in bcd */
808 /* are 00 then process no more nibbles, because this could be */
809 /* the 'odd' MSD declet and writing any more Units would then */
810 /* overflow the unit array */
811 if (n==0 && !bcd) break;
812
813 nibble=bcd & 0x00f;
814 if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
815 cut++;
816 if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
817 bcd>>=4;
818
819 nibble=bcd & 0x00f;
820 if (nibble) out=(Unit)(out+nibble*DECPOWERS[cut]);
821 cut++;
822 if (cut==DECDPUN) {*uout=out; if (out) {last=uout; out=0;} uout++; cut=0;}
823 } /* n */
824 if (cut!=0) { /* some more left over */
825 *uout=out; /* write out final unit */
826 if (out) last=uout; /* and note if non-zero */
827 }
828 #endif
829
830 /* here, last points to the most significant unit with digits; */
831 /* inspect it to get the final digits count -- this is essentially */
832 /* the same code as decGetDigits in decNumber.c */
833 dn->digits=(last-dn->lsu)*DECDPUN+1; /* floor of digits, plus */
834 /* must be at least 1 digit */
835 #if DECDPUN>1
836 if (*last<10) return; /* common odd digit or 0 */
837 dn->digits++; /* must be 2 at least */
838 #if DECDPUN>2
839 if (*last<100) return; /* 10-99 */
840 dn->digits++; /* must be 3 at least */
841 #if DECDPUN>3
842 if (*last<1000) return; /* 100-999 */
843 dn->digits++; /* must be 4 at least */
844 #if DECDPUN>4
845 for (pow=&DECPOWERS[4]; *last>=*pow; pow++) dn->digits++;
846 #endif
847 #endif
848 #endif
849 #endif
850 return;
851 } /*decDigitsFromDPD */
This page took 0.056762 seconds and 4 git commands to generate.