1 /* Decimal 128-bit format module from 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 decimal128 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 34 /* we need decNumbers with space for 34 */
35 #include "decNumber.h" /* base number library */
36 #include "decNumberLocal.h" /* decNumber local types, etc. */
37 #include "decimal128.h" /* our primary include */
38 #include "decUtility.h" /* utility routines */
40 #if DECTRACE || DECCHECK
41 void decimal128Show (const decimal128
*); /* 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 /* decimal128FromNumber -- convert decNumber to decimal128 */
52 /* ds is the target decimal128 */
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 DECIMAL128_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 decimal128FromNumber (decimal128
* d128
, 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
> DECIMAL128_Pmax
/* too many digits */
83 || ae
> DECIMAL128_Emax
/* likely overflow */
84 || ae
< DECIMAL128_Emin
)
85 { /* likely underflow */
86 decContextDefault (&dc
, DEC_INIT_DECIMAL128
); /* [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 decimal128] */
97 DEC_clear (d128
); /* 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
< DECIMAL128_Pmax
))
107 { /* coefficient fits */
108 decDensePackCoeff (dn
, d128
->bytes
, sizeof (d128
->bytes
), 0);
110 if (dn
->bits
& DECNAN
)
115 d128
->bytes
[0] = top
;
117 else if (decNumberIsZero (dn
))
119 /* set and clamp exponent */
120 if (dn
->exponent
< -DECIMAL128_Bias
)
123 status
|= DEC_Clamped
;
127 exp
= dn
->exponent
+ DECIMAL128_Bias
; /* bias exponent */
128 if (exp
> DECIMAL128_Ehigh
)
130 exp
= DECIMAL128_Ehigh
;
131 status
|= DEC_Clamped
;
134 comb
= (exp
>> 9) & 0x18; /* combination field */
135 d128
->bytes
[0] = (uByte
) (comb
<< 2);
136 exp
&= 0xfff; /* remaining exponent bits */
137 decimal128SetExpCon (d128
, 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
+ DECIMAL128_Bias
); /* bias exponent */
146 if (exp
> DECIMAL128_Ehigh
)
147 { /* fold-down case */
148 pad
= exp
- DECIMAL128_Ehigh
;
149 exp
= DECIMAL128_Ehigh
; /* [to maximum] */
150 status
|= DEC_Clamped
;
153 decDensePackCoeff (dn
, d128
->bytes
, sizeof (d128
->bytes
), pad
);
155 /* save and clear the top digit */
156 msd
= ((unsigned) d128
->bytes
[1] << 2) & 0x0c; /* top 2 bits */
157 msd
|= ((unsigned) d128
->bytes
[2] >> 6); /* low 2 bits */
158 d128
->bytes
[1] &= 0xfc;
159 d128
->bytes
[2] &= 0x3f;
161 /* create the combination field */
163 comb
= 0x18 | (msd
& 0x01) | ((exp
>> 11) & 0x06);
165 comb
= (msd
& 0x07) | ((exp
>> 9) & 0x18);
166 d128
->bytes
[0] = (uByte
) (comb
<< 2);
167 exp
&= 0xfff; /* remaining exponent bits */
168 decimal128SetExpCon (d128
, exp
);
172 decimal128SetSign (d128
, 1);
174 decContextSetStatus (set
, status
); /* pass on status */
176 /* decimal128Show(d128); */
180 /* ------------------------------------------------------------------ */
181 /* decimal128ToNumber -- convert decimal128 to decNumber */
182 /* d128 is the source decimal128 */
183 /* dn is the target number, with appropriate space */
184 /* No error is possible. */
185 /* ------------------------------------------------------------------ */
187 decimal128ToNumber (const decimal128
* d128
, decNumber
* dn
)
189 uInt msd
; /* coefficient MSD */
190 decimal128 wk
; /* working copy, if needed */
191 uInt top
= d128
->bytes
[0] & 0x7f; /* top byte, less sign bit */
192 decNumberZero (dn
); /* clean target */
193 /* set the sign if negative */
194 if (decimal128Sign (d128
))
199 if ((top
& 0x7c) == (DECIMAL_Inf
& 0x7c))
201 else if ((top
& 0x7e) == (DECIMAL_NaN
& 0x7e))
205 msd
= 0; /* no top digit */
208 { /* have a finite number */
209 uInt comb
= top
>> 2; /* combination field */
210 uInt exp
; /* exponent */
214 msd
= 8 + (comb
& 0x01);
215 exp
= (comb
& 0x06) << 11; /* MSBs */
220 exp
= (comb
& 0x18) << 9;
222 dn
->exponent
= exp
+ decimal128ExpCon (d128
) - DECIMAL128_Bias
; /* remove bias */
225 /* get the coefficient, unless infinite */
226 if (!(dn
->bits
& DECINF
))
228 Int bunches
= DECIMAL128_Pmax
/ 3; /* coefficient full bunches to convert */
229 Int odd
= 0; /* assume MSD is 0 (no odd bunch) */
231 { /* coefficient has leading non-0 digit */
232 /* make a copy of the decimal128, with an extra bunch which has */
233 /* the top digit ready for conversion */
234 wk
= *d128
; /* take a copy */
235 wk
.bytes
[0] = 0; /* clear all but coecon */
236 wk
.bytes
[1] = 0; /* .. */
237 wk
.bytes
[2] &= 0x3f; /* .. */
238 wk
.bytes
[1] |= (msd
>> 2); /* and prefix MSD */
239 wk
.bytes
[2] |= (msd
<< 6); /* .. */
240 odd
++; /* indicate the extra */
241 d128
= &wk
; /* use the work copy */
243 decDenseUnpackCoeff (d128
->bytes
, sizeof (d128
->bytes
), dn
, bunches
,
247 /* decNumberShow(dn); */
251 /* ------------------------------------------------------------------ */
252 /* to-scientific-string -- conversion to numeric string */
253 /* to-engineering-string -- conversion to numeric string */
255 /* decimal128ToString(d128, string); */
256 /* decimal128ToEngString(d128, string); */
258 /* d128 is the decimal128 format number to convert */
259 /* string is the string where the result will be laid out */
261 /* string must be at least 24 characters */
263 /* No error is possible, and no status can be set. */
264 /* ------------------------------------------------------------------ */
266 decimal128ToString (const decimal128
* d128
, char *string
)
268 decNumber dn
; /* work */
269 decimal128ToNumber (d128
, &dn
);
270 decNumberToString (&dn
, string
);
275 decimal128ToEngString (const decimal128
* d128
, char *string
)
277 decNumber dn
; /* work */
278 decimal128ToNumber (d128
, &dn
);
279 decNumberToEngString (&dn
, string
);
283 /* ------------------------------------------------------------------ */
284 /* to-number -- conversion from numeric string */
286 /* decimal128FromString(result, string, set); */
288 /* result is the decimal128 format number which gets the result of */
290 /* *string is the character string which should contain a valid */
291 /* number (which may be a special value) */
292 /* set is the context */
294 /* The context is supplied to this routine is used for error handling */
295 /* (setting of status and traps) and for the rounding mode, only. */
296 /* If an error occurs, the result will be a valid decimal128 NaN. */
297 /* ------------------------------------------------------------------ */
299 decimal128FromString (decimal128
* result
, const char *string
, decContext
* set
)
301 decContext dc
; /* work */
302 decNumber dn
; /* .. */
304 decContextDefault (&dc
, DEC_INIT_DECIMAL128
); /* no traps, please */
305 dc
.round
= set
->round
; /* use supplied rounding */
307 decNumberFromString (&dn
, string
, &dc
); /* will round if needed */
308 decimal128FromNumber (result
, &dn
, &dc
);
310 { /* something happened */
311 decContextSetStatus (set
, dc
.status
); /* .. pass it on */
317 #if DECTRACE || DECCHECK
318 /* ------------------------------------------------------------------ */
319 /* decimal128Show -- display a single in hexadecimal [debug aid] */
320 /* d128 -- the number to show */
321 /* ------------------------------------------------------------------ */
322 /* Also shows sign/cob/expconfields extracted */
324 decimal128Show (const decimal128
* d128
)
326 char buf
[DECIMAL128_Bytes
* 2 + 1];
329 for (i
= 0; i
< DECIMAL128_Bytes
; i
++)
331 sprintf (&buf
[j
], "%02x", d128
->bytes
[i
]);
334 printf (" D128> %s [S:%d Cb:%02x E:%d]\n", buf
,
335 decimal128Sign (d128
), decimal128Comb (d128
),
336 decimal128ExpCon (d128
));