* alias.c (init_alias_analysis): Correct whitespace.
[official-gcc.git] / libdecnumber / decimal64.c
blob8ae6f102920d08b9b69d8e2f6c1ab43496ee1d87
1 /* Decimal 64-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
10 version.
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.
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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* ------------------------------------------------------------------ */
23 /* This module comprises the routines for decimal64 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] */
33 #define DECNUMDIGITS 16 /* we need decNumbers with space for 16 */
34 #include "config.h"
35 #include "decNumber.h" /* base number library */
36 #include "decNumberLocal.h" /* decNumber local types, etc. */
37 #include "decimal64.h" /* our primary include */
38 #include "decUtility.h" /* utility routines */
40 #if DECTRACE || DECCHECK
41 void decimal64Show (const decimal64 *); /* for debug */
42 void decNumberShow (const decNumber *); /* .. */
43 #endif
45 /* Useful macro */
46 /* Clear a structure (e.g., a decNumber) */
47 #define DEC_clear(d) memset(d, 0, sizeof(*d))
49 /* ------------------------------------------------------------------ */
50 /* decimal64FromNumber -- convert decNumber to decimal64 */
51 /* */
52 /* ds is the target decimal64 */
53 /* dn is the source number (assumed valid) */
54 /* set is the context, used only for reporting errors */
55 /* */
56 /* The set argument is used only for status reporting and for the */
57 /* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */
58 /* digits or an overflow is detected). If the exponent is out of the */
59 /* valid range then Overflow or Underflow will be raised. */
60 /* After Underflow a subnormal result is possible. */
61 /* */
62 /* DEC_Clamped is set if the number has to be 'folded down' to fit, */
63 /* by reducing its exponent and multiplying the coefficient by a */
64 /* power of ten, or if the exponent on a zero had to be clamped. */
65 /* ------------------------------------------------------------------ */
66 decimal64 *
67 decimal64FromNumber (decimal64 * d64, const decNumber * dn, decContext * set)
69 uInt status = 0; /* status accumulator */
70 Int pad = 0; /* coefficient pad digits */
71 decNumber dw; /* work */
72 decContext dc; /* .. */
73 uByte isneg = dn->bits & DECNEG; /* non-0 if original sign set */
74 uInt comb, exp; /* work */
76 /* If the number is finite, and has too many digits, or the exponent */
77 /* could be out of range then we reduce the number under the */
78 /* appropriate constraints */
79 if (!(dn->bits & DECSPECIAL))
80 { /* not a special value */
81 Int ae = dn->exponent + dn->digits - 1; /* adjusted exponent */
82 if (dn->digits > DECIMAL64_Pmax /* too many digits */
83 || ae > DECIMAL64_Emax /* likely overflow */
84 || ae < DECIMAL64_Emin)
85 { /* likely underflow */
86 decContextDefault (&dc, DEC_INIT_DECIMAL64); /* [no traps] */
87 dc.round = set->round; /* use supplied rounding */
88 decNumberPlus (&dw, dn, &dc); /* (round and check) */
89 /* [this changes -0 to 0, but it will be restored below] */
90 status |= dc.status; /* save status */
91 dn = &dw; /* use the work number */
93 /* [this could have pushed number to Infinity or zero, so this */
94 /* rounding must be done before we generate the decimal64] */
97 DEC_clear (d64); /* clean the target */
98 if (dn->bits & DECSPECIAL)
99 { /* a special value */
100 uByte top; /* work */
101 if (dn->bits & DECINF)
102 top = DECIMAL_Inf;
103 else
104 { /* sNaN or qNaN */
105 if ((*dn->lsu != 0 || dn->digits > 1) /* non-zero coefficient */
106 && (dn->digits < DECIMAL64_Pmax))
107 { /* coefficient fits */
108 decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), 0);
110 if (dn->bits & DECNAN)
111 top = DECIMAL_NaN;
112 else
113 top = DECIMAL_sNaN;
115 d64->bytes[0] = top;
117 else if (decNumberIsZero (dn))
118 { /* a zero */
119 /* set and clamp exponent */
120 if (dn->exponent < -DECIMAL64_Bias)
122 exp = 0;
123 status |= DEC_Clamped;
125 else
127 exp = dn->exponent + DECIMAL64_Bias; /* bias exponent */
128 if (exp > DECIMAL64_Ehigh)
129 { /* top clamp */
130 exp = DECIMAL64_Ehigh;
131 status |= DEC_Clamped;
134 comb = (exp >> 5) & 0x18; /* combination field */
135 d64->bytes[0] = (uByte) (comb << 2);
136 exp &= 0xff; /* remaining exponent bits */
137 decimal64SetExpCon (d64, exp);
139 else
140 { /* non-zero finite number */
141 uInt msd; /* work */
143 /* we have a dn that fits, but it may need to be padded */
144 exp = (uInt) (dn->exponent + DECIMAL64_Bias); /* bias exponent */
145 if (exp > DECIMAL64_Ehigh)
146 { /* fold-down case */
147 pad = exp - DECIMAL64_Ehigh;
148 exp = DECIMAL64_Ehigh; /* [to maximum] */
149 status |= DEC_Clamped;
152 decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), pad);
154 /* save and clear the top digit */
155 msd = ((unsigned) d64->bytes[1] >> 2) & 0x0f;
156 d64->bytes[1] &= 0x03;
157 /* create the combination field */
158 if (msd >= 8)
159 comb = 0x18 | (msd & 0x01) | ((exp >> 7) & 0x06);
160 else
161 comb = (msd & 0x07) | ((exp >> 5) & 0x18);
162 d64->bytes[0] = (uByte) (comb << 2);
163 exp &= 0xff; /* remaining exponent bits */
164 decimal64SetExpCon (d64, exp);
167 if (isneg)
168 decimal64SetSign (d64, 1);
169 if (status != 0)
170 decContextSetStatus (set, status); /* pass on status */
172 /*decimal64Show(d64); */
173 return d64;
176 /* ------------------------------------------------------------------ */
177 /* decimal64ToNumber -- convert decimal64 to decNumber */
178 /* d64 is the source decimal64 */
179 /* dn is the target number, with appropriate space */
180 /* No error is possible. */
181 /* ------------------------------------------------------------------ */
182 decNumber *
183 decimal64ToNumber (const decimal64 * d64, decNumber * dn)
185 uInt msd; /* coefficient MSD */
186 decimal64 wk; /* working copy, if needed */
187 uInt top = d64->bytes[0] & 0x7f; /* top byte, less sign bit */
188 decNumberZero (dn); /* clean target */
189 /* set the sign if negative */
190 if (decimal64Sign (d64))
191 dn->bits = DECNEG;
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 */
203 else
204 { /* have a finite number */
205 uInt comb = top >> 2; /* combination field */
206 uInt exp; /* exponent */
208 if (comb >= 0x18)
210 msd = 8 + (comb & 0x01);
211 exp = (comb & 0x06) << 7; /* MSBs */
213 else
215 msd = comb & 0x07;
216 exp = (comb & 0x18) << 5;
218 dn->exponent = exp + decimal64ExpCon (d64) - DECIMAL64_Bias; /* remove bias */
221 /* get the coefficient, unless infinite */
222 if (!(dn->bits & DECINF))
224 Int bunches = DECIMAL64_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 decimal64, with an extra bunch which has */
229 /* the top digit ready for conversion */
230 wk = *d64; /* take a copy */
231 wk.bytes[0] = 0; /* clear all but coecon */
232 wk.bytes[1] &= 0x03; /* .. */
233 wk.bytes[1] |= (msd << 2); /* and prefix MSD */
234 odd++; /* indicate the extra */
235 d64 = &wk; /* use the work copy */
237 decDenseUnpackCoeff (d64->bytes, sizeof (d64->bytes), dn, bunches, odd);
239 return dn;
242 /* ------------------------------------------------------------------ */
243 /* to-scientific-string -- conversion to numeric string */
244 /* to-engineering-string -- conversion to numeric string */
245 /* */
246 /* decimal64ToString(d64, string); */
247 /* decimal64ToEngString(d64, string); */
248 /* */
249 /* d64 is the decimal64 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 decimal64ToString (const decimal64 * d64, char *string)
259 decNumber dn; /* work */
260 decimal64ToNumber (d64, &dn);
261 decNumberToString (&dn, string);
262 return string;
265 char *
266 decimal64ToEngString (const decimal64 * d64, char *string)
268 decNumber dn; /* work */
269 decimal64ToNumber (d64, &dn);
270 decNumberToEngString (&dn, string);
271 return string;
274 /* ------------------------------------------------------------------ */
275 /* to-number -- conversion from numeric string */
276 /* */
277 /* decimal64FromString(result, string, set); */
278 /* */
279 /* result is the decimal64 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 decimal64 NaN. */
288 /* ------------------------------------------------------------------ */
289 decimal64 *
290 decimal64FromString (decimal64 * result, const char *string, decContext * set)
292 decContext dc; /* work */
293 decNumber dn; /* .. */
295 decContextDefault (&dc, DEC_INIT_DECIMAL64); /* no traps, please */
296 dc.round = set->round; /* use supplied rounding */
298 decNumberFromString (&dn, string, &dc); /* will round if needed */
300 decimal64FromNumber (result, &dn, &dc);
301 if (dc.status != 0)
302 { /* something happened */
303 decContextSetStatus (set, dc.status); /* .. pass it on */
305 return result;
308 #if DECTRACE || DECCHECK
309 /* ------------------------------------------------------------------ */
310 /* decimal64Show -- display a single in hexadecimal [debug aid] */
311 /* d64 -- the number to show */
312 /* ------------------------------------------------------------------ */
313 /* Also shows sign/cob/expconfields extracted */
314 void
315 decimal64Show (const decimal64 * d64)
317 char buf[DECIMAL64_Bytes * 2 + 1];
318 Int i, j;
319 j = 0;
320 for (i = 0; i < DECIMAL64_Bytes; i++)
322 sprintf (&buf[j], "%02x", d64->bytes[i]);
323 j = j + 2;
325 printf (" D64> %s [S:%d Cb:%02x E:%d]\n", buf,
326 decimal64Sign (d64), decimal64Comb (d64), decimal64ExpCon (d64));
328 #endif