OSDN Git Service

Autoconfiscate this directory:
[pf3gnuchains/gcc-fork.git] / libdecnumber / decimal128.c
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.
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 decimal128 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 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 */
38
39 #if DECTRACE || DECCHECK
40 void decimal128Show (decimal128 *);     /* 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 /* decimal128FromNumber -- convert decNumber to decimal128            */
50 /*                                                                    */
51 /*   ds is the target decimal128                                      */
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 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.                    */
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 decimal128 *
66 decimal128FromNumber (decimal128 * d128, 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 > 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 */
91         }
92       /* [this could have pushed number to Infinity or zero, so this */
93       /* rounding must be done before we generate the decimal128] */
94     }
95
96   DEC_clear (d128);             /* 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 < DECIMAL128_Pmax))
106             {                   /* coefficient fits */
107               decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), 0);
108             }
109           if (dn->bits & DECNAN)
110             top = DECIMAL_NaN;
111           else
112             top = DECIMAL_sNaN;
113         }
114       d128->bytes[0] = top;
115     }
116   else if (decNumberIsZero (dn))
117     {                           /* a zero */
118       /* set and clamp exponent */
119       if (dn->exponent < -DECIMAL128_Bias)
120         {
121           exp = 0;
122           status |= DEC_Clamped;
123         }
124       else
125         {
126           exp = dn->exponent + DECIMAL128_Bias; /* bias exponent */
127           if (exp > DECIMAL128_Ehigh)
128             {                   /* top clamp */
129               exp = DECIMAL128_Ehigh;
130               status |= DEC_Clamped;
131             }
132         }
133       comb = (exp >> 9) & 0x18; /* combination field */
134       d128->bytes[0] = (uByte) (comb << 2);
135       exp &= 0xfff;             /* remaining exponent bits */
136       decimal128SetExpCon (d128, 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 + DECIMAL128_Bias);    /* bias exponent */
144
145       if (exp > DECIMAL128_Ehigh)
146         {                       /* fold-down case */
147           pad = exp - DECIMAL128_Ehigh;
148           exp = DECIMAL128_Ehigh;       /* [to maximum] */
149           status |= DEC_Clamped;
150         }
151
152       decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), pad);
153
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;
159
160       /* create the combination field */
161       if (msd >= 8)
162         comb = 0x18 | (msd & 0x01) | ((exp >> 11) & 0x06);
163       else
164         comb = (msd & 0x07) | ((exp >> 9) & 0x18);
165       d128->bytes[0] = (uByte) (comb << 2);
166       exp &= 0xfff;             /* remaining exponent bits */
167       decimal128SetExpCon (d128, exp);
168     }
169
170   if (isneg)
171     decimal128SetSign (d128, 1);
172   if (status != 0)
173     decContextSetStatus (set, status);  /* pass on status */
174
175   /* decimal128Show(d128); */
176   return d128;
177 }
178
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 /* ------------------------------------------------------------------ */
185 decNumber *
186 decimal128ToNumber (decimal128 * d128, decNumber * dn)
187 {
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))
194     dn->bits = DECNEG;
195
196   if (top >= 0x78)
197     {                           /* is a special */
198       if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
199         dn->bits |= DECINF;
200       else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
201         dn->bits |= DECNAN;
202       else
203         dn->bits |= DECSNAN;
204       msd = 0;                  /* no top digit */
205     }
206   else
207     {                           /* have a finite number */
208       uInt comb = top >> 2;     /* combination field */
209       uInt exp;                 /* exponent */
210
211       if (comb >= 0x18)
212         {
213           msd = 8 + (comb & 0x01);
214           exp = (comb & 0x06) << 11;    /* MSBs */
215         }
216       else
217         {
218           msd = comb & 0x07;
219           exp = (comb & 0x18) << 9;
220         }
221       dn->exponent = exp + decimal128ExpCon (d128) - DECIMAL128_Bias;   /* remove bias */
222     }
223
224   /* get the coefficient, unless infinite */
225   if (!(dn->bits & DECINF))
226     {
227       Int bunches = DECIMAL128_Pmax / 3;        /* coefficient full bunches to convert */
228       Int odd = 0;              /* assume MSD is 0 (no odd bunch) */
229       if (msd != 0)
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 */
241         }
242       decDenseUnpackCoeff (d128->bytes, sizeof (d128->bytes), dn, bunches,
243                            odd);
244     }
245
246   /* decNumberShow(dn); */
247   return dn;
248 }
249
250 /* ------------------------------------------------------------------ */
251 /* to-scientific-string -- conversion to numeric string               */
252 /* to-engineering-string -- conversion to numeric string              */
253 /*                                                                    */
254 /*   decimal128ToString(d128, string);                                */
255 /*   decimal128ToEngString(d128, string);                             */
256 /*                                                                    */
257 /*  d128 is the decimal128 format number to convert                   */
258 /*  string is the string where the result will be laid out            */
259 /*                                                                    */
260 /*  string must be at least 24 characters                             */
261 /*                                                                    */
262 /*  No error is possible, and no status can be set.                   */
263 /* ------------------------------------------------------------------ */
264 char *
265 decimal128ToString (decimal128 * d128, char *string)
266 {
267   decNumber dn;                 /* work */
268   decimal128ToNumber (d128, &dn);
269   decNumberToString (&dn, string);
270   return string;
271 }
272
273 char *
274 decimal128ToEngString (decimal128 * d128, char *string)
275 {
276   decNumber dn;                 /* work */
277   decimal128ToNumber (d128, &dn);
278   decNumberToEngString (&dn, string);
279   return string;
280 }
281
282 /* ------------------------------------------------------------------ */
283 /* to-number -- conversion from numeric string                        */
284 /*                                                                    */
285 /*   decimal128FromString(result, string, set);                       */
286 /*                                                                    */
287 /*  result  is the decimal128 format number which gets the result of  */
288 /*          the conversion                                            */
289 /*  *string is the character string which should contain a valid      */
290 /*          number (which may be a special value)                     */
291 /*  set     is the context                                            */
292 /*                                                                    */
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 /* ------------------------------------------------------------------ */
297 decimal128 *
298 decimal128FromString (decimal128 * result, char *string, decContext * set)
299 {
300   decContext dc;                /* work */
301   decNumber dn;                 /* .. */
302
303   decContextDefault (&dc, DEC_INIT_DECIMAL128); /* no traps, please */
304   dc.round = set->round;        /* use supplied rounding */
305
306   decNumberFromString (&dn, string, &dc);       /* will round if needed */
307   decimal128FromNumber (result, &dn, &dc);
308   if (dc.status != 0)
309     {                           /* something happened */
310       decContextSetStatus (set, dc.status);     /* .. pass it on */
311     }
312   return result;
313 }
314
315
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 */
322 void
323 decimal128Show (decimal128 * d128)
324 {
325   char buf[DECIMAL128_Bytes * 2 + 1];
326   Int i, j;
327   j = 0;
328   for (i = 0; i < DECIMAL128_Bytes; i++)
329     {
330       sprintf (&buf[j], "%02x", d128->bytes[i]);
331       j = j + 2;
332     }
333   printf (" D128> %s [S:%d Cb:%02x E:%d]\n", buf,
334           decimal128Sign (d128), decimal128Comb (d128),
335           decimal128ExpCon (d128));
336 }
337 #endif