OSDN Git Service

* libdecnumber: Import decNumber sources from the dfp-branch.
[pf3gnuchains/gcc-fork.git] / libdecnumber / decimal32.c
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.
4
5    This file is part of GCC.
6
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
10    version.
11
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
15    for more details.
16
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
20    02111-1307, USA.  */
21
22 /* ------------------------------------------------------------------ */
23 /* This module comprises the routines for decimal32 format numbers.   */
24 /* Conversions are supplied to and from decNumber and String.         */
25 /*                                                                    */
26 /* No arithmetic routines are included; decNumber provides these.     */
27 /*                                                                    */
28 /* Error handling is the same as decNumber (qv.).                     */
29 /* ------------------------------------------------------------------ */
30 #include <string.h>             /* [for memset/memcpy] */
31 #include <stdio.h>              /* [for printf] */
32
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 */
38
39 #if DECTRACE || DECCHECK
40 void decimal32Show (decimal32 *);       /* for debug */
41 void decNumberShow (decNumber *);       /* .. */
42 #endif
43
44 /* Useful macro */
45 /* Clear a structure (e.g., a decNumber) */
46 #define DEC_clear(d) memset(d, 0, sizeof(*d))
47
48 /* ------------------------------------------------------------------ */
49 /* decimal32FromNumber -- convert decNumber to decimal32              */
50 /*                                                                    */
51 /*   ds is the target decimal32                                       */
52 /*   dn is the source number (assumed valid)                          */
53 /*   set is the context, used only for reporting errors               */
54 /*                                                                    */
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.                    */
60 /*                                                                    */
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 /* ------------------------------------------------------------------ */
65 decimal32 *
66 decimal32FromNumber (decimal32 * d32, decNumber * dn, decContext * set)
67 {
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 */
74
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 */
91         }
92       /* [this could have pushed number to Infinity or zero, so this */
93       /* rounding must be done before we generate the decimal32] */
94     }
95
96   DEC_clear (d32);              /* clean the target */
97   if (dn->bits & DECSPECIAL)
98     {                           /* a special value */
99       uByte top;                /* work */
100       if (dn->bits & DECINF)
101         top = DECIMAL_Inf;
102       else
103         {                       /* sNaN or qNaN */
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);
108             }
109           if (dn->bits & DECNAN)
110             top = DECIMAL_NaN;
111           else
112             top = DECIMAL_sNaN;
113         }
114       d32->bytes[0] = top;
115     }
116   else if (decNumberIsZero (dn))
117     {                           /* a zero */
118       /* set and clamp exponent */
119       if (dn->exponent < -DECIMAL32_Bias)
120         {
121           exp = 0;
122           status |= DEC_Clamped;
123         }
124       else
125         {
126           exp = dn->exponent + DECIMAL32_Bias;  /* bias exponent */
127           if (exp > DECIMAL32_Ehigh)
128             {                   /* top clamp */
129               exp = DECIMAL32_Ehigh;
130               status |= DEC_Clamped;
131             }
132         }
133       comb = (exp >> 3) & 0x18; /* combination field */
134       d32->bytes[0] = (uByte) (comb << 2);
135       exp &= 0x3f;              /* remaining exponent bits */
136       decimal32SetExpCon (d32, exp);
137     }
138   else
139     {                           /* non-zero finite number */
140       uInt msd;                 /* work */
141
142       /* we have a dn that fits, but it may need to be padded */
143       exp = (uInt) (dn->exponent + DECIMAL32_Bias);     /* bias exponent */
144
145       if (exp > DECIMAL32_Ehigh)
146         {                       /* fold-down case */
147           pad = exp - DECIMAL32_Ehigh;
148           exp = DECIMAL32_Ehigh;        /* [to maximum] */
149           status |= DEC_Clamped;
150         }
151
152       decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), pad);
153
154       /* save and clear the top digit */
155       msd = ((unsigned) d32->bytes[1] >> 4);
156       d32->bytes[1] &= 0x0f;
157       /* create the combination field */
158       if (msd >= 8)
159         comb = 0x18 | (msd & 0x01) | ((exp >> 5) & 0x06);
160       else
161         comb = (msd & 0x07) | ((exp >> 3) & 0x18);
162       d32->bytes[0] = (uByte) (comb << 2);
163       exp &= 0x3f;              /* remaining exponent bits */
164       decimal32SetExpCon (d32, exp);
165     }
166
167   if (isneg)
168     decimal32SetSign (d32, 1);
169   if (status != 0)
170     decContextSetStatus (set, status);  /* pass on status */
171
172   /*decimal32Show(d32); */
173   return d32;
174 }
175
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 /* ------------------------------------------------------------------ */
182 decNumber *
183 decimal32ToNumber (decimal32 * d32, decNumber * dn)
184 {
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))
191     dn->bits = DECNEG;
192
193   if (top >= 0x78)
194     {                           /* is a special */
195       if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
196         dn->bits |= DECINF;
197       else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
198         dn->bits |= DECNAN;
199       else
200         dn->bits |= DECSNAN;
201       msd = 0;                  /* no top digit */
202     }
203   else
204     {                           /* have a finite number */
205       uInt comb = top >> 2;     /* combination field */
206       uInt exp;                 /* working exponent */
207
208       if (comb >= 0x18)
209         {
210           msd = 8 + (comb & 0x01);
211           exp = (comb & 0x06) << 5;     /* MSBs */
212         }
213       else
214         {
215           msd = comb & 0x07;
216           exp = (comb & 0x18) << 3;
217         }
218       dn->exponent = exp + decimal32ExpCon (d32) - DECIMAL32_Bias;      /* remove bias */
219     }
220
221   /* get the coefficient, unless infinite */
222   if (!(dn->bits & DECINF))
223     {
224       Int bunches = DECIMAL32_Pmax / 3; /* coefficient full bunches to convert */
225       Int odd = 0;              /* assume MSD is 0 (no odd bunch) */
226       if (msd != 0)
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 */
236         }
237       decDenseUnpackCoeff (d32->bytes, sizeof (d32->bytes), dn, bunches, odd);
238     }
239   return dn;
240 }
241
242 /* ------------------------------------------------------------------ */
243 /* to-scientific-string -- conversion to numeric string               */
244 /* to-engineering-string -- conversion to numeric string              */
245 /*                                                                    */
246 /*   decimal32ToString(d32, string);                                  */
247 /*   decimal32ToEngString(d32, string);                               */
248 /*                                                                    */
249 /*  d32 is the decimal32 format number to convert                     */
250 /*  string is the string where the result will be laid out            */
251 /*                                                                    */
252 /*  string must be at least 24 characters                             */
253 /*                                                                    */
254 /*  No error is possible, and no status can be set.                   */
255 /* ------------------------------------------------------------------ */
256 char *
257 decimal32ToString (decimal32 * d32, char *string)
258 {
259   decNumber dn;                 /* work */
260   decimal32ToNumber (d32, &dn);
261   decNumberToString (&dn, string);
262   return string;
263 }
264
265 char *
266 decimal32ToEngString (decimal32 * d32, char *string)
267 {
268   decNumber dn;                 /* work */
269   decimal32ToNumber (d32, &dn);
270   decNumberToEngString (&dn, string);
271   return string;
272 }
273
274 /* ------------------------------------------------------------------ */
275 /* to-number -- conversion from numeric string                        */
276 /*                                                                    */
277 /*   decimal32FromString(result, string, set);                        */
278 /*                                                                    */
279 /*  result  is the decimal32 format number which gets the result of   */
280 /*          the conversion                                            */
281 /*  *string is the character string which should contain a valid      */
282 /*          number (which may be a special value)                     */
283 /*  set     is the context                                            */
284 /*                                                                    */
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 /* ------------------------------------------------------------------ */
289 decimal32 *
290 decimal32FromString (decimal32 * result, char *string, decContext * set)
291 {
292   decContext dc;                /* work */
293   decNumber dn;                 /* .. */
294
295   decContextDefault (&dc, DEC_INIT_DECIMAL32);  /* no traps, please */
296   dc.round = set->round;        /* use supplied rounding */
297
298   decNumberFromString (&dn, string, &dc);       /* will round if needed */
299   decimal32FromNumber (result, &dn, &dc);
300   if (dc.status != 0)
301     {                           /* something happened */
302       decContextSetStatus (set, dc.status);     /* .. pass it on */
303     }
304   return result;
305 }
306
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 */
313 void
314 decimal32Show (decimal32 * d32)
315 {
316   char buf[DECIMAL32_Bytes * 2 + 1];
317   Int i, j;
318   j = 0;
319   for (i = 0; i < DECIMAL32_Bytes; i++)
320     {
321       sprintf (&buf[j], "%02x", d32->bytes[i]);
322       j = j + 2;
323     }
324   printf (" D32> %s [S:%d Cb:%02x E:%d]\n", buf,
325           decimal32Sign (d32), decimal32Comb (d32), decimal32ExpCon (d32));
326 }
327 #endif