1 /* Floating point routines for GDB, the GNU debugger.
3 Copyright (C) 2017 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 3 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, see <http://www.gnu.org/licenses/>. */
22 #include "floatformat.h"
23 #include "target-float.h"
26 /* Helper routines operating on binary floating-point data. */
30 #if (defined HAVE_LONG_DOUBLE && defined PRINTF_HAS_LONG_DOUBLE \
31 && defined SCANF_HAS_LONG_DOUBLE)
32 typedef long double DOUBLEST
;
34 typedef double DOUBLEST
;
35 /* If we can't scan or print long double, we don't want to use it
37 # undef HAVE_LONG_DOUBLE
38 # undef PRINTF_HAS_LONG_DOUBLE
39 # undef SCANF_HAS_LONG_DOUBLE
42 /* Different kinds of floatformat numbers recognized by
43 floatformat_classify. To avoid portability issues, we use local
44 values instead of the C99 macros (FP_NAN et cetera). */
53 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
54 going to bother with trying to muck around with whether it is defined in
55 a system header, what we do if not, etc. */
56 #define FLOATFORMAT_CHAR_BIT 8
58 /* The number of bytes that the largest floating-point type that we
59 can convert to doublest will need. */
60 #define FLOATFORMAT_LARGEST_BYTES 16
62 /* Return the floatformat's total size in host bytes. */
64 floatformat_totalsize_bytes (const struct floatformat
*fmt
)
66 return ((fmt
->totalsize
+ FLOATFORMAT_CHAR_BIT
- 1)
67 / FLOATFORMAT_CHAR_BIT
);
70 /* Return the precision of the floating point format FMT. */
72 floatformat_precision (const struct floatformat
*fmt
)
74 /* Assume the precision of and IBM long double is twice the precision
75 of the underlying double. This matches what GCC does. */
77 return 2 * floatformat_precision (fmt
->split_half
);
79 /* Otherwise, the precision is the size of mantissa in bits,
80 including the implicit bit if present. */
81 int prec
= fmt
->man_len
;
82 if (fmt
->intbit
== floatformat_intbit_no
)
88 /* Normalize the byte order of FROM into TO. If no normalization is
89 needed then FMT->byteorder is returned and TO is not changed;
90 otherwise the format of the normalized form in TO is returned. */
91 static enum floatformat_byteorders
92 floatformat_normalize_byteorder (const struct floatformat
*fmt
,
93 const void *from
, void *to
)
95 const unsigned char *swapin
;
96 unsigned char *swapout
;
99 if (fmt
->byteorder
== floatformat_little
100 || fmt
->byteorder
== floatformat_big
)
101 return fmt
->byteorder
;
103 words
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
106 swapout
= (unsigned char *)to
;
107 swapin
= (const unsigned char *)from
;
109 if (fmt
->byteorder
== floatformat_vax
)
113 *swapout
++ = swapin
[1];
114 *swapout
++ = swapin
[0];
115 *swapout
++ = swapin
[3];
116 *swapout
++ = swapin
[2];
119 /* This may look weird, since VAX is little-endian, but it is
120 easier to translate to big-endian than to little-endian. */
121 return floatformat_big
;
125 gdb_assert (fmt
->byteorder
== floatformat_littlebyte_bigword
);
129 *swapout
++ = swapin
[3];
130 *swapout
++ = swapin
[2];
131 *swapout
++ = swapin
[1];
132 *swapout
++ = swapin
[0];
135 return floatformat_big
;
139 /* Extract a field which starts at START and is LEN bytes long. DATA and
140 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
142 get_field (const bfd_byte
*data
, enum floatformat_byteorders order
,
143 unsigned int total_len
, unsigned int start
, unsigned int len
)
145 unsigned long result
;
146 unsigned int cur_byte
;
149 /* Caller must byte-swap words before calling this routine. */
150 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
152 /* Start at the least significant part of the field. */
153 if (order
== floatformat_little
)
155 /* We start counting from the other end (i.e, from the high bytes
156 rather than the low bytes). As such, we need to be concerned
157 with what happens if bit 0 doesn't start on a byte boundary.
158 I.e, we need to properly handle the case where total_len is
159 not evenly divisible by 8. So we compute ``excess'' which
160 represents the number of bits from the end of our starting
161 byte needed to get to bit 0. */
162 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
164 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
165 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
166 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
167 - FLOATFORMAT_CHAR_BIT
;
171 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
173 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
175 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
176 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
179 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
180 if (order
== floatformat_little
)
185 /* Move towards the most significant part of the field. */
186 while (cur_bitshift
< len
)
188 result
|= (unsigned long)*(data
+ cur_byte
) << cur_bitshift
;
189 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
192 case floatformat_little
:
195 case floatformat_big
:
200 if (len
< sizeof(result
) * FLOATFORMAT_CHAR_BIT
)
201 /* Mask out bits which are not part of the field. */
202 result
&= ((1UL << len
) - 1);
206 /* Set a field which starts at START and is LEN bytes long. DATA and
207 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
209 put_field (unsigned char *data
, enum floatformat_byteorders order
,
210 unsigned int total_len
, unsigned int start
, unsigned int len
,
211 unsigned long stuff_to_put
)
213 unsigned int cur_byte
;
216 /* Caller must byte-swap words before calling this routine. */
217 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
219 /* Start at the least significant part of the field. */
220 if (order
== floatformat_little
)
222 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
224 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
225 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
226 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
227 - FLOATFORMAT_CHAR_BIT
;
231 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
233 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
235 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
237 *(data
+ cur_byte
) &=
238 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1)
240 *(data
+ cur_byte
) |=
241 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
243 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
244 if (order
== floatformat_little
)
249 /* Move towards the most significant part of the field. */
250 while (cur_bitshift
< len
)
252 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
254 /* This is the last byte. */
255 *(data
+ cur_byte
) &=
256 ~((1 << (len
- cur_bitshift
)) - 1);
257 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
260 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
261 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
262 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
263 if (order
== floatformat_little
)
270 /* Check if VAL (which is assumed to be a floating point number whose
271 format is described by FMT) is negative. */
273 floatformat_is_negative (const struct floatformat
*fmt
,
274 const bfd_byte
*uval
)
276 enum floatformat_byteorders order
;
277 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
279 gdb_assert (fmt
!= NULL
);
280 gdb_assert (fmt
->totalsize
281 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
283 /* An IBM long double (a two element array of double) always takes the
284 sign of the first double. */
286 fmt
= fmt
->split_half
;
288 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
290 if (order
!= fmt
->byteorder
)
293 return get_field (uval
, order
, fmt
->totalsize
, fmt
->sign_start
, 1);
296 /* Check if VAL is "not a number" (NaN) for FMT. */
297 static enum float_kind
298 floatformat_classify (const struct floatformat
*fmt
,
299 const bfd_byte
*uval
)
303 unsigned int mant_bits
, mant_off
;
305 enum floatformat_byteorders order
;
306 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
309 gdb_assert (fmt
!= NULL
);
310 gdb_assert (fmt
->totalsize
311 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
313 /* An IBM long double (a two element array of double) can be classified
314 by looking at the first double. inf and nan are specified as
315 ignoring the second double. zero and subnormal will always have
316 the second double 0.0 if the long double is correctly rounded. */
318 fmt
= fmt
->split_half
;
320 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
322 if (order
!= fmt
->byteorder
)
325 exponent
= get_field (uval
, order
, fmt
->totalsize
, fmt
->exp_start
,
328 mant_bits_left
= fmt
->man_len
;
329 mant_off
= fmt
->man_start
;
332 while (mant_bits_left
> 0)
334 mant_bits
= std::min (mant_bits_left
, 32);
336 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
338 /* If there is an explicit integer bit, mask it off. */
339 if (mant_off
== fmt
->man_start
340 && fmt
->intbit
== floatformat_intbit_yes
)
341 mant
&= ~(1 << (mant_bits
- 1));
349 mant_off
+= mant_bits
;
350 mant_bits_left
-= mant_bits
;
353 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
368 return float_subnormal
;
371 if (exponent
== fmt
->exp_nan
)
374 return float_infinite
;
382 /* Convert the mantissa of VAL (which is assumed to be a floating
383 point number whose format is described by FMT) into a hexadecimal
384 and store it in a static string. Return a pointer to that string. */
386 floatformat_mantissa (const struct floatformat
*fmt
,
389 unsigned char *uval
= (unsigned char *) val
;
391 unsigned int mant_bits
, mant_off
;
396 enum floatformat_byteorders order
;
397 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
399 gdb_assert (fmt
!= NULL
);
400 gdb_assert (fmt
->totalsize
401 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
403 /* For IBM long double (a two element array of double), return the
404 mantissa of the first double. The problem with returning the
405 actual mantissa from both doubles is that there can be an
406 arbitrary number of implied 0's or 1's between the mantissas
407 of the first and second double. In any case, this function
408 is only used for dumping out nans, and a nan is specified to
409 ignore the value in the second double. */
411 fmt
= fmt
->split_half
;
413 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
415 if (order
!= fmt
->byteorder
)
421 /* Make sure we have enough room to store the mantissa. */
422 gdb_assert (sizeof res
> ((fmt
->man_len
+ 7) / 8) * 2);
424 mant_off
= fmt
->man_start
;
425 mant_bits_left
= fmt
->man_len
;
426 mant_bits
= (mant_bits_left
% 32) > 0 ? mant_bits_left
% 32 : 32;
428 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
430 len
= xsnprintf (res
, sizeof res
, "%lx", mant
);
432 mant_off
+= mant_bits
;
433 mant_bits_left
-= mant_bits
;
435 while (mant_bits_left
> 0)
437 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, 32);
439 xsnprintf (buf
, sizeof buf
, "%08lx", mant
);
440 gdb_assert (len
+ strlen (buf
) <= sizeof res
);
444 mant_bits_left
-= 32;
450 /* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
452 If the host and target formats agree, we just copy the raw data
453 into the appropriate type of variable and return, letting the host
454 increase precision as necessary. Otherwise, we call the conversion
455 routine and let it do the dirty work. Note that even if the target
456 and host floating-point formats match, the length of the types
457 might still be different, so the conversion routines must make sure
458 to not overrun any buffers. For example, on x86, long double is
459 the 80-bit extended precision type on both 32-bit and 64-bit ABIs,
460 but by default it is stored as 12 bytes on 32-bit, and 16 bytes on
461 64-bit, for alignment reasons. See comment in store_typed_floating
462 for a discussion about zeroing out remaining bytes in the target
465 static const struct floatformat
*host_float_format
= GDB_HOST_FLOAT_FORMAT
;
466 static const struct floatformat
*host_double_format
= GDB_HOST_DOUBLE_FORMAT
;
467 static const struct floatformat
*host_long_double_format
468 = GDB_HOST_LONG_DOUBLE_FORMAT
;
470 /* Convert from FMT to a DOUBLEST. FROM is the address of the extended float.
471 Store the DOUBLEST in *TO. */
473 floatformat_to_doublest (const struct floatformat
*fmt
,
474 const void *from
, DOUBLEST
*to
)
476 gdb_assert (fmt
!= NULL
);
478 if (fmt
== host_float_format
)
482 memcpy (&val
, from
, floatformat_totalsize_bytes (fmt
));
486 else if (fmt
== host_double_format
)
490 memcpy (&val
, from
, floatformat_totalsize_bytes (fmt
));
494 else if (fmt
== host_long_double_format
)
498 memcpy (&val
, from
, floatformat_totalsize_bytes (fmt
));
503 unsigned char *ufrom
= (unsigned char *) from
;
507 unsigned int mant_bits
, mant_off
;
509 int special_exponent
; /* It's a NaN, denorm or zero. */
510 enum floatformat_byteorders order
;
511 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
512 enum float_kind kind
;
514 gdb_assert (fmt
->totalsize
515 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
517 /* For non-numbers, reuse libiberty's logic to find the correct
518 format. We do not lose any precision in this case by passing
520 kind
= floatformat_classify (fmt
, (const bfd_byte
*) from
);
521 if (kind
== float_infinite
|| kind
== float_nan
)
525 floatformat_to_double (fmt
->split_half
? fmt
->split_half
: fmt
,
527 *to
= (DOUBLEST
) dto
;
531 order
= floatformat_normalize_byteorder (fmt
, ufrom
, newfrom
);
533 if (order
!= fmt
->byteorder
)
540 floatformat_to_doublest (fmt
->split_half
, ufrom
, &dtop
);
541 /* Preserve the sign of 0, which is the sign of the top
548 floatformat_to_doublest (fmt
->split_half
,
549 ufrom
+ fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
/ 2,
555 exponent
= get_field (ufrom
, order
, fmt
->totalsize
, fmt
->exp_start
,
557 /* Note that if exponent indicates a NaN, we can't really do anything useful
558 (not knowing if the host has NaN's, or how to build one). So it will
559 end up as an infinity or something close; that is OK. */
561 mant_bits_left
= fmt
->man_len
;
562 mant_off
= fmt
->man_start
;
565 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
567 /* Don't bias NaNs. Use minimum exponent for denorms. For
568 simplicity, we don't check for zero as the exponent doesn't matter.
569 Note the cast to int; exp_bias is unsigned, so it's important to
570 make sure the operation is done in signed arithmetic. */
571 if (!special_exponent
)
572 exponent
-= fmt
->exp_bias
;
573 else if (exponent
== 0)
574 exponent
= 1 - fmt
->exp_bias
;
576 /* Build the result algebraically. Might go infinite, underflow, etc;
579 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
580 increment the exponent by one to account for the integer bit. */
582 if (!special_exponent
)
584 if (fmt
->intbit
== floatformat_intbit_no
)
585 dto
= ldexp (1.0, exponent
);
590 while (mant_bits_left
> 0)
592 mant_bits
= std::min (mant_bits_left
, 32);
594 mant
= get_field (ufrom
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
596 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
597 exponent
-= mant_bits
;
598 mant_off
+= mant_bits
;
599 mant_bits_left
-= mant_bits
;
602 /* Negate it if negative. */
603 if (get_field (ufrom
, order
, fmt
->totalsize
, fmt
->sign_start
, 1))
608 /* Convert the DOUBLEST *FROM to an extended float in format FMT and
609 store where TO points. */
611 floatformat_from_doublest (const struct floatformat
*fmt
,
612 const DOUBLEST
*from
, void *to
)
614 gdb_assert (fmt
!= NULL
);
616 if (fmt
== host_float_format
)
620 memcpy (to
, &val
, floatformat_totalsize_bytes (fmt
));
623 else if (fmt
== host_double_format
)
627 memcpy (to
, &val
, floatformat_totalsize_bytes (fmt
));
630 else if (fmt
== host_long_double_format
)
632 long double val
= *from
;
634 memcpy (to
, &val
, floatformat_totalsize_bytes (fmt
));
641 unsigned int mant_bits
, mant_off
;
643 unsigned char *uto
= (unsigned char *) to
;
644 enum floatformat_byteorders order
= fmt
->byteorder
;
645 unsigned char newto
[FLOATFORMAT_LARGEST_BYTES
];
647 if (order
!= floatformat_little
)
648 order
= floatformat_big
;
650 if (order
!= fmt
->byteorder
)
653 memcpy (&dfrom
, from
, sizeof (dfrom
));
654 memset (uto
, 0, floatformat_totalsize_bytes (fmt
));
658 /* Use static volatile to ensure that any excess precision is
659 removed via storing in memory, and so the top half really is
660 the result of converting to double. */
661 static volatile double dtop
, dbot
;
662 DOUBLEST dtopnv
, dbotnv
;
664 dtop
= (double) dfrom
;
665 /* If the rounded top half is Inf, the bottom must be 0 not NaN
667 if (dtop
+ dtop
== dtop
&& dtop
!= 0.0)
670 dbot
= (double) (dfrom
- (DOUBLEST
) dtop
);
673 floatformat_from_doublest (fmt
->split_half
, &dtopnv
, uto
);
674 floatformat_from_doublest (fmt
->split_half
, &dbotnv
,
676 + fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
/ 2));
681 goto finalize_byteorder
; /* Result is zero */
682 if (dfrom
!= dfrom
) /* Result is NaN */
685 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
686 fmt
->exp_len
, fmt
->exp_nan
);
687 /* Be sure it's not infinity, but NaN value is irrel. */
688 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
690 goto finalize_byteorder
;
693 /* If negative, set the sign bit. */
696 put_field (uto
, order
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
700 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity. */
702 /* Infinity exponent is same as NaN's. */
703 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
704 fmt
->exp_len
, fmt
->exp_nan
);
705 /* Infinity mantissa is all zeroes. */
706 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
708 goto finalize_byteorder
;
711 #ifdef HAVE_LONG_DOUBLE
712 mant
= frexpl (dfrom
, &exponent
);
714 mant
= frexp (dfrom
, &exponent
);
717 if (exponent
+ fmt
->exp_bias
<= 0)
719 /* The value is too small to be expressed in the destination
720 type (not enough bits in the exponent. Treat as 0. */
721 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
723 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
725 goto finalize_byteorder
;
728 if (exponent
+ fmt
->exp_bias
>= (1 << fmt
->exp_len
))
730 /* The value is too large to fit into the destination.
731 Treat as infinity. */
732 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
733 fmt
->exp_len
, fmt
->exp_nan
);
734 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
736 goto finalize_byteorder
;
739 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
740 exponent
+ fmt
->exp_bias
- 1);
742 mant_bits_left
= fmt
->man_len
;
743 mant_off
= fmt
->man_start
;
744 while (mant_bits_left
> 0)
746 unsigned long mant_long
;
748 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
750 mant
*= 4294967296.0;
751 mant_long
= ((unsigned long) mant
) & 0xffffffffL
;
754 /* If the integer bit is implicit, then we need to discard it.
755 If we are discarding a zero, we should be (but are not) creating
756 a denormalized number which means adjusting the exponent
758 if (mant_bits_left
== fmt
->man_len
759 && fmt
->intbit
== floatformat_intbit_no
)
762 mant_long
&= 0xffffffffL
;
763 /* If we are processing the top 32 mantissa bits of a doublest
764 so as to convert to a float value with implied integer bit,
765 we will only be putting 31 of those 32 bits into the
766 final value due to the discarding of the top bit. In the
767 case of a small float value where the number of mantissa
768 bits is less than 32, discarding the top bit does not alter
769 the number of bits we will be adding to the result. */
776 /* The bits we want are in the most significant MANT_BITS bits of
777 mant_long. Move them to the least significant. */
778 mant_long
>>= 32 - mant_bits
;
781 put_field (uto
, order
, fmt
->totalsize
,
782 mant_off
, mant_bits
, mant_long
);
783 mant_off
+= mant_bits
;
784 mant_bits_left
-= mant_bits
;
788 /* Do we need to byte-swap the words in the result? */
789 if (order
!= fmt
->byteorder
)
790 floatformat_normalize_byteorder (fmt
, newto
, to
);
793 /* Convert the byte-stream ADDR, interpreted as floating-point format FMT,
794 to a string, optionally using the print format FORMAT. */
796 floatformat_to_string (const struct floatformat
*fmt
,
797 const gdb_byte
*in
, const char *format
)
799 /* Unless we need to adhere to a specific format, provide special
800 output for certain cases. */
801 if (format
== nullptr)
803 /* Detect invalid representations. */
804 if (!floatformat_is_valid (fmt
, in
))
805 return "<invalid float value>";
807 /* Handle NaN and Inf. */
808 enum float_kind kind
= floatformat_classify (fmt
, in
);
809 if (kind
== float_nan
)
811 const char *sign
= floatformat_is_negative (fmt
, in
)? "-" : "";
812 const char *mantissa
= floatformat_mantissa (fmt
, in
);
813 return string_printf ("%snan(0x%s)", sign
, mantissa
);
815 else if (kind
== float_infinite
)
817 const char *sign
= floatformat_is_negative (fmt
, in
)? "-" : "";
818 return string_printf ("%sinf", sign
);
822 /* Determine the format string to use on the host side. */
823 std::string host_format
;
826 if (format
== nullptr)
828 /* If no format was specified, print the number using a format string
829 where the precision is set to the DECIMAL_DIG value for the given
830 floating-point format. This value is computed as
832 ceil(1 + p * log10(b)),
834 where p is the precision of the floating-point format in bits, and
835 b is the base (which is always 2 for the formats we support). */
836 const double log10_2
= .30102999566398119521;
837 double d_decimal_dig
= 1 + floatformat_precision (fmt
) * log10_2
;
838 int decimal_dig
= d_decimal_dig
;
839 if (decimal_dig
< d_decimal_dig
)
842 host_format
= string_printf ("%%.%d", decimal_dig
);
847 /* Use the specified format, stripping out the conversion character
848 and length modifier, if present. */
849 size_t len
= strlen (format
);
850 gdb_assert (len
> 1);
851 conversion
= format
[--len
];
852 gdb_assert (conversion
== 'e' || conversion
== 'f' || conversion
== 'g'
853 || conversion
== 'E' || conversion
== 'G');
854 if (format
[len
- 1] == 'L')
857 host_format
= std::string (format
, len
);
860 /* Add the length modifier and conversion character appropriate for
861 handling the host DOUBLEST type. */
862 #ifdef HAVE_LONG_DOUBLE
865 host_format
+= conversion
;
868 floatformat_to_doublest (fmt
, in
, &doub
);
869 return string_printf (host_format
.c_str (), doub
);
872 /* Parse string STRING into a target floating-number of format FMT and
873 store it as byte-stream ADDR. Return whether parsing succeeded. */
875 floatformat_from_string (const struct floatformat
*fmt
, gdb_byte
*out
,
876 const std::string
&in
)
880 #ifdef HAVE_LONG_DOUBLE
881 const char *scan_format
= "%Lg%n";
883 const char *scan_format
= "%lg%n";
885 num
= sscanf (in
.c_str (), scan_format
, &doub
, &n
);
887 /* The sscanf man page suggests not making any assumptions on the effect
888 of %n on the result, so we don't.
889 That is why we simply test num == 0. */
893 /* We only accept the whole string. */
897 floatformat_from_doublest (fmt
, &doub
, out
);
901 /* Convert the byte-stream ADDR, interpreted as floating-point format FMT,
902 to an integer value (rounding towards zero). */
904 floatformat_to_longest (const struct floatformat
*fmt
, const gdb_byte
*addr
)
907 floatformat_to_doublest (fmt
, addr
, &d
);
911 /* Convert signed integer VAL to a target floating-number of format FMT
912 and store it as byte-stream ADDR. */
914 floatformat_from_longest (const struct floatformat
*fmt
, gdb_byte
*addr
,
917 DOUBLEST d
= (DOUBLEST
) val
;
918 floatformat_from_doublest (fmt
, &d
, addr
);
921 /* Convert unsigned integer VAL to a target floating-number of format FMT
922 and store it as byte-stream ADDR. */
924 floatformat_from_ulongest (const struct floatformat
*fmt
, gdb_byte
*addr
,
927 DOUBLEST d
= (DOUBLEST
) val
;
928 floatformat_from_doublest (fmt
, &d
, addr
);
931 /* Convert the byte-stream ADDR, interpreted as floating-point format FMT,
932 to a floating-point value in the host "double" format. */
934 floatformat_to_host_double (const struct floatformat
*fmt
,
935 const gdb_byte
*addr
)
938 floatformat_to_doublest (fmt
, addr
, &d
);
942 /* Convert floating-point value VAL in the host "double" format to a target
943 floating-number of format FMT and store it as byte-stream ADDR. */
945 floatformat_from_host_double (const struct floatformat
*fmt
, gdb_byte
*addr
,
948 DOUBLEST d
= (DOUBLEST
) val
;
949 floatformat_from_doublest (fmt
, &d
, addr
);
952 /* Convert a floating-point number of format FROM_FMT from the target
953 byte-stream FROM to a floating-point number of format TO_FMT, and
954 store it to the target byte-stream TO. */
956 floatformat_convert (const gdb_byte
*from
, const struct floatformat
*from_fmt
,
957 gdb_byte
*to
, const struct floatformat
*to_fmt
)
959 if (from_fmt
== to_fmt
)
961 /* The floating-point formats match, so we simply copy the data. */
962 memcpy (to
, from
, floatformat_totalsize_bytes (to_fmt
));
966 /* The floating-point formats don't match. The best we can do
967 (apart from simulating the target FPU) is converting to the
968 widest floating-point type supported by the host, and then
969 again to the desired type. */
972 floatformat_to_doublest (from_fmt
, from
, &d
);
973 floatformat_from_doublest (to_fmt
, &d
, to
);
977 /* Perform the binary operation indicated by OPCODE, using as operands the
978 target byte streams X and Y, interpreted as floating-point numbers of
979 formats FMT_X and FMT_Y, respectively. Convert the result to format
980 FMT_RES and store it into the byte-stream RES. */
982 floatformat_binop (enum exp_opcode op
,
983 const struct floatformat
*fmt_x
, const gdb_byte
*x
,
984 const struct floatformat
*fmt_y
, const gdb_byte
*y
,
985 const struct floatformat
*fmt_result
, gdb_byte
*result
)
987 DOUBLEST v1
, v2
, v
= 0;
989 floatformat_to_doublest (fmt_x
, x
, &v1
);
990 floatformat_to_doublest (fmt_y
, y
, &v2
);
1014 error (_("Cannot perform exponentiation: %s"),
1015 safe_strerror (errno
));
1019 v
= v1
< v2
? v1
: v2
;
1023 v
= v1
> v2
? v1
: v2
;
1027 error (_("Integer-only operation on floating point number."));
1031 floatformat_from_doublest (fmt_result
, &v
, result
);
1034 /* Compare the two target byte streams X and Y, interpreted as floating-point
1035 numbers of formats FMT_X and FMT_Y, respectively. Return zero if X and Y
1036 are equal, -1 if X is less than Y, and 1 otherwise. */
1038 floatformat_compare (const struct floatformat
*fmt_x
, const gdb_byte
*x
,
1039 const struct floatformat
*fmt_y
, const gdb_byte
*y
)
1043 floatformat_to_doublest (fmt_x
, x
, &v1
);
1044 floatformat_to_doublest (fmt_y
, y
, &v2
);
1054 /* Helper routines operating on decimal floating-point data. */
1056 /* Decimal floating point is one of the extension to IEEE 754, which is
1057 described in http://grouper.ieee.org/groups/754/revision.html and
1058 http://www2.hursley.ibm.com/decimal/. It completes binary floating
1059 point by representing floating point more exactly. */
1061 /* The order of the following headers is important for making sure
1062 decNumber structure is large enough to hold decimal128 digits. */
1064 #include "dpd/decimal128.h"
1065 #include "dpd/decimal64.h"
1066 #include "dpd/decimal32.h"
1068 /* When using decimal128, this is the maximum string length + 1
1069 (value comes from libdecnumber's DECIMAL128_String constant). */
1070 #define MAX_DECIMAL_STRING 43
1072 /* In GDB, we are using an array of gdb_byte to represent decimal values.
1073 They are stored in host byte order. This routine does the conversion if
1074 the target byte order is different. */
1076 match_endianness (const gdb_byte
*from
, int len
, enum bfd_endian byte_order
,
1082 #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE
1084 #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
1087 if (byte_order
== OPPOSITE_BYTE_ORDER
)
1088 for (i
= 0; i
< len
; i
++)
1089 to
[i
] = from
[len
- i
- 1];
1091 for (i
= 0; i
< len
; i
++)
1097 /* Helper function to get the appropriate libdecnumber context for each size
1098 of decimal float. */
1100 set_decnumber_context (decContext
*ctx
, int len
)
1105 decContextDefault (ctx
, DEC_INIT_DECIMAL32
);
1108 decContextDefault (ctx
, DEC_INIT_DECIMAL64
);
1111 decContextDefault (ctx
, DEC_INIT_DECIMAL128
);
1118 /* Check for errors signaled in the decimal context structure. */
1120 decimal_check_errors (decContext
*ctx
)
1122 /* An error here could be a division by zero, an overflow, an underflow or
1123 an invalid operation (from the DEC_Errors constant in decContext.h).
1124 Since GDB doesn't complain about division by zero, overflow or underflow
1125 errors for binary floating, we won't complain about them for decimal
1127 if (ctx
->status
& DEC_IEEE_854_Invalid_operation
)
1129 /* Leave only the error bits in the status flags. */
1130 ctx
->status
&= DEC_IEEE_854_Invalid_operation
;
1131 error (_("Cannot perform operation: %s"),
1132 decContextStatusToString (ctx
));
1136 /* Helper function to convert from libdecnumber's appropriate representation
1137 for computation to each size of decimal float. */
1139 decimal_from_number (const decNumber
*from
,
1140 gdb_byte
*to
, int len
, enum bfd_endian byte_order
)
1146 set_decnumber_context (&set
, len
);
1151 decimal32FromNumber ((decimal32
*) dec
, from
, &set
);
1154 decimal64FromNumber ((decimal64
*) dec
, from
, &set
);
1157 decimal128FromNumber ((decimal128
*) dec
, from
, &set
);
1160 error (_("Unknown decimal floating point type."));
1164 match_endianness (dec
, len
, byte_order
, to
);
1167 /* Helper function to convert each size of decimal float to libdecnumber's
1168 appropriate representation for computation. */
1170 decimal_to_number (const gdb_byte
*from
, int len
, enum bfd_endian byte_order
,
1174 match_endianness (from
, len
, byte_order
, dec
);
1179 decimal32ToNumber ((decimal32
*) dec
, to
);
1182 decimal64ToNumber ((decimal64
*) dec
, to
);
1185 decimal128ToNumber ((decimal128
*) dec
, to
);
1188 error (_("Unknown decimal floating point type."));
1193 /* Convert decimal type to its string representation. LEN is the length
1194 of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
1195 16 bytes for decimal128. */
1197 decimal_to_string (const gdb_byte
*decbytes
, int len
,
1198 enum bfd_endian byte_order
, const char *format
= nullptr)
1202 match_endianness (decbytes
, len
, byte_order
, dec
);
1204 if (format
!= nullptr)
1206 /* We don't handle format strings (yet). If the host printf supports
1207 decimal floating point types, just use this. Otherwise, fall back
1208 to printing the number while ignoring the format string. */
1209 #if defined (PRINTF_HAS_DECFLOAT)
1210 /* FIXME: This makes unwarranted assumptions about the host ABI! */
1211 return string_printf (format
, dec
);
1216 result
.resize (MAX_DECIMAL_STRING
);
1221 decimal32ToString ((decimal32
*) dec
, &result
[0]);
1224 decimal64ToString ((decimal64
*) dec
, &result
[0]);
1227 decimal128ToString ((decimal128
*) dec
, &result
[0]);
1230 error (_("Unknown decimal floating point type."));
1237 /* Convert the string form of a decimal value to its decimal representation.
1238 LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
1239 decimal64 and 16 bytes for decimal128. */
1241 decimal_from_string (gdb_byte
*decbytes
, int len
, enum bfd_endian byte_order
,
1242 const std::string
&string
)
1247 set_decnumber_context (&set
, len
);
1252 decimal32FromString ((decimal32
*) dec
, string
.c_str (), &set
);
1255 decimal64FromString ((decimal64
*) dec
, string
.c_str (), &set
);
1258 decimal128FromString ((decimal128
*) dec
, string
.c_str (), &set
);
1261 error (_("Unknown decimal floating point type."));
1265 match_endianness (dec
, len
, byte_order
, decbytes
);
1267 /* Check for errors in the DFP operation. */
1268 decimal_check_errors (&set
);
1273 /* Converts a LONGEST to a decimal float of specified LEN bytes. */
1275 decimal_from_longest (LONGEST from
,
1276 gdb_byte
*to
, int len
, enum bfd_endian byte_order
)
1280 if ((int32_t) from
!= from
)
1281 /* libdecnumber can convert only 32-bit integers. */
1282 error (_("Conversion of large integer to a "
1283 "decimal floating type is not supported."));
1285 decNumberFromInt32 (&number
, (int32_t) from
);
1287 decimal_from_number (&number
, to
, len
, byte_order
);
1290 /* Converts a ULONGEST to a decimal float of specified LEN bytes. */
1292 decimal_from_ulongest (ULONGEST from
,
1293 gdb_byte
*to
, int len
, enum bfd_endian byte_order
)
1297 if ((uint32_t) from
!= from
)
1298 /* libdecnumber can convert only 32-bit integers. */
1299 error (_("Conversion of large integer to a "
1300 "decimal floating type is not supported."));
1302 decNumberFromUInt32 (&number
, (uint32_t) from
);
1304 decimal_from_number (&number
, to
, len
, byte_order
);
1307 /* Converts a decimal float of LEN bytes to a LONGEST. */
1309 decimal_to_longest (const gdb_byte
*from
, int len
, enum bfd_endian byte_order
)
1311 /* libdecnumber has a function to convert from decimal to integer, but
1312 it doesn't work when the decimal number has a fractional part. */
1313 std::string str
= decimal_to_string (from
, len
, byte_order
);
1314 return strtoll (str
.c_str (), NULL
, 10);
1317 /* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
1318 and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
1319 RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */
1321 decimal_binop (enum exp_opcode op
,
1322 const gdb_byte
*x
, int len_x
, enum bfd_endian byte_order_x
,
1323 const gdb_byte
*y
, int len_y
, enum bfd_endian byte_order_y
,
1324 gdb_byte
*result
, int len_result
,
1325 enum bfd_endian byte_order_result
)
1328 decNumber number1
, number2
, number3
;
1330 decimal_to_number (x
, len_x
, byte_order_x
, &number1
);
1331 decimal_to_number (y
, len_y
, byte_order_y
, &number2
);
1333 set_decnumber_context (&set
, len_result
);
1338 decNumberAdd (&number3
, &number1
, &number2
, &set
);
1341 decNumberSubtract (&number3
, &number1
, &number2
, &set
);
1344 decNumberMultiply (&number3
, &number1
, &number2
, &set
);
1347 decNumberDivide (&number3
, &number1
, &number2
, &set
);
1350 decNumberPower (&number3
, &number1
, &number2
, &set
);
1353 error (_("Operation not valid for decimal floating point number."));
1357 /* Check for errors in the DFP operation. */
1358 decimal_check_errors (&set
);
1360 decimal_from_number (&number3
, result
, len_result
, byte_order_result
);
1363 /* Returns true if X (which is LEN bytes wide) is the number zero. */
1365 decimal_is_zero (const gdb_byte
*x
, int len
, enum bfd_endian byte_order
)
1369 decimal_to_number (x
, len
, byte_order
, &number
);
1371 return decNumberIsZero (&number
);
1374 /* Compares two numbers numerically. If X is less than Y then the return value
1375 will be -1. If they are equal, then the return value will be 0. If X is
1376 greater than the Y then the return value will be 1. */
1378 decimal_compare (const gdb_byte
*x
, int len_x
, enum bfd_endian byte_order_x
,
1379 const gdb_byte
*y
, int len_y
, enum bfd_endian byte_order_y
)
1381 decNumber number1
, number2
, result
;
1385 decimal_to_number (x
, len_x
, byte_order_x
, &number1
);
1386 decimal_to_number (y
, len_y
, byte_order_y
, &number2
);
1388 /* Perform the comparison in the larger of the two sizes. */
1389 len_result
= len_x
> len_y
? len_x
: len_y
;
1390 set_decnumber_context (&set
, len_result
);
1392 decNumberCompare (&result
, &number1
, &number2
, &set
);
1394 /* Check for errors in the DFP operation. */
1395 decimal_check_errors (&set
);
1397 if (decNumberIsNaN (&result
))
1398 error (_("Comparison with an invalid number (NaN)."));
1399 else if (decNumberIsZero (&result
))
1401 else if (decNumberIsNegative (&result
))
1407 /* Convert a decimal value from a decimal type with LEN_FROM bytes to a
1408 decimal type with LEN_TO bytes. */
1410 decimal_convert (const gdb_byte
*from
, int len_from
,
1411 enum bfd_endian byte_order_from
, gdb_byte
*to
, int len_to
,
1412 enum bfd_endian byte_order_to
)
1416 decimal_to_number (from
, len_from
, byte_order_from
, &number
);
1417 decimal_from_number (&number
, to
, len_to
, byte_order_to
);
1421 /* Typed floating-point routines. These routines operate on floating-point
1422 values in target format, represented by a byte buffer interpreted as a
1423 "struct type", which may be either a binary or decimal floating-point
1424 type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */
1426 /* Return whether the byte-stream ADDR holds a valid value of
1427 floating-point type TYPE. */
1429 target_float_is_valid (const gdb_byte
*addr
, const struct type
*type
)
1431 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1432 return floatformat_is_valid (floatformat_from_type (type
), addr
);
1434 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1437 gdb_assert_not_reached ("unexpected type code");
1440 /* Return whether the byte-stream ADDR, interpreted as floating-point
1441 type TYPE, is numerically equal to zero (of either sign). */
1443 target_float_is_zero (const gdb_byte
*addr
, const struct type
*type
)
1445 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1446 return (floatformat_classify (floatformat_from_type (type
), addr
)
1449 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1450 return decimal_is_zero (addr
, TYPE_LENGTH (type
),
1451 gdbarch_byte_order (get_type_arch (type
)));
1453 gdb_assert_not_reached ("unexpected type code");
1456 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1457 to a string, optionally using the print format FORMAT. */
1459 target_float_to_string (const gdb_byte
*addr
, const struct type
*type
,
1462 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1463 return floatformat_to_string (floatformat_from_type (type
), addr
, format
);
1465 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1466 return decimal_to_string (addr
, TYPE_LENGTH (type
),
1467 gdbarch_byte_order (get_type_arch (type
)),
1470 gdb_assert_not_reached ("unexpected type code");
1473 /* Parse string STRING into a target floating-number of type TYPE and
1474 store it as byte-stream ADDR. Return whether parsing succeeded. */
1476 target_float_from_string (gdb_byte
*addr
, const struct type
*type
,
1477 const std::string
&string
)
1479 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1480 memset (addr
, 0, TYPE_LENGTH (type
));
1482 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1483 return floatformat_from_string (floatformat_from_type (type
), addr
,
1486 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1487 return decimal_from_string (addr
, TYPE_LENGTH (type
),
1488 gdbarch_byte_order (get_type_arch (type
)),
1491 gdb_assert_not_reached ("unexpected type code");
1494 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1495 to an integer value (rounding towards zero). */
1497 target_float_to_longest (const gdb_byte
*addr
, const struct type
*type
)
1499 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1500 return floatformat_to_longest (floatformat_from_type (type
), addr
);
1502 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1503 return decimal_to_longest (addr
, TYPE_LENGTH (type
),
1504 gdbarch_byte_order (get_type_arch (type
)));
1506 gdb_assert_not_reached ("unexpected type code");
1509 /* Convert signed integer VAL to a target floating-number of type TYPE
1510 and store it as byte-stream ADDR. */
1512 target_float_from_longest (gdb_byte
*addr
, const struct type
*type
,
1515 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1516 memset (addr
, 0, TYPE_LENGTH (type
));
1518 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1520 floatformat_from_longest (floatformat_from_type (type
), addr
, val
);
1524 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1526 decimal_from_longest (val
, addr
, TYPE_LENGTH (type
),
1527 gdbarch_byte_order (get_type_arch (type
)));
1531 gdb_assert_not_reached ("unexpected type code");
1534 /* Convert unsigned integer VAL to a target floating-number of type TYPE
1535 and store it as byte-stream ADDR. */
1537 target_float_from_ulongest (gdb_byte
*addr
, const struct type
*type
,
1540 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1541 memset (addr
, 0, TYPE_LENGTH (type
));
1543 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1545 floatformat_from_ulongest (floatformat_from_type (type
), addr
, val
);
1549 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1551 decimal_from_ulongest (val
, addr
, TYPE_LENGTH (type
),
1552 gdbarch_byte_order (get_type_arch (type
)));
1556 gdb_assert_not_reached ("unexpected type code");
1559 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1560 to a floating-point value in the host "double" format. */
1562 target_float_to_host_double (const gdb_byte
*addr
,
1563 const struct type
*type
)
1565 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1566 return floatformat_to_host_double (floatformat_from_type (type
), addr
);
1568 /* We don't support conversions between target decimal floating-point
1569 types and the host double type here. */
1571 gdb_assert_not_reached ("unexpected type code");
1574 /* Convert floating-point value VAL in the host "double" format to a target
1575 floating-number of type TYPE and store it as byte-stream ADDR. */
1577 target_float_from_host_double (gdb_byte
*addr
, const struct type
*type
,
1580 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1581 memset (addr
, 0, TYPE_LENGTH (type
));
1583 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1585 floatformat_from_host_double (floatformat_from_type (type
), addr
, val
);
1589 /* We don't support conversions between target decimal floating-point
1590 types and the host double type here. */
1592 gdb_assert_not_reached ("unexpected type code");
1595 /* Convert a floating-point number of type FROM_TYPE from the target
1596 byte-stream FROM to a floating-point number of type TO_TYPE, and
1597 store it to the target byte-stream TO. */
1599 target_float_convert (const gdb_byte
*from
, const struct type
*from_type
,
1600 gdb_byte
*to
, const struct type
*to_type
)
1602 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1603 memset (to
, 0, TYPE_LENGTH (to_type
));
1605 /* Use direct conversion routines if we have them. */
1607 if (TYPE_CODE (from_type
) == TYPE_CODE_FLT
1608 && TYPE_CODE (to_type
) == TYPE_CODE_FLT
)
1610 floatformat_convert (from
, floatformat_from_type (from_type
),
1611 to
, floatformat_from_type (to_type
));
1615 if (TYPE_CODE (from_type
) == TYPE_CODE_DECFLOAT
1616 && TYPE_CODE (to_type
) == TYPE_CODE_DECFLOAT
)
1618 decimal_convert (from
, TYPE_LENGTH (from_type
),
1619 gdbarch_byte_order (get_type_arch (from_type
)),
1620 to
, TYPE_LENGTH (to_type
),
1621 gdbarch_byte_order (get_type_arch (to_type
)));
1625 /* We cannot directly convert between binary and decimal floating-point
1626 types, so go via an intermediary string. */
1628 if ((TYPE_CODE (from_type
) == TYPE_CODE_FLT
1629 && TYPE_CODE (to_type
) == TYPE_CODE_DECFLOAT
)
1630 || (TYPE_CODE (from_type
) == TYPE_CODE_DECFLOAT
1631 && TYPE_CODE (to_type
) == TYPE_CODE_FLT
))
1633 std::string str
= target_float_to_string (from
, from_type
);
1634 target_float_from_string (to
, to_type
, str
);
1638 gdb_assert_not_reached ("unexpected type code");
1641 /* Perform the binary operation indicated by OPCODE, using as operands the
1642 target byte streams X and Y, interpreted as floating-point numbers of
1643 types TYPE_X and TYPE_Y, respectively. Convert the result to type
1644 TYPE_RES and store it into the byte-stream RES.
1646 The three types must either be all binary floating-point types, or else
1647 all decimal floating-point types. Binary and decimal floating-point
1648 types cannot be mixed within a single operation. */
1650 target_float_binop (enum exp_opcode opcode
,
1651 const gdb_byte
*x
, const struct type
*type_x
,
1652 const gdb_byte
*y
, const struct type
*type_y
,
1653 gdb_byte
*res
, const struct type
*type_res
)
1655 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1656 memset (res
, 0, TYPE_LENGTH (type_res
));
1658 if (TYPE_CODE (type_res
) == TYPE_CODE_FLT
)
1660 gdb_assert (TYPE_CODE (type_x
) == TYPE_CODE_FLT
);
1661 gdb_assert (TYPE_CODE (type_y
) == TYPE_CODE_FLT
);
1662 return floatformat_binop (opcode
,
1663 floatformat_from_type (type_x
), x
,
1664 floatformat_from_type (type_y
), y
,
1665 floatformat_from_type (type_res
), res
);
1668 if (TYPE_CODE (type_res
) == TYPE_CODE_DECFLOAT
)
1670 gdb_assert (TYPE_CODE (type_x
) == TYPE_CODE_DECFLOAT
);
1671 gdb_assert (TYPE_CODE (type_y
) == TYPE_CODE_DECFLOAT
);
1672 return decimal_binop (opcode
,
1673 x
, TYPE_LENGTH (type_x
),
1674 gdbarch_byte_order (get_type_arch (type_x
)),
1675 y
, TYPE_LENGTH (type_y
),
1676 gdbarch_byte_order (get_type_arch (type_y
)),
1677 res
, TYPE_LENGTH (type_res
),
1678 gdbarch_byte_order (get_type_arch (type_res
)));
1681 gdb_assert_not_reached ("unexpected type code");
1684 /* Compare the two target byte streams X and Y, interpreted as floating-point
1685 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
1686 are equal, -1 if X is less than Y, and 1 otherwise.
1688 The two types must either both be binary floating-point types, or else
1689 both be decimal floating-point types. Binary and decimal floating-point
1690 types cannot compared directly against each other. */
1692 target_float_compare (const gdb_byte
*x
, const struct type
*type_x
,
1693 const gdb_byte
*y
, const struct type
*type_y
)
1695 if (TYPE_CODE (type_x
) == TYPE_CODE_FLT
)
1697 gdb_assert (TYPE_CODE (type_y
) == TYPE_CODE_FLT
);
1698 return floatformat_compare (floatformat_from_type (type_x
), x
,
1699 floatformat_from_type (type_y
), y
);
1702 if (TYPE_CODE (type_x
) == TYPE_CODE_DECFLOAT
)
1704 gdb_assert (TYPE_CODE (type_y
) == TYPE_CODE_DECFLOAT
);
1705 return decimal_compare (x
, TYPE_LENGTH (type_x
),
1706 gdbarch_byte_order (get_type_arch (type_x
)),
1707 y
, TYPE_LENGTH (type_y
),
1708 gdbarch_byte_order (get_type_arch (type_y
)));
1711 gdb_assert_not_reached ("unexpected type code");