1 1.1 christos /* Floating point routines for GDB, the GNU debugger. 2 1.1 christos 3 1.1.1.4 christos Copyright (C) 2017-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #include "gdbtypes.h" 21 1.1 christos #include "floatformat.h" 22 1.1 christos #include "target-float.h" 23 1.1.1.2 christos #include "gdbarch.h" 24 1.1 christos 25 1.1 christos /* Target floating-point operations. 26 1.1 christos 27 1.1 christos We provide multiple implementations of those operations, which differ 28 1.1 christos by the host-side intermediate format they perform computations in. 29 1.1 christos 30 1.1 christos Those multiple implementations all derive from the following abstract 31 1.1 christos base class, which specifies the set of operations to be implemented. */ 32 1.1 christos 33 1.1 christos class target_float_ops 34 1.1 christos { 35 1.1 christos public: 36 1.1 christos virtual std::string to_string (const gdb_byte *addr, const struct type *type, 37 1.1 christos const char *format) const = 0; 38 1.1 christos virtual bool from_string (gdb_byte *addr, const struct type *type, 39 1.1 christos const std::string &string) const = 0; 40 1.1 christos 41 1.1 christos virtual LONGEST to_longest (const gdb_byte *addr, 42 1.1 christos const struct type *type) const = 0; 43 1.1 christos virtual void from_longest (gdb_byte *addr, const struct type *type, 44 1.1 christos LONGEST val) const = 0; 45 1.1 christos virtual void from_ulongest (gdb_byte *addr, const struct type *type, 46 1.1 christos ULONGEST val) const = 0; 47 1.1 christos virtual double to_host_double (const gdb_byte *addr, 48 1.1 christos const struct type *type) const = 0; 49 1.1 christos virtual void from_host_double (gdb_byte *addr, const struct type *type, 50 1.1 christos double val) const = 0; 51 1.1 christos virtual void convert (const gdb_byte *from, const struct type *from_type, 52 1.1 christos gdb_byte *to, const struct type *to_type) const = 0; 53 1.1 christos 54 1.1 christos virtual void binop (enum exp_opcode opcode, 55 1.1 christos const gdb_byte *x, const struct type *type_x, 56 1.1 christos const gdb_byte *y, const struct type *type_y, 57 1.1 christos gdb_byte *res, const struct type *type_res) const = 0; 58 1.1 christos virtual int compare (const gdb_byte *x, const struct type *type_x, 59 1.1 christos const gdb_byte *y, const struct type *type_y) const = 0; 60 1.1 christos }; 61 1.1 christos 62 1.1 christos 63 1.1 christos /* Helper routines operating on binary floating-point data. */ 64 1.1 christos 65 1.1 christos #include <cmath> 66 1.1 christos #include <limits> 67 1.1 christos 68 1.1 christos /* Different kinds of floatformat numbers recognized by 69 1.1 christos floatformat_classify. To avoid portability issues, we use local 70 1.1 christos values instead of the C99 macros (FP_NAN et cetera). */ 71 1.1 christos enum float_kind { 72 1.1 christos float_nan, 73 1.1 christos float_infinite, 74 1.1 christos float_zero, 75 1.1 christos float_normal, 76 1.1 christos float_subnormal 77 1.1 christos }; 78 1.1 christos 79 1.1 christos /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not 80 1.1 christos going to bother with trying to muck around with whether it is defined in 81 1.1 christos a system header, what we do if not, etc. */ 82 1.1 christos #define FLOATFORMAT_CHAR_BIT 8 83 1.1 christos 84 1.1 christos /* The number of bytes that the largest floating-point type that we 85 1.1 christos can convert to doublest will need. */ 86 1.1 christos #define FLOATFORMAT_LARGEST_BYTES 16 87 1.1 christos 88 1.1 christos /* Return the floatformat's total size in host bytes. */ 89 1.1 christos static size_t 90 1.1 christos floatformat_totalsize_bytes (const struct floatformat *fmt) 91 1.1 christos { 92 1.1 christos return ((fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) 93 1.1 christos / FLOATFORMAT_CHAR_BIT); 94 1.1 christos } 95 1.1 christos 96 1.1 christos /* Return the precision of the floating point format FMT. */ 97 1.1 christos static int 98 1.1 christos floatformat_precision (const struct floatformat *fmt) 99 1.1 christos { 100 1.1 christos /* Assume the precision of and IBM long double is twice the precision 101 1.1 christos of the underlying double. This matches what GCC does. */ 102 1.1 christos if (fmt->split_half) 103 1.1 christos return 2 * floatformat_precision (fmt->split_half); 104 1.1 christos 105 1.1 christos /* Otherwise, the precision is the size of mantissa in bits, 106 1.1 christos including the implicit bit if present. */ 107 1.1 christos int prec = fmt->man_len; 108 1.1 christos if (fmt->intbit == floatformat_intbit_no) 109 1.1 christos prec++; 110 1.1 christos 111 1.1 christos return prec; 112 1.1 christos } 113 1.1 christos 114 1.1 christos /* Normalize the byte order of FROM into TO. If no normalization is 115 1.1 christos needed then FMT->byteorder is returned and TO is not changed; 116 1.1 christos otherwise the format of the normalized form in TO is returned. */ 117 1.1 christos static enum floatformat_byteorders 118 1.1 christos floatformat_normalize_byteorder (const struct floatformat *fmt, 119 1.1 christos const void *from, void *to) 120 1.1 christos { 121 1.1 christos const unsigned char *swapin; 122 1.1 christos unsigned char *swapout; 123 1.1 christos int words; 124 1.1 christos 125 1.1 christos if (fmt->byteorder == floatformat_little 126 1.1 christos || fmt->byteorder == floatformat_big) 127 1.1 christos return fmt->byteorder; 128 1.1 christos 129 1.1 christos words = fmt->totalsize / FLOATFORMAT_CHAR_BIT; 130 1.1 christos words >>= 2; 131 1.1 christos 132 1.1 christos swapout = (unsigned char *)to; 133 1.1 christos swapin = (const unsigned char *)from; 134 1.1 christos 135 1.1 christos if (fmt->byteorder == floatformat_vax) 136 1.1 christos { 137 1.1 christos while (words-- > 0) 138 1.1 christos { 139 1.1 christos *swapout++ = swapin[1]; 140 1.1 christos *swapout++ = swapin[0]; 141 1.1 christos *swapout++ = swapin[3]; 142 1.1 christos *swapout++ = swapin[2]; 143 1.1 christos swapin += 4; 144 1.1 christos } 145 1.1 christos /* This may look weird, since VAX is little-endian, but it is 146 1.1 christos easier to translate to big-endian than to little-endian. */ 147 1.1 christos return floatformat_big; 148 1.1 christos } 149 1.1 christos else 150 1.1 christos { 151 1.1 christos gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword); 152 1.1 christos 153 1.1 christos while (words-- > 0) 154 1.1 christos { 155 1.1 christos *swapout++ = swapin[3]; 156 1.1 christos *swapout++ = swapin[2]; 157 1.1 christos *swapout++ = swapin[1]; 158 1.1 christos *swapout++ = swapin[0]; 159 1.1 christos swapin += 4; 160 1.1 christos } 161 1.1 christos return floatformat_big; 162 1.1 christos } 163 1.1 christos } 164 1.1 christos 165 1.1 christos /* Extract a field which starts at START and is LEN bytes long. DATA and 166 1.1 christos TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ 167 1.1 christos static unsigned long 168 1.1 christos get_field (const bfd_byte *data, enum floatformat_byteorders order, 169 1.1 christos unsigned int total_len, unsigned int start, unsigned int len) 170 1.1 christos { 171 1.1 christos unsigned long result; 172 1.1 christos unsigned int cur_byte; 173 1.1 christos int cur_bitshift; 174 1.1 christos 175 1.1 christos /* Caller must byte-swap words before calling this routine. */ 176 1.1 christos gdb_assert (order == floatformat_little || order == floatformat_big); 177 1.1 christos 178 1.1 christos /* Start at the least significant part of the field. */ 179 1.1 christos if (order == floatformat_little) 180 1.1 christos { 181 1.1 christos /* We start counting from the other end (i.e, from the high bytes 182 1.1 christos rather than the low bytes). As such, we need to be concerned 183 1.1 christos with what happens if bit 0 doesn't start on a byte boundary. 184 1.1 christos I.e, we need to properly handle the case where total_len is 185 1.1 christos not evenly divisible by 8. So we compute ``excess'' which 186 1.1 christos represents the number of bits from the end of our starting 187 1.1 christos byte needed to get to bit 0. */ 188 1.1 christos int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); 189 1.1 christos 190 1.1 christos cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) 191 1.1.1.3 christos - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); 192 1.1 christos cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) 193 1.1.1.3 christos - FLOATFORMAT_CHAR_BIT; 194 1.1 christos } 195 1.1 christos else 196 1.1 christos { 197 1.1 christos cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; 198 1.1 christos cur_bitshift = 199 1.1 christos ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; 200 1.1 christos } 201 1.1 christos if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) 202 1.1 christos result = *(data + cur_byte) >> (-cur_bitshift); 203 1.1 christos else 204 1.1 christos result = 0; 205 1.1 christos cur_bitshift += FLOATFORMAT_CHAR_BIT; 206 1.1 christos if (order == floatformat_little) 207 1.1 christos ++cur_byte; 208 1.1 christos else 209 1.1 christos --cur_byte; 210 1.1 christos 211 1.1 christos /* Move towards the most significant part of the field. */ 212 1.1 christos while (cur_bitshift < len) 213 1.1 christos { 214 1.1 christos result |= (unsigned long)*(data + cur_byte) << cur_bitshift; 215 1.1 christos cur_bitshift += FLOATFORMAT_CHAR_BIT; 216 1.1 christos switch (order) 217 1.1 christos { 218 1.1 christos case floatformat_little: 219 1.1 christos ++cur_byte; 220 1.1 christos break; 221 1.1 christos case floatformat_big: 222 1.1 christos --cur_byte; 223 1.1 christos break; 224 1.1 christos } 225 1.1 christos } 226 1.1 christos if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT) 227 1.1 christos /* Mask out bits which are not part of the field. */ 228 1.1 christos result &= ((1UL << len) - 1); 229 1.1 christos return result; 230 1.1 christos } 231 1.1 christos 232 1.1 christos /* Set a field which starts at START and is LEN bytes long. DATA and 233 1.1 christos TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ 234 1.1 christos static void 235 1.1 christos put_field (unsigned char *data, enum floatformat_byteorders order, 236 1.1 christos unsigned int total_len, unsigned int start, unsigned int len, 237 1.1 christos unsigned long stuff_to_put) 238 1.1 christos { 239 1.1 christos unsigned int cur_byte; 240 1.1 christos int cur_bitshift; 241 1.1 christos 242 1.1 christos /* Caller must byte-swap words before calling this routine. */ 243 1.1 christos gdb_assert (order == floatformat_little || order == floatformat_big); 244 1.1 christos 245 1.1 christos /* Start at the least significant part of the field. */ 246 1.1 christos if (order == floatformat_little) 247 1.1 christos { 248 1.1 christos int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); 249 1.1 christos 250 1.1 christos cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) 251 1.1.1.3 christos - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); 252 1.1 christos cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) 253 1.1.1.3 christos - FLOATFORMAT_CHAR_BIT; 254 1.1 christos } 255 1.1 christos else 256 1.1 christos { 257 1.1 christos cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; 258 1.1 christos cur_bitshift = 259 1.1 christos ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; 260 1.1 christos } 261 1.1 christos if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) 262 1.1 christos { 263 1.1 christos *(data + cur_byte) &= 264 1.1 christos ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) 265 1.1 christos << (-cur_bitshift)); 266 1.1 christos *(data + cur_byte) |= 267 1.1 christos (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); 268 1.1 christos } 269 1.1 christos cur_bitshift += FLOATFORMAT_CHAR_BIT; 270 1.1 christos if (order == floatformat_little) 271 1.1 christos ++cur_byte; 272 1.1 christos else 273 1.1 christos --cur_byte; 274 1.1 christos 275 1.1 christos /* Move towards the most significant part of the field. */ 276 1.1 christos while (cur_bitshift < len) 277 1.1 christos { 278 1.1 christos if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) 279 1.1 christos { 280 1.1 christos /* This is the last byte. */ 281 1.1 christos *(data + cur_byte) &= 282 1.1 christos ~((1 << (len - cur_bitshift)) - 1); 283 1.1 christos *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); 284 1.1 christos } 285 1.1 christos else 286 1.1 christos *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) 287 1.1 christos & ((1 << FLOATFORMAT_CHAR_BIT) - 1)); 288 1.1 christos cur_bitshift += FLOATFORMAT_CHAR_BIT; 289 1.1 christos if (order == floatformat_little) 290 1.1 christos ++cur_byte; 291 1.1 christos else 292 1.1 christos --cur_byte; 293 1.1 christos } 294 1.1 christos } 295 1.1 christos 296 1.1 christos /* Check if VAL (which is assumed to be a floating point number whose 297 1.1 christos format is described by FMT) is negative. */ 298 1.1 christos static int 299 1.1 christos floatformat_is_negative (const struct floatformat *fmt, 300 1.1 christos const bfd_byte *uval) 301 1.1 christos { 302 1.1 christos enum floatformat_byteorders order; 303 1.1 christos unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; 304 1.1 christos 305 1.1 christos gdb_assert (fmt != NULL); 306 1.1 christos gdb_assert (fmt->totalsize 307 1.1 christos <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); 308 1.1 christos 309 1.1 christos /* An IBM long double (a two element array of double) always takes the 310 1.1 christos sign of the first double. */ 311 1.1 christos if (fmt->split_half) 312 1.1 christos fmt = fmt->split_half; 313 1.1 christos 314 1.1 christos order = floatformat_normalize_byteorder (fmt, uval, newfrom); 315 1.1 christos 316 1.1 christos if (order != fmt->byteorder) 317 1.1 christos uval = newfrom; 318 1.1 christos 319 1.1 christos return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1); 320 1.1 christos } 321 1.1 christos 322 1.1 christos /* Check if VAL is "not a number" (NaN) for FMT. */ 323 1.1 christos static enum float_kind 324 1.1 christos floatformat_classify (const struct floatformat *fmt, 325 1.1 christos const bfd_byte *uval) 326 1.1 christos { 327 1.1 christos long exponent; 328 1.1 christos unsigned long mant; 329 1.1 christos unsigned int mant_bits, mant_off; 330 1.1 christos int mant_bits_left; 331 1.1 christos enum floatformat_byteorders order; 332 1.1 christos unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; 333 1.1 christos int mant_zero; 334 1.1 christos 335 1.1 christos gdb_assert (fmt != NULL); 336 1.1 christos gdb_assert (fmt->totalsize 337 1.1 christos <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); 338 1.1 christos 339 1.1 christos /* An IBM long double (a two element array of double) can be classified 340 1.1 christos by looking at the first double. inf and nan are specified as 341 1.1 christos ignoring the second double. zero and subnormal will always have 342 1.1 christos the second double 0.0 if the long double is correctly rounded. */ 343 1.1 christos if (fmt->split_half) 344 1.1 christos fmt = fmt->split_half; 345 1.1 christos 346 1.1 christos order = floatformat_normalize_byteorder (fmt, uval, newfrom); 347 1.1 christos 348 1.1 christos if (order != fmt->byteorder) 349 1.1 christos uval = newfrom; 350 1.1 christos 351 1.1 christos exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start, 352 1.1 christos fmt->exp_len); 353 1.1 christos 354 1.1 christos mant_bits_left = fmt->man_len; 355 1.1 christos mant_off = fmt->man_start; 356 1.1 christos 357 1.1 christos mant_zero = 1; 358 1.1 christos while (mant_bits_left > 0) 359 1.1 christos { 360 1.1 christos mant_bits = std::min (mant_bits_left, 32); 361 1.1 christos 362 1.1 christos mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); 363 1.1 christos 364 1.1 christos /* If there is an explicit integer bit, mask it off. */ 365 1.1 christos if (mant_off == fmt->man_start 366 1.1 christos && fmt->intbit == floatformat_intbit_yes) 367 1.1 christos mant &= ~(1 << (mant_bits - 1)); 368 1.1 christos 369 1.1 christos if (mant) 370 1.1 christos { 371 1.1 christos mant_zero = 0; 372 1.1 christos break; 373 1.1 christos } 374 1.1 christos 375 1.1 christos mant_off += mant_bits; 376 1.1 christos mant_bits_left -= mant_bits; 377 1.1 christos } 378 1.1 christos 379 1.1 christos /* If exp_nan is not set, assume that inf, NaN, and subnormals are not 380 1.1 christos supported. */ 381 1.1 christos if (! fmt->exp_nan) 382 1.1 christos { 383 1.1 christos if (mant_zero) 384 1.1 christos return float_zero; 385 1.1 christos else 386 1.1 christos return float_normal; 387 1.1 christos } 388 1.1 christos 389 1.1 christos if (exponent == 0) 390 1.1 christos { 391 1.1 christos if (mant_zero) 392 1.1 christos return float_zero; 393 1.1 christos else 394 1.1 christos return float_subnormal; 395 1.1 christos } 396 1.1 christos 397 1.1 christos if (exponent == fmt->exp_nan) 398 1.1 christos { 399 1.1 christos if (mant_zero) 400 1.1 christos return float_infinite; 401 1.1 christos else 402 1.1 christos return float_nan; 403 1.1 christos } 404 1.1 christos 405 1.1 christos return float_normal; 406 1.1 christos } 407 1.1 christos 408 1.1 christos /* Convert the mantissa of VAL (which is assumed to be a floating 409 1.1 christos point number whose format is described by FMT) into a hexadecimal 410 1.1 christos and store it in a static string. Return a pointer to that string. */ 411 1.1 christos static const char * 412 1.1 christos floatformat_mantissa (const struct floatformat *fmt, 413 1.1 christos const bfd_byte *val) 414 1.1 christos { 415 1.1 christos unsigned char *uval = (unsigned char *) val; 416 1.1 christos unsigned long mant; 417 1.1 christos unsigned int mant_bits, mant_off; 418 1.1 christos int mant_bits_left; 419 1.1 christos static char res[50]; 420 1.1 christos char buf[9]; 421 1.1 christos int len; 422 1.1 christos enum floatformat_byteorders order; 423 1.1 christos unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; 424 1.1 christos 425 1.1 christos gdb_assert (fmt != NULL); 426 1.1 christos gdb_assert (fmt->totalsize 427 1.1 christos <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); 428 1.1 christos 429 1.1 christos /* For IBM long double (a two element array of double), return the 430 1.1 christos mantissa of the first double. The problem with returning the 431 1.1 christos actual mantissa from both doubles is that there can be an 432 1.1 christos arbitrary number of implied 0's or 1's between the mantissas 433 1.1 christos of the first and second double. In any case, this function 434 1.1 christos is only used for dumping out nans, and a nan is specified to 435 1.1 christos ignore the value in the second double. */ 436 1.1 christos if (fmt->split_half) 437 1.1 christos fmt = fmt->split_half; 438 1.1 christos 439 1.1 christos order = floatformat_normalize_byteorder (fmt, uval, newfrom); 440 1.1 christos 441 1.1 christos if (order != fmt->byteorder) 442 1.1 christos uval = newfrom; 443 1.1 christos 444 1.1 christos if (! fmt->exp_nan) 445 1.1 christos return 0; 446 1.1 christos 447 1.1 christos /* Make sure we have enough room to store the mantissa. */ 448 1.1 christos gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2); 449 1.1 christos 450 1.1 christos mant_off = fmt->man_start; 451 1.1 christos mant_bits_left = fmt->man_len; 452 1.1 christos mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32; 453 1.1 christos 454 1.1 christos mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); 455 1.1 christos 456 1.1 christos len = xsnprintf (res, sizeof res, "%lx", mant); 457 1.1 christos 458 1.1 christos mant_off += mant_bits; 459 1.1 christos mant_bits_left -= mant_bits; 460 1.1 christos 461 1.1 christos while (mant_bits_left > 0) 462 1.1 christos { 463 1.1 christos mant = get_field (uval, order, fmt->totalsize, mant_off, 32); 464 1.1 christos 465 1.1 christos xsnprintf (buf, sizeof buf, "%08lx", mant); 466 1.1 christos gdb_assert (len + strlen (buf) <= sizeof res); 467 1.1 christos strcat (res, buf); 468 1.1 christos 469 1.1 christos mant_off += 32; 470 1.1 christos mant_bits_left -= 32; 471 1.1 christos } 472 1.1 christos 473 1.1 christos return res; 474 1.1 christos } 475 1.1 christos 476 1.1 christos /* Convert printf format string FORMAT to the otherwise equivalent string 477 1.1 christos which may be used to print a host floating-point number using the length 478 1.1 christos modifier LENGTH (which may be 0 if none is needed). If FORMAT is null, 479 1.1 christos return a format appropriate to print the full precision of a target 480 1.1 christos floating-point number of format FMT. */ 481 1.1 christos static std::string 482 1.1 christos floatformat_printf_format (const struct floatformat *fmt, 483 1.1.1.3 christos const char *format, char length) 484 1.1 christos { 485 1.1 christos std::string host_format; 486 1.1 christos char conversion; 487 1.1 christos 488 1.1 christos if (format == nullptr) 489 1.1 christos { 490 1.1 christos /* If no format was specified, print the number using a format string 491 1.1 christos where the precision is set to the DECIMAL_DIG value for the given 492 1.1 christos floating-point format. This value is computed as 493 1.1 christos 494 1.1 christos ceil(1 + p * log10(b)), 495 1.1 christos 496 1.1 christos where p is the precision of the floating-point format in bits, and 497 1.1 christos b is the base (which is always 2 for the formats we support). */ 498 1.1 christos const double log10_2 = .30102999566398119521; 499 1.1 christos double d_decimal_dig = 1 + floatformat_precision (fmt) * log10_2; 500 1.1 christos int decimal_dig = d_decimal_dig; 501 1.1 christos if (decimal_dig < d_decimal_dig) 502 1.1 christos decimal_dig++; 503 1.1 christos 504 1.1 christos host_format = string_printf ("%%.%d", decimal_dig); 505 1.1 christos conversion = 'g'; 506 1.1 christos } 507 1.1 christos else 508 1.1 christos { 509 1.1 christos /* Use the specified format, stripping out the conversion character 510 1.1.1.3 christos and length modifier, if present. */ 511 1.1 christos size_t len = strlen (format); 512 1.1 christos gdb_assert (len > 1); 513 1.1 christos conversion = format[--len]; 514 1.1 christos gdb_assert (conversion == 'e' || conversion == 'f' || conversion == 'g' 515 1.1 christos || conversion == 'E' || conversion == 'G'); 516 1.1 christos if (format[len - 1] == 'L') 517 1.1 christos len--; 518 1.1 christos 519 1.1 christos host_format = std::string (format, len); 520 1.1 christos } 521 1.1 christos 522 1.1 christos /* Add the length modifier and conversion character appropriate for 523 1.1 christos handling the appropriate host floating-point type. */ 524 1.1 christos if (length) 525 1.1 christos host_format += length; 526 1.1 christos host_format += conversion; 527 1.1 christos 528 1.1 christos return host_format; 529 1.1 christos } 530 1.1 christos 531 1.1 christos /* Implementation of target_float_ops using the host floating-point type T 532 1.1 christos as intermediate type. */ 533 1.1 christos 534 1.1 christos template<typename T> class host_float_ops : public target_float_ops 535 1.1 christos { 536 1.1 christos public: 537 1.1 christos std::string to_string (const gdb_byte *addr, const struct type *type, 538 1.1 christos const char *format) const override; 539 1.1 christos bool from_string (gdb_byte *addr, const struct type *type, 540 1.1 christos const std::string &string) const override; 541 1.1 christos 542 1.1 christos LONGEST to_longest (const gdb_byte *addr, 543 1.1 christos const struct type *type) const override; 544 1.1 christos void from_longest (gdb_byte *addr, const struct type *type, 545 1.1 christos LONGEST val) const override; 546 1.1 christos void from_ulongest (gdb_byte *addr, const struct type *type, 547 1.1 christos ULONGEST val) const override; 548 1.1 christos double to_host_double (const gdb_byte *addr, 549 1.1 christos const struct type *type) const override; 550 1.1 christos void from_host_double (gdb_byte *addr, const struct type *type, 551 1.1 christos double val) const override; 552 1.1 christos void convert (const gdb_byte *from, const struct type *from_type, 553 1.1 christos gdb_byte *to, const struct type *to_type) const override; 554 1.1 christos 555 1.1 christos void binop (enum exp_opcode opcode, 556 1.1 christos const gdb_byte *x, const struct type *type_x, 557 1.1 christos const gdb_byte *y, const struct type *type_y, 558 1.1 christos gdb_byte *res, const struct type *type_res) const override; 559 1.1 christos int compare (const gdb_byte *x, const struct type *type_x, 560 1.1 christos const gdb_byte *y, const struct type *type_y) const override; 561 1.1 christos 562 1.1 christos private: 563 1.1 christos void from_target (const struct floatformat *fmt, 564 1.1 christos const gdb_byte *from, T *to) const; 565 1.1 christos void from_target (const struct type *type, 566 1.1 christos const gdb_byte *from, T *to) const; 567 1.1 christos 568 1.1 christos void to_target (const struct type *type, 569 1.1 christos const T *from, gdb_byte *to) const; 570 1.1 christos void to_target (const struct floatformat *fmt, 571 1.1 christos const T *from, gdb_byte *to) const; 572 1.1 christos }; 573 1.1 christos 574 1.1 christos 575 1.1 christos /* Convert TO/FROM target to the host floating-point format T. 576 1.1 christos 577 1.1 christos If the host and target formats agree, we just copy the raw data 578 1.1 christos into the appropriate type of variable and return, letting the host 579 1.1 christos increase precision as necessary. Otherwise, we call the conversion 580 1.1 christos routine and let it do the dirty work. Note that even if the target 581 1.1 christos and host floating-point formats match, the length of the types 582 1.1 christos might still be different, so the conversion routines must make sure 583 1.1 christos to not overrun any buffers. For example, on x86, long double is 584 1.1 christos the 80-bit extended precision type on both 32-bit and 64-bit ABIs, 585 1.1 christos but by default it is stored as 12 bytes on 32-bit, and 16 bytes on 586 1.1 christos 64-bit, for alignment reasons. See comment in store_typed_floating 587 1.1 christos for a discussion about zeroing out remaining bytes in the target 588 1.1 christos buffer. */ 589 1.1 christos 590 1.1 christos static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT; 591 1.1 christos static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT; 592 1.1 christos static const struct floatformat *host_long_double_format 593 1.1 christos = GDB_HOST_LONG_DOUBLE_FORMAT; 594 1.1 christos 595 1.1 christos /* Convert target floating-point value at FROM in format FMT to host 596 1.1 christos floating-point format of type T. */ 597 1.1 christos template<typename T> void 598 1.1 christos host_float_ops<T>::from_target (const struct floatformat *fmt, 599 1.1 christos const gdb_byte *from, T *to) const 600 1.1 christos { 601 1.1 christos gdb_assert (fmt != NULL); 602 1.1 christos 603 1.1 christos if (fmt == host_float_format) 604 1.1 christos { 605 1.1 christos float val = 0; 606 1.1 christos 607 1.1 christos memcpy (&val, from, floatformat_totalsize_bytes (fmt)); 608 1.1 christos *to = val; 609 1.1 christos return; 610 1.1 christos } 611 1.1 christos else if (fmt == host_double_format) 612 1.1 christos { 613 1.1 christos double val = 0; 614 1.1 christos 615 1.1 christos memcpy (&val, from, floatformat_totalsize_bytes (fmt)); 616 1.1 christos *to = val; 617 1.1 christos return; 618 1.1 christos } 619 1.1 christos else if (fmt == host_long_double_format) 620 1.1 christos { 621 1.1 christos long double val = 0; 622 1.1 christos 623 1.1 christos memcpy (&val, from, floatformat_totalsize_bytes (fmt)); 624 1.1 christos *to = val; 625 1.1 christos return; 626 1.1 christos } 627 1.1 christos 628 1.1 christos unsigned char *ufrom = (unsigned char *) from; 629 1.1 christos long exponent; 630 1.1 christos unsigned long mant; 631 1.1 christos unsigned int mant_bits, mant_off; 632 1.1 christos int mant_bits_left; 633 1.1 christos int special_exponent; /* It's a NaN, denorm or zero. */ 634 1.1 christos enum floatformat_byteorders order; 635 1.1 christos unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; 636 1.1 christos enum float_kind kind; 637 1.1 christos 638 1.1 christos gdb_assert (fmt->totalsize 639 1.1 christos <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); 640 1.1 christos 641 1.1 christos /* For non-numbers, reuse libiberty's logic to find the correct 642 1.1 christos format. We do not lose any precision in this case by passing 643 1.1 christos through a double. */ 644 1.1 christos kind = floatformat_classify (fmt, (const bfd_byte *) from); 645 1.1 christos if (kind == float_infinite || kind == float_nan) 646 1.1 christos { 647 1.1 christos double dto; 648 1.1 christos 649 1.1.1.2 christos floatformat_to_double /* ARI: floatformat_to_double */ 650 1.1.1.2 christos (fmt->split_half ? fmt->split_half : fmt, from, &dto); 651 1.1 christos *to = (T) dto; 652 1.1 christos return; 653 1.1 christos } 654 1.1 christos 655 1.1 christos order = floatformat_normalize_byteorder (fmt, ufrom, newfrom); 656 1.1 christos 657 1.1 christos if (order != fmt->byteorder) 658 1.1 christos ufrom = newfrom; 659 1.1 christos 660 1.1 christos if (fmt->split_half) 661 1.1 christos { 662 1.1 christos T dtop, dbot; 663 1.1 christos 664 1.1 christos from_target (fmt->split_half, ufrom, &dtop); 665 1.1 christos /* Preserve the sign of 0, which is the sign of the top 666 1.1 christos half. */ 667 1.1 christos if (dtop == 0.0) 668 1.1 christos { 669 1.1 christos *to = dtop; 670 1.1 christos return; 671 1.1 christos } 672 1.1 christos from_target (fmt->split_half, 673 1.1 christos ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, &dbot); 674 1.1 christos *to = dtop + dbot; 675 1.1 christos return; 676 1.1 christos } 677 1.1 christos 678 1.1 christos exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start, 679 1.1 christos fmt->exp_len); 680 1.1 christos /* Note that if exponent indicates a NaN, we can't really do anything useful 681 1.1 christos (not knowing if the host has NaN's, or how to build one). So it will 682 1.1 christos end up as an infinity or something close; that is OK. */ 683 1.1 christos 684 1.1 christos mant_bits_left = fmt->man_len; 685 1.1 christos mant_off = fmt->man_start; 686 1.1 christos T dto = 0.0; 687 1.1 christos 688 1.1 christos special_exponent = exponent == 0 || exponent == fmt->exp_nan; 689 1.1 christos 690 1.1 christos /* Don't bias NaNs. Use minimum exponent for denorms. For 691 1.1 christos simplicity, we don't check for zero as the exponent doesn't matter. 692 1.1 christos Note the cast to int; exp_bias is unsigned, so it's important to 693 1.1 christos make sure the operation is done in signed arithmetic. */ 694 1.1 christos if (!special_exponent) 695 1.1 christos exponent -= fmt->exp_bias; 696 1.1 christos else if (exponent == 0) 697 1.1 christos exponent = 1 - fmt->exp_bias; 698 1.1 christos 699 1.1 christos /* Build the result algebraically. Might go infinite, underflow, etc; 700 1.1 christos who cares. */ 701 1.1 christos 702 1.1 christos /* If this format uses a hidden bit, explicitly add it in now. Otherwise, 703 1.1 christos increment the exponent by one to account for the integer bit. */ 704 1.1 christos 705 1.1 christos if (!special_exponent) 706 1.1 christos { 707 1.1 christos if (fmt->intbit == floatformat_intbit_no) 708 1.1 christos dto = ldexp (1.0, exponent); 709 1.1 christos else 710 1.1 christos exponent++; 711 1.1 christos } 712 1.1 christos 713 1.1 christos while (mant_bits_left > 0) 714 1.1 christos { 715 1.1 christos mant_bits = std::min (mant_bits_left, 32); 716 1.1 christos 717 1.1 christos mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits); 718 1.1 christos 719 1.1 christos dto += ldexp ((T) mant, exponent - mant_bits); 720 1.1 christos exponent -= mant_bits; 721 1.1 christos mant_off += mant_bits; 722 1.1 christos mant_bits_left -= mant_bits; 723 1.1 christos } 724 1.1 christos 725 1.1 christos /* Negate it if negative. */ 726 1.1 christos if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1)) 727 1.1 christos dto = -dto; 728 1.1 christos *to = dto; 729 1.1 christos } 730 1.1 christos 731 1.1 christos template<typename T> void 732 1.1 christos host_float_ops<T>::from_target (const struct type *type, 733 1.1 christos const gdb_byte *from, T *to) const 734 1.1 christos { 735 1.1 christos from_target (floatformat_from_type (type), from, to); 736 1.1 christos } 737 1.1 christos 738 1.1 christos /* Convert host floating-point value of type T to target floating-point 739 1.1 christos value in format FMT and store at TO. */ 740 1.1 christos template<typename T> void 741 1.1 christos host_float_ops<T>::to_target (const struct floatformat *fmt, 742 1.1 christos const T *from, gdb_byte *to) const 743 1.1 christos { 744 1.1 christos gdb_assert (fmt != NULL); 745 1.1 christos 746 1.1 christos if (fmt == host_float_format) 747 1.1 christos { 748 1.1 christos float val = *from; 749 1.1 christos 750 1.1 christos memcpy (to, &val, floatformat_totalsize_bytes (fmt)); 751 1.1 christos return; 752 1.1 christos } 753 1.1 christos else if (fmt == host_double_format) 754 1.1 christos { 755 1.1 christos double val = *from; 756 1.1 christos 757 1.1 christos memcpy (to, &val, floatformat_totalsize_bytes (fmt)); 758 1.1 christos return; 759 1.1 christos } 760 1.1 christos else if (fmt == host_long_double_format) 761 1.1 christos { 762 1.1 christos long double val = *from; 763 1.1 christos 764 1.1 christos memcpy (to, &val, floatformat_totalsize_bytes (fmt)); 765 1.1 christos return; 766 1.1 christos } 767 1.1 christos 768 1.1 christos T dfrom; 769 1.1 christos int exponent; 770 1.1 christos T mant; 771 1.1 christos unsigned int mant_bits, mant_off; 772 1.1 christos int mant_bits_left; 773 1.1 christos unsigned char *uto = (unsigned char *) to; 774 1.1 christos enum floatformat_byteorders order = fmt->byteorder; 775 1.1 christos unsigned char newto[FLOATFORMAT_LARGEST_BYTES]; 776 1.1 christos 777 1.1 christos if (order != floatformat_little) 778 1.1 christos order = floatformat_big; 779 1.1 christos 780 1.1 christos if (order != fmt->byteorder) 781 1.1 christos uto = newto; 782 1.1 christos 783 1.1 christos memcpy (&dfrom, from, sizeof (dfrom)); 784 1.1 christos memset (uto, 0, floatformat_totalsize_bytes (fmt)); 785 1.1 christos 786 1.1 christos if (fmt->split_half) 787 1.1 christos { 788 1.1 christos /* Use static volatile to ensure that any excess precision is 789 1.1 christos removed via storing in memory, and so the top half really is 790 1.1 christos the result of converting to double. */ 791 1.1 christos static volatile double dtop, dbot; 792 1.1 christos T dtopnv, dbotnv; 793 1.1 christos 794 1.1 christos dtop = (double) dfrom; 795 1.1 christos /* If the rounded top half is Inf, the bottom must be 0 not NaN 796 1.1 christos or Inf. */ 797 1.1 christos if (dtop + dtop == dtop && dtop != 0.0) 798 1.1 christos dbot = 0.0; 799 1.1 christos else 800 1.1 christos dbot = (double) (dfrom - (T) dtop); 801 1.1 christos dtopnv = dtop; 802 1.1 christos dbotnv = dbot; 803 1.1 christos to_target (fmt->split_half, &dtopnv, uto); 804 1.1 christos to_target (fmt->split_half, &dbotnv, 805 1.1 christos uto + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2); 806 1.1 christos return; 807 1.1 christos } 808 1.1 christos 809 1.1 christos if (dfrom == 0) 810 1.1 christos goto finalize_byteorder; /* Result is zero */ 811 1.1 christos if (dfrom != dfrom) /* Result is NaN */ 812 1.1 christos { 813 1.1 christos /* From is NaN */ 814 1.1 christos put_field (uto, order, fmt->totalsize, fmt->exp_start, 815 1.1 christos fmt->exp_len, fmt->exp_nan); 816 1.1 christos /* Be sure it's not infinity, but NaN value is irrel. */ 817 1.1 christos put_field (uto, order, fmt->totalsize, fmt->man_start, 818 1.1 christos fmt->man_len, 1); 819 1.1 christos goto finalize_byteorder; 820 1.1 christos } 821 1.1 christos 822 1.1 christos /* If negative, set the sign bit. */ 823 1.1 christos if (dfrom < 0) 824 1.1 christos { 825 1.1 christos put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1); 826 1.1 christos dfrom = -dfrom; 827 1.1 christos } 828 1.1 christos 829 1.1 christos if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity. */ 830 1.1 christos { 831 1.1 christos /* Infinity exponent is same as NaN's. */ 832 1.1 christos put_field (uto, order, fmt->totalsize, fmt->exp_start, 833 1.1 christos fmt->exp_len, fmt->exp_nan); 834 1.1 christos /* Infinity mantissa is all zeroes. */ 835 1.1 christos put_field (uto, order, fmt->totalsize, fmt->man_start, 836 1.1 christos fmt->man_len, 0); 837 1.1 christos goto finalize_byteorder; 838 1.1 christos } 839 1.1 christos 840 1.1 christos mant = frexp (dfrom, &exponent); 841 1.1 christos 842 1.1 christos if (exponent + fmt->exp_bias <= 0) 843 1.1 christos { 844 1.1 christos /* The value is too small to be expressed in the destination 845 1.1 christos type (not enough bits in the exponent. Treat as 0. */ 846 1.1 christos put_field (uto, order, fmt->totalsize, fmt->exp_start, 847 1.1 christos fmt->exp_len, 0); 848 1.1 christos put_field (uto, order, fmt->totalsize, fmt->man_start, 849 1.1 christos fmt->man_len, 0); 850 1.1 christos goto finalize_byteorder; 851 1.1 christos } 852 1.1 christos 853 1.1 christos if (exponent + fmt->exp_bias >= (1 << fmt->exp_len)) 854 1.1 christos { 855 1.1 christos /* The value is too large to fit into the destination. 856 1.1 christos Treat as infinity. */ 857 1.1 christos put_field (uto, order, fmt->totalsize, fmt->exp_start, 858 1.1 christos fmt->exp_len, fmt->exp_nan); 859 1.1 christos put_field (uto, order, fmt->totalsize, fmt->man_start, 860 1.1 christos fmt->man_len, 0); 861 1.1 christos goto finalize_byteorder; 862 1.1 christos } 863 1.1 christos 864 1.1 christos put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len, 865 1.1 christos exponent + fmt->exp_bias - 1); 866 1.1 christos 867 1.1 christos mant_bits_left = fmt->man_len; 868 1.1 christos mant_off = fmt->man_start; 869 1.1 christos while (mant_bits_left > 0) 870 1.1 christos { 871 1.1 christos unsigned long mant_long; 872 1.1 christos 873 1.1 christos mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; 874 1.1 christos 875 1.1 christos mant *= 4294967296.0; 876 1.1 christos mant_long = ((unsigned long) mant) & 0xffffffffL; 877 1.1 christos mant -= mant_long; 878 1.1 christos 879 1.1 christos /* If the integer bit is implicit, then we need to discard it. 880 1.1.1.3 christos If we are discarding a zero, we should be (but are not) creating 881 1.1.1.3 christos a denormalized number which means adjusting the exponent 882 1.1.1.3 christos (I think). */ 883 1.1 christos if (mant_bits_left == fmt->man_len 884 1.1 christos && fmt->intbit == floatformat_intbit_no) 885 1.1 christos { 886 1.1 christos mant_long <<= 1; 887 1.1 christos mant_long &= 0xffffffffL; 888 1.1.1.3 christos /* If we are processing the top 32 mantissa bits of a doublest 889 1.1.1.3 christos so as to convert to a float value with implied integer bit, 890 1.1.1.3 christos we will only be putting 31 of those 32 bits into the 891 1.1.1.3 christos final value due to the discarding of the top bit. In the 892 1.1.1.3 christos case of a small float value where the number of mantissa 893 1.1.1.3 christos bits is less than 32, discarding the top bit does not alter 894 1.1.1.3 christos the number of bits we will be adding to the result. */ 895 1.1.1.3 christos if (mant_bits == 32) 896 1.1.1.3 christos mant_bits -= 1; 897 1.1 christos } 898 1.1 christos 899 1.1 christos if (mant_bits < 32) 900 1.1 christos { 901 1.1 christos /* The bits we want are in the most significant MANT_BITS bits of 902 1.1 christos mant_long. Move them to the least significant. */ 903 1.1 christos mant_long >>= 32 - mant_bits; 904 1.1 christos } 905 1.1 christos 906 1.1 christos put_field (uto, order, fmt->totalsize, 907 1.1 christos mant_off, mant_bits, mant_long); 908 1.1 christos mant_off += mant_bits; 909 1.1 christos mant_bits_left -= mant_bits; 910 1.1 christos } 911 1.1 christos 912 1.1 christos finalize_byteorder: 913 1.1 christos /* Do we need to byte-swap the words in the result? */ 914 1.1 christos if (order != fmt->byteorder) 915 1.1 christos floatformat_normalize_byteorder (fmt, newto, to); 916 1.1 christos } 917 1.1 christos 918 1.1 christos template<typename T> void 919 1.1 christos host_float_ops<T>::to_target (const struct type *type, 920 1.1 christos const T *from, gdb_byte *to) const 921 1.1 christos { 922 1.1 christos /* Ensure possible padding bytes in the target buffer are zeroed out. */ 923 1.1.1.3 christos memset (to, 0, type->length ()); 924 1.1 christos 925 1.1 christos to_target (floatformat_from_type (type), from, to); 926 1.1 christos } 927 1.1 christos 928 1.1 christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, 929 1.1 christos to a string, optionally using the print format FORMAT. */ 930 1.1 christos template<typename T> struct printf_length_modifier 931 1.1 christos { 932 1.1 christos static constexpr char value = 0; 933 1.1 christos }; 934 1.1 christos template<> struct printf_length_modifier<long double> 935 1.1 christos { 936 1.1 christos static constexpr char value = 'L'; 937 1.1 christos }; 938 1.1 christos template<typename T> std::string 939 1.1 christos host_float_ops<T>::to_string (const gdb_byte *addr, const struct type *type, 940 1.1 christos const char *format) const 941 1.1 christos { 942 1.1 christos /* Determine the format string to use on the host side. */ 943 1.1 christos constexpr char length = printf_length_modifier<T>::value; 944 1.1 christos const struct floatformat *fmt = floatformat_from_type (type); 945 1.1 christos std::string host_format = floatformat_printf_format (fmt, format, length); 946 1.1 christos 947 1.1 christos T host_float; 948 1.1 christos from_target (type, addr, &host_float); 949 1.1 christos 950 1.1 christos DIAGNOSTIC_PUSH 951 1.1 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL 952 1.1 christos return string_printf (host_format.c_str (), host_float); 953 1.1 christos DIAGNOSTIC_POP 954 1.1 christos } 955 1.1 christos 956 1.1 christos /* Parse string IN into a target floating-number of type TYPE and 957 1.1 christos store it as byte-stream ADDR. Return whether parsing succeeded. */ 958 1.1 christos template<typename T> struct scanf_length_modifier 959 1.1 christos { 960 1.1 christos static constexpr char value = 0; 961 1.1 christos }; 962 1.1 christos template<> struct scanf_length_modifier<double> 963 1.1 christos { 964 1.1 christos static constexpr char value = 'l'; 965 1.1 christos }; 966 1.1 christos template<> struct scanf_length_modifier<long double> 967 1.1 christos { 968 1.1 christos static constexpr char value = 'L'; 969 1.1 christos }; 970 1.1 christos template<typename T> bool 971 1.1 christos host_float_ops<T>::from_string (gdb_byte *addr, const struct type *type, 972 1.1 christos const std::string &in) const 973 1.1 christos { 974 1.1 christos T host_float; 975 1.1 christos int n, num; 976 1.1 christos 977 1.1 christos std::string scan_format = "%"; 978 1.1 christos if (scanf_length_modifier<T>::value) 979 1.1 christos scan_format += scanf_length_modifier<T>::value; 980 1.1 christos scan_format += "g%n"; 981 1.1 christos 982 1.1 christos DIAGNOSTIC_PUSH 983 1.1 christos DIAGNOSTIC_IGNORE_FORMAT_NONLITERAL 984 1.1 christos num = sscanf (in.c_str (), scan_format.c_str(), &host_float, &n); 985 1.1 christos DIAGNOSTIC_POP 986 1.1 christos 987 1.1 christos /* The sscanf man page suggests not making any assumptions on the effect 988 1.1 christos of %n on the result, so we don't. 989 1.1 christos That is why we simply test num == 0. */ 990 1.1 christos if (num == 0) 991 1.1 christos return false; 992 1.1 christos 993 1.1 christos /* We only accept the whole string. */ 994 1.1 christos if (in[n]) 995 1.1 christos return false; 996 1.1 christos 997 1.1 christos to_target (type, &host_float, addr); 998 1.1 christos return true; 999 1.1 christos } 1000 1.1 christos 1001 1.1 christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, 1002 1.1 christos to an integer value (rounding towards zero). */ 1003 1.1 christos template<typename T> LONGEST 1004 1.1 christos host_float_ops<T>::to_longest (const gdb_byte *addr, 1005 1.1 christos const struct type *type) const 1006 1.1 christos { 1007 1.1 christos T host_float; 1008 1.1 christos from_target (type, addr, &host_float); 1009 1.1.1.2 christos T min_possible_range = static_cast<T>(std::numeric_limits<LONGEST>::min()); 1010 1.1.1.2 christos T max_possible_range = -min_possible_range; 1011 1.1.1.2 christos /* host_float can be converted to an integer as long as it's in 1012 1.1.1.2 christos the range [min_possible_range, max_possible_range). If not, it is either 1013 1.1.1.2 christos too large, or too small, or is NaN; in this case return the maximum or 1014 1.1.1.2 christos minimum possible value. */ 1015 1.1.1.2 christos if (host_float < max_possible_range && host_float >= min_possible_range) 1016 1.1.1.2 christos return static_cast<LONGEST> (host_float); 1017 1.1.1.2 christos if (host_float < min_possible_range) 1018 1.1 christos return std::numeric_limits<LONGEST>::min(); 1019 1.1.1.2 christos /* This line will be executed if host_float is NaN. */ 1020 1.1.1.2 christos return std::numeric_limits<LONGEST>::max(); 1021 1.1 christos } 1022 1.1 christos 1023 1.1 christos /* Convert signed integer VAL to a target floating-number of type TYPE 1024 1.1 christos and store it as byte-stream ADDR. */ 1025 1.1 christos template<typename T> void 1026 1.1 christos host_float_ops<T>::from_longest (gdb_byte *addr, const struct type *type, 1027 1.1 christos LONGEST val) const 1028 1.1 christos { 1029 1.1 christos T host_float = (T) val; 1030 1.1 christos to_target (type, &host_float, addr); 1031 1.1 christos } 1032 1.1 christos 1033 1.1 christos /* Convert unsigned integer VAL to a target floating-number of type TYPE 1034 1.1 christos and store it as byte-stream ADDR. */ 1035 1.1 christos template<typename T> void 1036 1.1 christos host_float_ops<T>::from_ulongest (gdb_byte *addr, const struct type *type, 1037 1.1 christos ULONGEST val) const 1038 1.1 christos { 1039 1.1 christos T host_float = (T) val; 1040 1.1 christos to_target (type, &host_float, addr); 1041 1.1 christos } 1042 1.1 christos 1043 1.1 christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, 1044 1.1 christos to a floating-point value in the host "double" format. */ 1045 1.1 christos template<typename T> double 1046 1.1 christos host_float_ops<T>::to_host_double (const gdb_byte *addr, 1047 1.1 christos const struct type *type) const 1048 1.1 christos { 1049 1.1 christos T host_float; 1050 1.1 christos from_target (type, addr, &host_float); 1051 1.1 christos return (double) host_float; 1052 1.1 christos } 1053 1.1 christos 1054 1.1 christos /* Convert floating-point value VAL in the host "double" format to a target 1055 1.1 christos floating-number of type TYPE and store it as byte-stream ADDR. */ 1056 1.1 christos template<typename T> void 1057 1.1 christos host_float_ops<T>::from_host_double (gdb_byte *addr, const struct type *type, 1058 1.1 christos double val) const 1059 1.1 christos { 1060 1.1 christos T host_float = (T) val; 1061 1.1 christos to_target (type, &host_float, addr); 1062 1.1 christos } 1063 1.1 christos 1064 1.1 christos /* Convert a floating-point number of type FROM_TYPE from the target 1065 1.1 christos byte-stream FROM to a floating-point number of type TO_TYPE, and 1066 1.1 christos store it to the target byte-stream TO. */ 1067 1.1 christos template<typename T> void 1068 1.1 christos host_float_ops<T>::convert (const gdb_byte *from, 1069 1.1 christos const struct type *from_type, 1070 1.1 christos gdb_byte *to, 1071 1.1 christos const struct type *to_type) const 1072 1.1 christos { 1073 1.1 christos T host_float; 1074 1.1 christos from_target (from_type, from, &host_float); 1075 1.1 christos to_target (to_type, &host_float, to); 1076 1.1 christos } 1077 1.1 christos 1078 1.1 christos /* Perform the binary operation indicated by OPCODE, using as operands the 1079 1.1 christos target byte streams X and Y, interpreted as floating-point numbers of 1080 1.1 christos types TYPE_X and TYPE_Y, respectively. Convert the result to format 1081 1.1 christos TYPE_RES and store it into the byte-stream RES. */ 1082 1.1 christos template<typename T> void 1083 1.1 christos host_float_ops<T>::binop (enum exp_opcode op, 1084 1.1 christos const gdb_byte *x, const struct type *type_x, 1085 1.1 christos const gdb_byte *y, const struct type *type_y, 1086 1.1 christos gdb_byte *res, const struct type *type_res) const 1087 1.1 christos { 1088 1.1 christos T v1, v2, v = 0; 1089 1.1 christos 1090 1.1 christos from_target (type_x, x, &v1); 1091 1.1 christos from_target (type_y, y, &v2); 1092 1.1 christos 1093 1.1 christos switch (op) 1094 1.1 christos { 1095 1.1 christos case BINOP_ADD: 1096 1.1 christos v = v1 + v2; 1097 1.1 christos break; 1098 1.1 christos 1099 1.1 christos case BINOP_SUB: 1100 1.1 christos v = v1 - v2; 1101 1.1 christos break; 1102 1.1 christos 1103 1.1 christos case BINOP_MUL: 1104 1.1 christos v = v1 * v2; 1105 1.1 christos break; 1106 1.1 christos 1107 1.1 christos case BINOP_DIV: 1108 1.1 christos v = v1 / v2; 1109 1.1 christos break; 1110 1.1 christos 1111 1.1 christos case BINOP_EXP: 1112 1.1 christos errno = 0; 1113 1.1 christos v = pow (v1, v2); 1114 1.1 christos if (errno) 1115 1.1 christos error (_("Cannot perform exponentiation: %s"), 1116 1.1 christos safe_strerror (errno)); 1117 1.1 christos break; 1118 1.1 christos 1119 1.1 christos case BINOP_MIN: 1120 1.1 christos v = v1 < v2 ? v1 : v2; 1121 1.1 christos break; 1122 1.1 christos 1123 1.1 christos case BINOP_MAX: 1124 1.1 christos v = v1 > v2 ? v1 : v2; 1125 1.1 christos break; 1126 1.1 christos 1127 1.1 christos default: 1128 1.1 christos error (_("Integer-only operation on floating point number.")); 1129 1.1 christos break; 1130 1.1 christos } 1131 1.1 christos 1132 1.1 christos to_target (type_res, &v, res); 1133 1.1 christos } 1134 1.1 christos 1135 1.1 christos /* Compare the two target byte streams X and Y, interpreted as floating-point 1136 1.1 christos numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y 1137 1.1 christos are equal, -1 if X is less than Y, and 1 otherwise. */ 1138 1.1 christos template<typename T> int 1139 1.1 christos host_float_ops<T>::compare (const gdb_byte *x, const struct type *type_x, 1140 1.1 christos const gdb_byte *y, const struct type *type_y) const 1141 1.1 christos { 1142 1.1 christos T v1, v2; 1143 1.1 christos 1144 1.1 christos from_target (type_x, x, &v1); 1145 1.1 christos from_target (type_y, y, &v2); 1146 1.1 christos 1147 1.1 christos if (v1 == v2) 1148 1.1 christos return 0; 1149 1.1 christos if (v1 < v2) 1150 1.1 christos return -1; 1151 1.1 christos return 1; 1152 1.1 christos } 1153 1.1 christos 1154 1.1 christos 1155 1.1 christos /* Implementation of target_float_ops using the MPFR library 1156 1.1 christos mpfr_t as intermediate type. */ 1157 1.1 christos 1158 1.1 christos #define MPFR_USE_INTMAX_T 1159 1.1 christos 1160 1.1 christos #include <mpfr.h> 1161 1.1 christos 1162 1.1 christos class mpfr_float_ops : public target_float_ops 1163 1.1 christos { 1164 1.1 christos public: 1165 1.1 christos std::string to_string (const gdb_byte *addr, const struct type *type, 1166 1.1 christos const char *format) const override; 1167 1.1 christos bool from_string (gdb_byte *addr, const struct type *type, 1168 1.1 christos const std::string &string) const override; 1169 1.1 christos 1170 1.1 christos LONGEST to_longest (const gdb_byte *addr, 1171 1.1 christos const struct type *type) const override; 1172 1.1 christos void from_longest (gdb_byte *addr, const struct type *type, 1173 1.1 christos LONGEST val) const override; 1174 1.1 christos void from_ulongest (gdb_byte *addr, const struct type *type, 1175 1.1 christos ULONGEST val) const override; 1176 1.1 christos double to_host_double (const gdb_byte *addr, 1177 1.1 christos const struct type *type) const override; 1178 1.1 christos void from_host_double (gdb_byte *addr, const struct type *type, 1179 1.1 christos double val) const override; 1180 1.1 christos void convert (const gdb_byte *from, const struct type *from_type, 1181 1.1 christos gdb_byte *to, const struct type *to_type) const override; 1182 1.1 christos 1183 1.1 christos void binop (enum exp_opcode opcode, 1184 1.1 christos const gdb_byte *x, const struct type *type_x, 1185 1.1 christos const gdb_byte *y, const struct type *type_y, 1186 1.1 christos gdb_byte *res, const struct type *type_res) const override; 1187 1.1 christos int compare (const gdb_byte *x, const struct type *type_x, 1188 1.1 christos const gdb_byte *y, const struct type *type_y) const override; 1189 1.1 christos 1190 1.1 christos private: 1191 1.1.1.3 christos /* Local wrapper class to handle mpfr_t initialization and cleanup. */ 1192 1.1 christos class gdb_mpfr 1193 1.1 christos { 1194 1.1 christos public: 1195 1.1 christos mpfr_t val; 1196 1.1 christos 1197 1.1 christos gdb_mpfr (const struct type *type) 1198 1.1 christos { 1199 1.1 christos const struct floatformat *fmt = floatformat_from_type (type); 1200 1.1 christos mpfr_init2 (val, floatformat_precision (fmt)); 1201 1.1 christos } 1202 1.1 christos 1203 1.1 christos gdb_mpfr (const gdb_mpfr &source) 1204 1.1 christos { 1205 1.1 christos mpfr_init2 (val, mpfr_get_prec (source.val)); 1206 1.1 christos } 1207 1.1 christos 1208 1.1 christos ~gdb_mpfr () 1209 1.1 christos { 1210 1.1 christos mpfr_clear (val); 1211 1.1 christos } 1212 1.1 christos }; 1213 1.1 christos 1214 1.1 christos void from_target (const struct floatformat *fmt, 1215 1.1 christos const gdb_byte *from, gdb_mpfr &to) const; 1216 1.1 christos void from_target (const struct type *type, 1217 1.1 christos const gdb_byte *from, gdb_mpfr &to) const; 1218 1.1 christos 1219 1.1 christos void to_target (const struct type *type, 1220 1.1 christos const gdb_mpfr &from, gdb_byte *to) const; 1221 1.1 christos void to_target (const struct floatformat *fmt, 1222 1.1 christos const gdb_mpfr &from, gdb_byte *to) const; 1223 1.1 christos }; 1224 1.1 christos 1225 1.1 christos 1226 1.1 christos /* Convert TO/FROM target floating-point format to mpfr_t. */ 1227 1.1 christos 1228 1.1 christos void 1229 1.1 christos mpfr_float_ops::from_target (const struct floatformat *fmt, 1230 1.1 christos const gdb_byte *orig_from, gdb_mpfr &to) const 1231 1.1 christos { 1232 1.1 christos const gdb_byte *from = orig_from; 1233 1.1 christos mpfr_exp_t exponent; 1234 1.1 christos unsigned long mant; 1235 1.1 christos unsigned int mant_bits, mant_off; 1236 1.1 christos int mant_bits_left; 1237 1.1 christos int special_exponent; /* It's a NaN, denorm or zero. */ 1238 1.1 christos enum floatformat_byteorders order; 1239 1.1 christos unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; 1240 1.1 christos enum float_kind kind; 1241 1.1 christos 1242 1.1 christos gdb_assert (fmt->totalsize 1243 1.1 christos <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); 1244 1.1 christos 1245 1.1 christos /* Handle non-numbers. */ 1246 1.1 christos kind = floatformat_classify (fmt, from); 1247 1.1 christos if (kind == float_infinite) 1248 1.1 christos { 1249 1.1 christos mpfr_set_inf (to.val, floatformat_is_negative (fmt, from) ? -1 : 1); 1250 1.1 christos return; 1251 1.1 christos } 1252 1.1 christos if (kind == float_nan) 1253 1.1 christos { 1254 1.1 christos mpfr_set_nan (to.val); 1255 1.1 christos return; 1256 1.1 christos } 1257 1.1 christos 1258 1.1 christos order = floatformat_normalize_byteorder (fmt, from, newfrom); 1259 1.1 christos 1260 1.1 christos if (order != fmt->byteorder) 1261 1.1 christos from = newfrom; 1262 1.1 christos 1263 1.1 christos if (fmt->split_half) 1264 1.1 christos { 1265 1.1 christos gdb_mpfr top (to), bot (to); 1266 1.1 christos 1267 1.1 christos from_target (fmt->split_half, from, top); 1268 1.1 christos /* Preserve the sign of 0, which is the sign of the top half. */ 1269 1.1 christos if (mpfr_zero_p (top.val)) 1270 1.1 christos { 1271 1.1 christos mpfr_set (to.val, top.val, MPFR_RNDN); 1272 1.1 christos return; 1273 1.1 christos } 1274 1.1 christos from_target (fmt->split_half, 1275 1.1 christos from + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, bot); 1276 1.1 christos mpfr_add (to.val, top.val, bot.val, MPFR_RNDN); 1277 1.1 christos return; 1278 1.1 christos } 1279 1.1 christos 1280 1.1 christos exponent = get_field (from, order, fmt->totalsize, fmt->exp_start, 1281 1.1 christos fmt->exp_len); 1282 1.1 christos /* Note that if exponent indicates a NaN, we can't really do anything useful 1283 1.1 christos (not knowing if the host has NaN's, or how to build one). So it will 1284 1.1 christos end up as an infinity or something close; that is OK. */ 1285 1.1 christos 1286 1.1 christos mant_bits_left = fmt->man_len; 1287 1.1 christos mant_off = fmt->man_start; 1288 1.1 christos mpfr_set_zero (to.val, 0); 1289 1.1 christos 1290 1.1 christos special_exponent = exponent == 0 || exponent == fmt->exp_nan; 1291 1.1 christos 1292 1.1 christos /* Don't bias NaNs. Use minimum exponent for denorms. For 1293 1.1 christos simplicity, we don't check for zero as the exponent doesn't matter. 1294 1.1 christos Note the cast to int; exp_bias is unsigned, so it's important to 1295 1.1 christos make sure the operation is done in signed arithmetic. */ 1296 1.1 christos if (!special_exponent) 1297 1.1 christos exponent -= fmt->exp_bias; 1298 1.1 christos else if (exponent == 0) 1299 1.1 christos exponent = 1 - fmt->exp_bias; 1300 1.1 christos 1301 1.1 christos /* Build the result algebraically. Might go infinite, underflow, etc; 1302 1.1 christos who cares. */ 1303 1.1 christos 1304 1.1 christos /* If this format uses a hidden bit, explicitly add it in now. Otherwise, 1305 1.1 christos increment the exponent by one to account for the integer bit. */ 1306 1.1 christos 1307 1.1 christos if (!special_exponent) 1308 1.1 christos { 1309 1.1 christos if (fmt->intbit == floatformat_intbit_no) 1310 1.1 christos mpfr_set_ui_2exp (to.val, 1, exponent, MPFR_RNDN); 1311 1.1 christos else 1312 1.1 christos exponent++; 1313 1.1 christos } 1314 1.1 christos 1315 1.1 christos gdb_mpfr tmp (to); 1316 1.1 christos 1317 1.1 christos while (mant_bits_left > 0) 1318 1.1 christos { 1319 1.1 christos mant_bits = std::min (mant_bits_left, 32); 1320 1.1 christos 1321 1.1 christos mant = get_field (from, order, fmt->totalsize, mant_off, mant_bits); 1322 1.1 christos 1323 1.1 christos mpfr_set_ui (tmp.val, mant, MPFR_RNDN); 1324 1.1 christos mpfr_mul_2si (tmp.val, tmp.val, exponent - mant_bits, MPFR_RNDN); 1325 1.1 christos mpfr_add (to.val, to.val, tmp.val, MPFR_RNDN); 1326 1.1 christos exponent -= mant_bits; 1327 1.1 christos mant_off += mant_bits; 1328 1.1 christos mant_bits_left -= mant_bits; 1329 1.1 christos } 1330 1.1 christos 1331 1.1 christos /* Negate it if negative. */ 1332 1.1 christos if (get_field (from, order, fmt->totalsize, fmt->sign_start, 1)) 1333 1.1 christos mpfr_neg (to.val, to.val, MPFR_RNDN); 1334 1.1 christos } 1335 1.1 christos 1336 1.1 christos void 1337 1.1 christos mpfr_float_ops::from_target (const struct type *type, 1338 1.1 christos const gdb_byte *from, gdb_mpfr &to) const 1339 1.1 christos { 1340 1.1 christos from_target (floatformat_from_type (type), from, to); 1341 1.1 christos } 1342 1.1 christos 1343 1.1 christos void 1344 1.1 christos mpfr_float_ops::to_target (const struct floatformat *fmt, 1345 1.1 christos const gdb_mpfr &from, gdb_byte *orig_to) const 1346 1.1 christos { 1347 1.1 christos unsigned char *to = orig_to; 1348 1.1 christos mpfr_exp_t exponent; 1349 1.1 christos unsigned int mant_bits, mant_off; 1350 1.1 christos int mant_bits_left; 1351 1.1 christos enum floatformat_byteorders order = fmt->byteorder; 1352 1.1 christos unsigned char newto[FLOATFORMAT_LARGEST_BYTES]; 1353 1.1 christos 1354 1.1 christos if (order != floatformat_little) 1355 1.1 christos order = floatformat_big; 1356 1.1 christos 1357 1.1 christos if (order != fmt->byteorder) 1358 1.1 christos to = newto; 1359 1.1 christos 1360 1.1 christos memset (to, 0, floatformat_totalsize_bytes (fmt)); 1361 1.1 christos 1362 1.1 christos if (fmt->split_half) 1363 1.1 christos { 1364 1.1 christos gdb_mpfr top (from), bot (from); 1365 1.1 christos 1366 1.1 christos mpfr_set (top.val, from.val, MPFR_RNDN); 1367 1.1 christos /* If the rounded top half is Inf, the bottom must be 0 not NaN 1368 1.1 christos or Inf. */ 1369 1.1 christos if (mpfr_inf_p (top.val)) 1370 1.1 christos mpfr_set_zero (bot.val, 0); 1371 1.1 christos else 1372 1.1 christos mpfr_sub (bot.val, from.val, top.val, MPFR_RNDN); 1373 1.1 christos 1374 1.1 christos to_target (fmt->split_half, top, to); 1375 1.1 christos to_target (fmt->split_half, bot, 1376 1.1 christos to + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2); 1377 1.1 christos return; 1378 1.1 christos } 1379 1.1 christos 1380 1.1 christos gdb_mpfr tmp (from); 1381 1.1 christos 1382 1.1 christos if (mpfr_zero_p (from.val)) 1383 1.1 christos goto finalize_byteorder; /* Result is zero */ 1384 1.1 christos 1385 1.1 christos mpfr_set (tmp.val, from.val, MPFR_RNDN); 1386 1.1 christos 1387 1.1 christos if (mpfr_nan_p (tmp.val)) /* Result is NaN */ 1388 1.1 christos { 1389 1.1 christos /* From is NaN */ 1390 1.1 christos put_field (to, order, fmt->totalsize, fmt->exp_start, 1391 1.1 christos fmt->exp_len, fmt->exp_nan); 1392 1.1 christos /* Be sure it's not infinity, but NaN value is irrel. */ 1393 1.1 christos put_field (to, order, fmt->totalsize, fmt->man_start, 1394 1.1 christos fmt->man_len, 1); 1395 1.1 christos goto finalize_byteorder; 1396 1.1 christos } 1397 1.1 christos 1398 1.1 christos /* If negative, set the sign bit. */ 1399 1.1 christos if (mpfr_sgn (tmp.val) < 0) 1400 1.1 christos { 1401 1.1 christos put_field (to, order, fmt->totalsize, fmt->sign_start, 1, 1); 1402 1.1 christos mpfr_neg (tmp.val, tmp.val, MPFR_RNDN); 1403 1.1 christos } 1404 1.1 christos 1405 1.1 christos if (mpfr_inf_p (tmp.val)) /* Result is Infinity. */ 1406 1.1 christos { 1407 1.1 christos /* Infinity exponent is same as NaN's. */ 1408 1.1 christos put_field (to, order, fmt->totalsize, fmt->exp_start, 1409 1.1 christos fmt->exp_len, fmt->exp_nan); 1410 1.1 christos /* Infinity mantissa is all zeroes. */ 1411 1.1 christos put_field (to, order, fmt->totalsize, fmt->man_start, 1412 1.1 christos fmt->man_len, 0); 1413 1.1 christos goto finalize_byteorder; 1414 1.1 christos } 1415 1.1 christos 1416 1.1 christos mpfr_frexp (&exponent, tmp.val, tmp.val, MPFR_RNDN); 1417 1.1 christos 1418 1.1 christos if (exponent + fmt->exp_bias <= 0) 1419 1.1 christos { 1420 1.1 christos /* The value is too small to be expressed in the destination 1421 1.1 christos type (not enough bits in the exponent. Treat as 0. */ 1422 1.1 christos put_field (to, order, fmt->totalsize, fmt->exp_start, 1423 1.1 christos fmt->exp_len, 0); 1424 1.1 christos put_field (to, order, fmt->totalsize, fmt->man_start, 1425 1.1 christos fmt->man_len, 0); 1426 1.1 christos goto finalize_byteorder; 1427 1.1 christos } 1428 1.1 christos 1429 1.1 christos if (exponent + fmt->exp_bias >= (1 << fmt->exp_len)) 1430 1.1 christos { 1431 1.1 christos /* The value is too large to fit into the destination. 1432 1.1 christos Treat as infinity. */ 1433 1.1 christos put_field (to, order, fmt->totalsize, fmt->exp_start, 1434 1.1 christos fmt->exp_len, fmt->exp_nan); 1435 1.1 christos put_field (to, order, fmt->totalsize, fmt->man_start, 1436 1.1 christos fmt->man_len, 0); 1437 1.1 christos goto finalize_byteorder; 1438 1.1 christos } 1439 1.1 christos 1440 1.1 christos put_field (to, order, fmt->totalsize, fmt->exp_start, fmt->exp_len, 1441 1.1 christos exponent + fmt->exp_bias - 1); 1442 1.1 christos 1443 1.1 christos mant_bits_left = fmt->man_len; 1444 1.1 christos mant_off = fmt->man_start; 1445 1.1 christos while (mant_bits_left > 0) 1446 1.1 christos { 1447 1.1 christos unsigned long mant_long; 1448 1.1 christos 1449 1.1 christos mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; 1450 1.1 christos 1451 1.1 christos mpfr_mul_2ui (tmp.val, tmp.val, 32, MPFR_RNDN); 1452 1.1 christos mant_long = mpfr_get_ui (tmp.val, MPFR_RNDZ) & 0xffffffffL; 1453 1.1 christos mpfr_sub_ui (tmp.val, tmp.val, mant_long, MPFR_RNDZ); 1454 1.1 christos 1455 1.1 christos /* If the integer bit is implicit, then we need to discard it. 1456 1.1.1.3 christos If we are discarding a zero, we should be (but are not) creating 1457 1.1.1.3 christos a denormalized number which means adjusting the exponent 1458 1.1.1.3 christos (I think). */ 1459 1.1 christos if (mant_bits_left == fmt->man_len 1460 1.1 christos && fmt->intbit == floatformat_intbit_no) 1461 1.1 christos { 1462 1.1 christos mant_long <<= 1; 1463 1.1 christos mant_long &= 0xffffffffL; 1464 1.1.1.3 christos /* If we are processing the top 32 mantissa bits of a doublest 1465 1.1.1.3 christos so as to convert to a float value with implied integer bit, 1466 1.1.1.3 christos we will only be putting 31 of those 32 bits into the 1467 1.1.1.3 christos final value due to the discarding of the top bit. In the 1468 1.1.1.3 christos case of a small float value where the number of mantissa 1469 1.1.1.3 christos bits is less than 32, discarding the top bit does not alter 1470 1.1.1.3 christos the number of bits we will be adding to the result. */ 1471 1.1.1.3 christos if (mant_bits == 32) 1472 1.1.1.3 christos mant_bits -= 1; 1473 1.1 christos } 1474 1.1 christos 1475 1.1 christos if (mant_bits < 32) 1476 1.1 christos { 1477 1.1 christos /* The bits we want are in the most significant MANT_BITS bits of 1478 1.1 christos mant_long. Move them to the least significant. */ 1479 1.1 christos mant_long >>= 32 - mant_bits; 1480 1.1 christos } 1481 1.1 christos 1482 1.1 christos put_field (to, order, fmt->totalsize, 1483 1.1 christos mant_off, mant_bits, mant_long); 1484 1.1 christos mant_off += mant_bits; 1485 1.1 christos mant_bits_left -= mant_bits; 1486 1.1 christos } 1487 1.1 christos 1488 1.1 christos finalize_byteorder: 1489 1.1 christos /* Do we need to byte-swap the words in the result? */ 1490 1.1 christos if (order != fmt->byteorder) 1491 1.1 christos floatformat_normalize_byteorder (fmt, newto, orig_to); 1492 1.1 christos } 1493 1.1 christos 1494 1.1 christos void 1495 1.1 christos mpfr_float_ops::to_target (const struct type *type, 1496 1.1 christos const gdb_mpfr &from, gdb_byte *to) const 1497 1.1 christos { 1498 1.1 christos /* Ensure possible padding bytes in the target buffer are zeroed out. */ 1499 1.1.1.3 christos memset (to, 0, type->length ()); 1500 1.1 christos 1501 1.1 christos to_target (floatformat_from_type (type), from, to); 1502 1.1 christos } 1503 1.1 christos 1504 1.1 christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, 1505 1.1 christos to a string, optionally using the print format FORMAT. */ 1506 1.1 christos std::string 1507 1.1 christos mpfr_float_ops::to_string (const gdb_byte *addr, 1508 1.1 christos const struct type *type, 1509 1.1 christos const char *format) const 1510 1.1 christos { 1511 1.1 christos const struct floatformat *fmt = floatformat_from_type (type); 1512 1.1 christos 1513 1.1 christos /* Unless we need to adhere to a specific format, provide special 1514 1.1 christos output for certain cases. */ 1515 1.1 christos if (format == nullptr) 1516 1.1 christos { 1517 1.1 christos /* Detect invalid representations. */ 1518 1.1 christos if (!floatformat_is_valid (fmt, addr)) 1519 1.1 christos return "<invalid float value>"; 1520 1.1 christos 1521 1.1 christos /* Handle NaN and Inf. */ 1522 1.1 christos enum float_kind kind = floatformat_classify (fmt, addr); 1523 1.1 christos if (kind == float_nan) 1524 1.1 christos { 1525 1.1 christos const char *sign = floatformat_is_negative (fmt, addr)? "-" : ""; 1526 1.1 christos const char *mantissa = floatformat_mantissa (fmt, addr); 1527 1.1 christos return string_printf ("%snan(0x%s)", sign, mantissa); 1528 1.1 christos } 1529 1.1 christos else if (kind == float_infinite) 1530 1.1 christos { 1531 1.1 christos const char *sign = floatformat_is_negative (fmt, addr)? "-" : ""; 1532 1.1 christos return string_printf ("%sinf", sign); 1533 1.1 christos } 1534 1.1 christos } 1535 1.1 christos 1536 1.1 christos /* Determine the format string to use on the host side. */ 1537 1.1 christos std::string host_format = floatformat_printf_format (fmt, format, 'R'); 1538 1.1 christos 1539 1.1 christos gdb_mpfr tmp (type); 1540 1.1 christos from_target (type, addr, tmp); 1541 1.1 christos 1542 1.1 christos int size = mpfr_snprintf (NULL, 0, host_format.c_str (), tmp.val); 1543 1.1 christos std::string str (size, '\0'); 1544 1.1 christos mpfr_sprintf (&str[0], host_format.c_str (), tmp.val); 1545 1.1 christos 1546 1.1 christos return str; 1547 1.1 christos } 1548 1.1 christos 1549 1.1 christos /* Parse string STRING into a target floating-number of type TYPE and 1550 1.1 christos store it as byte-stream ADDR. Return whether parsing succeeded. */ 1551 1.1 christos bool 1552 1.1 christos mpfr_float_ops::from_string (gdb_byte *addr, 1553 1.1 christos const struct type *type, 1554 1.1 christos const std::string &in) const 1555 1.1 christos { 1556 1.1 christos gdb_mpfr tmp (type); 1557 1.1 christos 1558 1.1 christos char *endptr; 1559 1.1 christos mpfr_strtofr (tmp.val, in.c_str (), &endptr, 0, MPFR_RNDN); 1560 1.1 christos 1561 1.1 christos /* We only accept the whole string. */ 1562 1.1 christos if (*endptr) 1563 1.1 christos return false; 1564 1.1 christos 1565 1.1 christos to_target (type, tmp, addr); 1566 1.1 christos return true; 1567 1.1 christos } 1568 1.1 christos 1569 1.1 christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, 1570 1.1 christos to an integer value (rounding towards zero). */ 1571 1.1 christos LONGEST 1572 1.1 christos mpfr_float_ops::to_longest (const gdb_byte *addr, 1573 1.1 christos const struct type *type) const 1574 1.1 christos { 1575 1.1 christos gdb_mpfr tmp (type); 1576 1.1 christos from_target (type, addr, tmp); 1577 1.1 christos return mpfr_get_sj (tmp.val, MPFR_RNDZ); 1578 1.1 christos } 1579 1.1 christos 1580 1.1 christos /* Convert signed integer VAL to a target floating-number of type TYPE 1581 1.1 christos and store it as byte-stream ADDR. */ 1582 1.1 christos void 1583 1.1 christos mpfr_float_ops::from_longest (gdb_byte *addr, 1584 1.1 christos const struct type *type, 1585 1.1 christos LONGEST val) const 1586 1.1 christos { 1587 1.1 christos gdb_mpfr tmp (type); 1588 1.1 christos mpfr_set_sj (tmp.val, val, MPFR_RNDN); 1589 1.1 christos to_target (type, tmp, addr); 1590 1.1 christos } 1591 1.1 christos 1592 1.1 christos /* Convert unsigned integer VAL to a target floating-number of type TYPE 1593 1.1 christos and store it as byte-stream ADDR. */ 1594 1.1 christos void 1595 1.1 christos mpfr_float_ops::from_ulongest (gdb_byte *addr, 1596 1.1 christos const struct type *type, 1597 1.1 christos ULONGEST val) const 1598 1.1 christos { 1599 1.1 christos gdb_mpfr tmp (type); 1600 1.1 christos mpfr_set_uj (tmp.val, val, MPFR_RNDN); 1601 1.1 christos to_target (type, tmp, addr); 1602 1.1 christos } 1603 1.1 christos 1604 1.1 christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, 1605 1.1 christos to a floating-point value in the host "double" format. */ 1606 1.1 christos double 1607 1.1 christos mpfr_float_ops::to_host_double (const gdb_byte *addr, 1608 1.1 christos const struct type *type) const 1609 1.1 christos { 1610 1.1 christos gdb_mpfr tmp (type); 1611 1.1 christos from_target (type, addr, tmp); 1612 1.1 christos return mpfr_get_d (tmp.val, MPFR_RNDN); 1613 1.1 christos } 1614 1.1 christos 1615 1.1 christos /* Convert floating-point value VAL in the host "double" format to a target 1616 1.1 christos floating-number of type TYPE and store it as byte-stream ADDR. */ 1617 1.1 christos void 1618 1.1 christos mpfr_float_ops::from_host_double (gdb_byte *addr, 1619 1.1 christos const struct type *type, 1620 1.1 christos double val) const 1621 1.1 christos { 1622 1.1 christos gdb_mpfr tmp (type); 1623 1.1 christos mpfr_set_d (tmp.val, val, MPFR_RNDN); 1624 1.1 christos to_target (type, tmp, addr); 1625 1.1 christos } 1626 1.1 christos 1627 1.1 christos /* Convert a floating-point number of type FROM_TYPE from the target 1628 1.1 christos byte-stream FROM to a floating-point number of type TO_TYPE, and 1629 1.1 christos store it to the target byte-stream TO. */ 1630 1.1 christos void 1631 1.1 christos mpfr_float_ops::convert (const gdb_byte *from, 1632 1.1 christos const struct type *from_type, 1633 1.1 christos gdb_byte *to, 1634 1.1 christos const struct type *to_type) const 1635 1.1 christos { 1636 1.1 christos gdb_mpfr from_tmp (from_type), to_tmp (to_type); 1637 1.1 christos from_target (from_type, from, from_tmp); 1638 1.1 christos mpfr_set (to_tmp.val, from_tmp.val, MPFR_RNDN); 1639 1.1 christos to_target (to_type, to_tmp, to); 1640 1.1 christos } 1641 1.1 christos 1642 1.1 christos /* Perform the binary operation indicated by OPCODE, using as operands the 1643 1.1 christos target byte streams X and Y, interpreted as floating-point numbers of 1644 1.1 christos types TYPE_X and TYPE_Y, respectively. Convert the result to type 1645 1.1 christos TYPE_RES and store it into the byte-stream RES. */ 1646 1.1 christos void 1647 1.1 christos mpfr_float_ops::binop (enum exp_opcode op, 1648 1.1 christos const gdb_byte *x, const struct type *type_x, 1649 1.1 christos const gdb_byte *y, const struct type *type_y, 1650 1.1 christos gdb_byte *res, const struct type *type_res) const 1651 1.1 christos { 1652 1.1 christos gdb_mpfr x_tmp (type_x), y_tmp (type_y), tmp (type_res); 1653 1.1 christos 1654 1.1 christos from_target (type_x, x, x_tmp); 1655 1.1 christos from_target (type_y, y, y_tmp); 1656 1.1 christos 1657 1.1 christos switch (op) 1658 1.1 christos { 1659 1.1 christos case BINOP_ADD: 1660 1.1 christos mpfr_add (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); 1661 1.1 christos break; 1662 1.1 christos 1663 1.1 christos case BINOP_SUB: 1664 1.1 christos mpfr_sub (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); 1665 1.1 christos break; 1666 1.1 christos 1667 1.1 christos case BINOP_MUL: 1668 1.1 christos mpfr_mul (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); 1669 1.1 christos break; 1670 1.1 christos 1671 1.1 christos case BINOP_DIV: 1672 1.1 christos mpfr_div (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); 1673 1.1 christos break; 1674 1.1 christos 1675 1.1 christos case BINOP_EXP: 1676 1.1 christos mpfr_pow (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); 1677 1.1 christos break; 1678 1.1 christos 1679 1.1 christos case BINOP_MIN: 1680 1.1 christos mpfr_min (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); 1681 1.1 christos break; 1682 1.1 christos 1683 1.1 christos case BINOP_MAX: 1684 1.1 christos mpfr_max (tmp.val, x_tmp.val, y_tmp.val, MPFR_RNDN); 1685 1.1 christos break; 1686 1.1 christos 1687 1.1 christos default: 1688 1.1 christos error (_("Integer-only operation on floating point number.")); 1689 1.1 christos break; 1690 1.1 christos } 1691 1.1 christos 1692 1.1 christos to_target (type_res, tmp, res); 1693 1.1 christos } 1694 1.1 christos 1695 1.1 christos /* Compare the two target byte streams X and Y, interpreted as floating-point 1696 1.1 christos numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y 1697 1.1 christos are equal, -1 if X is less than Y, and 1 otherwise. */ 1698 1.1 christos int 1699 1.1 christos mpfr_float_ops::compare (const gdb_byte *x, const struct type *type_x, 1700 1.1 christos const gdb_byte *y, const struct type *type_y) const 1701 1.1 christos { 1702 1.1 christos gdb_mpfr x_tmp (type_x), y_tmp (type_y); 1703 1.1 christos 1704 1.1 christos from_target (type_x, x, x_tmp); 1705 1.1 christos from_target (type_y, y, y_tmp); 1706 1.1 christos 1707 1.1 christos if (mpfr_equal_p (x_tmp.val, y_tmp.val)) 1708 1.1 christos return 0; 1709 1.1 christos else if (mpfr_less_p (x_tmp.val, y_tmp.val)) 1710 1.1 christos return -1; 1711 1.1 christos else 1712 1.1 christos return 1; 1713 1.1 christos } 1714 1.1 christos 1715 1.1 christos 1716 1.1 christos /* Helper routines operating on decimal floating-point data. */ 1717 1.1 christos 1718 1.1 christos /* Decimal floating point is one of the extension to IEEE 754, which is 1719 1.1 christos described in http://grouper.ieee.org/groups/754/revision.html and 1720 1.1 christos http://www2.hursley.ibm.com/decimal/. It completes binary floating 1721 1.1 christos point by representing floating point more exactly. */ 1722 1.1 christos 1723 1.1 christos /* The order of the following headers is important for making sure 1724 1.1 christos decNumber structure is large enough to hold decimal128 digits. */ 1725 1.1 christos 1726 1.1 christos #include "dpd/decimal128.h" 1727 1.1 christos #include "dpd/decimal64.h" 1728 1.1 christos #include "dpd/decimal32.h" 1729 1.1 christos 1730 1.1 christos /* When using decimal128, this is the maximum string length + 1 1731 1.1 christos (value comes from libdecnumber's DECIMAL128_String constant). */ 1732 1.1 christos #define MAX_DECIMAL_STRING 43 1733 1.1 christos 1734 1.1 christos /* In GDB, we are using an array of gdb_byte to represent decimal values. 1735 1.1 christos They are stored in host byte order. This routine does the conversion if 1736 1.1 christos the target byte order is different. */ 1737 1.1 christos static void 1738 1.1 christos match_endianness (const gdb_byte *from, const struct type *type, gdb_byte *to) 1739 1.1 christos { 1740 1.1.1.2 christos gdb_assert (type->code () == TYPE_CODE_DECFLOAT); 1741 1.1 christos 1742 1.1.1.3 christos int len = type->length (); 1743 1.1 christos int i; 1744 1.1 christos 1745 1.1 christos #if WORDS_BIGENDIAN 1746 1.1 christos #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE 1747 1.1 christos #else 1748 1.1 christos #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG 1749 1.1 christos #endif 1750 1.1 christos 1751 1.1.1.2 christos if (type_byte_order (type) == OPPOSITE_BYTE_ORDER) 1752 1.1 christos for (i = 0; i < len; i++) 1753 1.1 christos to[i] = from[len - i - 1]; 1754 1.1 christos else 1755 1.1 christos for (i = 0; i < len; i++) 1756 1.1 christos to[i] = from[i]; 1757 1.1 christos 1758 1.1 christos return; 1759 1.1 christos } 1760 1.1 christos 1761 1.1 christos /* Helper function to get the appropriate libdecnumber context for each size 1762 1.1 christos of decimal float. */ 1763 1.1 christos static void 1764 1.1 christos set_decnumber_context (decContext *ctx, const struct type *type) 1765 1.1 christos { 1766 1.1.1.2 christos gdb_assert (type->code () == TYPE_CODE_DECFLOAT); 1767 1.1 christos 1768 1.1.1.3 christos switch (type->length ()) 1769 1.1 christos { 1770 1.1 christos case 4: 1771 1.1 christos decContextDefault (ctx, DEC_INIT_DECIMAL32); 1772 1.1 christos break; 1773 1.1 christos case 8: 1774 1.1 christos decContextDefault (ctx, DEC_INIT_DECIMAL64); 1775 1.1 christos break; 1776 1.1 christos case 16: 1777 1.1 christos decContextDefault (ctx, DEC_INIT_DECIMAL128); 1778 1.1 christos break; 1779 1.1 christos } 1780 1.1 christos 1781 1.1 christos ctx->traps = 0; 1782 1.1 christos } 1783 1.1 christos 1784 1.1 christos /* Check for errors signaled in the decimal context structure. */ 1785 1.1 christos static void 1786 1.1 christos decimal_check_errors (decContext *ctx) 1787 1.1 christos { 1788 1.1 christos /* An error here could be a division by zero, an overflow, an underflow or 1789 1.1 christos an invalid operation (from the DEC_Errors constant in decContext.h). 1790 1.1 christos Since GDB doesn't complain about division by zero, overflow or underflow 1791 1.1 christos errors for binary floating, we won't complain about them for decimal 1792 1.1 christos floating either. */ 1793 1.1 christos if (ctx->status & DEC_IEEE_854_Invalid_operation) 1794 1.1 christos { 1795 1.1 christos /* Leave only the error bits in the status flags. */ 1796 1.1 christos ctx->status &= DEC_IEEE_854_Invalid_operation; 1797 1.1 christos error (_("Cannot perform operation: %s"), 1798 1.1 christos decContextStatusToString (ctx)); 1799 1.1 christos } 1800 1.1 christos } 1801 1.1 christos 1802 1.1 christos /* Helper function to convert from libdecnumber's appropriate representation 1803 1.1 christos for computation to each size of decimal float. */ 1804 1.1 christos static void 1805 1.1 christos decimal_from_number (const decNumber *from, 1806 1.1.1.3 christos gdb_byte *to, const struct type *type) 1807 1.1 christos { 1808 1.1 christos gdb_byte dec[16]; 1809 1.1 christos 1810 1.1 christos decContext set; 1811 1.1 christos 1812 1.1 christos set_decnumber_context (&set, type); 1813 1.1 christos 1814 1.1.1.3 christos switch (type->length ()) 1815 1.1 christos { 1816 1.1 christos case 4: 1817 1.1 christos decimal32FromNumber ((decimal32 *) dec, from, &set); 1818 1.1 christos break; 1819 1.1 christos case 8: 1820 1.1 christos decimal64FromNumber ((decimal64 *) dec, from, &set); 1821 1.1 christos break; 1822 1.1 christos case 16: 1823 1.1 christos decimal128FromNumber ((decimal128 *) dec, from, &set); 1824 1.1 christos break; 1825 1.1 christos default: 1826 1.1 christos error (_("Unknown decimal floating point type.")); 1827 1.1 christos break; 1828 1.1 christos } 1829 1.1 christos 1830 1.1 christos match_endianness (dec, type, to); 1831 1.1 christos } 1832 1.1 christos 1833 1.1 christos /* Helper function to convert each size of decimal float to libdecnumber's 1834 1.1 christos appropriate representation for computation. */ 1835 1.1 christos static void 1836 1.1 christos decimal_to_number (const gdb_byte *addr, const struct type *type, 1837 1.1.1.3 christos decNumber *to) 1838 1.1 christos { 1839 1.1 christos gdb_byte dec[16]; 1840 1.1 christos match_endianness (addr, type, dec); 1841 1.1 christos 1842 1.1.1.3 christos switch (type->length ()) 1843 1.1 christos { 1844 1.1 christos case 4: 1845 1.1 christos decimal32ToNumber ((decimal32 *) dec, to); 1846 1.1 christos break; 1847 1.1 christos case 8: 1848 1.1 christos decimal64ToNumber ((decimal64 *) dec, to); 1849 1.1 christos break; 1850 1.1 christos case 16: 1851 1.1 christos decimal128ToNumber ((decimal128 *) dec, to); 1852 1.1 christos break; 1853 1.1 christos default: 1854 1.1 christos error (_("Unknown decimal floating point type.")); 1855 1.1 christos break; 1856 1.1 christos } 1857 1.1 christos } 1858 1.1 christos 1859 1.1 christos /* Returns true if ADDR (which is of type TYPE) is the number zero. */ 1860 1.1 christos static bool 1861 1.1 christos decimal_is_zero (const gdb_byte *addr, const struct type *type) 1862 1.1 christos { 1863 1.1 christos decNumber number; 1864 1.1 christos 1865 1.1 christos decimal_to_number (addr, type, &number); 1866 1.1 christos 1867 1.1 christos return decNumberIsZero (&number); 1868 1.1 christos } 1869 1.1 christos 1870 1.1 christos 1871 1.1 christos /* Implementation of target_float_ops using the libdecnumber decNumber type 1872 1.1 christos as intermediate format. */ 1873 1.1 christos 1874 1.1 christos class decimal_float_ops : public target_float_ops 1875 1.1 christos { 1876 1.1 christos public: 1877 1.1 christos std::string to_string (const gdb_byte *addr, const struct type *type, 1878 1.1 christos const char *format) const override; 1879 1.1 christos bool from_string (gdb_byte *addr, const struct type *type, 1880 1.1 christos const std::string &string) const override; 1881 1.1 christos 1882 1.1 christos LONGEST to_longest (const gdb_byte *addr, 1883 1.1 christos const struct type *type) const override; 1884 1.1 christos void from_longest (gdb_byte *addr, const struct type *type, 1885 1.1 christos LONGEST val) const override; 1886 1.1 christos void from_ulongest (gdb_byte *addr, const struct type *type, 1887 1.1 christos ULONGEST val) const override; 1888 1.1 christos double to_host_double (const gdb_byte *addr, 1889 1.1 christos const struct type *type) const override 1890 1.1 christos { 1891 1.1 christos /* We don't support conversions between target decimal floating-point 1892 1.1 christos types and the host double type. */ 1893 1.1 christos gdb_assert_not_reached ("invalid operation on decimal float"); 1894 1.1 christos } 1895 1.1 christos void from_host_double (gdb_byte *addr, const struct type *type, 1896 1.1 christos double val) const override 1897 1.1 christos { 1898 1.1 christos /* We don't support conversions between target decimal floating-point 1899 1.1 christos types and the host double type. */ 1900 1.1 christos gdb_assert_not_reached ("invalid operation on decimal float"); 1901 1.1 christos } 1902 1.1 christos void convert (const gdb_byte *from, const struct type *from_type, 1903 1.1 christos gdb_byte *to, const struct type *to_type) const override; 1904 1.1 christos 1905 1.1 christos void binop (enum exp_opcode opcode, 1906 1.1 christos const gdb_byte *x, const struct type *type_x, 1907 1.1 christos const gdb_byte *y, const struct type *type_y, 1908 1.1 christos gdb_byte *res, const struct type *type_res) const override; 1909 1.1 christos int compare (const gdb_byte *x, const struct type *type_x, 1910 1.1 christos const gdb_byte *y, const struct type *type_y) const override; 1911 1.1 christos }; 1912 1.1 christos 1913 1.1 christos /* Convert decimal type to its string representation. LEN is the length 1914 1.1 christos of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and 1915 1.1 christos 16 bytes for decimal128. */ 1916 1.1 christos std::string 1917 1.1 christos decimal_float_ops::to_string (const gdb_byte *addr, const struct type *type, 1918 1.1 christos const char *format = nullptr) const 1919 1.1 christos { 1920 1.1 christos gdb_byte dec[16]; 1921 1.1 christos 1922 1.1 christos match_endianness (addr, type, dec); 1923 1.1 christos 1924 1.1 christos if (format != nullptr) 1925 1.1 christos { 1926 1.1 christos /* We don't handle format strings (yet). If the host printf supports 1927 1.1 christos decimal floating point types, just use this. Otherwise, fall back 1928 1.1 christos to printing the number while ignoring the format string. */ 1929 1.1 christos #if defined (PRINTF_HAS_DECFLOAT) 1930 1.1 christos /* FIXME: This makes unwarranted assumptions about the host ABI! */ 1931 1.1 christos return string_printf (format, dec); 1932 1.1 christos #endif 1933 1.1 christos } 1934 1.1 christos 1935 1.1 christos std::string result; 1936 1.1 christos result.resize (MAX_DECIMAL_STRING); 1937 1.1 christos 1938 1.1.1.3 christos switch (type->length ()) 1939 1.1 christos { 1940 1.1 christos case 4: 1941 1.1 christos decimal32ToString ((decimal32 *) dec, &result[0]); 1942 1.1 christos break; 1943 1.1 christos case 8: 1944 1.1 christos decimal64ToString ((decimal64 *) dec, &result[0]); 1945 1.1 christos break; 1946 1.1 christos case 16: 1947 1.1 christos decimal128ToString ((decimal128 *) dec, &result[0]); 1948 1.1 christos break; 1949 1.1 christos default: 1950 1.1 christos error (_("Unknown decimal floating point type.")); 1951 1.1 christos break; 1952 1.1 christos } 1953 1.1 christos 1954 1.1 christos return result; 1955 1.1 christos } 1956 1.1 christos 1957 1.1 christos /* Convert the string form of a decimal value to its decimal representation. 1958 1.1 christos LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for 1959 1.1 christos decimal64 and 16 bytes for decimal128. */ 1960 1.1 christos bool 1961 1.1 christos decimal_float_ops::from_string (gdb_byte *addr, const struct type *type, 1962 1.1 christos const std::string &string) const 1963 1.1 christos { 1964 1.1 christos decContext set; 1965 1.1 christos gdb_byte dec[16]; 1966 1.1 christos 1967 1.1 christos set_decnumber_context (&set, type); 1968 1.1 christos 1969 1.1.1.3 christos switch (type->length ()) 1970 1.1 christos { 1971 1.1 christos case 4: 1972 1.1 christos decimal32FromString ((decimal32 *) dec, string.c_str (), &set); 1973 1.1 christos break; 1974 1.1 christos case 8: 1975 1.1 christos decimal64FromString ((decimal64 *) dec, string.c_str (), &set); 1976 1.1 christos break; 1977 1.1 christos case 16: 1978 1.1 christos decimal128FromString ((decimal128 *) dec, string.c_str (), &set); 1979 1.1 christos break; 1980 1.1 christos default: 1981 1.1 christos error (_("Unknown decimal floating point type.")); 1982 1.1 christos break; 1983 1.1 christos } 1984 1.1 christos 1985 1.1 christos match_endianness (dec, type, addr); 1986 1.1 christos 1987 1.1 christos /* Check for errors in the DFP operation. */ 1988 1.1 christos decimal_check_errors (&set); 1989 1.1 christos 1990 1.1 christos return true; 1991 1.1 christos } 1992 1.1 christos 1993 1.1 christos /* Converts a LONGEST to a decimal float of specified LEN bytes. */ 1994 1.1 christos void 1995 1.1 christos decimal_float_ops::from_longest (gdb_byte *addr, const struct type *type, 1996 1.1 christos LONGEST from) const 1997 1.1 christos { 1998 1.1 christos decNumber number; 1999 1.1 christos 2000 1.1 christos if ((int32_t) from != from) 2001 1.1 christos /* libdecnumber can convert only 32-bit integers. */ 2002 1.1 christos error (_("Conversion of large integer to a " 2003 1.1 christos "decimal floating type is not supported.")); 2004 1.1 christos 2005 1.1 christos decNumberFromInt32 (&number, (int32_t) from); 2006 1.1 christos 2007 1.1 christos decimal_from_number (&number, addr, type); 2008 1.1 christos } 2009 1.1 christos 2010 1.1 christos /* Converts a ULONGEST to a decimal float of specified LEN bytes. */ 2011 1.1 christos void 2012 1.1 christos decimal_float_ops::from_ulongest (gdb_byte *addr, const struct type *type, 2013 1.1 christos ULONGEST from) const 2014 1.1 christos { 2015 1.1 christos decNumber number; 2016 1.1 christos 2017 1.1 christos if ((uint32_t) from != from) 2018 1.1 christos /* libdecnumber can convert only 32-bit integers. */ 2019 1.1 christos error (_("Conversion of large integer to a " 2020 1.1 christos "decimal floating type is not supported.")); 2021 1.1 christos 2022 1.1 christos decNumberFromUInt32 (&number, (uint32_t) from); 2023 1.1 christos 2024 1.1 christos decimal_from_number (&number, addr, type); 2025 1.1 christos } 2026 1.1 christos 2027 1.1 christos /* Converts a decimal float of LEN bytes to a LONGEST. */ 2028 1.1 christos LONGEST 2029 1.1 christos decimal_float_ops::to_longest (const gdb_byte *addr, 2030 1.1.1.3 christos const struct type *type) const 2031 1.1 christos { 2032 1.1 christos /* libdecnumber has a function to convert from decimal to integer, but 2033 1.1 christos it doesn't work when the decimal number has a fractional part. */ 2034 1.1 christos std::string str = to_string (addr, type); 2035 1.1 christos return strtoll (str.c_str (), NULL, 10); 2036 1.1 christos } 2037 1.1 christos 2038 1.1 christos /* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y 2039 1.1 christos and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in 2040 1.1 christos RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */ 2041 1.1 christos void 2042 1.1 christos decimal_float_ops::binop (enum exp_opcode op, 2043 1.1 christos const gdb_byte *x, const struct type *type_x, 2044 1.1 christos const gdb_byte *y, const struct type *type_y, 2045 1.1 christos gdb_byte *res, const struct type *type_res) const 2046 1.1 christos { 2047 1.1 christos decContext set; 2048 1.1 christos decNumber number1, number2, number3; 2049 1.1 christos 2050 1.1 christos decimal_to_number (x, type_x, &number1); 2051 1.1 christos decimal_to_number (y, type_y, &number2); 2052 1.1 christos 2053 1.1 christos set_decnumber_context (&set, type_res); 2054 1.1 christos 2055 1.1 christos switch (op) 2056 1.1 christos { 2057 1.1 christos case BINOP_ADD: 2058 1.1 christos decNumberAdd (&number3, &number1, &number2, &set); 2059 1.1 christos break; 2060 1.1 christos case BINOP_SUB: 2061 1.1 christos decNumberSubtract (&number3, &number1, &number2, &set); 2062 1.1 christos break; 2063 1.1 christos case BINOP_MUL: 2064 1.1 christos decNumberMultiply (&number3, &number1, &number2, &set); 2065 1.1 christos break; 2066 1.1 christos case BINOP_DIV: 2067 1.1 christos decNumberDivide (&number3, &number1, &number2, &set); 2068 1.1 christos break; 2069 1.1 christos case BINOP_EXP: 2070 1.1 christos decNumberPower (&number3, &number1, &number2, &set); 2071 1.1 christos break; 2072 1.1 christos default: 2073 1.1 christos error (_("Operation not valid for decimal floating point number.")); 2074 1.1 christos break; 2075 1.1 christos } 2076 1.1 christos 2077 1.1 christos /* Check for errors in the DFP operation. */ 2078 1.1 christos decimal_check_errors (&set); 2079 1.1 christos 2080 1.1 christos decimal_from_number (&number3, res, type_res); 2081 1.1 christos } 2082 1.1 christos 2083 1.1 christos /* Compares two numbers numerically. If X is less than Y then the return value 2084 1.1 christos will be -1. If they are equal, then the return value will be 0. If X is 2085 1.1 christos greater than the Y then the return value will be 1. */ 2086 1.1 christos int 2087 1.1 christos decimal_float_ops::compare (const gdb_byte *x, const struct type *type_x, 2088 1.1 christos const gdb_byte *y, const struct type *type_y) const 2089 1.1 christos { 2090 1.1 christos decNumber number1, number2, result; 2091 1.1 christos decContext set; 2092 1.1 christos const struct type *type_result; 2093 1.1 christos 2094 1.1 christos decimal_to_number (x, type_x, &number1); 2095 1.1 christos decimal_to_number (y, type_y, &number2); 2096 1.1 christos 2097 1.1 christos /* Perform the comparison in the larger of the two sizes. */ 2098 1.1.1.3 christos type_result = type_x->length () > type_y->length () ? type_x : type_y; 2099 1.1 christos set_decnumber_context (&set, type_result); 2100 1.1 christos 2101 1.1 christos decNumberCompare (&result, &number1, &number2, &set); 2102 1.1 christos 2103 1.1 christos /* Check for errors in the DFP operation. */ 2104 1.1 christos decimal_check_errors (&set); 2105 1.1 christos 2106 1.1 christos if (decNumberIsNaN (&result)) 2107 1.1 christos error (_("Comparison with an invalid number (NaN).")); 2108 1.1 christos else if (decNumberIsZero (&result)) 2109 1.1 christos return 0; 2110 1.1 christos else if (decNumberIsNegative (&result)) 2111 1.1 christos return -1; 2112 1.1 christos else 2113 1.1 christos return 1; 2114 1.1 christos } 2115 1.1 christos 2116 1.1 christos /* Convert a decimal value from a decimal type with LEN_FROM bytes to a 2117 1.1 christos decimal type with LEN_TO bytes. */ 2118 1.1 christos void 2119 1.1 christos decimal_float_ops::convert (const gdb_byte *from, const struct type *from_type, 2120 1.1 christos gdb_byte *to, const struct type *to_type) const 2121 1.1 christos { 2122 1.1 christos decNumber number; 2123 1.1 christos 2124 1.1 christos decimal_to_number (from, from_type, &number); 2125 1.1 christos decimal_from_number (&number, to, to_type); 2126 1.1 christos } 2127 1.1 christos 2128 1.1 christos 2129 1.1 christos /* Typed floating-point routines. These routines operate on floating-point 2130 1.1 christos values in target format, represented by a byte buffer interpreted as a 2131 1.1 christos "struct type", which may be either a binary or decimal floating-point 2132 1.1 christos type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */ 2133 1.1 christos 2134 1.1 christos /* Return whether TYPE1 and TYPE2 are of the same category (binary or 2135 1.1 christos decimal floating-point). */ 2136 1.1 christos static bool 2137 1.1 christos target_float_same_category_p (const struct type *type1, 2138 1.1 christos const struct type *type2) 2139 1.1 christos { 2140 1.1.1.2 christos return type1->code () == type2->code (); 2141 1.1 christos } 2142 1.1 christos 2143 1.1 christos /* Return whether TYPE1 and TYPE2 use the same floating-point format. */ 2144 1.1 christos static bool 2145 1.1 christos target_float_same_format_p (const struct type *type1, 2146 1.1 christos const struct type *type2) 2147 1.1 christos { 2148 1.1 christos if (!target_float_same_category_p (type1, type2)) 2149 1.1 christos return false; 2150 1.1 christos 2151 1.1.1.2 christos switch (type1->code ()) 2152 1.1 christos { 2153 1.1 christos case TYPE_CODE_FLT: 2154 1.1 christos return floatformat_from_type (type1) == floatformat_from_type (type2); 2155 1.1 christos 2156 1.1 christos case TYPE_CODE_DECFLOAT: 2157 1.1.1.3 christos return (type1->length () == type2->length () 2158 1.1.1.2 christos && (type_byte_order (type1) 2159 1.1.1.2 christos == type_byte_order (type2))); 2160 1.1 christos 2161 1.1 christos default: 2162 1.1 christos gdb_assert_not_reached ("unexpected type code"); 2163 1.1 christos } 2164 1.1 christos } 2165 1.1 christos 2166 1.1 christos /* Return the size (without padding) of the target floating-point 2167 1.1 christos format used by TYPE. */ 2168 1.1 christos static int 2169 1.1 christos target_float_format_length (const struct type *type) 2170 1.1 christos { 2171 1.1.1.2 christos switch (type->code ()) 2172 1.1 christos { 2173 1.1 christos case TYPE_CODE_FLT: 2174 1.1 christos return floatformat_totalsize_bytes (floatformat_from_type (type)); 2175 1.1 christos 2176 1.1 christos case TYPE_CODE_DECFLOAT: 2177 1.1.1.3 christos return type->length (); 2178 1.1 christos 2179 1.1 christos default: 2180 1.1 christos gdb_assert_not_reached ("unexpected type code"); 2181 1.1 christos } 2182 1.1 christos } 2183 1.1 christos 2184 1.1 christos /* Identifiers of available host-side intermediate formats. These must 2185 1.1 christos be sorted so the that the more "general" kinds come later. */ 2186 1.1 christos enum target_float_ops_kind 2187 1.1 christos { 2188 1.1 christos /* Target binary floating-point formats that match a host format. */ 2189 1.1 christos host_float = 0, 2190 1.1 christos host_double, 2191 1.1 christos host_long_double, 2192 1.1 christos /* Any other target binary floating-point format. */ 2193 1.1 christos binary, 2194 1.1 christos /* Any target decimal floating-point format. */ 2195 1.1 christos decimal 2196 1.1 christos }; 2197 1.1 christos 2198 1.1 christos /* Given a target type TYPE, choose the best host-side intermediate format 2199 1.1 christos to perform operations on TYPE in. */ 2200 1.1 christos static enum target_float_ops_kind 2201 1.1 christos get_target_float_ops_kind (const struct type *type) 2202 1.1 christos { 2203 1.1.1.2 christos switch (type->code ()) 2204 1.1 christos { 2205 1.1 christos case TYPE_CODE_FLT: 2206 1.1.1.3 christos { 2207 1.1 christos const struct floatformat *fmt = floatformat_from_type (type); 2208 1.1 christos 2209 1.1 christos /* Binary floating-point formats matching a host format. */ 2210 1.1 christos if (fmt == host_float_format) 2211 1.1 christos return target_float_ops_kind::host_float; 2212 1.1 christos if (fmt == host_double_format) 2213 1.1 christos return target_float_ops_kind::host_double; 2214 1.1 christos if (fmt == host_long_double_format) 2215 1.1 christos return target_float_ops_kind::host_long_double; 2216 1.1 christos 2217 1.1 christos /* Any other binary floating-point format. */ 2218 1.1 christos return target_float_ops_kind::binary; 2219 1.1 christos } 2220 1.1 christos 2221 1.1 christos case TYPE_CODE_DECFLOAT: 2222 1.1 christos { 2223 1.1 christos /* Any decimal floating-point format. */ 2224 1.1 christos return target_float_ops_kind::decimal; 2225 1.1 christos } 2226 1.1 christos 2227 1.1 christos default: 2228 1.1 christos gdb_assert_not_reached ("unexpected type code"); 2229 1.1 christos } 2230 1.1 christos } 2231 1.1 christos 2232 1.1.1.3 christos /* Return target_float_ops to perform operations for KIND. */ 2233 1.1 christos static const target_float_ops * 2234 1.1 christos get_target_float_ops (enum target_float_ops_kind kind) 2235 1.1 christos { 2236 1.1 christos switch (kind) 2237 1.1 christos { 2238 1.1 christos /* If the type format matches one of the host floating-point 2239 1.1 christos types, use that type as intermediate format. */ 2240 1.1 christos case target_float_ops_kind::host_float: 2241 1.1.1.3 christos { 2242 1.1 christos static host_float_ops<float> host_float_ops_float; 2243 1.1 christos return &host_float_ops_float; 2244 1.1 christos } 2245 1.1 christos 2246 1.1 christos case target_float_ops_kind::host_double: 2247 1.1.1.3 christos { 2248 1.1 christos static host_float_ops<double> host_float_ops_double; 2249 1.1 christos return &host_float_ops_double; 2250 1.1 christos } 2251 1.1 christos 2252 1.1 christos case target_float_ops_kind::host_long_double: 2253 1.1.1.3 christos { 2254 1.1 christos static host_float_ops<long double> host_float_ops_long_double; 2255 1.1 christos return &host_float_ops_long_double; 2256 1.1 christos } 2257 1.1 christos 2258 1.1 christos /* For binary floating-point formats that do not match any host format, 2259 1.1.1.3 christos use mpfr_t as intermediate format to provide precise target-floating 2260 1.1.1.3 christos point emulation. However, if the MPFR library is not available, 2261 1.1.1.3 christos use the largest host floating-point type as intermediate format. */ 2262 1.1 christos case target_float_ops_kind::binary: 2263 1.1.1.3 christos { 2264 1.1 christos static mpfr_float_ops binary_float_ops; 2265 1.1 christos return &binary_float_ops; 2266 1.1 christos } 2267 1.1 christos 2268 1.1 christos /* For decimal floating-point types, always use the libdecnumber 2269 1.1 christos decNumber type as intermediate format. */ 2270 1.1 christos case target_float_ops_kind::decimal: 2271 1.1 christos { 2272 1.1 christos static decimal_float_ops decimal_float_ops; 2273 1.1 christos return &decimal_float_ops; 2274 1.1 christos } 2275 1.1 christos 2276 1.1 christos default: 2277 1.1 christos gdb_assert_not_reached ("unexpected target_float_ops_kind"); 2278 1.1 christos } 2279 1.1 christos } 2280 1.1 christos 2281 1.1 christos /* Given a target type TYPE, determine the best host-side intermediate format 2282 1.1 christos to perform operations on TYPE in. */ 2283 1.1 christos static const target_float_ops * 2284 1.1 christos get_target_float_ops (const struct type *type) 2285 1.1 christos { 2286 1.1 christos enum target_float_ops_kind kind = get_target_float_ops_kind (type); 2287 1.1 christos return get_target_float_ops (kind); 2288 1.1 christos } 2289 1.1 christos 2290 1.1 christos /* The same for operations involving two target types TYPE1 and TYPE2. */ 2291 1.1 christos static const target_float_ops * 2292 1.1 christos get_target_float_ops (const struct type *type1, const struct type *type2) 2293 1.1 christos { 2294 1.1.1.2 christos gdb_assert (type1->code () == type2->code ()); 2295 1.1 christos 2296 1.1 christos enum target_float_ops_kind kind1 = get_target_float_ops_kind (type1); 2297 1.1 christos enum target_float_ops_kind kind2 = get_target_float_ops_kind (type2); 2298 1.1 christos 2299 1.1 christos /* Given the way the kinds are sorted, we simply choose the larger one; 2300 1.1 christos this will be able to hold values of either type. */ 2301 1.1 christos return get_target_float_ops (std::max (kind1, kind2)); 2302 1.1 christos } 2303 1.1 christos 2304 1.1 christos /* Return whether the byte-stream ADDR holds a valid value of 2305 1.1 christos floating-point type TYPE. */ 2306 1.1 christos bool 2307 1.1 christos target_float_is_valid (const gdb_byte *addr, const struct type *type) 2308 1.1 christos { 2309 1.1.1.2 christos if (type->code () == TYPE_CODE_FLT) 2310 1.1 christos return floatformat_is_valid (floatformat_from_type (type), addr); 2311 1.1 christos 2312 1.1.1.2 christos if (type->code () == TYPE_CODE_DECFLOAT) 2313 1.1 christos return true; 2314 1.1 christos 2315 1.1 christos gdb_assert_not_reached ("unexpected type code"); 2316 1.1 christos } 2317 1.1 christos 2318 1.1 christos /* Return whether the byte-stream ADDR, interpreted as floating-point 2319 1.1 christos type TYPE, is numerically equal to zero (of either sign). */ 2320 1.1 christos bool 2321 1.1 christos target_float_is_zero (const gdb_byte *addr, const struct type *type) 2322 1.1 christos { 2323 1.1.1.2 christos if (type->code () == TYPE_CODE_FLT) 2324 1.1 christos return (floatformat_classify (floatformat_from_type (type), addr) 2325 1.1 christos == float_zero); 2326 1.1 christos 2327 1.1.1.2 christos if (type->code () == TYPE_CODE_DECFLOAT) 2328 1.1 christos return decimal_is_zero (addr, type); 2329 1.1 christos 2330 1.1 christos gdb_assert_not_reached ("unexpected type code"); 2331 1.1 christos } 2332 1.1 christos 2333 1.1 christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, 2334 1.1 christos to a string, optionally using the print format FORMAT. */ 2335 1.1 christos std::string 2336 1.1 christos target_float_to_string (const gdb_byte *addr, const struct type *type, 2337 1.1 christos const char *format) 2338 1.1 christos { 2339 1.1 christos /* Unless we need to adhere to a specific format, provide special 2340 1.1 christos output for special cases of binary floating-point numbers. */ 2341 1.1.1.2 christos if (format == nullptr && type->code () == TYPE_CODE_FLT) 2342 1.1 christos { 2343 1.1 christos const struct floatformat *fmt = floatformat_from_type (type); 2344 1.1 christos 2345 1.1 christos /* Detect invalid representations. */ 2346 1.1 christos if (!floatformat_is_valid (fmt, addr)) 2347 1.1 christos return "<invalid float value>"; 2348 1.1 christos 2349 1.1 christos /* Handle NaN and Inf. */ 2350 1.1 christos enum float_kind kind = floatformat_classify (fmt, addr); 2351 1.1 christos if (kind == float_nan) 2352 1.1 christos { 2353 1.1 christos const char *sign = floatformat_is_negative (fmt, addr)? "-" : ""; 2354 1.1 christos const char *mantissa = floatformat_mantissa (fmt, addr); 2355 1.1 christos return string_printf ("%snan(0x%s)", sign, mantissa); 2356 1.1 christos } 2357 1.1 christos else if (kind == float_infinite) 2358 1.1 christos { 2359 1.1 christos const char *sign = floatformat_is_negative (fmt, addr)? "-" : ""; 2360 1.1 christos return string_printf ("%sinf", sign); 2361 1.1 christos } 2362 1.1 christos } 2363 1.1 christos 2364 1.1 christos const target_float_ops *ops = get_target_float_ops (type); 2365 1.1 christos return ops->to_string (addr, type, format); 2366 1.1 christos } 2367 1.1 christos 2368 1.1 christos /* Parse string STRING into a target floating-number of type TYPE and 2369 1.1 christos store it as byte-stream ADDR. Return whether parsing succeeded. */ 2370 1.1 christos bool 2371 1.1 christos target_float_from_string (gdb_byte *addr, const struct type *type, 2372 1.1 christos const std::string &string) 2373 1.1 christos { 2374 1.1 christos const target_float_ops *ops = get_target_float_ops (type); 2375 1.1 christos return ops->from_string (addr, type, string); 2376 1.1 christos } 2377 1.1 christos 2378 1.1 christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, 2379 1.1 christos to an integer value (rounding towards zero). */ 2380 1.1 christos LONGEST 2381 1.1 christos target_float_to_longest (const gdb_byte *addr, const struct type *type) 2382 1.1 christos { 2383 1.1 christos const target_float_ops *ops = get_target_float_ops (type); 2384 1.1 christos return ops->to_longest (addr, type); 2385 1.1 christos } 2386 1.1 christos 2387 1.1 christos /* Convert signed integer VAL to a target floating-number of type TYPE 2388 1.1 christos and store it as byte-stream ADDR. */ 2389 1.1 christos void 2390 1.1 christos target_float_from_longest (gdb_byte *addr, const struct type *type, 2391 1.1 christos LONGEST val) 2392 1.1 christos { 2393 1.1 christos const target_float_ops *ops = get_target_float_ops (type); 2394 1.1 christos ops->from_longest (addr, type, val); 2395 1.1 christos } 2396 1.1 christos 2397 1.1 christos /* Convert unsigned integer VAL to a target floating-number of type TYPE 2398 1.1 christos and store it as byte-stream ADDR. */ 2399 1.1 christos void 2400 1.1 christos target_float_from_ulongest (gdb_byte *addr, const struct type *type, 2401 1.1 christos ULONGEST val) 2402 1.1 christos { 2403 1.1 christos const target_float_ops *ops = get_target_float_ops (type); 2404 1.1 christos ops->from_ulongest (addr, type, val); 2405 1.1 christos } 2406 1.1 christos 2407 1.1 christos /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, 2408 1.1 christos to a floating-point value in the host "double" format. */ 2409 1.1 christos double 2410 1.1 christos target_float_to_host_double (const gdb_byte *addr, 2411 1.1 christos const struct type *type) 2412 1.1 christos { 2413 1.1 christos const target_float_ops *ops = get_target_float_ops (type); 2414 1.1 christos return ops->to_host_double (addr, type); 2415 1.1 christos } 2416 1.1 christos 2417 1.1 christos /* Convert floating-point value VAL in the host "double" format to a target 2418 1.1 christos floating-number of type TYPE and store it as byte-stream ADDR. */ 2419 1.1 christos void 2420 1.1 christos target_float_from_host_double (gdb_byte *addr, const struct type *type, 2421 1.1 christos double val) 2422 1.1 christos { 2423 1.1 christos const target_float_ops *ops = get_target_float_ops (type); 2424 1.1 christos ops->from_host_double (addr, type, val); 2425 1.1 christos } 2426 1.1 christos 2427 1.1 christos /* Convert a floating-point number of type FROM_TYPE from the target 2428 1.1 christos byte-stream FROM to a floating-point number of type TO_TYPE, and 2429 1.1 christos store it to the target byte-stream TO. */ 2430 1.1 christos void 2431 1.1 christos target_float_convert (const gdb_byte *from, const struct type *from_type, 2432 1.1 christos gdb_byte *to, const struct type *to_type) 2433 1.1 christos { 2434 1.1 christos /* We cannot directly convert between binary and decimal floating-point 2435 1.1 christos types, so go via an intermediary string. */ 2436 1.1 christos if (!target_float_same_category_p (from_type, to_type)) 2437 1.1 christos { 2438 1.1 christos std::string str = target_float_to_string (from, from_type); 2439 1.1 christos target_float_from_string (to, to_type, str); 2440 1.1 christos return; 2441 1.1 christos } 2442 1.1 christos 2443 1.1 christos /* Convert between two different formats in the same category. */ 2444 1.1 christos if (!target_float_same_format_p (from_type, to_type)) 2445 1.1.1.3 christos { 2446 1.1.1.3 christos const target_float_ops *ops = get_target_float_ops (from_type, to_type); 2447 1.1.1.3 christos ops->convert (from, from_type, to, to_type); 2448 1.1.1.3 christos return; 2449 1.1.1.3 christos } 2450 1.1 christos 2451 1.1 christos /* The floating-point formats match, so we simply copy the data, ensuring 2452 1.1 christos possible padding bytes in the target buffer are zeroed out. */ 2453 1.1.1.3 christos memset (to, 0, to_type->length ()); 2454 1.1 christos memcpy (to, from, target_float_format_length (to_type)); 2455 1.1 christos } 2456 1.1 christos 2457 1.1 christos /* Perform the binary operation indicated by OPCODE, using as operands the 2458 1.1 christos target byte streams X and Y, interpreted as floating-point numbers of 2459 1.1 christos types TYPE_X and TYPE_Y, respectively. Convert the result to type 2460 1.1 christos TYPE_RES and store it into the byte-stream RES. 2461 1.1 christos 2462 1.1 christos The three types must either be all binary floating-point types, or else 2463 1.1 christos all decimal floating-point types. Binary and decimal floating-point 2464 1.1 christos types cannot be mixed within a single operation. */ 2465 1.1 christos void 2466 1.1 christos target_float_binop (enum exp_opcode opcode, 2467 1.1 christos const gdb_byte *x, const struct type *type_x, 2468 1.1 christos const gdb_byte *y, const struct type *type_y, 2469 1.1 christos gdb_byte *res, const struct type *type_res) 2470 1.1 christos { 2471 1.1 christos gdb_assert (target_float_same_category_p (type_x, type_res)); 2472 1.1 christos gdb_assert (target_float_same_category_p (type_y, type_res)); 2473 1.1 christos 2474 1.1 christos const target_float_ops *ops = get_target_float_ops (type_x, type_y); 2475 1.1 christos ops->binop (opcode, x, type_x, y, type_y, res, type_res); 2476 1.1 christos } 2477 1.1 christos 2478 1.1 christos /* Compare the two target byte streams X and Y, interpreted as floating-point 2479 1.1 christos numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y 2480 1.1 christos are equal, -1 if X is less than Y, and 1 otherwise. 2481 1.1 christos 2482 1.1 christos The two types must either both be binary floating-point types, or else 2483 1.1 christos both be decimal floating-point types. Binary and decimal floating-point 2484 1.1 christos types cannot compared directly against each other. */ 2485 1.1 christos int 2486 1.1 christos target_float_compare (const gdb_byte *x, const struct type *type_x, 2487 1.1 christos const gdb_byte *y, const struct type *type_y) 2488 1.1 christos { 2489 1.1 christos gdb_assert (target_float_same_category_p (type_x, type_y)); 2490 1.1 christos 2491 1.1 christos const target_float_ops *ops = get_target_float_ops (type_x, type_y); 2492 1.1 christos return ops->compare (x, type_x, y, type_y); 2493 1.1 christos } 2494 1.1 christos 2495