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 /* Target floating-point operations.
28 We provide multiple implementations of those operations, which differ
29 by the host-side intermediate format they perform computations in.
31 Those multiple implementations all derive from the following abstract
32 base class, which specifies the set of operations to be implemented. */
34 class target_float_ops
37 virtual std::string
to_string (const gdb_byte
*addr
, const struct type
*type
,
38 const char *format
) const = 0;
39 virtual bool from_string (gdb_byte
*addr
, const struct type
*type
,
40 const std::string
&string
) const = 0;
42 virtual LONGEST
to_longest (const gdb_byte
*addr
,
43 const struct type
*type
) const = 0;
44 virtual void from_longest (gdb_byte
*addr
, const struct type
*type
,
45 LONGEST val
) const = 0;
46 virtual void from_ulongest (gdb_byte
*addr
, const struct type
*type
,
47 ULONGEST val
) const = 0;
48 virtual double to_host_double (const gdb_byte
*addr
,
49 const struct type
*type
) const = 0;
50 virtual void from_host_double (gdb_byte
*addr
, const struct type
*type
,
51 double val
) const = 0;
52 virtual void convert (const gdb_byte
*from
, const struct type
*from_type
,
53 gdb_byte
*to
, const struct type
*to_type
) const = 0;
55 virtual void binop (enum exp_opcode opcode
,
56 const gdb_byte
*x
, const struct type
*type_x
,
57 const gdb_byte
*y
, const struct type
*type_y
,
58 gdb_byte
*res
, const struct type
*type_res
) const = 0;
59 virtual int compare (const gdb_byte
*x
, const struct type
*type_x
,
60 const gdb_byte
*y
, const struct type
*type_y
) const = 0;
64 /* Helper routines operating on binary floating-point data. */
69 /* Different kinds of floatformat numbers recognized by
70 floatformat_classify. To avoid portability issues, we use local
71 values instead of the C99 macros (FP_NAN et cetera). */
80 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
81 going to bother with trying to muck around with whether it is defined in
82 a system header, what we do if not, etc. */
83 #define FLOATFORMAT_CHAR_BIT 8
85 /* The number of bytes that the largest floating-point type that we
86 can convert to doublest will need. */
87 #define FLOATFORMAT_LARGEST_BYTES 16
89 /* Return the floatformat's total size in host bytes. */
91 floatformat_totalsize_bytes (const struct floatformat
*fmt
)
93 return ((fmt
->totalsize
+ FLOATFORMAT_CHAR_BIT
- 1)
94 / FLOATFORMAT_CHAR_BIT
);
97 /* Return the precision of the floating point format FMT. */
99 floatformat_precision (const struct floatformat
*fmt
)
101 /* Assume the precision of and IBM long double is twice the precision
102 of the underlying double. This matches what GCC does. */
104 return 2 * floatformat_precision (fmt
->split_half
);
106 /* Otherwise, the precision is the size of mantissa in bits,
107 including the implicit bit if present. */
108 int prec
= fmt
->man_len
;
109 if (fmt
->intbit
== floatformat_intbit_no
)
115 /* Normalize the byte order of FROM into TO. If no normalization is
116 needed then FMT->byteorder is returned and TO is not changed;
117 otherwise the format of the normalized form in TO is returned. */
118 static enum floatformat_byteorders
119 floatformat_normalize_byteorder (const struct floatformat
*fmt
,
120 const void *from
, void *to
)
122 const unsigned char *swapin
;
123 unsigned char *swapout
;
126 if (fmt
->byteorder
== floatformat_little
127 || fmt
->byteorder
== floatformat_big
)
128 return fmt
->byteorder
;
130 words
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
133 swapout
= (unsigned char *)to
;
134 swapin
= (const unsigned char *)from
;
136 if (fmt
->byteorder
== floatformat_vax
)
140 *swapout
++ = swapin
[1];
141 *swapout
++ = swapin
[0];
142 *swapout
++ = swapin
[3];
143 *swapout
++ = swapin
[2];
146 /* This may look weird, since VAX is little-endian, but it is
147 easier to translate to big-endian than to little-endian. */
148 return floatformat_big
;
152 gdb_assert (fmt
->byteorder
== floatformat_littlebyte_bigword
);
156 *swapout
++ = swapin
[3];
157 *swapout
++ = swapin
[2];
158 *swapout
++ = swapin
[1];
159 *swapout
++ = swapin
[0];
162 return floatformat_big
;
166 /* Extract a field which starts at START and is LEN bytes long. DATA and
167 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
169 get_field (const bfd_byte
*data
, enum floatformat_byteorders order
,
170 unsigned int total_len
, unsigned int start
, unsigned int len
)
172 unsigned long result
;
173 unsigned int cur_byte
;
176 /* Caller must byte-swap words before calling this routine. */
177 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
179 /* Start at the least significant part of the field. */
180 if (order
== floatformat_little
)
182 /* We start counting from the other end (i.e, from the high bytes
183 rather than the low bytes). As such, we need to be concerned
184 with what happens if bit 0 doesn't start on a byte boundary.
185 I.e, we need to properly handle the case where total_len is
186 not evenly divisible by 8. So we compute ``excess'' which
187 represents the number of bits from the end of our starting
188 byte needed to get to bit 0. */
189 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
191 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
192 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
193 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
194 - FLOATFORMAT_CHAR_BIT
;
198 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
200 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
202 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
203 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
206 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
207 if (order
== floatformat_little
)
212 /* Move towards the most significant part of the field. */
213 while (cur_bitshift
< len
)
215 result
|= (unsigned long)*(data
+ cur_byte
) << cur_bitshift
;
216 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
219 case floatformat_little
:
222 case floatformat_big
:
227 if (len
< sizeof(result
) * FLOATFORMAT_CHAR_BIT
)
228 /* Mask out bits which are not part of the field. */
229 result
&= ((1UL << len
) - 1);
233 /* Set a field which starts at START and is LEN bytes long. DATA and
234 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
236 put_field (unsigned char *data
, enum floatformat_byteorders order
,
237 unsigned int total_len
, unsigned int start
, unsigned int len
,
238 unsigned long stuff_to_put
)
240 unsigned int cur_byte
;
243 /* Caller must byte-swap words before calling this routine. */
244 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
246 /* Start at the least significant part of the field. */
247 if (order
== floatformat_little
)
249 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
251 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
252 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
253 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
254 - FLOATFORMAT_CHAR_BIT
;
258 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
260 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
262 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
264 *(data
+ cur_byte
) &=
265 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1)
267 *(data
+ cur_byte
) |=
268 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
270 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
271 if (order
== floatformat_little
)
276 /* Move towards the most significant part of the field. */
277 while (cur_bitshift
< len
)
279 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
281 /* This is the last byte. */
282 *(data
+ cur_byte
) &=
283 ~((1 << (len
- cur_bitshift
)) - 1);
284 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
287 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
288 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
289 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
290 if (order
== floatformat_little
)
297 /* Check if VAL (which is assumed to be a floating point number whose
298 format is described by FMT) is negative. */
300 floatformat_is_negative (const struct floatformat
*fmt
,
301 const bfd_byte
*uval
)
303 enum floatformat_byteorders order
;
304 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
306 gdb_assert (fmt
!= NULL
);
307 gdb_assert (fmt
->totalsize
308 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
310 /* An IBM long double (a two element array of double) always takes the
311 sign of the first double. */
313 fmt
= fmt
->split_half
;
315 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
317 if (order
!= fmt
->byteorder
)
320 return get_field (uval
, order
, fmt
->totalsize
, fmt
->sign_start
, 1);
323 /* Check if VAL is "not a number" (NaN) for FMT. */
324 static enum float_kind
325 floatformat_classify (const struct floatformat
*fmt
,
326 const bfd_byte
*uval
)
330 unsigned int mant_bits
, mant_off
;
332 enum floatformat_byteorders order
;
333 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
336 gdb_assert (fmt
!= NULL
);
337 gdb_assert (fmt
->totalsize
338 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
340 /* An IBM long double (a two element array of double) can be classified
341 by looking at the first double. inf and nan are specified as
342 ignoring the second double. zero and subnormal will always have
343 the second double 0.0 if the long double is correctly rounded. */
345 fmt
= fmt
->split_half
;
347 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
349 if (order
!= fmt
->byteorder
)
352 exponent
= get_field (uval
, order
, fmt
->totalsize
, fmt
->exp_start
,
355 mant_bits_left
= fmt
->man_len
;
356 mant_off
= fmt
->man_start
;
359 while (mant_bits_left
> 0)
361 mant_bits
= std::min (mant_bits_left
, 32);
363 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
365 /* If there is an explicit integer bit, mask it off. */
366 if (mant_off
== fmt
->man_start
367 && fmt
->intbit
== floatformat_intbit_yes
)
368 mant
&= ~(1 << (mant_bits
- 1));
376 mant_off
+= mant_bits
;
377 mant_bits_left
-= mant_bits
;
380 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
395 return float_subnormal
;
398 if (exponent
== fmt
->exp_nan
)
401 return float_infinite
;
409 /* Convert the mantissa of VAL (which is assumed to be a floating
410 point number whose format is described by FMT) into a hexadecimal
411 and store it in a static string. Return a pointer to that string. */
413 floatformat_mantissa (const struct floatformat
*fmt
,
416 unsigned char *uval
= (unsigned char *) val
;
418 unsigned int mant_bits
, mant_off
;
423 enum floatformat_byteorders order
;
424 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
426 gdb_assert (fmt
!= NULL
);
427 gdb_assert (fmt
->totalsize
428 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
430 /* For IBM long double (a two element array of double), return the
431 mantissa of the first double. The problem with returning the
432 actual mantissa from both doubles is that there can be an
433 arbitrary number of implied 0's or 1's between the mantissas
434 of the first and second double. In any case, this function
435 is only used for dumping out nans, and a nan is specified to
436 ignore the value in the second double. */
438 fmt
= fmt
->split_half
;
440 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
442 if (order
!= fmt
->byteorder
)
448 /* Make sure we have enough room to store the mantissa. */
449 gdb_assert (sizeof res
> ((fmt
->man_len
+ 7) / 8) * 2);
451 mant_off
= fmt
->man_start
;
452 mant_bits_left
= fmt
->man_len
;
453 mant_bits
= (mant_bits_left
% 32) > 0 ? mant_bits_left
% 32 : 32;
455 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
457 len
= xsnprintf (res
, sizeof res
, "%lx", mant
);
459 mant_off
+= mant_bits
;
460 mant_bits_left
-= mant_bits
;
462 while (mant_bits_left
> 0)
464 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, 32);
466 xsnprintf (buf
, sizeof buf
, "%08lx", mant
);
467 gdb_assert (len
+ strlen (buf
) <= sizeof res
);
471 mant_bits_left
-= 32;
477 /* Convert printf format string FORMAT to the otherwise equivalent string
478 which may be used to print a host floating-point number using the length
479 modifier LENGTH (which may be 0 if none is needed). If FORMAT is null,
480 return a format appropriate to print the full precision of a target
481 floating-point number of format FMT. */
483 floatformat_printf_format (const struct floatformat
*fmt
,
484 const char *format
, char length
)
486 std::string host_format
;
489 if (format
== nullptr)
491 /* If no format was specified, print the number using a format string
492 where the precision is set to the DECIMAL_DIG value for the given
493 floating-point format. This value is computed as
495 ceil(1 + p * log10(b)),
497 where p is the precision of the floating-point format in bits, and
498 b is the base (which is always 2 for the formats we support). */
499 const double log10_2
= .30102999566398119521;
500 double d_decimal_dig
= 1 + floatformat_precision (fmt
) * log10_2
;
501 int decimal_dig
= d_decimal_dig
;
502 if (decimal_dig
< d_decimal_dig
)
505 host_format
= string_printf ("%%.%d", decimal_dig
);
510 /* Use the specified format, stripping out the conversion character
511 and length modifier, if present. */
512 size_t len
= strlen (format
);
513 gdb_assert (len
> 1);
514 conversion
= format
[--len
];
515 gdb_assert (conversion
== 'e' || conversion
== 'f' || conversion
== 'g'
516 || conversion
== 'E' || conversion
== 'G');
517 if (format
[len
- 1] == 'L')
520 host_format
= std::string (format
, len
);
523 /* Add the length modifier and conversion character appropriate for
524 handling the appropriate host floating-point type. */
526 host_format
+= length
;
527 host_format
+= conversion
;
532 /* Implementation of target_float_ops using the host floating-point type T
533 as intermediate type. */
535 template<typename T
> class host_float_ops
: public target_float_ops
538 std::string
to_string (const gdb_byte
*addr
, const struct type
*type
,
539 const char *format
) const override
;
540 bool from_string (gdb_byte
*addr
, const struct type
*type
,
541 const std::string
&string
) const override
;
543 LONGEST
to_longest (const gdb_byte
*addr
,
544 const struct type
*type
) const override
;
545 void from_longest (gdb_byte
*addr
, const struct type
*type
,
546 LONGEST val
) const override
;
547 void from_ulongest (gdb_byte
*addr
, const struct type
*type
,
548 ULONGEST val
) const override
;
549 double to_host_double (const gdb_byte
*addr
,
550 const struct type
*type
) const override
;
551 void from_host_double (gdb_byte
*addr
, const struct type
*type
,
552 double val
) const override
;
553 void convert (const gdb_byte
*from
, const struct type
*from_type
,
554 gdb_byte
*to
, const struct type
*to_type
) const override
;
556 void binop (enum exp_opcode opcode
,
557 const gdb_byte
*x
, const struct type
*type_x
,
558 const gdb_byte
*y
, const struct type
*type_y
,
559 gdb_byte
*res
, const struct type
*type_res
) const override
;
560 int compare (const gdb_byte
*x
, const struct type
*type_x
,
561 const gdb_byte
*y
, const struct type
*type_y
) const override
;
564 void from_target (const struct floatformat
*fmt
,
565 const gdb_byte
*from
, T
*to
) const;
566 void from_target (const struct type
*type
,
567 const gdb_byte
*from
, T
*to
) const;
569 void to_target (const struct type
*type
,
570 const T
*from
, gdb_byte
*to
) const;
571 void to_target (const struct floatformat
*fmt
,
572 const T
*from
, gdb_byte
*to
) const;
576 /* Convert TO/FROM target to the host floating-point format T.
578 If the host and target formats agree, we just copy the raw data
579 into the appropriate type of variable and return, letting the host
580 increase precision as necessary. Otherwise, we call the conversion
581 routine and let it do the dirty work. Note that even if the target
582 and host floating-point formats match, the length of the types
583 might still be different, so the conversion routines must make sure
584 to not overrun any buffers. For example, on x86, long double is
585 the 80-bit extended precision type on both 32-bit and 64-bit ABIs,
586 but by default it is stored as 12 bytes on 32-bit, and 16 bytes on
587 64-bit, for alignment reasons. See comment in store_typed_floating
588 for a discussion about zeroing out remaining bytes in the target
591 static const struct floatformat
*host_float_format
= GDB_HOST_FLOAT_FORMAT
;
592 static const struct floatformat
*host_double_format
= GDB_HOST_DOUBLE_FORMAT
;
593 static const struct floatformat
*host_long_double_format
594 = GDB_HOST_LONG_DOUBLE_FORMAT
;
596 /* Convert target floating-point value at FROM in format FMT to host
597 floating-point format of type T. */
598 template<typename T
> void
599 host_float_ops
<T
>::from_target (const struct floatformat
*fmt
,
600 const gdb_byte
*from
, T
*to
) const
602 gdb_assert (fmt
!= NULL
);
604 if (fmt
== host_float_format
)
608 memcpy (&val
, from
, floatformat_totalsize_bytes (fmt
));
612 else if (fmt
== host_double_format
)
616 memcpy (&val
, from
, floatformat_totalsize_bytes (fmt
));
620 else if (fmt
== host_long_double_format
)
624 memcpy (&val
, from
, floatformat_totalsize_bytes (fmt
));
629 unsigned char *ufrom
= (unsigned char *) from
;
633 unsigned int mant_bits
, mant_off
;
635 int special_exponent
; /* It's a NaN, denorm or zero. */
636 enum floatformat_byteorders order
;
637 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
638 enum float_kind kind
;
640 gdb_assert (fmt
->totalsize
641 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
643 /* For non-numbers, reuse libiberty's logic to find the correct
644 format. We do not lose any precision in this case by passing
646 kind
= floatformat_classify (fmt
, (const bfd_byte
*) from
);
647 if (kind
== float_infinite
|| kind
== float_nan
)
651 floatformat_to_double (fmt
->split_half
? fmt
->split_half
: fmt
,
657 order
= floatformat_normalize_byteorder (fmt
, ufrom
, newfrom
);
659 if (order
!= fmt
->byteorder
)
666 from_target (fmt
->split_half
, ufrom
, &dtop
);
667 /* Preserve the sign of 0, which is the sign of the top
674 from_target (fmt
->split_half
,
675 ufrom
+ fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
/ 2, &dbot
);
680 exponent
= get_field (ufrom
, order
, fmt
->totalsize
, fmt
->exp_start
,
682 /* Note that if exponent indicates a NaN, we can't really do anything useful
683 (not knowing if the host has NaN's, or how to build one). So it will
684 end up as an infinity or something close; that is OK. */
686 mant_bits_left
= fmt
->man_len
;
687 mant_off
= fmt
->man_start
;
690 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
692 /* Don't bias NaNs. Use minimum exponent for denorms. For
693 simplicity, we don't check for zero as the exponent doesn't matter.
694 Note the cast to int; exp_bias is unsigned, so it's important to
695 make sure the operation is done in signed arithmetic. */
696 if (!special_exponent
)
697 exponent
-= fmt
->exp_bias
;
698 else if (exponent
== 0)
699 exponent
= 1 - fmt
->exp_bias
;
701 /* Build the result algebraically. Might go infinite, underflow, etc;
704 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
705 increment the exponent by one to account for the integer bit. */
707 if (!special_exponent
)
709 if (fmt
->intbit
== floatformat_intbit_no
)
710 dto
= ldexp (1.0, exponent
);
715 while (mant_bits_left
> 0)
717 mant_bits
= std::min (mant_bits_left
, 32);
719 mant
= get_field (ufrom
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
721 dto
+= ldexp ((T
) mant
, exponent
- mant_bits
);
722 exponent
-= mant_bits
;
723 mant_off
+= mant_bits
;
724 mant_bits_left
-= mant_bits
;
727 /* Negate it if negative. */
728 if (get_field (ufrom
, order
, fmt
->totalsize
, fmt
->sign_start
, 1))
733 template<typename T
> void
734 host_float_ops
<T
>::from_target (const struct type
*type
,
735 const gdb_byte
*from
, T
*to
) const
737 from_target (floatformat_from_type (type
), from
, to
);
740 /* Convert host floating-point value of type T to target floating-point
741 value in format FMT and store at TO. */
742 template<typename T
> void
743 host_float_ops
<T
>::to_target (const struct floatformat
*fmt
,
744 const T
*from
, gdb_byte
*to
) const
746 gdb_assert (fmt
!= NULL
);
748 if (fmt
== host_float_format
)
752 memcpy (to
, &val
, floatformat_totalsize_bytes (fmt
));
755 else if (fmt
== host_double_format
)
759 memcpy (to
, &val
, floatformat_totalsize_bytes (fmt
));
762 else if (fmt
== host_long_double_format
)
764 long double val
= *from
;
766 memcpy (to
, &val
, floatformat_totalsize_bytes (fmt
));
773 unsigned int mant_bits
, mant_off
;
775 unsigned char *uto
= (unsigned char *) to
;
776 enum floatformat_byteorders order
= fmt
->byteorder
;
777 unsigned char newto
[FLOATFORMAT_LARGEST_BYTES
];
779 if (order
!= floatformat_little
)
780 order
= floatformat_big
;
782 if (order
!= fmt
->byteorder
)
785 memcpy (&dfrom
, from
, sizeof (dfrom
));
786 memset (uto
, 0, floatformat_totalsize_bytes (fmt
));
790 /* Use static volatile to ensure that any excess precision is
791 removed via storing in memory, and so the top half really is
792 the result of converting to double. */
793 static volatile double dtop
, dbot
;
796 dtop
= (double) dfrom
;
797 /* If the rounded top half is Inf, the bottom must be 0 not NaN
799 if (dtop
+ dtop
== dtop
&& dtop
!= 0.0)
802 dbot
= (double) (dfrom
- (T
) dtop
);
805 to_target (fmt
->split_half
, &dtopnv
, uto
);
806 to_target (fmt
->split_half
, &dbotnv
,
807 uto
+ fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
/ 2);
812 goto finalize_byteorder
; /* Result is zero */
813 if (dfrom
!= dfrom
) /* Result is NaN */
816 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
817 fmt
->exp_len
, fmt
->exp_nan
);
818 /* Be sure it's not infinity, but NaN value is irrel. */
819 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
821 goto finalize_byteorder
;
824 /* If negative, set the sign bit. */
827 put_field (uto
, order
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
831 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity. */
833 /* Infinity exponent is same as NaN's. */
834 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
835 fmt
->exp_len
, fmt
->exp_nan
);
836 /* Infinity mantissa is all zeroes. */
837 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
839 goto finalize_byteorder
;
842 mant
= frexp (dfrom
, &exponent
);
844 if (exponent
+ fmt
->exp_bias
<= 0)
846 /* The value is too small to be expressed in the destination
847 type (not enough bits in the exponent. Treat as 0. */
848 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
850 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
852 goto finalize_byteorder
;
855 if (exponent
+ fmt
->exp_bias
>= (1 << fmt
->exp_len
))
857 /* The value is too large to fit into the destination.
858 Treat as infinity. */
859 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
860 fmt
->exp_len
, fmt
->exp_nan
);
861 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
863 goto finalize_byteorder
;
866 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
867 exponent
+ fmt
->exp_bias
- 1);
869 mant_bits_left
= fmt
->man_len
;
870 mant_off
= fmt
->man_start
;
871 while (mant_bits_left
> 0)
873 unsigned long mant_long
;
875 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
877 mant
*= 4294967296.0;
878 mant_long
= ((unsigned long) mant
) & 0xffffffffL
;
881 /* If the integer bit is implicit, then we need to discard it.
882 If we are discarding a zero, we should be (but are not) creating
883 a denormalized number which means adjusting the exponent
885 if (mant_bits_left
== fmt
->man_len
886 && fmt
->intbit
== floatformat_intbit_no
)
889 mant_long
&= 0xffffffffL
;
890 /* If we are processing the top 32 mantissa bits of a doublest
891 so as to convert to a float value with implied integer bit,
892 we will only be putting 31 of those 32 bits into the
893 final value due to the discarding of the top bit. In the
894 case of a small float value where the number of mantissa
895 bits is less than 32, discarding the top bit does not alter
896 the number of bits we will be adding to the result. */
903 /* The bits we want are in the most significant MANT_BITS bits of
904 mant_long. Move them to the least significant. */
905 mant_long
>>= 32 - mant_bits
;
908 put_field (uto
, order
, fmt
->totalsize
,
909 mant_off
, mant_bits
, mant_long
);
910 mant_off
+= mant_bits
;
911 mant_bits_left
-= mant_bits
;
915 /* Do we need to byte-swap the words in the result? */
916 if (order
!= fmt
->byteorder
)
917 floatformat_normalize_byteorder (fmt
, newto
, to
);
920 template<typename T
> void
921 host_float_ops
<T
>::to_target (const struct type
*type
,
922 const T
*from
, gdb_byte
*to
) const
924 /* Ensure possible padding bytes in the target buffer are zeroed out. */
925 memset (to
, 0, TYPE_LENGTH (type
));
927 to_target (floatformat_from_type (type
), from
, to
);
930 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
931 to a string, optionally using the print format FORMAT. */
932 template<typename T
> struct printf_length_modifier
934 static constexpr char value
= 0;
936 template<> struct printf_length_modifier
<long double>
938 static constexpr char value
= 'L';
940 template<typename T
> std::string
941 host_float_ops
<T
>::to_string (const gdb_byte
*addr
, const struct type
*type
,
942 const char *format
) const
944 /* Determine the format string to use on the host side. */
945 constexpr char length
= printf_length_modifier
<T
>::value
;
946 const struct floatformat
*fmt
= floatformat_from_type (type
);
947 std::string host_format
= floatformat_printf_format (fmt
, format
, length
);
950 from_target (type
, addr
, &host_float
);
951 return string_printf (host_format
.c_str (), host_float
);
954 /* Parse string IN into a target floating-number of type TYPE and
955 store it as byte-stream ADDR. Return whether parsing succeeded. */
956 template<typename T
> struct scanf_length_modifier
958 static constexpr char value
= 0;
960 template<> struct scanf_length_modifier
<double>
962 static constexpr char value
= 'l';
964 template<> struct scanf_length_modifier
<long double>
966 static constexpr char value
= 'L';
968 template<typename T
> bool
969 host_float_ops
<T
>::from_string (gdb_byte
*addr
, const struct type
*type
,
970 const std::string
&in
) const
975 std::string scan_format
= "%";
976 if (scanf_length_modifier
<T
>::value
)
977 scan_format
+= scanf_length_modifier
<T
>::value
;
978 scan_format
+= "g%n";
980 num
= sscanf (in
.c_str (), scan_format
.c_str(), &host_float
, &n
);
982 /* The sscanf man page suggests not making any assumptions on the effect
983 of %n on the result, so we don't.
984 That is why we simply test num == 0. */
988 /* We only accept the whole string. */
992 to_target (type
, &host_float
, addr
);
996 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
997 to an integer value (rounding towards zero). */
998 template<typename T
> LONGEST
999 host_float_ops
<T
>::to_longest (const gdb_byte
*addr
,
1000 const struct type
*type
) const
1003 from_target (type
, addr
, &host_float
);
1004 /* Converting an out-of-range value is undefined behavior in C, but we
1005 prefer to return a defined value here. */
1006 if (host_float
> std::numeric_limits
<LONGEST
>::max())
1007 return std::numeric_limits
<LONGEST
>::max();
1008 if (host_float
< std::numeric_limits
<LONGEST
>::min())
1009 return std::numeric_limits
<LONGEST
>::min();
1010 return (LONGEST
) host_float
;
1013 /* Convert signed integer VAL to a target floating-number of type TYPE
1014 and store it as byte-stream ADDR. */
1015 template<typename T
> void
1016 host_float_ops
<T
>::from_longest (gdb_byte
*addr
, const struct type
*type
,
1019 T host_float
= (T
) val
;
1020 to_target (type
, &host_float
, addr
);
1023 /* Convert unsigned integer VAL to a target floating-number of type TYPE
1024 and store it as byte-stream ADDR. */
1025 template<typename T
> void
1026 host_float_ops
<T
>::from_ulongest (gdb_byte
*addr
, const struct type
*type
,
1029 T host_float
= (T
) val
;
1030 to_target (type
, &host_float
, addr
);
1033 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1034 to a floating-point value in the host "double" format. */
1035 template<typename T
> double
1036 host_float_ops
<T
>::to_host_double (const gdb_byte
*addr
,
1037 const struct type
*type
) const
1040 from_target (type
, addr
, &host_float
);
1041 return (double) host_float
;
1044 /* Convert floating-point value VAL in the host "double" format to a target
1045 floating-number of type TYPE and store it as byte-stream ADDR. */
1046 template<typename T
> void
1047 host_float_ops
<T
>::from_host_double (gdb_byte
*addr
, const struct type
*type
,
1050 T host_float
= (T
) val
;
1051 to_target (type
, &host_float
, addr
);
1054 /* Convert a floating-point number of type FROM_TYPE from the target
1055 byte-stream FROM to a floating-point number of type TO_TYPE, and
1056 store it to the target byte-stream TO. */
1057 template<typename T
> void
1058 host_float_ops
<T
>::convert (const gdb_byte
*from
,
1059 const struct type
*from_type
,
1061 const struct type
*to_type
) const
1064 from_target (from_type
, from
, &host_float
);
1065 to_target (to_type
, &host_float
, to
);
1068 /* Perform the binary operation indicated by OPCODE, using as operands the
1069 target byte streams X and Y, interpreted as floating-point numbers of
1070 types TYPE_X and TYPE_Y, respectively. Convert the result to format
1071 TYPE_RES and store it into the byte-stream RES. */
1072 template<typename T
> void
1073 host_float_ops
<T
>::binop (enum exp_opcode op
,
1074 const gdb_byte
*x
, const struct type
*type_x
,
1075 const gdb_byte
*y
, const struct type
*type_y
,
1076 gdb_byte
*res
, const struct type
*type_res
) const
1080 from_target (type_x
, x
, &v1
);
1081 from_target (type_y
, y
, &v2
);
1105 error (_("Cannot perform exponentiation: %s"),
1106 safe_strerror (errno
));
1110 v
= v1
< v2
? v1
: v2
;
1114 v
= v1
> v2
? v1
: v2
;
1118 error (_("Integer-only operation on floating point number."));
1122 to_target (type_res
, &v
, res
);
1125 /* Compare the two target byte streams X and Y, interpreted as floating-point
1126 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
1127 are equal, -1 if X is less than Y, and 1 otherwise. */
1128 template<typename T
> int
1129 host_float_ops
<T
>::compare (const gdb_byte
*x
, const struct type
*type_x
,
1130 const gdb_byte
*y
, const struct type
*type_y
) const
1134 from_target (type_x
, x
, &v1
);
1135 from_target (type_y
, y
, &v2
);
1145 /* Helper routines operating on decimal floating-point data. */
1147 /* Decimal floating point is one of the extension to IEEE 754, which is
1148 described in http://grouper.ieee.org/groups/754/revision.html and
1149 http://www2.hursley.ibm.com/decimal/. It completes binary floating
1150 point by representing floating point more exactly. */
1152 /* The order of the following headers is important for making sure
1153 decNumber structure is large enough to hold decimal128 digits. */
1155 #include "dpd/decimal128.h"
1156 #include "dpd/decimal64.h"
1157 #include "dpd/decimal32.h"
1159 /* When using decimal128, this is the maximum string length + 1
1160 (value comes from libdecnumber's DECIMAL128_String constant). */
1161 #define MAX_DECIMAL_STRING 43
1163 /* In GDB, we are using an array of gdb_byte to represent decimal values.
1164 They are stored in host byte order. This routine does the conversion if
1165 the target byte order is different. */
1167 match_endianness (const gdb_byte
*from
, const struct type
*type
, gdb_byte
*to
)
1169 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
);
1171 int len
= TYPE_LENGTH (type
);
1175 #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE
1177 #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
1180 if (gdbarch_byte_order (get_type_arch (type
)) == OPPOSITE_BYTE_ORDER
)
1181 for (i
= 0; i
< len
; i
++)
1182 to
[i
] = from
[len
- i
- 1];
1184 for (i
= 0; i
< len
; i
++)
1190 /* Helper function to get the appropriate libdecnumber context for each size
1191 of decimal float. */
1193 set_decnumber_context (decContext
*ctx
, const struct type
*type
)
1195 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
);
1197 switch (TYPE_LENGTH (type
))
1200 decContextDefault (ctx
, DEC_INIT_DECIMAL32
);
1203 decContextDefault (ctx
, DEC_INIT_DECIMAL64
);
1206 decContextDefault (ctx
, DEC_INIT_DECIMAL128
);
1213 /* Check for errors signaled in the decimal context structure. */
1215 decimal_check_errors (decContext
*ctx
)
1217 /* An error here could be a division by zero, an overflow, an underflow or
1218 an invalid operation (from the DEC_Errors constant in decContext.h).
1219 Since GDB doesn't complain about division by zero, overflow or underflow
1220 errors for binary floating, we won't complain about them for decimal
1222 if (ctx
->status
& DEC_IEEE_854_Invalid_operation
)
1224 /* Leave only the error bits in the status flags. */
1225 ctx
->status
&= DEC_IEEE_854_Invalid_operation
;
1226 error (_("Cannot perform operation: %s"),
1227 decContextStatusToString (ctx
));
1231 /* Helper function to convert from libdecnumber's appropriate representation
1232 for computation to each size of decimal float. */
1234 decimal_from_number (const decNumber
*from
,
1235 gdb_byte
*to
, const struct type
*type
)
1241 set_decnumber_context (&set
, type
);
1243 switch (TYPE_LENGTH (type
))
1246 decimal32FromNumber ((decimal32
*) dec
, from
, &set
);
1249 decimal64FromNumber ((decimal64
*) dec
, from
, &set
);
1252 decimal128FromNumber ((decimal128
*) dec
, from
, &set
);
1255 error (_("Unknown decimal floating point type."));
1259 match_endianness (dec
, type
, to
);
1262 /* Helper function to convert each size of decimal float to libdecnumber's
1263 appropriate representation for computation. */
1265 decimal_to_number (const gdb_byte
*addr
, const struct type
*type
,
1269 match_endianness (addr
, type
, dec
);
1271 switch (TYPE_LENGTH (type
))
1274 decimal32ToNumber ((decimal32
*) dec
, to
);
1277 decimal64ToNumber ((decimal64
*) dec
, to
);
1280 decimal128ToNumber ((decimal128
*) dec
, to
);
1283 error (_("Unknown decimal floating point type."));
1288 /* Returns true if ADDR (which is of type TYPE) is the number zero. */
1290 decimal_is_zero (const gdb_byte
*addr
, const struct type
*type
)
1294 decimal_to_number (addr
, type
, &number
);
1296 return decNumberIsZero (&number
);
1300 /* Implementation of target_float_ops using the libdecnumber decNumber type
1301 as intermediate format. */
1303 class decimal_float_ops
: public target_float_ops
1306 std::string
to_string (const gdb_byte
*addr
, const struct type
*type
,
1307 const char *format
) const override
;
1308 bool from_string (gdb_byte
*addr
, const struct type
*type
,
1309 const std::string
&string
) const override
;
1311 LONGEST
to_longest (const gdb_byte
*addr
,
1312 const struct type
*type
) const override
;
1313 void from_longest (gdb_byte
*addr
, const struct type
*type
,
1314 LONGEST val
) const override
;
1315 void from_ulongest (gdb_byte
*addr
, const struct type
*type
,
1316 ULONGEST val
) const override
;
1317 double to_host_double (const gdb_byte
*addr
,
1318 const struct type
*type
) const override
1320 /* We don't support conversions between target decimal floating-point
1321 types and the host double type. */
1322 gdb_assert_not_reached ("invalid operation on decimal float");
1324 void from_host_double (gdb_byte
*addr
, const struct type
*type
,
1325 double val
) const override
1327 /* We don't support conversions between target decimal floating-point
1328 types and the host double type. */
1329 gdb_assert_not_reached ("invalid operation on decimal float");
1331 void convert (const gdb_byte
*from
, const struct type
*from_type
,
1332 gdb_byte
*to
, const struct type
*to_type
) const override
;
1334 void binop (enum exp_opcode opcode
,
1335 const gdb_byte
*x
, const struct type
*type_x
,
1336 const gdb_byte
*y
, const struct type
*type_y
,
1337 gdb_byte
*res
, const struct type
*type_res
) const override
;
1338 int compare (const gdb_byte
*x
, const struct type
*type_x
,
1339 const gdb_byte
*y
, const struct type
*type_y
) const override
;
1342 /* Convert decimal type to its string representation. LEN is the length
1343 of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
1344 16 bytes for decimal128. */
1346 decimal_float_ops::to_string (const gdb_byte
*addr
, const struct type
*type
,
1347 const char *format
= nullptr) const
1351 match_endianness (addr
, type
, dec
);
1353 if (format
!= nullptr)
1355 /* We don't handle format strings (yet). If the host printf supports
1356 decimal floating point types, just use this. Otherwise, fall back
1357 to printing the number while ignoring the format string. */
1358 #if defined (PRINTF_HAS_DECFLOAT)
1359 /* FIXME: This makes unwarranted assumptions about the host ABI! */
1360 return string_printf (format
, dec
);
1365 result
.resize (MAX_DECIMAL_STRING
);
1367 switch (TYPE_LENGTH (type
))
1370 decimal32ToString ((decimal32
*) dec
, &result
[0]);
1373 decimal64ToString ((decimal64
*) dec
, &result
[0]);
1376 decimal128ToString ((decimal128
*) dec
, &result
[0]);
1379 error (_("Unknown decimal floating point type."));
1386 /* Convert the string form of a decimal value to its decimal representation.
1387 LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
1388 decimal64 and 16 bytes for decimal128. */
1390 decimal_float_ops::from_string (gdb_byte
*addr
, const struct type
*type
,
1391 const std::string
&string
) const
1396 set_decnumber_context (&set
, type
);
1398 switch (TYPE_LENGTH (type
))
1401 decimal32FromString ((decimal32
*) dec
, string
.c_str (), &set
);
1404 decimal64FromString ((decimal64
*) dec
, string
.c_str (), &set
);
1407 decimal128FromString ((decimal128
*) dec
, string
.c_str (), &set
);
1410 error (_("Unknown decimal floating point type."));
1414 match_endianness (dec
, type
, addr
);
1416 /* Check for errors in the DFP operation. */
1417 decimal_check_errors (&set
);
1422 /* Converts a LONGEST to a decimal float of specified LEN bytes. */
1424 decimal_float_ops::from_longest (gdb_byte
*addr
, const struct type
*type
,
1429 if ((int32_t) from
!= from
)
1430 /* libdecnumber can convert only 32-bit integers. */
1431 error (_("Conversion of large integer to a "
1432 "decimal floating type is not supported."));
1434 decNumberFromInt32 (&number
, (int32_t) from
);
1436 decimal_from_number (&number
, addr
, type
);
1439 /* Converts a ULONGEST to a decimal float of specified LEN bytes. */
1441 decimal_float_ops::from_ulongest (gdb_byte
*addr
, const struct type
*type
,
1442 ULONGEST from
) const
1446 if ((uint32_t) from
!= from
)
1447 /* libdecnumber can convert only 32-bit integers. */
1448 error (_("Conversion of large integer to a "
1449 "decimal floating type is not supported."));
1451 decNumberFromUInt32 (&number
, (uint32_t) from
);
1453 decimal_from_number (&number
, addr
, type
);
1456 /* Converts a decimal float of LEN bytes to a LONGEST. */
1458 decimal_float_ops::to_longest (const gdb_byte
*addr
,
1459 const struct type
*type
) const
1461 /* libdecnumber has a function to convert from decimal to integer, but
1462 it doesn't work when the decimal number has a fractional part. */
1463 std::string str
= to_string (addr
, type
);
1464 return strtoll (str
.c_str (), NULL
, 10);
1467 /* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
1468 and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
1469 RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */
1471 decimal_float_ops::binop (enum exp_opcode op
,
1472 const gdb_byte
*x
, const struct type
*type_x
,
1473 const gdb_byte
*y
, const struct type
*type_y
,
1474 gdb_byte
*res
, const struct type
*type_res
) const
1477 decNumber number1
, number2
, number3
;
1479 decimal_to_number (x
, type_x
, &number1
);
1480 decimal_to_number (y
, type_y
, &number2
);
1482 set_decnumber_context (&set
, type_res
);
1487 decNumberAdd (&number3
, &number1
, &number2
, &set
);
1490 decNumberSubtract (&number3
, &number1
, &number2
, &set
);
1493 decNumberMultiply (&number3
, &number1
, &number2
, &set
);
1496 decNumberDivide (&number3
, &number1
, &number2
, &set
);
1499 decNumberPower (&number3
, &number1
, &number2
, &set
);
1502 error (_("Operation not valid for decimal floating point number."));
1506 /* Check for errors in the DFP operation. */
1507 decimal_check_errors (&set
);
1509 decimal_from_number (&number3
, res
, type_res
);
1512 /* Compares two numbers numerically. If X is less than Y then the return value
1513 will be -1. If they are equal, then the return value will be 0. If X is
1514 greater than the Y then the return value will be 1. */
1516 decimal_float_ops::compare (const gdb_byte
*x
, const struct type
*type_x
,
1517 const gdb_byte
*y
, const struct type
*type_y
) const
1519 decNumber number1
, number2
, result
;
1521 const struct type
*type_result
;
1523 decimal_to_number (x
, type_x
, &number1
);
1524 decimal_to_number (y
, type_y
, &number2
);
1526 /* Perform the comparison in the larger of the two sizes. */
1527 type_result
= TYPE_LENGTH (type_x
) > TYPE_LENGTH (type_y
) ? type_x
: type_y
;
1528 set_decnumber_context (&set
, type_result
);
1530 decNumberCompare (&result
, &number1
, &number2
, &set
);
1532 /* Check for errors in the DFP operation. */
1533 decimal_check_errors (&set
);
1535 if (decNumberIsNaN (&result
))
1536 error (_("Comparison with an invalid number (NaN)."));
1537 else if (decNumberIsZero (&result
))
1539 else if (decNumberIsNegative (&result
))
1545 /* Convert a decimal value from a decimal type with LEN_FROM bytes to a
1546 decimal type with LEN_TO bytes. */
1548 decimal_float_ops::convert (const gdb_byte
*from
, const struct type
*from_type
,
1549 gdb_byte
*to
, const struct type
*to_type
) const
1553 decimal_to_number (from
, from_type
, &number
);
1554 decimal_from_number (&number
, to
, to_type
);
1558 /* Typed floating-point routines. These routines operate on floating-point
1559 values in target format, represented by a byte buffer interpreted as a
1560 "struct type", which may be either a binary or decimal floating-point
1561 type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */
1563 /* Return whether TYPE1 and TYPE2 are of the same category (binary or
1564 decimal floating-point). */
1566 target_float_same_category_p (const struct type
*type1
,
1567 const struct type
*type2
)
1569 return TYPE_CODE (type1
) == TYPE_CODE (type2
);
1572 /* Return whether TYPE1 and TYPE2 use the same floating-point format. */
1574 target_float_same_format_p (const struct type
*type1
,
1575 const struct type
*type2
)
1577 if (!target_float_same_category_p (type1
, type2
))
1580 switch (TYPE_CODE (type1
))
1583 return floatformat_from_type (type1
) == floatformat_from_type (type2
);
1585 case TYPE_CODE_DECFLOAT
:
1586 return (TYPE_LENGTH (type1
) == TYPE_LENGTH (type2
)
1587 && (gdbarch_byte_order (get_type_arch (type1
))
1588 == gdbarch_byte_order (get_type_arch (type2
))));
1591 gdb_assert_not_reached ("unexpected type code");
1595 /* Return the size (without padding) of the target floating-point
1596 format used by TYPE. */
1598 target_float_format_length (const struct type
*type
)
1600 switch (TYPE_CODE (type
))
1603 return floatformat_totalsize_bytes (floatformat_from_type (type
));
1605 case TYPE_CODE_DECFLOAT
:
1606 return TYPE_LENGTH (type
);
1609 gdb_assert_not_reached ("unexpected type code");
1613 /* Identifiers of available host-side intermediate formats. These must
1614 be sorted so the that the more "general" kinds come later. */
1615 enum target_float_ops_kind
1617 /* Target binary floating-point formats that match a host format. */
1621 /* Any other target binary floating-point format. */
1623 /* Any target decimal floating-point format. */
1627 /* Given a target type TYPE, choose the best host-side intermediate format
1628 to perform operations on TYPE in. */
1629 static enum target_float_ops_kind
1630 get_target_float_ops_kind (const struct type
*type
)
1632 switch (TYPE_CODE (type
))
1636 const struct floatformat
*fmt
= floatformat_from_type (type
);
1638 /* Binary floating-point formats matching a host format. */
1639 if (fmt
== host_float_format
)
1640 return target_float_ops_kind::host_float
;
1641 if (fmt
== host_double_format
)
1642 return target_float_ops_kind::host_double
;
1643 if (fmt
== host_long_double_format
)
1644 return target_float_ops_kind::host_long_double
;
1646 /* Any other binary floating-point format. */
1647 return target_float_ops_kind::binary
;
1650 case TYPE_CODE_DECFLOAT
:
1652 /* Any decimal floating-point format. */
1653 return target_float_ops_kind::decimal
;
1657 gdb_assert_not_reached ("unexpected type code");
1661 /* Return target_float_ops to peform operations for KIND. */
1662 static const target_float_ops
*
1663 get_target_float_ops (enum target_float_ops_kind kind
)
1667 /* If the type format matches one of the host floating-point
1668 types, use that type as intermediate format. */
1669 case target_float_ops_kind::host_float
:
1671 static host_float_ops
<float> host_float_ops_float
;
1672 return &host_float_ops_float
;
1675 case target_float_ops_kind::host_double
:
1677 static host_float_ops
<double> host_float_ops_double
;
1678 return &host_float_ops_double
;
1681 case target_float_ops_kind::host_long_double
:
1683 static host_float_ops
<long double> host_float_ops_long_double
;
1684 return &host_float_ops_long_double
;
1687 /* For binary floating-point formats that do not match any host format,
1688 use the largest host floating-point type as intermediate format. */
1689 case target_float_ops_kind::binary
:
1691 static host_float_ops
<long double> binary_float_ops
;
1692 return &binary_float_ops
;
1695 /* For decimal floating-point types, always use the libdecnumber
1696 decNumber type as intermediate format. */
1697 case target_float_ops_kind::decimal
:
1699 static decimal_float_ops decimal_float_ops
;
1700 return &decimal_float_ops
;
1704 gdb_assert_not_reached ("unexpected target_float_ops_kind");
1708 /* Given a target type TYPE, determine the best host-side intermediate format
1709 to perform operations on TYPE in. */
1710 static const target_float_ops
*
1711 get_target_float_ops (const struct type
*type
)
1713 enum target_float_ops_kind kind
= get_target_float_ops_kind (type
);
1714 return get_target_float_ops (kind
);
1717 /* The same for operations involving two target types TYPE1 and TYPE2. */
1718 static const target_float_ops
*
1719 get_target_float_ops (const struct type
*type1
, const struct type
*type2
)
1721 gdb_assert (TYPE_CODE (type1
) == TYPE_CODE (type2
));
1723 enum target_float_ops_kind kind1
= get_target_float_ops_kind (type1
);
1724 enum target_float_ops_kind kind2
= get_target_float_ops_kind (type2
);
1726 /* Given the way the kinds are sorted, we simply choose the larger one;
1727 this will be able to hold values of either type. */
1728 return get_target_float_ops (std::max (kind1
, kind2
));
1731 /* Return whether the byte-stream ADDR holds a valid value of
1732 floating-point type TYPE. */
1734 target_float_is_valid (const gdb_byte
*addr
, const struct type
*type
)
1736 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1737 return floatformat_is_valid (floatformat_from_type (type
), addr
);
1739 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1742 gdb_assert_not_reached ("unexpected type code");
1745 /* Return whether the byte-stream ADDR, interpreted as floating-point
1746 type TYPE, is numerically equal to zero (of either sign). */
1748 target_float_is_zero (const gdb_byte
*addr
, const struct type
*type
)
1750 if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
1751 return (floatformat_classify (floatformat_from_type (type
), addr
)
1754 if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1755 return decimal_is_zero (addr
, type
);
1757 gdb_assert_not_reached ("unexpected type code");
1760 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1761 to a string, optionally using the print format FORMAT. */
1763 target_float_to_string (const gdb_byte
*addr
, const struct type
*type
,
1766 /* Unless we need to adhere to a specific format, provide special
1767 output for special cases of binary floating-point numbers. */
1768 if (format
== nullptr && TYPE_CODE (type
) == TYPE_CODE_FLT
)
1770 const struct floatformat
*fmt
= floatformat_from_type (type
);
1772 /* Detect invalid representations. */
1773 if (!floatformat_is_valid (fmt
, addr
))
1774 return "<invalid float value>";
1776 /* Handle NaN and Inf. */
1777 enum float_kind kind
= floatformat_classify (fmt
, addr
);
1778 if (kind
== float_nan
)
1780 const char *sign
= floatformat_is_negative (fmt
, addr
)? "-" : "";
1781 const char *mantissa
= floatformat_mantissa (fmt
, addr
);
1782 return string_printf ("%snan(0x%s)", sign
, mantissa
);
1784 else if (kind
== float_infinite
)
1786 const char *sign
= floatformat_is_negative (fmt
, addr
)? "-" : "";
1787 return string_printf ("%sinf", sign
);
1791 const target_float_ops
*ops
= get_target_float_ops (type
);
1792 return ops
->to_string (addr
, type
, format
);
1795 /* Parse string STRING into a target floating-number of type TYPE and
1796 store it as byte-stream ADDR. Return whether parsing succeeded. */
1798 target_float_from_string (gdb_byte
*addr
, const struct type
*type
,
1799 const std::string
&string
)
1801 const target_float_ops
*ops
= get_target_float_ops (type
);
1802 return ops
->from_string (addr
, type
, string
);
1805 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1806 to an integer value (rounding towards zero). */
1808 target_float_to_longest (const gdb_byte
*addr
, const struct type
*type
)
1810 const target_float_ops
*ops
= get_target_float_ops (type
);
1811 return ops
->to_longest (addr
, type
);
1814 /* Convert signed integer VAL to a target floating-number of type TYPE
1815 and store it as byte-stream ADDR. */
1817 target_float_from_longest (gdb_byte
*addr
, const struct type
*type
,
1820 const target_float_ops
*ops
= get_target_float_ops (type
);
1821 ops
->from_longest (addr
, type
, val
);
1824 /* Convert unsigned integer VAL to a target floating-number of type TYPE
1825 and store it as byte-stream ADDR. */
1827 target_float_from_ulongest (gdb_byte
*addr
, const struct type
*type
,
1830 const target_float_ops
*ops
= get_target_float_ops (type
);
1831 ops
->from_ulongest (addr
, type
, val
);
1834 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1835 to a floating-point value in the host "double" format. */
1837 target_float_to_host_double (const gdb_byte
*addr
,
1838 const struct type
*type
)
1840 const target_float_ops
*ops
= get_target_float_ops (type
);
1841 return ops
->to_host_double (addr
, type
);
1844 /* Convert floating-point value VAL in the host "double" format to a target
1845 floating-number of type TYPE and store it as byte-stream ADDR. */
1847 target_float_from_host_double (gdb_byte
*addr
, const struct type
*type
,
1850 const target_float_ops
*ops
= get_target_float_ops (type
);
1851 ops
->from_host_double (addr
, type
, val
);
1854 /* Convert a floating-point number of type FROM_TYPE from the target
1855 byte-stream FROM to a floating-point number of type TO_TYPE, and
1856 store it to the target byte-stream TO. */
1858 target_float_convert (const gdb_byte
*from
, const struct type
*from_type
,
1859 gdb_byte
*to
, const struct type
*to_type
)
1861 /* We cannot directly convert between binary and decimal floating-point
1862 types, so go via an intermediary string. */
1863 if (!target_float_same_category_p (from_type
, to_type
))
1865 std::string str
= target_float_to_string (from
, from_type
);
1866 target_float_from_string (to
, to_type
, str
);
1870 /* Convert between two different formats in the same category. */
1871 if (!target_float_same_format_p (from_type
, to_type
))
1873 const target_float_ops
*ops
= get_target_float_ops (from_type
, to_type
);
1874 ops
->convert (from
, from_type
, to
, to_type
);
1878 /* The floating-point formats match, so we simply copy the data, ensuring
1879 possible padding bytes in the target buffer are zeroed out. */
1880 memset (to
, 0, TYPE_LENGTH (to_type
));
1881 memcpy (to
, from
, target_float_format_length (to_type
));
1884 /* Perform the binary operation indicated by OPCODE, using as operands the
1885 target byte streams X and Y, interpreted as floating-point numbers of
1886 types TYPE_X and TYPE_Y, respectively. Convert the result to type
1887 TYPE_RES and store it into the byte-stream RES.
1889 The three types must either be all binary floating-point types, or else
1890 all decimal floating-point types. Binary and decimal floating-point
1891 types cannot be mixed within a single operation. */
1893 target_float_binop (enum exp_opcode opcode
,
1894 const gdb_byte
*x
, const struct type
*type_x
,
1895 const gdb_byte
*y
, const struct type
*type_y
,
1896 gdb_byte
*res
, const struct type
*type_res
)
1898 gdb_assert (target_float_same_category_p (type_x
, type_res
));
1899 gdb_assert (target_float_same_category_p (type_y
, type_res
));
1901 const target_float_ops
*ops
= get_target_float_ops (type_x
, type_y
);
1902 ops
->binop (opcode
, x
, type_x
, y
, type_y
, res
, type_res
);
1905 /* Compare the two target byte streams X and Y, interpreted as floating-point
1906 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
1907 are equal, -1 if X is less than Y, and 1 otherwise.
1909 The two types must either both be binary floating-point types, or else
1910 both be decimal floating-point types. Binary and decimal floating-point
1911 types cannot compared directly against each other. */
1913 target_float_compare (const gdb_byte
*x
, const struct type
*type_x
,
1914 const gdb_byte
*y
, const struct type
*type_y
)
1916 gdb_assert (target_float_same_category_p (type_x
, type_y
));
1918 const target_float_ops
*ops
= get_target_float_ops (type_x
, type_y
);
1919 return ops
->compare (x
, type_x
, y
, type_y
);