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