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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [libiberty/] [floatformat.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1181 sfurman
/* IEEE floating point support routines, for GDB, the GNU Debugger.
2
   Copyright (C) 1991, 1994, 1999, 2000 Free Software Foundation, Inc.
3
 
4
This file is part of GDB.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
 
20
#include "floatformat.h"
21
#include <math.h>               /* ldexp */
22
#ifdef __STDC__
23
#include <stddef.h>
24
extern void *memcpy (void *s1, const void *s2, size_t n);
25
extern void *memset (void *s, int c, size_t n);
26
#else
27
extern char *memcpy ();
28
extern char *memset ();
29
#endif
30
 
31
/* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
32
   going to bother with trying to muck around with whether it is defined in
33
   a system header, what we do if not, etc.  */
34
#define FLOATFORMAT_CHAR_BIT 8
35
 
36
/* floatformats for IEEE single and double, big and little endian.  */
37
const struct floatformat floatformat_ieee_single_big =
38
{
39
  floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
40
  floatformat_intbit_no,
41
  "floatformat_ieee_single_big"
42
};
43
const struct floatformat floatformat_ieee_single_little =
44
{
45
  floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
46
  floatformat_intbit_no,
47
  "floatformat_ieee_single_little"
48
};
49
const struct floatformat floatformat_ieee_double_big =
50
{
51
  floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
52
  floatformat_intbit_no,
53
  "floatformat_ieee_double_big"
54
};
55
const struct floatformat floatformat_ieee_double_little =
56
{
57
  floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
58
  floatformat_intbit_no,
59
  "floatformat_ieee_double_little"
60
};
61
 
62
/* floatformat for IEEE double, little endian byte order, with big endian word
63
   ordering, as on the ARM.  */
64
 
65
const struct floatformat floatformat_ieee_double_littlebyte_bigword =
66
{
67
  floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
68
  floatformat_intbit_no,
69
  "floatformat_ieee_double_littlebyte_bigword"
70
};
71
 
72
const struct floatformat floatformat_i387_ext =
73
{
74
  floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
75
  floatformat_intbit_yes,
76
  "floatformat_i387_ext"
77
};
78
const struct floatformat floatformat_m68881_ext =
79
{
80
  /* Note that the bits from 16 to 31 are unused.  */
81
  floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
82
  floatformat_intbit_yes,
83
  "floatformat_m68881_ext"
84
};
85
const struct floatformat floatformat_i960_ext =
86
{
87
  /* Note that the bits from 0 to 15 are unused.  */
88
  floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
89
  floatformat_intbit_yes,
90
  "floatformat_i960_ext"
91
};
92
const struct floatformat floatformat_m88110_ext =
93
{
94
  floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
95
  floatformat_intbit_yes,
96
  "floatformat_m88110_ext"
97
};
98
const struct floatformat floatformat_m88110_harris_ext =
99
{
100
  /* Harris uses raw format 128 bytes long, but the number is just an ieee
101
     double, and the last 64 bits are wasted. */
102
  floatformat_big,128, 0, 1, 11,  0x3ff,  0x7ff, 12, 52,
103
  floatformat_intbit_no,
104
  "floatformat_m88110_ext_harris"
105
};
106
const struct floatformat floatformat_arm_ext_big =
107
{
108
  /* Bits 1 to 16 are unused.  */
109
  floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
110
  floatformat_intbit_yes,
111
  "floatformat_arm_ext_big"
112
};
113
const struct floatformat floatformat_arm_ext_littlebyte_bigword =
114
{
115
  /* Bits 1 to 16 are unused.  */
116
  floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
117
  floatformat_intbit_yes,
118
  "floatformat_arm_ext_littlebyte_bigword"
119
};
120
const struct floatformat floatformat_ia64_spill_big =
121
{
122
  floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
123
  floatformat_intbit_yes,
124
  "floatformat_ia64_spill_big"
125
};
126
const struct floatformat floatformat_ia64_spill_little =
127
{
128
  floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
129
  floatformat_intbit_yes,
130
  "floatformat_ia64_spill_little"
131
};
132
const struct floatformat floatformat_ia64_quad_big =
133
{
134
  floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
135
  floatformat_intbit_no,
136
  "floatformat_ia64_quad_big"
137
};
138
const struct floatformat floatformat_ia64_quad_little =
139
{
140
  floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
141
  floatformat_intbit_no,
142
  "floatformat_ia64_quad_little"
143
};
144
 
145
static unsigned long get_field PARAMS ((unsigned char *,
146
                                        enum floatformat_byteorders,
147
                                        unsigned int,
148
                                        unsigned int,
149
                                        unsigned int));
150
 
151
/* Extract a field which starts at START and is LEN bytes long.  DATA and
152
   TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
153
static unsigned long
154
get_field (data, order, total_len, start, len)
155
     unsigned char *data;
156
     enum floatformat_byteorders order;
157
     unsigned int total_len;
158
     unsigned int start;
159
     unsigned int len;
160
{
161
  unsigned long result;
162
  unsigned int cur_byte;
163
  int cur_bitshift;
164
 
165
  /* Start at the least significant part of the field.  */
166
  cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
167
  if (order == floatformat_little)
168
    cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
169
  cur_bitshift =
170
    ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
171
  result = *(data + cur_byte) >> (-cur_bitshift);
172
  cur_bitshift += FLOATFORMAT_CHAR_BIT;
173
  if (order == floatformat_little)
174
    ++cur_byte;
175
  else
176
    --cur_byte;
177
 
178
  /* Move towards the most significant part of the field.  */
179
  while ((unsigned int) cur_bitshift < len)
180
    {
181
      if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
182
        /* This is the last byte; zero out the bits which are not part of
183
           this field.  */
184
        result |=
185
          (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
186
            << cur_bitshift;
187
      else
188
        result |= *(data + cur_byte) << cur_bitshift;
189
      cur_bitshift += FLOATFORMAT_CHAR_BIT;
190
      if (order == floatformat_little)
191
        ++cur_byte;
192
      else
193
        --cur_byte;
194
    }
195
  return result;
196
}
197
 
198
#ifndef min
199
#define min(a, b) ((a) < (b) ? (a) : (b))
200
#endif
201
 
202
/* Convert from FMT to a double.
203
   FROM is the address of the extended float.
204
   Store the double in *TO.  */
205
 
206
void
207
floatformat_to_double (fmt, from, to)
208
     const struct floatformat *fmt;
209
     char *from;
210
     double *to;
211
{
212
  unsigned char *ufrom = (unsigned char *)from;
213
  double dto;
214
  long exponent;
215
  unsigned long mant;
216
  unsigned int mant_bits, mant_off;
217
  int mant_bits_left;
218
  int special_exponent;         /* It's a NaN, denorm or zero */
219
 
220
  exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
221
                        fmt->exp_start, fmt->exp_len);
222
  /* Note that if exponent indicates a NaN, we can't really do anything useful
223
     (not knowing if the host has NaN's, or how to build one).  So it will
224
     end up as an infinity or something close; that is OK.  */
225
 
226
  mant_bits_left = fmt->man_len;
227
  mant_off = fmt->man_start;
228
  dto = 0.0;
229
 
230
  special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
231
 
232
  /* Don't bias zero's, denorms or NaNs.  */
233
  if (!special_exponent)
234
    exponent -= fmt->exp_bias;
235
 
236
  /* Build the result algebraically.  Might go infinite, underflow, etc;
237
     who cares. */
238
 
239
  /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
240
     increment the exponent by one to account for the integer bit.  */
241
 
242
  if (!special_exponent)
243
    {
244
      if (fmt->intbit == floatformat_intbit_no)
245
        dto = ldexp (1.0, exponent);
246
      else
247
        exponent++;
248
    }
249
 
250
  while (mant_bits_left > 0)
251
    {
252
      mant_bits = min (mant_bits_left, 32);
253
 
254
      mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
255
                         mant_off, mant_bits);
256
 
257
      dto += ldexp ((double)mant, exponent - mant_bits);
258
      exponent -= mant_bits;
259
      mant_off += mant_bits;
260
      mant_bits_left -= mant_bits;
261
    }
262
 
263
  /* Negate it if negative.  */
264
  if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
265
    dto = -dto;
266
  *to = dto;
267
}
268
 
269
static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders,
270
                               unsigned int,
271
                               unsigned int,
272
                               unsigned int,
273
                               unsigned long));
274
 
275
/* Set a field which starts at START and is LEN bytes long.  DATA and
276
   TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
277
static void
278
put_field (data, order, total_len, start, len, stuff_to_put)
279
     unsigned char *data;
280
     enum floatformat_byteorders order;
281
     unsigned int total_len;
282
     unsigned int start;
283
     unsigned int len;
284
     unsigned long stuff_to_put;
285
{
286
  unsigned int cur_byte;
287
  int cur_bitshift;
288
 
289
  /* Start at the least significant part of the field.  */
290
  cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
291
  if (order == floatformat_little)
292
    cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
293
  cur_bitshift =
294
    ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
295
  *(data + cur_byte) &=
296
    ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
297
  *(data + cur_byte) |=
298
    (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
299
  cur_bitshift += FLOATFORMAT_CHAR_BIT;
300
  if (order == floatformat_little)
301
    ++cur_byte;
302
  else
303
    --cur_byte;
304
 
305
  /* Move towards the most significant part of the field.  */
306
  while ((unsigned int) cur_bitshift < len)
307
    {
308
      if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
309
        {
310
          /* This is the last byte.  */
311
          *(data + cur_byte) &=
312
            ~((1 << (len - cur_bitshift)) - 1);
313
          *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
314
        }
315
      else
316
        *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
317
                              & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
318
      cur_bitshift += FLOATFORMAT_CHAR_BIT;
319
      if (order == floatformat_little)
320
        ++cur_byte;
321
      else
322
        --cur_byte;
323
    }
324
}
325
 
326
/* The converse: convert the double *FROM to an extended float
327
   and store where TO points.  Neither FROM nor TO have any alignment
328
   restrictions.  */
329
 
330
void
331
floatformat_from_double (fmt, from, to)
332
     const struct floatformat *fmt;
333
     double *from;
334
     char *to;
335
{
336
  double dfrom;
337
  int exponent;
338
  double mant;
339
  unsigned int mant_bits, mant_off;
340
  int mant_bits_left;
341
  unsigned char *uto = (unsigned char *)to;
342
 
343
  memcpy (&dfrom, from, sizeof (dfrom));
344
  memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
345
  if (dfrom == 0)
346
    return;                     /* Result is zero */
347
  if (dfrom != dfrom)
348
    {
349
      /* From is NaN */
350
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
351
                 fmt->exp_len, fmt->exp_nan);
352
      /* Be sure it's not infinity, but NaN value is irrel */
353
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
354
                 32, 1);
355
      return;
356
    }
357
 
358
  /* If negative, set the sign bit.  */
359
  if (dfrom < 0)
360
    {
361
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
362
      dfrom = -dfrom;
363
    }
364
 
365
  /* How to tell an infinity from an ordinary number?  FIXME-someday */
366
 
367
  mant = frexp (dfrom, &exponent);
368
  put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
369
             exponent + fmt->exp_bias - 1);
370
 
371
  mant_bits_left = fmt->man_len;
372
  mant_off = fmt->man_start;
373
  while (mant_bits_left > 0)
374
    {
375
      unsigned long mant_long;
376
      mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
377
 
378
      mant *= 4294967296.0;
379
      mant_long = (unsigned long)mant;
380
      mant -= mant_long;
381
 
382
      /* If the integer bit is implicit, then we need to discard it.
383
         If we are discarding a zero, we should be (but are not) creating
384
         a denormalized number which means adjusting the exponent
385
         (I think).  */
386
      if ((unsigned int) mant_bits_left == fmt->man_len
387
          && fmt->intbit == floatformat_intbit_no)
388
        {
389
          mant_long &= 0x7fffffff;
390
          mant_bits -= 1;
391
        }
392
      else if (mant_bits < 32)
393
        {
394
          /* The bits we want are in the most significant MANT_BITS bits of
395
             mant_long.  Move them to the least significant.  */
396
          mant_long >>= 32 - mant_bits;
397
        }
398
 
399
      put_field (uto, fmt->byteorder, fmt->totalsize,
400
                 mant_off, mant_bits, mant_long);
401
      mant_off += mant_bits;
402
      mant_bits_left -= mant_bits;
403
    }
404
}
405
 
406
 
407
#ifdef IEEE_DEBUG
408
 
409
/* This is to be run on a host which uses IEEE floating point.  */
410
 
411
void
412
ieee_test (n)
413
     double n;
414
{
415
  double result;
416
  char exten[16];
417
 
418
  floatformat_to_double (&floatformat_ieee_double_big, &n, &result);
419
  if (n != result)
420
    printf ("Differ(to): %.20g -> %.20g\n", n, result);
421
  floatformat_from_double (&floatformat_ieee_double_big, &n, &result);
422
  if (n != result)
423
    printf ("Differ(from): %.20g -> %.20g\n", n, result);
424
 
425
  floatformat_from_double (&floatformat_m68881_ext, &n, exten);
426
  floatformat_to_double (&floatformat_m68881_ext, exten, &result);
427
  if (n != result)
428
    printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
429
 
430
#if IEEE_DEBUG > 1
431
  /* This is to be run on a host which uses 68881 format.  */
432
  {
433
    long double ex = *(long double *)exten;
434
    if (ex != n)
435
      printf ("Differ(from vs. extended): %.20g\n", n);
436
  }
437
#endif
438
}
439
 
440
int
441
main ()
442
{
443
  ieee_test (0.5);
444
  ieee_test (256.0);
445
  ieee_test (0.12345);
446
  ieee_test (234235.78907234);
447
  ieee_test (-512.0);
448
  ieee_test (-0.004321);
449
  return 0;
450
}
451
#endif

powered by: WebSVN 2.1.0

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