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