1 /* IEEE floating point support routines, for GDB, the GNU Debugger.
2 Copyright (C) 1991-2020 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* This is needed to pick up the NAN macro on some systems. */
35 /* On some platforms, <float.h> provides DBL_QNAN. */
41 #include "libiberty.h"
42 #include "floatformat.h"
46 #define INFINITY HUGE_VAL
48 #define INFINITY (1.0 / 0.0)
56 #define NAN (0.0 / 0.0)
60 static int mant_bits_set (const struct floatformat
*, const unsigned char *);
61 static unsigned long get_field (const unsigned char *,
62 enum floatformat_byteorders
,
66 static int floatformat_always_valid (const struct floatformat
*fmt
,
70 floatformat_always_valid (const struct floatformat
*fmt ATTRIBUTE_UNUSED
,
71 const void *from ATTRIBUTE_UNUSED
)
76 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
77 going to bother with trying to muck around with whether it is defined in
78 a system header, what we do if not, etc. */
79 #define FLOATFORMAT_CHAR_BIT 8
81 /* floatformats for IEEE half, single and double, big and little endian. */
82 const struct floatformat floatformat_ieee_half_big
=
84 floatformat_big
, 16, 0, 1, 5, 15, 31, 6, 10,
85 floatformat_intbit_no
,
86 "floatformat_ieee_half_big",
87 floatformat_always_valid
,
90 const struct floatformat floatformat_ieee_half_little
=
92 floatformat_little
, 16, 0, 1, 5, 15, 31, 6, 10,
93 floatformat_intbit_no
,
94 "floatformat_ieee_half_little",
95 floatformat_always_valid
,
98 const struct floatformat floatformat_ieee_single_big
=
100 floatformat_big
, 32, 0, 1, 8, 127, 255, 9, 23,
101 floatformat_intbit_no
,
102 "floatformat_ieee_single_big",
103 floatformat_always_valid
,
106 const struct floatformat floatformat_ieee_single_little
=
108 floatformat_little
, 32, 0, 1, 8, 127, 255, 9, 23,
109 floatformat_intbit_no
,
110 "floatformat_ieee_single_little",
111 floatformat_always_valid
,
114 const struct floatformat floatformat_ieee_double_big
=
116 floatformat_big
, 64, 0, 1, 11, 1023, 2047, 12, 52,
117 floatformat_intbit_no
,
118 "floatformat_ieee_double_big",
119 floatformat_always_valid
,
122 const struct floatformat floatformat_ieee_double_little
=
124 floatformat_little
, 64, 0, 1, 11, 1023, 2047, 12, 52,
125 floatformat_intbit_no
,
126 "floatformat_ieee_double_little",
127 floatformat_always_valid
,
131 /* floatformat for IEEE double, little endian byte order, with big endian word
132 ordering, as on the ARM. */
134 const struct floatformat floatformat_ieee_double_littlebyte_bigword
=
136 floatformat_littlebyte_bigword
, 64, 0, 1, 11, 1023, 2047, 12, 52,
137 floatformat_intbit_no
,
138 "floatformat_ieee_double_littlebyte_bigword",
139 floatformat_always_valid
,
143 /* floatformat for VAX. Not quite IEEE, but close enough. */
145 const struct floatformat floatformat_vax_f
=
147 floatformat_vax
, 32, 0, 1, 8, 129, 0, 9, 23,
148 floatformat_intbit_no
,
150 floatformat_always_valid
,
153 const struct floatformat floatformat_vax_d
=
155 floatformat_vax
, 64, 0, 1, 8, 129, 0, 9, 55,
156 floatformat_intbit_no
,
158 floatformat_always_valid
,
161 const struct floatformat floatformat_vax_g
=
163 floatformat_vax
, 64, 0, 1, 11, 1025, 0, 12, 52,
164 floatformat_intbit_no
,
166 floatformat_always_valid
,
170 static int floatformat_i387_ext_is_valid (const struct floatformat
*fmt
,
174 floatformat_i387_ext_is_valid (const struct floatformat
*fmt
, const void *from
)
176 /* In the i387 double-extended format, if the exponent is all ones,
177 then the integer bit must be set. If the exponent is neither 0
178 nor ~0, the intbit must also be set. Only if the exponent is
179 zero can it be zero, and then it must be zero. */
180 unsigned long exponent
, int_bit
;
181 const unsigned char *ufrom
= (const unsigned char *) from
;
183 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
184 fmt
->exp_start
, fmt
->exp_len
);
185 int_bit
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
188 if ((exponent
== 0) != (int_bit
== 0))
194 const struct floatformat floatformat_i387_ext
=
196 floatformat_little
, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
197 floatformat_intbit_yes
,
198 "floatformat_i387_ext",
199 floatformat_i387_ext_is_valid
,
202 const struct floatformat floatformat_m68881_ext
=
204 /* Note that the bits from 16 to 31 are unused. */
205 floatformat_big
, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
206 floatformat_intbit_yes
,
207 "floatformat_m68881_ext",
208 floatformat_always_valid
,
211 const struct floatformat floatformat_i960_ext
=
213 /* Note that the bits from 0 to 15 are unused. */
214 floatformat_little
, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
215 floatformat_intbit_yes
,
216 "floatformat_i960_ext",
217 floatformat_always_valid
,
220 const struct floatformat floatformat_m88110_ext
=
222 floatformat_big
, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
223 floatformat_intbit_yes
,
224 "floatformat_m88110_ext",
225 floatformat_always_valid
,
228 const struct floatformat floatformat_m88110_harris_ext
=
230 /* Harris uses raw format 128 bytes long, but the number is just an ieee
231 double, and the last 64 bits are wasted. */
232 floatformat_big
,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
233 floatformat_intbit_no
,
234 "floatformat_m88110_ext_harris",
235 floatformat_always_valid
,
238 const struct floatformat floatformat_arm_ext_big
=
240 /* Bits 1 to 16 are unused. */
241 floatformat_big
, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
242 floatformat_intbit_yes
,
243 "floatformat_arm_ext_big",
244 floatformat_always_valid
,
247 const struct floatformat floatformat_arm_ext_littlebyte_bigword
=
249 /* Bits 1 to 16 are unused. */
250 floatformat_littlebyte_bigword
, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
251 floatformat_intbit_yes
,
252 "floatformat_arm_ext_littlebyte_bigword",
253 floatformat_always_valid
,
256 const struct floatformat floatformat_ia64_spill_big
=
258 floatformat_big
, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
259 floatformat_intbit_yes
,
260 "floatformat_ia64_spill_big",
261 floatformat_always_valid
,
264 const struct floatformat floatformat_ia64_spill_little
=
266 floatformat_little
, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
267 floatformat_intbit_yes
,
268 "floatformat_ia64_spill_little",
269 floatformat_always_valid
,
272 const struct floatformat floatformat_ia64_quad_big
=
274 floatformat_big
, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
275 floatformat_intbit_no
,
276 "floatformat_ia64_quad_big",
277 floatformat_always_valid
,
280 const struct floatformat floatformat_ia64_quad_little
=
282 floatformat_little
, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
283 floatformat_intbit_no
,
284 "floatformat_ia64_quad_little",
285 floatformat_always_valid
,
290 floatformat_ibm_long_double_is_valid (const struct floatformat
*fmt
,
293 const unsigned char *ufrom
= (const unsigned char *) from
;
294 const struct floatformat
*hfmt
= fmt
->split_half
;
295 long top_exp
, bot_exp
;
298 top_exp
= get_field (ufrom
, hfmt
->byteorder
, hfmt
->totalsize
,
299 hfmt
->exp_start
, hfmt
->exp_len
);
300 bot_exp
= get_field (ufrom
+ 8, hfmt
->byteorder
, hfmt
->totalsize
,
301 hfmt
->exp_start
, hfmt
->exp_len
);
303 if ((unsigned long) top_exp
== hfmt
->exp_nan
)
304 top_nan
= mant_bits_set (hfmt
, ufrom
);
306 /* A NaN is valid with any low part. */
310 /* An infinity, zero or denormal requires low part 0 (positive or
312 if ((unsigned long) top_exp
== hfmt
->exp_nan
|| top_exp
== 0)
317 return !mant_bits_set (hfmt
, ufrom
+ 8);
320 /* The top part is now a finite normal value. The long double value
321 is the sum of the two parts, and the top part must equal the
322 result of rounding the long double value to nearest double. Thus
323 the bottom part must be <= 0.5ulp of the top part in absolute
324 value, and if it is < 0.5ulp then the long double is definitely
326 if (bot_exp
< top_exp
- 53)
328 if (bot_exp
> top_exp
- 53 && bot_exp
!= 0)
332 /* The bottom part is 0 or denormal. Determine which, and if
333 denormal the first two set bits. */
334 int first_bit
= -1, second_bit
= -1, cur_bit
;
335 for (cur_bit
= 0; (unsigned int) cur_bit
< hfmt
->man_len
; cur_bit
++)
336 if (get_field (ufrom
+ 8, hfmt
->byteorder
, hfmt
->totalsize
,
337 hfmt
->man_start
+ cur_bit
, 1))
343 second_bit
= cur_bit
;
347 /* Bottom part 0 is OK. */
350 /* The real exponent of the bottom part is -first_bit. */
351 if (-first_bit
< top_exp
- 53)
353 if (-first_bit
> top_exp
- 53)
355 /* The bottom part is at least 0.5ulp of the top part. For this
356 to be OK, the bottom part must be exactly 0.5ulp (i.e. no
357 more bits set) and the top part must have last bit 0. */
358 if (second_bit
!= -1)
360 return !get_field (ufrom
, hfmt
->byteorder
, hfmt
->totalsize
,
361 hfmt
->man_start
+ hfmt
->man_len
- 1, 1);
365 /* The bottom part is at least 0.5ulp of the top part. For this
366 to be OK, it must be exactly 0.5ulp (i.e. no explicit bits
367 set) and the top part must have last bit 0. */
368 if (get_field (ufrom
, hfmt
->byteorder
, hfmt
->totalsize
,
369 hfmt
->man_start
+ hfmt
->man_len
- 1, 1))
371 return !mant_bits_set (hfmt
, ufrom
+ 8);
375 const struct floatformat floatformat_ibm_long_double_big
=
377 floatformat_big
, 128, 0, 1, 11, 1023, 2047, 12, 52,
378 floatformat_intbit_no
,
379 "floatformat_ibm_long_double_big",
380 floatformat_ibm_long_double_is_valid
,
381 &floatformat_ieee_double_big
384 const struct floatformat floatformat_ibm_long_double_little
=
386 floatformat_little
, 128, 0, 1, 11, 1023, 2047, 12, 52,
387 floatformat_intbit_no
,
388 "floatformat_ibm_long_double_little",
389 floatformat_ibm_long_double_is_valid
,
390 &floatformat_ieee_double_little
393 const struct floatformat floatformat_bfloat16_big
=
395 floatformat_big
, 16, 0, 1, 8, 127, 255, 9, 7,
396 floatformat_intbit_no
,
397 "floatformat_bfloat16_big",
398 floatformat_always_valid
,
402 const struct floatformat floatformat_bfloat16_little
=
404 floatformat_little
, 16, 0, 1, 8, 127, 255, 9, 7,
405 floatformat_intbit_no
,
406 "floatformat_bfloat16_little",
407 floatformat_always_valid
,
412 #define min(a, b) ((a) < (b) ? (a) : (b))
415 /* Return 1 if any bits are explicitly set in the mantissa of UFROM,
416 format FMT, 0 otherwise. */
418 mant_bits_set (const struct floatformat
*fmt
, const unsigned char *ufrom
)
420 unsigned int mant_bits
, mant_off
;
423 mant_off
= fmt
->man_start
;
424 mant_bits_left
= fmt
->man_len
;
425 while (mant_bits_left
> 0)
427 mant_bits
= min (mant_bits_left
, 32);
429 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
430 mant_off
, mant_bits
) != 0)
433 mant_off
+= mant_bits
;
434 mant_bits_left
-= mant_bits
;
439 /* Extract a field which starts at START and is LEN bits long. DATA and
440 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
442 get_field (const unsigned char *data
, enum floatformat_byteorders order
,
443 unsigned int total_len
, unsigned int start
, unsigned int len
)
445 unsigned long result
= 0;
446 unsigned int cur_byte
;
447 int lo_bit
, hi_bit
, cur_bitshift
= 0;
448 int nextbyte
= (order
== floatformat_little
) ? 1 : -1;
450 /* Start is in big-endian bit order! Fix that first. */
451 start
= total_len
- (start
+ len
);
453 /* Start at the least significant part of the field. */
454 if (order
== floatformat_little
)
455 cur_byte
= start
/ FLOATFORMAT_CHAR_BIT
;
457 cur_byte
= (total_len
- start
- 1) / FLOATFORMAT_CHAR_BIT
;
459 lo_bit
= start
% FLOATFORMAT_CHAR_BIT
;
460 hi_bit
= min (lo_bit
+ len
, FLOATFORMAT_CHAR_BIT
);
464 unsigned int shifted
= *(data
+ cur_byte
) >> lo_bit
;
465 unsigned int bits
= hi_bit
- lo_bit
;
466 unsigned int mask
= (1 << bits
) - 1;
467 result
|= (shifted
& mask
) << cur_bitshift
;
469 cur_bitshift
+= bits
;
470 cur_byte
+= nextbyte
;
472 hi_bit
= min (len
, FLOATFORMAT_CHAR_BIT
);
479 /* Convert from FMT to a double.
480 FROM is the address of the extended float.
481 Store the double in *TO. */
484 floatformat_to_double (const struct floatformat
*fmt
,
485 const void *from
, double *to
)
487 const unsigned char *ufrom
= (const unsigned char *) from
;
491 unsigned int mant_bits
, mant_off
;
494 /* Split values are not handled specially, since the top half has
495 the correctly rounded double value (in the only supported case of
498 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
499 fmt
->exp_start
, fmt
->exp_len
);
501 /* If the exponent indicates a NaN, we don't have information to
502 decide what to do. So we handle it like IEEE, except that we
503 don't try to preserve the type of NaN. FIXME. */
504 if ((unsigned long) exponent
== fmt
->exp_nan
)
506 int nan
= mant_bits_set (fmt
, ufrom
);
508 /* On certain systems (such as GNU/Linux), the use of the
509 INFINITY macro below may generate a warning that cannot be
510 silenced due to a bug in GCC (PR preprocessor/11931). The
511 preprocessor fails to recognise the __extension__ keyword in
512 conjunction with the GNU/C99 extension for hexadecimal
513 floating point constants and will issue a warning when
514 compiling with -pedantic. */
520 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
528 mant_bits_left
= fmt
->man_len
;
529 mant_off
= fmt
->man_start
;
532 /* Build the result algebraically. Might go infinite, underflow, etc;
535 /* For denorms use minimum exponent. */
537 exponent
= 1 - fmt
->exp_bias
;
540 exponent
-= fmt
->exp_bias
;
542 /* If this format uses a hidden bit, explicitly add it in now.
543 Otherwise, increment the exponent by one to account for the
546 if (fmt
->intbit
== floatformat_intbit_no
)
547 dto
= ldexp (1.0, exponent
);
552 while (mant_bits_left
> 0)
554 mant_bits
= min (mant_bits_left
, 32);
556 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
557 mant_off
, mant_bits
);
559 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
560 exponent
-= mant_bits
;
561 mant_off
+= mant_bits
;
562 mant_bits_left
-= mant_bits
;
565 /* Negate it if negative. */
566 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
571 static void put_field (unsigned char *, enum floatformat_byteorders
,
577 /* Set a field which starts at START and is LEN bits long. DATA and
578 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
580 put_field (unsigned char *data
, enum floatformat_byteorders order
,
581 unsigned int total_len
, unsigned int start
, unsigned int len
,
582 unsigned long stuff_to_put
)
584 unsigned int cur_byte
;
586 int nextbyte
= (order
== floatformat_little
) ? 1 : -1;
588 /* Start is in big-endian bit order! Fix that first. */
589 start
= total_len
- (start
+ len
);
591 /* Start at the least significant part of the field. */
592 if (order
== floatformat_little
)
593 cur_byte
= start
/ FLOATFORMAT_CHAR_BIT
;
595 cur_byte
= (total_len
- start
- 1) / FLOATFORMAT_CHAR_BIT
;
597 lo_bit
= start
% FLOATFORMAT_CHAR_BIT
;
598 hi_bit
= min (lo_bit
+ len
, FLOATFORMAT_CHAR_BIT
);
602 unsigned char *byte_ptr
= data
+ cur_byte
;
603 unsigned int bits
= hi_bit
- lo_bit
;
604 unsigned int mask
= ((1 << bits
) - 1) << lo_bit
;
605 *byte_ptr
= (*byte_ptr
& ~mask
) | ((stuff_to_put
<< lo_bit
) & mask
);
606 stuff_to_put
>>= bits
;
608 cur_byte
+= nextbyte
;
610 hi_bit
= min (len
, FLOATFORMAT_CHAR_BIT
);
615 /* The converse: convert the double *FROM to an extended float
616 and store where TO points. Neither FROM nor TO have any alignment
620 floatformat_from_double (const struct floatformat
*fmt
,
621 const double *from
, void *to
)
626 unsigned int mant_bits
, mant_off
;
628 unsigned char *uto
= (unsigned char *) to
;
631 memset (uto
, 0, fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
);
633 /* Split values are not handled specially, since a bottom half of
634 zero is correct for any value representable as double (in the
635 only supported case of split values). */
637 /* If negative, set the sign bit. */
640 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
653 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
654 fmt
->exp_len
, fmt
->exp_nan
);
655 /* Be sure it's not infinity, but NaN value is irrelevant. */
656 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
661 if (dfrom
+ dfrom
== dfrom
)
663 /* This can only happen for an infinite value (or zero, which we
664 already handled above). */
665 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
666 fmt
->exp_len
, fmt
->exp_nan
);
670 mant
= frexp (dfrom
, &exponent
);
671 if (exponent
+ fmt
->exp_bias
- 1 > 0)
672 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
673 fmt
->exp_len
, exponent
+ fmt
->exp_bias
- 1);
676 /* Handle a denormalized number. FIXME: What should we do for
678 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
680 mant
= ldexp (mant
, exponent
+ fmt
->exp_bias
- 1);
683 mant_bits_left
= fmt
->man_len
;
684 mant_off
= fmt
->man_start
;
685 while (mant_bits_left
> 0)
687 unsigned long mant_long
;
688 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
690 mant
*= 4294967296.0;
691 mant_long
= (unsigned long)mant
;
694 /* If the integer bit is implicit, and we are not creating a
695 denormalized number, then we need to discard it. */
696 if ((unsigned int) mant_bits_left
== fmt
->man_len
697 && fmt
->intbit
== floatformat_intbit_no
698 && exponent
+ fmt
->exp_bias
- 1 > 0)
700 mant_long
&= 0x7fffffff;
703 else if (mant_bits
< 32)
705 /* The bits we want are in the most significant MANT_BITS bits of
706 mant_long. Move them to the least significant. */
707 mant_long
>>= 32 - mant_bits
;
710 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
711 mant_off
, mant_bits
, mant_long
);
712 mant_off
+= mant_bits
;
713 mant_bits_left
-= mant_bits
;
717 /* Return non-zero iff the data at FROM is a valid number in format FMT. */
720 floatformat_is_valid (const struct floatformat
*fmt
, const void *from
)
722 return fmt
->is_valid (fmt
, from
);
730 /* This is to be run on a host which uses IEEE floating point. */
737 floatformat_to_double (&floatformat_ieee_double_little
, &n
, &result
);
738 if ((n
!= result
&& (! isnan (n
) || ! isnan (result
)))
739 || (n
< 0 && result
>= 0)
740 || (n
>= 0 && result
< 0))
741 printf ("Differ(to): %.20g -> %.20g\n", n
, result
);
743 floatformat_from_double (&floatformat_ieee_double_little
, &n
, &result
);
744 if ((n
!= result
&& (! isnan (n
) || ! isnan (result
)))
745 || (n
< 0 && result
>= 0)
746 || (n
>= 0 && result
< 0))
747 printf ("Differ(from): %.20g -> %.20g\n", n
, result
);
753 floatformat_from_double (&floatformat_m68881_ext
, &n
, exten
);
754 floatformat_to_double (&floatformat_m68881_ext
, exten
, &result
);
756 printf ("Differ(to+from): %.20g -> %.20g\n", n
, result
);
761 /* This is to be run on a host which uses 68881 format. */
763 long double ex
= *(long double *)exten
;
765 printf ("Differ(from vs. extended): %.20g\n", n
);
778 ieee_test (234235.78907234);
780 ieee_test (-0.004321);
782 ieee_test (1.2E-316);
783 ieee_test (4.9406564584124654E-324);
784 ieee_test (- 4.9406564584124654E-324);
786 ieee_test (- INFINITY
);
788 ieee_test (INFINITY
);