1*404b540aSrobert /* Decimal 32-bit format module for the decNumber C Library
2*404b540aSrobert Copyright (C) 2005 Free Software Foundation, Inc.
3*404b540aSrobert Contributed by IBM Corporation. Author Mike Cowlishaw.
4*404b540aSrobert
5*404b540aSrobert This file is part of GCC.
6*404b540aSrobert
7*404b540aSrobert GCC is free software; you can redistribute it and/or modify it under
8*404b540aSrobert the terms of the GNU General Public License as published by the Free
9*404b540aSrobert Software Foundation; either version 2, or (at your option) any later
10*404b540aSrobert version.
11*404b540aSrobert
12*404b540aSrobert In addition to the permissions in the GNU General Public License,
13*404b540aSrobert the Free Software Foundation gives you unlimited permission to link
14*404b540aSrobert the compiled version of this file into combinations with other
15*404b540aSrobert programs, and to distribute those combinations without any
16*404b540aSrobert restriction coming from the use of this file. (The General Public
17*404b540aSrobert License restrictions do apply in other respects; for example, they
18*404b540aSrobert cover modification of the file, and distribution when not linked
19*404b540aSrobert into a combine executable.)
20*404b540aSrobert
21*404b540aSrobert GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22*404b540aSrobert WARRANTY; without even the implied warranty of MERCHANTABILITY or
23*404b540aSrobert FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24*404b540aSrobert for more details.
25*404b540aSrobert
26*404b540aSrobert You should have received a copy of the GNU General Public License
27*404b540aSrobert along with GCC; see the file COPYING. If not, write to the Free
28*404b540aSrobert Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29*404b540aSrobert 02110-1301, USA. */
30*404b540aSrobert
31*404b540aSrobert /* ------------------------------------------------------------------ */
32*404b540aSrobert /* This module comprises the routines for decimal32 format numbers. */
33*404b540aSrobert /* Conversions are supplied to and from decNumber and String. */
34*404b540aSrobert /* */
35*404b540aSrobert /* No arithmetic routines are included; decNumber provides these. */
36*404b540aSrobert /* */
37*404b540aSrobert /* Error handling is the same as decNumber (qv.). */
38*404b540aSrobert /* ------------------------------------------------------------------ */
39*404b540aSrobert #include <string.h> /* [for memset/memcpy] */
40*404b540aSrobert #include <stdio.h> /* [for printf] */
41*404b540aSrobert
42*404b540aSrobert #define DECNUMDIGITS 7 /* we need decNumbers with space for 7 */
43*404b540aSrobert #include "config.h"
44*404b540aSrobert #include "decNumber.h" /* base number library */
45*404b540aSrobert #include "decNumberLocal.h" /* decNumber local types, etc. */
46*404b540aSrobert #include "decimal32.h" /* our primary include */
47*404b540aSrobert #include "decUtility.h" /* utility routines */
48*404b540aSrobert
49*404b540aSrobert #if DECTRACE || DECCHECK
50*404b540aSrobert void decimal32Show (const decimal32 *); /* for debug */
51*404b540aSrobert void decNumberShow (const decNumber *); /* .. */
52*404b540aSrobert #endif
53*404b540aSrobert
54*404b540aSrobert /* Useful macro */
55*404b540aSrobert /* Clear a structure (e.g., a decNumber) */
56*404b540aSrobert #define DEC_clear(d) memset(d, 0, sizeof(*d))
57*404b540aSrobert
58*404b540aSrobert /* ------------------------------------------------------------------ */
59*404b540aSrobert /* decimal32FromNumber -- convert decNumber to decimal32 */
60*404b540aSrobert /* */
61*404b540aSrobert /* ds is the target decimal32 */
62*404b540aSrobert /* dn is the source number (assumed valid) */
63*404b540aSrobert /* set is the context, used only for reporting errors */
64*404b540aSrobert /* */
65*404b540aSrobert /* The set argument is used only for status reporting and for the */
66*404b540aSrobert /* rounding mode (used if the coefficient is more than DECIMAL32_Pmax */
67*404b540aSrobert /* digits or an overflow is detected). If the exponent is out of the */
68*404b540aSrobert /* valid range then Overflow or Underflow will be raised. */
69*404b540aSrobert /* After Underflow a subnormal result is possible. */
70*404b540aSrobert /* */
71*404b540aSrobert /* DEC_Clamped is set if the number has to be 'folded down' to fit, */
72*404b540aSrobert /* by reducing its exponent and multiplying the coefficient by a */
73*404b540aSrobert /* power of ten, or if the exponent on a zero had to be clamped. */
74*404b540aSrobert /* ------------------------------------------------------------------ */
75*404b540aSrobert decimal32 *
decimal32FromNumber(decimal32 * d32,const decNumber * dn,decContext * set)76*404b540aSrobert decimal32FromNumber (decimal32 * d32, const decNumber * dn, decContext * set)
77*404b540aSrobert {
78*404b540aSrobert uInt status = 0; /* status accumulator */
79*404b540aSrobert Int pad = 0; /* coefficient pad digits */
80*404b540aSrobert decNumber dw; /* work */
81*404b540aSrobert decContext dc; /* .. */
82*404b540aSrobert uByte isneg = dn->bits & DECNEG; /* non-0 if original sign set */
83*404b540aSrobert uInt comb, exp; /* work */
84*404b540aSrobert
85*404b540aSrobert /* If the number is finite, and has too many digits, or the exponent */
86*404b540aSrobert /* could be out of range then we reduce the number under the */
87*404b540aSrobert /* appropriate constraints */
88*404b540aSrobert if (!(dn->bits & DECSPECIAL))
89*404b540aSrobert { /* not a special value */
90*404b540aSrobert Int ae = dn->exponent + dn->digits - 1; /* adjusted exponent */
91*404b540aSrobert if (dn->digits > DECIMAL32_Pmax /* too many digits */
92*404b540aSrobert || ae > DECIMAL32_Emax /* likely overflow */
93*404b540aSrobert || ae < DECIMAL32_Emin)
94*404b540aSrobert { /* likely underflow */
95*404b540aSrobert decContextDefault (&dc, DEC_INIT_DECIMAL32); /* [no traps] */
96*404b540aSrobert dc.round = set->round; /* use supplied rounding */
97*404b540aSrobert decNumberPlus (&dw, dn, &dc); /* (round and check) */
98*404b540aSrobert /* [this changes -0 to 0, but it will be restored below] */
99*404b540aSrobert status |= dc.status; /* save status */
100*404b540aSrobert dn = &dw; /* use the work number */
101*404b540aSrobert }
102*404b540aSrobert /* [this could have pushed number to Infinity or zero, so this */
103*404b540aSrobert /* rounding must be done before we generate the decimal32] */
104*404b540aSrobert }
105*404b540aSrobert
106*404b540aSrobert DEC_clear (d32); /* clean the target */
107*404b540aSrobert if (dn->bits & DECSPECIAL)
108*404b540aSrobert { /* a special value */
109*404b540aSrobert uByte top; /* work */
110*404b540aSrobert if (dn->bits & DECINF)
111*404b540aSrobert top = DECIMAL_Inf;
112*404b540aSrobert else
113*404b540aSrobert { /* sNaN or qNaN */
114*404b540aSrobert if ((*dn->lsu != 0 || dn->digits > 1) /* non-zero coefficient */
115*404b540aSrobert && (dn->digits < DECIMAL32_Pmax))
116*404b540aSrobert { /* coefficient fits */
117*404b540aSrobert decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), 0);
118*404b540aSrobert }
119*404b540aSrobert if (dn->bits & DECNAN)
120*404b540aSrobert top = DECIMAL_NaN;
121*404b540aSrobert else
122*404b540aSrobert top = DECIMAL_sNaN;
123*404b540aSrobert }
124*404b540aSrobert d32->bytes[0] = top;
125*404b540aSrobert }
126*404b540aSrobert else if (decNumberIsZero (dn))
127*404b540aSrobert { /* a zero */
128*404b540aSrobert /* set and clamp exponent */
129*404b540aSrobert if (dn->exponent < -DECIMAL32_Bias)
130*404b540aSrobert {
131*404b540aSrobert exp = 0;
132*404b540aSrobert status |= DEC_Clamped;
133*404b540aSrobert }
134*404b540aSrobert else
135*404b540aSrobert {
136*404b540aSrobert exp = dn->exponent + DECIMAL32_Bias; /* bias exponent */
137*404b540aSrobert if (exp > DECIMAL32_Ehigh)
138*404b540aSrobert { /* top clamp */
139*404b540aSrobert exp = DECIMAL32_Ehigh;
140*404b540aSrobert status |= DEC_Clamped;
141*404b540aSrobert }
142*404b540aSrobert }
143*404b540aSrobert comb = (exp >> 3) & 0x18; /* combination field */
144*404b540aSrobert d32->bytes[0] = (uByte) (comb << 2);
145*404b540aSrobert exp &= 0x3f; /* remaining exponent bits */
146*404b540aSrobert decimal32SetExpCon (d32, exp);
147*404b540aSrobert }
148*404b540aSrobert else
149*404b540aSrobert { /* non-zero finite number */
150*404b540aSrobert uInt msd; /* work */
151*404b540aSrobert
152*404b540aSrobert /* we have a dn that fits, but it may need to be padded */
153*404b540aSrobert exp = (uInt) (dn->exponent + DECIMAL32_Bias); /* bias exponent */
154*404b540aSrobert
155*404b540aSrobert if (exp > DECIMAL32_Ehigh)
156*404b540aSrobert { /* fold-down case */
157*404b540aSrobert pad = exp - DECIMAL32_Ehigh;
158*404b540aSrobert exp = DECIMAL32_Ehigh; /* [to maximum] */
159*404b540aSrobert status |= DEC_Clamped;
160*404b540aSrobert }
161*404b540aSrobert
162*404b540aSrobert decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), pad);
163*404b540aSrobert
164*404b540aSrobert /* save and clear the top digit */
165*404b540aSrobert msd = ((unsigned) d32->bytes[1] >> 4);
166*404b540aSrobert d32->bytes[1] &= 0x0f;
167*404b540aSrobert /* create the combination field */
168*404b540aSrobert if (msd >= 8)
169*404b540aSrobert comb = 0x18 | (msd & 0x01) | ((exp >> 5) & 0x06);
170*404b540aSrobert else
171*404b540aSrobert comb = (msd & 0x07) | ((exp >> 3) & 0x18);
172*404b540aSrobert d32->bytes[0] = (uByte) (comb << 2);
173*404b540aSrobert exp &= 0x3f; /* remaining exponent bits */
174*404b540aSrobert decimal32SetExpCon (d32, exp);
175*404b540aSrobert }
176*404b540aSrobert
177*404b540aSrobert if (isneg)
178*404b540aSrobert decimal32SetSign (d32, 1);
179*404b540aSrobert if (status != 0)
180*404b540aSrobert decContextSetStatus (set, status); /* pass on status */
181*404b540aSrobert
182*404b540aSrobert /*decimal32Show(d32); */
183*404b540aSrobert return d32;
184*404b540aSrobert }
185*404b540aSrobert
186*404b540aSrobert /* ------------------------------------------------------------------ */
187*404b540aSrobert /* decimal32ToNumber -- convert decimal32 to decNumber */
188*404b540aSrobert /* d32 is the source decimal32 */
189*404b540aSrobert /* dn is the target number, with appropriate space */
190*404b540aSrobert /* No error is possible. */
191*404b540aSrobert /* ------------------------------------------------------------------ */
192*404b540aSrobert decNumber *
decimal32ToNumber(const decimal32 * d32,decNumber * dn)193*404b540aSrobert decimal32ToNumber (const decimal32 * d32, decNumber * dn)
194*404b540aSrobert {
195*404b540aSrobert uInt msd; /* coefficient MSD */
196*404b540aSrobert decimal32 wk; /* working copy, if needed */
197*404b540aSrobert uInt top = d32->bytes[0] & 0x7f; /* top byte, less sign bit */
198*404b540aSrobert decNumberZero (dn); /* clean target */
199*404b540aSrobert /* set the sign if negative */
200*404b540aSrobert if (decimal32Sign (d32))
201*404b540aSrobert dn->bits = DECNEG;
202*404b540aSrobert
203*404b540aSrobert if (top >= 0x78)
204*404b540aSrobert { /* is a special */
205*404b540aSrobert if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
206*404b540aSrobert dn->bits |= DECINF;
207*404b540aSrobert else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
208*404b540aSrobert dn->bits |= DECNAN;
209*404b540aSrobert else
210*404b540aSrobert dn->bits |= DECSNAN;
211*404b540aSrobert msd = 0; /* no top digit */
212*404b540aSrobert }
213*404b540aSrobert else
214*404b540aSrobert { /* have a finite number */
215*404b540aSrobert uInt comb = top >> 2; /* combination field */
216*404b540aSrobert uInt exp; /* working exponent */
217*404b540aSrobert
218*404b540aSrobert if (comb >= 0x18)
219*404b540aSrobert {
220*404b540aSrobert msd = 8 + (comb & 0x01);
221*404b540aSrobert exp = (comb & 0x06) << 5; /* MSBs */
222*404b540aSrobert }
223*404b540aSrobert else
224*404b540aSrobert {
225*404b540aSrobert msd = comb & 0x07;
226*404b540aSrobert exp = (comb & 0x18) << 3;
227*404b540aSrobert }
228*404b540aSrobert dn->exponent = exp + decimal32ExpCon (d32) - DECIMAL32_Bias; /* remove bias */
229*404b540aSrobert }
230*404b540aSrobert
231*404b540aSrobert /* get the coefficient, unless infinite */
232*404b540aSrobert if (!(dn->bits & DECINF))
233*404b540aSrobert {
234*404b540aSrobert Int bunches = DECIMAL32_Pmax / 3; /* coefficient full bunches to convert */
235*404b540aSrobert Int odd = 0; /* assume MSD is 0 (no odd bunch) */
236*404b540aSrobert if (msd != 0)
237*404b540aSrobert { /* coefficient has leading non-0 digit */
238*404b540aSrobert /* make a copy of the decimal32, with an extra bunch which has */
239*404b540aSrobert /* the top digit ready for conversion */
240*404b540aSrobert wk = *d32; /* take a copy */
241*404b540aSrobert wk.bytes[0] = 0; /* clear all but coecon */
242*404b540aSrobert wk.bytes[1] &= 0x0f; /* .. */
243*404b540aSrobert wk.bytes[1] |= (msd << 4); /* and prefix MSD */
244*404b540aSrobert odd++; /* indicate the extra */
245*404b540aSrobert d32 = &wk; /* use the work copy */
246*404b540aSrobert }
247*404b540aSrobert decDenseUnpackCoeff (d32->bytes, sizeof (d32->bytes), dn, bunches, odd);
248*404b540aSrobert }
249*404b540aSrobert return dn;
250*404b540aSrobert }
251*404b540aSrobert
252*404b540aSrobert /* ------------------------------------------------------------------ */
253*404b540aSrobert /* to-scientific-string -- conversion to numeric string */
254*404b540aSrobert /* to-engineering-string -- conversion to numeric string */
255*404b540aSrobert /* */
256*404b540aSrobert /* decimal32ToString(d32, string); */
257*404b540aSrobert /* decimal32ToEngString(d32, string); */
258*404b540aSrobert /* */
259*404b540aSrobert /* d32 is the decimal32 format number to convert */
260*404b540aSrobert /* string is the string where the result will be laid out */
261*404b540aSrobert /* */
262*404b540aSrobert /* string must be at least 24 characters */
263*404b540aSrobert /* */
264*404b540aSrobert /* No error is possible, and no status can be set. */
265*404b540aSrobert /* ------------------------------------------------------------------ */
266*404b540aSrobert char *
decimal32ToString(const decimal32 * d32,char * string)267*404b540aSrobert decimal32ToString (const decimal32 * d32, char *string)
268*404b540aSrobert {
269*404b540aSrobert decNumber dn; /* work */
270*404b540aSrobert decimal32ToNumber (d32, &dn);
271*404b540aSrobert decNumberToString (&dn, string);
272*404b540aSrobert return string;
273*404b540aSrobert }
274*404b540aSrobert
275*404b540aSrobert char *
decimal32ToEngString(const decimal32 * d32,char * string)276*404b540aSrobert decimal32ToEngString (const decimal32 * d32, char *string)
277*404b540aSrobert {
278*404b540aSrobert decNumber dn; /* work */
279*404b540aSrobert decimal32ToNumber (d32, &dn);
280*404b540aSrobert decNumberToEngString (&dn, string);
281*404b540aSrobert return string;
282*404b540aSrobert }
283*404b540aSrobert
284*404b540aSrobert /* ------------------------------------------------------------------ */
285*404b540aSrobert /* to-number -- conversion from numeric string */
286*404b540aSrobert /* */
287*404b540aSrobert /* decimal32FromString(result, string, set); */
288*404b540aSrobert /* */
289*404b540aSrobert /* result is the decimal32 format number which gets the result of */
290*404b540aSrobert /* the conversion */
291*404b540aSrobert /* *string is the character string which should contain a valid */
292*404b540aSrobert /* number (which may be a special value) */
293*404b540aSrobert /* set is the context */
294*404b540aSrobert /* */
295*404b540aSrobert /* The context is supplied to this routine is used for error handling */
296*404b540aSrobert /* (setting of status and traps) and for the rounding mode, only. */
297*404b540aSrobert /* If an error occurs, the result will be a valid decimal32 NaN. */
298*404b540aSrobert /* ------------------------------------------------------------------ */
299*404b540aSrobert decimal32 *
decimal32FromString(decimal32 * result,const char * string,decContext * set)300*404b540aSrobert decimal32FromString (decimal32 * result, const char *string, decContext * set)
301*404b540aSrobert {
302*404b540aSrobert decContext dc; /* work */
303*404b540aSrobert decNumber dn; /* .. */
304*404b540aSrobert
305*404b540aSrobert decContextDefault (&dc, DEC_INIT_DECIMAL32); /* no traps, please */
306*404b540aSrobert dc.round = set->round; /* use supplied rounding */
307*404b540aSrobert
308*404b540aSrobert decNumberFromString (&dn, string, &dc); /* will round if needed */
309*404b540aSrobert decimal32FromNumber (result, &dn, &dc);
310*404b540aSrobert if (dc.status != 0)
311*404b540aSrobert { /* something happened */
312*404b540aSrobert decContextSetStatus (set, dc.status); /* .. pass it on */
313*404b540aSrobert }
314*404b540aSrobert return result;
315*404b540aSrobert }
316*404b540aSrobert
317*404b540aSrobert #if DECTRACE || DECCHECK
318*404b540aSrobert /* ------------------------------------------------------------------ */
319*404b540aSrobert /* decimal32Show -- display a single in hexadecimal [debug aid] */
320*404b540aSrobert /* d32 -- the number to show */
321*404b540aSrobert /* ------------------------------------------------------------------ */
322*404b540aSrobert /* Also shows sign/cob/expconfields extracted */
323*404b540aSrobert void
decimal32Show(const decimal32 * d32)324*404b540aSrobert decimal32Show (const decimal32 * d32)
325*404b540aSrobert {
326*404b540aSrobert char buf[DECIMAL32_Bytes * 2 + 1];
327*404b540aSrobert Int i, j;
328*404b540aSrobert j = 0;
329*404b540aSrobert for (i = 0; i < DECIMAL32_Bytes; i++)
330*404b540aSrobert {
331*404b540aSrobert sprintf (&buf[j], "%02x", d32->bytes[i]);
332*404b540aSrobert j = j + 2;
333*404b540aSrobert }
334*404b540aSrobert printf (" D32> %s [S:%d Cb:%02x E:%d]\n", buf,
335*404b540aSrobert decimal32Sign (d32), decimal32Comb (d32), decimal32ExpCon (d32));
336*404b540aSrobert }
337*404b540aSrobert #endif
338