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
, gdb_byte
*to
, int len
)
1143 set_decnumber_context (&set
, len
);
1148 decimal32FromNumber ((decimal32
*) to
, from
, &set
);
1151 decimal64FromNumber ((decimal64
*) to
, from
, &set
);
1154 decimal128FromNumber ((decimal128
*) to
, from
, &set
);
1159 /* Helper function to convert each size of decimal float to libdecnumber's
1160 appropriate representation for computation. */
1162 decimal_to_number (const gdb_byte
*from
, int len
, decNumber
*to
)
1167 decimal32ToNumber ((decimal32
*) from
, to
);
1170 decimal64ToNumber ((decimal64
*) from
, to
);
1173 decimal128ToNumber ((decimal128
*) from
, to
);
1176 error (_("Unknown decimal floating point type."));
1181 /* Convert decimal type to its string representation. LEN is the length
1182 of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
1183 16 bytes for decimal128. */
1185 decimal_to_string (const gdb_byte
*decbytes
, int len
,
1186 enum bfd_endian byte_order
, const char *format
= nullptr)
1190 match_endianness (decbytes
, len
, byte_order
, dec
);
1192 if (format
!= nullptr)
1194 /* We don't handle format strings (yet). If the host printf supports
1195 decimal floating point types, just use this. Otherwise, fall back
1196 to printing the number while ignoring the format string. */
1197 #if defined (PRINTF_HAS_DECFLOAT)
1198 /* FIXME: This makes unwarranted assumptions about the host ABI! */
1199 return string_printf (format
, dec
);
1204 result
.resize (MAX_DECIMAL_STRING
);
1209 decimal32ToString ((decimal32
*) dec
, &result
[0]);
1212 decimal64ToString ((decimal64
*) dec
, &result
[0]);
1215 decimal128ToString ((decimal128
*) dec
, &result
[0]);
1218 error (_("Unknown decimal floating point type."));
1225 /* Convert the string form of a decimal value to its decimal representation.
1226 LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
1227 decimal64 and 16 bytes for decimal128. */
1229 decimal_from_string (gdb_byte
*decbytes
, int len
, enum bfd_endian byte_order
,
1230 const std::string
&string
)
1235 set_decnumber_context (&set
, len
);
1240 decimal32FromString ((decimal32
*) dec
, string
.c_str (), &set
);
1243 decimal64FromString ((decimal64
*) dec
, string
.c_str (), &set
);
1246 decimal128FromString ((decimal128
*) dec
, string
.c_str (), &set
);
1249 error (_("Unknown decimal floating point type."));
1253 match_endianness (dec
, len
, byte_order
, decbytes
);
1255 /* Check for errors in the DFP operation. */
1256 decimal_check_errors (&set
);
1261 /* Converts a LONGEST to a decimal float of specified LEN bytes. */
1263 decimal_from_longest (LONGEST from
,
1264 gdb_byte
*to
, int len
, enum bfd_endian byte_order
)
1268 if ((int32_t) from
!= from
)
1269 /* libdecnumber can convert only 32-bit integers. */
1270 error (_("Conversion of large integer to a "
1271 "decimal floating type is not supported."));
1273 decNumberFromInt32 (&number
, (int32_t) from
);
1275 decimal_from_number (&number
, dec
, len
);
1276 match_endianness (dec
, len
, byte_order
, to
);
1279 /* Converts a ULONGEST to a decimal float of specified LEN bytes. */
1281 decimal_from_ulongest (ULONGEST from
,
1282 gdb_byte
*to
, int len
, enum bfd_endian byte_order
)
1287 if ((uint32_t) from
!= from
)
1288 /* libdecnumber can convert only 32-bit integers. */
1289 error (_("Conversion of large integer to a "
1290 "decimal floating type is not supported."));
1292 decNumberFromUInt32 (&number
, (uint32_t) from
);
1294 decimal_from_number (&number
, dec
, len
);
1295 match_endianness (dec
, len
, byte_order
, to
);
1298 /* Converts a decimal float of LEN bytes to a LONGEST. */
1300 decimal_to_longest (const gdb_byte
*from
, int len
, enum bfd_endian byte_order
)
1302 /* libdecnumber has a function to convert from decimal to integer, but
1303 it doesn't work when the decimal number has a fractional part. */
1304 std::string str
= decimal_to_string (from
, len
, byte_order
);
1305 return strtoll (str
.c_str (), NULL
, 10);
1308 /* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
1309 and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
1310 RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */
1312 decimal_binop (enum exp_opcode op
,
1313 const gdb_byte
*x
, int len_x
, enum bfd_endian byte_order_x
,
1314 const gdb_byte
*y
, int len_y
, enum bfd_endian byte_order_y
,
1315 gdb_byte
*result
, int len_result
,
1316 enum bfd_endian byte_order_result
)
1319 decNumber number1
, number2
, number3
;
1320 gdb_byte dec1
[16], dec2
[16], dec3
[16];
1322 match_endianness (x
, len_x
, byte_order_x
, dec1
);
1323 match_endianness (y
, len_y
, byte_order_y
, dec2
);
1325 decimal_to_number (dec1
, len_x
, &number1
);
1326 decimal_to_number (dec2
, len_y
, &number2
);
1328 set_decnumber_context (&set
, len_result
);
1333 decNumberAdd (&number3
, &number1
, &number2
, &set
);
1336 decNumberSubtract (&number3
, &number1
, &number2
, &set
);
1339 decNumberMultiply (&number3
, &number1
, &number2
, &set
);
1342 decNumberDivide (&number3
, &number1
, &number2
, &set
);
1345 decNumberPower (&number3
, &number1
, &number2
, &set
);
1348 error (_("Operation not valid for decimal floating point number."));
1352 /* Check for errors in the DFP operation. */
1353 decimal_check_errors (&set
);
1355 decimal_from_number (&number3
, dec3
, len_result
);
1357 match_endianness (dec3
, len_result
, byte_order_result
, result
);
1360 /* Returns true if X (which is LEN bytes wide) is the number zero. */
1362 decimal_is_zero (const gdb_byte
*x
, int len
, enum bfd_endian byte_order
)
1367 match_endianness (x
, len
, byte_order
, dec
);
1368 decimal_to_number (dec
, len
, &number
);
1370 return decNumberIsZero (&number
);
1373 /* Compares two numbers numerically. If X is less than Y then the return value
1374 will be -1. If they are equal, then the return value will be 0. If X is
1375 greater than the Y then the return value will be 1. */
1377 decimal_compare (const gdb_byte
*x
, int len_x
, enum bfd_endian byte_order_x
,
1378 const gdb_byte
*y
, int len_y
, enum bfd_endian byte_order_y
)
1380 decNumber number1
, number2
, result
;
1382 gdb_byte dec1
[16], dec2
[16];
1385 match_endianness (x
, len_x
, byte_order_x
, dec1
);
1386 match_endianness (y
, len_y
, byte_order_y
, dec2
);
1388 decimal_to_number (dec1
, len_x
, &number1
);
1389 decimal_to_number (dec2
, len_y
, &number2
);
1391 /* Perform the comparison in the larger of the two sizes. */
1392 len_result
= len_x
> len_y
? len_x
: len_y
;
1393 set_decnumber_context (&set
, len_result
);
1395 decNumberCompare (&result
, &number1
, &number2
, &set
);
1397 /* Check for errors in the DFP operation. */
1398 decimal_check_errors (&set
);
1400 if (decNumberIsNaN (&result
))
1401 error (_("Comparison with an invalid number (NaN)."));
1402 else if (decNumberIsZero (&result
))
1404 else if (decNumberIsNegative (&result
))
1410 /* Convert a decimal value from a decimal type with LEN_FROM bytes to a
1411 decimal type with LEN_TO bytes. */
1413 decimal_convert (const gdb_byte
*from
, int len_from
,
1414 enum bfd_endian byte_order_from
, gdb_byte
*to
, int len_to
,
1415 enum bfd_endian byte_order_to
)
1420 match_endianness (from
, len_from
, byte_order_from
, dec
);
1422 decimal_to_number (dec
, len_from
, &number
);
1423 decimal_from_number (&number
, dec
, len_to
);
1425 match_endianness (dec
, len_to
, byte_order_to
, to
);
1429 /* Typed floating-point routines. These routines operate on floating-point
1430 values in target format, represented by a byte buffer interpreted as a
1431 "struct type", which may be either a binary or decimal floating-point
1432 type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */
1434 /* Return whether the byte-stream ADDR holds a valid value of
1435 floating-point type TYPE. */
1437 target_float_is_valid (const gdb_byte
*addr
, const struct type
*type
)
1439 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1440 return floatformat_is_valid (floatformat_from_type (type
), addr
);
1442 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1445 gdb_assert_not_reached ("unexpected type code");
1448 /* Return whether the byte-stream ADDR, interpreted as floating-point
1449 type TYPE, is numerically equal to zero (of either sign). */
1451 target_float_is_zero (const gdb_byte
*addr
, const struct type
*type
)
1453 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1454 return (floatformat_classify (floatformat_from_type (type
), addr
)
1457 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1458 return decimal_is_zero (addr
, TYPE_LENGTH (type
),
1459 gdbarch_byte_order (get_type_arch (type
)));
1461 gdb_assert_not_reached ("unexpected type code");
1464 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1465 to a string, optionally using the print format FORMAT. */
1467 target_float_to_string (const gdb_byte
*addr
, const struct type
*type
,
1470 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1471 return floatformat_to_string (floatformat_from_type (type
), addr
, format
);
1473 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1474 return decimal_to_string (addr
, TYPE_LENGTH (type
),
1475 gdbarch_byte_order (get_type_arch (type
)),
1478 gdb_assert_not_reached ("unexpected type code");
1481 /* Parse string STRING into a target floating-number of type TYPE and
1482 store it as byte-stream ADDR. Return whether parsing succeeded. */
1484 target_float_from_string (gdb_byte
*addr
, const struct type
*type
,
1485 const std::string
&string
)
1487 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1488 memset (addr
, 0, TYPE_LENGTH (type
));
1490 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1491 return floatformat_from_string (floatformat_from_type (type
), addr
,
1494 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1495 return decimal_from_string (addr
, TYPE_LENGTH (type
),
1496 gdbarch_byte_order (get_type_arch (type
)),
1499 gdb_assert_not_reached ("unexpected type code");
1502 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1503 to an integer value (rounding towards zero). */
1505 target_float_to_longest (const gdb_byte
*addr
, const struct type
*type
)
1507 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1508 return floatformat_to_longest (floatformat_from_type (type
), addr
);
1510 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1511 return decimal_to_longest (addr
, TYPE_LENGTH (type
),
1512 gdbarch_byte_order (get_type_arch (type
)));
1514 gdb_assert_not_reached ("unexpected type code");
1517 /* Convert signed integer VAL to a target floating-number of type TYPE
1518 and store it as byte-stream ADDR. */
1520 target_float_from_longest (gdb_byte
*addr
, const struct type
*type
,
1523 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1524 memset (addr
, 0, TYPE_LENGTH (type
));
1526 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1528 floatformat_from_longest (floatformat_from_type (type
), addr
, val
);
1532 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1534 decimal_from_longest (val
, addr
, TYPE_LENGTH (type
),
1535 gdbarch_byte_order (get_type_arch (type
)));
1539 gdb_assert_not_reached ("unexpected type code");
1542 /* Convert unsigned integer VAL to a target floating-number of type TYPE
1543 and store it as byte-stream ADDR. */
1545 target_float_from_ulongest (gdb_byte
*addr
, const struct type
*type
,
1548 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1549 memset (addr
, 0, TYPE_LENGTH (type
));
1551 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1553 floatformat_from_ulongest (floatformat_from_type (type
), addr
, val
);
1557 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1559 decimal_from_ulongest (val
, addr
, TYPE_LENGTH (type
),
1560 gdbarch_byte_order (get_type_arch (type
)));
1564 gdb_assert_not_reached ("unexpected type code");
1567 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1568 to a floating-point value in the host "double" format. */
1570 target_float_to_host_double (const gdb_byte
*addr
,
1571 const struct type
*type
)
1573 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1574 return floatformat_to_host_double (floatformat_from_type (type
), addr
);
1576 /* We don't support conversions between target decimal floating-point
1577 types and the host double type here. */
1579 gdb_assert_not_reached ("unexpected type code");
1582 /* Convert floating-point value VAL in the host "double" format to a target
1583 floating-number of type TYPE and store it as byte-stream ADDR. */
1585 target_float_from_host_double (gdb_byte
*addr
, const struct type
*type
,
1588 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1589 memset (addr
, 0, TYPE_LENGTH (type
));
1591 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1593 floatformat_from_host_double (floatformat_from_type (type
), addr
, val
);
1597 /* We don't support conversions between target decimal floating-point
1598 types and the host double type here. */
1600 gdb_assert_not_reached ("unexpected type code");
1603 /* Convert a floating-point number of type FROM_TYPE from the target
1604 byte-stream FROM to a floating-point number of type TO_TYPE, and
1605 store it to the target byte-stream TO. */
1607 target_float_convert (const gdb_byte
*from
, const struct type
*from_type
,
1608 gdb_byte
*to
, const struct type
*to_type
)
1610 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1611 memset (to
, 0, TYPE_LENGTH (to_type
));
1613 /* Use direct conversion routines if we have them. */
1615 if (TYPE_CODE (from_type
) == TYPE_CODE_FLT
1616 && TYPE_CODE (to_type
) == TYPE_CODE_FLT
)
1618 floatformat_convert (from
, floatformat_from_type (from_type
),
1619 to
, floatformat_from_type (to_type
));
1623 if (TYPE_CODE (from_type
) == TYPE_CODE_DECFLOAT
1624 && TYPE_CODE (to_type
) == TYPE_CODE_DECFLOAT
)
1626 decimal_convert (from
, TYPE_LENGTH (from_type
),
1627 gdbarch_byte_order (get_type_arch (from_type
)),
1628 to
, TYPE_LENGTH (to_type
),
1629 gdbarch_byte_order (get_type_arch (to_type
)));
1633 /* We cannot directly convert between binary and decimal floating-point
1634 types, so go via an intermediary string. */
1636 if ((TYPE_CODE (from_type
) == TYPE_CODE_FLT
1637 && TYPE_CODE (to_type
) == TYPE_CODE_DECFLOAT
)
1638 || (TYPE_CODE (from_type
) == TYPE_CODE_DECFLOAT
1639 && TYPE_CODE (to_type
) == TYPE_CODE_FLT
))
1641 std::string str
= target_float_to_string (from
, from_type
);
1642 target_float_from_string (to
, to_type
, str
);
1646 gdb_assert_not_reached ("unexpected type code");
1649 /* Perform the binary operation indicated by OPCODE, using as operands the
1650 target byte streams X and Y, interpreted as floating-point numbers of
1651 types TYPE_X and TYPE_Y, respectively. Convert the result to type
1652 TYPE_RES and store it into the byte-stream RES.
1654 The three types must either be all binary floating-point types, or else
1655 all decimal floating-point types. Binary and decimal floating-point
1656 types cannot be mixed within a single operation. */
1658 target_float_binop (enum exp_opcode opcode
,
1659 const gdb_byte
*x
, const struct type
*type_x
,
1660 const gdb_byte
*y
, const struct type
*type_y
,
1661 gdb_byte
*res
, const struct type
*type_res
)
1663 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1664 memset (res
, 0, TYPE_LENGTH (type_res
));
1666 if (TYPE_CODE (type_res
) == TYPE_CODE_FLT
)
1668 gdb_assert (TYPE_CODE (type_x
) == TYPE_CODE_FLT
);
1669 gdb_assert (TYPE_CODE (type_y
) == TYPE_CODE_FLT
);
1670 return floatformat_binop (opcode
,
1671 floatformat_from_type (type_x
), x
,
1672 floatformat_from_type (type_y
), y
,
1673 floatformat_from_type (type_res
), res
);
1676 if (TYPE_CODE (type_res
) == TYPE_CODE_DECFLOAT
)
1678 gdb_assert (TYPE_CODE (type_x
) == TYPE_CODE_DECFLOAT
);
1679 gdb_assert (TYPE_CODE (type_y
) == TYPE_CODE_DECFLOAT
);
1680 return decimal_binop (opcode
,
1681 x
, TYPE_LENGTH (type_x
),
1682 gdbarch_byte_order (get_type_arch (type_x
)),
1683 y
, TYPE_LENGTH (type_y
),
1684 gdbarch_byte_order (get_type_arch (type_y
)),
1685 res
, TYPE_LENGTH (type_res
),
1686 gdbarch_byte_order (get_type_arch (type_res
)));
1689 gdb_assert_not_reached ("unexpected type code");
1692 /* Compare the two target byte streams X and Y, interpreted as floating-point
1693 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
1694 are equal, -1 if X is less than Y, and 1 otherwise.
1696 The two types must either both be binary floating-point types, or else
1697 both be decimal floating-point types. Binary and decimal floating-point
1698 types cannot compared directly against each other. */
1700 target_float_compare (const gdb_byte
*x
, const struct type
*type_x
,
1701 const gdb_byte
*y
, const struct type
*type_y
)
1703 if (TYPE_CODE (type_x
) == TYPE_CODE_FLT
)
1705 gdb_assert (TYPE_CODE (type_y
) == TYPE_CODE_FLT
);
1706 return floatformat_compare (floatformat_from_type (type_x
), x
,
1707 floatformat_from_type (type_y
), y
);
1710 if (TYPE_CODE (type_x
) == TYPE_CODE_DECFLOAT
)
1712 gdb_assert (TYPE_CODE (type_y
) == TYPE_CODE_DECFLOAT
);
1713 return decimal_compare (x
, TYPE_LENGTH (type_x
),
1714 gdbarch_byte_order (get_type_arch (type_x
)),
1715 y
, TYPE_LENGTH (type_y
),
1716 gdbarch_byte_order (get_type_arch (type_y
)));
1719 gdb_assert_not_reached ("unexpected type code");