1 /* Decimal 64-bit format module for the decNumber C Library
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 Contributed by IBM Corporation. Author Mike Cowlishaw.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 /* ------------------------------------------------------------------ */
23 /* This module comprises the routines for decimal64 format numbers. */
24 /* Conversions are supplied to and from decNumber and String. */
26 /* No arithmetic routines are included; decNumber provides these. */
28 /* Error handling is the same as decNumber (qv.). */
29 /* ------------------------------------------------------------------ */
30 #include <string.h> /* [for memset/memcpy] */
31 #include <stdio.h> /* [for printf] */
33 #define DECNUMDIGITS 16 /* we need decNumbers with space for 16 */
35 #include "decNumber.h" /* base number library */
36 #include "decNumberLocal.h" /* decNumber local types, etc. */
37 #include "decimal64.h" /* our primary include */
38 #include "decUtility.h" /* utility routines */
40 #if DECTRACE || DECCHECK
41 void decimal64Show (const decimal64
*); /* for debug */
42 void decNumberShow (const decNumber
*); /* .. */
46 /* Clear a structure (e.g., a decNumber) */
47 #define DEC_clear(d) memset(d, 0, sizeof(*d))
49 /* ------------------------------------------------------------------ */
50 /* decimal64FromNumber -- convert decNumber to decimal64 */
52 /* ds is the target decimal64 */
53 /* dn is the source number (assumed valid) */
54 /* set is the context, used only for reporting errors */
56 /* The set argument is used only for status reporting and for the */
57 /* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */
58 /* digits or an overflow is detected). If the exponent is out of the */
59 /* valid range then Overflow or Underflow will be raised. */
60 /* After Underflow a subnormal result is possible. */
62 /* DEC_Clamped is set if the number has to be 'folded down' to fit, */
63 /* by reducing its exponent and multiplying the coefficient by a */
64 /* power of ten, or if the exponent on a zero had to be clamped. */
65 /* ------------------------------------------------------------------ */
67 decimal64FromNumber (decimal64
* d64
, const decNumber
* dn
, decContext
* set
)
69 uInt status
= 0; /* status accumulator */
70 Int pad
= 0; /* coefficient pad digits */
71 decNumber dw
; /* work */
72 decContext dc
; /* .. */
73 uByte isneg
= dn
->bits
& DECNEG
; /* non-0 if original sign set */
74 uInt comb
, exp
; /* work */
76 /* If the number is finite, and has too many digits, or the exponent */
77 /* could be out of range then we reduce the number under the */
78 /* appropriate constraints */
79 if (!(dn
->bits
& DECSPECIAL
))
80 { /* not a special value */
81 Int ae
= dn
->exponent
+ dn
->digits
- 1; /* adjusted exponent */
82 if (dn
->digits
> DECIMAL64_Pmax
/* too many digits */
83 || ae
> DECIMAL64_Emax
/* likely overflow */
84 || ae
< DECIMAL64_Emin
)
85 { /* likely underflow */
86 decContextDefault (&dc
, DEC_INIT_DECIMAL64
); /* [no traps] */
87 dc
.round
= set
->round
; /* use supplied rounding */
88 decNumberPlus (&dw
, dn
, &dc
); /* (round and check) */
89 /* [this changes -0 to 0, but it will be restored below] */
90 status
|= dc
.status
; /* save status */
91 dn
= &dw
; /* use the work number */
93 /* [this could have pushed number to Infinity or zero, so this */
94 /* rounding must be done before we generate the decimal64] */
97 DEC_clear (d64
); /* clean the target */
98 if (dn
->bits
& DECSPECIAL
)
99 { /* a special value */
100 uByte top
; /* work */
101 if (dn
->bits
& DECINF
)
105 if ((*dn
->lsu
!= 0 || dn
->digits
> 1) /* non-zero coefficient */
106 && (dn
->digits
< DECIMAL64_Pmax
))
107 { /* coefficient fits */
108 decDensePackCoeff (dn
, d64
->bytes
, sizeof (d64
->bytes
), 0);
110 if (dn
->bits
& DECNAN
)
117 else if (decNumberIsZero (dn
))
119 /* set and clamp exponent */
120 if (dn
->exponent
< -DECIMAL64_Bias
)
123 status
|= DEC_Clamped
;
127 exp
= dn
->exponent
+ DECIMAL64_Bias
; /* bias exponent */
128 if (exp
> DECIMAL64_Ehigh
)
130 exp
= DECIMAL64_Ehigh
;
131 status
|= DEC_Clamped
;
134 comb
= (exp
>> 5) & 0x18; /* combination field */
135 d64
->bytes
[0] = (uByte
) (comb
<< 2);
136 exp
&= 0xff; /* remaining exponent bits */
137 decimal64SetExpCon (d64
, exp
);
140 { /* non-zero finite number */
143 /* we have a dn that fits, but it may need to be padded */
144 exp
= (uInt
) (dn
->exponent
+ DECIMAL64_Bias
); /* bias exponent */
145 if (exp
> DECIMAL64_Ehigh
)
146 { /* fold-down case */
147 pad
= exp
- DECIMAL64_Ehigh
;
148 exp
= DECIMAL64_Ehigh
; /* [to maximum] */
149 status
|= DEC_Clamped
;
152 decDensePackCoeff (dn
, d64
->bytes
, sizeof (d64
->bytes
), pad
);
154 /* save and clear the top digit */
155 msd
= ((unsigned) d64
->bytes
[1] >> 2) & 0x0f;
156 d64
->bytes
[1] &= 0x03;
157 /* create the combination field */
159 comb
= 0x18 | (msd
& 0x01) | ((exp
>> 7) & 0x06);
161 comb
= (msd
& 0x07) | ((exp
>> 5) & 0x18);
162 d64
->bytes
[0] = (uByte
) (comb
<< 2);
163 exp
&= 0xff; /* remaining exponent bits */
164 decimal64SetExpCon (d64
, exp
);
168 decimal64SetSign (d64
, 1);
170 decContextSetStatus (set
, status
); /* pass on status */
172 /*decimal64Show(d64); */
176 /* ------------------------------------------------------------------ */
177 /* decimal64ToNumber -- convert decimal64 to decNumber */
178 /* d64 is the source decimal64 */
179 /* dn is the target number, with appropriate space */
180 /* No error is possible. */
181 /* ------------------------------------------------------------------ */
183 decimal64ToNumber (const decimal64
* d64
, decNumber
* dn
)
185 uInt msd
; /* coefficient MSD */
186 decimal64 wk
; /* working copy, if needed */
187 uInt top
= d64
->bytes
[0] & 0x7f; /* top byte, less sign bit */
188 decNumberZero (dn
); /* clean target */
189 /* set the sign if negative */
190 if (decimal64Sign (d64
))
195 if ((top
& 0x7c) == (DECIMAL_Inf
& 0x7c))
197 else if ((top
& 0x7e) == (DECIMAL_NaN
& 0x7e))
201 msd
= 0; /* no top digit */
204 { /* have a finite number */
205 uInt comb
= top
>> 2; /* combination field */
206 uInt exp
; /* exponent */
210 msd
= 8 + (comb
& 0x01);
211 exp
= (comb
& 0x06) << 7; /* MSBs */
216 exp
= (comb
& 0x18) << 5;
218 dn
->exponent
= exp
+ decimal64ExpCon (d64
) - DECIMAL64_Bias
; /* remove bias */
221 /* get the coefficient, unless infinite */
222 if (!(dn
->bits
& DECINF
))
224 Int bunches
= DECIMAL64_Pmax
/ 3; /* coefficient full bunches to convert */
225 Int odd
= 0; /* assume MSD is 0 (no odd bunch) */
227 { /* coefficient has leading non-0 digit */
228 /* make a copy of the decimal64, with an extra bunch which has */
229 /* the top digit ready for conversion */
230 wk
= *d64
; /* take a copy */
231 wk
.bytes
[0] = 0; /* clear all but coecon */
232 wk
.bytes
[1] &= 0x03; /* .. */
233 wk
.bytes
[1] |= (msd
<< 2); /* and prefix MSD */
234 odd
++; /* indicate the extra */
235 d64
= &wk
; /* use the work copy */
237 decDenseUnpackCoeff (d64
->bytes
, sizeof (d64
->bytes
), dn
, bunches
, odd
);
242 /* ------------------------------------------------------------------ */
243 /* to-scientific-string -- conversion to numeric string */
244 /* to-engineering-string -- conversion to numeric string */
246 /* decimal64ToString(d64, string); */
247 /* decimal64ToEngString(d64, string); */
249 /* d64 is the decimal64 format number to convert */
250 /* string is the string where the result will be laid out */
252 /* string must be at least 24 characters */
254 /* No error is possible, and no status can be set. */
255 /* ------------------------------------------------------------------ */
257 decimal64ToString (const decimal64
* d64
, char *string
)
259 decNumber dn
; /* work */
260 decimal64ToNumber (d64
, &dn
);
261 decNumberToString (&dn
, string
);
266 decimal64ToEngString (const decimal64
* d64
, char *string
)
268 decNumber dn
; /* work */
269 decimal64ToNumber (d64
, &dn
);
270 decNumberToEngString (&dn
, string
);
274 /* ------------------------------------------------------------------ */
275 /* to-number -- conversion from numeric string */
277 /* decimal64FromString(result, string, set); */
279 /* result is the decimal64 format number which gets the result of */
281 /* *string is the character string which should contain a valid */
282 /* number (which may be a special value) */
283 /* set is the context */
285 /* The context is supplied to this routine is used for error handling */
286 /* (setting of status and traps) and for the rounding mode, only. */
287 /* If an error occurs, the result will be a valid decimal64 NaN. */
288 /* ------------------------------------------------------------------ */
290 decimal64FromString (decimal64
* result
, const char *string
, decContext
* set
)
292 decContext dc
; /* work */
293 decNumber dn
; /* .. */
295 decContextDefault (&dc
, DEC_INIT_DECIMAL64
); /* no traps, please */
296 dc
.round
= set
->round
; /* use supplied rounding */
298 decNumberFromString (&dn
, string
, &dc
); /* will round if needed */
300 decimal64FromNumber (result
, &dn
, &dc
);
302 { /* something happened */
303 decContextSetStatus (set
, dc
.status
); /* .. pass it on */
308 #if DECTRACE || DECCHECK
309 /* ------------------------------------------------------------------ */
310 /* decimal64Show -- display a single in hexadecimal [debug aid] */
311 /* d64 -- the number to show */
312 /* ------------------------------------------------------------------ */
313 /* Also shows sign/cob/expconfields extracted */
315 decimal64Show (const decimal64
* d64
)
317 char buf
[DECIMAL64_Bytes
* 2 + 1];
320 for (i
= 0; i
< DECIMAL64_Bytes
; i
++)
322 sprintf (&buf
[j
], "%02x", d64
->bytes
[i
]);
325 printf (" D64> %s [S:%d Cb:%02x E:%d]\n", buf
,
326 decimal64Sign (d64
), decimal64Comb (d64
), decimal64ExpCon (d64
));