(Ada) problem printing renaming which references a subprogram parameter
[binutils-gdb.git] / gdb / target-float.c
blobc2878cb0a08f11ff74192873c257f71d8fa6dc0a
1 /* Floating point routines for GDB, the GNU debugger.
3 Copyright (C) 2017-2018 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/>. */
20 #include "defs.h"
21 #include "gdbtypes.h"
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
36 public:
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. */
66 #include <cmath>
67 #include <limits>
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). */
72 enum float_kind {
73 float_nan,
74 float_infinite,
75 float_zero,
76 float_normal,
77 float_subnormal
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. */
90 static size_t
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. */
98 static int
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. */
103 if (fmt->split_half)
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)
110 prec++;
112 return prec;
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;
124 int words;
126 if (fmt->byteorder == floatformat_little
127 || fmt->byteorder == floatformat_big)
128 return fmt->byteorder;
130 words = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
131 words >>= 2;
133 swapout = (unsigned char *)to;
134 swapin = (const unsigned char *)from;
136 if (fmt->byteorder == floatformat_vax)
138 while (words-- > 0)
140 *swapout++ = swapin[1];
141 *swapout++ = swapin[0];
142 *swapout++ = swapin[3];
143 *swapout++ = swapin[2];
144 swapin += 4;
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;
150 else
152 gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword);
154 while (words-- > 0)
156 *swapout++ = swapin[3];
157 *swapout++ = swapin[2];
158 *swapout++ = swapin[1];
159 *swapout++ = swapin[0];
160 swapin += 4;
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. */
168 static unsigned long
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;
174 int cur_bitshift;
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;
196 else
198 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
199 cur_bitshift =
200 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
202 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
203 result = *(data + cur_byte) >> (-cur_bitshift);
204 else
205 result = 0;
206 cur_bitshift += FLOATFORMAT_CHAR_BIT;
207 if (order == floatformat_little)
208 ++cur_byte;
209 else
210 --cur_byte;
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;
217 switch (order)
219 case floatformat_little:
220 ++cur_byte;
221 break;
222 case floatformat_big:
223 --cur_byte;
224 break;
227 if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
228 /* Mask out bits which are not part of the field. */
229 result &= ((1UL << len) - 1);
230 return result;
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. */
235 static void
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;
241 int cur_bitshift;
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;
256 else
258 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
259 cur_bitshift =
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)
266 << (-cur_bitshift));
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)
272 ++cur_byte;
273 else
274 --cur_byte;
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);
286 else
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)
291 ++cur_byte;
292 else
293 --cur_byte;
297 /* Check if VAL (which is assumed to be a floating point number whose
298 format is described by FMT) is negative. */
299 static int
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. */
312 if (fmt->split_half)
313 fmt = fmt->split_half;
315 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
317 if (order != fmt->byteorder)
318 uval = newfrom;
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)
328 long exponent;
329 unsigned long mant;
330 unsigned int mant_bits, mant_off;
331 int mant_bits_left;
332 enum floatformat_byteorders order;
333 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
334 int mant_zero;
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. */
344 if (fmt->split_half)
345 fmt = fmt->split_half;
347 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
349 if (order != fmt->byteorder)
350 uval = newfrom;
352 exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start,
353 fmt->exp_len);
355 mant_bits_left = fmt->man_len;
356 mant_off = fmt->man_start;
358 mant_zero = 1;
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));
370 if (mant)
372 mant_zero = 0;
373 break;
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
381 supported. */
382 if (! fmt->exp_nan)
384 if (mant_zero)
385 return float_zero;
386 else
387 return float_normal;
390 if (exponent == 0)
392 if (mant_zero)
393 return float_zero;
394 else
395 return float_subnormal;
398 if (exponent == fmt->exp_nan)
400 if (mant_zero)
401 return float_infinite;
402 else
403 return float_nan;
406 return float_normal;
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. */
412 static const char *
413 floatformat_mantissa (const struct floatformat *fmt,
414 const bfd_byte *val)
416 unsigned char *uval = (unsigned char *) val;
417 unsigned long mant;
418 unsigned int mant_bits, mant_off;
419 int mant_bits_left;
420 static char res[50];
421 char buf[9];
422 int len;
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. */
437 if (fmt->split_half)
438 fmt = fmt->split_half;
440 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
442 if (order != fmt->byteorder)
443 uval = newfrom;
445 if (! fmt->exp_nan)
446 return 0;
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);
468 strcat (res, buf);
470 mant_off += 32;
471 mant_bits_left -= 32;
474 return res;
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. */
482 static std::string
483 floatformat_printf_format (const struct floatformat *fmt,
484 const char *format, char length)
486 std::string host_format;
487 char conversion;
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)
503 decimal_dig++;
505 host_format = string_printf ("%%.%d", decimal_dig);
506 conversion = 'g';
508 else
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')
518 len--;
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. */
525 if (length)
526 host_format += length;
527 host_format += conversion;
529 return host_format;
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
537 public:
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;
563 private:
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
589 buffer. */
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)
606 float val = 0;
608 memcpy (&val, from, floatformat_totalsize_bytes (fmt));
609 *to = val;
610 return;
612 else if (fmt == host_double_format)
614 double val = 0;
616 memcpy (&val, from, floatformat_totalsize_bytes (fmt));
617 *to = val;
618 return;
620 else if (fmt == host_long_double_format)
622 long double val = 0;
624 memcpy (&val, from, floatformat_totalsize_bytes (fmt));
625 *to = val;
626 return;
629 unsigned char *ufrom = (unsigned char *) from;
630 T dto;
631 long exponent;
632 unsigned long mant;
633 unsigned int mant_bits, mant_off;
634 int mant_bits_left;
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
645 through a double. */
646 kind = floatformat_classify (fmt, (const bfd_byte *) from);
647 if (kind == float_infinite || kind == float_nan)
649 double dto;
651 floatformat_to_double (fmt->split_half ? fmt->split_half : fmt,
652 from, &dto);
653 *to = (T) dto;
654 return;
657 order = floatformat_normalize_byteorder (fmt, ufrom, newfrom);
659 if (order != fmt->byteorder)
660 ufrom = newfrom;
662 if (fmt->split_half)
664 T dtop, dbot;
666 from_target (fmt->split_half, ufrom, &dtop);
667 /* Preserve the sign of 0, which is the sign of the top
668 half. */
669 if (dtop == 0.0)
671 *to = dtop;
672 return;
674 from_target (fmt->split_half,
675 ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, &dbot);
676 *to = dtop + dbot;
677 return;
680 exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start,
681 fmt->exp_len);
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;
688 dto = 0.0;
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;
702 who cares. */
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);
711 else
712 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))
729 dto = -dto;
730 *to = dto;
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)
750 float val = *from;
752 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
753 return;
755 else if (fmt == host_double_format)
757 double val = *from;
759 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
760 return;
762 else if (fmt == host_long_double_format)
764 long double val = *from;
766 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
767 return;
770 T dfrom;
771 int exponent;
772 T mant;
773 unsigned int mant_bits, mant_off;
774 int mant_bits_left;
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)
783 uto = newto;
785 memcpy (&dfrom, from, sizeof (dfrom));
786 memset (uto, 0, floatformat_totalsize_bytes (fmt));
788 if (fmt->split_half)
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;
794 T dtopnv, dbotnv;
796 dtop = (double) dfrom;
797 /* If the rounded top half is Inf, the bottom must be 0 not NaN
798 or Inf. */
799 if (dtop + dtop == dtop && dtop != 0.0)
800 dbot = 0.0;
801 else
802 dbot = (double) (dfrom - (T) dtop);
803 dtopnv = dtop;
804 dbotnv = dbot;
805 to_target (fmt->split_half, &dtopnv, uto);
806 to_target (fmt->split_half, &dbotnv,
807 uto + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2);
808 return;
811 if (dfrom == 0)
812 goto finalize_byteorder; /* Result is zero */
813 if (dfrom != dfrom) /* Result is NaN */
815 /* From 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,
820 fmt->man_len, 1);
821 goto finalize_byteorder;
824 /* If negative, set the sign bit. */
825 if (dfrom < 0)
827 put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1);
828 dfrom = -dfrom;
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,
838 fmt->man_len, 0);
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,
849 fmt->exp_len, 0);
850 put_field (uto, order, fmt->totalsize, fmt->man_start,
851 fmt->man_len, 0);
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,
862 fmt->man_len, 0);
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;
879 mant -= mant_long;
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
884 (I think). */
885 if (mant_bits_left == fmt->man_len
886 && fmt->intbit == floatformat_intbit_no)
888 mant_long <<= 1;
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. */
897 if (mant_bits == 32)
898 mant_bits -= 1;
901 if (mant_bits < 32)
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;
914 finalize_byteorder:
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);
949 T host_float;
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
972 T host_float;
973 int n, num;
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. */
985 if (num == 0)
986 return false;
988 /* We only accept the whole string. */
989 if (in[n])
990 return false;
992 to_target (type, &host_float, addr);
993 return true;
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
1002 T host_float;
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,
1017 LONGEST val) const
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,
1027 ULONGEST val) const
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
1039 T host_float;
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,
1048 double val) const
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,
1060 gdb_byte *to,
1061 const struct type *to_type) const
1063 T host_float;
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
1078 T v1, v2, v = 0;
1080 from_target (type_x, x, &v1);
1081 from_target (type_y, y, &v2);
1083 switch (op)
1085 case BINOP_ADD:
1086 v = v1 + v2;
1087 break;
1089 case BINOP_SUB:
1090 v = v1 - v2;
1091 break;
1093 case BINOP_MUL:
1094 v = v1 * v2;
1095 break;
1097 case BINOP_DIV:
1098 v = v1 / v2;
1099 break;
1101 case BINOP_EXP:
1102 errno = 0;
1103 v = pow (v1, v2);
1104 if (errno)
1105 error (_("Cannot perform exponentiation: %s"),
1106 safe_strerror (errno));
1107 break;
1109 case BINOP_MIN:
1110 v = v1 < v2 ? v1 : v2;
1111 break;
1113 case BINOP_MAX:
1114 v = v1 > v2 ? v1 : v2;
1115 break;
1117 default:
1118 error (_("Integer-only operation on floating point number."));
1119 break;
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
1132 T v1, v2;
1134 from_target (type_x, x, &v1);
1135 from_target (type_y, y, &v2);
1137 if (v1 == v2)
1138 return 0;
1139 if (v1 < v2)
1140 return -1;
1141 return 1;
1145 /* Implementation of target_float_ops using the MPFR library
1146 mpfr_t as intermediate type. */
1148 #ifdef HAVE_LIBMPFR
1150 #define MPFR_USE_INTMAX_T
1152 #include <mpfr.h>
1154 class mpfr_float_ops : public target_float_ops
1156 public:
1157 std::string to_string (const gdb_byte *addr, const struct type *type,
1158 const char *format) const override;
1159 bool from_string (gdb_byte *addr, const struct type *type,
1160 const std::string &string) const override;
1162 LONGEST to_longest (const gdb_byte *addr,
1163 const struct type *type) const override;
1164 void from_longest (gdb_byte *addr, const struct type *type,
1165 LONGEST val) const override;
1166 void from_ulongest (gdb_byte *addr, const struct type *type,
1167 ULONGEST val) const override;
1168 double to_host_double (const gdb_byte *addr,
1169 const struct type *type) const override;
1170 void from_host_double (gdb_byte *addr, const struct type *type,
1171 double val) const override;
1172 void convert (const gdb_byte *from, const struct type *from_type,
1173 gdb_byte *to, const struct type *to_type) const override;
1175 void binop (enum exp_opcode opcode,
1176 const gdb_byte *x, const struct type *type_x,
1177 const gdb_byte *y, const struct type *type_y,
1178 gdb_byte *res, const struct type *type_res) const override;
1179 int compare (const gdb_byte *x, const struct type *type_x,
1180 const gdb_byte *y, const struct type *type_y) const override;
1182 private:
1183 /* Local wrapper class to handle mpfr_t initalization and cleanup. */
1184 class gdb_mpfr
1186 public:
1187 mpfr_t val;
1189 gdb_mpfr (const struct type *type)
1191 const struct floatformat *fmt = floatformat_from_type (type);
1192 mpfr_init2 (val, floatformat_precision (fmt));
1195 gdb_mpfr (const gdb_mpfr &source)
1197 mpfr_init2 (val, mpfr_get_prec (source.val));
1200 ~gdb_mpfr ()
1202 mpfr_clear (val);
1206 void from_target (const struct floatformat *fmt,
1207 const gdb_byte *from, gdb_mpfr &to) const;
1208 void from_target (const struct type *type,
1209 const gdb_byte *from, gdb_mpfr &to) const;
1211 void to_target (const struct type *type,
1212 const gdb_mpfr &from, gdb_byte *to) const;
1213 void to_target (const struct floatformat *fmt,
1214 const gdb_mpfr &from, gdb_byte *to) const;
1218 /* Convert TO/FROM target floating-point format to mpfr_t. */
1220 void
1221 mpfr_float_ops::from_target (const struct floatformat *fmt,
1222 const gdb_byte *orig_from, gdb_mpfr &to) const
1224 const gdb_byte *from = orig_from;
1225 mpfr_exp_t exponent;
1226 unsigned long mant;
1227 unsigned int mant_bits, mant_off;
1228 int mant_bits_left;
1229 int special_exponent; /* It's a NaN, denorm or zero. */
1230 enum floatformat_byteorders order;
1231 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
1232 enum float_kind kind;
1234 gdb_assert (fmt->totalsize
1235 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
1237 /* Handle non-numbers. */
1238 kind = floatformat_classify (fmt, from);
1239 if (kind == float_infinite)
1241 mpfr_set_inf (to.val, floatformat_is_negative (fmt, from) ? -1 : 1);
1242 return;
1244 if (kind == float_nan)
1246 mpfr_set_nan (to.val);
1247 return;
1250 order = floatformat_normalize_byteorder (fmt, from, newfrom);
1252 if (order != fmt->byteorder)
1253 from = newfrom;
1255 if (fmt->split_half)
1257 gdb_mpfr top (to), bot (to);
1259 from_target (fmt->split_half, from, top);
1260 /* Preserve the sign of 0, which is the sign of the top half. */
1261 if (mpfr_zero_p (top.val))
1263 mpfr_set (to.val, top.val, MPFR_RNDN);
1264 return;
1266 from_target (fmt->split_half,
1267 from + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, bot);
1268 mpfr_add (to.val, top.val, bot.val, MPFR_RNDN);
1269 return;
1272 exponent = get_field (from, order, fmt->totalsize, fmt->exp_start,
1273 fmt->exp_len);
1274 /* Note that if exponent indicates a NaN, we can't really do anything useful
1275 (not knowing if the host has NaN's, or how to build one). So it will
1276 end up as an infinity or something close; that is OK. */
1278 mant_bits_left = fmt->man_len;
1279 mant_off = fmt->man_start;
1280 mpfr_set_zero (to.val, 0);
1282 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
1284 /* Don't bias NaNs. Use minimum exponent for denorms. For
1285 simplicity, we don't check for zero as the exponent doesn't matter.
1286 Note the cast to int; exp_bias is unsigned, so it's important to
1287 make sure the operation is done in signed arithmetic. */
1288 if (!special_exponent)
1289 exponent -= fmt->exp_bias;
1290 else if (exponent == 0)
1291 exponent = 1 - fmt->exp_bias;
1293 /* Build the result algebraically. Might go infinite, underflow, etc;
1294 who cares. */
1296 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
1297 increment the exponent by one to account for the integer bit. */
1299 if (!special_exponent)
1301 if (fmt->intbit == floatformat_intbit_no)
1302 mpfr_set_ui_2exp (to.val, 1, exponent, MPFR_RNDN);
1303 else
1304 exponent++;
1307 gdb_mpfr tmp (to);
1309 while (mant_bits_left > 0)
1311 mant_bits = std::min (mant_bits_left, 32);
1313 mant = get_field (from, order, fmt->totalsize, mant_off, mant_bits);
1315 mpfr_set_ui (tmp.val, mant, MPFR_RNDN);
1316 mpfr_mul_2si (tmp.val, tmp.val, exponent - mant_bits, MPFR_RNDN);
1317 mpfr_add (to.val, to.val, tmp.val, MPFR_RNDN);
1318 exponent -= mant_bits;
1319 mant_off += mant_bits;
1320 mant_bits_left -= mant_bits;
1323 /* Negate it if negative. */
1324 if (get_field (from, order, fmt->totalsize, fmt->sign_start, 1))
1325 mpfr_neg (to.val, to.val, MPFR_RNDN);
1328 void
1329 mpfr_float_ops::from_target (const struct type *type,
1330 const gdb_byte *from, gdb_mpfr &to) const
1332 from_target (floatformat_from_type (type), from, to);
1335 void
1336 mpfr_float_ops::to_target (const struct floatformat *fmt,
1337 const gdb_mpfr &from, gdb_byte *orig_to) const
1339 unsigned char *to = orig_to;
1340 mpfr_exp_t exponent;
1341 unsigned int mant_bits, mant_off;
1342 int mant_bits_left;
1343 enum floatformat_byteorders order = fmt->byteorder;
1344 unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
1346 if (order != floatformat_little)
1347 order = floatformat_big;
1349 if (order != fmt->byteorder)
1350 to = newto;
1352 memset (to, 0, floatformat_totalsize_bytes (fmt));
1354 if (fmt->split_half)
1356 gdb_mpfr top (from), bot (from);
1358 mpfr_set (top.val, from.val, MPFR_RNDN);
1359 /* If the rounded top half is Inf, the bottom must be 0 not NaN
1360 or Inf. */
1361 if (mpfr_inf_p (top.val))
1362 mpfr_set_zero (bot.val, 0);
1363 else
1364 mpfr_sub (bot.val, from.val, top.val, MPFR_RNDN);
1366 to_target (fmt->split_half, top, to);
1367 to_target (fmt->split_half, bot,
1368 to + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2);
1369 return;
1372 gdb_mpfr tmp (from);
1374 if (mpfr_zero_p (from.val))
1375 goto finalize_byteorder; /* Result is zero */
1377 mpfr_set (tmp.val, from.val, MPFR_RNDN);
1379 if (mpfr_nan_p (tmp.val)) /* Result is NaN */
1381 /* From is NaN */
1382 put_field (to, order, fmt->totalsize, fmt->exp_start,
1383 fmt->exp_len, fmt->exp_nan);
1384 /* Be sure it's not infinity, but NaN value is irrel. */
1385 put_field (to, order, fmt->totalsize, fmt->man_start,
1386 fmt->man_len, 1);
1387 goto finalize_byteorder;
1390 /* If negative, set the sign bit. */
1391 if (mpfr_sgn (tmp.val) < 0)
1393 put_field (to, order, fmt->totalsize, fmt->sign_start, 1, 1);
1394 mpfr_neg (tmp.val, tmp.val, MPFR_RNDN);
1397 if (mpfr_inf_p (tmp.val)) /* Result is Infinity. */
1399 /* Infinity exponent is same as NaN's. */
1400 put_field (to, order, fmt->totalsize, fmt->exp_start,
1401 fmt->exp_len, fmt->exp_nan);
1402 /* Infinity mantissa is all zeroes. */
1403 put_field (to, order, fmt->totalsize, fmt->man_start,
1404 fmt->man_len, 0);
1405 goto finalize_byteorder;
1408 mpfr_frexp (&exponent, tmp.val, tmp.val, MPFR_RNDN);
1410 if (exponent + fmt->exp_bias <= 0)
1412 /* The value is too small to be expressed in the destination
1413 type (not enough bits in the exponent. Treat as 0. */
1414 put_field (to, order, fmt->totalsize, fmt->exp_start,
1415 fmt->exp_len, 0);
1416 put_field (to, order, fmt->totalsize, fmt->man_start,
1417 fmt->man_len, 0);
1418 goto finalize_byteorder;
1421 if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
1423 /* The value is too large to fit into the destination.
1424 Treat as infinity. */
1425 put_field (to, order, fmt->totalsize, fmt->exp_start,
1426 fmt->exp_len, fmt->exp_nan);
1427 put_field (to, order, fmt->totalsize, fmt->man_start,
1428 fmt->man_len, 0);
1429 goto finalize_byteorder;
1432 put_field (to, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
1433 exponent + fmt->exp_bias - 1);
1435 mant_bits_left = fmt->man_len;
1436 mant_off = fmt->man_start;
1437 while (mant_bits_left > 0)
1439 unsigned long mant_long;
1441 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
1443 mpfr_mul_2ui (tmp.val, tmp.val, 32, MPFR_RNDN);
1444 mant_long = mpfr_get_ui (tmp.val, MPFR_RNDZ) & 0xffffffffL;
1445 mpfr_sub_ui (tmp.val, tmp.val, mant_long, MPFR_RNDZ);
1447 /* If the integer bit is implicit, then we need to discard it.
1448 If we are discarding a zero, we should be (but are not) creating
1449 a denormalized number which means adjusting the exponent
1450 (I think). */
1451 if (mant_bits_left == fmt->man_len
1452 && fmt->intbit == floatformat_intbit_no)
1454 mant_long <<= 1;
1455 mant_long &= 0xffffffffL;
1456 /* If we are processing the top 32 mantissa bits of a doublest
1457 so as to convert to a float value with implied integer bit,
1458 we will only be putting 31 of those 32 bits into the
1459 final value due to the discarding of the top bit. In the
1460 case of a small float value where the number of mantissa
1461 bits is less than 32, discarding the top bit does not alter
1462 the number of bits we will be adding to the result. */
1463 if (mant_bits == 32)
1464 mant_bits -= 1;
1467 if (mant_bits < 32)
1469 /* The bits we want are in the most significant MANT_BITS bits of
1470 mant_long. Move them to the least significant. */
1471 mant_long >>= 32 - mant_bits;
1474 put_field (to, order, fmt->totalsize,
1475 mant_off, mant_bits, mant_long);
1476 mant_off += mant_bits;
1477 mant_bits_left -= mant_bits;
1480 finalize_byteorder:
1481 /* Do we need to byte-swap the words in the result? */
1482 if (order != fmt->byteorder)
1483 floatformat_normalize_byteorder (fmt, newto, orig_to);
1486 void
1487 mpfr_float_ops::to_target (const struct type *type,
1488 const gdb_mpfr &from, gdb_byte *to) const
1490 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1491 memset (to, 0, TYPE_LENGTH (type));
1493 to_target (floatformat_from_type (type), from, to);
1496 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1497 to a string, optionally using the print format FORMAT. */
1498 std::string
1499 mpfr_float_ops::to_string (const gdb_byte *addr,
1500 const struct type *type,
1501 const char *format) const
1503 const struct floatformat *fmt = floatformat_from_type (type);
1505 /* Unless we need to adhere to a specific format, provide special
1506 output for certain cases. */
1507 if (format == nullptr)
1509 /* Detect invalid representations. */
1510 if (!floatformat_is_valid (fmt, addr))
1511 return "<invalid float value>";
1513 /* Handle NaN and Inf. */
1514 enum float_kind kind = floatformat_classify (fmt, addr);
1515 if (kind == float_nan)
1517 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
1518 const char *mantissa = floatformat_mantissa (fmt, addr);
1519 return string_printf ("%snan(0x%s)", sign, mantissa);
1521 else if (kind == float_infinite)
1523 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
1524 return string_printf ("%sinf", sign);
1528 /* Determine the format string to use on the host side. */
1529 std::string host_format = floatformat_printf_format (fmt, format, 'R');
1531 gdb_mpfr tmp (type);
1532 from_target (type, addr, tmp);
1534 int size = mpfr_snprintf (NULL, 0, host_format.c_str (), tmp.val);
1535 std::string str (size, '\0');
1536 mpfr_sprintf (&str[0], host_format.c_str (), tmp.val);
1538 return str;
1541 /* Parse string STRING into a target floating-number of type TYPE and
1542 store it as byte-stream ADDR. Return whether parsing succeeded. */
1543 bool
1544 mpfr_float_ops::from_string (gdb_byte *addr,
1545 const struct type *type,
1546 const std::string &in) const
1548 gdb_mpfr tmp (type);
1550 char *endptr;
1551 mpfr_strtofr (tmp.val, in.c_str (), &endptr, 0, MPFR_RNDN);
1553 /* We only accept the whole string. */
1554 if (*endptr)
1555 return false;
1557 to_target (type, tmp, addr);
1558 return true;
1561 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1562 to an integer value (rounding towards zero). */
1563 LONGEST
1564 mpfr_float_ops::to_longest (const gdb_byte *addr,
1565 const struct type *type) const
1567 gdb_mpfr tmp (type);
1568 from_target (type, addr, tmp);
1569 return mpfr_get_sj (tmp.val, MPFR_RNDZ);
1572 /* Convert signed integer VAL to a target floating-number of type TYPE
1573 and store it as byte-stream ADDR. */
1574 void
1575 mpfr_float_ops::from_longest (gdb_byte *addr,
1576 const struct type *type,
1577 LONGEST val) const
1579 gdb_mpfr tmp (type);
1580 mpfr_set_sj (tmp.val, val, MPFR_RNDN);
1581 to_target (type, tmp, addr);
1584 /* Convert unsigned integer VAL to a target floating-number of type TYPE
1585 and store it as byte-stream ADDR. */
1586 void
1587 mpfr_float_ops::from_ulongest (gdb_byte *addr,
1588 const struct type *type,
1589 ULONGEST val) const
1591 gdb_mpfr tmp (type);
1592 mpfr_set_uj (tmp.val, val, MPFR_RNDN);
1593 to_target (type, tmp, addr);
1596 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1597 to a floating-point value in the host "double" format. */
1598 double
1599 mpfr_float_ops::to_host_double (const gdb_byte *addr,
1600 const struct type *type) const
1602 gdb_mpfr tmp (type);
1603 from_target (type, addr, tmp);
1604 return mpfr_get_d (tmp.val, MPFR_RNDN);
1607 /* Convert floating-point value VAL in the host "double" format to a target
1608 floating-number of type TYPE and store it as byte-stream ADDR. */
1609 void
1610 mpfr_float_ops::from_host_double (gdb_byte *addr,
1611 const struct type *type,
1612 double val) const
1614 gdb_mpfr tmp (type);
1615 mpfr_set_d (tmp.val, val, MPFR_RNDN);
1616 to_target (type, tmp, addr);
1619 /* Convert a floating-point number of type FROM_TYPE from the target
1620 byte-stream FROM to a floating-point number of type TO_TYPE, and
1621 store it to the target byte-stream TO. */
1622 void
1623 mpfr_float_ops::convert (const gdb_byte *from,
1624 const struct type *from_type,
1625 gdb_byte *to,
1626 const struct type *to_type) const
1628 gdb_mpfr from_tmp (from_type), to_tmp (to_type);
1629 from_target (from_type, from, from_tmp);
1630 mpfr_set (to_tmp.val, from_tmp.val, MPFR_RNDN);
1631 to_target (to_type, to_tmp, to);
1634 /* Perform the binary operation indicated by OPCODE, using as operands the
1635 target byte streams X and Y, interpreted as floating-point numbers of
1636 types TYPE_X and TYPE_Y, respectively. Convert the result to type
1637 TYPE_RES and store it into the byte-stream RES. */
1638 void
1639 mpfr_float_ops::binop (enum exp_opcode op,
1640 const gdb_byte *x, const struct type *type_x,
1641 const gdb_byte *y, const struct type *type_y,
1642 gdb_byte *res, const struct type *type_res) const
1644 gdb_mpfr x_tmp (type_x), y_tmp (type_y), tmp (type_res);
1646 from_target (type_x, x, x_tmp);
1647 from_target (type_y, y, y_tmp);
1649 switch (op)
1651 case BINOP_ADD:
1652 mpfr_add (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1653 break;
1655 case BINOP_SUB:
1656 mpfr_sub (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1657 break;
1659 case BINOP_MUL:
1660 mpfr_mul (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1661 break;
1663 case BINOP_DIV:
1664 mpfr_div (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1665 break;
1667 case BINOP_EXP:
1668 mpfr_pow (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1669 break;
1671 case BINOP_MIN:
1672 mpfr_min (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1673 break;
1675 case BINOP_MAX:
1676 mpfr_max (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1677 break;
1679 default:
1680 error (_("Integer-only operation on floating point number."));
1681 break;
1684 to_target (type_res, tmp, res);
1687 /* Compare the two target byte streams X and Y, interpreted as floating-point
1688 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
1689 are equal, -1 if X is less than Y, and 1 otherwise. */
1691 mpfr_float_ops::compare (const gdb_byte *x, const struct type *type_x,
1692 const gdb_byte *y, const struct type *type_y) const
1694 gdb_mpfr x_tmp (type_x), y_tmp (type_y);
1696 from_target (type_x, x, x_tmp);
1697 from_target (type_y, y, y_tmp);
1699 if (mpfr_equal_p (x_tmp.val, y_tmp.val))
1700 return 0;
1701 else if (mpfr_less_p (x_tmp.val, y_tmp.val))
1702 return -1;
1703 else
1704 return 1;
1707 #endif
1710 /* Helper routines operating on decimal floating-point data. */
1712 /* Decimal floating point is one of the extension to IEEE 754, which is
1713 described in http://grouper.ieee.org/groups/754/revision.html and
1714 http://www2.hursley.ibm.com/decimal/. It completes binary floating
1715 point by representing floating point more exactly. */
1717 /* The order of the following headers is important for making sure
1718 decNumber structure is large enough to hold decimal128 digits. */
1720 #include "dpd/decimal128.h"
1721 #include "dpd/decimal64.h"
1722 #include "dpd/decimal32.h"
1724 /* When using decimal128, this is the maximum string length + 1
1725 (value comes from libdecnumber's DECIMAL128_String constant). */
1726 #define MAX_DECIMAL_STRING 43
1728 /* In GDB, we are using an array of gdb_byte to represent decimal values.
1729 They are stored in host byte order. This routine does the conversion if
1730 the target byte order is different. */
1731 static void
1732 match_endianness (const gdb_byte *from, const struct type *type, gdb_byte *to)
1734 gdb_assert (TYPE_CODE (type) == TYPE_CODE_DECFLOAT);
1736 int len = TYPE_LENGTH (type);
1737 int i;
1739 #if WORDS_BIGENDIAN
1740 #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE
1741 #else
1742 #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
1743 #endif
1745 if (gdbarch_byte_order (get_type_arch (type)) == OPPOSITE_BYTE_ORDER)
1746 for (i = 0; i < len; i++)
1747 to[i] = from[len - i - 1];
1748 else
1749 for (i = 0; i < len; i++)
1750 to[i] = from[i];
1752 return;
1755 /* Helper function to get the appropriate libdecnumber context for each size
1756 of decimal float. */
1757 static void
1758 set_decnumber_context (decContext *ctx, const struct type *type)
1760 gdb_assert (TYPE_CODE (type) == TYPE_CODE_DECFLOAT);
1762 switch (TYPE_LENGTH (type))
1764 case 4:
1765 decContextDefault (ctx, DEC_INIT_DECIMAL32);
1766 break;
1767 case 8:
1768 decContextDefault (ctx, DEC_INIT_DECIMAL64);
1769 break;
1770 case 16:
1771 decContextDefault (ctx, DEC_INIT_DECIMAL128);
1772 break;
1775 ctx->traps = 0;
1778 /* Check for errors signaled in the decimal context structure. */
1779 static void
1780 decimal_check_errors (decContext *ctx)
1782 /* An error here could be a division by zero, an overflow, an underflow or
1783 an invalid operation (from the DEC_Errors constant in decContext.h).
1784 Since GDB doesn't complain about division by zero, overflow or underflow
1785 errors for binary floating, we won't complain about them for decimal
1786 floating either. */
1787 if (ctx->status & DEC_IEEE_854_Invalid_operation)
1789 /* Leave only the error bits in the status flags. */
1790 ctx->status &= DEC_IEEE_854_Invalid_operation;
1791 error (_("Cannot perform operation: %s"),
1792 decContextStatusToString (ctx));
1796 /* Helper function to convert from libdecnumber's appropriate representation
1797 for computation to each size of decimal float. */
1798 static void
1799 decimal_from_number (const decNumber *from,
1800 gdb_byte *to, const struct type *type)
1802 gdb_byte dec[16];
1804 decContext set;
1806 set_decnumber_context (&set, type);
1808 switch (TYPE_LENGTH (type))
1810 case 4:
1811 decimal32FromNumber ((decimal32 *) dec, from, &set);
1812 break;
1813 case 8:
1814 decimal64FromNumber ((decimal64 *) dec, from, &set);
1815 break;
1816 case 16:
1817 decimal128FromNumber ((decimal128 *) dec, from, &set);
1818 break;
1819 default:
1820 error (_("Unknown decimal floating point type."));
1821 break;
1824 match_endianness (dec, type, to);
1827 /* Helper function to convert each size of decimal float to libdecnumber's
1828 appropriate representation for computation. */
1829 static void
1830 decimal_to_number (const gdb_byte *addr, const struct type *type,
1831 decNumber *to)
1833 gdb_byte dec[16];
1834 match_endianness (addr, type, dec);
1836 switch (TYPE_LENGTH (type))
1838 case 4:
1839 decimal32ToNumber ((decimal32 *) dec, to);
1840 break;
1841 case 8:
1842 decimal64ToNumber ((decimal64 *) dec, to);
1843 break;
1844 case 16:
1845 decimal128ToNumber ((decimal128 *) dec, to);
1846 break;
1847 default:
1848 error (_("Unknown decimal floating point type."));
1849 break;
1853 /* Returns true if ADDR (which is of type TYPE) is the number zero. */
1854 static bool
1855 decimal_is_zero (const gdb_byte *addr, const struct type *type)
1857 decNumber number;
1859 decimal_to_number (addr, type, &number);
1861 return decNumberIsZero (&number);
1865 /* Implementation of target_float_ops using the libdecnumber decNumber type
1866 as intermediate format. */
1868 class decimal_float_ops : public target_float_ops
1870 public:
1871 std::string to_string (const gdb_byte *addr, const struct type *type,
1872 const char *format) const override;
1873 bool from_string (gdb_byte *addr, const struct type *type,
1874 const std::string &string) const override;
1876 LONGEST to_longest (const gdb_byte *addr,
1877 const struct type *type) const override;
1878 void from_longest (gdb_byte *addr, const struct type *type,
1879 LONGEST val) const override;
1880 void from_ulongest (gdb_byte *addr, const struct type *type,
1881 ULONGEST val) const override;
1882 double to_host_double (const gdb_byte *addr,
1883 const struct type *type) const override
1885 /* We don't support conversions between target decimal floating-point
1886 types and the host double type. */
1887 gdb_assert_not_reached ("invalid operation on decimal float");
1889 void from_host_double (gdb_byte *addr, const struct type *type,
1890 double val) const override
1892 /* We don't support conversions between target decimal floating-point
1893 types and the host double type. */
1894 gdb_assert_not_reached ("invalid operation on decimal float");
1896 void convert (const gdb_byte *from, const struct type *from_type,
1897 gdb_byte *to, const struct type *to_type) const override;
1899 void binop (enum exp_opcode opcode,
1900 const gdb_byte *x, const struct type *type_x,
1901 const gdb_byte *y, const struct type *type_y,
1902 gdb_byte *res, const struct type *type_res) const override;
1903 int compare (const gdb_byte *x, const struct type *type_x,
1904 const gdb_byte *y, const struct type *type_y) const override;
1907 /* Convert decimal type to its string representation. LEN is the length
1908 of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
1909 16 bytes for decimal128. */
1910 std::string
1911 decimal_float_ops::to_string (const gdb_byte *addr, const struct type *type,
1912 const char *format = nullptr) const
1914 gdb_byte dec[16];
1916 match_endianness (addr, type, dec);
1918 if (format != nullptr)
1920 /* We don't handle format strings (yet). If the host printf supports
1921 decimal floating point types, just use this. Otherwise, fall back
1922 to printing the number while ignoring the format string. */
1923 #if defined (PRINTF_HAS_DECFLOAT)
1924 /* FIXME: This makes unwarranted assumptions about the host ABI! */
1925 return string_printf (format, dec);
1926 #endif
1929 std::string result;
1930 result.resize (MAX_DECIMAL_STRING);
1932 switch (TYPE_LENGTH (type))
1934 case 4:
1935 decimal32ToString ((decimal32 *) dec, &result[0]);
1936 break;
1937 case 8:
1938 decimal64ToString ((decimal64 *) dec, &result[0]);
1939 break;
1940 case 16:
1941 decimal128ToString ((decimal128 *) dec, &result[0]);
1942 break;
1943 default:
1944 error (_("Unknown decimal floating point type."));
1945 break;
1948 return result;
1951 /* Convert the string form of a decimal value to its decimal representation.
1952 LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
1953 decimal64 and 16 bytes for decimal128. */
1954 bool
1955 decimal_float_ops::from_string (gdb_byte *addr, const struct type *type,
1956 const std::string &string) const
1958 decContext set;
1959 gdb_byte dec[16];
1961 set_decnumber_context (&set, type);
1963 switch (TYPE_LENGTH (type))
1965 case 4:
1966 decimal32FromString ((decimal32 *) dec, string.c_str (), &set);
1967 break;
1968 case 8:
1969 decimal64FromString ((decimal64 *) dec, string.c_str (), &set);
1970 break;
1971 case 16:
1972 decimal128FromString ((decimal128 *) dec, string.c_str (), &set);
1973 break;
1974 default:
1975 error (_("Unknown decimal floating point type."));
1976 break;
1979 match_endianness (dec, type, addr);
1981 /* Check for errors in the DFP operation. */
1982 decimal_check_errors (&set);
1984 return true;
1987 /* Converts a LONGEST to a decimal float of specified LEN bytes. */
1988 void
1989 decimal_float_ops::from_longest (gdb_byte *addr, const struct type *type,
1990 LONGEST from) const
1992 decNumber number;
1994 if ((int32_t) from != from)
1995 /* libdecnumber can convert only 32-bit integers. */
1996 error (_("Conversion of large integer to a "
1997 "decimal floating type is not supported."));
1999 decNumberFromInt32 (&number, (int32_t) from);
2001 decimal_from_number (&number, addr, type);
2004 /* Converts a ULONGEST to a decimal float of specified LEN bytes. */
2005 void
2006 decimal_float_ops::from_ulongest (gdb_byte *addr, const struct type *type,
2007 ULONGEST from) const
2009 decNumber number;
2011 if ((uint32_t) from != from)
2012 /* libdecnumber can convert only 32-bit integers. */
2013 error (_("Conversion of large integer to a "
2014 "decimal floating type is not supported."));
2016 decNumberFromUInt32 (&number, (uint32_t) from);
2018 decimal_from_number (&number, addr, type);
2021 /* Converts a decimal float of LEN bytes to a LONGEST. */
2022 LONGEST
2023 decimal_float_ops::to_longest (const gdb_byte *addr,
2024 const struct type *type) const
2026 /* libdecnumber has a function to convert from decimal to integer, but
2027 it doesn't work when the decimal number has a fractional part. */
2028 std::string str = to_string (addr, type);
2029 return strtoll (str.c_str (), NULL, 10);
2032 /* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
2033 and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
2034 RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */
2035 void
2036 decimal_float_ops::binop (enum exp_opcode op,
2037 const gdb_byte *x, const struct type *type_x,
2038 const gdb_byte *y, const struct type *type_y,
2039 gdb_byte *res, const struct type *type_res) const
2041 decContext set;
2042 decNumber number1, number2, number3;
2044 decimal_to_number (x, type_x, &number1);
2045 decimal_to_number (y, type_y, &number2);
2047 set_decnumber_context (&set, type_res);
2049 switch (op)
2051 case BINOP_ADD:
2052 decNumberAdd (&number3, &number1, &number2, &set);
2053 break;
2054 case BINOP_SUB:
2055 decNumberSubtract (&number3, &number1, &number2, &set);
2056 break;
2057 case BINOP_MUL:
2058 decNumberMultiply (&number3, &number1, &number2, &set);
2059 break;
2060 case BINOP_DIV:
2061 decNumberDivide (&number3, &number1, &number2, &set);
2062 break;
2063 case BINOP_EXP:
2064 decNumberPower (&number3, &number1, &number2, &set);
2065 break;
2066 default:
2067 error (_("Operation not valid for decimal floating point number."));
2068 break;
2071 /* Check for errors in the DFP operation. */
2072 decimal_check_errors (&set);
2074 decimal_from_number (&number3, res, type_res);
2077 /* Compares two numbers numerically. If X is less than Y then the return value
2078 will be -1. If they are equal, then the return value will be 0. If X is
2079 greater than the Y then the return value will be 1. */
2081 decimal_float_ops::compare (const gdb_byte *x, const struct type *type_x,
2082 const gdb_byte *y, const struct type *type_y) const
2084 decNumber number1, number2, result;
2085 decContext set;
2086 const struct type *type_result;
2088 decimal_to_number (x, type_x, &number1);
2089 decimal_to_number (y, type_y, &number2);
2091 /* Perform the comparison in the larger of the two sizes. */
2092 type_result = TYPE_LENGTH (type_x) > TYPE_LENGTH (type_y) ? type_x : type_y;
2093 set_decnumber_context (&set, type_result);
2095 decNumberCompare (&result, &number1, &number2, &set);
2097 /* Check for errors in the DFP operation. */
2098 decimal_check_errors (&set);
2100 if (decNumberIsNaN (&result))
2101 error (_("Comparison with an invalid number (NaN)."));
2102 else if (decNumberIsZero (&result))
2103 return 0;
2104 else if (decNumberIsNegative (&result))
2105 return -1;
2106 else
2107 return 1;
2110 /* Convert a decimal value from a decimal type with LEN_FROM bytes to a
2111 decimal type with LEN_TO bytes. */
2112 void
2113 decimal_float_ops::convert (const gdb_byte *from, const struct type *from_type,
2114 gdb_byte *to, const struct type *to_type) const
2116 decNumber number;
2118 decimal_to_number (from, from_type, &number);
2119 decimal_from_number (&number, to, to_type);
2123 /* Typed floating-point routines. These routines operate on floating-point
2124 values in target format, represented by a byte buffer interpreted as a
2125 "struct type", which may be either a binary or decimal floating-point
2126 type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */
2128 /* Return whether TYPE1 and TYPE2 are of the same category (binary or
2129 decimal floating-point). */
2130 static bool
2131 target_float_same_category_p (const struct type *type1,
2132 const struct type *type2)
2134 return TYPE_CODE (type1) == TYPE_CODE (type2);
2137 /* Return whether TYPE1 and TYPE2 use the same floating-point format. */
2138 static bool
2139 target_float_same_format_p (const struct type *type1,
2140 const struct type *type2)
2142 if (!target_float_same_category_p (type1, type2))
2143 return false;
2145 switch (TYPE_CODE (type1))
2147 case TYPE_CODE_FLT:
2148 return floatformat_from_type (type1) == floatformat_from_type (type2);
2150 case TYPE_CODE_DECFLOAT:
2151 return (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
2152 && (gdbarch_byte_order (get_type_arch (type1))
2153 == gdbarch_byte_order (get_type_arch (type2))));
2155 default:
2156 gdb_assert_not_reached ("unexpected type code");
2160 /* Return the size (without padding) of the target floating-point
2161 format used by TYPE. */
2162 static int
2163 target_float_format_length (const struct type *type)
2165 switch (TYPE_CODE (type))
2167 case TYPE_CODE_FLT:
2168 return floatformat_totalsize_bytes (floatformat_from_type (type));
2170 case TYPE_CODE_DECFLOAT:
2171 return TYPE_LENGTH (type);
2173 default:
2174 gdb_assert_not_reached ("unexpected type code");
2178 /* Identifiers of available host-side intermediate formats. These must
2179 be sorted so the that the more "general" kinds come later. */
2180 enum target_float_ops_kind
2182 /* Target binary floating-point formats that match a host format. */
2183 host_float = 0,
2184 host_double,
2185 host_long_double,
2186 /* Any other target binary floating-point format. */
2187 binary,
2188 /* Any target decimal floating-point format. */
2189 decimal
2192 /* Given a target type TYPE, choose the best host-side intermediate format
2193 to perform operations on TYPE in. */
2194 static enum target_float_ops_kind
2195 get_target_float_ops_kind (const struct type *type)
2197 switch (TYPE_CODE (type))
2199 case TYPE_CODE_FLT:
2201 const struct floatformat *fmt = floatformat_from_type (type);
2203 /* Binary floating-point formats matching a host format. */
2204 if (fmt == host_float_format)
2205 return target_float_ops_kind::host_float;
2206 if (fmt == host_double_format)
2207 return target_float_ops_kind::host_double;
2208 if (fmt == host_long_double_format)
2209 return target_float_ops_kind::host_long_double;
2211 /* Any other binary floating-point format. */
2212 return target_float_ops_kind::binary;
2215 case TYPE_CODE_DECFLOAT:
2217 /* Any decimal floating-point format. */
2218 return target_float_ops_kind::decimal;
2221 default:
2222 gdb_assert_not_reached ("unexpected type code");
2226 /* Return target_float_ops to peform operations for KIND. */
2227 static const target_float_ops *
2228 get_target_float_ops (enum target_float_ops_kind kind)
2230 switch (kind)
2232 /* If the type format matches one of the host floating-point
2233 types, use that type as intermediate format. */
2234 case target_float_ops_kind::host_float:
2236 static host_float_ops<float> host_float_ops_float;
2237 return &host_float_ops_float;
2240 case target_float_ops_kind::host_double:
2242 static host_float_ops<double> host_float_ops_double;
2243 return &host_float_ops_double;
2246 case target_float_ops_kind::host_long_double:
2248 static host_float_ops<long double> host_float_ops_long_double;
2249 return &host_float_ops_long_double;
2252 /* For binary floating-point formats that do not match any host format,
2253 use mpfr_t as intermediate format to provide precise target-floating
2254 point emulation. However, if the MPFR library is not availabe,
2255 use the largest host floating-point type as intermediate format. */
2256 case target_float_ops_kind::binary:
2258 #ifdef HAVE_LIBMPFR
2259 static mpfr_float_ops binary_float_ops;
2260 #else
2261 static host_float_ops<long double> binary_float_ops;
2262 #endif
2263 return &binary_float_ops;
2266 /* For decimal floating-point types, always use the libdecnumber
2267 decNumber type as intermediate format. */
2268 case target_float_ops_kind::decimal:
2270 static decimal_float_ops decimal_float_ops;
2271 return &decimal_float_ops;
2274 default:
2275 gdb_assert_not_reached ("unexpected target_float_ops_kind");
2279 /* Given a target type TYPE, determine the best host-side intermediate format
2280 to perform operations on TYPE in. */
2281 static const target_float_ops *
2282 get_target_float_ops (const struct type *type)
2284 enum target_float_ops_kind kind = get_target_float_ops_kind (type);
2285 return get_target_float_ops (kind);
2288 /* The same for operations involving two target types TYPE1 and TYPE2. */
2289 static const target_float_ops *
2290 get_target_float_ops (const struct type *type1, const struct type *type2)
2292 gdb_assert (TYPE_CODE (type1) == TYPE_CODE (type2));
2294 enum target_float_ops_kind kind1 = get_target_float_ops_kind (type1);
2295 enum target_float_ops_kind kind2 = get_target_float_ops_kind (type2);
2297 /* Given the way the kinds are sorted, we simply choose the larger one;
2298 this will be able to hold values of either type. */
2299 return get_target_float_ops (std::max (kind1, kind2));
2302 /* Return whether the byte-stream ADDR holds a valid value of
2303 floating-point type TYPE. */
2304 bool
2305 target_float_is_valid (const gdb_byte *addr, const struct type *type)
2307 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2308 return floatformat_is_valid (floatformat_from_type (type), addr);
2310 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2311 return true;
2313 gdb_assert_not_reached ("unexpected type code");
2316 /* Return whether the byte-stream ADDR, interpreted as floating-point
2317 type TYPE, is numerically equal to zero (of either sign). */
2318 bool
2319 target_float_is_zero (const gdb_byte *addr, const struct type *type)
2321 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2322 return (floatformat_classify (floatformat_from_type (type), addr)
2323 == float_zero);
2325 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2326 return decimal_is_zero (addr, type);
2328 gdb_assert_not_reached ("unexpected type code");
2331 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
2332 to a string, optionally using the print format FORMAT. */
2333 std::string
2334 target_float_to_string (const gdb_byte *addr, const struct type *type,
2335 const char *format)
2337 /* Unless we need to adhere to a specific format, provide special
2338 output for special cases of binary floating-point numbers. */
2339 if (format == nullptr && TYPE_CODE (type) == TYPE_CODE_FLT)
2341 const struct floatformat *fmt = floatformat_from_type (type);
2343 /* Detect invalid representations. */
2344 if (!floatformat_is_valid (fmt, addr))
2345 return "<invalid float value>";
2347 /* Handle NaN and Inf. */
2348 enum float_kind kind = floatformat_classify (fmt, addr);
2349 if (kind == float_nan)
2351 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
2352 const char *mantissa = floatformat_mantissa (fmt, addr);
2353 return string_printf ("%snan(0x%s)", sign, mantissa);
2355 else if (kind == float_infinite)
2357 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
2358 return string_printf ("%sinf", sign);
2362 const target_float_ops *ops = get_target_float_ops (type);
2363 return ops->to_string (addr, type, format);
2366 /* Parse string STRING into a target floating-number of type TYPE and
2367 store it as byte-stream ADDR. Return whether parsing succeeded. */
2368 bool
2369 target_float_from_string (gdb_byte *addr, const struct type *type,
2370 const std::string &string)
2372 const target_float_ops *ops = get_target_float_ops (type);
2373 return ops->from_string (addr, type, string);
2376 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
2377 to an integer value (rounding towards zero). */
2378 LONGEST
2379 target_float_to_longest (const gdb_byte *addr, const struct type *type)
2381 const target_float_ops *ops = get_target_float_ops (type);
2382 return ops->to_longest (addr, type);
2385 /* Convert signed integer VAL to a target floating-number of type TYPE
2386 and store it as byte-stream ADDR. */
2387 void
2388 target_float_from_longest (gdb_byte *addr, const struct type *type,
2389 LONGEST val)
2391 const target_float_ops *ops = get_target_float_ops (type);
2392 ops->from_longest (addr, type, val);
2395 /* Convert unsigned integer VAL to a target floating-number of type TYPE
2396 and store it as byte-stream ADDR. */
2397 void
2398 target_float_from_ulongest (gdb_byte *addr, const struct type *type,
2399 ULONGEST val)
2401 const target_float_ops *ops = get_target_float_ops (type);
2402 ops->from_ulongest (addr, type, val);
2405 /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
2406 to a floating-point value in the host "double" format. */
2407 double
2408 target_float_to_host_double (const gdb_byte *addr,
2409 const struct type *type)
2411 const target_float_ops *ops = get_target_float_ops (type);
2412 return ops->to_host_double (addr, type);
2415 /* Convert floating-point value VAL in the host "double" format to a target
2416 floating-number of type TYPE and store it as byte-stream ADDR. */
2417 void
2418 target_float_from_host_double (gdb_byte *addr, const struct type *type,
2419 double val)
2421 const target_float_ops *ops = get_target_float_ops (type);
2422 ops->from_host_double (addr, type, val);
2425 /* Convert a floating-point number of type FROM_TYPE from the target
2426 byte-stream FROM to a floating-point number of type TO_TYPE, and
2427 store it to the target byte-stream TO. */
2428 void
2429 target_float_convert (const gdb_byte *from, const struct type *from_type,
2430 gdb_byte *to, const struct type *to_type)
2432 /* We cannot directly convert between binary and decimal floating-point
2433 types, so go via an intermediary string. */
2434 if (!target_float_same_category_p (from_type, to_type))
2436 std::string str = target_float_to_string (from, from_type);
2437 target_float_from_string (to, to_type, str);
2438 return;
2441 /* Convert between two different formats in the same category. */
2442 if (!target_float_same_format_p (from_type, to_type))
2444 const target_float_ops *ops = get_target_float_ops (from_type, to_type);
2445 ops->convert (from, from_type, to, to_type);
2446 return;
2449 /* The floating-point formats match, so we simply copy the data, ensuring
2450 possible padding bytes in the target buffer are zeroed out. */
2451 memset (to, 0, TYPE_LENGTH (to_type));
2452 memcpy (to, from, target_float_format_length (to_type));
2455 /* Perform the binary operation indicated by OPCODE, using as operands the
2456 target byte streams X and Y, interpreted as floating-point numbers of
2457 types TYPE_X and TYPE_Y, respectively. Convert the result to type
2458 TYPE_RES and store it into the byte-stream RES.
2460 The three types must either be all binary floating-point types, or else
2461 all decimal floating-point types. Binary and decimal floating-point
2462 types cannot be mixed within a single operation. */
2463 void
2464 target_float_binop (enum exp_opcode opcode,
2465 const gdb_byte *x, const struct type *type_x,
2466 const gdb_byte *y, const struct type *type_y,
2467 gdb_byte *res, const struct type *type_res)
2469 gdb_assert (target_float_same_category_p (type_x, type_res));
2470 gdb_assert (target_float_same_category_p (type_y, type_res));
2472 const target_float_ops *ops = get_target_float_ops (type_x, type_y);
2473 ops->binop (opcode, x, type_x, y, type_y, res, type_res);
2476 /* Compare the two target byte streams X and Y, interpreted as floating-point
2477 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
2478 are equal, -1 if X is less than Y, and 1 otherwise.
2480 The two types must either both be binary floating-point types, or else
2481 both be decimal floating-point types. Binary and decimal floating-point
2482 types cannot compared directly against each other. */
2484 target_float_compare (const gdb_byte *x, const struct type *type_x,
2485 const gdb_byte *y, const struct type *type_y)
2487 gdb_assert (target_float_same_category_p (type_x, type_y));
2489 const target_float_ops *ops = get_target_float_ops (type_x, type_y);
2490 return ops->compare (x, type_x, y, type_y);