Target FP: Make use of MPFR if available
[deliverable/binutils-gdb.git] / gdb / target-float.c
CommitLineData
70100014
UW
1/* Floating point routines for GDB, the GNU debugger.
2
3 Copyright (C) 2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
70100014
UW
21#include "gdbtypes.h"
22#include "floatformat.h"
23#include "target-float.h"
24
25
7a26362d 26/* Target floating-point operations.
50637b26 27
7a26362d
UW
28 We provide multiple implementations of those operations, which differ
29 by the host-side intermediate format they perform computations in.
66c02b9e 30
7a26362d
UW
31 Those multiple implementations all derive from the following abstract
32 base class, which specifies the set of operations to be implemented. */
33
34class target_float_ops
35{
36public:
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;
41
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;
54
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;
61};
62
63
64/* Helper routines operating on binary floating-point data. */
65
66#include <cmath>
67#include <limits>
1cfb73db
UW
68
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). */
72enum float_kind {
73 float_nan,
74 float_infinite,
75 float_zero,
76 float_normal,
77 float_subnormal
78};
79
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
84
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
88
89/* Return the floatformat's total size in host bytes. */
90static size_t
91floatformat_totalsize_bytes (const struct floatformat *fmt)
92{
93 return ((fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1)
94 / FLOATFORMAT_CHAR_BIT);
95}
96
97/* Return the precision of the floating point format FMT. */
98static int
99floatformat_precision (const struct floatformat *fmt)
100{
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);
105
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++;
111
112 return prec;
113}
114
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. */
118static enum floatformat_byteorders
119floatformat_normalize_byteorder (const struct floatformat *fmt,
120 const void *from, void *to)
121{
122 const unsigned char *swapin;
123 unsigned char *swapout;
124 int words;
125
126 if (fmt->byteorder == floatformat_little
127 || fmt->byteorder == floatformat_big)
128 return fmt->byteorder;
129
130 words = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
131 words >>= 2;
132
133 swapout = (unsigned char *)to;
134 swapin = (const unsigned char *)from;
135
136 if (fmt->byteorder == floatformat_vax)
137 {
138 while (words-- > 0)
139 {
140 *swapout++ = swapin[1];
141 *swapout++ = swapin[0];
142 *swapout++ = swapin[3];
143 *swapout++ = swapin[2];
144 swapin += 4;
145 }
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;
149 }
150 else
151 {
152 gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword);
153
154 while (words-- > 0)
155 {
156 *swapout++ = swapin[3];
157 *swapout++ = swapin[2];
158 *swapout++ = swapin[1];
159 *swapout++ = swapin[0];
160 swapin += 4;
161 }
162 return floatformat_big;
163 }
164}
165
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. */
168static unsigned long
169get_field (const bfd_byte *data, enum floatformat_byteorders order,
170 unsigned int total_len, unsigned int start, unsigned int len)
171{
172 unsigned long result;
173 unsigned int cur_byte;
174 int cur_bitshift;
175
176 /* Caller must byte-swap words before calling this routine. */
177 gdb_assert (order == floatformat_little || order == floatformat_big);
178
179 /* Start at the least significant part of the field. */
180 if (order == floatformat_little)
181 {
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);
190
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;
195 }
196 else
197 {
198 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
199 cur_bitshift =
200 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
201 }
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;
211
212 /* Move towards the most significant part of the field. */
213 while (cur_bitshift < len)
214 {
215 result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
216 cur_bitshift += FLOATFORMAT_CHAR_BIT;
217 switch (order)
218 {
219 case floatformat_little:
220 ++cur_byte;
221 break;
222 case floatformat_big:
223 --cur_byte;
224 break;
225 }
226 }
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;
231}
232
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. */
235static void
236put_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)
239{
240 unsigned int cur_byte;
241 int cur_bitshift;
242
243 /* Caller must byte-swap words before calling this routine. */
244 gdb_assert (order == floatformat_little || order == floatformat_big);
245
246 /* Start at the least significant part of the field. */
247 if (order == floatformat_little)
248 {
249 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
250
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;
255 }
256 else
257 {
258 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
259 cur_bitshift =
260 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
261 }
262 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
263 {
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);
269 }
270 cur_bitshift += FLOATFORMAT_CHAR_BIT;
271 if (order == floatformat_little)
272 ++cur_byte;
273 else
274 --cur_byte;
275
276 /* Move towards the most significant part of the field. */
277 while (cur_bitshift < len)
278 {
279 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
280 {
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);
285 }
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;
294 }
295}
296
297/* Check if VAL (which is assumed to be a floating point number whose
298 format is described by FMT) is negative. */
299static int
300floatformat_is_negative (const struct floatformat *fmt,
301 const bfd_byte *uval)
302{
303 enum floatformat_byteorders order;
304 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
305
306 gdb_assert (fmt != NULL);
307 gdb_assert (fmt->totalsize
308 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
309
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;
314
315 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
316
317 if (order != fmt->byteorder)
318 uval = newfrom;
319
320 return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1);
321}
322
323/* Check if VAL is "not a number" (NaN) for FMT. */
324static enum float_kind
325floatformat_classify (const struct floatformat *fmt,
326 const bfd_byte *uval)
327{
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;
335
336 gdb_assert (fmt != NULL);
337 gdb_assert (fmt->totalsize
338 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
339
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;
346
347 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
348
349 if (order != fmt->byteorder)
350 uval = newfrom;
351
352 exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start,
353 fmt->exp_len);
354
355 mant_bits_left = fmt->man_len;
356 mant_off = fmt->man_start;
357
358 mant_zero = 1;
359 while (mant_bits_left > 0)
360 {
361 mant_bits = std::min (mant_bits_left, 32);
362
363 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
364
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));
369
370 if (mant)
371 {
372 mant_zero = 0;
373 break;
374 }
375
376 mant_off += mant_bits;
377 mant_bits_left -= mant_bits;
378 }
379
380 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
381 supported. */
382 if (! fmt->exp_nan)
383 {
384 if (mant_zero)
385 return float_zero;
386 else
387 return float_normal;
388 }
389
390 if (exponent == 0)
391 {
392 if (mant_zero)
393 return float_zero;
394 else
395 return float_subnormal;
396 }
397
398 if (exponent == fmt->exp_nan)
399 {
400 if (mant_zero)
401 return float_infinite;
402 else
403 return float_nan;
404 }
405
406 return float_normal;
407}
408
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. */
412static const char *
413floatformat_mantissa (const struct floatformat *fmt,
414 const bfd_byte *val)
415{
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];
425
426 gdb_assert (fmt != NULL);
427 gdb_assert (fmt->totalsize
428 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
429
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;
439
440 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
441
442 if (order != fmt->byteorder)
443 uval = newfrom;
444
445 if (! fmt->exp_nan)
446 return 0;
447
448 /* Make sure we have enough room to store the mantissa. */
449 gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2);
450
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;
454
455 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
456
457 len = xsnprintf (res, sizeof res, "%lx", mant);
458
459 mant_off += mant_bits;
460 mant_bits_left -= mant_bits;
461
462 while (mant_bits_left > 0)
463 {
464 mant = get_field (uval, order, fmt->totalsize, mant_off, 32);
465
466 xsnprintf (buf, sizeof buf, "%08lx", mant);
467 gdb_assert (len + strlen (buf) <= sizeof res);
468 strcat (res, buf);
469
470 mant_off += 32;
471 mant_bits_left -= 32;
472 }
473
474 return res;
475}
476
7a26362d
UW
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. */
482static std::string
483floatformat_printf_format (const struct floatformat *fmt,
484 const char *format, char length)
485{
486 std::string host_format;
487 char conversion;
488
489 if (format == nullptr)
490 {
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
494
495 ceil(1 + p * log10(b)),
496
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++;
504
505 host_format = string_printf ("%%.%d", decimal_dig);
506 conversion = 'g';
507 }
508 else
509 {
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--;
519
520 host_format = std::string (format, len);
521 }
522
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;
528
529 return host_format;
530}
531
532/* Implementation of target_float_ops using the host floating-point type T
533 as intermediate type. */
534
535template<typename T> class host_float_ops : public target_float_ops
536{
537public:
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;
542
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;
555
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;
562
563private:
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;
568
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;
573};
574
575
576/* Convert TO/FROM target to the host floating-point format T.
1cfb73db
UW
577
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. */
590
591static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT;
592static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT;
593static const struct floatformat *host_long_double_format
594 = GDB_HOST_LONG_DOUBLE_FORMAT;
595
7a26362d
UW
596/* Convert target floating-point value at FROM in format FMT to host
597 floating-point format of type T. */
598template<typename T> void
599host_float_ops<T>::from_target (const struct floatformat *fmt,
600 const gdb_byte *from, T *to) const
1cfb73db
UW
601{
602 gdb_assert (fmt != NULL);
603
604 if (fmt == host_float_format)
605 {
606 float val = 0;
607
608 memcpy (&val, from, floatformat_totalsize_bytes (fmt));
609 *to = val;
610 return;
611 }
612 else if (fmt == host_double_format)
613 {
614 double val = 0;
615
616 memcpy (&val, from, floatformat_totalsize_bytes (fmt));
617 *to = val;
618 return;
619 }
620 else if (fmt == host_long_double_format)
621 {
622 long double val = 0;
623
624 memcpy (&val, from, floatformat_totalsize_bytes (fmt));
625 *to = val;
626 return;
627 }
628
629 unsigned char *ufrom = (unsigned char *) from;
7a26362d 630 T dto;
1cfb73db
UW
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;
639
640 gdb_assert (fmt->totalsize
641 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
642
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)
648 {
649 double dto;
650
651 floatformat_to_double (fmt->split_half ? fmt->split_half : fmt,
652 from, &dto);
7a26362d 653 *to = (T) dto;
1cfb73db
UW
654 return;
655 }
656
657 order = floatformat_normalize_byteorder (fmt, ufrom, newfrom);
658
659 if (order != fmt->byteorder)
660 ufrom = newfrom;
661
662 if (fmt->split_half)
663 {
7a26362d 664 T dtop, dbot;
1cfb73db 665
7a26362d 666 from_target (fmt->split_half, ufrom, &dtop);
1cfb73db
UW
667 /* Preserve the sign of 0, which is the sign of the top
668 half. */
669 if (dtop == 0.0)
670 {
671 *to = dtop;
672 return;
673 }
7a26362d
UW
674 from_target (fmt->split_half,
675 ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, &dbot);
1cfb73db
UW
676 *to = dtop + dbot;
677 return;
678 }
679
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. */
685
686 mant_bits_left = fmt->man_len;
687 mant_off = fmt->man_start;
688 dto = 0.0;
689
690 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
691
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;
700
701 /* Build the result algebraically. Might go infinite, underflow, etc;
702 who cares. */
703
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. */
706
707 if (!special_exponent)
708 {
709 if (fmt->intbit == floatformat_intbit_no)
710 dto = ldexp (1.0, exponent);
711 else
712 exponent++;
713 }
714
715 while (mant_bits_left > 0)
716 {
717 mant_bits = std::min (mant_bits_left, 32);
718
719 mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits);
720
7a26362d 721 dto += ldexp ((T) mant, exponent - mant_bits);
1cfb73db
UW
722 exponent -= mant_bits;
723 mant_off += mant_bits;
724 mant_bits_left -= mant_bits;
725 }
726
727 /* Negate it if negative. */
728 if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1))
729 dto = -dto;
730 *to = dto;
731}
732
7a26362d
UW
733template<typename T> void
734host_float_ops<T>::from_target (const struct type *type,
735 const gdb_byte *from, T *to) const
736{
737 from_target (floatformat_from_type (type), from, to);
738}
739
740/* Convert host floating-point value of type T to target floating-point
741 value in format FMT and store at TO. */
742template<typename T> void
743host_float_ops<T>::to_target (const struct floatformat *fmt,
744 const T *from, gdb_byte *to) const
1cfb73db
UW
745{
746 gdb_assert (fmt != NULL);
747
748 if (fmt == host_float_format)
749 {
750 float val = *from;
751
752 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
753 return;
754 }
755 else if (fmt == host_double_format)
756 {
757 double val = *from;
758
759 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
760 return;
761 }
762 else if (fmt == host_long_double_format)
763 {
764 long double val = *from;
765
766 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
767 return;
768 }
769
7a26362d 770 T dfrom;
1cfb73db 771 int exponent;
7a26362d 772 T mant;
1cfb73db
UW
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];
778
779 if (order != floatformat_little)
780 order = floatformat_big;
781
782 if (order != fmt->byteorder)
783 uto = newto;
784
785 memcpy (&dfrom, from, sizeof (dfrom));
786 memset (uto, 0, floatformat_totalsize_bytes (fmt));
787
788 if (fmt->split_half)
789 {
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;
7a26362d 794 T dtopnv, dbotnv;
1cfb73db
UW
795
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
7a26362d 802 dbot = (double) (dfrom - (T) dtop);
1cfb73db
UW
803 dtopnv = dtop;
804 dbotnv = dbot;
7a26362d
UW
805 to_target (fmt->split_half, &dtopnv, uto);
806 to_target (fmt->split_half, &dbotnv,
807 uto + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2);
1cfb73db
UW
808 return;
809 }
810
811 if (dfrom == 0)
812 goto finalize_byteorder; /* Result is zero */
813 if (dfrom != dfrom) /* Result is NaN */
814 {
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;
822 }
823
824 /* If negative, set the sign bit. */
825 if (dfrom < 0)
826 {
827 put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1);
828 dfrom = -dfrom;
829 }
830
831 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity. */
832 {
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;
840 }
841
1cfb73db 842 mant = frexp (dfrom, &exponent);
1cfb73db
UW
843
844 if (exponent + fmt->exp_bias <= 0)
845 {
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;
853 }
854
855 if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
856 {
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;
864 }
865
866 put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
867 exponent + fmt->exp_bias - 1);
868
869 mant_bits_left = fmt->man_len;
870 mant_off = fmt->man_start;
871 while (mant_bits_left > 0)
872 {
873 unsigned long mant_long;
874
875 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
876
877 mant *= 4294967296.0;
878 mant_long = ((unsigned long) mant) & 0xffffffffL;
879 mant -= mant_long;
880
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)
887 {
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;
899 }
900
901 if (mant_bits < 32)
902 {
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;
906 }
907
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;
912 }
913
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);
918}
919
7a26362d
UW
920template<typename T> void
921host_float_ops<T>::to_target (const struct type *type,
922 const T *from, gdb_byte *to) const
1cfb73db 923{
7a26362d
UW
924 /* Ensure possible padding bytes in the target buffer are zeroed out. */
925 memset (to, 0, TYPE_LENGTH (type));
1cfb73db 926
7a26362d
UW
927 to_target (floatformat_from_type (type), from, to);
928}
1cfb73db 929
7a26362d
UW
930/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
931 to a string, optionally using the print format FORMAT. */
932template<typename T> struct printf_length_modifier
933{
934 static constexpr char value = 0;
935};
936template<> struct printf_length_modifier<long double>
937{
938 static constexpr char value = 'L';
939};
940template<typename T> std::string
941host_float_ops<T>::to_string (const gdb_byte *addr, const struct type *type,
942 const char *format) const
943{
1cfb73db 944 /* Determine the format string to use on the host side. */
7a26362d
UW
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);
1cfb73db 948
7a26362d
UW
949 T host_float;
950 from_target (type, addr, &host_float);
951 return string_printf (host_format.c_str (), host_float);
1cfb73db
UW
952}
953
7a26362d 954/* Parse string IN into a target floating-number of type TYPE and
1cfb73db 955 store it as byte-stream ADDR. Return whether parsing succeeded. */
7a26362d
UW
956template<typename T> struct scanf_length_modifier
957{
958 static constexpr char value = 0;
959};
960template<> struct scanf_length_modifier<double>
961{
962 static constexpr char value = 'l';
963};
964template<> struct scanf_length_modifier<long double>
965{
966 static constexpr char value = 'L';
967};
968template<typename T> bool
969host_float_ops<T>::from_string (gdb_byte *addr, const struct type *type,
970 const std::string &in) const
1cfb73db 971{
7a26362d 972 T host_float;
1cfb73db 973 int n, num;
7a26362d
UW
974
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";
979
980 num = sscanf (in.c_str (), scan_format.c_str(), &host_float, &n);
1cfb73db
UW
981
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;
987
988 /* We only accept the whole string. */
989 if (in[n])
990 return false;
991
7a26362d 992 to_target (type, &host_float, addr);
1cfb73db
UW
993 return true;
994}
995
7a26362d 996/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
50637b26 997 to an integer value (rounding towards zero). */
7a26362d
UW
998template<typename T> LONGEST
999host_float_ops<T>::to_longest (const gdb_byte *addr,
1000 const struct type *type) const
50637b26 1001{
7a26362d
UW
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;
50637b26
UW
1011}
1012
7a26362d 1013/* Convert signed integer VAL to a target floating-number of type TYPE
50637b26 1014 and store it as byte-stream ADDR. */
7a26362d
UW
1015template<typename T> void
1016host_float_ops<T>::from_longest (gdb_byte *addr, const struct type *type,
1017 LONGEST val) const
50637b26 1018{
7a26362d
UW
1019 T host_float = (T) val;
1020 to_target (type, &host_float, addr);
50637b26
UW
1021}
1022
7a26362d 1023/* Convert unsigned integer VAL to a target floating-number of type TYPE
50637b26 1024 and store it as byte-stream ADDR. */
7a26362d
UW
1025template<typename T> void
1026host_float_ops<T>::from_ulongest (gdb_byte *addr, const struct type *type,
1027 ULONGEST val) const
50637b26 1028{
7a26362d
UW
1029 T host_float = (T) val;
1030 to_target (type, &host_float, addr);
50637b26
UW
1031}
1032
7a26362d 1033/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
14ad9311 1034 to a floating-point value in the host "double" format. */
7a26362d
UW
1035template<typename T> double
1036host_float_ops<T>::to_host_double (const gdb_byte *addr,
1037 const struct type *type) const
14ad9311 1038{
7a26362d
UW
1039 T host_float;
1040 from_target (type, addr, &host_float);
1041 return (double) host_float;
14ad9311
UW
1042}
1043
1044/* Convert floating-point value VAL in the host "double" format to a target
7a26362d
UW
1045 floating-number of type TYPE and store it as byte-stream ADDR. */
1046template<typename T> void
1047host_float_ops<T>::from_host_double (gdb_byte *addr, const struct type *type,
1048 double val) const
14ad9311 1049{
7a26362d
UW
1050 T host_float = (T) val;
1051 to_target (type, &host_float, addr);
14ad9311
UW
1052}
1053
7a26362d
UW
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
50637b26 1056 store it to the target byte-stream TO. */
7a26362d
UW
1057template<typename T> void
1058host_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
50637b26 1062{
7a26362d
UW
1063 T host_float;
1064 from_target (from_type, from, &host_float);
1065 to_target (to_type, &host_float, to);
50637b26
UW
1066}
1067
66c02b9e
UW
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
7a26362d
UW
1070 types TYPE_X and TYPE_Y, respectively. Convert the result to format
1071 TYPE_RES and store it into the byte-stream RES. */
1072template<typename T> void
1073host_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
66c02b9e 1077{
7a26362d 1078 T v1, v2, v = 0;
66c02b9e 1079
7a26362d
UW
1080 from_target (type_x, x, &v1);
1081 from_target (type_y, y, &v2);
66c02b9e
UW
1082
1083 switch (op)
1084 {
1085 case BINOP_ADD:
1086 v = v1 + v2;
1087 break;
1088
1089 case BINOP_SUB:
1090 v = v1 - v2;
1091 break;
1092
1093 case BINOP_MUL:
1094 v = v1 * v2;
1095 break;
1096
1097 case BINOP_DIV:
1098 v = v1 / v2;
1099 break;
1100
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;
1108
1109 case BINOP_MIN:
1110 v = v1 < v2 ? v1 : v2;
1111 break;
1112
1113 case BINOP_MAX:
1114 v = v1 > v2 ? v1 : v2;
1115 break;
1116
1117 default:
1118 error (_("Integer-only operation on floating point number."));
1119 break;
1120 }
1121
7a26362d 1122 to_target (type_res, &v, res);
66c02b9e
UW
1123}
1124
1125/* Compare the two target byte streams X and Y, interpreted as floating-point
7a26362d 1126 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
66c02b9e 1127 are equal, -1 if X is less than Y, and 1 otherwise. */
7a26362d
UW
1128template<typename T> int
1129host_float_ops<T>::compare (const gdb_byte *x, const struct type *type_x,
1130 const gdb_byte *y, const struct type *type_y) const
66c02b9e 1131{
7a26362d 1132 T v1, v2;
66c02b9e 1133
7a26362d
UW
1134 from_target (type_x, x, &v1);
1135 from_target (type_y, y, &v2);
66c02b9e
UW
1136
1137 if (v1 == v2)
1138 return 0;
1139 if (v1 < v2)
1140 return -1;
1141 return 1;
1142}
1143
50637b26 1144
2400729e
UW
1145/* Implementation of target_float_ops using the MPFR library
1146 mpfr_t as intermediate type. */
1147
1148#ifdef HAVE_LIBMPFR
1149
1150#include <mpfr.h>
1151
1152class mpfr_float_ops : public target_float_ops
1153{
1154public:
1155 std::string to_string (const gdb_byte *addr, const struct type *type,
1156 const char *format) const override;
1157 bool from_string (gdb_byte *addr, const struct type *type,
1158 const std::string &string) const override;
1159
1160 LONGEST to_longest (const gdb_byte *addr,
1161 const struct type *type) const override;
1162 void from_longest (gdb_byte *addr, const struct type *type,
1163 LONGEST val) const override;
1164 void from_ulongest (gdb_byte *addr, const struct type *type,
1165 ULONGEST val) const override;
1166 double to_host_double (const gdb_byte *addr,
1167 const struct type *type) const override;
1168 void from_host_double (gdb_byte *addr, const struct type *type,
1169 double val) const override;
1170 void convert (const gdb_byte *from, const struct type *from_type,
1171 gdb_byte *to, const struct type *to_type) const override;
1172
1173 void binop (enum exp_opcode opcode,
1174 const gdb_byte *x, const struct type *type_x,
1175 const gdb_byte *y, const struct type *type_y,
1176 gdb_byte *res, const struct type *type_res) const override;
1177 int compare (const gdb_byte *x, const struct type *type_x,
1178 const gdb_byte *y, const struct type *type_y) const override;
1179
1180private:
1181 /* Local wrapper class to handle mpfr_t initalization and cleanup. */
1182 class gdb_mpfr
1183 {
1184 public:
1185 mpfr_t val;
1186
1187 gdb_mpfr (const struct type *type)
1188 {
1189 const struct floatformat *fmt = floatformat_from_type (type);
1190 mpfr_init2 (val, floatformat_precision (fmt));
1191 }
1192
1193 gdb_mpfr (const gdb_mpfr &source)
1194 {
1195 mpfr_init2 (val, mpfr_get_prec (source.val));
1196 }
1197
1198 ~gdb_mpfr ()
1199 {
1200 mpfr_clear (val);
1201 }
1202 };
1203
1204 void from_target (const struct floatformat *fmt,
1205 const gdb_byte *from, gdb_mpfr &to) const;
1206 void from_target (const struct type *type,
1207 const gdb_byte *from, gdb_mpfr &to) const;
1208
1209 void to_target (const struct type *type,
1210 const gdb_mpfr &from, gdb_byte *to) const;
1211 void to_target (const struct floatformat *fmt,
1212 const gdb_mpfr &from, gdb_byte *to) const;
1213};
1214
1215
1216/* Convert TO/FROM target floating-point format to mpfr_t. */
1217
1218void
1219mpfr_float_ops::from_target (const struct floatformat *fmt,
1220 const gdb_byte *orig_from, gdb_mpfr &to) const
1221{
1222 const gdb_byte *from = orig_from;
1223 mpfr_exp_t exponent;
1224 unsigned long mant;
1225 unsigned int mant_bits, mant_off;
1226 int mant_bits_left;
1227 int special_exponent; /* It's a NaN, denorm or zero. */
1228 enum floatformat_byteorders order;
1229 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
1230 enum float_kind kind;
1231
1232 gdb_assert (fmt->totalsize
1233 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
1234
1235 /* Handle non-numbers. */
1236 kind = floatformat_classify (fmt, from);
1237 if (kind == float_infinite)
1238 {
1239 mpfr_set_inf (to.val, floatformat_is_negative (fmt, from) ? -1 : 1);
1240 return;
1241 }
1242 if (kind == float_nan)
1243 {
1244 mpfr_set_nan (to.val);
1245 return;
1246 }
1247
1248 order = floatformat_normalize_byteorder (fmt, from, newfrom);
1249
1250 if (order != fmt->byteorder)
1251 from = newfrom;
1252
1253 if (fmt->split_half)
1254 {
1255 gdb_mpfr top (to), bot (to);
1256
1257 from_target (fmt->split_half, from, top);
1258 /* Preserve the sign of 0, which is the sign of the top half. */
1259 if (mpfr_zero_p (top.val))
1260 {
1261 mpfr_set (to.val, top.val, MPFR_RNDN);
1262 return;
1263 }
1264 from_target (fmt->split_half,
1265 from + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, bot);
1266 mpfr_add (to.val, top.val, bot.val, MPFR_RNDN);
1267 return;
1268 }
1269
1270 exponent = get_field (from, order, fmt->totalsize, fmt->exp_start,
1271 fmt->exp_len);
1272 /* Note that if exponent indicates a NaN, we can't really do anything useful
1273 (not knowing if the host has NaN's, or how to build one). So it will
1274 end up as an infinity or something close; that is OK. */
1275
1276 mant_bits_left = fmt->man_len;
1277 mant_off = fmt->man_start;
1278 mpfr_set_zero (to.val, 0);
1279
1280 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
1281
1282 /* Don't bias NaNs. Use minimum exponent for denorms. For
1283 simplicity, we don't check for zero as the exponent doesn't matter.
1284 Note the cast to int; exp_bias is unsigned, so it's important to
1285 make sure the operation is done in signed arithmetic. */
1286 if (!special_exponent)
1287 exponent -= fmt->exp_bias;
1288 else if (exponent == 0)
1289 exponent = 1 - fmt->exp_bias;
1290
1291 /* Build the result algebraically. Might go infinite, underflow, etc;
1292 who cares. */
1293
1294 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
1295 increment the exponent by one to account for the integer bit. */
1296
1297 if (!special_exponent)
1298 {
1299 if (fmt->intbit == floatformat_intbit_no)
1300 mpfr_set_ui_2exp (to.val, 1, exponent, MPFR_RNDN);
1301 else
1302 exponent++;
1303 }
1304
1305 gdb_mpfr tmp (to);
1306
1307 while (mant_bits_left > 0)
1308 {
1309 mant_bits = std::min (mant_bits_left, 32);
1310
1311 mant = get_field (from, order, fmt->totalsize, mant_off, mant_bits);
1312
1313 mpfr_set_si (tmp.val, mant, MPFR_RNDN);
1314 mpfr_mul_2si (tmp.val, tmp.val, exponent - mant_bits, MPFR_RNDN);
1315 mpfr_add (to.val, to.val, tmp.val, MPFR_RNDN);
1316 exponent -= mant_bits;
1317 mant_off += mant_bits;
1318 mant_bits_left -= mant_bits;
1319 }
1320
1321 /* Negate it if negative. */
1322 if (get_field (from, order, fmt->totalsize, fmt->sign_start, 1))
1323 mpfr_neg (to.val, to.val, MPFR_RNDN);
1324}
1325
1326void
1327mpfr_float_ops::from_target (const struct type *type,
1328 const gdb_byte *from, gdb_mpfr &to) const
1329{
1330 from_target (floatformat_from_type (type), from, to);
1331}
1332
1333void
1334mpfr_float_ops::to_target (const struct floatformat *fmt,
1335 const gdb_mpfr &from, gdb_byte *orig_to) const
1336{
1337 unsigned char *to = orig_to;
1338 mpfr_exp_t exponent;
1339 unsigned int mant_bits, mant_off;
1340 int mant_bits_left;
1341 enum floatformat_byteorders order = fmt->byteorder;
1342 unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
1343
1344 if (order != floatformat_little)
1345 order = floatformat_big;
1346
1347 if (order != fmt->byteorder)
1348 to = newto;
1349
1350 memset (to, 0, floatformat_totalsize_bytes (fmt));
1351
1352 if (fmt->split_half)
1353 {
1354 gdb_mpfr top (from), bot (from);
1355
1356 mpfr_set (top.val, from.val, MPFR_RNDN);
1357 /* If the rounded top half is Inf, the bottom must be 0 not NaN
1358 or Inf. */
1359 if (mpfr_inf_p (top.val))
1360 mpfr_set_zero (bot.val, 0);
1361 else
1362 mpfr_sub (bot.val, from.val, top.val, MPFR_RNDN);
1363
1364 to_target (fmt->split_half, top, to);
1365 to_target (fmt->split_half, bot,
1366 to + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2);
1367 return;
1368 }
1369
1370 gdb_mpfr tmp (from);
1371
1372 if (mpfr_zero_p (from.val))
1373 goto finalize_byteorder; /* Result is zero */
1374
1375 mpfr_set (tmp.val, from.val, MPFR_RNDN);
1376
1377 if (mpfr_nan_p (tmp.val)) /* Result is NaN */
1378 {
1379 /* From is NaN */
1380 put_field (to, order, fmt->totalsize, fmt->exp_start,
1381 fmt->exp_len, fmt->exp_nan);
1382 /* Be sure it's not infinity, but NaN value is irrel. */
1383 put_field (to, order, fmt->totalsize, fmt->man_start,
1384 fmt->man_len, 1);
1385 goto finalize_byteorder;
1386 }
1387
1388 /* If negative, set the sign bit. */
1389 if (mpfr_sgn (tmp.val) < 0)
1390 {
1391 put_field (to, order, fmt->totalsize, fmt->sign_start, 1, 1);
1392 mpfr_neg (tmp.val, tmp.val, MPFR_RNDN);
1393 }
1394
1395 if (mpfr_inf_p (tmp.val)) /* Result is Infinity. */
1396 {
1397 /* Infinity exponent is same as NaN's. */
1398 put_field (to, order, fmt->totalsize, fmt->exp_start,
1399 fmt->exp_len, fmt->exp_nan);
1400 /* Infinity mantissa is all zeroes. */
1401 put_field (to, order, fmt->totalsize, fmt->man_start,
1402 fmt->man_len, 0);
1403 goto finalize_byteorder;
1404 }
1405
1406 mpfr_frexp (&exponent, tmp.val, tmp.val, MPFR_RNDN);
1407
1408 if (exponent + fmt->exp_bias <= 0)
1409 {
1410 /* The value is too small to be expressed in the destination
1411 type (not enough bits in the exponent. Treat as 0. */
1412 put_field (to, order, fmt->totalsize, fmt->exp_start,
1413 fmt->exp_len, 0);
1414 put_field (to, order, fmt->totalsize, fmt->man_start,
1415 fmt->man_len, 0);
1416 goto finalize_byteorder;
1417 }
1418
1419 if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
1420 {
1421 /* The value is too large to fit into the destination.
1422 Treat as infinity. */
1423 put_field (to, order, fmt->totalsize, fmt->exp_start,
1424 fmt->exp_len, fmt->exp_nan);
1425 put_field (to, order, fmt->totalsize, fmt->man_start,
1426 fmt->man_len, 0);
1427 goto finalize_byteorder;
1428 }
1429
1430 put_field (to, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
1431 exponent + fmt->exp_bias - 1);
1432
1433 mant_bits_left = fmt->man_len;
1434 mant_off = fmt->man_start;
1435 while (mant_bits_left > 0)
1436 {
1437 unsigned long mant_long;
1438
1439 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
1440
1441 mpfr_mul_2ui (tmp.val, tmp.val, 32, MPFR_RNDN);
1442 mant_long = mpfr_get_ui (tmp.val, MPFR_RNDZ) & 0xffffffffL;
1443 mpfr_sub_ui (tmp.val, tmp.val, mant_long, MPFR_RNDZ);
1444
1445 /* If the integer bit is implicit, then we need to discard it.
1446 If we are discarding a zero, we should be (but are not) creating
1447 a denormalized number which means adjusting the exponent
1448 (I think). */
1449 if (mant_bits_left == fmt->man_len
1450 && fmt->intbit == floatformat_intbit_no)
1451 {
1452 mant_long <<= 1;
1453 mant_long &= 0xffffffffL;
1454 /* If we are processing the top 32 mantissa bits of a doublest
1455 so as to convert to a float value with implied integer bit,
1456 we will only be putting 31 of those 32 bits into the
1457 final value due to the discarding of the top bit. In the
1458 case of a small float value where the number of mantissa
1459 bits is less than 32, discarding the top bit does not alter
1460 the number of bits we will be adding to the result. */
1461 if (mant_bits == 32)
1462 mant_bits -= 1;
1463 }
1464
1465 if (mant_bits < 32)
1466 {
1467 /* The bits we want are in the most significant MANT_BITS bits of
1468 mant_long. Move them to the least significant. */
1469 mant_long >>= 32 - mant_bits;
1470 }
1471
1472 put_field (to, order, fmt->totalsize,
1473 mant_off, mant_bits, mant_long);
1474 mant_off += mant_bits;
1475 mant_bits_left -= mant_bits;
1476 }
1477
1478 finalize_byteorder:
1479 /* Do we need to byte-swap the words in the result? */
1480 if (order != fmt->byteorder)
1481 floatformat_normalize_byteorder (fmt, newto, orig_to);
1482}
1483
1484void
1485mpfr_float_ops::to_target (const struct type *type,
1486 const gdb_mpfr &from, gdb_byte *to) const
1487{
1488 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1489 memset (to, 0, TYPE_LENGTH (type));
1490
1491 to_target (floatformat_from_type (type), from, to);
1492}
1493
1494/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1495 to a string, optionally using the print format FORMAT. */
1496std::string
1497mpfr_float_ops::to_string (const gdb_byte *addr,
1498 const struct type *type,
1499 const char *format) const
1500{
1501 const struct floatformat *fmt = floatformat_from_type (type);
1502
1503 /* Unless we need to adhere to a specific format, provide special
1504 output for certain cases. */
1505 if (format == nullptr)
1506 {
1507 /* Detect invalid representations. */
1508 if (!floatformat_is_valid (fmt, addr))
1509 return "<invalid float value>";
1510
1511 /* Handle NaN and Inf. */
1512 enum float_kind kind = floatformat_classify (fmt, addr);
1513 if (kind == float_nan)
1514 {
1515 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
1516 const char *mantissa = floatformat_mantissa (fmt, addr);
1517 return string_printf ("%snan(0x%s)", sign, mantissa);
1518 }
1519 else if (kind == float_infinite)
1520 {
1521 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
1522 return string_printf ("%sinf", sign);
1523 }
1524 }
1525
1526 /* Determine the format string to use on the host side. */
1527 std::string host_format = floatformat_printf_format (fmt, format, 'R');
1528
1529 gdb_mpfr tmp (type);
1530 from_target (type, addr, tmp);
1531
1532 int size = mpfr_snprintf (NULL, 0, host_format.c_str (), tmp.val);
1533 std::string str (size, '\0');
1534 mpfr_sprintf (&str[0], host_format.c_str (), tmp.val);
1535
1536 return str;
1537}
1538
1539/* Parse string STRING into a target floating-number of type TYPE and
1540 store it as byte-stream ADDR. Return whether parsing succeeded. */
1541bool
1542mpfr_float_ops::from_string (gdb_byte *addr,
1543 const struct type *type,
1544 const std::string &in) const
1545{
1546 gdb_mpfr tmp (type);
1547
1548 char *endptr;
1549 mpfr_strtofr (tmp.val, in.c_str (), &endptr, 0, MPFR_RNDN);
1550
1551 /* We only accept the whole string. */
1552 if (*endptr)
1553 return false;
1554
1555 to_target (type, tmp, addr);
1556 return true;
1557}
1558
1559/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1560 to an integer value (rounding towards zero). */
1561LONGEST
1562mpfr_float_ops::to_longest (const gdb_byte *addr,
1563 const struct type *type) const
1564{
1565 gdb_mpfr tmp (type);
1566 from_target (type, addr, tmp);
1567 return mpfr_get_sj (tmp.val, MPFR_RNDZ);
1568}
1569
1570/* Convert signed integer VAL to a target floating-number of type TYPE
1571 and store it as byte-stream ADDR. */
1572void
1573mpfr_float_ops::from_longest (gdb_byte *addr,
1574 const struct type *type,
1575 LONGEST val) const
1576{
1577 gdb_mpfr tmp (type);
1578 mpfr_set_sj (tmp.val, val, MPFR_RNDN);
1579 to_target (type, tmp, addr);
1580}
1581
1582/* Convert unsigned integer VAL to a target floating-number of type TYPE
1583 and store it as byte-stream ADDR. */
1584void
1585mpfr_float_ops::from_ulongest (gdb_byte *addr,
1586 const struct type *type,
1587 ULONGEST val) const
1588{
1589 gdb_mpfr tmp (type);
1590 mpfr_set_uj (tmp.val, val, MPFR_RNDN);
1591 to_target (type, tmp, addr);
1592}
1593
1594/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1595 to a floating-point value in the host "double" format. */
1596double
1597mpfr_float_ops::to_host_double (const gdb_byte *addr,
1598 const struct type *type) const
1599{
1600 gdb_mpfr tmp (type);
1601 from_target (type, addr, tmp);
1602 return mpfr_get_d (tmp.val, MPFR_RNDN);
1603}
1604
1605/* Convert floating-point value VAL in the host "double" format to a target
1606 floating-number of type TYPE and store it as byte-stream ADDR. */
1607void
1608mpfr_float_ops::from_host_double (gdb_byte *addr,
1609 const struct type *type,
1610 double val) const
1611{
1612 gdb_mpfr tmp (type);
1613 mpfr_set_d (tmp.val, val, MPFR_RNDN);
1614 to_target (type, tmp, addr);
1615}
1616
1617/* Convert a floating-point number of type FROM_TYPE from the target
1618 byte-stream FROM to a floating-point number of type TO_TYPE, and
1619 store it to the target byte-stream TO. */
1620void
1621mpfr_float_ops::convert (const gdb_byte *from,
1622 const struct type *from_type,
1623 gdb_byte *to,
1624 const struct type *to_type) const
1625{
1626 gdb_mpfr from_tmp (from_type), to_tmp (to_type);
1627 from_target (from_type, from, from_tmp);
1628 mpfr_set (to_tmp.val, from_tmp.val, MPFR_RNDN);
1629 to_target (to_type, to_tmp, to);
1630}
1631
1632/* Perform the binary operation indicated by OPCODE, using as operands the
1633 target byte streams X and Y, interpreted as floating-point numbers of
1634 types TYPE_X and TYPE_Y, respectively. Convert the result to type
1635 TYPE_RES and store it into the byte-stream RES. */
1636void
1637mpfr_float_ops::binop (enum exp_opcode op,
1638 const gdb_byte *x, const struct type *type_x,
1639 const gdb_byte *y, const struct type *type_y,
1640 gdb_byte *res, const struct type *type_res) const
1641{
1642 gdb_mpfr x_tmp (type_x), y_tmp (type_y), tmp (type_res);
1643
1644 from_target (type_x, x, x_tmp);
1645 from_target (type_y, y, y_tmp);
1646
1647 switch (op)
1648 {
1649 case BINOP_ADD:
1650 mpfr_add (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1651 break;
1652
1653 case BINOP_SUB:
1654 mpfr_sub (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1655 break;
1656
1657 case BINOP_MUL:
1658 mpfr_mul (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1659 break;
1660
1661 case BINOP_DIV:
1662 mpfr_div (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1663 break;
1664
1665 case BINOP_EXP:
1666 mpfr_pow (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1667 break;
1668
1669 case BINOP_MIN:
1670 mpfr_min (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1671 break;
1672
1673 case BINOP_MAX:
1674 mpfr_max (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1675 break;
1676
1677 default:
1678 error (_("Integer-only operation on floating point number."));
1679 break;
1680 }
1681
1682 to_target (type_res, tmp, res);
1683}
1684
1685/* Compare the two target byte streams X and Y, interpreted as floating-point
1686 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
1687 are equal, -1 if X is less than Y, and 1 otherwise. */
1688int
1689mpfr_float_ops::compare (const gdb_byte *x, const struct type *type_x,
1690 const gdb_byte *y, const struct type *type_y) const
1691{
1692 gdb_mpfr x_tmp (type_x), y_tmp (type_y);
1693
1694 from_target (type_x, x, x_tmp);
1695 from_target (type_y, y, y_tmp);
1696
1697 if (mpfr_equal_p (x_tmp.val, y_tmp.val))
1698 return 0;
1699 else if (mpfr_less_p (x_tmp.val, y_tmp.val))
1700 return -1;
1701 else
1702 return 1;
1703}
1704
1705#endif
1706
1707
1cfb73db
UW
1708/* Helper routines operating on decimal floating-point data. */
1709
1710/* Decimal floating point is one of the extension to IEEE 754, which is
1711 described in http://grouper.ieee.org/groups/754/revision.html and
1712 http://www2.hursley.ibm.com/decimal/. It completes binary floating
1713 point by representing floating point more exactly. */
1714
1715/* The order of the following headers is important for making sure
1716 decNumber structure is large enough to hold decimal128 digits. */
1717
1718#include "dpd/decimal128.h"
1719#include "dpd/decimal64.h"
1720#include "dpd/decimal32.h"
1721
1722/* When using decimal128, this is the maximum string length + 1
1723 (value comes from libdecnumber's DECIMAL128_String constant). */
1724#define MAX_DECIMAL_STRING 43
1725
1726/* In GDB, we are using an array of gdb_byte to represent decimal values.
1727 They are stored in host byte order. This routine does the conversion if
1728 the target byte order is different. */
1729static void
7a26362d 1730match_endianness (const gdb_byte *from, const struct type *type, gdb_byte *to)
1cfb73db 1731{
7a26362d
UW
1732 gdb_assert (TYPE_CODE (type) == TYPE_CODE_DECFLOAT);
1733
1734 int len = TYPE_LENGTH (type);
1cfb73db
UW
1735 int i;
1736
1737#if WORDS_BIGENDIAN
1738#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE
1739#else
1740#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
1741#endif
1742
7a26362d 1743 if (gdbarch_byte_order (get_type_arch (type)) == OPPOSITE_BYTE_ORDER)
1cfb73db
UW
1744 for (i = 0; i < len; i++)
1745 to[i] = from[len - i - 1];
1746 else
1747 for (i = 0; i < len; i++)
1748 to[i] = from[i];
1749
1750 return;
1751}
1752
1753/* Helper function to get the appropriate libdecnumber context for each size
1754 of decimal float. */
1755static void
7a26362d 1756set_decnumber_context (decContext *ctx, const struct type *type)
1cfb73db 1757{
7a26362d
UW
1758 gdb_assert (TYPE_CODE (type) == TYPE_CODE_DECFLOAT);
1759
1760 switch (TYPE_LENGTH (type))
1cfb73db
UW
1761 {
1762 case 4:
1763 decContextDefault (ctx, DEC_INIT_DECIMAL32);
1764 break;
1765 case 8:
1766 decContextDefault (ctx, DEC_INIT_DECIMAL64);
1767 break;
1768 case 16:
1769 decContextDefault (ctx, DEC_INIT_DECIMAL128);
1770 break;
1771 }
1772
1773 ctx->traps = 0;
1774}
1775
1776/* Check for errors signaled in the decimal context structure. */
1777static void
1778decimal_check_errors (decContext *ctx)
1779{
1780 /* An error here could be a division by zero, an overflow, an underflow or
1781 an invalid operation (from the DEC_Errors constant in decContext.h).
1782 Since GDB doesn't complain about division by zero, overflow or underflow
1783 errors for binary floating, we won't complain about them for decimal
1784 floating either. */
1785 if (ctx->status & DEC_IEEE_854_Invalid_operation)
1786 {
1787 /* Leave only the error bits in the status flags. */
1788 ctx->status &= DEC_IEEE_854_Invalid_operation;
1789 error (_("Cannot perform operation: %s"),
1790 decContextStatusToString (ctx));
1791 }
1792}
1793
1794/* Helper function to convert from libdecnumber's appropriate representation
1795 for computation to each size of decimal float. */
1796static void
d7236961 1797decimal_from_number (const decNumber *from,
7a26362d 1798 gdb_byte *to, const struct type *type)
1cfb73db 1799{
d7236961
UW
1800 gdb_byte dec[16];
1801
1cfb73db
UW
1802 decContext set;
1803
7a26362d 1804 set_decnumber_context (&set, type);
1cfb73db 1805
7a26362d 1806 switch (TYPE_LENGTH (type))
1cfb73db
UW
1807 {
1808 case 4:
d7236961 1809 decimal32FromNumber ((decimal32 *) dec, from, &set);
1cfb73db
UW
1810 break;
1811 case 8:
d7236961 1812 decimal64FromNumber ((decimal64 *) dec, from, &set);
1cfb73db
UW
1813 break;
1814 case 16:
d7236961
UW
1815 decimal128FromNumber ((decimal128 *) dec, from, &set);
1816 break;
1817 default:
1818 error (_("Unknown decimal floating point type."));
1cfb73db
UW
1819 break;
1820 }
d7236961 1821
7a26362d 1822 match_endianness (dec, type, to);
1cfb73db
UW
1823}
1824
1825/* Helper function to convert each size of decimal float to libdecnumber's
1826 appropriate representation for computation. */
1827static void
7a26362d 1828decimal_to_number (const gdb_byte *addr, const struct type *type,
d7236961 1829 decNumber *to)
1cfb73db 1830{
d7236961 1831 gdb_byte dec[16];
7a26362d 1832 match_endianness (addr, type, dec);
d7236961 1833
7a26362d 1834 switch (TYPE_LENGTH (type))
1cfb73db
UW
1835 {
1836 case 4:
d7236961 1837 decimal32ToNumber ((decimal32 *) dec, to);
1cfb73db
UW
1838 break;
1839 case 8:
d7236961 1840 decimal64ToNumber ((decimal64 *) dec, to);
1cfb73db
UW
1841 break;
1842 case 16:
d7236961 1843 decimal128ToNumber ((decimal128 *) dec, to);
1cfb73db
UW
1844 break;
1845 default:
1846 error (_("Unknown decimal floating point type."));
1847 break;
1848 }
1849}
1850
7a26362d
UW
1851/* Returns true if ADDR (which is of type TYPE) is the number zero. */
1852static bool
1853decimal_is_zero (const gdb_byte *addr, const struct type *type)
1854{
1855 decNumber number;
1856
1857 decimal_to_number (addr, type, &number);
1858
1859 return decNumberIsZero (&number);
1860}
1861
1862
1863/* Implementation of target_float_ops using the libdecnumber decNumber type
1864 as intermediate format. */
1865
1866class decimal_float_ops : public target_float_ops
1867{
1868public:
1869 std::string to_string (const gdb_byte *addr, const struct type *type,
1870 const char *format) const override;
1871 bool from_string (gdb_byte *addr, const struct type *type,
1872 const std::string &string) const override;
1873
1874 LONGEST to_longest (const gdb_byte *addr,
1875 const struct type *type) const override;
1876 void from_longest (gdb_byte *addr, const struct type *type,
1877 LONGEST val) const override;
1878 void from_ulongest (gdb_byte *addr, const struct type *type,
1879 ULONGEST val) const override;
1880 double to_host_double (const gdb_byte *addr,
1881 const struct type *type) const override
1882 {
1883 /* We don't support conversions between target decimal floating-point
1884 types and the host double type. */
1885 gdb_assert_not_reached ("invalid operation on decimal float");
1886 }
1887 void from_host_double (gdb_byte *addr, const struct type *type,
1888 double val) const override
1889 {
1890 /* We don't support conversions between target decimal floating-point
1891 types and the host double type. */
1892 gdb_assert_not_reached ("invalid operation on decimal float");
1893 }
1894 void convert (const gdb_byte *from, const struct type *from_type,
1895 gdb_byte *to, const struct type *to_type) const override;
1896
1897 void binop (enum exp_opcode opcode,
1898 const gdb_byte *x, const struct type *type_x,
1899 const gdb_byte *y, const struct type *type_y,
1900 gdb_byte *res, const struct type *type_res) const override;
1901 int compare (const gdb_byte *x, const struct type *type_x,
1902 const gdb_byte *y, const struct type *type_y) const override;
1903};
1904
1cfb73db
UW
1905/* Convert decimal type to its string representation. LEN is the length
1906 of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
1907 16 bytes for decimal128. */
7a26362d
UW
1908std::string
1909decimal_float_ops::to_string (const gdb_byte *addr, const struct type *type,
1910 const char *format = nullptr) const
1cfb73db
UW
1911{
1912 gdb_byte dec[16];
1913
7a26362d 1914 match_endianness (addr, type, dec);
1cfb73db
UW
1915
1916 if (format != nullptr)
1917 {
1918 /* We don't handle format strings (yet). If the host printf supports
1919 decimal floating point types, just use this. Otherwise, fall back
1920 to printing the number while ignoring the format string. */
1921#if defined (PRINTF_HAS_DECFLOAT)
1922 /* FIXME: This makes unwarranted assumptions about the host ABI! */
1923 return string_printf (format, dec);
1924#endif
1925 }
1926
1927 std::string result;
1928 result.resize (MAX_DECIMAL_STRING);
1929
7a26362d 1930 switch (TYPE_LENGTH (type))
1cfb73db
UW
1931 {
1932 case 4:
1933 decimal32ToString ((decimal32 *) dec, &result[0]);
1934 break;
1935 case 8:
1936 decimal64ToString ((decimal64 *) dec, &result[0]);
1937 break;
1938 case 16:
1939 decimal128ToString ((decimal128 *) dec, &result[0]);
1940 break;
1941 default:
1942 error (_("Unknown decimal floating point type."));
1943 break;
1944 }
1945
1946 return result;
1947}
1948
1949/* Convert the string form of a decimal value to its decimal representation.
1950 LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
1951 decimal64 and 16 bytes for decimal128. */
7a26362d
UW
1952bool
1953decimal_float_ops::from_string (gdb_byte *addr, const struct type *type,
1954 const std::string &string) const
1cfb73db
UW
1955{
1956 decContext set;
1957 gdb_byte dec[16];
1958
7a26362d 1959 set_decnumber_context (&set, type);
1cfb73db 1960
7a26362d 1961 switch (TYPE_LENGTH (type))
1cfb73db
UW
1962 {
1963 case 4:
1964 decimal32FromString ((decimal32 *) dec, string.c_str (), &set);
1965 break;
1966 case 8:
1967 decimal64FromString ((decimal64 *) dec, string.c_str (), &set);
1968 break;
1969 case 16:
1970 decimal128FromString ((decimal128 *) dec, string.c_str (), &set);
1971 break;
1972 default:
1973 error (_("Unknown decimal floating point type."));
1974 break;
1975 }
1976
7a26362d 1977 match_endianness (dec, type, addr);
1cfb73db
UW
1978
1979 /* Check for errors in the DFP operation. */
1980 decimal_check_errors (&set);
1981
1982 return true;
1983}
1984
1985/* Converts a LONGEST to a decimal float of specified LEN bytes. */
7a26362d
UW
1986void
1987decimal_float_ops::from_longest (gdb_byte *addr, const struct type *type,
1988 LONGEST from) const
1cfb73db 1989{
1cfb73db 1990 decNumber number;
d7236961 1991
1cfb73db
UW
1992 if ((int32_t) from != from)
1993 /* libdecnumber can convert only 32-bit integers. */
1994 error (_("Conversion of large integer to a "
1995 "decimal floating type is not supported."));
1996
1997 decNumberFromInt32 (&number, (int32_t) from);
1998
7a26362d 1999 decimal_from_number (&number, addr, type);
1cfb73db
UW
2000}
2001
2002/* Converts a ULONGEST to a decimal float of specified LEN bytes. */
7a26362d
UW
2003void
2004decimal_float_ops::from_ulongest (gdb_byte *addr, const struct type *type,
2005 ULONGEST from) const
1cfb73db 2006{
1cfb73db
UW
2007 decNumber number;
2008
2009 if ((uint32_t) from != from)
2010 /* libdecnumber can convert only 32-bit integers. */
2011 error (_("Conversion of large integer to a "
2012 "decimal floating type is not supported."));
2013
2014 decNumberFromUInt32 (&number, (uint32_t) from);
2015
7a26362d 2016 decimal_from_number (&number, addr, type);
1cfb73db
UW
2017}
2018
2019/* Converts a decimal float of LEN bytes to a LONGEST. */
7a26362d
UW
2020LONGEST
2021decimal_float_ops::to_longest (const gdb_byte *addr,
2022 const struct type *type) const
1cfb73db
UW
2023{
2024 /* libdecnumber has a function to convert from decimal to integer, but
2025 it doesn't work when the decimal number has a fractional part. */
7a26362d 2026 std::string str = to_string (addr, type);
1cfb73db
UW
2027 return strtoll (str.c_str (), NULL, 10);
2028}
2029
2030/* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
2031 and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
2032 RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */
7a26362d
UW
2033void
2034decimal_float_ops::binop (enum exp_opcode op,
2035 const gdb_byte *x, const struct type *type_x,
2036 const gdb_byte *y, const struct type *type_y,
2037 gdb_byte *res, const struct type *type_res) const
1cfb73db
UW
2038{
2039 decContext set;
2040 decNumber number1, number2, number3;
1cfb73db 2041
7a26362d
UW
2042 decimal_to_number (x, type_x, &number1);
2043 decimal_to_number (y, type_y, &number2);
1cfb73db 2044
7a26362d 2045 set_decnumber_context (&set, type_res);
1cfb73db
UW
2046
2047 switch (op)
2048 {
2049 case BINOP_ADD:
2050 decNumberAdd (&number3, &number1, &number2, &set);
2051 break;
2052 case BINOP_SUB:
2053 decNumberSubtract (&number3, &number1, &number2, &set);
2054 break;
2055 case BINOP_MUL:
2056 decNumberMultiply (&number3, &number1, &number2, &set);
2057 break;
2058 case BINOP_DIV:
2059 decNumberDivide (&number3, &number1, &number2, &set);
2060 break;
2061 case BINOP_EXP:
2062 decNumberPower (&number3, &number1, &number2, &set);
2063 break;
2064 default:
2065 error (_("Operation not valid for decimal floating point number."));
2066 break;
2067 }
2068
2069 /* Check for errors in the DFP operation. */
2070 decimal_check_errors (&set);
2071
7a26362d 2072 decimal_from_number (&number3, res, type_res);
1cfb73db
UW
2073}
2074
2075/* Compares two numbers numerically. If X is less than Y then the return value
2076 will be -1. If they are equal, then the return value will be 0. If X is
2077 greater than the Y then the return value will be 1. */
7a26362d
UW
2078int
2079decimal_float_ops::compare (const gdb_byte *x, const struct type *type_x,
2080 const gdb_byte *y, const struct type *type_y) const
1cfb73db
UW
2081{
2082 decNumber number1, number2, result;
2083 decContext set;
7a26362d 2084 const struct type *type_result;
1cfb73db 2085
7a26362d
UW
2086 decimal_to_number (x, type_x, &number1);
2087 decimal_to_number (y, type_y, &number2);
1cfb73db
UW
2088
2089 /* Perform the comparison in the larger of the two sizes. */
7a26362d
UW
2090 type_result = TYPE_LENGTH (type_x) > TYPE_LENGTH (type_y) ? type_x : type_y;
2091 set_decnumber_context (&set, type_result);
1cfb73db
UW
2092
2093 decNumberCompare (&result, &number1, &number2, &set);
2094
2095 /* Check for errors in the DFP operation. */
2096 decimal_check_errors (&set);
2097
2098 if (decNumberIsNaN (&result))
2099 error (_("Comparison with an invalid number (NaN)."));
2100 else if (decNumberIsZero (&result))
2101 return 0;
2102 else if (decNumberIsNegative (&result))
2103 return -1;
2104 else
2105 return 1;
2106}
2107
2108/* Convert a decimal value from a decimal type with LEN_FROM bytes to a
2109 decimal type with LEN_TO bytes. */
7a26362d
UW
2110void
2111decimal_float_ops::convert (const gdb_byte *from, const struct type *from_type,
2112 gdb_byte *to, const struct type *to_type) const
1cfb73db
UW
2113{
2114 decNumber number;
1cfb73db 2115
7a26362d
UW
2116 decimal_to_number (from, from_type, &number);
2117 decimal_from_number (&number, to, to_type);
1cfb73db
UW
2118}
2119
2120
70100014
UW
2121/* Typed floating-point routines. These routines operate on floating-point
2122 values in target format, represented by a byte buffer interpreted as a
2123 "struct type", which may be either a binary or decimal floating-point
2124 type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */
2125
7a26362d
UW
2126/* Return whether TYPE1 and TYPE2 are of the same category (binary or
2127 decimal floating-point). */
2128static bool
2129target_float_same_category_p (const struct type *type1,
2130 const struct type *type2)
2131{
2132 return TYPE_CODE (type1) == TYPE_CODE (type2);
2133}
2134
2135/* Return whether TYPE1 and TYPE2 use the same floating-point format. */
2136static bool
2137target_float_same_format_p (const struct type *type1,
2138 const struct type *type2)
2139{
2140 if (!target_float_same_category_p (type1, type2))
2141 return false;
2142
2143 switch (TYPE_CODE (type1))
2144 {
2145 case TYPE_CODE_FLT:
2146 return floatformat_from_type (type1) == floatformat_from_type (type2);
2147
2148 case TYPE_CODE_DECFLOAT:
2149 return (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
2150 && (gdbarch_byte_order (get_type_arch (type1))
2151 == gdbarch_byte_order (get_type_arch (type2))));
2152
2153 default:
2154 gdb_assert_not_reached ("unexpected type code");
2155 }
2156}
2157
2158/* Return the size (without padding) of the target floating-point
2159 format used by TYPE. */
2160static int
2161target_float_format_length (const struct type *type)
2162{
2163 switch (TYPE_CODE (type))
2164 {
2165 case TYPE_CODE_FLT:
2166 return floatformat_totalsize_bytes (floatformat_from_type (type));
2167
2168 case TYPE_CODE_DECFLOAT:
2169 return TYPE_LENGTH (type);
2170
2171 default:
2172 gdb_assert_not_reached ("unexpected type code");
2173 }
2174}
2175
2176/* Identifiers of available host-side intermediate formats. These must
2177 be sorted so the that the more "general" kinds come later. */
2178enum target_float_ops_kind
2179{
2180 /* Target binary floating-point formats that match a host format. */
2181 host_float = 0,
2182 host_double,
2183 host_long_double,
2184 /* Any other target binary floating-point format. */
2185 binary,
2186 /* Any target decimal floating-point format. */
2187 decimal
2188};
2189
2190/* Given a target type TYPE, choose the best host-side intermediate format
2191 to perform operations on TYPE in. */
2192static enum target_float_ops_kind
2193get_target_float_ops_kind (const struct type *type)
2194{
2195 switch (TYPE_CODE (type))
2196 {
2197 case TYPE_CODE_FLT:
2198 {
2199 const struct floatformat *fmt = floatformat_from_type (type);
2200
2201 /* Binary floating-point formats matching a host format. */
2202 if (fmt == host_float_format)
2203 return target_float_ops_kind::host_float;
2204 if (fmt == host_double_format)
2205 return target_float_ops_kind::host_double;
2206 if (fmt == host_long_double_format)
2207 return target_float_ops_kind::host_long_double;
2208
2209 /* Any other binary floating-point format. */
2210 return target_float_ops_kind::binary;
2211 }
2212
2213 case TYPE_CODE_DECFLOAT:
2214 {
2215 /* Any decimal floating-point format. */
2216 return target_float_ops_kind::decimal;
2217 }
2218
2219 default:
2220 gdb_assert_not_reached ("unexpected type code");
2221 }
2222}
2223
2224/* Return target_float_ops to peform operations for KIND. */
2225static const target_float_ops *
2226get_target_float_ops (enum target_float_ops_kind kind)
2227{
2228 switch (kind)
2229 {
2230 /* If the type format matches one of the host floating-point
2231 types, use that type as intermediate format. */
2232 case target_float_ops_kind::host_float:
2233 {
2234 static host_float_ops<float> host_float_ops_float;
2235 return &host_float_ops_float;
2236 }
2237
2238 case target_float_ops_kind::host_double:
2239 {
2240 static host_float_ops<double> host_float_ops_double;
2241 return &host_float_ops_double;
2242 }
2243
2244 case target_float_ops_kind::host_long_double:
2245 {
2246 static host_float_ops<long double> host_float_ops_long_double;
2247 return &host_float_ops_long_double;
2248 }
2249
2250 /* For binary floating-point formats that do not match any host format,
2400729e
UW
2251 use mpfr_t as intermediate format to provide precise target-floating
2252 point emulation. However, if the MPFR library is not availabe,
7a26362d
UW
2253 use the largest host floating-point type as intermediate format. */
2254 case target_float_ops_kind::binary:
2255 {
2400729e
UW
2256#ifdef HAVE_LIBMPFR
2257 static mpfr_float_ops binary_float_ops;
2258#else
7a26362d 2259 static host_float_ops<long double> binary_float_ops;
2400729e 2260#endif
7a26362d
UW
2261 return &binary_float_ops;
2262 }
2263
2264 /* For decimal floating-point types, always use the libdecnumber
2265 decNumber type as intermediate format. */
2266 case target_float_ops_kind::decimal:
2267 {
2268 static decimal_float_ops decimal_float_ops;
2269 return &decimal_float_ops;
2270 }
2271
2272 default:
2273 gdb_assert_not_reached ("unexpected target_float_ops_kind");
2274 }
2275}
2276
2277/* Given a target type TYPE, determine the best host-side intermediate format
2278 to perform operations on TYPE in. */
2279static const target_float_ops *
2280get_target_float_ops (const struct type *type)
2281{
2282 enum target_float_ops_kind kind = get_target_float_ops_kind (type);
2283 return get_target_float_ops (kind);
2284}
2285
2286/* The same for operations involving two target types TYPE1 and TYPE2. */
2287static const target_float_ops *
2288get_target_float_ops (const struct type *type1, const struct type *type2)
2289{
2290 gdb_assert (TYPE_CODE (type1) == TYPE_CODE (type2));
2291
2292 enum target_float_ops_kind kind1 = get_target_float_ops_kind (type1);
2293 enum target_float_ops_kind kind2 = get_target_float_ops_kind (type2);
2294
2295 /* Given the way the kinds are sorted, we simply choose the larger one;
2296 this will be able to hold values of either type. */
2297 return get_target_float_ops (std::max (kind1, kind2));
2298}
2299
70100014
UW
2300/* Return whether the byte-stream ADDR holds a valid value of
2301 floating-point type TYPE. */
2302bool
2303target_float_is_valid (const gdb_byte *addr, const struct type *type)
2304{
2305 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2306 return floatformat_is_valid (floatformat_from_type (type), addr);
2307
2308 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2309 return true;
2310
2311 gdb_assert_not_reached ("unexpected type code");
2312}
2313
2314/* Return whether the byte-stream ADDR, interpreted as floating-point
2315 type TYPE, is numerically equal to zero (of either sign). */
2316bool
2317target_float_is_zero (const gdb_byte *addr, const struct type *type)
2318{
2319 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2320 return (floatformat_classify (floatformat_from_type (type), addr)
2321 == float_zero);
2322
2323 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
7a26362d 2324 return decimal_is_zero (addr, type);
70100014
UW
2325
2326 gdb_assert_not_reached ("unexpected type code");
2327}
2328
f69fdf9b
UW
2329/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
2330 to a string, optionally using the print format FORMAT. */
2331std::string
2332target_float_to_string (const gdb_byte *addr, const struct type *type,
2333 const char *format)
2334{
7a26362d
UW
2335 /* Unless we need to adhere to a specific format, provide special
2336 output for special cases of binary floating-point numbers. */
2337 if (format == nullptr && TYPE_CODE (type) == TYPE_CODE_FLT)
2338 {
2339 const struct floatformat *fmt = floatformat_from_type (type);
f69fdf9b 2340
7a26362d
UW
2341 /* Detect invalid representations. */
2342 if (!floatformat_is_valid (fmt, addr))
2343 return "<invalid float value>";
f69fdf9b 2344
7a26362d
UW
2345 /* Handle NaN and Inf. */
2346 enum float_kind kind = floatformat_classify (fmt, addr);
2347 if (kind == float_nan)
2348 {
2349 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
2350 const char *mantissa = floatformat_mantissa (fmt, addr);
2351 return string_printf ("%snan(0x%s)", sign, mantissa);
2352 }
2353 else if (kind == float_infinite)
2354 {
2355 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
2356 return string_printf ("%sinf", sign);
2357 }
2358 }
2359
2360 const target_float_ops *ops = get_target_float_ops (type);
2361 return ops->to_string (addr, type, format);
f69fdf9b
UW
2362}
2363
2364/* Parse string STRING into a target floating-number of type TYPE and
2365 store it as byte-stream ADDR. Return whether parsing succeeded. */
2366bool
2367target_float_from_string (gdb_byte *addr, const struct type *type,
2368 const std::string &string)
2369{
7a26362d
UW
2370 const target_float_ops *ops = get_target_float_ops (type);
2371 return ops->from_string (addr, type, string);
f69fdf9b 2372}
50637b26
UW
2373
2374/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
2375 to an integer value (rounding towards zero). */
2376LONGEST
2377target_float_to_longest (const gdb_byte *addr, const struct type *type)
2378{
7a26362d
UW
2379 const target_float_ops *ops = get_target_float_ops (type);
2380 return ops->to_longest (addr, type);
50637b26
UW
2381}
2382
2383/* Convert signed integer VAL to a target floating-number of type TYPE
2384 and store it as byte-stream ADDR. */
2385void
2386target_float_from_longest (gdb_byte *addr, const struct type *type,
2387 LONGEST val)
2388{
7a26362d
UW
2389 const target_float_ops *ops = get_target_float_ops (type);
2390 ops->from_longest (addr, type, val);
50637b26
UW
2391}
2392
2393/* Convert unsigned integer VAL to a target floating-number of type TYPE
2394 and store it as byte-stream ADDR. */
2395void
2396target_float_from_ulongest (gdb_byte *addr, const struct type *type,
2397 ULONGEST val)
2398{
7a26362d
UW
2399 const target_float_ops *ops = get_target_float_ops (type);
2400 ops->from_ulongest (addr, type, val);
50637b26
UW
2401}
2402
14ad9311
UW
2403/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
2404 to a floating-point value in the host "double" format. */
2405double
2406target_float_to_host_double (const gdb_byte *addr,
2407 const struct type *type)
2408{
7a26362d
UW
2409 const target_float_ops *ops = get_target_float_ops (type);
2410 return ops->to_host_double (addr, type);
14ad9311
UW
2411}
2412
2413/* Convert floating-point value VAL in the host "double" format to a target
2414 floating-number of type TYPE and store it as byte-stream ADDR. */
2415void
2416target_float_from_host_double (gdb_byte *addr, const struct type *type,
2417 double val)
2418{
7a26362d
UW
2419 const target_float_ops *ops = get_target_float_ops (type);
2420 ops->from_host_double (addr, type, val);
14ad9311
UW
2421}
2422
50637b26
UW
2423/* Convert a floating-point number of type FROM_TYPE from the target
2424 byte-stream FROM to a floating-point number of type TO_TYPE, and
2425 store it to the target byte-stream TO. */
2426void
2427target_float_convert (const gdb_byte *from, const struct type *from_type,
2428 gdb_byte *to, const struct type *to_type)
2429{
50637b26
UW
2430 /* We cannot directly convert between binary and decimal floating-point
2431 types, so go via an intermediary string. */
7a26362d 2432 if (!target_float_same_category_p (from_type, to_type))
50637b26
UW
2433 {
2434 std::string str = target_float_to_string (from, from_type);
2435 target_float_from_string (to, to_type, str);
2436 return;
2437 }
2438
7a26362d
UW
2439 /* Convert between two different formats in the same category. */
2440 if (!target_float_same_format_p (from_type, to_type))
2441 {
2442 const target_float_ops *ops = get_target_float_ops (from_type, to_type);
2443 ops->convert (from, from_type, to, to_type);
2444 return;
2445 }
2446
2447 /* The floating-point formats match, so we simply copy the data, ensuring
2448 possible padding bytes in the target buffer are zeroed out. */
2449 memset (to, 0, TYPE_LENGTH (to_type));
2450 memcpy (to, from, target_float_format_length (to_type));
50637b26 2451}
66c02b9e
UW
2452
2453/* Perform the binary operation indicated by OPCODE, using as operands the
2454 target byte streams X and Y, interpreted as floating-point numbers of
2455 types TYPE_X and TYPE_Y, respectively. Convert the result to type
2456 TYPE_RES and store it into the byte-stream RES.
2457
2458 The three types must either be all binary floating-point types, or else
2459 all decimal floating-point types. Binary and decimal floating-point
2460 types cannot be mixed within a single operation. */
2461void
2462target_float_binop (enum exp_opcode opcode,
2463 const gdb_byte *x, const struct type *type_x,
2464 const gdb_byte *y, const struct type *type_y,
2465 gdb_byte *res, const struct type *type_res)
2466{
7a26362d
UW
2467 gdb_assert (target_float_same_category_p (type_x, type_res));
2468 gdb_assert (target_float_same_category_p (type_y, type_res));
66c02b9e 2469
7a26362d
UW
2470 const target_float_ops *ops = get_target_float_ops (type_x, type_y);
2471 ops->binop (opcode, x, type_x, y, type_y, res, type_res);
66c02b9e
UW
2472}
2473
2474/* Compare the two target byte streams X and Y, interpreted as floating-point
2475 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
2476 are equal, -1 if X is less than Y, and 1 otherwise.
2477
2478 The two types must either both be binary floating-point types, or else
2479 both be decimal floating-point types. Binary and decimal floating-point
2480 types cannot compared directly against each other. */
2481int
2482target_float_compare (const gdb_byte *x, const struct type *type_x,
2483 const gdb_byte *y, const struct type *type_y)
2484{
7a26362d 2485 gdb_assert (target_float_same_category_p (type_x, type_y));
66c02b9e 2486
7a26362d
UW
2487 const target_float_ops *ops = get_target_float_ops (type_x, type_y);
2488 return ops->compare (x, type_x, y, type_y);
66c02b9e
UW
2489}
2490
This page took 0.129238 seconds and 4 git commands to generate.