]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/target-float.c
Fix build failure in darwin-nat.c
[thirdparty/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
50637b26
UW
26/* Helper routines operating on binary floating-point data. */
27
66c02b9e
UW
28#include <math.h>
29
1cfb73db
UW
30#if (defined HAVE_LONG_DOUBLE && defined PRINTF_HAS_LONG_DOUBLE \
31 && defined SCANF_HAS_LONG_DOUBLE)
32typedef long double DOUBLEST;
33#else
34typedef double DOUBLEST;
35/* If we can't scan or print long double, we don't want to use it
36 anywhere. */
37# undef HAVE_LONG_DOUBLE
38# undef PRINTF_HAS_LONG_DOUBLE
39# undef SCANF_HAS_LONG_DOUBLE
40#endif
41
42/* Different kinds of floatformat numbers recognized by
43 floatformat_classify. To avoid portability issues, we use local
44 values instead of the C99 macros (FP_NAN et cetera). */
45enum float_kind {
46 float_nan,
47 float_infinite,
48 float_zero,
49 float_normal,
50 float_subnormal
51};
52
53/* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
54 going to bother with trying to muck around with whether it is defined in
55 a system header, what we do if not, etc. */
56#define FLOATFORMAT_CHAR_BIT 8
57
58/* The number of bytes that the largest floating-point type that we
59 can convert to doublest will need. */
60#define FLOATFORMAT_LARGEST_BYTES 16
61
62/* Return the floatformat's total size in host bytes. */
63static size_t
64floatformat_totalsize_bytes (const struct floatformat *fmt)
65{
66 return ((fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1)
67 / FLOATFORMAT_CHAR_BIT);
68}
69
70/* Return the precision of the floating point format FMT. */
71static int
72floatformat_precision (const struct floatformat *fmt)
73{
74 /* Assume the precision of and IBM long double is twice the precision
75 of the underlying double. This matches what GCC does. */
76 if (fmt->split_half)
77 return 2 * floatformat_precision (fmt->split_half);
78
79 /* Otherwise, the precision is the size of mantissa in bits,
80 including the implicit bit if present. */
81 int prec = fmt->man_len;
82 if (fmt->intbit == floatformat_intbit_no)
83 prec++;
84
85 return prec;
86}
87
88/* Normalize the byte order of FROM into TO. If no normalization is
89 needed then FMT->byteorder is returned and TO is not changed;
90 otherwise the format of the normalized form in TO is returned. */
91static enum floatformat_byteorders
92floatformat_normalize_byteorder (const struct floatformat *fmt,
93 const void *from, void *to)
94{
95 const unsigned char *swapin;
96 unsigned char *swapout;
97 int words;
98
99 if (fmt->byteorder == floatformat_little
100 || fmt->byteorder == floatformat_big)
101 return fmt->byteorder;
102
103 words = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
104 words >>= 2;
105
106 swapout = (unsigned char *)to;
107 swapin = (const unsigned char *)from;
108
109 if (fmt->byteorder == floatformat_vax)
110 {
111 while (words-- > 0)
112 {
113 *swapout++ = swapin[1];
114 *swapout++ = swapin[0];
115 *swapout++ = swapin[3];
116 *swapout++ = swapin[2];
117 swapin += 4;
118 }
119 /* This may look weird, since VAX is little-endian, but it is
120 easier to translate to big-endian than to little-endian. */
121 return floatformat_big;
122 }
123 else
124 {
125 gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword);
126
127 while (words-- > 0)
128 {
129 *swapout++ = swapin[3];
130 *swapout++ = swapin[2];
131 *swapout++ = swapin[1];
132 *swapout++ = swapin[0];
133 swapin += 4;
134 }
135 return floatformat_big;
136 }
137}
138
139/* Extract a field which starts at START and is LEN bytes long. DATA and
140 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
141static unsigned long
142get_field (const bfd_byte *data, enum floatformat_byteorders order,
143 unsigned int total_len, unsigned int start, unsigned int len)
144{
145 unsigned long result;
146 unsigned int cur_byte;
147 int cur_bitshift;
148
149 /* Caller must byte-swap words before calling this routine. */
150 gdb_assert (order == floatformat_little || order == floatformat_big);
151
152 /* Start at the least significant part of the field. */
153 if (order == floatformat_little)
154 {
155 /* We start counting from the other end (i.e, from the high bytes
156 rather than the low bytes). As such, we need to be concerned
157 with what happens if bit 0 doesn't start on a byte boundary.
158 I.e, we need to properly handle the case where total_len is
159 not evenly divisible by 8. So we compute ``excess'' which
160 represents the number of bits from the end of our starting
161 byte needed to get to bit 0. */
162 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
163
164 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
165 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
166 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
167 - FLOATFORMAT_CHAR_BIT;
168 }
169 else
170 {
171 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
172 cur_bitshift =
173 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
174 }
175 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
176 result = *(data + cur_byte) >> (-cur_bitshift);
177 else
178 result = 0;
179 cur_bitshift += FLOATFORMAT_CHAR_BIT;
180 if (order == floatformat_little)
181 ++cur_byte;
182 else
183 --cur_byte;
184
185 /* Move towards the most significant part of the field. */
186 while (cur_bitshift < len)
187 {
188 result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
189 cur_bitshift += FLOATFORMAT_CHAR_BIT;
190 switch (order)
191 {
192 case floatformat_little:
193 ++cur_byte;
194 break;
195 case floatformat_big:
196 --cur_byte;
197 break;
198 }
199 }
200 if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
201 /* Mask out bits which are not part of the field. */
202 result &= ((1UL << len) - 1);
203 return result;
204}
205
206/* Set a field which starts at START and is LEN bytes long. DATA and
207 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
208static void
209put_field (unsigned char *data, enum floatformat_byteorders order,
210 unsigned int total_len, unsigned int start, unsigned int len,
211 unsigned long stuff_to_put)
212{
213 unsigned int cur_byte;
214 int cur_bitshift;
215
216 /* Caller must byte-swap words before calling this routine. */
217 gdb_assert (order == floatformat_little || order == floatformat_big);
218
219 /* Start at the least significant part of the field. */
220 if (order == floatformat_little)
221 {
222 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
223
224 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
225 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
226 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
227 - FLOATFORMAT_CHAR_BIT;
228 }
229 else
230 {
231 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
232 cur_bitshift =
233 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
234 }
235 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
236 {
237 *(data + cur_byte) &=
238 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
239 << (-cur_bitshift));
240 *(data + cur_byte) |=
241 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
242 }
243 cur_bitshift += FLOATFORMAT_CHAR_BIT;
244 if (order == floatformat_little)
245 ++cur_byte;
246 else
247 --cur_byte;
248
249 /* Move towards the most significant part of the field. */
250 while (cur_bitshift < len)
251 {
252 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
253 {
254 /* This is the last byte. */
255 *(data + cur_byte) &=
256 ~((1 << (len - cur_bitshift)) - 1);
257 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
258 }
259 else
260 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
261 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
262 cur_bitshift += FLOATFORMAT_CHAR_BIT;
263 if (order == floatformat_little)
264 ++cur_byte;
265 else
266 --cur_byte;
267 }
268}
269
270/* Check if VAL (which is assumed to be a floating point number whose
271 format is described by FMT) is negative. */
272static int
273floatformat_is_negative (const struct floatformat *fmt,
274 const bfd_byte *uval)
275{
276 enum floatformat_byteorders order;
277 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
278
279 gdb_assert (fmt != NULL);
280 gdb_assert (fmt->totalsize
281 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
282
283 /* An IBM long double (a two element array of double) always takes the
284 sign of the first double. */
285 if (fmt->split_half)
286 fmt = fmt->split_half;
287
288 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
289
290 if (order != fmt->byteorder)
291 uval = newfrom;
292
293 return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1);
294}
295
296/* Check if VAL is "not a number" (NaN) for FMT. */
297static enum float_kind
298floatformat_classify (const struct floatformat *fmt,
299 const bfd_byte *uval)
300{
301 long exponent;
302 unsigned long mant;
303 unsigned int mant_bits, mant_off;
304 int mant_bits_left;
305 enum floatformat_byteorders order;
306 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
307 int mant_zero;
308
309 gdb_assert (fmt != NULL);
310 gdb_assert (fmt->totalsize
311 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
312
313 /* An IBM long double (a two element array of double) can be classified
314 by looking at the first double. inf and nan are specified as
315 ignoring the second double. zero and subnormal will always have
316 the second double 0.0 if the long double is correctly rounded. */
317 if (fmt->split_half)
318 fmt = fmt->split_half;
319
320 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
321
322 if (order != fmt->byteorder)
323 uval = newfrom;
324
325 exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start,
326 fmt->exp_len);
327
328 mant_bits_left = fmt->man_len;
329 mant_off = fmt->man_start;
330
331 mant_zero = 1;
332 while (mant_bits_left > 0)
333 {
334 mant_bits = std::min (mant_bits_left, 32);
335
336 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
337
338 /* If there is an explicit integer bit, mask it off. */
339 if (mant_off == fmt->man_start
340 && fmt->intbit == floatformat_intbit_yes)
341 mant &= ~(1 << (mant_bits - 1));
342
343 if (mant)
344 {
345 mant_zero = 0;
346 break;
347 }
348
349 mant_off += mant_bits;
350 mant_bits_left -= mant_bits;
351 }
352
353 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
354 supported. */
355 if (! fmt->exp_nan)
356 {
357 if (mant_zero)
358 return float_zero;
359 else
360 return float_normal;
361 }
362
363 if (exponent == 0)
364 {
365 if (mant_zero)
366 return float_zero;
367 else
368 return float_subnormal;
369 }
370
371 if (exponent == fmt->exp_nan)
372 {
373 if (mant_zero)
374 return float_infinite;
375 else
376 return float_nan;
377 }
378
379 return float_normal;
380}
381
382/* Convert the mantissa of VAL (which is assumed to be a floating
383 point number whose format is described by FMT) into a hexadecimal
384 and store it in a static string. Return a pointer to that string. */
385static const char *
386floatformat_mantissa (const struct floatformat *fmt,
387 const bfd_byte *val)
388{
389 unsigned char *uval = (unsigned char *) val;
390 unsigned long mant;
391 unsigned int mant_bits, mant_off;
392 int mant_bits_left;
393 static char res[50];
394 char buf[9];
395 int len;
396 enum floatformat_byteorders order;
397 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
398
399 gdb_assert (fmt != NULL);
400 gdb_assert (fmt->totalsize
401 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
402
403 /* For IBM long double (a two element array of double), return the
404 mantissa of the first double. The problem with returning the
405 actual mantissa from both doubles is that there can be an
406 arbitrary number of implied 0's or 1's between the mantissas
407 of the first and second double. In any case, this function
408 is only used for dumping out nans, and a nan is specified to
409 ignore the value in the second double. */
410 if (fmt->split_half)
411 fmt = fmt->split_half;
412
413 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
414
415 if (order != fmt->byteorder)
416 uval = newfrom;
417
418 if (! fmt->exp_nan)
419 return 0;
420
421 /* Make sure we have enough room to store the mantissa. */
422 gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2);
423
424 mant_off = fmt->man_start;
425 mant_bits_left = fmt->man_len;
426 mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32;
427
428 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
429
430 len = xsnprintf (res, sizeof res, "%lx", mant);
431
432 mant_off += mant_bits;
433 mant_bits_left -= mant_bits;
434
435 while (mant_bits_left > 0)
436 {
437 mant = get_field (uval, order, fmt->totalsize, mant_off, 32);
438
439 xsnprintf (buf, sizeof buf, "%08lx", mant);
440 gdb_assert (len + strlen (buf) <= sizeof res);
441 strcat (res, buf);
442
443 mant_off += 32;
444 mant_bits_left -= 32;
445 }
446
447 return res;
448}
449
450/* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
451
452 If the host and target formats agree, we just copy the raw data
453 into the appropriate type of variable and return, letting the host
454 increase precision as necessary. Otherwise, we call the conversion
455 routine and let it do the dirty work. Note that even if the target
456 and host floating-point formats match, the length of the types
457 might still be different, so the conversion routines must make sure
458 to not overrun any buffers. For example, on x86, long double is
459 the 80-bit extended precision type on both 32-bit and 64-bit ABIs,
460 but by default it is stored as 12 bytes on 32-bit, and 16 bytes on
461 64-bit, for alignment reasons. See comment in store_typed_floating
462 for a discussion about zeroing out remaining bytes in the target
463 buffer. */
464
465static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT;
466static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT;
467static const struct floatformat *host_long_double_format
468 = GDB_HOST_LONG_DOUBLE_FORMAT;
469
470/* Convert from FMT to a DOUBLEST. FROM is the address of the extended float.
471 Store the DOUBLEST in *TO. */
472static void
473floatformat_to_doublest (const struct floatformat *fmt,
474 const void *from, DOUBLEST *to)
475{
476 gdb_assert (fmt != NULL);
477
478 if (fmt == host_float_format)
479 {
480 float val = 0;
481
482 memcpy (&val, from, floatformat_totalsize_bytes (fmt));
483 *to = val;
484 return;
485 }
486 else if (fmt == host_double_format)
487 {
488 double val = 0;
489
490 memcpy (&val, from, floatformat_totalsize_bytes (fmt));
491 *to = val;
492 return;
493 }
494 else if (fmt == host_long_double_format)
495 {
496 long double val = 0;
497
498 memcpy (&val, from, floatformat_totalsize_bytes (fmt));
499 *to = val;
500 return;
501 }
502
503 unsigned char *ufrom = (unsigned char *) from;
504 DOUBLEST dto;
505 long exponent;
506 unsigned long mant;
507 unsigned int mant_bits, mant_off;
508 int mant_bits_left;
509 int special_exponent; /* It's a NaN, denorm or zero. */
510 enum floatformat_byteorders order;
511 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
512 enum float_kind kind;
513
514 gdb_assert (fmt->totalsize
515 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
516
517 /* For non-numbers, reuse libiberty's logic to find the correct
518 format. We do not lose any precision in this case by passing
519 through a double. */
520 kind = floatformat_classify (fmt, (const bfd_byte *) from);
521 if (kind == float_infinite || kind == float_nan)
522 {
523 double dto;
524
525 floatformat_to_double (fmt->split_half ? fmt->split_half : fmt,
526 from, &dto);
527 *to = (DOUBLEST) dto;
528 return;
529 }
530
531 order = floatformat_normalize_byteorder (fmt, ufrom, newfrom);
532
533 if (order != fmt->byteorder)
534 ufrom = newfrom;
535
536 if (fmt->split_half)
537 {
538 DOUBLEST dtop, dbot;
539
540 floatformat_to_doublest (fmt->split_half, ufrom, &dtop);
541 /* Preserve the sign of 0, which is the sign of the top
542 half. */
543 if (dtop == 0.0)
544 {
545 *to = dtop;
546 return;
547 }
548 floatformat_to_doublest (fmt->split_half,
549 ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2,
550 &dbot);
551 *to = dtop + dbot;
552 return;
553 }
554
555 exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start,
556 fmt->exp_len);
557 /* Note that if exponent indicates a NaN, we can't really do anything useful
558 (not knowing if the host has NaN's, or how to build one). So it will
559 end up as an infinity or something close; that is OK. */
560
561 mant_bits_left = fmt->man_len;
562 mant_off = fmt->man_start;
563 dto = 0.0;
564
565 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
566
567 /* Don't bias NaNs. Use minimum exponent for denorms. For
568 simplicity, we don't check for zero as the exponent doesn't matter.
569 Note the cast to int; exp_bias is unsigned, so it's important to
570 make sure the operation is done in signed arithmetic. */
571 if (!special_exponent)
572 exponent -= fmt->exp_bias;
573 else if (exponent == 0)
574 exponent = 1 - fmt->exp_bias;
575
576 /* Build the result algebraically. Might go infinite, underflow, etc;
577 who cares. */
578
579 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
580 increment the exponent by one to account for the integer bit. */
581
582 if (!special_exponent)
583 {
584 if (fmt->intbit == floatformat_intbit_no)
585 dto = ldexp (1.0, exponent);
586 else
587 exponent++;
588 }
589
590 while (mant_bits_left > 0)
591 {
592 mant_bits = std::min (mant_bits_left, 32);
593
594 mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits);
595
596 dto += ldexp ((double) mant, exponent - mant_bits);
597 exponent -= mant_bits;
598 mant_off += mant_bits;
599 mant_bits_left -= mant_bits;
600 }
601
602 /* Negate it if negative. */
603 if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1))
604 dto = -dto;
605 *to = dto;
606}
607
608/* Convert the DOUBLEST *FROM to an extended float in format FMT and
609 store where TO points. */
610static void
611floatformat_from_doublest (const struct floatformat *fmt,
612 const DOUBLEST *from, void *to)
613{
614 gdb_assert (fmt != NULL);
615
616 if (fmt == host_float_format)
617 {
618 float val = *from;
619
620 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
621 return;
622 }
623 else if (fmt == host_double_format)
624 {
625 double val = *from;
626
627 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
628 return;
629 }
630 else if (fmt == host_long_double_format)
631 {
632 long double val = *from;
633
634 memcpy (to, &val, floatformat_totalsize_bytes (fmt));
635 return;
636 }
637
638 DOUBLEST dfrom;
639 int exponent;
640 DOUBLEST mant;
641 unsigned int mant_bits, mant_off;
642 int mant_bits_left;
643 unsigned char *uto = (unsigned char *) to;
644 enum floatformat_byteorders order = fmt->byteorder;
645 unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
646
647 if (order != floatformat_little)
648 order = floatformat_big;
649
650 if (order != fmt->byteorder)
651 uto = newto;
652
653 memcpy (&dfrom, from, sizeof (dfrom));
654 memset (uto, 0, floatformat_totalsize_bytes (fmt));
655
656 if (fmt->split_half)
657 {
658 /* Use static volatile to ensure that any excess precision is
659 removed via storing in memory, and so the top half really is
660 the result of converting to double. */
661 static volatile double dtop, dbot;
662 DOUBLEST dtopnv, dbotnv;
663
664 dtop = (double) dfrom;
665 /* If the rounded top half is Inf, the bottom must be 0 not NaN
666 or Inf. */
667 if (dtop + dtop == dtop && dtop != 0.0)
668 dbot = 0.0;
669 else
670 dbot = (double) (dfrom - (DOUBLEST) dtop);
671 dtopnv = dtop;
672 dbotnv = dbot;
673 floatformat_from_doublest (fmt->split_half, &dtopnv, uto);
674 floatformat_from_doublest (fmt->split_half, &dbotnv,
675 (uto
676 + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2));
677 return;
678 }
679
680 if (dfrom == 0)
681 goto finalize_byteorder; /* Result is zero */
682 if (dfrom != dfrom) /* Result is NaN */
683 {
684 /* From is NaN */
685 put_field (uto, order, fmt->totalsize, fmt->exp_start,
686 fmt->exp_len, fmt->exp_nan);
687 /* Be sure it's not infinity, but NaN value is irrel. */
688 put_field (uto, order, fmt->totalsize, fmt->man_start,
689 fmt->man_len, 1);
690 goto finalize_byteorder;
691 }
692
693 /* If negative, set the sign bit. */
694 if (dfrom < 0)
695 {
696 put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1);
697 dfrom = -dfrom;
698 }
699
700 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity. */
701 {
702 /* Infinity exponent is same as NaN's. */
703 put_field (uto, order, fmt->totalsize, fmt->exp_start,
704 fmt->exp_len, fmt->exp_nan);
705 /* Infinity mantissa is all zeroes. */
706 put_field (uto, order, fmt->totalsize, fmt->man_start,
707 fmt->man_len, 0);
708 goto finalize_byteorder;
709 }
710
711#ifdef HAVE_LONG_DOUBLE
712 mant = frexpl (dfrom, &exponent);
713#else
714 mant = frexp (dfrom, &exponent);
715#endif
716
717 if (exponent + fmt->exp_bias <= 0)
718 {
719 /* The value is too small to be expressed in the destination
720 type (not enough bits in the exponent. Treat as 0. */
721 put_field (uto, order, fmt->totalsize, fmt->exp_start,
722 fmt->exp_len, 0);
723 put_field (uto, order, fmt->totalsize, fmt->man_start,
724 fmt->man_len, 0);
725 goto finalize_byteorder;
726 }
727
728 if (exponent + fmt->exp_bias >= (1 << fmt->exp_len))
729 {
730 /* The value is too large to fit into the destination.
731 Treat as infinity. */
732 put_field (uto, order, fmt->totalsize, fmt->exp_start,
733 fmt->exp_len, fmt->exp_nan);
734 put_field (uto, order, fmt->totalsize, fmt->man_start,
735 fmt->man_len, 0);
736 goto finalize_byteorder;
737 }
738
739 put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
740 exponent + fmt->exp_bias - 1);
741
742 mant_bits_left = fmt->man_len;
743 mant_off = fmt->man_start;
744 while (mant_bits_left > 0)
745 {
746 unsigned long mant_long;
747
748 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
749
750 mant *= 4294967296.0;
751 mant_long = ((unsigned long) mant) & 0xffffffffL;
752 mant -= mant_long;
753
754 /* If the integer bit is implicit, then we need to discard it.
755 If we are discarding a zero, we should be (but are not) creating
756 a denormalized number which means adjusting the exponent
757 (I think). */
758 if (mant_bits_left == fmt->man_len
759 && fmt->intbit == floatformat_intbit_no)
760 {
761 mant_long <<= 1;
762 mant_long &= 0xffffffffL;
763 /* If we are processing the top 32 mantissa bits of a doublest
764 so as to convert to a float value with implied integer bit,
765 we will only be putting 31 of those 32 bits into the
766 final value due to the discarding of the top bit. In the
767 case of a small float value where the number of mantissa
768 bits is less than 32, discarding the top bit does not alter
769 the number of bits we will be adding to the result. */
770 if (mant_bits == 32)
771 mant_bits -= 1;
772 }
773
774 if (mant_bits < 32)
775 {
776 /* The bits we want are in the most significant MANT_BITS bits of
777 mant_long. Move them to the least significant. */
778 mant_long >>= 32 - mant_bits;
779 }
780
781 put_field (uto, order, fmt->totalsize,
782 mant_off, mant_bits, mant_long);
783 mant_off += mant_bits;
784 mant_bits_left -= mant_bits;
785 }
786
787 finalize_byteorder:
788 /* Do we need to byte-swap the words in the result? */
789 if (order != fmt->byteorder)
790 floatformat_normalize_byteorder (fmt, newto, to);
791}
792
793/* Convert the byte-stream ADDR, interpreted as floating-point format FMT,
794 to a string, optionally using the print format FORMAT. */
795static std::string
796floatformat_to_string (const struct floatformat *fmt,
797 const gdb_byte *in, const char *format)
798{
799 /* Unless we need to adhere to a specific format, provide special
800 output for certain cases. */
801 if (format == nullptr)
802 {
803 /* Detect invalid representations. */
804 if (!floatformat_is_valid (fmt, in))
805 return "<invalid float value>";
806
807 /* Handle NaN and Inf. */
808 enum float_kind kind = floatformat_classify (fmt, in);
809 if (kind == float_nan)
810 {
811 const char *sign = floatformat_is_negative (fmt, in)? "-" : "";
812 const char *mantissa = floatformat_mantissa (fmt, in);
813 return string_printf ("%snan(0x%s)", sign, mantissa);
814 }
815 else if (kind == float_infinite)
816 {
817 const char *sign = floatformat_is_negative (fmt, in)? "-" : "";
818 return string_printf ("%sinf", sign);
819 }
820 }
821
822 /* Determine the format string to use on the host side. */
823 std::string host_format;
824 char conversion;
825
826 if (format == nullptr)
827 {
828 /* If no format was specified, print the number using a format string
829 where the precision is set to the DECIMAL_DIG value for the given
830 floating-point format. This value is computed as
831
832 ceil(1 + p * log10(b)),
833
834 where p is the precision of the floating-point format in bits, and
835 b is the base (which is always 2 for the formats we support). */
836 const double log10_2 = .30102999566398119521;
837 double d_decimal_dig = 1 + floatformat_precision (fmt) * log10_2;
838 int decimal_dig = d_decimal_dig;
839 if (decimal_dig < d_decimal_dig)
840 decimal_dig++;
841
842 host_format = string_printf ("%%.%d", decimal_dig);
843 conversion = 'g';
844 }
845 else
846 {
847 /* Use the specified format, stripping out the conversion character
848 and length modifier, if present. */
849 size_t len = strlen (format);
850 gdb_assert (len > 1);
851 conversion = format[--len];
852 gdb_assert (conversion == 'e' || conversion == 'f' || conversion == 'g'
853 || conversion == 'E' || conversion == 'G');
854 if (format[len - 1] == 'L')
855 len--;
856
857 host_format = std::string (format, len);
858 }
859
860 /* Add the length modifier and conversion character appropriate for
861 handling the host DOUBLEST type. */
862#ifdef HAVE_LONG_DOUBLE
863 host_format += 'L';
864#endif
865 host_format += conversion;
866
867 DOUBLEST doub;
868 floatformat_to_doublest (fmt, in, &doub);
869 return string_printf (host_format.c_str (), doub);
870}
871
872/* Parse string STRING into a target floating-number of format FMT and
873 store it as byte-stream ADDR. Return whether parsing succeeded. */
874static bool
875floatformat_from_string (const struct floatformat *fmt, gdb_byte *out,
876 const std::string &in)
877{
878 DOUBLEST doub;
879 int n, num;
880#ifdef HAVE_LONG_DOUBLE
881 const char *scan_format = "%Lg%n";
882#else
883 const char *scan_format = "%lg%n";
884#endif
885 num = sscanf (in.c_str (), scan_format, &doub, &n);
886
887 /* The sscanf man page suggests not making any assumptions on the effect
888 of %n on the result, so we don't.
889 That is why we simply test num == 0. */
890 if (num == 0)
891 return false;
892
893 /* We only accept the whole string. */
894 if (in[n])
895 return false;
896
897 floatformat_from_doublest (fmt, &doub, out);
898 return true;
899}
900
50637b26
UW
901/* Convert the byte-stream ADDR, interpreted as floating-point format FMT,
902 to an integer value (rounding towards zero). */
903static LONGEST
904floatformat_to_longest (const struct floatformat *fmt, const gdb_byte *addr)
905{
906 DOUBLEST d;
907 floatformat_to_doublest (fmt, addr, &d);
908 return (LONGEST) d;
909}
910
911/* Convert signed integer VAL to a target floating-number of format FMT
912 and store it as byte-stream ADDR. */
913static void
914floatformat_from_longest (const struct floatformat *fmt, gdb_byte *addr,
915 LONGEST val)
916{
917 DOUBLEST d = (DOUBLEST) val;
918 floatformat_from_doublest (fmt, &d, addr);
919}
920
921/* Convert unsigned integer VAL to a target floating-number of format FMT
922 and store it as byte-stream ADDR. */
923static void
924floatformat_from_ulongest (const struct floatformat *fmt, gdb_byte *addr,
925 ULONGEST val)
926{
927 DOUBLEST d = (DOUBLEST) val;
928 floatformat_from_doublest (fmt, &d, addr);
929}
930
14ad9311
UW
931/* Convert the byte-stream ADDR, interpreted as floating-point format FMT,
932 to a floating-point value in the host "double" format. */
933static double
934floatformat_to_host_double (const struct floatformat *fmt,
935 const gdb_byte *addr)
936{
937 DOUBLEST d;
938 floatformat_to_doublest (fmt, addr, &d);
939 return (double) d;
940}
941
942/* Convert floating-point value VAL in the host "double" format to a target
943 floating-number of format FMT and store it as byte-stream ADDR. */
944static void
945floatformat_from_host_double (const struct floatformat *fmt, gdb_byte *addr,
946 double val)
947{
948 DOUBLEST d = (DOUBLEST) val;
949 floatformat_from_doublest (fmt, &d, addr);
950}
951
50637b26
UW
952/* Convert a floating-point number of format FROM_FMT from the target
953 byte-stream FROM to a floating-point number of format TO_FMT, and
954 store it to the target byte-stream TO. */
955static void
956floatformat_convert (const gdb_byte *from, const struct floatformat *from_fmt,
957 gdb_byte *to, const struct floatformat *to_fmt)
958{
959 if (from_fmt == to_fmt)
960 {
961 /* The floating-point formats match, so we simply copy the data. */
962 memcpy (to, from, floatformat_totalsize_bytes (to_fmt));
963 }
964 else
965 {
966 /* The floating-point formats don't match. The best we can do
967 (apart from simulating the target FPU) is converting to the
968 widest floating-point type supported by the host, and then
969 again to the desired type. */
970 DOUBLEST d;
971
972 floatformat_to_doublest (from_fmt, from, &d);
973 floatformat_from_doublest (to_fmt, &d, to);
974 }
975}
976
66c02b9e
UW
977/* Perform the binary operation indicated by OPCODE, using as operands the
978 target byte streams X and Y, interpreted as floating-point numbers of
979 formats FMT_X and FMT_Y, respectively. Convert the result to format
980 FMT_RES and store it into the byte-stream RES. */
981static void
982floatformat_binop (enum exp_opcode op,
983 const struct floatformat *fmt_x, const gdb_byte *x,
984 const struct floatformat *fmt_y, const gdb_byte *y,
985 const struct floatformat *fmt_result, gdb_byte *result)
986{
987 DOUBLEST v1, v2, v = 0;
988
989 floatformat_to_doublest (fmt_x, x, &v1);
990 floatformat_to_doublest (fmt_y, y, &v2);
991
992 switch (op)
993 {
994 case BINOP_ADD:
995 v = v1 + v2;
996 break;
997
998 case BINOP_SUB:
999 v = v1 - v2;
1000 break;
1001
1002 case BINOP_MUL:
1003 v = v1 * v2;
1004 break;
1005
1006 case BINOP_DIV:
1007 v = v1 / v2;
1008 break;
1009
1010 case BINOP_EXP:
1011 errno = 0;
1012 v = pow (v1, v2);
1013 if (errno)
1014 error (_("Cannot perform exponentiation: %s"),
1015 safe_strerror (errno));
1016 break;
1017
1018 case BINOP_MIN:
1019 v = v1 < v2 ? v1 : v2;
1020 break;
1021
1022 case BINOP_MAX:
1023 v = v1 > v2 ? v1 : v2;
1024 break;
1025
1026 default:
1027 error (_("Integer-only operation on floating point number."));
1028 break;
1029 }
1030
1031 floatformat_from_doublest (fmt_result, &v, result);
1032}
1033
1034/* Compare the two target byte streams X and Y, interpreted as floating-point
1035 numbers of formats FMT_X and FMT_Y, respectively. Return zero if X and Y
1036 are equal, -1 if X is less than Y, and 1 otherwise. */
1037static int
1038floatformat_compare (const struct floatformat *fmt_x, const gdb_byte *x,
1039 const struct floatformat *fmt_y, const gdb_byte *y)
1040{
1041 DOUBLEST v1, v2;
1042
1043 floatformat_to_doublest (fmt_x, x, &v1);
1044 floatformat_to_doublest (fmt_y, y, &v2);
1045
1046 if (v1 == v2)
1047 return 0;
1048 if (v1 < v2)
1049 return -1;
1050 return 1;
1051}
1052
50637b26 1053
1cfb73db
UW
1054/* Helper routines operating on decimal floating-point data. */
1055
1056/* Decimal floating point is one of the extension to IEEE 754, which is
1057 described in http://grouper.ieee.org/groups/754/revision.html and
1058 http://www2.hursley.ibm.com/decimal/. It completes binary floating
1059 point by representing floating point more exactly. */
1060
1061/* The order of the following headers is important for making sure
1062 decNumber structure is large enough to hold decimal128 digits. */
1063
1064#include "dpd/decimal128.h"
1065#include "dpd/decimal64.h"
1066#include "dpd/decimal32.h"
1067
1068/* When using decimal128, this is the maximum string length + 1
1069 (value comes from libdecnumber's DECIMAL128_String constant). */
1070#define MAX_DECIMAL_STRING 43
1071
1072/* In GDB, we are using an array of gdb_byte to represent decimal values.
1073 They are stored in host byte order. This routine does the conversion if
1074 the target byte order is different. */
1075static void
1076match_endianness (const gdb_byte *from, int len, enum bfd_endian byte_order,
1077 gdb_byte *to)
1078{
1079 int i;
1080
1081#if WORDS_BIGENDIAN
1082#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE
1083#else
1084#define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG
1085#endif
1086
1087 if (byte_order == OPPOSITE_BYTE_ORDER)
1088 for (i = 0; i < len; i++)
1089 to[i] = from[len - i - 1];
1090 else
1091 for (i = 0; i < len; i++)
1092 to[i] = from[i];
1093
1094 return;
1095}
1096
1097/* Helper function to get the appropriate libdecnumber context for each size
1098 of decimal float. */
1099static void
1100set_decnumber_context (decContext *ctx, int len)
1101{
1102 switch (len)
1103 {
1104 case 4:
1105 decContextDefault (ctx, DEC_INIT_DECIMAL32);
1106 break;
1107 case 8:
1108 decContextDefault (ctx, DEC_INIT_DECIMAL64);
1109 break;
1110 case 16:
1111 decContextDefault (ctx, DEC_INIT_DECIMAL128);
1112 break;
1113 }
1114
1115 ctx->traps = 0;
1116}
1117
1118/* Check for errors signaled in the decimal context structure. */
1119static void
1120decimal_check_errors (decContext *ctx)
1121{
1122 /* An error here could be a division by zero, an overflow, an underflow or
1123 an invalid operation (from the DEC_Errors constant in decContext.h).
1124 Since GDB doesn't complain about division by zero, overflow or underflow
1125 errors for binary floating, we won't complain about them for decimal
1126 floating either. */
1127 if (ctx->status & DEC_IEEE_854_Invalid_operation)
1128 {
1129 /* Leave only the error bits in the status flags. */
1130 ctx->status &= DEC_IEEE_854_Invalid_operation;
1131 error (_("Cannot perform operation: %s"),
1132 decContextStatusToString (ctx));
1133 }
1134}
1135
1136/* Helper function to convert from libdecnumber's appropriate representation
1137 for computation to each size of decimal float. */
1138static void
d7236961
UW
1139decimal_from_number (const decNumber *from,
1140 gdb_byte *to, int len, enum bfd_endian byte_order)
1cfb73db 1141{
d7236961
UW
1142 gdb_byte dec[16];
1143
1cfb73db
UW
1144 decContext set;
1145
1146 set_decnumber_context (&set, len);
1147
1148 switch (len)
1149 {
1150 case 4:
d7236961 1151 decimal32FromNumber ((decimal32 *) dec, from, &set);
1cfb73db
UW
1152 break;
1153 case 8:
d7236961 1154 decimal64FromNumber ((decimal64 *) dec, from, &set);
1cfb73db
UW
1155 break;
1156 case 16:
d7236961
UW
1157 decimal128FromNumber ((decimal128 *) dec, from, &set);
1158 break;
1159 default:
1160 error (_("Unknown decimal floating point type."));
1cfb73db
UW
1161 break;
1162 }
d7236961
UW
1163
1164 match_endianness (dec, len, byte_order, to);
1cfb73db
UW
1165}
1166
1167/* Helper function to convert each size of decimal float to libdecnumber's
1168 appropriate representation for computation. */
1169static void
d7236961
UW
1170decimal_to_number (const gdb_byte *from, int len, enum bfd_endian byte_order,
1171 decNumber *to)
1cfb73db 1172{
d7236961
UW
1173 gdb_byte dec[16];
1174 match_endianness (from, len, byte_order, dec);
1175
1cfb73db
UW
1176 switch (len)
1177 {
1178 case 4:
d7236961 1179 decimal32ToNumber ((decimal32 *) dec, to);
1cfb73db
UW
1180 break;
1181 case 8:
d7236961 1182 decimal64ToNumber ((decimal64 *) dec, to);
1cfb73db
UW
1183 break;
1184 case 16:
d7236961 1185 decimal128ToNumber ((decimal128 *) dec, to);
1cfb73db
UW
1186 break;
1187 default:
1188 error (_("Unknown decimal floating point type."));
1189 break;
1190 }
1191}
1192
1193/* Convert decimal type to its string representation. LEN is the length
1194 of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and
1195 16 bytes for decimal128. */
1196static std::string
1197decimal_to_string (const gdb_byte *decbytes, int len,
1198 enum bfd_endian byte_order, const char *format = nullptr)
1199{
1200 gdb_byte dec[16];
1201
1202 match_endianness (decbytes, len, byte_order, dec);
1203
1204 if (format != nullptr)
1205 {
1206 /* We don't handle format strings (yet). If the host printf supports
1207 decimal floating point types, just use this. Otherwise, fall back
1208 to printing the number while ignoring the format string. */
1209#if defined (PRINTF_HAS_DECFLOAT)
1210 /* FIXME: This makes unwarranted assumptions about the host ABI! */
1211 return string_printf (format, dec);
1212#endif
1213 }
1214
1215 std::string result;
1216 result.resize (MAX_DECIMAL_STRING);
1217
1218 switch (len)
1219 {
1220 case 4:
1221 decimal32ToString ((decimal32 *) dec, &result[0]);
1222 break;
1223 case 8:
1224 decimal64ToString ((decimal64 *) dec, &result[0]);
1225 break;
1226 case 16:
1227 decimal128ToString ((decimal128 *) dec, &result[0]);
1228 break;
1229 default:
1230 error (_("Unknown decimal floating point type."));
1231 break;
1232 }
1233
1234 return result;
1235}
1236
1237/* Convert the string form of a decimal value to its decimal representation.
1238 LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for
1239 decimal64 and 16 bytes for decimal128. */
1240static bool
1241decimal_from_string (gdb_byte *decbytes, int len, enum bfd_endian byte_order,
1242 const std::string &string)
1243{
1244 decContext set;
1245 gdb_byte dec[16];
1246
1247 set_decnumber_context (&set, len);
1248
1249 switch (len)
1250 {
1251 case 4:
1252 decimal32FromString ((decimal32 *) dec, string.c_str (), &set);
1253 break;
1254 case 8:
1255 decimal64FromString ((decimal64 *) dec, string.c_str (), &set);
1256 break;
1257 case 16:
1258 decimal128FromString ((decimal128 *) dec, string.c_str (), &set);
1259 break;
1260 default:
1261 error (_("Unknown decimal floating point type."));
1262 break;
1263 }
1264
1265 match_endianness (dec, len, byte_order, decbytes);
1266
1267 /* Check for errors in the DFP operation. */
1268 decimal_check_errors (&set);
1269
1270 return true;
1271}
1272
1273/* Converts a LONGEST to a decimal float of specified LEN bytes. */
1274static void
1275decimal_from_longest (LONGEST from,
1276 gdb_byte *to, int len, enum bfd_endian byte_order)
1277{
1cfb73db 1278 decNumber number;
d7236961 1279
1cfb73db
UW
1280 if ((int32_t) from != from)
1281 /* libdecnumber can convert only 32-bit integers. */
1282 error (_("Conversion of large integer to a "
1283 "decimal floating type is not supported."));
1284
1285 decNumberFromInt32 (&number, (int32_t) from);
1286
d7236961 1287 decimal_from_number (&number, to, len, byte_order);
1cfb73db
UW
1288}
1289
1290/* Converts a ULONGEST to a decimal float of specified LEN bytes. */
1291static void
1292decimal_from_ulongest (ULONGEST from,
1293 gdb_byte *to, int len, enum bfd_endian byte_order)
1294{
1cfb73db
UW
1295 decNumber number;
1296
1297 if ((uint32_t) from != from)
1298 /* libdecnumber can convert only 32-bit integers. */
1299 error (_("Conversion of large integer to a "
1300 "decimal floating type is not supported."));
1301
1302 decNumberFromUInt32 (&number, (uint32_t) from);
1303
d7236961 1304 decimal_from_number (&number, to, len, byte_order);
1cfb73db
UW
1305}
1306
1307/* Converts a decimal float of LEN bytes to a LONGEST. */
1308static LONGEST
1309decimal_to_longest (const gdb_byte *from, int len, enum bfd_endian byte_order)
1310{
1311 /* libdecnumber has a function to convert from decimal to integer, but
1312 it doesn't work when the decimal number has a fractional part. */
1313 std::string str = decimal_to_string (from, len, byte_order);
1314 return strtoll (str.c_str (), NULL, 10);
1315}
1316
1317/* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y
1318 and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in
1319 RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */
1320static void
1321decimal_binop (enum exp_opcode op,
1322 const gdb_byte *x, int len_x, enum bfd_endian byte_order_x,
1323 const gdb_byte *y, int len_y, enum bfd_endian byte_order_y,
1324 gdb_byte *result, int len_result,
1325 enum bfd_endian byte_order_result)
1326{
1327 decContext set;
1328 decNumber number1, number2, number3;
1cfb73db 1329
d7236961
UW
1330 decimal_to_number (x, len_x, byte_order_x, &number1);
1331 decimal_to_number (y, len_y, byte_order_y, &number2);
1cfb73db
UW
1332
1333 set_decnumber_context (&set, len_result);
1334
1335 switch (op)
1336 {
1337 case BINOP_ADD:
1338 decNumberAdd (&number3, &number1, &number2, &set);
1339 break;
1340 case BINOP_SUB:
1341 decNumberSubtract (&number3, &number1, &number2, &set);
1342 break;
1343 case BINOP_MUL:
1344 decNumberMultiply (&number3, &number1, &number2, &set);
1345 break;
1346 case BINOP_DIV:
1347 decNumberDivide (&number3, &number1, &number2, &set);
1348 break;
1349 case BINOP_EXP:
1350 decNumberPower (&number3, &number1, &number2, &set);
1351 break;
1352 default:
1353 error (_("Operation not valid for decimal floating point number."));
1354 break;
1355 }
1356
1357 /* Check for errors in the DFP operation. */
1358 decimal_check_errors (&set);
1359
d7236961 1360 decimal_from_number (&number3, result, len_result, byte_order_result);
1cfb73db
UW
1361}
1362
1363/* Returns true if X (which is LEN bytes wide) is the number zero. */
1364static int
1365decimal_is_zero (const gdb_byte *x, int len, enum bfd_endian byte_order)
1366{
1367 decNumber number;
1cfb73db 1368
d7236961 1369 decimal_to_number (x, len, byte_order, &number);
1cfb73db
UW
1370
1371 return decNumberIsZero (&number);
1372}
1373
1374/* Compares two numbers numerically. If X is less than Y then the return value
1375 will be -1. If they are equal, then the return value will be 0. If X is
1376 greater than the Y then the return value will be 1. */
1377static int
1378decimal_compare (const gdb_byte *x, int len_x, enum bfd_endian byte_order_x,
1379 const gdb_byte *y, int len_y, enum bfd_endian byte_order_y)
1380{
1381 decNumber number1, number2, result;
1382 decContext set;
1cfb73db
UW
1383 int len_result;
1384
d7236961
UW
1385 decimal_to_number (x, len_x, byte_order_x, &number1);
1386 decimal_to_number (y, len_y, byte_order_y, &number2);
1cfb73db
UW
1387
1388 /* Perform the comparison in the larger of the two sizes. */
1389 len_result = len_x > len_y ? len_x : len_y;
1390 set_decnumber_context (&set, len_result);
1391
1392 decNumberCompare (&result, &number1, &number2, &set);
1393
1394 /* Check for errors in the DFP operation. */
1395 decimal_check_errors (&set);
1396
1397 if (decNumberIsNaN (&result))
1398 error (_("Comparison with an invalid number (NaN)."));
1399 else if (decNumberIsZero (&result))
1400 return 0;
1401 else if (decNumberIsNegative (&result))
1402 return -1;
1403 else
1404 return 1;
1405}
1406
1407/* Convert a decimal value from a decimal type with LEN_FROM bytes to a
1408 decimal type with LEN_TO bytes. */
1409static void
1410decimal_convert (const gdb_byte *from, int len_from,
1411 enum bfd_endian byte_order_from, gdb_byte *to, int len_to,
1412 enum bfd_endian byte_order_to)
1413{
1414 decNumber number;
1cfb73db 1415
d7236961
UW
1416 decimal_to_number (from, len_from, byte_order_from, &number);
1417 decimal_from_number (&number, to, len_to, byte_order_to);
1cfb73db
UW
1418}
1419
1420
70100014
UW
1421/* Typed floating-point routines. These routines operate on floating-point
1422 values in target format, represented by a byte buffer interpreted as a
1423 "struct type", which may be either a binary or decimal floating-point
1424 type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */
1425
1426/* Return whether the byte-stream ADDR holds a valid value of
1427 floating-point type TYPE. */
1428bool
1429target_float_is_valid (const gdb_byte *addr, const struct type *type)
1430{
1431 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1432 return floatformat_is_valid (floatformat_from_type (type), addr);
1433
1434 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1435 return true;
1436
1437 gdb_assert_not_reached ("unexpected type code");
1438}
1439
1440/* Return whether the byte-stream ADDR, interpreted as floating-point
1441 type TYPE, is numerically equal to zero (of either sign). */
1442bool
1443target_float_is_zero (const gdb_byte *addr, const struct type *type)
1444{
1445 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1446 return (floatformat_classify (floatformat_from_type (type), addr)
1447 == float_zero);
1448
1449 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1450 return decimal_is_zero (addr, TYPE_LENGTH (type),
1451 gdbarch_byte_order (get_type_arch (type)));
1452
1453 gdb_assert_not_reached ("unexpected type code");
1454}
1455
f69fdf9b
UW
1456/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1457 to a string, optionally using the print format FORMAT. */
1458std::string
1459target_float_to_string (const gdb_byte *addr, const struct type *type,
1460 const char *format)
1461{
1462 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1463 return floatformat_to_string (floatformat_from_type (type), addr, format);
1464
1465 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1466 return decimal_to_string (addr, TYPE_LENGTH (type),
1467 gdbarch_byte_order (get_type_arch (type)),
1468 format);
1469
1470 gdb_assert_not_reached ("unexpected type code");
1471}
1472
1473/* Parse string STRING into a target floating-number of type TYPE and
1474 store it as byte-stream ADDR. Return whether parsing succeeded. */
1475bool
1476target_float_from_string (gdb_byte *addr, const struct type *type,
1477 const std::string &string)
1478{
1479 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1480 memset (addr, 0, TYPE_LENGTH (type));
1481
1482 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1483 return floatformat_from_string (floatformat_from_type (type), addr,
1484 string);
1485
1486 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1487 return decimal_from_string (addr, TYPE_LENGTH (type),
1488 gdbarch_byte_order (get_type_arch (type)),
1489 string);
1490
1491 gdb_assert_not_reached ("unexpected type code");
1492}
50637b26
UW
1493
1494/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1495 to an integer value (rounding towards zero). */
1496LONGEST
1497target_float_to_longest (const gdb_byte *addr, const struct type *type)
1498{
1499 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1500 return floatformat_to_longest (floatformat_from_type (type), addr);
1501
1502 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1503 return decimal_to_longest (addr, TYPE_LENGTH (type),
1504 gdbarch_byte_order (get_type_arch (type)));
1505
1506 gdb_assert_not_reached ("unexpected type code");
1507}
1508
1509/* Convert signed integer VAL to a target floating-number of type TYPE
1510 and store it as byte-stream ADDR. */
1511void
1512target_float_from_longest (gdb_byte *addr, const struct type *type,
1513 LONGEST val)
1514{
1515 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1516 memset (addr, 0, TYPE_LENGTH (type));
1517
1518 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1519 {
1520 floatformat_from_longest (floatformat_from_type (type), addr, val);
1521 return;
1522 }
1523
1524 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1525 {
1526 decimal_from_longest (val, addr, TYPE_LENGTH (type),
1527 gdbarch_byte_order (get_type_arch (type)));
1528 return;
1529 }
1530
1531 gdb_assert_not_reached ("unexpected type code");
1532}
1533
1534/* Convert unsigned integer VAL to a target floating-number of type TYPE
1535 and store it as byte-stream ADDR. */
1536void
1537target_float_from_ulongest (gdb_byte *addr, const struct type *type,
1538 ULONGEST val)
1539{
1540 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1541 memset (addr, 0, TYPE_LENGTH (type));
1542
1543 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1544 {
1545 floatformat_from_ulongest (floatformat_from_type (type), addr, val);
1546 return;
1547 }
1548
1549 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1550 {
1551 decimal_from_ulongest (val, addr, TYPE_LENGTH (type),
1552 gdbarch_byte_order (get_type_arch (type)));
1553 return;
1554 }
1555
1556 gdb_assert_not_reached ("unexpected type code");
1557}
1558
14ad9311
UW
1559/* Convert the byte-stream ADDR, interpreted as floating-point type TYPE,
1560 to a floating-point value in the host "double" format. */
1561double
1562target_float_to_host_double (const gdb_byte *addr,
1563 const struct type *type)
1564{
1565 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1566 return floatformat_to_host_double (floatformat_from_type (type), addr);
1567
1568 /* We don't support conversions between target decimal floating-point
1569 types and the host double type here. */
1570
1571 gdb_assert_not_reached ("unexpected type code");
1572}
1573
1574/* Convert floating-point value VAL in the host "double" format to a target
1575 floating-number of type TYPE and store it as byte-stream ADDR. */
1576void
1577target_float_from_host_double (gdb_byte *addr, const struct type *type,
1578 double val)
1579{
1580 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1581 memset (addr, 0, TYPE_LENGTH (type));
1582
1583 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1584 {
1585 floatformat_from_host_double (floatformat_from_type (type), addr, val);
1586 return;
1587 }
1588
1589 /* We don't support conversions between target decimal floating-point
1590 types and the host double type here. */
1591
1592 gdb_assert_not_reached ("unexpected type code");
1593}
1594
50637b26
UW
1595/* Convert a floating-point number of type FROM_TYPE from the target
1596 byte-stream FROM to a floating-point number of type TO_TYPE, and
1597 store it to the target byte-stream TO. */
1598void
1599target_float_convert (const gdb_byte *from, const struct type *from_type,
1600 gdb_byte *to, const struct type *to_type)
1601{
1602 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1603 memset (to, 0, TYPE_LENGTH (to_type));
1604
1605 /* Use direct conversion routines if we have them. */
1606
1607 if (TYPE_CODE (from_type) == TYPE_CODE_FLT
1608 && TYPE_CODE (to_type) == TYPE_CODE_FLT)
1609 {
1610 floatformat_convert (from, floatformat_from_type (from_type),
1611 to, floatformat_from_type (to_type));
1612 return;
1613 }
1614
1615 if (TYPE_CODE (from_type) == TYPE_CODE_DECFLOAT
1616 && TYPE_CODE (to_type) == TYPE_CODE_DECFLOAT)
1617 {
1618 decimal_convert (from, TYPE_LENGTH (from_type),
1619 gdbarch_byte_order (get_type_arch (from_type)),
1620 to, TYPE_LENGTH (to_type),
1621 gdbarch_byte_order (get_type_arch (to_type)));
1622 return;
1623 }
1624
1625 /* We cannot directly convert between binary and decimal floating-point
1626 types, so go via an intermediary string. */
1627
1628 if ((TYPE_CODE (from_type) == TYPE_CODE_FLT
1629 && TYPE_CODE (to_type) == TYPE_CODE_DECFLOAT)
1630 || (TYPE_CODE (from_type) == TYPE_CODE_DECFLOAT
1631 && TYPE_CODE (to_type) == TYPE_CODE_FLT))
1632 {
1633 std::string str = target_float_to_string (from, from_type);
1634 target_float_from_string (to, to_type, str);
1635 return;
1636 }
1637
1638 gdb_assert_not_reached ("unexpected type code");
1639}
66c02b9e
UW
1640
1641/* Perform the binary operation indicated by OPCODE, using as operands the
1642 target byte streams X and Y, interpreted as floating-point numbers of
1643 types TYPE_X and TYPE_Y, respectively. Convert the result to type
1644 TYPE_RES and store it into the byte-stream RES.
1645
1646 The three types must either be all binary floating-point types, or else
1647 all decimal floating-point types. Binary and decimal floating-point
1648 types cannot be mixed within a single operation. */
1649void
1650target_float_binop (enum exp_opcode opcode,
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)
1654{
1655 /* Ensure possible padding bytes in the target buffer are zeroed out. */
1656 memset (res, 0, TYPE_LENGTH (type_res));
1657
1658 if (TYPE_CODE (type_res) == TYPE_CODE_FLT)
1659 {
1660 gdb_assert (TYPE_CODE (type_x) == TYPE_CODE_FLT);
1661 gdb_assert (TYPE_CODE (type_y) == TYPE_CODE_FLT);
1662 return floatformat_binop (opcode,
1663 floatformat_from_type (type_x), x,
1664 floatformat_from_type (type_y), y,
1665 floatformat_from_type (type_res), res);
1666 }
1667
1668 if (TYPE_CODE (type_res) == TYPE_CODE_DECFLOAT)
1669 {
1670 gdb_assert (TYPE_CODE (type_x) == TYPE_CODE_DECFLOAT);
1671 gdb_assert (TYPE_CODE (type_y) == TYPE_CODE_DECFLOAT);
1672 return decimal_binop (opcode,
1673 x, TYPE_LENGTH (type_x),
1674 gdbarch_byte_order (get_type_arch (type_x)),
1675 y, TYPE_LENGTH (type_y),
1676 gdbarch_byte_order (get_type_arch (type_y)),
1677 res, TYPE_LENGTH (type_res),
1678 gdbarch_byte_order (get_type_arch (type_res)));
1679 }
1680
1681 gdb_assert_not_reached ("unexpected type code");
1682}
1683
1684/* Compare the two target byte streams X and Y, interpreted as floating-point
1685 numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y
1686 are equal, -1 if X is less than Y, and 1 otherwise.
1687
1688 The two types must either both be binary floating-point types, or else
1689 both be decimal floating-point types. Binary and decimal floating-point
1690 types cannot compared directly against each other. */
1691int
1692target_float_compare (const gdb_byte *x, const struct type *type_x,
1693 const gdb_byte *y, const struct type *type_y)
1694{
1695 if (TYPE_CODE (type_x) == TYPE_CODE_FLT)
1696 {
1697 gdb_assert (TYPE_CODE (type_y) == TYPE_CODE_FLT);
1698 return floatformat_compare (floatformat_from_type (type_x), x,
1699 floatformat_from_type (type_y), y);
1700 }
1701
1702 if (TYPE_CODE (type_x) == TYPE_CODE_DECFLOAT)
1703 {
1704 gdb_assert (TYPE_CODE (type_y) == TYPE_CODE_DECFLOAT);
1705 return decimal_compare (x, TYPE_LENGTH (type_x),
1706 gdbarch_byte_order (get_type_arch (type_x)),
1707 y, TYPE_LENGTH (type_y),
1708 gdbarch_byte_order (get_type_arch (type_y)));
1709 }
1710
1711 gdb_assert_not_reached ("unexpected type code");
1712}
1713