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, 59 Temple Place - Suite 330, 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 */
34 #include "decNumber.h" /* base number library */
35 #include "decNumberLocal.h" /* decNumber local types, etc. */
36 #include "decimal128.h" /* our primary include */
37 #include "decUtility.h" /* utility routines */
39 #if DECTRACE || DECCHECK
40 void decimal128Show (decimal128 *); /* for debug */
41 void decNumberShow (decNumber *); /* .. */
45 /* Clear a structure (e.g., a decNumber) */
46 #define DEC_clear(d) memset(d, 0, sizeof(*d))
48 /* ------------------------------------------------------------------ */
49 /* decimal128FromNumber -- convert decNumber to decimal128 */
51 /* ds is the target decimal128 */
52 /* dn is the source number (assumed valid) */
53 /* set is the context, used only for reporting errors */
55 /* The set argument is used only for status reporting and for the */
56 /* rounding mode (used if the coefficient is more than DECIMAL128_Pmax*/
57 /* digits or an overflow is detected). If the exponent is out of the */
58 /* valid range then Overflow or Underflow will be raised. */
59 /* After Underflow a subnormal result is possible. */
61 /* DEC_Clamped is set if the number has to be 'folded down' to fit, */
62 /* by reducing its exponent and multiplying the coefficient by a */
63 /* power of ten, or if the exponent on a zero had to be clamped. */
64 /* ------------------------------------------------------------------ */
66 decimal128FromNumber (decimal128 * d128, decNumber * dn, decContext * set)
68 uInt status = 0; /* status accumulator */
69 Int pad = 0; /* coefficient pad digits */
70 decNumber dw; /* work */
71 decContext dc; /* .. */
72 uByte isneg = dn->bits & DECNEG; /* non-0 if original sign set */
73 uInt comb, exp; /* work */
75 /* If the number is finite, and has too many digits, or the exponent */
76 /* could be out of range then we reduce the number under the */
77 /* appropriate constraints */
78 if (!(dn->bits & DECSPECIAL))
79 { /* not a special value */
80 Int ae = dn->exponent + dn->digits - 1; /* adjusted exponent */
81 if (dn->digits > DECIMAL128_Pmax /* too many digits */
82 || ae > DECIMAL128_Emax /* likely overflow */
83 || ae < DECIMAL128_Emin)
84 { /* likely underflow */
85 decContextDefault (&dc, DEC_INIT_DECIMAL128); /* [no traps] */
86 dc.round = set->round; /* use supplied rounding */
87 decNumberPlus (&dw, dn, &dc); /* (round and check) */
88 /* [this changes -0 to 0, but it will be restored below] */
89 status |= dc.status; /* save status */
90 dn = &dw; /* use the work number */
92 /* [this could have pushed number to Infinity or zero, so this */
93 /* rounding must be done before we generate the decimal128] */
96 DEC_clear (d128); /* clean the target */
97 if (dn->bits & DECSPECIAL)
98 { /* a special value */
100 if (dn->bits & DECINF)
104 if ((*dn->lsu != 0 || dn->digits > 1) /* non-zero coefficient */
105 && (dn->digits < DECIMAL128_Pmax))
106 { /* coefficient fits */
107 decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), 0);
109 if (dn->bits & DECNAN)
114 d128->bytes[0] = top;
116 else if (decNumberIsZero (dn))
118 /* set and clamp exponent */
119 if (dn->exponent < -DECIMAL128_Bias)
122 status |= DEC_Clamped;
126 exp = dn->exponent + DECIMAL128_Bias; /* bias exponent */
127 if (exp > DECIMAL128_Ehigh)
129 exp = DECIMAL128_Ehigh;
130 status |= DEC_Clamped;
133 comb = (exp >> 9) & 0x18; /* combination field */
134 d128->bytes[0] = (uByte) (comb << 2);
135 exp &= 0xfff; /* remaining exponent bits */
136 decimal128SetExpCon (d128, exp);
139 { /* non-zero finite number */
142 /* we have a dn that fits, but it may need to be padded */
143 exp = (uInt) (dn->exponent + DECIMAL128_Bias); /* bias exponent */
145 if (exp > DECIMAL128_Ehigh)
146 { /* fold-down case */
147 pad = exp - DECIMAL128_Ehigh;
148 exp = DECIMAL128_Ehigh; /* [to maximum] */
149 status |= DEC_Clamped;
152 decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), pad);
154 /* save and clear the top digit */
155 msd = ((unsigned) d128->bytes[1] << 2) & 0x0c; /* top 2 bits */
156 msd |= ((unsigned) d128->bytes[2] >> 6); /* low 2 bits */
157 d128->bytes[1] &= 0xfc;
158 d128->bytes[2] &= 0x3f;
160 /* create the combination field */
162 comb = 0x18 | (msd & 0x01) | ((exp >> 11) & 0x06);
164 comb = (msd & 0x07) | ((exp >> 9) & 0x18);
165 d128->bytes[0] = (uByte) (comb << 2);
166 exp &= 0xfff; /* remaining exponent bits */
167 decimal128SetExpCon (d128, exp);
171 decimal128SetSign (d128, 1);
173 decContextSetStatus (set, status); /* pass on status */
175 /* decimal128Show(d128); */
179 /* ------------------------------------------------------------------ */
180 /* decimal128ToNumber -- convert decimal128 to decNumber */
181 /* d128 is the source decimal128 */
182 /* dn is the target number, with appropriate space */
183 /* No error is possible. */
184 /* ------------------------------------------------------------------ */
186 decimal128ToNumber (decimal128 * d128, decNumber * dn)
188 uInt msd; /* coefficient MSD */
189 decimal128 wk; /* working copy, if needed */
190 uInt top = d128->bytes[0] & 0x7f; /* top byte, less sign bit */
191 decNumberZero (dn); /* clean target */
192 /* set the sign if negative */
193 if (decimal128Sign (d128))
198 if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
200 else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
204 msd = 0; /* no top digit */
207 { /* have a finite number */
208 uInt comb = top >> 2; /* combination field */
209 uInt exp; /* exponent */
213 msd = 8 + (comb & 0x01);
214 exp = (comb & 0x06) << 11; /* MSBs */
219 exp = (comb & 0x18) << 9;
221 dn->exponent = exp + decimal128ExpCon (d128) - DECIMAL128_Bias; /* remove bias */
224 /* get the coefficient, unless infinite */
225 if (!(dn->bits & DECINF))
227 Int bunches = DECIMAL128_Pmax / 3; /* coefficient full bunches to convert */
228 Int odd = 0; /* assume MSD is 0 (no odd bunch) */
230 { /* coefficient has leading non-0 digit */
231 /* make a copy of the decimal128, with an extra bunch which has */
232 /* the top digit ready for conversion */
233 wk = *d128; /* take a copy */
234 wk.bytes[0] = 0; /* clear all but coecon */
235 wk.bytes[1] = 0; /* .. */
236 wk.bytes[2] &= 0x3f; /* .. */
237 wk.bytes[1] |= (msd >> 2); /* and prefix MSD */
238 wk.bytes[2] |= (msd << 6); /* .. */
239 odd++; /* indicate the extra */
240 d128 = &wk; /* use the work copy */
242 decDenseUnpackCoeff (d128->bytes, sizeof (d128->bytes), dn, bunches,
246 /* decNumberShow(dn); */
250 /* ------------------------------------------------------------------ */
251 /* to-scientific-string -- conversion to numeric string */
252 /* to-engineering-string -- conversion to numeric string */
254 /* decimal128ToString(d128, string); */
255 /* decimal128ToEngString(d128, string); */
257 /* d128 is the decimal128 format number to convert */
258 /* string is the string where the result will be laid out */
260 /* string must be at least 24 characters */
262 /* No error is possible, and no status can be set. */
263 /* ------------------------------------------------------------------ */
265 decimal128ToString (decimal128 * d128, char *string)
267 decNumber dn; /* work */
268 decimal128ToNumber (d128, &dn);
269 decNumberToString (&dn, string);
274 decimal128ToEngString (decimal128 * d128, char *string)
276 decNumber dn; /* work */
277 decimal128ToNumber (d128, &dn);
278 decNumberToEngString (&dn, string);
282 /* ------------------------------------------------------------------ */
283 /* to-number -- conversion from numeric string */
285 /* decimal128FromString(result, string, set); */
287 /* result is the decimal128 format number which gets the result of */
289 /* *string is the character string which should contain a valid */
290 /* number (which may be a special value) */
291 /* set is the context */
293 /* The context is supplied to this routine is used for error handling */
294 /* (setting of status and traps) and for the rounding mode, only. */
295 /* If an error occurs, the result will be a valid decimal128 NaN. */
296 /* ------------------------------------------------------------------ */
298 decimal128FromString (decimal128 * result, char *string, decContext * set)
300 decContext dc; /* work */
301 decNumber dn; /* .. */
303 decContextDefault (&dc, DEC_INIT_DECIMAL128); /* no traps, please */
304 dc.round = set->round; /* use supplied rounding */
306 decNumberFromString (&dn, string, &dc); /* will round if needed */
307 decimal128FromNumber (result, &dn, &dc);
309 { /* something happened */
310 decContextSetStatus (set, dc.status); /* .. pass it on */
316 #if DECTRACE || DECCHECK
317 /* ------------------------------------------------------------------ */
318 /* decimal128Show -- display a single in hexadecimal [debug aid] */
319 /* d128 -- the number to show */
320 /* ------------------------------------------------------------------ */
321 /* Also shows sign/cob/expconfields extracted */
323 decimal128Show (decimal128 * d128)
325 char buf[DECIMAL128_Bytes * 2 + 1];
328 for (i = 0; i < DECIMAL128_Bytes; i++)
330 sprintf (&buf[j], "%02x", d128->bytes[i]);
333 printf (" D128> %s [S:%d Cb:%02x E:%d]\n", buf,
334 decimal128Sign (d128), decimal128Comb (d128),
335 decimal128ExpCon (d128));