Silence ARI warning about floatformat_to_double
[deliverable/binutils-gdb.git] / gdb / target-float.c
CommitLineData
70100014
UW
1/* Floating point routines for GDB, the GNU debugger.
2
42a4f53d 3 Copyright (C) 2017-2019 Free Software Foundation, Inc.
70100014
UW
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"
0d12e84c 24#include "gdbarch.h"
70100014 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;
1cfb73db
UW
630 long exponent;
631 unsigned long mant;
632 unsigned int mant_bits, mant_off;
633 int mant_bits_left;
634 int special_exponent; /* It's a NaN, denorm or zero. */
635 enum floatformat_byteorders order;
636 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
637 enum float_kind kind;
638
639 gdb_assert (fmt->totalsize
640 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
641
642 /* For non-numbers, reuse libiberty's logic to find the correct
643 format. We do not lose any precision in this case by passing
644 through a double. */
645 kind = floatformat_classify (fmt, (const bfd_byte *) from);
646 if (kind == float_infinite || kind == float_nan)
647 {
648 double dto;
649
c6cbf900
TT
650 floatformat_to_double /* ARI: floatformat_to_double */
651 (fmt->split_half ? fmt->split_half : fmt, from, &dto);
7a26362d 652 *to = (T) dto;
1cfb73db
UW
653 return;
654 }
655
656 order = floatformat_normalize_byteorder (fmt, ufrom, newfrom);
657
658 if (order != fmt->byteorder)
659 ufrom = newfrom;
660
661 if (fmt->split_half)
662 {
7a26362d 663 T dtop, dbot;
1cfb73db 664
7a26362d 665 from_target (fmt->split_half, ufrom, &dtop);
1cfb73db
UW
666 /* Preserve the sign of 0, which is the sign of the top
667 half. */
668 if (dtop == 0.0)
669 {
670 *to = dtop;
671 return;
672 }
7a26362d
UW
673 from_target (fmt->split_half,
674 ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, &dbot);
1cfb73db
UW
675 *to = dtop + dbot;
676 return;
677 }
678
679 exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start,
680 fmt->exp_len);
681 /* Note that if exponent indicates a NaN, we can't really do anything useful
682 (not knowing if the host has NaN's, or how to build one). So it will
683 end up as an infinity or something close; that is OK. */
684
685 mant_bits_left = fmt->man_len;
686 mant_off = fmt->man_start;
b926417a 687 T dto = 0.0;
1cfb73db
UW
688
689 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
690
691 /* Don't bias NaNs. Use minimum exponent for denorms. For
692 simplicity, we don't check for zero as the exponent doesn't matter.
693 Note the cast to int; exp_bias is unsigned, so it's important to
694 make sure the operation is done in signed arithmetic. */
695 if (!special_exponent)
696 exponent -= fmt->exp_bias;
697 else if (exponent == 0)
698 exponent = 1 - fmt->exp_bias;
699
700 /* Build the result algebraically. Might go infinite, underflow, etc;
701 who cares. */
702
703 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
704 increment the exponent by one to account for the integer bit. */
705
706 if (!special_exponent)
707 {
708 if (fmt->intbit == floatformat_intbit_no)
709 dto = ldexp (1.0, exponent);
710 else
711 exponent++;
712 }
713
714 while (mant_bits_left > 0)
715 {
716 mant_bits = std::min (mant_bits_left, 32);
717
718 mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits);
719
7a26362d 720 dto += ldexp ((T) mant, exponent - mant_bits);
1cfb73db
UW
721 exponent -= mant_bits;
722 mant_off += mant_bits;
723 mant_bits_left -= mant_bits;
724 }
725
726 /* Negate it if negative. */
727 if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1))
728 dto = -dto;
729 *to = dto;
730}
731
7a26362d
UW
732template<typename T> void
733host_float_ops<T>::from_target (const struct type *type,
734 const gdb_byte *from, T *to) const
735{
736 from_target (floatformat_from_type (type), from, to);
737}
738
739/* Convert host floating-point value of type T to target floating-point
740 value in format FMT and store at TO. */
741template<typename T> void
742host_float_ops<T>::to_target (const struct floatformat *fmt,
743 const T *from, gdb_byte *to) const
1cfb73db
UW
744{
745 gdb_assert (fmt != NULL);
746
747 if (fmt == host_float_format)
748 {
749 float val = *from;
750
751 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
752 return;
753 }
754 else if (fmt == host_double_format)
755 {
756 double val = *from;
757
758 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
759 return;
760 }
761 else if (fmt == host_long_double_format)
762 {
763 long double val = *from;
764
765 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
766 return;
767 }
768
7a26362d 769 T dfrom;
1cfb73db 770 int exponent;
7a26362d 771 T mant;
1cfb73db
UW
772 unsigned int mant_bits, mant_off;
773 int mant_bits_left;
774 unsigned char *uto = (unsigned char *) to;
775 enum floatformat_byteorders order = fmt->byteorder;
776 unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
777
778 if (order != floatformat_little)
779 order = floatformat_big;
780
781 if (order != fmt->byteorder)
782 uto = newto;
783
784 memcpy (&dfrom, from, sizeof (dfrom));
785 memset (uto, 0, floatformat_totalsize_bytes (fmt));
786
787 if (fmt->split_half)
788 {
789 /* Use static volatile to ensure that any excess precision is
790 removed via storing in memory, and so the top half really is
791 the result of converting to double. */
792 static volatile double dtop, dbot;
7a26362d 793 T dtopnv, dbotnv;
1cfb73db
UW
794
795 dtop = (double) dfrom;
796 /* If the rounded top half is Inf, the bottom must be 0 not NaN
797 or Inf. */
798 if (dtop + dtop == dtop && dtop != 0.0)
799 dbot = 0.0;
800 else
7a26362d 801 dbot = (double) (dfrom - (T) dtop);
1cfb73db
UW
802 dtopnv = dtop;
803 dbotnv = dbot;
7a26362d
UW
804 to_target (fmt->split_half, &dtopnv, uto);
805 to_target (fmt->split_half, &dbotnv,
806 uto + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2);
1cfb73db
UW
807 return;
808 }
809
810 if (dfrom == 0)
811 goto finalize_byteorder; /* Result is zero */
812 if (dfrom != dfrom) /* Result is NaN */
813 {
814 /* From is NaN */
815 put_field (uto, order, fmt->totalsize, fmt->exp_start,
816 fmt->exp_len, fmt->exp_nan);
817 /* Be sure it's not infinity, but NaN value is irrel. */
818 put_field (uto, order, fmt->totalsize, fmt->man_start,
819 fmt->man_len, 1);
820 goto finalize_byteorder;
821 }
822
823 /* If negative, set the sign bit. */
824 if (dfrom < 0)
825 {
826 put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1);
827 dfrom = -dfrom;
828 }
829
830 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity. */
831 {
832 /* Infinity exponent is same as NaN's. */
833 put_field (uto, order, fmt->totalsize, fmt->exp_start,
834 fmt->exp_len, fmt->exp_nan);
835 /* Infinity mantissa is all zeroes. */
836 put_field (uto, order, fmt->totalsize, fmt->man_start,
837 fmt->man_len, 0);
838 goto finalize_byteorder;
839 }
840
1cfb73db 841 mant = frexp (dfrom, &exponent);
1cfb73db
UW
842
843 if (exponent + fmt->exp_bias <= 0)
844 {
845 /* The value is too small to be expressed in the destination
846 type (not enough bits in the exponent. Treat as 0. */
847 put_field (uto, order, fmt->totalsize, fmt->exp_start,
848 fmt->exp_len, 0);
849 put_field (uto, order, fmt->totalsize, fmt->man_start,
850 fmt->man_len, 0);
851 goto finalize_byteorder;
852 }
853
854 if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
855 {
856 /* The value is too large to fit into the destination.
857 Treat as infinity. */
858 put_field (uto, order, fmt->totalsize, fmt->exp_start,
859 fmt->exp_len, fmt->exp_nan);
860 put_field (uto, order, fmt->totalsize, fmt->man_start,
861 fmt->man_len, 0);
862 goto finalize_byteorder;
863 }
864
865 put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
866 exponent + fmt->exp_bias - 1);
867
868 mant_bits_left = fmt->man_len;
869 mant_off = fmt->man_start;
870 while (mant_bits_left > 0)
871 {
872 unsigned long mant_long;
873
874 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
875
876 mant *= 4294967296.0;
877 mant_long = ((unsigned long) mant) & 0xffffffffL;
878 mant -= mant_long;
879
880 /* If the integer bit is implicit, then we need to discard it.
881 If we are discarding a zero, we should be (but are not) creating
882 a denormalized number which means adjusting the exponent
883 (I think). */
884 if (mant_bits_left == fmt->man_len
885 && fmt->intbit == floatformat_intbit_no)
886 {
887 mant_long <<= 1;
888 mant_long &= 0xffffffffL;
889 /* If we are processing the top 32 mantissa bits of a doublest
890 so as to convert to a float value with implied integer bit,
891 we will only be putting 31 of those 32 bits into the
892 final value due to the discarding of the top bit. In the
893 case of a small float value where the number of mantissa
894 bits is less than 32, discarding the top bit does not alter
895 the number of bits we will be adding to the result. */
896 if (mant_bits == 32)
897 mant_bits -= 1;
898 }
899
900 if (mant_bits < 32)
901 {
902 /* The bits we want are in the most significant MANT_BITS bits of
903 mant_long. Move them to the least significant. */
904 mant_long >>= 32 - mant_bits;
905 }
906
907 put_field (uto, order, fmt->totalsize,
908 mant_off, mant_bits, mant_long);
909 mant_off += mant_bits;
910 mant_bits_left -= mant_bits;
911 }
912
913 finalize_byteorder:
914 /* Do we need to byte-swap the words in the result? */
915 if (order != fmt->byteorder)
916 floatformat_normalize_byteorder (fmt, newto, to);
917}
918
7a26362d
UW
919template<typename T> void
920host_float_ops<T>::to_target (const struct type *type,
921 const T *from, gdb_byte *to) const
1cfb73db 922{
7a26362d
UW
923 /* Ensure possible padding bytes in the target buffer are zeroed out. */
924 memset (to, 0, TYPE_LENGTH (type));
1cfb73db 925
7a26362d
UW
926 to_target (floatformat_from_type (type), from, to);
927}
1cfb73db 928
7a26362d
UW
929/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
930 to a string, optionally using the print format FORMAT. */
931template<typename T> struct printf_length_modifier
932{
933 static constexpr char value = 0;
934};
935template<> struct printf_length_modifier<long double>
936{
937 static constexpr char value = 'L';
938};
939template<typename T> std::string
940host_float_ops<T>::to_string (const gdb_byte *addr, const struct type *type,
941 const char *format) const
942{
1cfb73db 943 /* Determine the format string to use on the host side. */
7a26362d
UW
944 constexpr char length = printf_length_modifier<T>::value;
945 const struct floatformat *fmt = floatformat_from_type (type);
946 std::string host_format = floatformat_printf_format (fmt, format, length);
1cfb73db 947
7a26362d
UW
948 T host_float;
949 from_target (type, addr, &host_float);
f1628857
TT
950
951 DIAGNOSTIC_PUSH
952 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
7a26362d 953 return string_printf (host_format.c_str (), host_float);
f1628857 954 DIAGNOSTIC_POP
1cfb73db
UW
955}
956
7a26362d 957/* Parse string IN into a target floating-number of type TYPE and
1cfb73db 958 store it as byte-stream ADDR. Return whether parsing succeeded. */
7a26362d
UW
959template<typename T> struct scanf_length_modifier
960{
961 static constexpr char value = 0;
962};
963template<> struct scanf_length_modifier<double>
964{
965 static constexpr char value = 'l';
966};
967template<> struct scanf_length_modifier<long double>
968{
969 static constexpr char value = 'L';
970};
971template<typename T> bool
972host_float_ops<T>::from_string (gdb_byte *addr, const struct type *type,
973 const std::string &in) const
1cfb73db 974{
7a26362d 975 T host_float;
1cfb73db 976 int n, num;
7a26362d
UW
977
978 std::string scan_format = "%";
979 if (scanf_length_modifier<T>::value)
980 scan_format += scanf_length_modifier<T>::value;
981 scan_format += "g%n";
982
f1628857
TT
983 DIAGNOSTIC_PUSH
984 DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL
7a26362d 985 num = sscanf (in.c_str (), scan_format.c_str(), &host_float, &n);
f1628857 986 DIAGNOSTIC_POP
1cfb73db
UW
987
988 /* The sscanf man page suggests not making any assumptions on the effect
989 of %n on the result, so we don't.
990 That is why we simply test num == 0. */
991 if (num == 0)
992 return false;
993
994 /* We only accept the whole string. */
995 if (in[n])
996 return false;
997
7a26362d 998 to_target (type, &host_float, addr);
1cfb73db
UW
999 return true;
1000}
1001
7a26362d 1002/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
50637b26 1003 to an integer value (rounding towards zero). */
7a26362d
UW
1004template<typename T> LONGEST
1005host_float_ops<T>::to_longest (const gdb_byte *addr,
1006 const struct type *type) const
50637b26 1007{
7a26362d
UW
1008 T host_float;
1009 from_target (type, addr, &host_float);
9cab7ecd
AT
1010 T min_possible_range = static_cast<T>(std::numeric_limits<LONGEST>::min());
1011 T max_possible_range = -min_possible_range;
1012 /* host_float can be converted to an integer as long as it's in
1013 the range [min_possible_range, max_possible_range). If not, it is either
1014 too large, or too small, or is NaN; in this case return the maximum or
1015 minimum possible value. */
1016 if (host_float < max_possible_range && host_float >= min_possible_range)
1017 return static_cast<LONGEST> (host_float);
1018 if (host_float < min_possible_range)
7a26362d 1019 return std::numeric_limits<LONGEST>::min();
9cab7ecd
AT
1020 /* This line will be executed if host_float is NaN. */
1021 return std::numeric_limits<LONGEST>::max();
50637b26
UW
1022}
1023
7a26362d 1024/* Convert signed integer VAL to a target floating-number of type TYPE
50637b26 1025 and store it as byte-stream ADDR. */
7a26362d
UW
1026template<typename T> void
1027host_float_ops<T>::from_longest (gdb_byte *addr, const struct type *type,
1028 LONGEST val) const
50637b26 1029{
7a26362d
UW
1030 T host_float = (T) val;
1031 to_target (type, &host_float, addr);
50637b26
UW
1032}
1033
7a26362d 1034/* Convert unsigned integer VAL to a target floating-number of type TYPE
50637b26 1035 and store it as byte-stream ADDR. */
7a26362d
UW
1036template<typename T> void
1037host_float_ops<T>::from_ulongest (gdb_byte *addr, const struct type *type,
1038 ULONGEST val) const
50637b26 1039{
7a26362d
UW
1040 T host_float = (T) val;
1041 to_target (type, &host_float, addr);
50637b26
UW
1042}
1043
7a26362d 1044/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
14ad9311 1045 to a floating-point value in the host "double" format. */
7a26362d
UW
1046template<typename T> double
1047host_float_ops<T>::to_host_double (const gdb_byte *addr,
1048 const struct type *type) const
14ad9311 1049{
7a26362d
UW
1050 T host_float;
1051 from_target (type, addr, &host_float);
1052 return (double) host_float;
14ad9311
UW
1053}
1054
1055/* Convert floating-point value VAL in the host "double" format to a target
7a26362d
UW
1056 floating-number of type TYPE and store it as byte-stream ADDR. */
1057template<typename T> void
1058host_float_ops<T>::from_host_double (gdb_byte *addr, const struct type *type,
1059 double val) const
14ad9311 1060{
7a26362d
UW
1061 T host_float = (T) val;
1062 to_target (type, &host_float, addr);
14ad9311
UW
1063}
1064
7a26362d
UW
1065/* Convert a floating-point number of type FROM_TYPE from the target
1066 byte-stream FROM to a floating-point number of type TO_TYPE, and
50637b26 1067 store it to the target byte-stream TO. */
7a26362d
UW
1068template<typename T> void
1069host_float_ops<T>::convert (const gdb_byte *from,
1070 const struct type *from_type,
1071 gdb_byte *to,
1072 const struct type *to_type) const
50637b26 1073{
7a26362d
UW
1074 T host_float;
1075 from_target (from_type, from, &host_float);
1076 to_target (to_type, &host_float, to);
50637b26
UW
1077}
1078
66c02b9e
UW
1079/* Perform the binary operation indicated by OPCODE, using as operands the
1080 target byte streams X and Y, interpreted as floating-point numbers of
7a26362d
UW
1081 types TYPE_X and TYPE_Y, respectively. Convert the result to format
1082 TYPE_RES and store it into the byte-stream RES. */
1083template<typename T> void
1084host_float_ops<T>::binop (enum exp_opcode op,
1085 const gdb_byte *x, const struct type *type_x,
1086 const gdb_byte *y, const struct type *type_y,
1087 gdb_byte *res, const struct type *type_res) const
66c02b9e 1088{
7a26362d 1089 T v1, v2, v = 0;
66c02b9e 1090
7a26362d
UW
1091 from_target (type_x, x, &v1);
1092 from_target (type_y, y, &v2);
66c02b9e
UW
1093
1094 switch (op)
1095 {
1096 case BINOP_ADD:
1097 v = v1 + v2;
1098 break;
1099
1100 case BINOP_SUB:
1101 v = v1 - v2;
1102 break;
1103
1104 case BINOP_MUL:
1105 v = v1 * v2;
1106 break;
1107
1108 case BINOP_DIV:
1109 v = v1 / v2;
1110 break;
1111
1112 case BINOP_EXP:
1113 errno = 0;
1114 v = pow (v1, v2);
1115 if (errno)
1116 error (_("Cannot perform exponentiation: %s"),
1117 safe_strerror (errno));
1118 break;
1119
1120 case BINOP_MIN:
1121 v = v1 < v2 ? v1 : v2;
1122 break;
1123
1124 case BINOP_MAX:
1125 v = v1 > v2 ? v1 : v2;
1126 break;
1127
1128 default:
1129 error (_("Integer-only operation on floating point number."));
1130 break;
1131 }
1132
7a26362d 1133 to_target (type_res, &v, res);
66c02b9e
UW
1134}
1135
1136/* Compare the two target byte streams X and Y, interpreted as floating-point
7a26362d 1137 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
66c02b9e 1138 are equal, -1 if X is less than Y, and 1 otherwise. */
7a26362d
UW
1139template<typename T> int
1140host_float_ops<T>::compare (const gdb_byte *x, const struct type *type_x,
1141 const gdb_byte *y, const struct type *type_y) const
66c02b9e 1142{
7a26362d 1143 T v1, v2;
66c02b9e 1144
7a26362d
UW
1145 from_target (type_x, x, &v1);
1146 from_target (type_y, y, &v2);
66c02b9e
UW
1147
1148 if (v1 == v2)
1149 return 0;
1150 if (v1 < v2)
1151 return -1;
1152 return 1;
1153}
1154
50637b26 1155
2400729e
UW
1156/* Implementation of target_float_ops using the MPFR library
1157 mpfr_t as intermediate type. */
1158
1159#ifdef HAVE_LIBMPFR
1160
75059544
JB
1161#define MPFR_USE_INTMAX_T
1162
2400729e
UW
1163#include <mpfr.h>
1164
1165class mpfr_float_ops : public target_float_ops
1166{
1167public:
1168 std::string to_string (const gdb_byte *addr, const struct type *type,
1169 const char *format) const override;
1170 bool from_string (gdb_byte *addr, const struct type *type,
1171 const std::string &string) const override;
1172
1173 LONGEST to_longest (const gdb_byte *addr,
1174 const struct type *type) const override;
1175 void from_longest (gdb_byte *addr, const struct type *type,
1176 LONGEST val) const override;
1177 void from_ulongest (gdb_byte *addr, const struct type *type,
1178 ULONGEST val) const override;
1179 double to_host_double (const gdb_byte *addr,
1180 const struct type *type) const override;
1181 void from_host_double (gdb_byte *addr, const struct type *type,
1182 double val) const override;
1183 void convert (const gdb_byte *from, const struct type *from_type,
1184 gdb_byte *to, const struct type *to_type) const override;
1185
1186 void binop (enum exp_opcode opcode,
1187 const gdb_byte *x, const struct type *type_x,
1188 const gdb_byte *y, const struct type *type_y,
1189 gdb_byte *res, const struct type *type_res) const override;
1190 int compare (const gdb_byte *x, const struct type *type_x,
1191 const gdb_byte *y, const struct type *type_y) const override;
1192
1193private:
1194 /* Local wrapper class to handle mpfr_t initalization and cleanup. */
1195 class gdb_mpfr
1196 {
1197 public:
1198 mpfr_t val;
1199
1200 gdb_mpfr (const struct type *type)
1201 {
1202 const struct floatformat *fmt = floatformat_from_type (type);
1203 mpfr_init2 (val, floatformat_precision (fmt));
1204 }
1205
1206 gdb_mpfr (const gdb_mpfr &source)
1207 {
1208 mpfr_init2 (val, mpfr_get_prec (source.val));
1209 }
1210
1211 ~gdb_mpfr ()
1212 {
1213 mpfr_clear (val);
1214 }
1215 };
1216
1217 void from_target (const struct floatformat *fmt,
1218 const gdb_byte *from, gdb_mpfr &to) const;
1219 void from_target (const struct type *type,
1220 const gdb_byte *from, gdb_mpfr &to) const;
1221
1222 void to_target (const struct type *type,
1223 const gdb_mpfr &from, gdb_byte *to) const;
1224 void to_target (const struct floatformat *fmt,
1225 const gdb_mpfr &from, gdb_byte *to) const;
1226};
1227
1228
1229/* Convert TO/FROM target floating-point format to mpfr_t. */
1230
1231void
1232mpfr_float_ops::from_target (const struct floatformat *fmt,
1233 const gdb_byte *orig_from, gdb_mpfr &to) const
1234{
1235 const gdb_byte *from = orig_from;
1236 mpfr_exp_t exponent;
1237 unsigned long mant;
1238 unsigned int mant_bits, mant_off;
1239 int mant_bits_left;
1240 int special_exponent; /* It's a NaN, denorm or zero. */
1241 enum floatformat_byteorders order;
1242 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
1243 enum float_kind kind;
1244
1245 gdb_assert (fmt->totalsize
1246 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
1247
1248 /* Handle non-numbers. */
1249 kind = floatformat_classify (fmt, from);
1250 if (kind == float_infinite)
1251 {
1252 mpfr_set_inf (to.val, floatformat_is_negative (fmt, from) ? -1 : 1);
1253 return;
1254 }
1255 if (kind == float_nan)
1256 {
1257 mpfr_set_nan (to.val);
1258 return;
1259 }
1260
1261 order = floatformat_normalize_byteorder (fmt, from, newfrom);
1262
1263 if (order != fmt->byteorder)
1264 from = newfrom;
1265
1266 if (fmt->split_half)
1267 {
1268 gdb_mpfr top (to), bot (to);
1269
1270 from_target (fmt->split_half, from, top);
1271 /* Preserve the sign of 0, which is the sign of the top half. */
1272 if (mpfr_zero_p (top.val))
1273 {
1274 mpfr_set (to.val, top.val, MPFR_RNDN);
1275 return;
1276 }
1277 from_target (fmt->split_half,
1278 from + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, bot);
1279 mpfr_add (to.val, top.val, bot.val, MPFR_RNDN);
1280 return;
1281 }
1282
1283 exponent = get_field (from, order, fmt->totalsize, fmt->exp_start,
1284 fmt->exp_len);
1285 /* Note that if exponent indicates a NaN, we can't really do anything useful
1286 (not knowing if the host has NaN's, or how to build one). So it will
1287 end up as an infinity or something close; that is OK. */
1288
1289 mant_bits_left = fmt->man_len;
1290 mant_off = fmt->man_start;
1291 mpfr_set_zero (to.val, 0);
1292
1293 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
1294
1295 /* Don't bias NaNs. Use minimum exponent for denorms. For
1296 simplicity, we don't check for zero as the exponent doesn't matter.
1297 Note the cast to int; exp_bias is unsigned, so it's important to
1298 make sure the operation is done in signed arithmetic. */
1299 if (!special_exponent)
1300 exponent -= fmt->exp_bias;
1301 else if (exponent == 0)
1302 exponent = 1 - fmt->exp_bias;
1303
1304 /* Build the result algebraically. Might go infinite, underflow, etc;
1305 who cares. */
1306
1307 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
1308 increment the exponent by one to account for the integer bit. */
1309
1310 if (!special_exponent)
1311 {
1312 if (fmt->intbit == floatformat_intbit_no)
1313 mpfr_set_ui_2exp (to.val, 1, exponent, MPFR_RNDN);
1314 else
1315 exponent++;
1316 }
1317
1318 gdb_mpfr tmp (to);
1319
1320 while (mant_bits_left > 0)
1321 {
1322 mant_bits = std::min (mant_bits_left, 32);
1323
1324 mant = get_field (from, order, fmt->totalsize, mant_off, mant_bits);
1325
f2f9e7ec 1326 mpfr_set_ui (tmp.val, mant, MPFR_RNDN);
2400729e
UW
1327 mpfr_mul_2si (tmp.val, tmp.val, exponent - mant_bits, MPFR_RNDN);
1328 mpfr_add (to.val, to.val, tmp.val, MPFR_RNDN);
1329 exponent -= mant_bits;
1330 mant_off += mant_bits;
1331 mant_bits_left -= mant_bits;
1332 }
1333
1334 /* Negate it if negative. */
1335 if (get_field (from, order, fmt->totalsize, fmt->sign_start, 1))
1336 mpfr_neg (to.val, to.val, MPFR_RNDN);
1337}
1338
1339void
1340mpfr_float_ops::from_target (const struct type *type,
1341 const gdb_byte *from, gdb_mpfr &to) const
1342{
1343 from_target (floatformat_from_type (type), from, to);
1344}
1345
1346void
1347mpfr_float_ops::to_target (const struct floatformat *fmt,
1348 const gdb_mpfr &from, gdb_byte *orig_to) const
1349{
1350 unsigned char *to = orig_to;
1351 mpfr_exp_t exponent;
1352 unsigned int mant_bits, mant_off;
1353 int mant_bits_left;
1354 enum floatformat_byteorders order = fmt->byteorder;
1355 unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
1356
1357 if (order != floatformat_little)
1358 order = floatformat_big;
1359
1360 if (order != fmt->byteorder)
1361 to = newto;
1362
1363 memset (to, 0, floatformat_totalsize_bytes (fmt));
1364
1365 if (fmt->split_half)
1366 {
1367 gdb_mpfr top (from), bot (from);
1368
1369 mpfr_set (top.val, from.val, MPFR_RNDN);
1370 /* If the rounded top half is Inf, the bottom must be 0 not NaN
1371 or Inf. */
1372 if (mpfr_inf_p (top.val))
1373 mpfr_set_zero (bot.val, 0);
1374 else
1375 mpfr_sub (bot.val, from.val, top.val, MPFR_RNDN);
1376
1377 to_target (fmt->split_half, top, to);
1378 to_target (fmt->split_half, bot,
1379 to + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2);
1380 return;
1381 }
1382
1383 gdb_mpfr tmp (from);
1384
1385 if (mpfr_zero_p (from.val))
1386 goto finalize_byteorder; /* Result is zero */
1387
1388 mpfr_set (tmp.val, from.val, MPFR_RNDN);
1389
1390 if (mpfr_nan_p (tmp.val)) /* Result is NaN */
1391 {
1392 /* From is NaN */
1393 put_field (to, order, fmt->totalsize, fmt->exp_start,
1394 fmt->exp_len, fmt->exp_nan);
1395 /* Be sure it's not infinity, but NaN value is irrel. */
1396 put_field (to, order, fmt->totalsize, fmt->man_start,
1397 fmt->man_len, 1);
1398 goto finalize_byteorder;
1399 }
1400
1401 /* If negative, set the sign bit. */
1402 if (mpfr_sgn (tmp.val) < 0)
1403 {
1404 put_field (to, order, fmt->totalsize, fmt->sign_start, 1, 1);
1405 mpfr_neg (tmp.val, tmp.val, MPFR_RNDN);
1406 }
1407
1408 if (mpfr_inf_p (tmp.val)) /* Result is Infinity. */
1409 {
1410 /* Infinity exponent is same as NaN's. */
1411 put_field (to, order, fmt->totalsize, fmt->exp_start,
1412 fmt->exp_len, fmt->exp_nan);
1413 /* Infinity mantissa is all zeroes. */
1414 put_field (to, order, fmt->totalsize, fmt->man_start,
1415 fmt->man_len, 0);
1416 goto finalize_byteorder;
1417 }
1418
1419 mpfr_frexp (&exponent, tmp.val, tmp.val, MPFR_RNDN);
1420
1421 if (exponent + fmt->exp_bias <= 0)
1422 {
1423 /* The value is too small to be expressed in the destination
1424 type (not enough bits in the exponent. Treat as 0. */
1425 put_field (to, order, fmt->totalsize, fmt->exp_start,
1426 fmt->exp_len, 0);
1427 put_field (to, order, fmt->totalsize, fmt->man_start,
1428 fmt->man_len, 0);
1429 goto finalize_byteorder;
1430 }
1431
1432 if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
1433 {
1434 /* The value is too large to fit into the destination.
1435 Treat as infinity. */
1436 put_field (to, order, fmt->totalsize, fmt->exp_start,
1437 fmt->exp_len, fmt->exp_nan);
1438 put_field (to, order, fmt->totalsize, fmt->man_start,
1439 fmt->man_len, 0);
1440 goto finalize_byteorder;
1441 }
1442
1443 put_field (to, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
1444 exponent + fmt->exp_bias - 1);
1445
1446 mant_bits_left = fmt->man_len;
1447 mant_off = fmt->man_start;
1448 while (mant_bits_left > 0)
1449 {
1450 unsigned long mant_long;
1451
1452 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
1453
1454 mpfr_mul_2ui (tmp.val, tmp.val, 32, MPFR_RNDN);
1455 mant_long = mpfr_get_ui (tmp.val, MPFR_RNDZ) & 0xffffffffL;
1456 mpfr_sub_ui (tmp.val, tmp.val, mant_long, MPFR_RNDZ);
1457
1458 /* If the integer bit is implicit, then we need to discard it.
1459 If we are discarding a zero, we should be (but are not) creating
1460 a denormalized number which means adjusting the exponent
1461 (I think). */
1462 if (mant_bits_left == fmt->man_len
1463 && fmt->intbit == floatformat_intbit_no)
1464 {
1465 mant_long <<= 1;
1466 mant_long &= 0xffffffffL;
1467 /* If we are processing the top 32 mantissa bits of a doublest
1468 so as to convert to a float value with implied integer bit,
1469 we will only be putting 31 of those 32 bits into the
1470 final value due to the discarding of the top bit. In the
1471 case of a small float value where the number of mantissa
1472 bits is less than 32, discarding the top bit does not alter
1473 the number of bits we will be adding to the result. */
1474 if (mant_bits == 32)
1475 mant_bits -= 1;
1476 }
1477
1478 if (mant_bits < 32)
1479 {
1480 /* The bits we want are in the most significant MANT_BITS bits of
1481 mant_long. Move them to the least significant. */
1482 mant_long >>= 32 - mant_bits;
1483 }
1484
1485 put_field (to, order, fmt->totalsize,
1486 mant_off, mant_bits, mant_long);
1487 mant_off += mant_bits;
1488 mant_bits_left -= mant_bits;
1489 }
1490
1491 finalize_byteorder:
1492 /* Do we need to byte-swap the words in the result? */
1493 if (order != fmt->byteorder)
1494 floatformat_normalize_byteorder (fmt, newto, orig_to);
1495}
1496
1497void
1498mpfr_float_ops::to_target (const struct type *type,
1499 const gdb_mpfr &from, gdb_byte *to) const
1500{
1501 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1502 memset (to, 0, TYPE_LENGTH (type));
1503
1504 to_target (floatformat_from_type (type), from, to);
1505}
1506
1507/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1508 to a string, optionally using the print format FORMAT. */
1509std::string
1510mpfr_float_ops::to_string (const gdb_byte *addr,
1511 const struct type *type,
1512 const char *format) const
1513{
1514 const struct floatformat *fmt = floatformat_from_type (type);
1515
1516 /* Unless we need to adhere to a specific format, provide special
1517 output for certain cases. */
1518 if (format == nullptr)
1519 {
1520 /* Detect invalid representations. */
1521 if (!floatformat_is_valid (fmt, addr))
1522 return "<invalid float value>";
1523
1524 /* Handle NaN and Inf. */
1525 enum float_kind kind = floatformat_classify (fmt, addr);
1526 if (kind == float_nan)
1527 {
1528 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
1529 const char *mantissa = floatformat_mantissa (fmt, addr);
1530 return string_printf ("%snan(0x%s)", sign, mantissa);
1531 }
1532 else if (kind == float_infinite)
1533 {
1534 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
1535 return string_printf ("%sinf", sign);
1536 }
1537 }
1538
1539 /* Determine the format string to use on the host side. */
1540 std::string host_format = floatformat_printf_format (fmt, format, 'R');
1541
1542 gdb_mpfr tmp (type);
1543 from_target (type, addr, tmp);
1544
1545 int size = mpfr_snprintf (NULL, 0, host_format.c_str (), tmp.val);
1546 std::string str (size, '\0');
1547 mpfr_sprintf (&str[0], host_format.c_str (), tmp.val);
1548
1549 return str;
1550}
1551
1552/* Parse string STRING into a target floating-number of type TYPE and
1553 store it as byte-stream ADDR. Return whether parsing succeeded. */
1554bool
1555mpfr_float_ops::from_string (gdb_byte *addr,
1556 const struct type *type,
1557 const std::string &in) const
1558{
1559 gdb_mpfr tmp (type);
1560
1561 char *endptr;
1562 mpfr_strtofr (tmp.val, in.c_str (), &endptr, 0, MPFR_RNDN);
1563
1564 /* We only accept the whole string. */
1565 if (*endptr)
1566 return false;
1567
1568 to_target (type, tmp, addr);
1569 return true;
1570}
1571
1572/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1573 to an integer value (rounding towards zero). */
1574LONGEST
1575mpfr_float_ops::to_longest (const gdb_byte *addr,
1576 const struct type *type) const
1577{
1578 gdb_mpfr tmp (type);
1579 from_target (type, addr, tmp);
1580 return mpfr_get_sj (tmp.val, MPFR_RNDZ);
1581}
1582
1583/* Convert signed integer VAL to a target floating-number of type TYPE
1584 and store it as byte-stream ADDR. */
1585void
1586mpfr_float_ops::from_longest (gdb_byte *addr,
1587 const struct type *type,
1588 LONGEST val) const
1589{
1590 gdb_mpfr tmp (type);
1591 mpfr_set_sj (tmp.val, val, MPFR_RNDN);
1592 to_target (type, tmp, addr);
1593}
1594
1595/* Convert unsigned integer VAL to a target floating-number of type TYPE
1596 and store it as byte-stream ADDR. */
1597void
1598mpfr_float_ops::from_ulongest (gdb_byte *addr,
1599 const struct type *type,
1600 ULONGEST val) const
1601{
1602 gdb_mpfr tmp (type);
1603 mpfr_set_uj (tmp.val, val, MPFR_RNDN);
1604 to_target (type, tmp, addr);
1605}
1606
1607/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1608 to a floating-point value in the host "double" format. */
1609double
1610mpfr_float_ops::to_host_double (const gdb_byte *addr,
1611 const struct type *type) const
1612{
1613 gdb_mpfr tmp (type);
1614 from_target (type, addr, tmp);
1615 return mpfr_get_d (tmp.val, MPFR_RNDN);
1616}
1617
1618/* Convert floating-point value VAL in the host "double" format to a target
1619 floating-number of type TYPE and store it as byte-stream ADDR. */
1620void
1621mpfr_float_ops::from_host_double (gdb_byte *addr,
1622 const struct type *type,
1623 double val) const
1624{
1625 gdb_mpfr tmp (type);
1626 mpfr_set_d (tmp.val, val, MPFR_RNDN);
1627 to_target (type, tmp, addr);
1628}
1629
1630/* Convert a floating-point number of type FROM_TYPE from the target
1631 byte-stream FROM to a floating-point number of type TO_TYPE, and
1632 store it to the target byte-stream TO. */
1633void
1634mpfr_float_ops::convert (const gdb_byte *from,
1635 const struct type *from_type,
1636 gdb_byte *to,
1637 const struct type *to_type) const
1638{
1639 gdb_mpfr from_tmp (from_type), to_tmp (to_type);
1640 from_target (from_type, from, from_tmp);
1641 mpfr_set (to_tmp.val, from_tmp.val, MPFR_RNDN);
1642 to_target (to_type, to_tmp, to);
1643}
1644
1645/* Perform the binary operation indicated by OPCODE, using as operands the
1646 target byte streams X and Y, interpreted as floating-point numbers of
1647 types TYPE_X and TYPE_Y, respectively. Convert the result to type
1648 TYPE_RES and store it into the byte-stream RES. */
1649void
1650mpfr_float_ops::binop (enum exp_opcode op,
1651 const gdb_byte *x, const struct type *type_x,
1652 const gdb_byte *y, const struct type *type_y,
1653 gdb_byte *res, const struct type *type_res) const
1654{
1655 gdb_mpfr x_tmp (type_x), y_tmp (type_y), tmp (type_res);
1656
1657 from_target (type_x, x, x_tmp);
1658 from_target (type_y, y, y_tmp);
1659
1660 switch (op)
1661 {
1662 case BINOP_ADD:
1663 mpfr_add (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1664 break;
1665
1666 case BINOP_SUB:
1667 mpfr_sub (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1668 break;
1669
1670 case BINOP_MUL:
1671 mpfr_mul (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1672 break;
1673
1674 case BINOP_DIV:
1675 mpfr_div (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1676 break;
1677
1678 case BINOP_EXP:
1679 mpfr_pow (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1680 break;
1681
1682 case BINOP_MIN:
1683 mpfr_min (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1684 break;
1685
1686 case BINOP_MAX:
1687 mpfr_max (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN);
1688 break;
1689
1690 default:
1691 error (_("Integer-only operation on floating point number."));
1692 break;
1693 }
1694
1695 to_target (type_res, tmp, res);
1696}
1697
1698/* Compare the two target byte streams X and Y, interpreted as floating-point
1699 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
1700 are equal, -1 if X is less than Y, and 1 otherwise. */
1701int
1702mpfr_float_ops::compare (const gdb_byte *x, const struct type *type_x,
1703 const gdb_byte *y, const struct type *type_y) const
1704{
1705 gdb_mpfr x_tmp (type_x), y_tmp (type_y);
1706
1707 from_target (type_x, x, x_tmp);
1708 from_target (type_y, y, y_tmp);
1709
1710 if (mpfr_equal_p (x_tmp.val, y_tmp.val))
1711 return 0;
1712 else if (mpfr_less_p (x_tmp.val, y_tmp.val))
1713 return -1;
1714 else
1715 return 1;
1716}
1717
1718#endif
1719
1720
1cfb73db
UW
1721/* Helper routines operating on decimal floating-point data. */
1722
1723/* Decimal floating point is one of the extension to IEEE 754, which is
1724 described in http://grouper.ieee.org/groups/754/revision.html and
1725 http://www2.hursley.ibm.com/decimal/. It completes binary floating
1726 point by representing floating point more exactly. */
1727
1728/* The order of the following headers is important for making sure
1729 decNumber structure is large enough to hold decimal128 digits. */
1730
1731#include "dpd/decimal128.h"
1732#include "dpd/decimal64.h"
1733#include "dpd/decimal32.h"
1734
1735/* When using decimal128, this is the maximum string length + 1
1736 (value comes from libdecnumber's DECIMAL128_String constant). */
1737#define MAX_DECIMAL_STRING 43
1738
1739/* In GDB, we are using an array of gdb_byte to represent decimal values.
1740 They are stored in host byte order. This routine does the conversion if
1741 the target byte order is different. */
1742static void
7a26362d 1743match_endianness (const gdb_byte *from, const struct type *type, gdb_byte *to)
1cfb73db 1744{
7a26362d
UW
1745 gdb_assert (TYPE_CODE (type) == TYPE_CODE_DECFLOAT);
1746
1747 int len = TYPE_LENGTH (type);
1cfb73db
UW
1748 int i;
1749
1750#if WORDS_BIGENDIAN
1751#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE
1752#else
1753#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
1754#endif
1755
34877895 1756 if (type_byte_order (type) == OPPOSITE_BYTE_ORDER)
1cfb73db
UW
1757 for (i = 0; i < len; i++)
1758 to[i] = from[len - i - 1];
1759 else
1760 for (i = 0; i < len; i++)
1761 to[i] = from[i];
1762
1763 return;
1764}
1765
1766/* Helper function to get the appropriate libdecnumber context for each size
1767 of decimal float. */
1768static void
7a26362d 1769set_decnumber_context (decContext *ctx, const struct type *type)
1cfb73db 1770{
7a26362d
UW
1771 gdb_assert (TYPE_CODE (type) == TYPE_CODE_DECFLOAT);
1772
1773 switch (TYPE_LENGTH (type))
1cfb73db
UW
1774 {
1775 case 4:
1776 decContextDefault (ctx, DEC_INIT_DECIMAL32);
1777 break;
1778 case 8:
1779 decContextDefault (ctx, DEC_INIT_DECIMAL64);
1780 break;
1781 case 16:
1782 decContextDefault (ctx, DEC_INIT_DECIMAL128);
1783 break;
1784 }
1785
1786 ctx->traps = 0;
1787}
1788
1789/* Check for errors signaled in the decimal context structure. */
1790static void
1791decimal_check_errors (decContext *ctx)
1792{
1793 /* An error here could be a division by zero, an overflow, an underflow or
1794 an invalid operation (from the DEC_Errors constant in decContext.h).
1795 Since GDB doesn't complain about division by zero, overflow or underflow
1796 errors for binary floating, we won't complain about them for decimal
1797 floating either. */
1798 if (ctx->status & DEC_IEEE_854_Invalid_operation)
1799 {
1800 /* Leave only the error bits in the status flags. */
1801 ctx->status &= DEC_IEEE_854_Invalid_operation;
1802 error (_("Cannot perform operation: %s"),
1803 decContextStatusToString (ctx));
1804 }
1805}
1806
1807/* Helper function to convert from libdecnumber's appropriate representation
1808 for computation to each size of decimal float. */
1809static void
d7236961 1810decimal_from_number (const decNumber *from,
7a26362d 1811 gdb_byte *to, const struct type *type)
1cfb73db 1812{
d7236961
UW
1813 gdb_byte dec[16];
1814
1cfb73db
UW
1815 decContext set;
1816
7a26362d 1817 set_decnumber_context (&set, type);
1cfb73db 1818
7a26362d 1819 switch (TYPE_LENGTH (type))
1cfb73db
UW
1820 {
1821 case 4:
d7236961 1822 decimal32FromNumber ((decimal32 *) dec, from, &set);
1cfb73db
UW
1823 break;
1824 case 8:
d7236961 1825 decimal64FromNumber ((decimal64 *) dec, from, &set);
1cfb73db
UW
1826 break;
1827 case 16:
d7236961
UW
1828 decimal128FromNumber ((decimal128 *) dec, from, &set);
1829 break;
1830 default:
1831 error (_("Unknown decimal floating point type."));
1cfb73db
UW
1832 break;
1833 }
d7236961 1834
7a26362d 1835 match_endianness (dec, type, to);
1cfb73db
UW
1836}
1837
1838/* Helper function to convert each size of decimal float to libdecnumber's
1839 appropriate representation for computation. */
1840static void
7a26362d 1841decimal_to_number (const gdb_byte *addr, const struct type *type,
d7236961 1842 decNumber *to)
1cfb73db 1843{
d7236961 1844 gdb_byte dec[16];
7a26362d 1845 match_endianness (addr, type, dec);
d7236961 1846
7a26362d 1847 switch (TYPE_LENGTH (type))
1cfb73db
UW
1848 {
1849 case 4:
d7236961 1850 decimal32ToNumber ((decimal32 *) dec, to);
1cfb73db
UW
1851 break;
1852 case 8:
d7236961 1853 decimal64ToNumber ((decimal64 *) dec, to);
1cfb73db
UW
1854 break;
1855 case 16:
d7236961 1856 decimal128ToNumber ((decimal128 *) dec, to);
1cfb73db
UW
1857 break;
1858 default:
1859 error (_("Unknown decimal floating point type."));
1860 break;
1861 }
1862}
1863
7a26362d
UW
1864/* Returns true if ADDR (which is of type TYPE) is the number zero. */
1865static bool
1866decimal_is_zero (const gdb_byte *addr, const struct type *type)
1867{
1868 decNumber number;
1869
1870 decimal_to_number (addr, type, &number);
1871
1872 return decNumberIsZero (&number);
1873}
1874
1875
1876/* Implementation of target_float_ops using the libdecnumber decNumber type
1877 as intermediate format. */
1878
1879class decimal_float_ops : public target_float_ops
1880{
1881public:
1882 std::string to_string (const gdb_byte *addr, const struct type *type,
1883 const char *format) const override;
1884 bool from_string (gdb_byte *addr, const struct type *type,
1885 const std::string &string) const override;
1886
1887 LONGEST to_longest (const gdb_byte *addr,
1888 const struct type *type) const override;
1889 void from_longest (gdb_byte *addr, const struct type *type,
1890 LONGEST val) const override;
1891 void from_ulongest (gdb_byte *addr, const struct type *type,
1892 ULONGEST val) const override;
1893 double to_host_double (const gdb_byte *addr,
1894 const struct type *type) const override
1895 {
1896 /* We don't support conversions between target decimal floating-point
1897 types and the host double type. */
1898 gdb_assert_not_reached ("invalid operation on decimal float");
1899 }
1900 void from_host_double (gdb_byte *addr, const struct type *type,
1901 double val) const override
1902 {
1903 /* We don't support conversions between target decimal floating-point
1904 types and the host double type. */
1905 gdb_assert_not_reached ("invalid operation on decimal float");
1906 }
1907 void convert (const gdb_byte *from, const struct type *from_type,
1908 gdb_byte *to, const struct type *to_type) const override;
1909
1910 void binop (enum exp_opcode opcode,
1911 const gdb_byte *x, const struct type *type_x,
1912 const gdb_byte *y, const struct type *type_y,
1913 gdb_byte *res, const struct type *type_res) const override;
1914 int compare (const gdb_byte *x, const struct type *type_x,
1915 const gdb_byte *y, const struct type *type_y) const override;
1916};
1917
1cfb73db
UW
1918/* Convert decimal type to its string representation. LEN is the length
1919 of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
1920 16 bytes for decimal128. */
7a26362d
UW
1921std::string
1922decimal_float_ops::to_string (const gdb_byte *addr, const struct type *type,
1923 const char *format = nullptr) const
1cfb73db
UW
1924{
1925 gdb_byte dec[16];
1926
7a26362d 1927 match_endianness (addr, type, dec);
1cfb73db
UW
1928
1929 if (format != nullptr)
1930 {
1931 /* We don't handle format strings (yet). If the host printf supports
1932 decimal floating point types, just use this. Otherwise, fall back
1933 to printing the number while ignoring the format string. */
1934#if defined (PRINTF_HAS_DECFLOAT)
1935 /* FIXME: This makes unwarranted assumptions about the host ABI! */
1936 return string_printf (format, dec);
1937#endif
1938 }
1939
1940 std::string result;
1941 result.resize (MAX_DECIMAL_STRING);
1942
7a26362d 1943 switch (TYPE_LENGTH (type))
1cfb73db
UW
1944 {
1945 case 4:
1946 decimal32ToString ((decimal32 *) dec, &result[0]);
1947 break;
1948 case 8:
1949 decimal64ToString ((decimal64 *) dec, &result[0]);
1950 break;
1951 case 16:
1952 decimal128ToString ((decimal128 *) dec, &result[0]);
1953 break;
1954 default:
1955 error (_("Unknown decimal floating point type."));
1956 break;
1957 }
1958
1959 return result;
1960}
1961
1962/* Convert the string form of a decimal value to its decimal representation.
1963 LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
1964 decimal64 and 16 bytes for decimal128. */
7a26362d
UW
1965bool
1966decimal_float_ops::from_string (gdb_byte *addr, const struct type *type,
1967 const std::string &string) const
1cfb73db
UW
1968{
1969 decContext set;
1970 gdb_byte dec[16];
1971
7a26362d 1972 set_decnumber_context (&set, type);
1cfb73db 1973
7a26362d 1974 switch (TYPE_LENGTH (type))
1cfb73db
UW
1975 {
1976 case 4:
1977 decimal32FromString ((decimal32 *) dec, string.c_str (), &set);
1978 break;
1979 case 8:
1980 decimal64FromString ((decimal64 *) dec, string.c_str (), &set);
1981 break;
1982 case 16:
1983 decimal128FromString ((decimal128 *) dec, string.c_str (), &set);
1984 break;
1985 default:
1986 error (_("Unknown decimal floating point type."));
1987 break;
1988 }
1989
7a26362d 1990 match_endianness (dec, type, addr);
1cfb73db
UW
1991
1992 /* Check for errors in the DFP operation. */
1993 decimal_check_errors (&set);
1994
1995 return true;
1996}
1997
1998/* Converts a LONGEST to a decimal float of specified LEN bytes. */
7a26362d
UW
1999void
2000decimal_float_ops::from_longest (gdb_byte *addr, const struct type *type,
2001 LONGEST from) const
1cfb73db 2002{
1cfb73db 2003 decNumber number;
d7236961 2004
1cfb73db
UW
2005 if ((int32_t) from != from)
2006 /* libdecnumber can convert only 32-bit integers. */
2007 error (_("Conversion of large integer to a "
2008 "decimal floating type is not supported."));
2009
2010 decNumberFromInt32 (&number, (int32_t) from);
2011
7a26362d 2012 decimal_from_number (&number, addr, type);
1cfb73db
UW
2013}
2014
2015/* Converts a ULONGEST to a decimal float of specified LEN bytes. */
7a26362d
UW
2016void
2017decimal_float_ops::from_ulongest (gdb_byte *addr, const struct type *type,
2018 ULONGEST from) const
1cfb73db 2019{
1cfb73db
UW
2020 decNumber number;
2021
2022 if ((uint32_t) from != from)
2023 /* libdecnumber can convert only 32-bit integers. */
2024 error (_("Conversion of large integer to a "
2025 "decimal floating type is not supported."));
2026
2027 decNumberFromUInt32 (&number, (uint32_t) from);
2028
7a26362d 2029 decimal_from_number (&number, addr, type);
1cfb73db
UW
2030}
2031
2032/* Converts a decimal float of LEN bytes to a LONGEST. */
7a26362d
UW
2033LONGEST
2034decimal_float_ops::to_longest (const gdb_byte *addr,
2035 const struct type *type) const
1cfb73db
UW
2036{
2037 /* libdecnumber has a function to convert from decimal to integer, but
2038 it doesn't work when the decimal number has a fractional part. */
7a26362d 2039 std::string str = to_string (addr, type);
1cfb73db
UW
2040 return strtoll (str.c_str (), NULL, 10);
2041}
2042
2043/* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
2044 and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
2045 RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */
7a26362d
UW
2046void
2047decimal_float_ops::binop (enum exp_opcode op,
2048 const gdb_byte *x, const struct type *type_x,
2049 const gdb_byte *y, const struct type *type_y,
2050 gdb_byte *res, const struct type *type_res) const
1cfb73db
UW
2051{
2052 decContext set;
2053 decNumber number1, number2, number3;
1cfb73db 2054
7a26362d
UW
2055 decimal_to_number (x, type_x, &number1);
2056 decimal_to_number (y, type_y, &number2);
1cfb73db 2057
7a26362d 2058 set_decnumber_context (&set, type_res);
1cfb73db
UW
2059
2060 switch (op)
2061 {
2062 case BINOP_ADD:
2063 decNumberAdd (&number3, &number1, &number2, &set);
2064 break;
2065 case BINOP_SUB:
2066 decNumberSubtract (&number3, &number1, &number2, &set);
2067 break;
2068 case BINOP_MUL:
2069 decNumberMultiply (&number3, &number1, &number2, &set);
2070 break;
2071 case BINOP_DIV:
2072 decNumberDivide (&number3, &number1, &number2, &set);
2073 break;
2074 case BINOP_EXP:
2075 decNumberPower (&number3, &number1, &number2, &set);
2076 break;
2077 default:
2078 error (_("Operation not valid for decimal floating point number."));
2079 break;
2080 }
2081
2082 /* Check for errors in the DFP operation. */
2083 decimal_check_errors (&set);
2084
7a26362d 2085 decimal_from_number (&number3, res, type_res);
1cfb73db
UW
2086}
2087
2088/* Compares two numbers numerically. If X is less than Y then the return value
2089 will be -1. If they are equal, then the return value will be 0. If X is
2090 greater than the Y then the return value will be 1. */
7a26362d
UW
2091int
2092decimal_float_ops::compare (const gdb_byte *x, const struct type *type_x,
2093 const gdb_byte *y, const struct type *type_y) const
1cfb73db
UW
2094{
2095 decNumber number1, number2, result;
2096 decContext set;
7a26362d 2097 const struct type *type_result;
1cfb73db 2098
7a26362d
UW
2099 decimal_to_number (x, type_x, &number1);
2100 decimal_to_number (y, type_y, &number2);
1cfb73db
UW
2101
2102 /* Perform the comparison in the larger of the two sizes. */
7a26362d
UW
2103 type_result = TYPE_LENGTH (type_x) > TYPE_LENGTH (type_y) ? type_x : type_y;
2104 set_decnumber_context (&set, type_result);
1cfb73db
UW
2105
2106 decNumberCompare (&result, &number1, &number2, &set);
2107
2108 /* Check for errors in the DFP operation. */
2109 decimal_check_errors (&set);
2110
2111 if (decNumberIsNaN (&result))
2112 error (_("Comparison with an invalid number (NaN)."));
2113 else if (decNumberIsZero (&result))
2114 return 0;
2115 else if (decNumberIsNegative (&result))
2116 return -1;
2117 else
2118 return 1;
2119}
2120
2121/* Convert a decimal value from a decimal type with LEN_FROM bytes to a
2122 decimal type with LEN_TO bytes. */
7a26362d
UW
2123void
2124decimal_float_ops::convert (const gdb_byte *from, const struct type *from_type,
2125 gdb_byte *to, const struct type *to_type) const
1cfb73db
UW
2126{
2127 decNumber number;
1cfb73db 2128
7a26362d
UW
2129 decimal_to_number (from, from_type, &number);
2130 decimal_from_number (&number, to, to_type);
1cfb73db
UW
2131}
2132
2133
70100014
UW
2134/* Typed floating-point routines. These routines operate on floating-point
2135 values in target format, represented by a byte buffer interpreted as a
2136 "struct type", which may be either a binary or decimal floating-point
2137 type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */
2138
7a26362d
UW
2139/* Return whether TYPE1 and TYPE2 are of the same category (binary or
2140 decimal floating-point). */
2141static bool
2142target_float_same_category_p (const struct type *type1,
2143 const struct type *type2)
2144{
2145 return TYPE_CODE (type1) == TYPE_CODE (type2);
2146}
2147
2148/* Return whether TYPE1 and TYPE2 use the same floating-point format. */
2149static bool
2150target_float_same_format_p (const struct type *type1,
2151 const struct type *type2)
2152{
2153 if (!target_float_same_category_p (type1, type2))
2154 return false;
2155
2156 switch (TYPE_CODE (type1))
2157 {
2158 case TYPE_CODE_FLT:
2159 return floatformat_from_type (type1) == floatformat_from_type (type2);
2160
2161 case TYPE_CODE_DECFLOAT:
2162 return (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
34877895
PJ
2163 && (type_byte_order (type1)
2164 == type_byte_order (type2)));
7a26362d
UW
2165
2166 default:
2167 gdb_assert_not_reached ("unexpected type code");
2168 }
2169}
2170
2171/* Return the size (without padding) of the target floating-point
2172 format used by TYPE. */
2173static int
2174target_float_format_length (const struct type *type)
2175{
2176 switch (TYPE_CODE (type))
2177 {
2178 case TYPE_CODE_FLT:
2179 return floatformat_totalsize_bytes (floatformat_from_type (type));
2180
2181 case TYPE_CODE_DECFLOAT:
2182 return TYPE_LENGTH (type);
2183
2184 default:
2185 gdb_assert_not_reached ("unexpected type code");
2186 }
2187}
2188
2189/* Identifiers of available host-side intermediate formats. These must
2190 be sorted so the that the more "general" kinds come later. */
2191enum target_float_ops_kind
2192{
2193 /* Target binary floating-point formats that match a host format. */
2194 host_float = 0,
2195 host_double,
2196 host_long_double,
2197 /* Any other target binary floating-point format. */
2198 binary,
2199 /* Any target decimal floating-point format. */
2200 decimal
2201};
2202
2203/* Given a target type TYPE, choose the best host-side intermediate format
2204 to perform operations on TYPE in. */
2205static enum target_float_ops_kind
2206get_target_float_ops_kind (const struct type *type)
2207{
2208 switch (TYPE_CODE (type))
2209 {
2210 case TYPE_CODE_FLT:
2211 {
2212 const struct floatformat *fmt = floatformat_from_type (type);
2213
2214 /* Binary floating-point formats matching a host format. */
2215 if (fmt == host_float_format)
2216 return target_float_ops_kind::host_float;
2217 if (fmt == host_double_format)
2218 return target_float_ops_kind::host_double;
2219 if (fmt == host_long_double_format)
2220 return target_float_ops_kind::host_long_double;
2221
2222 /* Any other binary floating-point format. */
2223 return target_float_ops_kind::binary;
2224 }
2225
2226 case TYPE_CODE_DECFLOAT:
2227 {
2228 /* Any decimal floating-point format. */
2229 return target_float_ops_kind::decimal;
2230 }
2231
2232 default:
2233 gdb_assert_not_reached ("unexpected type code");
2234 }
2235}
2236
2237/* Return target_float_ops to peform operations for KIND. */
2238static const target_float_ops *
2239get_target_float_ops (enum target_float_ops_kind kind)
2240{
2241 switch (kind)
2242 {
2243 /* If the type format matches one of the host floating-point
2244 types, use that type as intermediate format. */
2245 case target_float_ops_kind::host_float:
2246 {
2247 static host_float_ops<float> host_float_ops_float;
2248 return &host_float_ops_float;
2249 }
2250
2251 case target_float_ops_kind::host_double:
2252 {
2253 static host_float_ops<double> host_float_ops_double;
2254 return &host_float_ops_double;
2255 }
2256
2257 case target_float_ops_kind::host_long_double:
2258 {
2259 static host_float_ops<long double> host_float_ops_long_double;
2260 return &host_float_ops_long_double;
2261 }
2262
2263 /* For binary floating-point formats that do not match any host format,
2400729e 2264 use mpfr_t as intermediate format to provide precise target-floating
85102364 2265 point emulation. However, if the MPFR library is not available,
7a26362d
UW
2266 use the largest host floating-point type as intermediate format. */
2267 case target_float_ops_kind::binary:
2268 {
2400729e
UW
2269#ifdef HAVE_LIBMPFR
2270 static mpfr_float_ops binary_float_ops;
2271#else
7a26362d 2272 static host_float_ops<long double> binary_float_ops;
2400729e 2273#endif
7a26362d
UW
2274 return &binary_float_ops;
2275 }
2276
2277 /* For decimal floating-point types, always use the libdecnumber
2278 decNumber type as intermediate format. */
2279 case target_float_ops_kind::decimal:
2280 {
2281 static decimal_float_ops decimal_float_ops;
2282 return &decimal_float_ops;
2283 }
2284
2285 default:
2286 gdb_assert_not_reached ("unexpected target_float_ops_kind");
2287 }
2288}
2289
2290/* Given a target type TYPE, determine the best host-side intermediate format
2291 to perform operations on TYPE in. */
2292static const target_float_ops *
2293get_target_float_ops (const struct type *type)
2294{
2295 enum target_float_ops_kind kind = get_target_float_ops_kind (type);
2296 return get_target_float_ops (kind);
2297}
2298
2299/* The same for operations involving two target types TYPE1 and TYPE2. */
2300static const target_float_ops *
2301get_target_float_ops (const struct type *type1, const struct type *type2)
2302{
2303 gdb_assert (TYPE_CODE (type1) == TYPE_CODE (type2));
2304
2305 enum target_float_ops_kind kind1 = get_target_float_ops_kind (type1);
2306 enum target_float_ops_kind kind2 = get_target_float_ops_kind (type2);
2307
2308 /* Given the way the kinds are sorted, we simply choose the larger one;
2309 this will be able to hold values of either type. */
2310 return get_target_float_ops (std::max (kind1, kind2));
2311}
2312
70100014
UW
2313/* Return whether the byte-stream ADDR holds a valid value of
2314 floating-point type TYPE. */
2315bool
2316target_float_is_valid (const gdb_byte *addr, const struct type *type)
2317{
2318 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2319 return floatformat_is_valid (floatformat_from_type (type), addr);
2320
2321 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2322 return true;
2323
2324 gdb_assert_not_reached ("unexpected type code");
2325}
2326
2327/* Return whether the byte-stream ADDR, interpreted as floating-point
2328 type TYPE, is numerically equal to zero (of either sign). */
2329bool
2330target_float_is_zero (const gdb_byte *addr, const struct type *type)
2331{
2332 if (TYPE_CODE (type) == TYPE_CODE_FLT)
2333 return (floatformat_classify (floatformat_from_type (type), addr)
2334 == float_zero);
2335
2336 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
7a26362d 2337 return decimal_is_zero (addr, type);
70100014
UW
2338
2339 gdb_assert_not_reached ("unexpected type code");
2340}
2341
f69fdf9b
UW
2342/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
2343 to a string, optionally using the print format FORMAT. */
2344std::string
2345target_float_to_string (const gdb_byte *addr, const struct type *type,
2346 const char *format)
2347{
7a26362d
UW
2348 /* Unless we need to adhere to a specific format, provide special
2349 output for special cases of binary floating-point numbers. */
2350 if (format == nullptr && TYPE_CODE (type) == TYPE_CODE_FLT)
2351 {
2352 const struct floatformat *fmt = floatformat_from_type (type);
f69fdf9b 2353
7a26362d
UW
2354 /* Detect invalid representations. */
2355 if (!floatformat_is_valid (fmt, addr))
2356 return "<invalid float value>";
f69fdf9b 2357
7a26362d
UW
2358 /* Handle NaN and Inf. */
2359 enum float_kind kind = floatformat_classify (fmt, addr);
2360 if (kind == float_nan)
2361 {
2362 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
2363 const char *mantissa = floatformat_mantissa (fmt, addr);
2364 return string_printf ("%snan(0x%s)", sign, mantissa);
2365 }
2366 else if (kind == float_infinite)
2367 {
2368 const char *sign = floatformat_is_negative (fmt, addr)? "-" : "";
2369 return string_printf ("%sinf", sign);
2370 }
2371 }
2372
2373 const target_float_ops *ops = get_target_float_ops (type);
2374 return ops->to_string (addr, type, format);
f69fdf9b
UW
2375}
2376
2377/* Parse string STRING into a target floating-number of type TYPE and
2378 store it as byte-stream ADDR. Return whether parsing succeeded. */
2379bool
2380target_float_from_string (gdb_byte *addr, const struct type *type,
2381 const std::string &string)
2382{
7a26362d
UW
2383 const target_float_ops *ops = get_target_float_ops (type);
2384 return ops->from_string (addr, type, string);
f69fdf9b 2385}
50637b26
UW
2386
2387/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
2388 to an integer value (rounding towards zero). */
2389LONGEST
2390target_float_to_longest (const gdb_byte *addr, const struct type *type)
2391{
7a26362d
UW
2392 const target_float_ops *ops = get_target_float_ops (type);
2393 return ops->to_longest (addr, type);
50637b26
UW
2394}
2395
2396/* Convert signed integer VAL to a target floating-number of type TYPE
2397 and store it as byte-stream ADDR. */
2398void
2399target_float_from_longest (gdb_byte *addr, const struct type *type,
2400 LONGEST val)
2401{
7a26362d
UW
2402 const target_float_ops *ops = get_target_float_ops (type);
2403 ops->from_longest (addr, type, val);
50637b26
UW
2404}
2405
2406/* Convert unsigned integer VAL to a target floating-number of type TYPE
2407 and store it as byte-stream ADDR. */
2408void
2409target_float_from_ulongest (gdb_byte *addr, const struct type *type,
2410 ULONGEST val)
2411{
7a26362d
UW
2412 const target_float_ops *ops = get_target_float_ops (type);
2413 ops->from_ulongest (addr, type, val);
50637b26
UW
2414}
2415
14ad9311
UW
2416/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
2417 to a floating-point value in the host "double" format. */
2418double
2419target_float_to_host_double (const gdb_byte *addr,
2420 const struct type *type)
2421{
7a26362d
UW
2422 const target_float_ops *ops = get_target_float_ops (type);
2423 return ops->to_host_double (addr, type);
14ad9311
UW
2424}
2425
2426/* Convert floating-point value VAL in the host "double" format to a target
2427 floating-number of type TYPE and store it as byte-stream ADDR. */
2428void
2429target_float_from_host_double (gdb_byte *addr, const struct type *type,
2430 double val)
2431{
7a26362d
UW
2432 const target_float_ops *ops = get_target_float_ops (type);
2433 ops->from_host_double (addr, type, val);
14ad9311
UW
2434}
2435
50637b26
UW
2436/* Convert a floating-point number of type FROM_TYPE from the target
2437 byte-stream FROM to a floating-point number of type TO_TYPE, and
2438 store it to the target byte-stream TO. */
2439void
2440target_float_convert (const gdb_byte *from, const struct type *from_type,
2441 gdb_byte *to, const struct type *to_type)
2442{
50637b26
UW
2443 /* We cannot directly convert between binary and decimal floating-point
2444 types, so go via an intermediary string. */
7a26362d 2445 if (!target_float_same_category_p (from_type, to_type))
50637b26
UW
2446 {
2447 std::string str = target_float_to_string (from, from_type);
2448 target_float_from_string (to, to_type, str);
2449 return;
2450 }
2451
7a26362d
UW
2452 /* Convert between two different formats in the same category. */
2453 if (!target_float_same_format_p (from_type, to_type))
2454 {
2455 const target_float_ops *ops = get_target_float_ops (from_type, to_type);
2456 ops->convert (from, from_type, to, to_type);
2457 return;
2458 }
2459
2460 /* The floating-point formats match, so we simply copy the data, ensuring
2461 possible padding bytes in the target buffer are zeroed out. */
2462 memset (to, 0, TYPE_LENGTH (to_type));
2463 memcpy (to, from, target_float_format_length (to_type));
50637b26 2464}
66c02b9e
UW
2465
2466/* Perform the binary operation indicated by OPCODE, using as operands the
2467 target byte streams X and Y, interpreted as floating-point numbers of
2468 types TYPE_X and TYPE_Y, respectively. Convert the result to type
2469 TYPE_RES and store it into the byte-stream RES.
2470
2471 The three types must either be all binary floating-point types, or else
2472 all decimal floating-point types. Binary and decimal floating-point
2473 types cannot be mixed within a single operation. */
2474void
2475target_float_binop (enum exp_opcode opcode,
2476 const gdb_byte *x, const struct type *type_x,
2477 const gdb_byte *y, const struct type *type_y,
2478 gdb_byte *res, const struct type *type_res)
2479{
7a26362d
UW
2480 gdb_assert (target_float_same_category_p (type_x, type_res));
2481 gdb_assert (target_float_same_category_p (type_y, type_res));
66c02b9e 2482
7a26362d
UW
2483 const target_float_ops *ops = get_target_float_ops (type_x, type_y);
2484 ops->binop (opcode, x, type_x, y, type_y, res, type_res);
66c02b9e
UW
2485}
2486
2487/* Compare the two target byte streams X and Y, interpreted as floating-point
2488 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
2489 are equal, -1 if X is less than Y, and 1 otherwise.
2490
2491 The two types must either both be binary floating-point types, or else
2492 both be decimal floating-point types. Binary and decimal floating-point
2493 types cannot compared directly against each other. */
2494int
2495target_float_compare (const gdb_byte *x, const struct type *type_x,
2496 const gdb_byte *y, const struct type *type_y)
2497{
7a26362d 2498 gdb_assert (target_float_same_category_p (type_x, type_y));
66c02b9e 2499
7a26362d
UW
2500 const target_float_ops *ops = get_target_float_ops (type_x, type_y);
2501 return ops->compare (x, type_x, y, type_y);
66c02b9e
UW
2502}
2503
This page took 0.38081 seconds and 4 git commands to generate.