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