OSDN Git Service

2006-01-19 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libiberty / floatformat.c
1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2    Copyright 1991, 1994, 1999, 2000, 2003, 2005
3    Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 /* This is needed to pick up the NAN macro on some systems.  */
22 #define _GNU_SOURCE
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <math.h>
29
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
33
34 #include "ansidecl.h"
35 #include "libiberty.h"
36 #include "floatformat.h"
37
38 #ifndef INFINITY
39 #ifdef HUGE_VAL
40 #define INFINITY HUGE_VAL
41 #else
42 #define INFINITY (1.0 / 0.0)
43 #endif
44 #endif
45
46 #ifndef NAN
47 #define NAN (0.0 / 0.0)
48 #endif
49
50 static unsigned long get_field (const unsigned char *,
51                                 enum floatformat_byteorders,
52                                 unsigned int,
53                                 unsigned int,
54                                 unsigned int);
55 static int floatformat_always_valid (const struct floatformat *fmt,
56                                      const void *from);
57
58 static int
59 floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
60                           const void *from ATTRIBUTE_UNUSED)
61 {
62   return 1;
63 }
64
65 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
66    going to bother with trying to muck around with whether it is defined in
67    a system header, what we do if not, etc.  */
68 #define FLOATFORMAT_CHAR_BIT 8
69
70 /* floatformats for IEEE single and double, big and little endian.  */
71 const struct floatformat floatformat_ieee_single_big =
72 {
73   floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
74   floatformat_intbit_no,
75   "floatformat_ieee_single_big",
76   floatformat_always_valid
77 };
78 const struct floatformat floatformat_ieee_single_little =
79 {
80   floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
81   floatformat_intbit_no,
82   "floatformat_ieee_single_little",
83   floatformat_always_valid
84 };
85 const struct floatformat floatformat_ieee_double_big =
86 {
87   floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
88   floatformat_intbit_no,
89   "floatformat_ieee_double_big",
90   floatformat_always_valid
91 };
92 const struct floatformat floatformat_ieee_double_little =
93 {
94   floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
95   floatformat_intbit_no,
96   "floatformat_ieee_double_little",
97   floatformat_always_valid
98 };
99
100 /* floatformat for IEEE double, little endian byte order, with big endian word
101    ordering, as on the ARM.  */
102
103 const struct floatformat floatformat_ieee_double_littlebyte_bigword =
104 {
105   floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
106   floatformat_intbit_no,
107   "floatformat_ieee_double_littlebyte_bigword",
108   floatformat_always_valid
109 };
110
111 /* floatformat for VAX.  Not quite IEEE, but close enough.  */
112
113 const struct floatformat floatformat_vax_f =
114 {
115   floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
116   floatformat_intbit_no,
117   "floatformat_vax_f",
118   floatformat_always_valid
119 };
120 const struct floatformat floatformat_vax_d =
121 {
122   floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
123   floatformat_intbit_no,
124   "floatformat_vax_d",
125   floatformat_always_valid
126 };
127 const struct floatformat floatformat_vax_g =
128 {
129   floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
130   floatformat_intbit_no,
131   "floatformat_vax_g",
132   floatformat_always_valid
133 };
134
135 static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
136                                           const void *from);
137
138 static int
139 floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
140 {
141   /* In the i387 double-extended format, if the exponent is all ones,
142      then the integer bit must be set.  If the exponent is neither 0
143      nor ~0, the intbit must also be set.  Only if the exponent is
144      zero can it be zero, and then it must be zero.  */
145   unsigned long exponent, int_bit;
146   const unsigned char *ufrom = (const unsigned char *) from;
147
148   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
149                         fmt->exp_start, fmt->exp_len);
150   int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
151                        fmt->man_start, 1);
152
153   if ((exponent == 0) != (int_bit == 0))
154     return 0;
155   else
156     return 1;
157 }
158
159 const struct floatformat floatformat_i387_ext =
160 {
161   floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
162   floatformat_intbit_yes,
163   "floatformat_i387_ext",
164   floatformat_i387_ext_is_valid
165 };
166 const struct floatformat floatformat_m68881_ext =
167 {
168   /* Note that the bits from 16 to 31 are unused.  */
169   floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
170   floatformat_intbit_yes,
171   "floatformat_m68881_ext",
172   floatformat_always_valid
173 };
174 const struct floatformat floatformat_i960_ext =
175 {
176   /* Note that the bits from 0 to 15 are unused.  */
177   floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
178   floatformat_intbit_yes,
179   "floatformat_i960_ext",
180   floatformat_always_valid
181 };
182 const struct floatformat floatformat_m88110_ext =
183 {
184   floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
185   floatformat_intbit_yes,
186   "floatformat_m88110_ext",
187   floatformat_always_valid
188 };
189 const struct floatformat floatformat_m88110_harris_ext =
190 {
191   /* Harris uses raw format 128 bytes long, but the number is just an ieee
192      double, and the last 64 bits are wasted. */
193   floatformat_big,128, 0, 1, 11,  0x3ff,  0x7ff, 12, 52,
194   floatformat_intbit_no,
195   "floatformat_m88110_ext_harris",
196   floatformat_always_valid
197 };
198 const struct floatformat floatformat_arm_ext_big =
199 {
200   /* Bits 1 to 16 are unused.  */
201   floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
202   floatformat_intbit_yes,
203   "floatformat_arm_ext_big",
204   floatformat_always_valid
205 };
206 const struct floatformat floatformat_arm_ext_littlebyte_bigword =
207 {
208   /* Bits 1 to 16 are unused.  */
209   floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
210   floatformat_intbit_yes,
211   "floatformat_arm_ext_littlebyte_bigword",
212   floatformat_always_valid
213 };
214 const struct floatformat floatformat_ia64_spill_big =
215 {
216   floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
217   floatformat_intbit_yes,
218   "floatformat_ia64_spill_big",
219   floatformat_always_valid
220 };
221 const struct floatformat floatformat_ia64_spill_little =
222 {
223   floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
224   floatformat_intbit_yes,
225   "floatformat_ia64_spill_little",
226   floatformat_always_valid
227 };
228 const struct floatformat floatformat_ia64_quad_big =
229 {
230   floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
231   floatformat_intbit_no,
232   "floatformat_ia64_quad_big",
233   floatformat_always_valid
234 };
235 const struct floatformat floatformat_ia64_quad_little =
236 {
237   floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
238   floatformat_intbit_no,
239   "floatformat_ia64_quad_little",
240   floatformat_always_valid
241 };
242 \f
243 /* Extract a field which starts at START and is LEN bits long.  DATA and
244    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
245 static unsigned long
246 get_field (const unsigned char *data, enum floatformat_byteorders order,
247            unsigned int total_len, unsigned int start, unsigned int len)
248 {
249   unsigned long result;
250   unsigned int cur_byte;
251   int cur_bitshift;
252
253   /* Start at the least significant part of the field.  */
254   cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
255   if (order == floatformat_little)
256     cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
257   cur_bitshift =
258     ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
259   result = *(data + cur_byte) >> (-cur_bitshift);
260   cur_bitshift += FLOATFORMAT_CHAR_BIT;
261   if (order == floatformat_little)
262     ++cur_byte;
263   else
264     --cur_byte;
265
266   /* Move towards the most significant part of the field.  */
267   while ((unsigned int) cur_bitshift < len)
268     {
269       if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
270         /* This is the last byte; zero out the bits which are not part of
271            this field.  */
272         result |=
273           (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
274             << cur_bitshift;
275       else
276         result |= *(data + cur_byte) << cur_bitshift;
277       cur_bitshift += FLOATFORMAT_CHAR_BIT;
278       if (order == floatformat_little)
279         ++cur_byte;
280       else
281         --cur_byte;
282     }
283   return result;
284 }
285   
286 #ifndef min
287 #define min(a, b) ((a) < (b) ? (a) : (b))
288 #endif
289
290 /* Convert from FMT to a double.
291    FROM is the address of the extended float.
292    Store the double in *TO.  */
293
294 void
295 floatformat_to_double (const struct floatformat *fmt,
296                        const void *from, double *to)
297 {
298   const unsigned char *ufrom = (const unsigned char *) from;
299   double dto;
300   long exponent;
301   unsigned long mant;
302   unsigned int mant_bits, mant_off;
303   int mant_bits_left;
304   int special_exponent;         /* It's a NaN, denorm or zero */
305
306   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
307                         fmt->exp_start, fmt->exp_len);
308
309   /* If the exponent indicates a NaN, we don't have information to
310      decide what to do.  So we handle it like IEEE, except that we
311      don't try to preserve the type of NaN.  FIXME.  */
312   if ((unsigned long) exponent == fmt->exp_nan)
313     {
314       int nan;
315
316       mant_off = fmt->man_start;
317       mant_bits_left = fmt->man_len;
318       nan = 0;
319       while (mant_bits_left > 0)
320         {
321           mant_bits = min (mant_bits_left, 32);
322
323           if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
324                          mant_off, mant_bits) != 0)
325             {
326               /* This is a NaN.  */
327               nan = 1;
328               break;
329             }
330
331           mant_off += mant_bits;
332           mant_bits_left -= mant_bits;
333         }
334
335       /* On certain systems (such as GNU/Linux), the use of the
336          INFINITY macro below may generate a warning that can not be
337          silenced due to a bug in GCC (PR preprocessor/11931).  The
338          preprocessor fails to recognise the __extension__ keyword in
339          conjunction with the GNU/C99 extension for hexadecimal
340          floating point constants and will issue a warning when
341          compiling with -pedantic.  */
342       if (nan)
343         dto = NAN;
344       else
345         dto = INFINITY;
346
347       if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
348         dto = -dto;
349
350       *to = dto;
351
352       return;
353     }
354
355   mant_bits_left = fmt->man_len;
356   mant_off = fmt->man_start;
357   dto = 0.0;
358
359   special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
360
361   /* Don't bias zero's, denorms or NaNs.  */
362   if (!special_exponent)
363     exponent -= fmt->exp_bias;
364
365   /* Build the result algebraically.  Might go infinite, underflow, etc;
366      who cares. */
367
368   /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
369      increment the exponent by one to account for the integer bit.  */
370
371   if (!special_exponent)
372     {
373       if (fmt->intbit == floatformat_intbit_no)
374         dto = ldexp (1.0, exponent);
375       else
376         exponent++;
377     }
378
379   while (mant_bits_left > 0)
380     {
381       mant_bits = min (mant_bits_left, 32);
382
383       mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
384                          mant_off, mant_bits);
385
386       /* Handle denormalized numbers.  FIXME: What should we do for
387          non-IEEE formats?  */
388       if (exponent == 0 && mant != 0)
389         dto += ldexp ((double)mant,
390                       (- fmt->exp_bias
391                        - mant_bits
392                        - (mant_off - fmt->man_start)
393                        + 1));
394       else
395         dto += ldexp ((double)mant, exponent - mant_bits);
396       if (exponent != 0)
397         exponent -= mant_bits;
398       mant_off += mant_bits;
399       mant_bits_left -= mant_bits;
400     }
401
402   /* Negate it if negative.  */
403   if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
404     dto = -dto;
405   *to = dto;
406 }
407 \f
408 static void put_field (unsigned char *, enum floatformat_byteorders,
409                        unsigned int,
410                        unsigned int,
411                        unsigned int,
412                        unsigned long);
413
414 /* Set a field which starts at START and is LEN bits long.  DATA and
415    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
416 static void
417 put_field (unsigned char *data, enum floatformat_byteorders order,
418            unsigned int total_len, unsigned int start, unsigned int len,
419            unsigned long stuff_to_put)
420 {
421   unsigned int cur_byte;
422   int cur_bitshift;
423
424   /* Start at the least significant part of the field.  */
425   cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
426   if (order == floatformat_little)
427     cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
428   cur_bitshift =
429     ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
430   *(data + cur_byte) &=
431     ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
432   *(data + cur_byte) |=
433     (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
434   cur_bitshift += FLOATFORMAT_CHAR_BIT;
435   if (order == floatformat_little)
436     ++cur_byte;
437   else
438     --cur_byte;
439
440   /* Move towards the most significant part of the field.  */
441   while ((unsigned int) cur_bitshift < len)
442     {
443       if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
444         {
445           /* This is the last byte.  */
446           *(data + cur_byte) &=
447             ~((1 << (len - cur_bitshift)) - 1);
448           *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
449         }
450       else
451         *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
452                               & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
453       cur_bitshift += FLOATFORMAT_CHAR_BIT;
454       if (order == floatformat_little)
455         ++cur_byte;
456       else
457         --cur_byte;
458     }
459 }
460
461 /* The converse: convert the double *FROM to an extended float
462    and store where TO points.  Neither FROM nor TO have any alignment
463    restrictions.  */
464
465 void
466 floatformat_from_double (const struct floatformat *fmt,
467                          const double *from, void *to)
468 {
469   double dfrom;
470   int exponent;
471   double mant;
472   unsigned int mant_bits, mant_off;
473   int mant_bits_left;
474   unsigned char *uto = (unsigned char *) to;
475
476   dfrom = *from;
477   memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
478
479   /* If negative, set the sign bit.  */
480   if (dfrom < 0)
481     {
482       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
483       dfrom = -dfrom;
484     }
485
486   if (dfrom == 0)
487     {
488       /* 0.0.  */
489       return;
490     }
491
492   if (dfrom != dfrom)
493     {
494       /* NaN.  */
495       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
496                  fmt->exp_len, fmt->exp_nan);
497       /* Be sure it's not infinity, but NaN value is irrelevant.  */
498       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
499                  32, 1);
500       return;
501     }
502
503   if (dfrom + dfrom == dfrom)
504     {
505       /* This can only happen for an infinite value (or zero, which we
506          already handled above).  */
507       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
508                  fmt->exp_len, fmt->exp_nan);
509       return;
510     }
511
512   mant = frexp (dfrom, &exponent);
513   if (exponent + fmt->exp_bias - 1 > 0)
514     put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
515                fmt->exp_len, exponent + fmt->exp_bias - 1);
516   else
517     {
518       /* Handle a denormalized number.  FIXME: What should we do for
519          non-IEEE formats?  */
520       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
521                  fmt->exp_len, 0);
522       mant = ldexp (mant, exponent + fmt->exp_bias - 1);
523     }
524
525   mant_bits_left = fmt->man_len;
526   mant_off = fmt->man_start;
527   while (mant_bits_left > 0)
528     {
529       unsigned long mant_long;
530       mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
531
532       mant *= 4294967296.0;
533       mant_long = (unsigned long)mant;
534       mant -= mant_long;
535
536       /* If the integer bit is implicit, and we are not creating a
537          denormalized number, then we need to discard it.  */
538       if ((unsigned int) mant_bits_left == fmt->man_len
539           && fmt->intbit == floatformat_intbit_no
540           && exponent + fmt->exp_bias - 1 > 0)
541         {
542           mant_long &= 0x7fffffff;
543           mant_bits -= 1;
544         }
545       else if (mant_bits < 32)
546         {
547           /* The bits we want are in the most significant MANT_BITS bits of
548              mant_long.  Move them to the least significant.  */
549           mant_long >>= 32 - mant_bits;
550         }
551
552       put_field (uto, fmt->byteorder, fmt->totalsize,
553                  mant_off, mant_bits, mant_long);
554       mant_off += mant_bits;
555       mant_bits_left -= mant_bits;
556     }
557 }
558
559 /* Return non-zero iff the data at FROM is a valid number in format FMT.  */
560
561 int
562 floatformat_is_valid (const struct floatformat *fmt, const void *from)
563 {
564   return fmt->is_valid (fmt, from);
565 }
566
567
568 #ifdef IEEE_DEBUG
569
570 #include <stdio.h>
571
572 /* This is to be run on a host which uses IEEE floating point.  */
573
574 void
575 ieee_test (double n)
576 {
577   double result;
578
579   floatformat_to_double (&floatformat_ieee_double_little, &n, &result);
580   if ((n != result && (! isnan (n) || ! isnan (result)))
581       || (n < 0 && result >= 0)
582       || (n >= 0 && result < 0))
583     printf ("Differ(to): %.20g -> %.20g\n", n, result);
584
585   floatformat_from_double (&floatformat_ieee_double_little, &n, &result);
586   if ((n != result && (! isnan (n) || ! isnan (result)))
587       || (n < 0 && result >= 0)
588       || (n >= 0 && result < 0))
589     printf ("Differ(from): %.20g -> %.20g\n", n, result);
590
591 #if 0
592   {
593     char exten[16];
594
595     floatformat_from_double (&floatformat_m68881_ext, &n, exten);
596     floatformat_to_double (&floatformat_m68881_ext, exten, &result);
597     if (n != result)
598       printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
599   }
600 #endif
601
602 #if IEEE_DEBUG > 1
603   /* This is to be run on a host which uses 68881 format.  */
604   {
605     long double ex = *(long double *)exten;
606     if (ex != n)
607       printf ("Differ(from vs. extended): %.20g\n", n);
608   }
609 #endif
610 }
611
612 int
613 main (void)
614 {
615   ieee_test (0.0);
616   ieee_test (0.5);
617   ieee_test (256.0);
618   ieee_test (0.12345);
619   ieee_test (234235.78907234);
620   ieee_test (-512.0);
621   ieee_test (-0.004321);
622   ieee_test (1.2E-70);
623   ieee_test (1.2E-316);
624   ieee_test (4.9406564584124654E-324);
625   ieee_test (- 4.9406564584124654E-324);
626   ieee_test (- 0.0);
627   ieee_test (- INFINITY);
628   ieee_test (- NAN);
629   ieee_test (INFINITY);
630   ieee_test (NAN);
631   return 0;
632 }
633 #endif