1 /* Decimal 32-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, 59 Temple Place - Suite 330, Boston, MA
22 /* ------------------------------------------------------------------ */
23 /* This module comprises the routines for decimal32 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 7 /* we need decNumbers with space for 7 */
34 #include "decNumber.h" /* base number library */
35 #include "decNumberLocal.h" /* decNumber local types, etc. */
36 #include "decimal32.h" /* our primary include */
37 #include "decUtility.h" /* utility routines */
39 #if DECTRACE || DECCHECK
40 void decimal32Show (decimal32 *); /* 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 /* decimal32FromNumber -- convert decNumber to decimal32 */
51 /* ds is the target decimal32 */
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 DECIMAL32_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 decimal32FromNumber (decimal32 * d32, 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 > DECIMAL32_Pmax /* too many digits */
82 || ae > DECIMAL32_Emax /* likely overflow */
83 || ae < DECIMAL32_Emin)
84 { /* likely underflow */
85 decContextDefault (&dc, DEC_INIT_DECIMAL32); /* [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 decimal32] */
96 DEC_clear (d32); /* 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 < DECIMAL32_Pmax))
106 { /* coefficient fits */
107 decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), 0);
109 if (dn->bits & DECNAN)
116 else if (decNumberIsZero (dn))
118 /* set and clamp exponent */
119 if (dn->exponent < -DECIMAL32_Bias)
122 status |= DEC_Clamped;
126 exp = dn->exponent + DECIMAL32_Bias; /* bias exponent */
127 if (exp > DECIMAL32_Ehigh)
129 exp = DECIMAL32_Ehigh;
130 status |= DEC_Clamped;
133 comb = (exp >> 3) & 0x18; /* combination field */
134 d32->bytes[0] = (uByte) (comb << 2);
135 exp &= 0x3f; /* remaining exponent bits */
136 decimal32SetExpCon (d32, 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 + DECIMAL32_Bias); /* bias exponent */
145 if (exp > DECIMAL32_Ehigh)
146 { /* fold-down case */
147 pad = exp - DECIMAL32_Ehigh;
148 exp = DECIMAL32_Ehigh; /* [to maximum] */
149 status |= DEC_Clamped;
152 decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), pad);
154 /* save and clear the top digit */
155 msd = ((unsigned) d32->bytes[1] >> 4);
156 d32->bytes[1] &= 0x0f;
157 /* create the combination field */
159 comb = 0x18 | (msd & 0x01) | ((exp >> 5) & 0x06);
161 comb = (msd & 0x07) | ((exp >> 3) & 0x18);
162 d32->bytes[0] = (uByte) (comb << 2);
163 exp &= 0x3f; /* remaining exponent bits */
164 decimal32SetExpCon (d32, exp);
168 decimal32SetSign (d32, 1);
170 decContextSetStatus (set, status); /* pass on status */
172 /*decimal32Show(d32); */
176 /* ------------------------------------------------------------------ */
177 /* decimal32ToNumber -- convert decimal32 to decNumber */
178 /* d32 is the source decimal32 */
179 /* dn is the target number, with appropriate space */
180 /* No error is possible. */
181 /* ------------------------------------------------------------------ */
183 decimal32ToNumber (decimal32 * d32, decNumber * dn)
185 uInt msd; /* coefficient MSD */
186 decimal32 wk; /* working copy, if needed */
187 uInt top = d32->bytes[0] & 0x7f; /* top byte, less sign bit */
188 decNumberZero (dn); /* clean target */
189 /* set the sign if negative */
190 if (decimal32Sign (d32))
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; /* working exponent */
210 msd = 8 + (comb & 0x01);
211 exp = (comb & 0x06) << 5; /* MSBs */
216 exp = (comb & 0x18) << 3;
218 dn->exponent = exp + decimal32ExpCon (d32) - DECIMAL32_Bias; /* remove bias */
221 /* get the coefficient, unless infinite */
222 if (!(dn->bits & DECINF))
224 Int bunches = DECIMAL32_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 decimal32, with an extra bunch which has */
229 /* the top digit ready for conversion */
230 wk = *d32; /* take a copy */
231 wk.bytes[0] = 0; /* clear all but coecon */
232 wk.bytes[1] &= 0x0f; /* .. */
233 wk.bytes[1] |= (msd << 4); /* and prefix MSD */
234 odd++; /* indicate the extra */
235 d32 = &wk; /* use the work copy */
237 decDenseUnpackCoeff (d32->bytes, sizeof (d32->bytes), dn, bunches, odd);
242 /* ------------------------------------------------------------------ */
243 /* to-scientific-string -- conversion to numeric string */
244 /* to-engineering-string -- conversion to numeric string */
246 /* decimal32ToString(d32, string); */
247 /* decimal32ToEngString(d32, string); */
249 /* d32 is the decimal32 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 decimal32ToString (decimal32 * d32, char *string)
259 decNumber dn; /* work */
260 decimal32ToNumber (d32, &dn);
261 decNumberToString (&dn, string);
266 decimal32ToEngString (decimal32 * d32, char *string)
268 decNumber dn; /* work */
269 decimal32ToNumber (d32, &dn);
270 decNumberToEngString (&dn, string);
274 /* ------------------------------------------------------------------ */
275 /* to-number -- conversion from numeric string */
277 /* decimal32FromString(result, string, set); */
279 /* result is the decimal32 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 decimal32 NaN. */
288 /* ------------------------------------------------------------------ */
290 decimal32FromString (decimal32 * result, char *string, decContext * set)
292 decContext dc; /* work */
293 decNumber dn; /* .. */
295 decContextDefault (&dc, DEC_INIT_DECIMAL32); /* no traps, please */
296 dc.round = set->round; /* use supplied rounding */
298 decNumberFromString (&dn, string, &dc); /* will round if needed */
299 decimal32FromNumber (result, &dn, &dc);
301 { /* something happened */
302 decContextSetStatus (set, dc.status); /* .. pass it on */
307 #if DECTRACE || DECCHECK
308 /* ------------------------------------------------------------------ */
309 /* decimal32Show -- display a single in hexadecimal [debug aid] */
310 /* d32 -- the number to show */
311 /* ------------------------------------------------------------------ */
312 /* Also shows sign/cob/expconfields extracted */
314 decimal32Show (decimal32 * d32)
316 char buf[DECIMAL32_Bytes * 2 + 1];
319 for (i = 0; i < DECIMAL32_Bytes; i++)
321 sprintf (&buf[j], "%02x", d32->bytes[i]);
324 printf (" D32> %s [S:%d Cb:%02x E:%d]\n", buf,
325 decimal32Sign (d32), decimal32Comb (d32), decimal32ExpCon (d32));