1169695Skan/* Decimal 128-bit format module from 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 decimal128 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 34	/* we need decNumbers with space for 34 */
43169695Skan#include "config.h"
44169695Skan#include "decNumber.h"		/* base number library */
45169695Skan#include "decNumberLocal.h"	/* decNumber local types, etc. */
46169695Skan#include "decimal128.h"		/* our primary include */
47169695Skan#include "decUtility.h"		/* utility routines */
48169695Skan
49169695Skan#if DECTRACE || DECCHECK
50169695Skanvoid decimal128Show (const decimal128 *);	/* 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/* decimal128FromNumber -- convert decNumber to decimal128            */
60169695Skan/*                                                                    */
61169695Skan/*   ds is the target decimal128                                      */
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 DECIMAL128_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/* ------------------------------------------------------------------ */
75169695Skandecimal128 *
76169695Skandecimal128FromNumber (decimal128 * d128, 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 > DECIMAL128_Pmax	/* too many digits */
92169695Skan	  || ae > DECIMAL128_Emax	/* likely overflow */
93169695Skan	  || ae < DECIMAL128_Emin)
94169695Skan	{			/* likely underflow */
95169695Skan	  decContextDefault (&dc, DEC_INIT_DECIMAL128);	/* [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 decimal128] */
104169695Skan    }
105169695Skan
106169695Skan  DEC_clear (d128);		/* 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 < DECIMAL128_Pmax))
116169695Skan	    {			/* coefficient fits */
117169695Skan	      decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), 0);
118169695Skan	    }
119169695Skan	  if (dn->bits & DECNAN)
120169695Skan	    top = DECIMAL_NaN;
121169695Skan	  else
122169695Skan	    top = DECIMAL_sNaN;
123169695Skan	}
124169695Skan      d128->bytes[0] = top;
125169695Skan    }
126169695Skan  else if (decNumberIsZero (dn))
127169695Skan    {				/* a zero */
128169695Skan      /* set and clamp exponent */
129169695Skan      if (dn->exponent < -DECIMAL128_Bias)
130169695Skan	{
131169695Skan	  exp = 0;
132169695Skan	  status |= DEC_Clamped;
133169695Skan	}
134169695Skan      else
135169695Skan	{
136169695Skan	  exp = dn->exponent + DECIMAL128_Bias;	/* bias exponent */
137169695Skan	  if (exp > DECIMAL128_Ehigh)
138169695Skan	    {			/* top clamp */
139169695Skan	      exp = DECIMAL128_Ehigh;
140169695Skan	      status |= DEC_Clamped;
141169695Skan	    }
142169695Skan	}
143169695Skan      comb = (exp >> 9) & 0x18;	/* combination field */
144169695Skan      d128->bytes[0] = (uByte) (comb << 2);
145169695Skan      exp &= 0xfff;		/* remaining exponent bits */
146169695Skan      decimal128SetExpCon (d128, 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 + DECIMAL128_Bias);	/* bias exponent */
154169695Skan
155169695Skan      if (exp > DECIMAL128_Ehigh)
156169695Skan	{			/* fold-down case */
157169695Skan	  pad = exp - DECIMAL128_Ehigh;
158169695Skan	  exp = DECIMAL128_Ehigh;	/* [to maximum] */
159169695Skan	  status |= DEC_Clamped;
160169695Skan	}
161169695Skan
162169695Skan      decDensePackCoeff (dn, d128->bytes, sizeof (d128->bytes), pad);
163169695Skan
164169695Skan      /* save and clear the top digit */
165169695Skan      msd = ((unsigned) d128->bytes[1] << 2) & 0x0c;	/* top 2 bits */
166169695Skan      msd |= ((unsigned) d128->bytes[2] >> 6);	/* low 2 bits */
167169695Skan      d128->bytes[1] &= 0xfc;
168169695Skan      d128->bytes[2] &= 0x3f;
169169695Skan
170169695Skan      /* create the combination field */
171169695Skan      if (msd >= 8)
172169695Skan	comb = 0x18 | (msd & 0x01) | ((exp >> 11) & 0x06);
173169695Skan      else
174169695Skan	comb = (msd & 0x07) | ((exp >> 9) & 0x18);
175169695Skan      d128->bytes[0] = (uByte) (comb << 2);
176169695Skan      exp &= 0xfff;		/* remaining exponent bits */
177169695Skan      decimal128SetExpCon (d128, exp);
178169695Skan    }
179169695Skan
180169695Skan  if (isneg)
181169695Skan    decimal128SetSign (d128, 1);
182169695Skan  if (status != 0)
183169695Skan    decContextSetStatus (set, status);	/* pass on status */
184169695Skan
185169695Skan  /* decimal128Show(d128); */
186169695Skan  return d128;
187169695Skan}
188169695Skan
189169695Skan/* ------------------------------------------------------------------ */
190169695Skan/* decimal128ToNumber -- convert decimal128 to decNumber              */
191169695Skan/*   d128 is the source decimal128                                    */
192169695Skan/*   dn is the target number, with appropriate space                  */
193169695Skan/* No error is possible.                                              */
194169695Skan/* ------------------------------------------------------------------ */
195169695SkandecNumber *
196169695Skandecimal128ToNumber (const decimal128 * d128, decNumber * dn)
197169695Skan{
198169695Skan  uInt msd;			/* coefficient MSD */
199169695Skan  decimal128 wk;		/* working copy, if needed */
200169695Skan  uInt top = d128->bytes[0] & 0x7f;	/* top byte, less sign bit */
201169695Skan  decNumberZero (dn);		/* clean target */
202169695Skan  /* set the sign if negative */
203169695Skan  if (decimal128Sign (d128))
204169695Skan    dn->bits = DECNEG;
205169695Skan
206169695Skan  if (top >= 0x78)
207169695Skan    {				/* is a special */
208169695Skan      if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
209169695Skan	dn->bits |= DECINF;
210169695Skan      else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
211169695Skan	dn->bits |= DECNAN;
212169695Skan      else
213169695Skan	dn->bits |= DECSNAN;
214169695Skan      msd = 0;			/* no top digit */
215169695Skan    }
216169695Skan  else
217169695Skan    {				/* have a finite number */
218169695Skan      uInt comb = top >> 2;	/* combination field */
219169695Skan      uInt exp;			/* exponent */
220169695Skan
221169695Skan      if (comb >= 0x18)
222169695Skan	{
223169695Skan	  msd = 8 + (comb & 0x01);
224169695Skan	  exp = (comb & 0x06) << 11;	/* MSBs */
225169695Skan	}
226169695Skan      else
227169695Skan	{
228169695Skan	  msd = comb & 0x07;
229169695Skan	  exp = (comb & 0x18) << 9;
230169695Skan	}
231169695Skan      dn->exponent = exp + decimal128ExpCon (d128) - DECIMAL128_Bias;	/* remove bias */
232169695Skan    }
233169695Skan
234169695Skan  /* get the coefficient, unless infinite */
235169695Skan  if (!(dn->bits & DECINF))
236169695Skan    {
237169695Skan      Int bunches = DECIMAL128_Pmax / 3;	/* coefficient full bunches to convert */
238169695Skan      Int odd = 0;		/* assume MSD is 0 (no odd bunch) */
239169695Skan      if (msd != 0)
240169695Skan	{			/* coefficient has leading non-0 digit */
241169695Skan	  /* make a copy of the decimal128, with an extra bunch which has */
242169695Skan	  /* the top digit ready for conversion */
243169695Skan	  wk = *d128;		/* take a copy */
244169695Skan	  wk.bytes[0] = 0;	/* clear all but coecon */
245169695Skan	  wk.bytes[1] = 0;	/* .. */
246169695Skan	  wk.bytes[2] &= 0x3f;	/* .. */
247169695Skan	  wk.bytes[1] |= (msd >> 2);	/* and prefix MSD */
248169695Skan	  wk.bytes[2] |= (msd << 6);	/* .. */
249169695Skan	  odd++;		/* indicate the extra */
250169695Skan	  d128 = &wk;		/* use the work copy */
251169695Skan	}
252169695Skan      decDenseUnpackCoeff (d128->bytes, sizeof (d128->bytes), dn, bunches,
253169695Skan			   odd);
254169695Skan    }
255169695Skan
256169695Skan  /* decNumberShow(dn); */
257169695Skan  return dn;
258169695Skan}
259169695Skan
260169695Skan/* ------------------------------------------------------------------ */
261169695Skan/* to-scientific-string -- conversion to numeric string               */
262169695Skan/* to-engineering-string -- conversion to numeric string              */
263169695Skan/*                                                                    */
264169695Skan/*   decimal128ToString(d128, string);                                */
265169695Skan/*   decimal128ToEngString(d128, string);                             */
266169695Skan/*                                                                    */
267169695Skan/*  d128 is the decimal128 format number to convert                   */
268169695Skan/*  string is the string where the result will be laid out            */
269169695Skan/*                                                                    */
270169695Skan/*  string must be at least 24 characters                             */
271169695Skan/*                                                                    */
272169695Skan/*  No error is possible, and no status can be set.                   */
273169695Skan/* ------------------------------------------------------------------ */
274169695Skanchar *
275169695Skandecimal128ToString (const decimal128 * d128, char *string)
276169695Skan{
277169695Skan  decNumber dn;			/* work */
278169695Skan  decimal128ToNumber (d128, &dn);
279169695Skan  decNumberToString (&dn, string);
280169695Skan  return string;
281169695Skan}
282169695Skan
283169695Skanchar *
284169695Skandecimal128ToEngString (const decimal128 * d128, char *string)
285169695Skan{
286169695Skan  decNumber dn;			/* work */
287169695Skan  decimal128ToNumber (d128, &dn);
288169695Skan  decNumberToEngString (&dn, string);
289169695Skan  return string;
290169695Skan}
291169695Skan
292169695Skan/* ------------------------------------------------------------------ */
293169695Skan/* to-number -- conversion from numeric string                        */
294169695Skan/*                                                                    */
295169695Skan/*   decimal128FromString(result, string, set);                       */
296169695Skan/*                                                                    */
297169695Skan/*  result  is the decimal128 format number which gets the result of  */
298169695Skan/*          the conversion                                            */
299169695Skan/*  *string is the character string which should contain a valid      */
300169695Skan/*          number (which may be a special value)                     */
301169695Skan/*  set     is the context                                            */
302169695Skan/*                                                                    */
303169695Skan/* The context is supplied to this routine is used for error handling */
304169695Skan/* (setting of status and traps) and for the rounding mode, only.     */
305169695Skan/* If an error occurs, the result will be a valid decimal128 NaN.     */
306169695Skan/* ------------------------------------------------------------------ */
307169695Skandecimal128 *
308169695Skandecimal128FromString (decimal128 * result, const char *string, decContext * set)
309169695Skan{
310169695Skan  decContext dc;		/* work */
311169695Skan  decNumber dn;			/* .. */
312169695Skan
313169695Skan  decContextDefault (&dc, DEC_INIT_DECIMAL128);	/* no traps, please */
314169695Skan  dc.round = set->round;	/* use supplied rounding */
315169695Skan
316169695Skan  decNumberFromString (&dn, string, &dc);	/* will round if needed */
317169695Skan  decimal128FromNumber (result, &dn, &dc);
318169695Skan  if (dc.status != 0)
319169695Skan    {				/* something happened */
320169695Skan      decContextSetStatus (set, dc.status);	/* .. pass it on */
321169695Skan    }
322169695Skan  return result;
323169695Skan}
324169695Skan
325169695Skan
326169695Skan#if DECTRACE || DECCHECK
327169695Skan/* ------------------------------------------------------------------ */
328169695Skan/* decimal128Show -- display a single in hexadecimal [debug aid]      */
329169695Skan/*   d128 -- the number to show                                       */
330169695Skan/* ------------------------------------------------------------------ */
331169695Skan/* Also shows sign/cob/expconfields extracted */
332169695Skanvoid
333169695Skandecimal128Show (const decimal128 * d128)
334169695Skan{
335169695Skan  char buf[DECIMAL128_Bytes * 2 + 1];
336169695Skan  Int i, j;
337169695Skan  j = 0;
338169695Skan  for (i = 0; i < DECIMAL128_Bytes; i++)
339169695Skan    {
340169695Skan      sprintf (&buf[j], "%02x", d128->bytes[i]);
341169695Skan      j = j + 2;
342169695Skan    }
343169695Skan  printf (" D128> %s [S:%d Cb:%02x E:%d]\n", buf,
344169695Skan	  decimal128Sign (d128), decimal128Comb (d128),
345169695Skan	  decimal128ExpCon (d128));
346169695Skan}
347169695Skan#endif
348