1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright 1991, 1994, 1999, 2000, 2003, 2005, 2006, 2010
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. */
31 /* On some platforms, <float.h> provides DBL_QNAN. */
37 #include "floatformat.h"
41 #define INFINITY HUGE_VAL
43 #define INFINITY (1.0 / 0.0)
51 #define NAN (0.0 / 0.0)
55 static int mant_bits_set (const struct floatformat *, const unsigned char *);
56 static unsigned long get_field (const unsigned char *,
57 enum floatformat_byteorders,
61 static int floatformat_always_valid (const struct floatformat *fmt,
65 floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
66 const void *from ATTRIBUTE_UNUSED)
71 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
72 going to bother with trying to muck around with whether it is defined in
73 a system header, what we do if not, etc. */
74 #define FLOATFORMAT_CHAR_BIT 8
76 /* floatformats for IEEE half, single and double, big and little endian. */
77 const struct floatformat floatformat_ieee_half_big =
79 floatformat_big, 16, 0, 1, 5, 15, 31, 6, 10,
80 floatformat_intbit_no,
81 "floatformat_ieee_half_big",
82 floatformat_always_valid,
85 const struct floatformat floatformat_ieee_half_little =
87 floatformat_little, 16, 0, 1, 5, 15, 31, 6, 10,
88 floatformat_intbit_no,
89 "floatformat_ieee_half_little",
90 floatformat_always_valid,
93 const struct floatformat floatformat_ieee_single_big =
95 floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
96 floatformat_intbit_no,
97 "floatformat_ieee_single_big",
98 floatformat_always_valid,
101 const struct floatformat floatformat_ieee_single_little =
103 floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
104 floatformat_intbit_no,
105 "floatformat_ieee_single_little",
106 floatformat_always_valid,
109 const struct floatformat floatformat_ieee_double_big =
111 floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
112 floatformat_intbit_no,
113 "floatformat_ieee_double_big",
114 floatformat_always_valid,
117 const struct floatformat floatformat_ieee_double_little =
119 floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
120 floatformat_intbit_no,
121 "floatformat_ieee_double_little",
122 floatformat_always_valid,
126 /* floatformat for IEEE double, little endian byte order, with big endian word
127 ordering, as on the ARM. */
129 const struct floatformat floatformat_ieee_double_littlebyte_bigword =
131 floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
132 floatformat_intbit_no,
133 "floatformat_ieee_double_littlebyte_bigword",
134 floatformat_always_valid,
138 /* floatformat for VAX. Not quite IEEE, but close enough. */
140 const struct floatformat floatformat_vax_f =
142 floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
143 floatformat_intbit_no,
145 floatformat_always_valid,
148 const struct floatformat floatformat_vax_d =
150 floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
151 floatformat_intbit_no,
153 floatformat_always_valid,
156 const struct floatformat floatformat_vax_g =
158 floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
159 floatformat_intbit_no,
161 floatformat_always_valid,
165 static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
169 floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
171 /* In the i387 double-extended format, if the exponent is all ones,
172 then the integer bit must be set. If the exponent is neither 0
173 nor ~0, the intbit must also be set. Only if the exponent is
174 zero can it be zero, and then it must be zero. */
175 unsigned long exponent, int_bit;
176 const unsigned char *ufrom = (const unsigned char *) from;
178 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
179 fmt->exp_start, fmt->exp_len);
180 int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
183 if ((exponent == 0) != (int_bit == 0))
189 const struct floatformat floatformat_i387_ext =
191 floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
192 floatformat_intbit_yes,
193 "floatformat_i387_ext",
194 floatformat_i387_ext_is_valid,
197 const struct floatformat floatformat_m68881_ext =
199 /* Note that the bits from 16 to 31 are unused. */
200 floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
201 floatformat_intbit_yes,
202 "floatformat_m68881_ext",
203 floatformat_always_valid,
206 const struct floatformat floatformat_i960_ext =
208 /* Note that the bits from 0 to 15 are unused. */
209 floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
210 floatformat_intbit_yes,
211 "floatformat_i960_ext",
212 floatformat_always_valid,
215 const struct floatformat floatformat_m88110_ext =
217 floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
218 floatformat_intbit_yes,
219 "floatformat_m88110_ext",
220 floatformat_always_valid,
223 const struct floatformat floatformat_m88110_harris_ext =
225 /* Harris uses raw format 128 bytes long, but the number is just an ieee
226 double, and the last 64 bits are wasted. */
227 floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
228 floatformat_intbit_no,
229 "floatformat_m88110_ext_harris",
230 floatformat_always_valid,
233 const struct floatformat floatformat_arm_ext_big =
235 /* Bits 1 to 16 are unused. */
236 floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
237 floatformat_intbit_yes,
238 "floatformat_arm_ext_big",
239 floatformat_always_valid,
242 const struct floatformat floatformat_arm_ext_littlebyte_bigword =
244 /* Bits 1 to 16 are unused. */
245 floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
246 floatformat_intbit_yes,
247 "floatformat_arm_ext_littlebyte_bigword",
248 floatformat_always_valid,
251 const struct floatformat floatformat_ia64_spill_big =
253 floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
254 floatformat_intbit_yes,
255 "floatformat_ia64_spill_big",
256 floatformat_always_valid,
259 const struct floatformat floatformat_ia64_spill_little =
261 floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
262 floatformat_intbit_yes,
263 "floatformat_ia64_spill_little",
264 floatformat_always_valid,
267 const struct floatformat floatformat_ia64_quad_big =
269 floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
270 floatformat_intbit_no,
271 "floatformat_ia64_quad_big",
272 floatformat_always_valid,
275 const struct floatformat floatformat_ia64_quad_little =
277 floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
278 floatformat_intbit_no,
279 "floatformat_ia64_quad_little",
280 floatformat_always_valid,
285 floatformat_ibm_long_double_is_valid (const struct floatformat *fmt,
288 const unsigned char *ufrom = (const unsigned char *) from;
289 const struct floatformat *hfmt = fmt->split_half;
290 long top_exp, bot_exp;
293 top_exp = get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
294 hfmt->exp_start, hfmt->exp_len);
295 bot_exp = get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
296 hfmt->exp_start, hfmt->exp_len);
298 if ((unsigned long) top_exp == hfmt->exp_nan)
299 top_nan = mant_bits_set (hfmt, ufrom);
301 /* A NaN is valid with any low part. */
305 /* An infinity, zero or denormal requires low part 0 (positive or
307 if ((unsigned long) top_exp == hfmt->exp_nan || top_exp == 0)
312 return !mant_bits_set (hfmt, ufrom + 8);
315 /* The top part is now a finite normal value. The long double value
316 is the sum of the two parts, and the top part must equal the
317 result of rounding the long double value to nearest double. Thus
318 the bottom part must be <= 0.5ulp of the top part in absolute
319 value, and if it is < 0.5ulp then the long double is definitely
321 if (bot_exp < top_exp - 53)
323 if (bot_exp > top_exp - 53 && bot_exp != 0)
327 /* The bottom part is 0 or denormal. Determine which, and if
328 denormal the first two set bits. */
329 int first_bit = -1, second_bit = -1, cur_bit;
330 for (cur_bit = 0; (unsigned int) cur_bit < hfmt->man_len; cur_bit++)
331 if (get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
332 hfmt->man_start + cur_bit, 1))
338 second_bit = cur_bit;
342 /* Bottom part 0 is OK. */
345 /* The real exponent of the bottom part is -first_bit. */
346 if (-first_bit < top_exp - 53)
348 if (-first_bit > top_exp - 53)
350 /* The bottom part is at least 0.5ulp of the top part. For this
351 to be OK, the bottom part must be exactly 0.5ulp (i.e. no
352 more bits set) and the top part must have last bit 0. */
353 if (second_bit != -1)
355 return !get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
356 hfmt->man_start + hfmt->man_len - 1, 1);
360 /* The bottom part is at least 0.5ulp of the top part. For this
361 to be OK, it must be exactly 0.5ulp (i.e. no explicit bits
362 set) and the top part must have last bit 0. */
363 if (get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
364 hfmt->man_start + hfmt->man_len - 1, 1))
366 return !mant_bits_set (hfmt, ufrom + 8);
370 const struct floatformat floatformat_ibm_long_double =
372 floatformat_big, 128, 0, 1, 11, 1023, 2047, 12, 52,
373 floatformat_intbit_no,
374 "floatformat_ibm_long_double",
375 floatformat_ibm_long_double_is_valid,
376 &floatformat_ieee_double_big
381 #define min(a, b) ((a) < (b) ? (a) : (b))
384 /* Return 1 if any bits are explicitly set in the mantissa of UFROM,
385 format FMT, 0 otherwise. */
387 mant_bits_set (const struct floatformat *fmt, const unsigned char *ufrom)
389 unsigned int mant_bits, mant_off;
392 mant_off = fmt->man_start;
393 mant_bits_left = fmt->man_len;
394 while (mant_bits_left > 0)
396 mant_bits = min (mant_bits_left, 32);
398 if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
399 mant_off, mant_bits) != 0)
402 mant_off += mant_bits;
403 mant_bits_left -= mant_bits;
408 /* Extract a field which starts at START and is LEN bits long. DATA and
409 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
411 get_field (const unsigned char *data, enum floatformat_byteorders order,
412 unsigned int total_len, unsigned int start, unsigned int len)
414 unsigned long result = 0;
415 unsigned int cur_byte;
416 int lo_bit, hi_bit, cur_bitshift = 0;
417 int nextbyte = (order == floatformat_little) ? 1 : -1;
419 /* Start is in big-endian bit order! Fix that first. */
420 start = total_len - (start + len);
422 /* Start at the least significant part of the field. */
423 if (order == floatformat_little)
424 cur_byte = start / FLOATFORMAT_CHAR_BIT;
426 cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
428 lo_bit = start % FLOATFORMAT_CHAR_BIT;
429 hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
433 unsigned int shifted = *(data + cur_byte) >> lo_bit;
434 unsigned int bits = hi_bit - lo_bit;
435 unsigned int mask = (1 << bits) - 1;
436 result |= (shifted & mask) << cur_bitshift;
438 cur_bitshift += bits;
439 cur_byte += nextbyte;
441 hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
448 /* Convert from FMT to a double.
449 FROM is the address of the extended float.
450 Store the double in *TO. */
453 floatformat_to_double (const struct floatformat *fmt,
454 const void *from, double *to)
456 const unsigned char *ufrom = (const unsigned char *) from;
460 unsigned int mant_bits, mant_off;
462 int special_exponent; /* It's a NaN, denorm or zero */
464 /* Split values are not handled specially, since the top half has
465 the correctly rounded double value (in the only supported case of
468 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
469 fmt->exp_start, fmt->exp_len);
471 /* If the exponent indicates a NaN, we don't have information to
472 decide what to do. So we handle it like IEEE, except that we
473 don't try to preserve the type of NaN. FIXME. */
474 if ((unsigned long) exponent == fmt->exp_nan)
476 int nan = mant_bits_set (fmt, ufrom);
478 /* On certain systems (such as GNU/Linux), the use of the
479 INFINITY macro below may generate a warning that can not be
480 silenced due to a bug in GCC (PR preprocessor/11931). The
481 preprocessor fails to recognise the __extension__ keyword in
482 conjunction with the GNU/C99 extension for hexadecimal
483 floating point constants and will issue a warning when
484 compiling with -pedantic. */
490 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
498 mant_bits_left = fmt->man_len;
499 mant_off = fmt->man_start;
502 special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
504 /* Don't bias zero's, denorms or NaNs. */
505 if (!special_exponent)
506 exponent -= fmt->exp_bias;
508 /* Build the result algebraically. Might go infinite, underflow, etc;
511 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
512 increment the exponent by one to account for the integer bit. */
514 if (!special_exponent)
516 if (fmt->intbit == floatformat_intbit_no)
517 dto = ldexp (1.0, exponent);
522 while (mant_bits_left > 0)
524 mant_bits = min (mant_bits_left, 32);
526 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
527 mant_off, mant_bits);
529 /* Handle denormalized numbers. FIXME: What should we do for
531 if (special_exponent && exponent == 0 && mant != 0)
532 dto += ldexp ((double)mant,
535 - (mant_off - fmt->man_start)
538 dto += ldexp ((double)mant, exponent - mant_bits);
540 exponent -= mant_bits;
541 mant_off += mant_bits;
542 mant_bits_left -= mant_bits;
545 /* Negate it if negative. */
546 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
551 static void put_field (unsigned char *, enum floatformat_byteorders,
557 /* Set a field which starts at START and is LEN bits long. DATA and
558 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
560 put_field (unsigned char *data, enum floatformat_byteorders order,
561 unsigned int total_len, unsigned int start, unsigned int len,
562 unsigned long stuff_to_put)
564 unsigned int cur_byte;
566 int nextbyte = (order == floatformat_little) ? 1 : -1;
568 /* Start is in big-endian bit order! Fix that first. */
569 start = total_len - (start + len);
571 /* Start at the least significant part of the field. */
572 if (order == floatformat_little)
573 cur_byte = start / FLOATFORMAT_CHAR_BIT;
575 cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
577 lo_bit = start % FLOATFORMAT_CHAR_BIT;
578 hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
582 unsigned char *byte_ptr = data + cur_byte;
583 unsigned int bits = hi_bit - lo_bit;
584 unsigned int mask = ((1 << bits) - 1) << lo_bit;
585 *byte_ptr = (*byte_ptr & ~mask) | ((stuff_to_put << lo_bit) & mask);
586 stuff_to_put >>= bits;
588 cur_byte += nextbyte;
590 hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
595 /* The converse: convert the double *FROM to an extended float
596 and store where TO points. Neither FROM nor TO have any alignment
600 floatformat_from_double (const struct floatformat *fmt,
601 const double *from, void *to)
606 unsigned int mant_bits, mant_off;
608 unsigned char *uto = (unsigned char *) to;
611 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
613 /* Split values are not handled specially, since a bottom half of
614 zero is correct for any value representable as double (in the
615 only supported case of split values). */
617 /* If negative, set the sign bit. */
620 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
633 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
634 fmt->exp_len, fmt->exp_nan);
635 /* Be sure it's not infinity, but NaN value is irrelevant. */
636 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
641 if (dfrom + dfrom == dfrom)
643 /* This can only happen for an infinite value (or zero, which we
644 already handled above). */
645 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
646 fmt->exp_len, fmt->exp_nan);
650 mant = frexp (dfrom, &exponent);
651 if (exponent + fmt->exp_bias - 1 > 0)
652 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
653 fmt->exp_len, exponent + fmt->exp_bias - 1);
656 /* Handle a denormalized number. FIXME: What should we do for
658 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
660 mant = ldexp (mant, exponent + fmt->exp_bias - 1);
663 mant_bits_left = fmt->man_len;
664 mant_off = fmt->man_start;
665 while (mant_bits_left > 0)
667 unsigned long mant_long;
668 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
670 mant *= 4294967296.0;
671 mant_long = (unsigned long)mant;
674 /* If the integer bit is implicit, and we are not creating a
675 denormalized number, then we need to discard it. */
676 if ((unsigned int) mant_bits_left == fmt->man_len
677 && fmt->intbit == floatformat_intbit_no
678 && exponent + fmt->exp_bias - 1 > 0)
680 mant_long &= 0x7fffffff;
683 else if (mant_bits < 32)
685 /* The bits we want are in the most significant MANT_BITS bits of
686 mant_long. Move them to the least significant. */
687 mant_long >>= 32 - mant_bits;
690 put_field (uto, fmt->byteorder, fmt->totalsize,
691 mant_off, mant_bits, mant_long);
692 mant_off += mant_bits;
693 mant_bits_left -= mant_bits;
697 /* Return non-zero iff the data at FROM is a valid number in format FMT. */
700 floatformat_is_valid (const struct floatformat *fmt, const void *from)
702 return fmt->is_valid (fmt, from);
710 /* This is to be run on a host which uses IEEE floating point. */
717 floatformat_to_double (&floatformat_ieee_double_little, &n, &result);
718 if ((n != result && (! isnan (n) || ! isnan (result)))
719 || (n < 0 && result >= 0)
720 || (n >= 0 && result < 0))
721 printf ("Differ(to): %.20g -> %.20g\n", n, result);
723 floatformat_from_double (&floatformat_ieee_double_little, &n, &result);
724 if ((n != result && (! isnan (n) || ! isnan (result)))
725 || (n < 0 && result >= 0)
726 || (n >= 0 && result < 0))
727 printf ("Differ(from): %.20g -> %.20g\n", n, result);
733 floatformat_from_double (&floatformat_m68881_ext, &n, exten);
734 floatformat_to_double (&floatformat_m68881_ext, exten, &result);
736 printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
741 /* This is to be run on a host which uses 68881 format. */
743 long double ex = *(long double *)exten;
745 printf ("Differ(from vs. extended): %.20g\n", n);
757 ieee_test (234235.78907234);
759 ieee_test (-0.004321);
761 ieee_test (1.2E-316);
762 ieee_test (4.9406564584124654E-324);
763 ieee_test (- 4.9406564584124654E-324);
765 ieee_test (- INFINITY);
767 ieee_test (INFINITY);
.