1 /* Floating point routines for GDB, the GNU debugger. 2 3 Copyright (C) 1986, 1988-2001, 2003-2005, 2007-2012 Free Software 4 Foundation, Inc. 5 6 This file is part of GDB. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 20 21 /* Support for converting target fp numbers into host DOUBLEST format. */ 22 23 /* XXX - This code should really be in libiberty/floatformat.c, 24 however configuration issues with libiberty made this very 25 difficult to do in the available time. */ 26 27 #include "defs.h" 28 #include "doublest.h" 29 #include "floatformat.h" 30 #include "gdb_assert.h" 31 #include "gdb_string.h" 32 #include "gdbtypes.h" 33 #include <math.h> /* ldexp */ 34 35 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not 36 going to bother with trying to muck around with whether it is defined in 37 a system header, what we do if not, etc. */ 38 #define FLOATFORMAT_CHAR_BIT 8 39 40 /* The number of bytes that the largest floating-point type that we 41 can convert to doublest will need. */ 42 #define FLOATFORMAT_LARGEST_BYTES 16 43 44 /* Extract a field which starts at START and is LEN bytes long. DATA and 45 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ 46 static unsigned long 47 get_field (const bfd_byte *data, enum floatformat_byteorders order, 48 unsigned int total_len, unsigned int start, unsigned int len) 49 { 50 unsigned long result; 51 unsigned int cur_byte; 52 int cur_bitshift; 53 54 /* Caller must byte-swap words before calling this routine. */ 55 gdb_assert (order == floatformat_little || order == floatformat_big); 56 57 /* Start at the least significant part of the field. */ 58 if (order == floatformat_little) 59 { 60 /* We start counting from the other end (i.e, from the high bytes 61 rather than the low bytes). As such, we need to be concerned 62 with what happens if bit 0 doesn't start on a byte boundary. 63 I.e, we need to properly handle the case where total_len is 64 not evenly divisible by 8. So we compute ``excess'' which 65 represents the number of bits from the end of our starting 66 byte needed to get to bit 0. */ 67 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); 68 69 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) 70 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); 71 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) 72 - FLOATFORMAT_CHAR_BIT; 73 } 74 else 75 { 76 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; 77 cur_bitshift = 78 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; 79 } 80 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) 81 result = *(data + cur_byte) >> (-cur_bitshift); 82 else 83 result = 0; 84 cur_bitshift += FLOATFORMAT_CHAR_BIT; 85 if (order == floatformat_little) 86 ++cur_byte; 87 else 88 --cur_byte; 89 90 /* Move towards the most significant part of the field. */ 91 while (cur_bitshift < len) 92 { 93 result |= (unsigned long)*(data + cur_byte) << cur_bitshift; 94 cur_bitshift += FLOATFORMAT_CHAR_BIT; 95 switch (order) 96 { 97 case floatformat_little: 98 ++cur_byte; 99 break; 100 case floatformat_big: 101 --cur_byte; 102 break; 103 } 104 } 105 if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT) 106 /* Mask out bits which are not part of the field. */ 107 result &= ((1UL << len) - 1); 108 return result; 109 } 110 111 /* Normalize the byte order of FROM into TO. If no normalization is 112 needed then FMT->byteorder is returned and TO is not changed; 113 otherwise the format of the normalized form in TO is returned. */ 114 115 static enum floatformat_byteorders 116 floatformat_normalize_byteorder (const struct floatformat *fmt, 117 const void *from, void *to) 118 { 119 const unsigned char *swapin; 120 unsigned char *swapout; 121 int words; 122 123 if (fmt->byteorder == floatformat_little 124 || fmt->byteorder == floatformat_big) 125 return fmt->byteorder; 126 127 words = fmt->totalsize / FLOATFORMAT_CHAR_BIT; 128 words >>= 2; 129 130 swapout = (unsigned char *)to; 131 swapin = (const unsigned char *)from; 132 133 if (fmt->byteorder == floatformat_vax) 134 { 135 while (words-- > 0) 136 { 137 *swapout++ = swapin[1]; 138 *swapout++ = swapin[0]; 139 *swapout++ = swapin[3]; 140 *swapout++ = swapin[2]; 141 swapin += 4; 142 } 143 /* This may look weird, since VAX is little-endian, but it is 144 easier to translate to big-endian than to little-endian. */ 145 return floatformat_big; 146 } 147 else 148 { 149 gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword); 150 151 while (words-- > 0) 152 { 153 *swapout++ = swapin[3]; 154 *swapout++ = swapin[2]; 155 *swapout++ = swapin[1]; 156 *swapout++ = swapin[0]; 157 swapin += 4; 158 } 159 return floatformat_big; 160 } 161 } 162 163 /* Convert from FMT to a DOUBLEST. 164 FROM is the address of the extended float. 165 Store the DOUBLEST in *TO. */ 166 167 static void 168 convert_floatformat_to_doublest (const struct floatformat *fmt, 169 const void *from, 170 DOUBLEST *to) 171 { 172 unsigned char *ufrom = (unsigned char *) from; 173 DOUBLEST dto; 174 long exponent; 175 unsigned long mant; 176 unsigned int mant_bits, mant_off; 177 int mant_bits_left; 178 int special_exponent; /* It's a NaN, denorm or zero. */ 179 enum floatformat_byteorders order; 180 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; 181 enum float_kind kind; 182 183 gdb_assert (fmt->totalsize 184 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); 185 186 /* For non-numbers, reuse libiberty's logic to find the correct 187 format. We do not lose any precision in this case by passing 188 through a double. */ 189 kind = floatformat_classify (fmt, from); 190 if (kind == float_infinite || kind == float_nan) 191 { 192 double dto; 193 194 floatformat_to_double (fmt, from, &dto); 195 *to = (DOUBLEST) dto; 196 return; 197 } 198 199 order = floatformat_normalize_byteorder (fmt, ufrom, newfrom); 200 201 if (order != fmt->byteorder) 202 ufrom = newfrom; 203 204 if (fmt->split_half) 205 { 206 DOUBLEST dtop, dbot; 207 208 floatformat_to_doublest (fmt->split_half, ufrom, &dtop); 209 /* Preserve the sign of 0, which is the sign of the top 210 half. */ 211 if (dtop == 0.0) 212 { 213 *to = dtop; 214 return; 215 } 216 floatformat_to_doublest (fmt->split_half, 217 ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, 218 &dbot); 219 *to = dtop + dbot; 220 return; 221 } 222 223 exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start, 224 fmt->exp_len); 225 /* Note that if exponent indicates a NaN, we can't really do anything useful 226 (not knowing if the host has NaN's, or how to build one). So it will 227 end up as an infinity or something close; that is OK. */ 228 229 mant_bits_left = fmt->man_len; 230 mant_off = fmt->man_start; 231 dto = 0.0; 232 233 special_exponent = exponent == 0 || exponent == fmt->exp_nan; 234 235 /* Don't bias NaNs. Use minimum exponent for denorms. For 236 simplicity, we don't check for zero as the exponent doesn't matter. 237 Note the cast to int; exp_bias is unsigned, so it's important to 238 make sure the operation is done in signed arithmetic. */ 239 if (!special_exponent) 240 exponent -= fmt->exp_bias; 241 else if (exponent == 0) 242 exponent = 1 - fmt->exp_bias; 243 244 /* Build the result algebraically. Might go infinite, underflow, etc; 245 who cares. */ 246 247 /* If this format uses a hidden bit, explicitly add it in now. Otherwise, 248 increment the exponent by one to account for the integer bit. */ 249 250 if (!special_exponent) 251 { 252 if (fmt->intbit == floatformat_intbit_no) 253 dto = ldexp (1.0, exponent); 254 else 255 exponent++; 256 } 257 258 while (mant_bits_left > 0) 259 { 260 mant_bits = min (mant_bits_left, 32); 261 262 mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits); 263 264 dto += ldexp ((double) mant, exponent - mant_bits); 265 exponent -= mant_bits; 266 mant_off += mant_bits; 267 mant_bits_left -= mant_bits; 268 } 269 270 /* Negate it if negative. */ 271 if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1)) 272 dto = -dto; 273 *to = dto; 274 } 275 276 /* Set a field which starts at START and is LEN bytes long. DATA and 277 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ 278 static void 279 put_field (unsigned char *data, enum floatformat_byteorders order, 280 unsigned int total_len, unsigned int start, unsigned int len, 281 unsigned long stuff_to_put) 282 { 283 unsigned int cur_byte; 284 int cur_bitshift; 285 286 /* Caller must byte-swap words before calling this routine. */ 287 gdb_assert (order == floatformat_little || order == floatformat_big); 288 289 /* Start at the least significant part of the field. */ 290 if (order == floatformat_little) 291 { 292 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); 293 294 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) 295 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); 296 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) 297 - FLOATFORMAT_CHAR_BIT; 298 } 299 else 300 { 301 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; 302 cur_bitshift = 303 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; 304 } 305 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) 306 { 307 *(data + cur_byte) &= 308 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) 309 << (-cur_bitshift)); 310 *(data + cur_byte) |= 311 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); 312 } 313 cur_bitshift += FLOATFORMAT_CHAR_BIT; 314 if (order == floatformat_little) 315 ++cur_byte; 316 else 317 --cur_byte; 318 319 /* Move towards the most significant part of the field. */ 320 while (cur_bitshift < len) 321 { 322 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) 323 { 324 /* This is the last byte. */ 325 *(data + cur_byte) &= 326 ~((1 << (len - cur_bitshift)) - 1); 327 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); 328 } 329 else 330 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) 331 & ((1 << FLOATFORMAT_CHAR_BIT) - 1)); 332 cur_bitshift += FLOATFORMAT_CHAR_BIT; 333 if (order == floatformat_little) 334 ++cur_byte; 335 else 336 --cur_byte; 337 } 338 } 339 340 #ifdef HAVE_LONG_DOUBLE 341 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR. 342 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to 343 frexp, but operates on the long double data type. */ 344 345 static long double ldfrexp (long double value, int *eptr); 346 347 static long double 348 ldfrexp (long double value, int *eptr) 349 { 350 long double tmp; 351 int exp; 352 353 /* Unfortunately, there are no portable functions for extracting the 354 exponent of a long double, so we have to do it iteratively by 355 multiplying or dividing by two until the fraction is between 0.5 356 and 1.0. */ 357 358 if (value < 0.0l) 359 value = -value; 360 361 tmp = 1.0l; 362 exp = 0; 363 364 if (value >= tmp) /* Value >= 1.0 */ 365 while (value >= tmp) 366 { 367 tmp *= 2.0l; 368 exp++; 369 } 370 else if (value != 0.0l) /* Value < 1.0 and > 0.0 */ 371 { 372 while (value < tmp) 373 { 374 tmp /= 2.0l; 375 exp--; 376 } 377 tmp *= 2.0l; 378 exp++; 379 } 380 381 *eptr = exp; 382 return value / tmp; 383 } 384 #endif /* HAVE_LONG_DOUBLE */ 385 386 387 /* The converse: convert the DOUBLEST *FROM to an extended float and 388 store where TO points. Neither FROM nor TO have any alignment 389 restrictions. */ 390 391 static void 392 convert_doublest_to_floatformat (CONST struct floatformat *fmt, 393 const DOUBLEST *from, void *to) 394 { 395 DOUBLEST dfrom; 396 int exponent; 397 DOUBLEST mant; 398 unsigned int mant_bits, mant_off; 399 int mant_bits_left; 400 unsigned char *uto = (unsigned char *) to; 401 enum floatformat_byteorders order = fmt->byteorder; 402 unsigned char newto[FLOATFORMAT_LARGEST_BYTES]; 403 404 if (order != floatformat_little) 405 order = floatformat_big; 406 407 if (order != fmt->byteorder) 408 uto = newto; 409 410 memcpy (&dfrom, from, sizeof (dfrom)); 411 memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) 412 / FLOATFORMAT_CHAR_BIT); 413 414 if (fmt->split_half) 415 { 416 /* Use static volatile to ensure that any excess precision is 417 removed via storing in memory, and so the top half really is 418 the result of converting to double. */ 419 static volatile double dtop, dbot; 420 DOUBLEST dtopnv, dbotnv; 421 422 dtop = (double) dfrom; 423 /* If the rounded top half is Inf, the bottom must be 0 not NaN 424 or Inf. */ 425 if (dtop + dtop == dtop && dtop != 0.0) 426 dbot = 0.0; 427 else 428 dbot = (double) (dfrom - (DOUBLEST) dtop); 429 dtopnv = dtop; 430 dbotnv = dbot; 431 floatformat_from_doublest (fmt->split_half, &dtopnv, uto); 432 floatformat_from_doublest (fmt->split_half, &dbotnv, 433 (uto 434 + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2)); 435 return; 436 } 437 438 if (dfrom == 0) 439 return; /* Result is zero */ 440 if (dfrom != dfrom) /* Result is NaN */ 441 { 442 /* From is NaN */ 443 put_field (uto, order, fmt->totalsize, fmt->exp_start, 444 fmt->exp_len, fmt->exp_nan); 445 /* Be sure it's not infinity, but NaN value is irrel. */ 446 put_field (uto, order, fmt->totalsize, fmt->man_start, 447 fmt->man_len, 1); 448 goto finalize_byteorder; 449 } 450 451 /* If negative, set the sign bit. */ 452 if (dfrom < 0) 453 { 454 put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1); 455 dfrom = -dfrom; 456 } 457 458 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity. */ 459 { 460 /* Infinity exponent is same as NaN's. */ 461 put_field (uto, order, fmt->totalsize, fmt->exp_start, 462 fmt->exp_len, fmt->exp_nan); 463 /* Infinity mantissa is all zeroes. */ 464 put_field (uto, order, fmt->totalsize, fmt->man_start, 465 fmt->man_len, 0); 466 goto finalize_byteorder; 467 } 468 469 #ifdef HAVE_LONG_DOUBLE 470 mant = ldfrexp (dfrom, &exponent); 471 #else 472 mant = frexp (dfrom, &exponent); 473 #endif 474 475 put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len, 476 exponent + fmt->exp_bias - 1); 477 478 mant_bits_left = fmt->man_len; 479 mant_off = fmt->man_start; 480 while (mant_bits_left > 0) 481 { 482 unsigned long mant_long; 483 484 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; 485 486 mant *= 4294967296.0; 487 mant_long = ((unsigned long) mant) & 0xffffffffL; 488 mant -= mant_long; 489 490 /* If the integer bit is implicit, then we need to discard it. 491 If we are discarding a zero, we should be (but are not) creating 492 a denormalized number which means adjusting the exponent 493 (I think). */ 494 if (mant_bits_left == fmt->man_len 495 && fmt->intbit == floatformat_intbit_no) 496 { 497 mant_long <<= 1; 498 mant_long &= 0xffffffffL; 499 /* If we are processing the top 32 mantissa bits of a doublest 500 so as to convert to a float value with implied integer bit, 501 we will only be putting 31 of those 32 bits into the 502 final value due to the discarding of the top bit. In the 503 case of a small float value where the number of mantissa 504 bits is less than 32, discarding the top bit does not alter 505 the number of bits we will be adding to the result. */ 506 if (mant_bits == 32) 507 mant_bits -= 1; 508 } 509 510 if (mant_bits < 32) 511 { 512 /* The bits we want are in the most significant MANT_BITS bits of 513 mant_long. Move them to the least significant. */ 514 mant_long >>= 32 - mant_bits; 515 } 516 517 put_field (uto, order, fmt->totalsize, 518 mant_off, mant_bits, mant_long); 519 mant_off += mant_bits; 520 mant_bits_left -= mant_bits; 521 } 522 523 finalize_byteorder: 524 /* Do we need to byte-swap the words in the result? */ 525 if (order != fmt->byteorder) 526 floatformat_normalize_byteorder (fmt, newto, to); 527 } 528 529 /* Check if VAL (which is assumed to be a floating point number whose 530 format is described by FMT) is negative. */ 531 532 int 533 floatformat_is_negative (const struct floatformat *fmt, 534 const bfd_byte *uval) 535 { 536 enum floatformat_byteorders order; 537 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; 538 539 gdb_assert (fmt != NULL); 540 gdb_assert (fmt->totalsize 541 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); 542 543 order = floatformat_normalize_byteorder (fmt, uval, newfrom); 544 545 if (order != fmt->byteorder) 546 uval = newfrom; 547 548 return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1); 549 } 550 551 /* Check if VAL is "not a number" (NaN) for FMT. */ 552 553 enum float_kind 554 floatformat_classify (const struct floatformat *fmt, 555 const bfd_byte *uval) 556 { 557 long exponent; 558 unsigned long mant; 559 unsigned int mant_bits, mant_off; 560 int mant_bits_left; 561 enum floatformat_byteorders order; 562 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; 563 int mant_zero; 564 565 gdb_assert (fmt != NULL); 566 gdb_assert (fmt->totalsize 567 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); 568 569 order = floatformat_normalize_byteorder (fmt, uval, newfrom); 570 571 if (order != fmt->byteorder) 572 uval = newfrom; 573 574 exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start, 575 fmt->exp_len); 576 577 mant_bits_left = fmt->man_len; 578 mant_off = fmt->man_start; 579 580 mant_zero = 1; 581 while (mant_bits_left > 0) 582 { 583 mant_bits = min (mant_bits_left, 32); 584 585 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); 586 587 /* If there is an explicit integer bit, mask it off. */ 588 if (mant_off == fmt->man_start 589 && fmt->intbit == floatformat_intbit_yes) 590 mant &= ~(1 << (mant_bits - 1)); 591 592 if (mant) 593 { 594 mant_zero = 0; 595 break; 596 } 597 598 mant_off += mant_bits; 599 mant_bits_left -= mant_bits; 600 } 601 602 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not 603 supported. */ 604 if (! fmt->exp_nan) 605 { 606 if (mant_zero) 607 return float_zero; 608 else 609 return float_normal; 610 } 611 612 if (exponent == 0 && !mant_zero) 613 return float_subnormal; 614 615 if (exponent == fmt->exp_nan) 616 { 617 if (mant_zero) 618 return float_infinite; 619 else 620 return float_nan; 621 } 622 623 if (mant_zero) 624 return float_zero; 625 626 return float_normal; 627 } 628 629 /* Convert the mantissa of VAL (which is assumed to be a floating 630 point number whose format is described by FMT) into a hexadecimal 631 and store it in a static string. Return a pointer to that string. */ 632 633 const char * 634 floatformat_mantissa (const struct floatformat *fmt, 635 const bfd_byte *val) 636 { 637 unsigned char *uval = (unsigned char *) val; 638 unsigned long mant; 639 unsigned int mant_bits, mant_off; 640 int mant_bits_left; 641 static char res[50]; 642 char buf[9]; 643 int len; 644 enum floatformat_byteorders order; 645 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; 646 647 gdb_assert (fmt != NULL); 648 gdb_assert (fmt->totalsize 649 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); 650 651 order = floatformat_normalize_byteorder (fmt, uval, newfrom); 652 653 if (order != fmt->byteorder) 654 uval = newfrom; 655 656 if (! fmt->exp_nan) 657 return 0; 658 659 /* Make sure we have enough room to store the mantissa. */ 660 gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2); 661 662 mant_off = fmt->man_start; 663 mant_bits_left = fmt->man_len; 664 mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32; 665 666 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); 667 668 len = xsnprintf (res, sizeof res, "%lx", mant); 669 670 mant_off += mant_bits; 671 mant_bits_left -= mant_bits; 672 673 while (mant_bits_left > 0) 674 { 675 mant = get_field (uval, order, fmt->totalsize, mant_off, 32); 676 677 xsnprintf (buf, sizeof buf, "%08lx", mant); 678 gdb_assert (len + strlen (buf) <= sizeof res); 679 strcat (res, buf); 680 681 mant_off += 32; 682 mant_bits_left -= 32; 683 } 684 685 return res; 686 } 687 688 689 /* Convert TO/FROM target to the hosts DOUBLEST floating-point format. 690 691 If the host and target formats agree, we just copy the raw data 692 into the appropriate type of variable and return, letting the host 693 increase precision as necessary. Otherwise, we call the conversion 694 routine and let it do the dirty work. */ 695 696 static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT; 697 static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT; 698 static const struct floatformat *host_long_double_format 699 = GDB_HOST_LONG_DOUBLE_FORMAT; 700 701 void 702 floatformat_to_doublest (const struct floatformat *fmt, 703 const void *in, DOUBLEST *out) 704 { 705 gdb_assert (fmt != NULL); 706 if (fmt == host_float_format) 707 { 708 float val; 709 710 memcpy (&val, in, sizeof (val)); 711 *out = val; 712 } 713 else if (fmt == host_double_format) 714 { 715 double val; 716 717 memcpy (&val, in, sizeof (val)); 718 *out = val; 719 } 720 else if (fmt == host_long_double_format) 721 { 722 long double val; 723 724 memcpy (&val, in, sizeof (val)); 725 *out = val; 726 } 727 else 728 convert_floatformat_to_doublest (fmt, in, out); 729 } 730 731 void 732 floatformat_from_doublest (const struct floatformat *fmt, 733 const DOUBLEST *in, void *out) 734 { 735 gdb_assert (fmt != NULL); 736 if (fmt == host_float_format) 737 { 738 float val = *in; 739 740 memcpy (out, &val, sizeof (val)); 741 } 742 else if (fmt == host_double_format) 743 { 744 double val = *in; 745 746 memcpy (out, &val, sizeof (val)); 747 } 748 else if (fmt == host_long_double_format) 749 { 750 long double val = *in; 751 752 memcpy (out, &val, sizeof (val)); 753 } 754 else 755 convert_doublest_to_floatformat (fmt, in, out); 756 } 757 758 759 /* Return a floating-point format for a floating-point variable of 760 length LEN. If no suitable floating-point format is found, an 761 error is thrown. 762 763 We need this functionality since information about the 764 floating-point format of a type is not always available to GDB; the 765 debug information typically only tells us the size of a 766 floating-point type. 767 768 FIXME: kettenis/2001-10-28: In many places, particularly in 769 target-dependent code, the format of floating-point types is known, 770 but not passed on by GDB. This should be fixed. */ 771 772 static const struct floatformat * 773 floatformat_from_length (struct gdbarch *gdbarch, int len) 774 { 775 const struct floatformat *format; 776 777 if (len * TARGET_CHAR_BIT == gdbarch_half_bit (gdbarch)) 778 format = gdbarch_half_format (gdbarch) 779 [gdbarch_byte_order (gdbarch)]; 780 else if (len * TARGET_CHAR_BIT == gdbarch_float_bit (gdbarch)) 781 format = gdbarch_float_format (gdbarch) 782 [gdbarch_byte_order (gdbarch)]; 783 else if (len * TARGET_CHAR_BIT == gdbarch_double_bit (gdbarch)) 784 format = gdbarch_double_format (gdbarch) 785 [gdbarch_byte_order (gdbarch)]; 786 else if (len * TARGET_CHAR_BIT == gdbarch_long_double_bit (gdbarch)) 787 format = gdbarch_long_double_format (gdbarch) 788 [gdbarch_byte_order (gdbarch)]; 789 /* On i386 the 'long double' type takes 96 bits, 790 while the real number of used bits is only 80, 791 both in processor and in memory. 792 The code below accepts the real bit size. */ 793 else if ((gdbarch_long_double_format (gdbarch) != NULL) 794 && (len * TARGET_CHAR_BIT 795 == gdbarch_long_double_format (gdbarch)[0]->totalsize)) 796 format = gdbarch_long_double_format (gdbarch) 797 [gdbarch_byte_order (gdbarch)]; 798 else 799 format = NULL; 800 if (format == NULL) 801 error (_("Unrecognized %d-bit floating-point type."), 802 len * TARGET_CHAR_BIT); 803 return format; 804 } 805 806 const struct floatformat * 807 floatformat_from_type (const struct type *type) 808 { 809 struct gdbarch *gdbarch = get_type_arch (type); 810 811 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT); 812 if (TYPE_FLOATFORMAT (type) != NULL) 813 return TYPE_FLOATFORMAT (type)[gdbarch_byte_order (gdbarch)]; 814 else 815 return floatformat_from_length (gdbarch, TYPE_LENGTH (type)); 816 } 817 818 /* Extract a floating-point number of type TYPE from a target-order 819 byte-stream at ADDR. Returns the value as type DOUBLEST. */ 820 821 DOUBLEST 822 extract_typed_floating (const void *addr, const struct type *type) 823 { 824 const struct floatformat *fmt = floatformat_from_type (type); 825 DOUBLEST retval; 826 827 floatformat_to_doublest (fmt, addr, &retval); 828 return retval; 829 } 830 831 /* Store VAL as a floating-point number of type TYPE to a target-order 832 byte-stream at ADDR. */ 833 834 void 835 store_typed_floating (void *addr, const struct type *type, DOUBLEST val) 836 { 837 const struct floatformat *fmt = floatformat_from_type (type); 838 839 /* FIXME: kettenis/2001-10-28: It is debatable whether we should 840 zero out any remaining bytes in the target buffer when TYPE is 841 longer than the actual underlying floating-point format. Perhaps 842 we should store a fixed bitpattern in those remaining bytes, 843 instead of zero, or perhaps we shouldn't touch those remaining 844 bytes at all. 845 846 NOTE: cagney/2001-10-28: With the way things currently work, it 847 isn't a good idea to leave the end bits undefined. This is 848 because GDB writes out the entire sizeof(<floating>) bits of the 849 floating-point type even though the value might only be stored 850 in, and the target processor may only refer to, the first N < 851 TYPE_LENGTH (type) bits. If the end of the buffer wasn't 852 initialized, GDB would write undefined data to the target. An 853 errant program, refering to that undefined data, would then 854 become non-deterministic. 855 856 See also the function convert_typed_floating below. */ 857 memset (addr, 0, TYPE_LENGTH (type)); 858 859 floatformat_from_doublest (fmt, &val, addr); 860 } 861 862 /* Convert a floating-point number of type FROM_TYPE from a 863 target-order byte-stream at FROM to a floating-point number of type 864 TO_TYPE, and store it to a target-order byte-stream at TO. */ 865 866 void 867 convert_typed_floating (const void *from, const struct type *from_type, 868 void *to, const struct type *to_type) 869 { 870 const struct floatformat *from_fmt = floatformat_from_type (from_type); 871 const struct floatformat *to_fmt = floatformat_from_type (to_type); 872 873 if (from_fmt == NULL || to_fmt == NULL) 874 { 875 /* If we don't know the floating-point format of FROM_TYPE or 876 TO_TYPE, there's not much we can do. We might make the 877 assumption that if the length of FROM_TYPE and TO_TYPE match, 878 their floating-point format would match too, but that 879 assumption might be wrong on targets that support 880 floating-point types that only differ in endianness for 881 example. So we warn instead, and zero out the target buffer. */ 882 warning (_("Can't convert floating-point number to desired type.")); 883 memset (to, 0, TYPE_LENGTH (to_type)); 884 } 885 else if (from_fmt == to_fmt) 886 { 887 /* We're in business. The floating-point format of FROM_TYPE 888 and TO_TYPE match. However, even though the floating-point 889 format matches, the length of the type might still be 890 different. Make sure we don't overrun any buffers. See 891 comment in store_typed_floating for a discussion about 892 zeroing out remaining bytes in the target buffer. */ 893 memset (to, 0, TYPE_LENGTH (to_type)); 894 memcpy (to, from, min (TYPE_LENGTH (from_type), TYPE_LENGTH (to_type))); 895 } 896 else 897 { 898 /* The floating-point types don't match. The best we can do 899 (apart from simulating the target FPU) is converting to the 900 widest floating-point type supported by the host, and then 901 again to the desired type. */ 902 DOUBLEST d; 903 904 floatformat_to_doublest (from_fmt, from, &d); 905 floatformat_from_doublest (to_fmt, &d, to); 906 } 907 } 908 909 const struct floatformat *floatformat_ieee_single[BFD_ENDIAN_UNKNOWN]; 910 const struct floatformat *floatformat_ieee_double[BFD_ENDIAN_UNKNOWN]; 911 const struct floatformat *floatformat_ieee_quad[BFD_ENDIAN_UNKNOWN]; 912 const struct floatformat *floatformat_arm_ext[BFD_ENDIAN_UNKNOWN]; 913 const struct floatformat *floatformat_ia64_spill[BFD_ENDIAN_UNKNOWN]; 914 915 extern void _initialize_doublest (void); 916 917 extern void 918 _initialize_doublest (void) 919 { 920 floatformat_ieee_single[BFD_ENDIAN_LITTLE] = &floatformat_ieee_single_little; 921 floatformat_ieee_single[BFD_ENDIAN_BIG] = &floatformat_ieee_single_big; 922 floatformat_ieee_double[BFD_ENDIAN_LITTLE] = &floatformat_ieee_double_little; 923 floatformat_ieee_double[BFD_ENDIAN_BIG] = &floatformat_ieee_double_big; 924 floatformat_arm_ext[BFD_ENDIAN_LITTLE] 925 = &floatformat_arm_ext_littlebyte_bigword; 926 floatformat_arm_ext[BFD_ENDIAN_BIG] = &floatformat_arm_ext_big; 927 floatformat_ia64_spill[BFD_ENDIAN_LITTLE] = &floatformat_ia64_spill_little; 928 floatformat_ia64_spill[BFD_ENDIAN_BIG] = &floatformat_ia64_spill_big; 929 floatformat_ieee_quad[BFD_ENDIAN_LITTLE] = &floatformat_ia64_quad_little; 930 floatformat_ieee_quad[BFD_ENDIAN_BIG] = &floatformat_ia64_quad_big; 931 } 932