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