1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright 1991, 1994, 1999, 2000, 2003, 2005, 2006, 2010, 2012
3 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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. */
21 /* This is needed to pick up the NAN macro on some systems. */
34 /* On some platforms, <float.h> provides DBL_QNAN. */
40 #include "libiberty.h"
41 #include "floatformat.h"
45 #define INFINITY HUGE_VAL
47 #define INFINITY (1.0 / 0.0)
55 #define NAN (0.0 / 0.0)
59 static int mant_bits_set (const struct floatformat *, const unsigned char *);
60 static unsigned long get_field (const unsigned char *,
61 enum floatformat_byteorders,
65 static int floatformat_always_valid (const struct floatformat *fmt,
69 floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
70 const void *from ATTRIBUTE_UNUSED)
75 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
76 going to bother with trying to muck around with whether it is defined in
77 a system header, what we do if not, etc. */
78 #define FLOATFORMAT_CHAR_BIT 8
80 /* floatformats for IEEE half, single and double, big and little endian. */
81 const struct floatformat floatformat_ieee_half_big =
83 floatformat_big, 16, 0, 1, 5, 15, 31, 6, 10,
84 floatformat_intbit_no,
85 "floatformat_ieee_half_big",
86 floatformat_always_valid,
89 const struct floatformat floatformat_ieee_half_little =
91 floatformat_little, 16, 0, 1, 5, 15, 31, 6, 10,
92 floatformat_intbit_no,
93 "floatformat_ieee_half_little",
94 floatformat_always_valid,
97 const struct floatformat floatformat_ieee_single_big =
99 floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
100 floatformat_intbit_no,
101 "floatformat_ieee_single_big",
102 floatformat_always_valid,
105 const struct floatformat floatformat_ieee_single_little =
107 floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
108 floatformat_intbit_no,
109 "floatformat_ieee_single_little",
110 floatformat_always_valid,
113 const struct floatformat floatformat_ieee_double_big =
115 floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
116 floatformat_intbit_no,
117 "floatformat_ieee_double_big",
118 floatformat_always_valid,
121 const struct floatformat floatformat_ieee_double_little =
123 floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
124 floatformat_intbit_no,
125 "floatformat_ieee_double_little",
126 floatformat_always_valid,
130 /* floatformat for IEEE double, little endian byte order, with big endian word
131 ordering, as on the ARM. */
133 const struct floatformat floatformat_ieee_double_littlebyte_bigword =
135 floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
136 floatformat_intbit_no,
137 "floatformat_ieee_double_littlebyte_bigword",
138 floatformat_always_valid,
142 /* floatformat for VAX. Not quite IEEE, but close enough. */
144 const struct floatformat floatformat_vax_f =
146 floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
147 floatformat_intbit_no,
149 floatformat_always_valid,
152 const struct floatformat floatformat_vax_d =
154 floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
155 floatformat_intbit_no,
157 floatformat_always_valid,
160 const struct floatformat floatformat_vax_g =
162 floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
163 floatformat_intbit_no,
165 floatformat_always_valid,
169 static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
173 floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
175 /* In the i387 double-extended format, if the exponent is all ones,
176 then the integer bit must be set. If the exponent is neither 0
177 nor ~0, the intbit must also be set. Only if the exponent is
178 zero can it be zero, and then it must be zero. */
179 unsigned long exponent, int_bit;
180 const unsigned char *ufrom = (const unsigned char *) from;
182 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
183 fmt->exp_start, fmt->exp_len);
184 int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
187 if ((exponent == 0) != (int_bit == 0))
193 const struct floatformat floatformat_i387_ext =
195 floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
196 floatformat_intbit_yes,
197 "floatformat_i387_ext",
198 floatformat_i387_ext_is_valid,
201 const struct floatformat floatformat_m68881_ext =
203 /* Note that the bits from 16 to 31 are unused. */
204 floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
205 floatformat_intbit_yes,
206 "floatformat_m68881_ext",
207 floatformat_always_valid,
210 const struct floatformat floatformat_i960_ext =
212 /* Note that the bits from 0 to 15 are unused. */
213 floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
214 floatformat_intbit_yes,
215 "floatformat_i960_ext",
216 floatformat_always_valid,
219 const struct floatformat floatformat_m88110_ext =
221 floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
222 floatformat_intbit_yes,
223 "floatformat_m88110_ext",
224 floatformat_always_valid,
227 const struct floatformat floatformat_m88110_harris_ext =
229 /* Harris uses raw format 128 bytes long, but the number is just an ieee
230 double, and the last 64 bits are wasted. */
231 floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
232 floatformat_intbit_no,
233 "floatformat_m88110_ext_harris",
234 floatformat_always_valid,
237 const struct floatformat floatformat_arm_ext_big =
239 /* Bits 1 to 16 are unused. */
240 floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
241 floatformat_intbit_yes,
242 "floatformat_arm_ext_big",
243 floatformat_always_valid,
246 const struct floatformat floatformat_arm_ext_littlebyte_bigword =
248 /* Bits 1 to 16 are unused. */
249 floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
250 floatformat_intbit_yes,
251 "floatformat_arm_ext_littlebyte_bigword",
252 floatformat_always_valid,
255 const struct floatformat floatformat_ia64_spill_big =
257 floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
258 floatformat_intbit_yes,
259 "floatformat_ia64_spill_big",
260 floatformat_always_valid,
263 const struct floatformat floatformat_ia64_spill_little =
265 floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
266 floatformat_intbit_yes,
267 "floatformat_ia64_spill_little",
268 floatformat_always_valid,
271 const struct floatformat floatformat_ia64_quad_big =
273 floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
274 floatformat_intbit_no,
275 "floatformat_ia64_quad_big",
276 floatformat_always_valid,
279 const struct floatformat floatformat_ia64_quad_little =
281 floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
282 floatformat_intbit_no,
283 "floatformat_ia64_quad_little",
284 floatformat_always_valid,
289 floatformat_ibm_long_double_is_valid (const struct floatformat *fmt,
292 const unsigned char *ufrom = (const unsigned char *) from;
293 const struct floatformat *hfmt = fmt->split_half;
294 long top_exp, bot_exp;
297 top_exp = get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
298 hfmt->exp_start, hfmt->exp_len);
299 bot_exp = get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
300 hfmt->exp_start, hfmt->exp_len);
302 if ((unsigned long) top_exp == hfmt->exp_nan)
303 top_nan = mant_bits_set (hfmt, ufrom);
305 /* A NaN is valid with any low part. */
309 /* An infinity, zero or denormal requires low part 0 (positive or
311 if ((unsigned long) top_exp == hfmt->exp_nan || top_exp == 0)
316 return !mant_bits_set (hfmt, ufrom + 8);
319 /* The top part is now a finite normal value. The long double value
320 is the sum of the two parts, and the top part must equal the
321 result of rounding the long double value to nearest double. Thus
322 the bottom part must be <= 0.5ulp of the top part in absolute
323 value, and if it is < 0.5ulp then the long double is definitely
325 if (bot_exp < top_exp - 53)
327 if (bot_exp > top_exp - 53 && bot_exp != 0)
331 /* The bottom part is 0 or denormal. Determine which, and if
332 denormal the first two set bits. */
333 int first_bit = -1, second_bit = -1, cur_bit;
334 for (cur_bit = 0; (unsigned int) cur_bit < hfmt->man_len; cur_bit++)
335 if (get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
336 hfmt->man_start + cur_bit, 1))
342 second_bit = cur_bit;
346 /* Bottom part 0 is OK. */
349 /* The real exponent of the bottom part is -first_bit. */
350 if (-first_bit < top_exp - 53)
352 if (-first_bit > top_exp - 53)
354 /* The bottom part is at least 0.5ulp of the top part. For this
355 to be OK, the bottom part must be exactly 0.5ulp (i.e. no
356 more bits set) and the top part must have last bit 0. */
357 if (second_bit != -1)
359 return !get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
360 hfmt->man_start + hfmt->man_len - 1, 1);
364 /* The bottom part is at least 0.5ulp of the top part. For this
365 to be OK, it must be exactly 0.5ulp (i.e. no explicit bits
366 set) and the top part must have last bit 0. */
367 if (get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
368 hfmt->man_start + hfmt->man_len - 1, 1))
370 return !mant_bits_set (hfmt, ufrom + 8);
374 const struct floatformat floatformat_ibm_long_double_big =
376 floatformat_big, 128, 0, 1, 11, 1023, 2047, 12, 52,
377 floatformat_intbit_no,
378 "floatformat_ibm_long_double_big",
379 floatformat_ibm_long_double_is_valid,
380 &floatformat_ieee_double_big
383 const struct floatformat floatformat_ibm_long_double_little =
385 floatformat_little, 128, 0, 1, 11, 1023, 2047, 12, 52,
386 floatformat_intbit_no,
387 "floatformat_ibm_long_double_little",
388 floatformat_ibm_long_double_is_valid,
389 &floatformat_ieee_double_little
394 #define min(a, b) ((a) < (b) ? (a) : (b))
397 /* Return 1 if any bits are explicitly set in the mantissa of UFROM,
398 format FMT, 0 otherwise. */
400 mant_bits_set (const struct floatformat *fmt, const unsigned char *ufrom)
402 unsigned int mant_bits, mant_off;
405 mant_off = fmt->man_start;
406 mant_bits_left = fmt->man_len;
407 while (mant_bits_left > 0)
409 mant_bits = min (mant_bits_left, 32);
411 if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
412 mant_off, mant_bits) != 0)
415 mant_off += mant_bits;
416 mant_bits_left -= mant_bits;
421 /* Extract a field which starts at START and is LEN bits long. DATA and
422 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
424 get_field (const unsigned char *data, enum floatformat_byteorders order,
425 unsigned int total_len, unsigned int start, unsigned int len)
427 unsigned long result = 0;
428 unsigned int cur_byte;
429 int lo_bit, hi_bit, cur_bitshift = 0;
430 int nextbyte = (order == floatformat_little) ? 1 : -1;
432 /* Start is in big-endian bit order! Fix that first. */
433 start = total_len - (start + len);
435 /* Start at the least significant part of the field. */
436 if (order == floatformat_little)
437 cur_byte = start / FLOATFORMAT_CHAR_BIT;
439 cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
441 lo_bit = start % FLOATFORMAT_CHAR_BIT;
442 hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
446 unsigned int shifted = *(data + cur_byte) >> lo_bit;
447 unsigned int bits = hi_bit - lo_bit;
448 unsigned int mask = (1 << bits) - 1;
449 result |= (shifted & mask) << cur_bitshift;
451 cur_bitshift += bits;
452 cur_byte += nextbyte;
454 hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
461 /* Convert from FMT to a double.
462 FROM is the address of the extended float.
463 Store the double in *TO. */
466 floatformat_to_double (const struct floatformat *fmt,
467 const void *from, double *to)
469 const unsigned char *ufrom = (const unsigned char *) from;
473 unsigned int mant_bits, mant_off;
476 /* Split values are not handled specially, since the top half has
477 the correctly rounded double value (in the only supported case of
480 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
481 fmt->exp_start, fmt->exp_len);
483 /* If the exponent indicates a NaN, we don't have information to
484 decide what to do. So we handle it like IEEE, except that we
485 don't try to preserve the type of NaN. FIXME. */
486 if ((unsigned long) exponent == fmt->exp_nan)
488 int nan = mant_bits_set (fmt, ufrom);
490 /* On certain systems (such as GNU/Linux), the use of the
491 INFINITY macro below may generate a warning that can not be
492 silenced due to a bug in GCC (PR preprocessor/11931). The
493 preprocessor fails to recognise the __extension__ keyword in
494 conjunction with the GNU/C99 extension for hexadecimal
495 floating point constants and will issue a warning when
496 compiling with -pedantic. */
502 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
510 mant_bits_left = fmt->man_len;
511 mant_off = fmt->man_start;
514 /* Build the result algebraically. Might go infinite, underflow, etc;
517 /* For denorms use minimum exponent. */
519 exponent = 1 - fmt->exp_bias;
522 exponent -= fmt->exp_bias;
524 /* If this format uses a hidden bit, explicitly add it in now.
525 Otherwise, increment the exponent by one to account for the
528 if (fmt->intbit == floatformat_intbit_no)
529 dto = ldexp (1.0, exponent);
534 while (mant_bits_left > 0)
536 mant_bits = min (mant_bits_left, 32);
538 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
539 mant_off, mant_bits);
541 dto += ldexp ((double) mant, exponent - mant_bits);
542 exponent -= mant_bits;
543 mant_off += mant_bits;
544 mant_bits_left -= mant_bits;
547 /* Negate it if negative. */
548 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
553 static void put_field (unsigned char *, enum floatformat_byteorders,
559 /* Set a field which starts at START and is LEN bits long. DATA and
560 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
562 put_field (unsigned char *data, enum floatformat_byteorders order,
563 unsigned int total_len, unsigned int start, unsigned int len,
564 unsigned long stuff_to_put)
566 unsigned int cur_byte;
568 int nextbyte = (order == floatformat_little) ? 1 : -1;
570 /* Start is in big-endian bit order! Fix that first. */
571 start = total_len - (start + len);
573 /* Start at the least significant part of the field. */
574 if (order == floatformat_little)
575 cur_byte = start / FLOATFORMAT_CHAR_BIT;
577 cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
579 lo_bit = start % FLOATFORMAT_CHAR_BIT;
580 hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
584 unsigned char *byte_ptr = data + cur_byte;
585 unsigned int bits = hi_bit - lo_bit;
586 unsigned int mask = ((1 << bits) - 1) << lo_bit;
587 *byte_ptr = (*byte_ptr & ~mask) | ((stuff_to_put << lo_bit) & mask);
588 stuff_to_put >>= bits;
590 cur_byte += nextbyte;
592 hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
597 /* The converse: convert the double *FROM to an extended float
598 and store where TO points. Neither FROM nor TO have any alignment
602 floatformat_from_double (const struct floatformat *fmt,
603 const double *from, void *to)
608 unsigned int mant_bits, mant_off;
610 unsigned char *uto = (unsigned char *) to;
613 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
615 /* Split values are not handled specially, since a bottom half of
616 zero is correct for any value representable as double (in the
617 only supported case of split values). */
619 /* If negative, set the sign bit. */
622 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
635 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
636 fmt->exp_len, fmt->exp_nan);
637 /* Be sure it's not infinity, but NaN value is irrelevant. */
638 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
643 if (dfrom + dfrom == dfrom)
645 /* This can only happen for an infinite value (or zero, which we
646 already handled above). */
647 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
648 fmt->exp_len, fmt->exp_nan);
652 mant = frexp (dfrom, &exponent);
653 if (exponent + fmt->exp_bias - 1 > 0)
654 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
655 fmt->exp_len, exponent + fmt->exp_bias - 1);
658 /* Handle a denormalized number. FIXME: What should we do for
660 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
662 mant = ldexp (mant, exponent + fmt->exp_bias - 1);
665 mant_bits_left = fmt->man_len;
666 mant_off = fmt->man_start;
667 while (mant_bits_left > 0)
669 unsigned long mant_long;
670 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
672 mant *= 4294967296.0;
673 mant_long = (unsigned long)mant;
676 /* If the integer bit is implicit, and we are not creating a
677 denormalized number, then we need to discard it. */
678 if ((unsigned int) mant_bits_left == fmt->man_len
679 && fmt->intbit == floatformat_intbit_no
680 && exponent + fmt->exp_bias - 1 > 0)
682 mant_long &= 0x7fffffff;
685 else if (mant_bits < 32)
687 /* The bits we want are in the most significant MANT_BITS bits of
688 mant_long. Move them to the least significant. */
689 mant_long >>= 32 - mant_bits;
692 put_field (uto, fmt->byteorder, fmt->totalsize,
693 mant_off, mant_bits, mant_long);
694 mant_off += mant_bits;
695 mant_bits_left -= mant_bits;
699 /* Return non-zero iff the data at FROM is a valid number in format FMT. */
702 floatformat_is_valid (const struct floatformat *fmt, const void *from)
704 return fmt->is_valid (fmt, from);
712 /* This is to be run on a host which uses IEEE floating point. */
719 floatformat_to_double (&floatformat_ieee_double_little, &n, &result);
720 if ((n != result && (! isnan (n) || ! isnan (result)))
721 || (n < 0 && result >= 0)
722 || (n >= 0 && result < 0))
723 printf ("Differ(to): %.20g -> %.20g\n", n, result);
725 floatformat_from_double (&floatformat_ieee_double_little, &n, &result);
726 if ((n != result && (! isnan (n) || ! isnan (result)))
727 || (n < 0 && result >= 0)
728 || (n >= 0 && result < 0))
729 printf ("Differ(from): %.20g -> %.20g\n", n, result);
735 floatformat_from_double (&floatformat_m68881_ext, &n, exten);
736 floatformat_to_double (&floatformat_m68881_ext, exten, &result);
738 printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
743 /* This is to be run on a host which uses 68881 format. */
745 long double ex = *(long double *)exten;
747 printf ("Differ(from vs. extended): %.20g\n", n);
760 ieee_test (234235.78907234);
762 ieee_test (-0.004321);
764 ieee_test (1.2E-316);
765 ieee_test (4.9406564584124654E-324);
766 ieee_test (- 4.9406564584124654E-324);
768 ieee_test (- INFINITY);
770 ieee_test (INFINITY);