OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.2.2/] [libdecnumber/] [decimal32.c] - Blame information for rev 452

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.