xref: /openbsd-src/gnu/gcc/libdecnumber/decimal64.c (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert /* Decimal 64-bit format module for the decNumber C Library
2*404b540aSrobert    Copyright (C) 2005 Free Software Foundation, Inc.
3*404b540aSrobert    Contributed by IBM Corporation.  Author Mike Cowlishaw.
4*404b540aSrobert 
5*404b540aSrobert    This file is part of GCC.
6*404b540aSrobert 
7*404b540aSrobert    GCC is free software; you can redistribute it and/or modify it under
8*404b540aSrobert    the terms of the GNU General Public License as published by the Free
9*404b540aSrobert    Software Foundation; either version 2, or (at your option) any later
10*404b540aSrobert    version.
11*404b540aSrobert 
12*404b540aSrobert    In addition to the permissions in the GNU General Public License,
13*404b540aSrobert    the Free Software Foundation gives you unlimited permission to link
14*404b540aSrobert    the compiled version of this file into combinations with other
15*404b540aSrobert    programs, and to distribute those combinations without any
16*404b540aSrobert    restriction coming from the use of this file.  (The General Public
17*404b540aSrobert    License restrictions do apply in other respects; for example, they
18*404b540aSrobert    cover modification of the file, and distribution when not linked
19*404b540aSrobert    into a combine executable.)
20*404b540aSrobert 
21*404b540aSrobert    GCC is distributed in the hope that it will be useful, but WITHOUT ANY
22*404b540aSrobert    WARRANTY; without even the implied warranty of MERCHANTABILITY or
23*404b540aSrobert    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24*404b540aSrobert    for more details.
25*404b540aSrobert 
26*404b540aSrobert    You should have received a copy of the GNU General Public License
27*404b540aSrobert    along with GCC; see the file COPYING.  If not, write to the Free
28*404b540aSrobert    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29*404b540aSrobert    02110-1301, USA.  */
30*404b540aSrobert 
31*404b540aSrobert /* ------------------------------------------------------------------ */
32*404b540aSrobert /* This module comprises the routines for decimal64 format numbers.   */
33*404b540aSrobert /* Conversions are supplied to and from decNumber and String.         */
34*404b540aSrobert /*                                                                    */
35*404b540aSrobert /* No arithmetic routines are included; decNumber provides these.     */
36*404b540aSrobert /*                                                                    */
37*404b540aSrobert /* Error handling is the same as decNumber (qv.).                     */
38*404b540aSrobert /* ------------------------------------------------------------------ */
39*404b540aSrobert #include <string.h>		/* [for memset/memcpy] */
40*404b540aSrobert #include <stdio.h>		/* [for printf] */
41*404b540aSrobert 
42*404b540aSrobert #define  DECNUMDIGITS 16	/* we need decNumbers with space for 16 */
43*404b540aSrobert #include "config.h"
44*404b540aSrobert #include "decNumber.h"		/* base number library */
45*404b540aSrobert #include "decNumberLocal.h"	/* decNumber local types, etc. */
46*404b540aSrobert #include "decimal64.h"		/* our primary include */
47*404b540aSrobert #include "decUtility.h"		/* utility routines */
48*404b540aSrobert 
49*404b540aSrobert #if DECTRACE || DECCHECK
50*404b540aSrobert void decimal64Show (const decimal64 *);	/* for debug */
51*404b540aSrobert void decNumberShow (const decNumber *);	/* .. */
52*404b540aSrobert #endif
53*404b540aSrobert 
54*404b540aSrobert /* Useful macro */
55*404b540aSrobert /* Clear a structure (e.g., a decNumber) */
56*404b540aSrobert #define DEC_clear(d) memset(d, 0, sizeof(*d))
57*404b540aSrobert 
58*404b540aSrobert /* ------------------------------------------------------------------ */
59*404b540aSrobert /* decimal64FromNumber -- convert decNumber to decimal64              */
60*404b540aSrobert /*                                                                    */
61*404b540aSrobert /*   ds is the target decimal64                                       */
62*404b540aSrobert /*   dn is the source number (assumed valid)                          */
63*404b540aSrobert /*   set is the context, used only for reporting errors               */
64*404b540aSrobert /*                                                                    */
65*404b540aSrobert /* The set argument is used only for status reporting and for the     */
66*404b540aSrobert /* rounding mode (used if the coefficient is more than DECIMAL64_Pmax */
67*404b540aSrobert /* digits or an overflow is detected).  If the exponent is out of the */
68*404b540aSrobert /* valid range then Overflow or Underflow will be raised.             */
69*404b540aSrobert /* After Underflow a subnormal result is possible.                    */
70*404b540aSrobert /*                                                                    */
71*404b540aSrobert /* DEC_Clamped is set if the number has to be 'folded down' to fit,   */
72*404b540aSrobert /* by reducing its exponent and multiplying the coefficient by a      */
73*404b540aSrobert /* power of ten, or if the exponent on a zero had to be clamped.      */
74*404b540aSrobert /* ------------------------------------------------------------------ */
75*404b540aSrobert decimal64 *
decimal64FromNumber(decimal64 * d64,const decNumber * dn,decContext * set)76*404b540aSrobert decimal64FromNumber (decimal64 * d64, const decNumber * dn, decContext * set)
77*404b540aSrobert {
78*404b540aSrobert   uInt status = 0;		/* status accumulator */
79*404b540aSrobert   Int pad = 0;			/* coefficient pad digits */
80*404b540aSrobert   decNumber dw;			/* work */
81*404b540aSrobert   decContext dc;		/* .. */
82*404b540aSrobert   uByte isneg = dn->bits & DECNEG;	/* non-0 if original sign set */
83*404b540aSrobert   uInt comb, exp;		/* work */
84*404b540aSrobert 
85*404b540aSrobert   /* If the number is finite, and has too many digits, or the exponent */
86*404b540aSrobert   /* could be out of range then we reduce the number under the */
87*404b540aSrobert   /* appropriate constraints */
88*404b540aSrobert   if (!(dn->bits & DECSPECIAL))
89*404b540aSrobert     {				/* not a special value */
90*404b540aSrobert       Int ae = dn->exponent + dn->digits - 1;	/* adjusted exponent */
91*404b540aSrobert       if (dn->digits > DECIMAL64_Pmax	/* too many digits */
92*404b540aSrobert 	  || ae > DECIMAL64_Emax	/* likely overflow */
93*404b540aSrobert 	  || ae < DECIMAL64_Emin)
94*404b540aSrobert 	{			/* likely underflow */
95*404b540aSrobert 	  decContextDefault (&dc, DEC_INIT_DECIMAL64);	/* [no traps] */
96*404b540aSrobert 	  dc.round = set->round;	/* use supplied rounding */
97*404b540aSrobert 	  decNumberPlus (&dw, dn, &dc);	/* (round and check) */
98*404b540aSrobert 	  /* [this changes -0 to 0, but it will be restored below] */
99*404b540aSrobert 	  status |= dc.status;	/* save status */
100*404b540aSrobert 	  dn = &dw;		/* use the work number */
101*404b540aSrobert 	}
102*404b540aSrobert       /* [this could have pushed number to Infinity or zero, so this */
103*404b540aSrobert       /* rounding must be done before we generate the decimal64] */
104*404b540aSrobert     }
105*404b540aSrobert 
106*404b540aSrobert   DEC_clear (d64);		/* clean the target */
107*404b540aSrobert   if (dn->bits & DECSPECIAL)
108*404b540aSrobert     {				/* a special value */
109*404b540aSrobert       uByte top;		/* work */
110*404b540aSrobert       if (dn->bits & DECINF)
111*404b540aSrobert 	top = DECIMAL_Inf;
112*404b540aSrobert       else
113*404b540aSrobert 	{			/* sNaN or qNaN */
114*404b540aSrobert 	  if ((*dn->lsu != 0 || dn->digits > 1)	/* non-zero coefficient */
115*404b540aSrobert 	      && (dn->digits < DECIMAL64_Pmax))
116*404b540aSrobert 	    {			/* coefficient fits */
117*404b540aSrobert 	      decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), 0);
118*404b540aSrobert 	    }
119*404b540aSrobert 	  if (dn->bits & DECNAN)
120*404b540aSrobert 	    top = DECIMAL_NaN;
121*404b540aSrobert 	  else
122*404b540aSrobert 	    top = DECIMAL_sNaN;
123*404b540aSrobert 	}
124*404b540aSrobert       d64->bytes[0] = top;
125*404b540aSrobert     }
126*404b540aSrobert   else if (decNumberIsZero (dn))
127*404b540aSrobert     {				/* a zero */
128*404b540aSrobert       /* set and clamp exponent */
129*404b540aSrobert       if (dn->exponent < -DECIMAL64_Bias)
130*404b540aSrobert 	{
131*404b540aSrobert 	  exp = 0;
132*404b540aSrobert 	  status |= DEC_Clamped;
133*404b540aSrobert 	}
134*404b540aSrobert       else
135*404b540aSrobert 	{
136*404b540aSrobert 	  exp = dn->exponent + DECIMAL64_Bias;	/* bias exponent */
137*404b540aSrobert 	  if (exp > DECIMAL64_Ehigh)
138*404b540aSrobert 	    {			/* top clamp */
139*404b540aSrobert 	      exp = DECIMAL64_Ehigh;
140*404b540aSrobert 	      status |= DEC_Clamped;
141*404b540aSrobert 	    }
142*404b540aSrobert 	}
143*404b540aSrobert       comb = (exp >> 5) & 0x18;	/* combination field */
144*404b540aSrobert       d64->bytes[0] = (uByte) (comb << 2);
145*404b540aSrobert       exp &= 0xff;		/* remaining exponent bits */
146*404b540aSrobert       decimal64SetExpCon (d64, exp);
147*404b540aSrobert     }
148*404b540aSrobert   else
149*404b540aSrobert     {				/* non-zero finite number */
150*404b540aSrobert       uInt msd;			/* work */
151*404b540aSrobert 
152*404b540aSrobert       /* we have a dn that fits, but it may need to be padded */
153*404b540aSrobert       exp = (uInt) (dn->exponent + DECIMAL64_Bias);	/* bias exponent */
154*404b540aSrobert       if (exp > DECIMAL64_Ehigh)
155*404b540aSrobert 	{			/* fold-down case */
156*404b540aSrobert 	  pad = exp - DECIMAL64_Ehigh;
157*404b540aSrobert 	  exp = DECIMAL64_Ehigh;	/* [to maximum] */
158*404b540aSrobert 	  status |= DEC_Clamped;
159*404b540aSrobert 	}
160*404b540aSrobert 
161*404b540aSrobert       decDensePackCoeff (dn, d64->bytes, sizeof (d64->bytes), pad);
162*404b540aSrobert 
163*404b540aSrobert       /* save and clear the top digit */
164*404b540aSrobert       msd = ((unsigned) d64->bytes[1] >> 2) & 0x0f;
165*404b540aSrobert       d64->bytes[1] &= 0x03;
166*404b540aSrobert       /* create the combination field */
167*404b540aSrobert       if (msd >= 8)
168*404b540aSrobert 	comb = 0x18 | (msd & 0x01) | ((exp >> 7) & 0x06);
169*404b540aSrobert       else
170*404b540aSrobert 	comb = (msd & 0x07) | ((exp >> 5) & 0x18);
171*404b540aSrobert       d64->bytes[0] = (uByte) (comb << 2);
172*404b540aSrobert       exp &= 0xff;		/* remaining exponent bits */
173*404b540aSrobert       decimal64SetExpCon (d64, exp);
174*404b540aSrobert     }
175*404b540aSrobert 
176*404b540aSrobert   if (isneg)
177*404b540aSrobert     decimal64SetSign (d64, 1);
178*404b540aSrobert   if (status != 0)
179*404b540aSrobert     decContextSetStatus (set, status);	/* pass on status */
180*404b540aSrobert 
181*404b540aSrobert   /*decimal64Show(d64); */
182*404b540aSrobert   return d64;
183*404b540aSrobert }
184*404b540aSrobert 
185*404b540aSrobert /* ------------------------------------------------------------------ */
186*404b540aSrobert /* decimal64ToNumber -- convert decimal64 to decNumber                */
187*404b540aSrobert /*   d64 is the source decimal64                                      */
188*404b540aSrobert /*   dn is the target number, with appropriate space                  */
189*404b540aSrobert /* No error is possible.                                              */
190*404b540aSrobert /* ------------------------------------------------------------------ */
191*404b540aSrobert decNumber *
decimal64ToNumber(const decimal64 * d64,decNumber * dn)192*404b540aSrobert decimal64ToNumber (const decimal64 * d64, decNumber * dn)
193*404b540aSrobert {
194*404b540aSrobert   uInt msd;			/* coefficient MSD */
195*404b540aSrobert   decimal64 wk;			/* working copy, if needed */
196*404b540aSrobert   uInt top = d64->bytes[0] & 0x7f;	/* top byte, less sign bit */
197*404b540aSrobert   decNumberZero (dn);		/* clean target */
198*404b540aSrobert   /* set the sign if negative */
199*404b540aSrobert   if (decimal64Sign (d64))
200*404b540aSrobert     dn->bits = DECNEG;
201*404b540aSrobert 
202*404b540aSrobert   if (top >= 0x78)
203*404b540aSrobert     {				/* is a special */
204*404b540aSrobert       if ((top & 0x7c) == (DECIMAL_Inf & 0x7c))
205*404b540aSrobert 	dn->bits |= DECINF;
206*404b540aSrobert       else if ((top & 0x7e) == (DECIMAL_NaN & 0x7e))
207*404b540aSrobert 	dn->bits |= DECNAN;
208*404b540aSrobert       else
209*404b540aSrobert 	dn->bits |= DECSNAN;
210*404b540aSrobert       msd = 0;			/* no top digit */
211*404b540aSrobert     }
212*404b540aSrobert   else
213*404b540aSrobert     {				/* have a finite number */
214*404b540aSrobert       uInt comb = top >> 2;	/* combination field */
215*404b540aSrobert       uInt exp;			/* exponent */
216*404b540aSrobert 
217*404b540aSrobert       if (comb >= 0x18)
218*404b540aSrobert 	{
219*404b540aSrobert 	  msd = 8 + (comb & 0x01);
220*404b540aSrobert 	  exp = (comb & 0x06) << 7;	/* MSBs */
221*404b540aSrobert 	}
222*404b540aSrobert       else
223*404b540aSrobert 	{
224*404b540aSrobert 	  msd = comb & 0x07;
225*404b540aSrobert 	  exp = (comb & 0x18) << 5;
226*404b540aSrobert 	}
227*404b540aSrobert       dn->exponent = exp + decimal64ExpCon (d64) - DECIMAL64_Bias;	/* remove bias */
228*404b540aSrobert     }
229*404b540aSrobert 
230*404b540aSrobert   /* get the coefficient, unless infinite */
231*404b540aSrobert   if (!(dn->bits & DECINF))
232*404b540aSrobert     {
233*404b540aSrobert       Int bunches = DECIMAL64_Pmax / 3;	/* coefficient full bunches to convert */
234*404b540aSrobert       Int odd = 0;		/* assume MSD is 0 (no odd bunch) */
235*404b540aSrobert       if (msd != 0)
236*404b540aSrobert 	{			/* coefficient has leading non-0 digit */
237*404b540aSrobert 	  /* make a copy of the decimal64, with an extra bunch which has */
238*404b540aSrobert 	  /* the top digit ready for conversion */
239*404b540aSrobert 	  wk = *d64;		/* take a copy */
240*404b540aSrobert 	  wk.bytes[0] = 0;	/* clear all but coecon */
241*404b540aSrobert 	  wk.bytes[1] &= 0x03;	/* .. */
242*404b540aSrobert 	  wk.bytes[1] |= (msd << 2);	/* and prefix MSD */
243*404b540aSrobert 	  odd++;		/* indicate the extra */
244*404b540aSrobert 	  d64 = &wk;		/* use the work copy */
245*404b540aSrobert 	}
246*404b540aSrobert       decDenseUnpackCoeff (d64->bytes, sizeof (d64->bytes), dn, bunches, odd);
247*404b540aSrobert     }
248*404b540aSrobert   return dn;
249*404b540aSrobert }
250*404b540aSrobert 
251*404b540aSrobert /* ------------------------------------------------------------------ */
252*404b540aSrobert /* to-scientific-string -- conversion to numeric string               */
253*404b540aSrobert /* to-engineering-string -- conversion to numeric string              */
254*404b540aSrobert /*                                                                    */
255*404b540aSrobert /*   decimal64ToString(d64, string);                                  */
256*404b540aSrobert /*   decimal64ToEngString(d64, string);                               */
257*404b540aSrobert /*                                                                    */
258*404b540aSrobert /*  d64 is the decimal64 format number to convert                     */
259*404b540aSrobert /*  string is the string where the result will be laid out            */
260*404b540aSrobert /*                                                                    */
261*404b540aSrobert /*  string must be at least 24 characters                             */
262*404b540aSrobert /*                                                                    */
263*404b540aSrobert /*  No error is possible, and no status can be set.                   */
264*404b540aSrobert /* ------------------------------------------------------------------ */
265*404b540aSrobert char *
decimal64ToString(const decimal64 * d64,char * string)266*404b540aSrobert decimal64ToString (const decimal64 * d64, char *string)
267*404b540aSrobert {
268*404b540aSrobert   decNumber dn;			/* work */
269*404b540aSrobert   decimal64ToNumber (d64, &dn);
270*404b540aSrobert   decNumberToString (&dn, string);
271*404b540aSrobert   return string;
272*404b540aSrobert }
273*404b540aSrobert 
274*404b540aSrobert char *
decimal64ToEngString(const decimal64 * d64,char * string)275*404b540aSrobert decimal64ToEngString (const decimal64 * d64, char *string)
276*404b540aSrobert {
277*404b540aSrobert   decNumber dn;			/* work */
278*404b540aSrobert   decimal64ToNumber (d64, &dn);
279*404b540aSrobert   decNumberToEngString (&dn, string);
280*404b540aSrobert   return string;
281*404b540aSrobert }
282*404b540aSrobert 
283*404b540aSrobert /* ------------------------------------------------------------------ */
284*404b540aSrobert /* to-number -- conversion from numeric string                        */
285*404b540aSrobert /*                                                                    */
286*404b540aSrobert /*   decimal64FromString(result, string, set);                        */
287*404b540aSrobert /*                                                                    */
288*404b540aSrobert /*  result  is the decimal64 format number which gets the result of   */
289*404b540aSrobert /*          the conversion                                            */
290*404b540aSrobert /*  *string is the character string which should contain a valid      */
291*404b540aSrobert /*          number (which may be a special value)                     */
292*404b540aSrobert /*  set     is the context                                            */
293*404b540aSrobert /*                                                                    */
294*404b540aSrobert /* The context is supplied to this routine is used for error handling */
295*404b540aSrobert /* (setting of status and traps) and for the rounding mode, only.     */
296*404b540aSrobert /* If an error occurs, the result will be a valid decimal64 NaN.      */
297*404b540aSrobert /* ------------------------------------------------------------------ */
298*404b540aSrobert decimal64 *
decimal64FromString(decimal64 * result,const char * string,decContext * set)299*404b540aSrobert decimal64FromString (decimal64 * result, const char *string, decContext * set)
300*404b540aSrobert {
301*404b540aSrobert   decContext dc;		/* work */
302*404b540aSrobert   decNumber dn;			/* .. */
303*404b540aSrobert 
304*404b540aSrobert   decContextDefault (&dc, DEC_INIT_DECIMAL64);	/* no traps, please */
305*404b540aSrobert   dc.round = set->round;	/* use supplied rounding */
306*404b540aSrobert 
307*404b540aSrobert   decNumberFromString (&dn, string, &dc);	/* will round if needed */
308*404b540aSrobert 
309*404b540aSrobert   decimal64FromNumber (result, &dn, &dc);
310*404b540aSrobert   if (dc.status != 0)
311*404b540aSrobert     {				/* something happened */
312*404b540aSrobert       decContextSetStatus (set, dc.status);	/* .. pass it on */
313*404b540aSrobert     }
314*404b540aSrobert   return result;
315*404b540aSrobert }
316*404b540aSrobert 
317*404b540aSrobert #if DECTRACE || DECCHECK
318*404b540aSrobert /* ------------------------------------------------------------------ */
319*404b540aSrobert /* decimal64Show -- display a single in hexadecimal [debug aid]       */
320*404b540aSrobert /*   d64 -- the number to show                                        */
321*404b540aSrobert /* ------------------------------------------------------------------ */
322*404b540aSrobert /* Also shows sign/cob/expconfields extracted */
323*404b540aSrobert void
decimal64Show(const decimal64 * d64)324*404b540aSrobert decimal64Show (const decimal64 * d64)
325*404b540aSrobert {
326*404b540aSrobert   char buf[DECIMAL64_Bytes * 2 + 1];
327*404b540aSrobert   Int i, j;
328*404b540aSrobert   j = 0;
329*404b540aSrobert   for (i = 0; i < DECIMAL64_Bytes; i++)
330*404b540aSrobert     {
331*404b540aSrobert       sprintf (&buf[j], "%02x", d64->bytes[i]);
332*404b540aSrobert       j = j + 2;
333*404b540aSrobert     }
334*404b540aSrobert   printf (" D64> %s [S:%d Cb:%02x E:%d]\n", buf,
335*404b540aSrobert 	  decimal64Sign (d64), decimal64Comb (d64), decimal64ExpCon (d64));
336*404b540aSrobert }
337*404b540aSrobert #endif
338