1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright 1991, 1994, 1999, 2000, 2003, 2005
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. */
35 #include "libiberty.h"
36 #include "floatformat.h"
40 #define INFINITY HUGE_VAL
42 #define INFINITY (1.0 / 0.0)
47 #define NAN (0.0 / 0.0)
50 static unsigned long get_field (const unsigned char *,
51 enum floatformat_byteorders,
55 static int floatformat_always_valid (const struct floatformat *fmt,
59 floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
60 const void *from ATTRIBUTE_UNUSED)
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
70 /* floatformats for IEEE single and double, big and little endian. */
71 const struct floatformat floatformat_ieee_single_big =
73 floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
74 floatformat_intbit_no,
75 "floatformat_ieee_single_big",
76 floatformat_always_valid
78 const struct floatformat floatformat_ieee_single_little =
80 floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
81 floatformat_intbit_no,
82 "floatformat_ieee_single_little",
83 floatformat_always_valid
85 const struct floatformat floatformat_ieee_double_big =
87 floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
88 floatformat_intbit_no,
89 "floatformat_ieee_double_big",
90 floatformat_always_valid
92 const struct floatformat floatformat_ieee_double_little =
94 floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
95 floatformat_intbit_no,
96 "floatformat_ieee_double_little",
97 floatformat_always_valid
100 /* floatformat for IEEE double, little endian byte order, with big endian word
101 ordering, as on the ARM. */
103 const struct floatformat floatformat_ieee_double_littlebyte_bigword =
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
111 /* floatformat for VAX. Not quite IEEE, but close enough. */
113 const struct floatformat floatformat_vax_f =
115 floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
116 floatformat_intbit_no,
118 floatformat_always_valid
120 const struct floatformat floatformat_vax_d =
122 floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
123 floatformat_intbit_no,
125 floatformat_always_valid
127 const struct floatformat floatformat_vax_g =
129 floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
130 floatformat_intbit_no,
132 floatformat_always_valid
135 static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
139 floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
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;
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,
153 if ((exponent == 0) != (int_bit == 0))
159 const struct floatformat floatformat_i387_ext =
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
166 const struct floatformat floatformat_m68881_ext =
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
174 const struct floatformat floatformat_i960_ext =
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
182 const struct floatformat floatformat_m88110_ext =
184 floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
185 floatformat_intbit_yes,
186 "floatformat_m88110_ext",
187 floatformat_always_valid
189 const struct floatformat floatformat_m88110_harris_ext =
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
198 const struct floatformat floatformat_arm_ext_big =
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
206 const struct floatformat floatformat_arm_ext_littlebyte_bigword =
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
214 const struct floatformat floatformat_ia64_spill_big =
216 floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
217 floatformat_intbit_yes,
218 "floatformat_ia64_spill_big",
219 floatformat_always_valid
221 const struct floatformat floatformat_ia64_spill_little =
223 floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
224 floatformat_intbit_yes,
225 "floatformat_ia64_spill_little",
226 floatformat_always_valid
228 const struct floatformat floatformat_ia64_quad_big =
230 floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
231 floatformat_intbit_no,
232 "floatformat_ia64_quad_big",
233 floatformat_always_valid
235 const struct floatformat floatformat_ia64_quad_little =
237 floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
238 floatformat_intbit_no,
239 "floatformat_ia64_quad_little",
240 floatformat_always_valid
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. */
246 get_field (const unsigned char *data, enum floatformat_byteorders order,
247 unsigned int total_len, unsigned int start, unsigned int len)
249 unsigned long result;
250 unsigned int cur_byte;
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;
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)
266 /* Move towards the most significant part of the field. */
267 while ((unsigned int) cur_bitshift < len)
269 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
270 /* This is the last byte; zero out the bits which are not part of
273 (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
276 result |= *(data + cur_byte) << cur_bitshift;
277 cur_bitshift += FLOATFORMAT_CHAR_BIT;
278 if (order == floatformat_little)
287 #define min(a, b) ((a) < (b) ? (a) : (b))
290 /* Convert from FMT to a double.
291 FROM is the address of the extended float.
292 Store the double in *TO. */
295 floatformat_to_double (const struct floatformat *fmt,
296 const void *from, double *to)
298 const unsigned char *ufrom = (const unsigned char *) from;
302 unsigned int mant_bits, mant_off;
304 int special_exponent; /* It's a NaN, denorm or zero */
306 exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
307 fmt->exp_start, fmt->exp_len);
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)
316 mant_off = fmt->man_start;
317 mant_bits_left = fmt->man_len;
319 while (mant_bits_left > 0)
321 mant_bits = min (mant_bits_left, 32);
323 if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
324 mant_off, mant_bits) != 0)
331 mant_off += mant_bits;
332 mant_bits_left -= mant_bits;
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. */
347 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
355 mant_bits_left = fmt->man_len;
356 mant_off = fmt->man_start;
359 special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
361 /* Don't bias zero's, denorms or NaNs. */
362 if (!special_exponent)
363 exponent -= fmt->exp_bias;
365 /* Build the result algebraically. Might go infinite, underflow, etc;
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. */
371 if (!special_exponent)
373 if (fmt->intbit == floatformat_intbit_no)
374 dto = ldexp (1.0, exponent);
379 while (mant_bits_left > 0)
381 mant_bits = min (mant_bits_left, 32);
383 mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
384 mant_off, mant_bits);
386 /* Handle denormalized numbers. FIXME: What should we do for
388 if (exponent == 0 && mant != 0)
389 dto += ldexp ((double)mant,
392 - (mant_off - fmt->man_start)
395 dto += ldexp ((double)mant, exponent - mant_bits);
397 exponent -= mant_bits;
398 mant_off += mant_bits;
399 mant_bits_left -= mant_bits;
402 /* Negate it if negative. */
403 if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
408 static void put_field (unsigned char *, enum floatformat_byteorders,
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. */
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)
421 unsigned int cur_byte;
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;
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)
440 /* Move towards the most significant part of the field. */
441 while ((unsigned int) cur_bitshift < len)
443 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
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);
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)
461 /* The converse: convert the double *FROM to an extended float
462 and store where TO points. Neither FROM nor TO have any alignment
466 floatformat_from_double (const struct floatformat *fmt,
467 const double *from, void *to)
472 unsigned int mant_bits, mant_off;
474 unsigned char *uto = (unsigned char *) to;
477 memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
479 /* If negative, set the sign bit. */
482 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
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,
503 if (dfrom + dfrom == dfrom)
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);
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);
518 /* Handle a denormalized number. FIXME: What should we do for
520 put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
522 mant = ldexp (mant, exponent + fmt->exp_bias - 1);
525 mant_bits_left = fmt->man_len;
526 mant_off = fmt->man_start;
527 while (mant_bits_left > 0)
529 unsigned long mant_long;
530 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
532 mant *= 4294967296.0;
533 mant_long = (unsigned long)mant;
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)
542 mant_long &= 0x7fffffff;
545 else if (mant_bits < 32)
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;
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;
559 /* Return non-zero iff the data at FROM is a valid number in format FMT. */
562 floatformat_is_valid (const struct floatformat *fmt, const void *from)
564 return fmt->is_valid (fmt, from);
572 /* This is to be run on a host which uses IEEE floating point. */
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);
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);
595 floatformat_from_double (&floatformat_m68881_ext, &n, exten);
596 floatformat_to_double (&floatformat_m68881_ext, exten, &result);
598 printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
603 /* This is to be run on a host which uses 68881 format. */
605 long double ex = *(long double *)exten;
607 printf ("Differ(from vs. extended): %.20g\n", n);
619 ieee_test (234235.78907234);
621 ieee_test (-0.004321);
623 ieee_test (1.2E-316);
624 ieee_test (4.9406564584124654E-324);
625 ieee_test (- 4.9406564584124654E-324);
627 ieee_test (- INFINITY);
629 ieee_test (INFINITY);