1169695Skan/* Decimal 32-bit format module for the decNumber C Library
2169695Skan   Copyright (C) 2005 Free Software Foundation, Inc.
3169695Skan   Contributed by IBM Corporation.  Author Mike Cowlishaw.
4169695Skan
5169695Skan   This file is part of GCC.
6169695Skan
7169695Skan   GCC is free software; you can redistribute it and/or modify it under
8169695Skan   the terms of the GNU General Public License as published by the Free
9169695Skan   Software Foundation; either version 2, or (at your option) any later
10169695Skan   version.
11169695Skan
12169695Skan   In addition to the permissions in the GNU General Public License,
13169695Skan   the Free Software Foundation gives you unlimited permission to link
14169695Skan   the compiled version of this file into combinations with other
15169695Skan   programs, and to distribute those combinations without any
16169695Skan   restriction coming from the use of this file.  (The General Public
17169695Skan   License restrictions do apply in other respects; for example, they
18169695Skan   cover modification of the file, and distribution when not linked
19169695Skan   into a combine executable.)
20169695Skan
21169695Skan   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22169695Skan   WARRANTY; without even the implied warranty of MERCHANTABILITY or
23169695Skan   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24169695Skan   for more details.
25169695Skan
26169695Skan   You should have received a copy of the GNU General Public License
27169695Skan   along with GCC; see the file COPYING.  If not, write to the Free
28169695Skan   Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29169695Skan   02110-1301, USA.  */
30169695Skan
31169695Skan/* ------------------------------------------------------------------ */
32169695Skan/* This module comprises the routines for decimal32 format numbers.   */
33169695Skan/* Conversions are supplied to and from decNumber and String.         */
34169695Skan/*                                                                    */
35169695Skan/* No arithmetic routines are included; decNumber provides these.     */
36169695Skan/*                                                                    */
37169695Skan/* Error handling is the same as decNumber (qv.).                     */
38169695Skan/* ------------------------------------------------------------------ */
39169695Skan#include <string.h>		/* [for memset/memcpy] */
40169695Skan#include <stdio.h>		/* [for printf] */
41169695Skan
42169695Skan#define  DECNUMDIGITS  7	/* we need decNumbers with space for 7 */
43169695Skan#include "config.h"
44169695Skan#include "decNumber.h"		/* base number library */
45169695Skan#include "decNumberLocal.h"	/* decNumber local types, etc. */
46169695Skan#include "decimal32.h"		/* our primary include */
47169695Skan#include "decUtility.h"		/* utility routines */
48169695Skan
49169695Skan#if DECTRACE || DECCHECK
50169695Skanvoid decimal32Show (const decimal32 *);	/* for debug */
51169695Skanvoid decNumberShow (const decNumber *);	/* .. */
52169695Skan#endif
53169695Skan
54169695Skan/* Useful macro */
55169695Skan/* Clear a structure (e.g., a decNumber) */
56169695Skan#define DEC_clear(d) memset(d, 0, sizeof(*d))
57169695Skan
58169695Skan/* ------------------------------------------------------------------ */
59169695Skan/* decimal32FromNumber -- convert decNumber to decimal32              */
60169695Skan/*                                                                    */
61169695Skan/*   ds is the target decimal32                                       */
62169695Skan/*   dn is the source number (assumed valid)                          */
63169695Skan/*   set is the context, used only for reporting errors               */
64169695Skan/*                                                                    */
65169695Skan/* The set argument is used only for status reporting and for the     */
66169695Skan/* rounding mode (used if the coefficient is more than DECIMAL32_Pmax */
67169695Skan/* digits or an overflow is detected).  If the exponent is out of the */
68169695Skan/* valid range then Overflow or Underflow will be raised.             */
69169695Skan/* After Underflow a subnormal result is possible.                    */
70169695Skan/*                                                                    */
71169695Skan/* DEC_Clamped is set if the number has to be 'folded down' to fit,   */
72169695Skan/* by reducing its exponent and multiplying the coefficient by a      */
73169695Skan/* power of ten, or if the exponent on a zero had to be clamped.      */
74169695Skan/* ------------------------------------------------------------------ */
75169695Skandecimal32 *
76169695Skandecimal32FromNumber (decimal32 * d32, const decNumber * dn, decContext * set)
77169695Skan{
78169695Skan  uInt status = 0;		/* status accumulator */
79169695Skan  Int pad = 0;			/* coefficient pad digits */
80169695Skan  decNumber dw;			/* work */
81169695Skan  decContext dc;		/* .. */
82169695Skan  uByte isneg = dn->bits & DECNEG;	/* non-0 if original sign set */
83169695Skan  uInt comb, exp;		/* work */
84169695Skan
85169695Skan  /* If the number is finite, and has too many digits, or the exponent */
86169695Skan  /* could be out of range then we reduce the number under the */
87169695Skan  /* appropriate constraints */
88169695Skan  if (!(dn->bits & DECSPECIAL))
89169695Skan    {				/* not a special value */
90169695Skan      Int ae = dn->exponent + dn->digits - 1;	/* adjusted exponent */
91169695Skan      if (dn->digits > DECIMAL32_Pmax	/* too many digits */
92169695Skan	  || ae > DECIMAL32_Emax	/* likely overflow */
93169695Skan	  || ae < DECIMAL32_Emin)
94169695Skan	{			/* likely underflow */
95169695Skan	  decContextDefault (&dc, DEC_INIT_DECIMAL32);	/* [no traps] */
96169695Skan	  dc.round = set->round;	/* use supplied rounding */
97169695Skan	  decNumberPlus (&dw, dn, &dc);	/* (round and check) */
98169695Skan	  /* [this changes -0 to 0, but it will be restored below] */
99169695Skan	  status |= dc.status;	/* save status */
100169695Skan	  dn = &dw;		/* use the work number */
101169695Skan	}
102169695Skan      /* [this could have pushed number to Infinity or zero, so this */
103169695Skan      /* rounding must be done before we generate the decimal32] */
104169695Skan    }
105169695Skan
106169695Skan  DEC_clear (d32);		/* clean the target */
107169695Skan  if (dn->bits & DECSPECIAL)
108169695Skan    {				/* a special value */
109169695Skan      uByte top;		/* work */
110169695Skan      if (dn->bits & DECINF)
111169695Skan	top = DECIMAL_Inf;
112169695Skan      else
113169695Skan	{			/* sNaN or qNaN */
114169695Skan	  if ((*dn->lsu != 0 || dn->digits > 1)	/* non-zero coefficient */
115169695Skan	      && (dn->digits < DECIMAL32_Pmax))
116169695Skan	    {			/* coefficient fits */
117169695Skan	      decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), 0);
118169695Skan	    }
119169695Skan	  if (dn->bits & DECNAN)
120169695Skan	    top = DECIMAL_NaN;
121169695Skan	  else
122169695Skan	    top = DECIMAL_sNaN;
123169695Skan	}
124169695Skan      d32->bytes[0] = top;
125169695Skan    }
126169695Skan  else if (decNumberIsZero (dn))
127169695Skan    {				/* a zero */
128169695Skan      /* set and clamp exponent */
129169695Skan      if (dn->exponent < -DECIMAL32_Bias)
130169695Skan	{
131169695Skan	  exp = 0;
132169695Skan	  status |= DEC_Clamped;
133169695Skan	}
134169695Skan      else
135169695Skan	{
136169695Skan	  exp = dn->exponent + DECIMAL32_Bias;	/* bias exponent */
137169695Skan	  if (exp > DECIMAL32_Ehigh)
138169695Skan	    {			/* top clamp */
139169695Skan	      exp = DECIMAL32_Ehigh;
140169695Skan	      status |= DEC_Clamped;
141169695Skan	    }
142169695Skan	}
143169695Skan      comb = (exp >> 3) & 0x18;	/* combination field */
144169695Skan      d32->bytes[0] = (uByte) (comb << 2);
145169695Skan      exp &= 0x3f;		/* remaining exponent bits */
146169695Skan      decimal32SetExpCon (d32, exp);
147169695Skan    }
148169695Skan  else
149169695Skan    {				/* non-zero finite number */
150169695Skan      uInt msd;			/* work */
151169695Skan
152169695Skan      /* we have a dn that fits, but it may need to be padded */
153169695Skan      exp = (uInt) (dn->exponent + DECIMAL32_Bias);	/* bias exponent */
154169695Skan
155169695Skan      if (exp > DECIMAL32_Ehigh)
156169695Skan	{			/* fold-down case */
157169695Skan	  pad = exp - DECIMAL32_Ehigh;
158169695Skan	  exp = DECIMAL32_Ehigh;	/* [to maximum] */
159169695Skan	  status |= DEC_Clamped;
160169695Skan	}
161169695Skan
162169695Skan      decDensePackCoeff (dn, d32->bytes, sizeof (d32->bytes), pad);
163169695Skan
164169695Skan      /* save and clear the top digit */
165169695Skan      msd = ((unsigned) d32->bytes[1] >> 4);
166169695Skan      d32->bytes[1] &= 0x0f;
167169695Skan      /* create the combination field */
168169695Skan      if (msd >= 8)
169169695Skan	comb = 0x18 | (msd & 0x01) | ((exp >> 5) & 0x06);
170169695Skan      else
171169695Skan	comb = (msd & 0x07) | ((exp >> 3) & 0x18);
172169695Skan      d32->bytes[0] = (uByte) (comb << 2);
173169695Skan      exp &= 0x3f;		/* remaining exponent bits */
174169695Skan      decimal32SetExpCon (d32, exp);
175169695Skan    }
176169695Skan
177169695Skan  if (isneg)
178169695Skan    decimal32SetSign (d32, 1);
179169695Skan  if (status != 0)
180169695Skan    decContextSetStatus (set, status);	/* pass on status */
181169695Skan
182169695Skan  /*decimal32Show(d32); */
183169695Skan  return d32;
184169695Skan}
185169695Skan
186169695Skan/* ------------------------------------------------------------------ */
187169695Skan/* decimal32ToNumber -- convert decimal32 to decNumber                */
188169695Skan/*   d32 is the source decimal32                                      */
189169695Skan/*   dn is the target number, with appropriate space                  */
190169695Skan/* No error is possible.                                              */
191169695Skan/* ------------------------------------------------------------------ */
192169695SkandecNumber *
193169695Skandecimal32ToNumber (const decimal32 * d32, decNumber * dn)
194169695Skan{
195169695Skan  uInt msd;			/* coefficient MSD */
196169695Skan  decimal32 wk;			/* working copy, if needed */
197169695Skan  uInt top = d32->bytes[0] & 0x7f;	/* top byte, less sign bit */
198169695Skan  decNumberZero (dn);		/* clean target */
199169695Skan  /* set the sign if negative */
200169695Skan  if (decimal32Sign (d32))
201169695Skan    dn->bits = DECNEG;
202169695Skan
203169695Skan  if (top >= 0x78)
204169695Skan    {				/* is a special */
205169695Skan      if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
206169695Skan	dn->bits |= DECINF;
207169695Skan      else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
208169695Skan	dn->bits |= DECNAN;
209169695Skan      else
210169695Skan	dn->bits |= DECSNAN;
211169695Skan      msd = 0;			/* no top digit */
212169695Skan    }
213169695Skan  else
214169695Skan    {				/* have a finite number */
215169695Skan      uInt comb = top >> 2;	/* combination field */
216169695Skan      uInt exp;			/* working exponent */
217169695Skan
218169695Skan      if (comb >= 0x18)
219169695Skan	{
220169695Skan	  msd = 8 + (comb & 0x01);
221169695Skan	  exp = (comb & 0x06) << 5;	/* MSBs */
222169695Skan	}
223169695Skan      else
224169695Skan	{
225169695Skan	  msd = comb & 0x07;
226169695Skan	  exp = (comb & 0x18) << 3;
227169695Skan	}
228169695Skan      dn->exponent = exp + decimal32ExpCon (d32) - DECIMAL32_Bias;	/* remove bias */
229169695Skan    }
230169695Skan
231169695Skan  /* get the coefficient, unless infinite */
232169695Skan  if (!(dn->bits & DECINF))
233169695Skan    {
234169695Skan      Int bunches = DECIMAL32_Pmax / 3;	/* coefficient full bunches to convert */
235169695Skan      Int odd = 0;		/* assume MSD is 0 (no odd bunch) */
236169695Skan      if (msd != 0)
237169695Skan	{			/* coefficient has leading non-0 digit */
238169695Skan	  /* make a copy of the decimal32, with an extra bunch which has */
239169695Skan	  /* the top digit ready for conversion */
240169695Skan	  wk = *d32;		/* take a copy */
241169695Skan	  wk.bytes[0] = 0;	/* clear all but coecon */
242169695Skan	  wk.bytes[1] &= 0x0f;	/* .. */
243169695Skan	  wk.bytes[1] |= (msd << 4);	/* and prefix MSD */
244169695Skan	  odd++;		/* indicate the extra */
245169695Skan	  d32 = &wk;		/* use the work copy */
246169695Skan	}
247169695Skan      decDenseUnpackCoeff (d32->bytes, sizeof (d32->bytes), dn, bunches, odd);
248169695Skan    }
249169695Skan  return dn;
250169695Skan}
251169695Skan
252169695Skan/* ------------------------------------------------------------------ */
253169695Skan/* to-scientific-string -- conversion to numeric string               */
254169695Skan/* to-engineering-string -- conversion to numeric string              */
255169695Skan/*                                                                    */
256169695Skan/*   decimal32ToString(d32, string);                                  */
257169695Skan/*   decimal32ToEngString(d32, string);                               */
258169695Skan/*                                                                    */
259169695Skan/*  d32 is the decimal32 format number to convert                     */
260169695Skan/*  string is the string where the result will be laid out            */
261169695Skan/*                                                                    */
262169695Skan/*  string must be at least 24 characters                             */
263169695Skan/*                                                                    */
264169695Skan/*  No error is possible, and no status can be set.                   */
265169695Skan/* ------------------------------------------------------------------ */
266169695Skanchar *
267169695Skandecimal32ToString (const decimal32 * d32, char *string)
268169695Skan{
269169695Skan  decNumber dn;			/* work */
270169695Skan  decimal32ToNumber (d32, &dn);
271169695Skan  decNumberToString (&dn, string);
272169695Skan  return string;
273169695Skan}
274169695Skan
275169695Skanchar *
276169695Skandecimal32ToEngString (const decimal32 * d32, char *string)
277169695Skan{
278169695Skan  decNumber dn;			/* work */
279169695Skan  decimal32ToNumber (d32, &dn);
280169695Skan  decNumberToEngString (&dn, string);
281169695Skan  return string;
282169695Skan}
283169695Skan
284169695Skan/* ------------------------------------------------------------------ */
285169695Skan/* to-number -- conversion from numeric string                        */
286169695Skan/*                                                                    */
287169695Skan/*   decimal32FromString(result, string, set);                        */
288169695Skan/*                                                                    */
289169695Skan/*  result  is the decimal32 format number which gets the result of   */
290169695Skan/*          the conversion                                            */
291169695Skan/*  *string is the character string which should contain a valid      */
292169695Skan/*          number (which may be a special value)                     */
293169695Skan/*  set     is the context                                            */
294169695Skan/*                                                                    */
295169695Skan/* The context is supplied to this routine is used for error handling */
296169695Skan/* (setting of status and traps) and for the rounding mode, only.     */
297169695Skan/* If an error occurs, the result will be a valid decimal32 NaN.      */
298169695Skan/* ------------------------------------------------------------------ */
299169695Skandecimal32 *
300169695Skandecimal32FromString (decimal32 * result, const char *string, decContext * set)
301169695Skan{
302169695Skan  decContext dc;		/* work */
303169695Skan  decNumber dn;			/* .. */
304169695Skan
305169695Skan  decContextDefault (&dc, DEC_INIT_DECIMAL32);	/* no traps, please */
306169695Skan  dc.round = set->round;	/* use supplied rounding */
307169695Skan
308169695Skan  decNumberFromString (&dn, string, &dc);	/* will round if needed */
309169695Skan  decimal32FromNumber (result, &dn, &dc);
310169695Skan  if (dc.status != 0)
311169695Skan    {				/* something happened */
312169695Skan      decContextSetStatus (set, dc.status);	/* .. pass it on */
313169695Skan    }
314169695Skan  return result;
315169695Skan}
316169695Skan
317169695Skan#if DECTRACE || DECCHECK
318169695Skan/* ------------------------------------------------------------------ */
319169695Skan/* decimal32Show -- display a single in hexadecimal [debug aid]       */
320169695Skan/*   d32 -- the number to show                                        */
321169695Skan/* ------------------------------------------------------------------ */
322169695Skan/* Also shows sign/cob/expconfields extracted */
323169695Skanvoid
324169695Skandecimal32Show (const decimal32 * d32)
325169695Skan{
326169695Skan  char buf[DECIMAL32_Bytes * 2 + 1];
327169695Skan  Int i, j;
328169695Skan  j = 0;
329169695Skan  for (i = 0; i < DECIMAL32_Bytes; i++)
330169695Skan    {
331169695Skan      sprintf (&buf[j], "%02x", d32->bytes[i]);
332169695Skan      j = j + 2;
333169695Skan    }
334169695Skan  printf (" D32> %s [S:%d Cb:%02x E:%d]\n", buf,
335169695Skan	  decimal32Sign (d32), decimal32Comb (d32), decimal32ExpCon (d32));
336169695Skan}
337169695Skan#endif
338