Commit | Line | Data |
---|---|---|
70100014 UW |
1 | /* Floating point routines for GDB, the GNU debugger. |
2 | ||
3 | Copyright (C) 2017 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
70100014 UW |
21 | #include "gdbtypes.h" |
22 | #include "floatformat.h" | |
23 | #include "target-float.h" | |
24 | ||
25 | ||
50637b26 UW |
26 | /* Helper routines operating on binary floating-point data. */ |
27 | ||
66c02b9e UW |
28 | #include <math.h> |
29 | ||
1cfb73db UW |
30 | #if (defined HAVE_LONG_DOUBLE && defined PRINTF_HAS_LONG_DOUBLE \ |
31 | && defined SCANF_HAS_LONG_DOUBLE) | |
32 | typedef long double DOUBLEST; | |
33 | #else | |
34 | typedef double DOUBLEST; | |
35 | /* If we can't scan or print long double, we don't want to use it | |
36 | anywhere. */ | |
37 | # undef HAVE_LONG_DOUBLE | |
38 | # undef PRINTF_HAS_LONG_DOUBLE | |
39 | # undef SCANF_HAS_LONG_DOUBLE | |
40 | #endif | |
41 | ||
42 | /* Different kinds of floatformat numbers recognized by | |
43 | floatformat_classify. To avoid portability issues, we use local | |
44 | values instead of the C99 macros (FP_NAN et cetera). */ | |
45 | enum float_kind { | |
46 | float_nan, | |
47 | float_infinite, | |
48 | float_zero, | |
49 | float_normal, | |
50 | float_subnormal | |
51 | }; | |
52 | ||
53 | /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not | |
54 | going to bother with trying to muck around with whether it is defined in | |
55 | a system header, what we do if not, etc. */ | |
56 | #define FLOATFORMAT_CHAR_BIT 8 | |
57 | ||
58 | /* The number of bytes that the largest floating-point type that we | |
59 | can convert to doublest will need. */ | |
60 | #define FLOATFORMAT_LARGEST_BYTES 16 | |
61 | ||
62 | /* Return the floatformat's total size in host bytes. */ | |
63 | static size_t | |
64 | floatformat_totalsize_bytes (const struct floatformat *fmt) | |
65 | { | |
66 | return ((fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) | |
67 | / FLOATFORMAT_CHAR_BIT); | |
68 | } | |
69 | ||
70 | /* Return the precision of the floating point format FMT. */ | |
71 | static int | |
72 | floatformat_precision (const struct floatformat *fmt) | |
73 | { | |
74 | /* Assume the precision of and IBM long double is twice the precision | |
75 | of the underlying double. This matches what GCC does. */ | |
76 | if (fmt->split_half) | |
77 | return 2 * floatformat_precision (fmt->split_half); | |
78 | ||
79 | /* Otherwise, the precision is the size of mantissa in bits, | |
80 | including the implicit bit if present. */ | |
81 | int prec = fmt->man_len; | |
82 | if (fmt->intbit == floatformat_intbit_no) | |
83 | prec++; | |
84 | ||
85 | return prec; | |
86 | } | |
87 | ||
88 | /* Normalize the byte order of FROM into TO. If no normalization is | |
89 | needed then FMT->byteorder is returned and TO is not changed; | |
90 | otherwise the format of the normalized form in TO is returned. */ | |
91 | static enum floatformat_byteorders | |
92 | floatformat_normalize_byteorder (const struct floatformat *fmt, | |
93 | const void *from, void *to) | |
94 | { | |
95 | const unsigned char *swapin; | |
96 | unsigned char *swapout; | |
97 | int words; | |
98 | ||
99 | if (fmt->byteorder == floatformat_little | |
100 | || fmt->byteorder == floatformat_big) | |
101 | return fmt->byteorder; | |
102 | ||
103 | words = fmt->totalsize / FLOATFORMAT_CHAR_BIT; | |
104 | words >>= 2; | |
105 | ||
106 | swapout = (unsigned char *)to; | |
107 | swapin = (const unsigned char *)from; | |
108 | ||
109 | if (fmt->byteorder == floatformat_vax) | |
110 | { | |
111 | while (words-- > 0) | |
112 | { | |
113 | *swapout++ = swapin[1]; | |
114 | *swapout++ = swapin[0]; | |
115 | *swapout++ = swapin[3]; | |
116 | *swapout++ = swapin[2]; | |
117 | swapin += 4; | |
118 | } | |
119 | /* This may look weird, since VAX is little-endian, but it is | |
120 | easier to translate to big-endian than to little-endian. */ | |
121 | return floatformat_big; | |
122 | } | |
123 | else | |
124 | { | |
125 | gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword); | |
126 | ||
127 | while (words-- > 0) | |
128 | { | |
129 | *swapout++ = swapin[3]; | |
130 | *swapout++ = swapin[2]; | |
131 | *swapout++ = swapin[1]; | |
132 | *swapout++ = swapin[0]; | |
133 | swapin += 4; | |
134 | } | |
135 | return floatformat_big; | |
136 | } | |
137 | } | |
138 | ||
139 | /* Extract a field which starts at START and is LEN bytes long. DATA and | |
140 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ | |
141 | static unsigned long | |
142 | get_field (const bfd_byte *data, enum floatformat_byteorders order, | |
143 | unsigned int total_len, unsigned int start, unsigned int len) | |
144 | { | |
145 | unsigned long result; | |
146 | unsigned int cur_byte; | |
147 | int cur_bitshift; | |
148 | ||
149 | /* Caller must byte-swap words before calling this routine. */ | |
150 | gdb_assert (order == floatformat_little || order == floatformat_big); | |
151 | ||
152 | /* Start at the least significant part of the field. */ | |
153 | if (order == floatformat_little) | |
154 | { | |
155 | /* We start counting from the other end (i.e, from the high bytes | |
156 | rather than the low bytes). As such, we need to be concerned | |
157 | with what happens if bit 0 doesn't start on a byte boundary. | |
158 | I.e, we need to properly handle the case where total_len is | |
159 | not evenly divisible by 8. So we compute ``excess'' which | |
160 | represents the number of bits from the end of our starting | |
161 | byte needed to get to bit 0. */ | |
162 | int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); | |
163 | ||
164 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) | |
165 | - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); | |
166 | cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) | |
167 | - FLOATFORMAT_CHAR_BIT; | |
168 | } | |
169 | else | |
170 | { | |
171 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
172 | cur_bitshift = | |
173 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
174 | } | |
175 | if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) | |
176 | result = *(data + cur_byte) >> (-cur_bitshift); | |
177 | else | |
178 | result = 0; | |
179 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
180 | if (order == floatformat_little) | |
181 | ++cur_byte; | |
182 | else | |
183 | --cur_byte; | |
184 | ||
185 | /* Move towards the most significant part of the field. */ | |
186 | while (cur_bitshift < len) | |
187 | { | |
188 | result |= (unsigned long)*(data + cur_byte) << cur_bitshift; | |
189 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
190 | switch (order) | |
191 | { | |
192 | case floatformat_little: | |
193 | ++cur_byte; | |
194 | break; | |
195 | case floatformat_big: | |
196 | --cur_byte; | |
197 | break; | |
198 | } | |
199 | } | |
200 | if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT) | |
201 | /* Mask out bits which are not part of the field. */ | |
202 | result &= ((1UL << len) - 1); | |
203 | return result; | |
204 | } | |
205 | ||
206 | /* Set a field which starts at START and is LEN bytes long. DATA and | |
207 | TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */ | |
208 | static void | |
209 | put_field (unsigned char *data, enum floatformat_byteorders order, | |
210 | unsigned int total_len, unsigned int start, unsigned int len, | |
211 | unsigned long stuff_to_put) | |
212 | { | |
213 | unsigned int cur_byte; | |
214 | int cur_bitshift; | |
215 | ||
216 | /* Caller must byte-swap words before calling this routine. */ | |
217 | gdb_assert (order == floatformat_little || order == floatformat_big); | |
218 | ||
219 | /* Start at the least significant part of the field. */ | |
220 | if (order == floatformat_little) | |
221 | { | |
222 | int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT); | |
223 | ||
224 | cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) | |
225 | - ((start + len + excess) / FLOATFORMAT_CHAR_BIT); | |
226 | cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) | |
227 | - FLOATFORMAT_CHAR_BIT; | |
228 | } | |
229 | else | |
230 | { | |
231 | cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT; | |
232 | cur_bitshift = | |
233 | ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT; | |
234 | } | |
235 | if (cur_bitshift > -FLOATFORMAT_CHAR_BIT) | |
236 | { | |
237 | *(data + cur_byte) &= | |
238 | ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) | |
239 | << (-cur_bitshift)); | |
240 | *(data + cur_byte) |= | |
241 | (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift); | |
242 | } | |
243 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
244 | if (order == floatformat_little) | |
245 | ++cur_byte; | |
246 | else | |
247 | --cur_byte; | |
248 | ||
249 | /* Move towards the most significant part of the field. */ | |
250 | while (cur_bitshift < len) | |
251 | { | |
252 | if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT) | |
253 | { | |
254 | /* This is the last byte. */ | |
255 | *(data + cur_byte) &= | |
256 | ~((1 << (len - cur_bitshift)) - 1); | |
257 | *(data + cur_byte) |= (stuff_to_put >> cur_bitshift); | |
258 | } | |
259 | else | |
260 | *(data + cur_byte) = ((stuff_to_put >> cur_bitshift) | |
261 | & ((1 << FLOATFORMAT_CHAR_BIT) - 1)); | |
262 | cur_bitshift += FLOATFORMAT_CHAR_BIT; | |
263 | if (order == floatformat_little) | |
264 | ++cur_byte; | |
265 | else | |
266 | --cur_byte; | |
267 | } | |
268 | } | |
269 | ||
270 | /* Check if VAL (which is assumed to be a floating point number whose | |
271 | format is described by FMT) is negative. */ | |
272 | static int | |
273 | floatformat_is_negative (const struct floatformat *fmt, | |
274 | const bfd_byte *uval) | |
275 | { | |
276 | enum floatformat_byteorders order; | |
277 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
278 | ||
279 | gdb_assert (fmt != NULL); | |
280 | gdb_assert (fmt->totalsize | |
281 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
282 | ||
283 | /* An IBM long double (a two element array of double) always takes the | |
284 | sign of the first double. */ | |
285 | if (fmt->split_half) | |
286 | fmt = fmt->split_half; | |
287 | ||
288 | order = floatformat_normalize_byteorder (fmt, uval, newfrom); | |
289 | ||
290 | if (order != fmt->byteorder) | |
291 | uval = newfrom; | |
292 | ||
293 | return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1); | |
294 | } | |
295 | ||
296 | /* Check if VAL is "not a number" (NaN) for FMT. */ | |
297 | static enum float_kind | |
298 | floatformat_classify (const struct floatformat *fmt, | |
299 | const bfd_byte *uval) | |
300 | { | |
301 | long exponent; | |
302 | unsigned long mant; | |
303 | unsigned int mant_bits, mant_off; | |
304 | int mant_bits_left; | |
305 | enum floatformat_byteorders order; | |
306 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
307 | int mant_zero; | |
308 | ||
309 | gdb_assert (fmt != NULL); | |
310 | gdb_assert (fmt->totalsize | |
311 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
312 | ||
313 | /* An IBM long double (a two element array of double) can be classified | |
314 | by looking at the first double. inf and nan are specified as | |
315 | ignoring the second double. zero and subnormal will always have | |
316 | the second double 0.0 if the long double is correctly rounded. */ | |
317 | if (fmt->split_half) | |
318 | fmt = fmt->split_half; | |
319 | ||
320 | order = floatformat_normalize_byteorder (fmt, uval, newfrom); | |
321 | ||
322 | if (order != fmt->byteorder) | |
323 | uval = newfrom; | |
324 | ||
325 | exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start, | |
326 | fmt->exp_len); | |
327 | ||
328 | mant_bits_left = fmt->man_len; | |
329 | mant_off = fmt->man_start; | |
330 | ||
331 | mant_zero = 1; | |
332 | while (mant_bits_left > 0) | |
333 | { | |
334 | mant_bits = std::min (mant_bits_left, 32); | |
335 | ||
336 | mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); | |
337 | ||
338 | /* If there is an explicit integer bit, mask it off. */ | |
339 | if (mant_off == fmt->man_start | |
340 | && fmt->intbit == floatformat_intbit_yes) | |
341 | mant &= ~(1 << (mant_bits - 1)); | |
342 | ||
343 | if (mant) | |
344 | { | |
345 | mant_zero = 0; | |
346 | break; | |
347 | } | |
348 | ||
349 | mant_off += mant_bits; | |
350 | mant_bits_left -= mant_bits; | |
351 | } | |
352 | ||
353 | /* If exp_nan is not set, assume that inf, NaN, and subnormals are not | |
354 | supported. */ | |
355 | if (! fmt->exp_nan) | |
356 | { | |
357 | if (mant_zero) | |
358 | return float_zero; | |
359 | else | |
360 | return float_normal; | |
361 | } | |
362 | ||
363 | if (exponent == 0) | |
364 | { | |
365 | if (mant_zero) | |
366 | return float_zero; | |
367 | else | |
368 | return float_subnormal; | |
369 | } | |
370 | ||
371 | if (exponent == fmt->exp_nan) | |
372 | { | |
373 | if (mant_zero) | |
374 | return float_infinite; | |
375 | else | |
376 | return float_nan; | |
377 | } | |
378 | ||
379 | return float_normal; | |
380 | } | |
381 | ||
382 | /* Convert the mantissa of VAL (which is assumed to be a floating | |
383 | point number whose format is described by FMT) into a hexadecimal | |
384 | and store it in a static string. Return a pointer to that string. */ | |
385 | static const char * | |
386 | floatformat_mantissa (const struct floatformat *fmt, | |
387 | const bfd_byte *val) | |
388 | { | |
389 | unsigned char *uval = (unsigned char *) val; | |
390 | unsigned long mant; | |
391 | unsigned int mant_bits, mant_off; | |
392 | int mant_bits_left; | |
393 | static char res[50]; | |
394 | char buf[9]; | |
395 | int len; | |
396 | enum floatformat_byteorders order; | |
397 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
398 | ||
399 | gdb_assert (fmt != NULL); | |
400 | gdb_assert (fmt->totalsize | |
401 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
402 | ||
403 | /* For IBM long double (a two element array of double), return the | |
404 | mantissa of the first double. The problem with returning the | |
405 | actual mantissa from both doubles is that there can be an | |
406 | arbitrary number of implied 0's or 1's between the mantissas | |
407 | of the first and second double. In any case, this function | |
408 | is only used for dumping out nans, and a nan is specified to | |
409 | ignore the value in the second double. */ | |
410 | if (fmt->split_half) | |
411 | fmt = fmt->split_half; | |
412 | ||
413 | order = floatformat_normalize_byteorder (fmt, uval, newfrom); | |
414 | ||
415 | if (order != fmt->byteorder) | |
416 | uval = newfrom; | |
417 | ||
418 | if (! fmt->exp_nan) | |
419 | return 0; | |
420 | ||
421 | /* Make sure we have enough room to store the mantissa. */ | |
422 | gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2); | |
423 | ||
424 | mant_off = fmt->man_start; | |
425 | mant_bits_left = fmt->man_len; | |
426 | mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32; | |
427 | ||
428 | mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits); | |
429 | ||
430 | len = xsnprintf (res, sizeof res, "%lx", mant); | |
431 | ||
432 | mant_off += mant_bits; | |
433 | mant_bits_left -= mant_bits; | |
434 | ||
435 | while (mant_bits_left > 0) | |
436 | { | |
437 | mant = get_field (uval, order, fmt->totalsize, mant_off, 32); | |
438 | ||
439 | xsnprintf (buf, sizeof buf, "%08lx", mant); | |
440 | gdb_assert (len + strlen (buf) <= sizeof res); | |
441 | strcat (res, buf); | |
442 | ||
443 | mant_off += 32; | |
444 | mant_bits_left -= 32; | |
445 | } | |
446 | ||
447 | return res; | |
448 | } | |
449 | ||
450 | /* Convert TO/FROM target to the hosts DOUBLEST floating-point format. | |
451 | ||
452 | If the host and target formats agree, we just copy the raw data | |
453 | into the appropriate type of variable and return, letting the host | |
454 | increase precision as necessary. Otherwise, we call the conversion | |
455 | routine and let it do the dirty work. Note that even if the target | |
456 | and host floating-point formats match, the length of the types | |
457 | might still be different, so the conversion routines must make sure | |
458 | to not overrun any buffers. For example, on x86, long double is | |
459 | the 80-bit extended precision type on both 32-bit and 64-bit ABIs, | |
460 | but by default it is stored as 12 bytes on 32-bit, and 16 bytes on | |
461 | 64-bit, for alignment reasons. See comment in store_typed_floating | |
462 | for a discussion about zeroing out remaining bytes in the target | |
463 | buffer. */ | |
464 | ||
465 | static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT; | |
466 | static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT; | |
467 | static const struct floatformat *host_long_double_format | |
468 | = GDB_HOST_LONG_DOUBLE_FORMAT; | |
469 | ||
470 | /* Convert from FMT to a DOUBLEST. FROM is the address of the extended float. | |
471 | Store the DOUBLEST in *TO. */ | |
472 | static void | |
473 | floatformat_to_doublest (const struct floatformat *fmt, | |
474 | const void *from, DOUBLEST *to) | |
475 | { | |
476 | gdb_assert (fmt != NULL); | |
477 | ||
478 | if (fmt == host_float_format) | |
479 | { | |
480 | float val = 0; | |
481 | ||
482 | memcpy (&val, from, floatformat_totalsize_bytes (fmt)); | |
483 | *to = val; | |
484 | return; | |
485 | } | |
486 | else if (fmt == host_double_format) | |
487 | { | |
488 | double val = 0; | |
489 | ||
490 | memcpy (&val, from, floatformat_totalsize_bytes (fmt)); | |
491 | *to = val; | |
492 | return; | |
493 | } | |
494 | else if (fmt == host_long_double_format) | |
495 | { | |
496 | long double val = 0; | |
497 | ||
498 | memcpy (&val, from, floatformat_totalsize_bytes (fmt)); | |
499 | *to = val; | |
500 | return; | |
501 | } | |
502 | ||
503 | unsigned char *ufrom = (unsigned char *) from; | |
504 | DOUBLEST dto; | |
505 | long exponent; | |
506 | unsigned long mant; | |
507 | unsigned int mant_bits, mant_off; | |
508 | int mant_bits_left; | |
509 | int special_exponent; /* It's a NaN, denorm or zero. */ | |
510 | enum floatformat_byteorders order; | |
511 | unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES]; | |
512 | enum float_kind kind; | |
513 | ||
514 | gdb_assert (fmt->totalsize | |
515 | <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT); | |
516 | ||
517 | /* For non-numbers, reuse libiberty's logic to find the correct | |
518 | format. We do not lose any precision in this case by passing | |
519 | through a double. */ | |
520 | kind = floatformat_classify (fmt, (const bfd_byte *) from); | |
521 | if (kind == float_infinite || kind == float_nan) | |
522 | { | |
523 | double dto; | |
524 | ||
525 | floatformat_to_double (fmt->split_half ? fmt->split_half : fmt, | |
526 | from, &dto); | |
527 | *to = (DOUBLEST) dto; | |
528 | return; | |
529 | } | |
530 | ||
531 | order = floatformat_normalize_byteorder (fmt, ufrom, newfrom); | |
532 | ||
533 | if (order != fmt->byteorder) | |
534 | ufrom = newfrom; | |
535 | ||
536 | if (fmt->split_half) | |
537 | { | |
538 | DOUBLEST dtop, dbot; | |
539 | ||
540 | floatformat_to_doublest (fmt->split_half, ufrom, &dtop); | |
541 | /* Preserve the sign of 0, which is the sign of the top | |
542 | half. */ | |
543 | if (dtop == 0.0) | |
544 | { | |
545 | *to = dtop; | |
546 | return; | |
547 | } | |
548 | floatformat_to_doublest (fmt->split_half, | |
549 | ufrom + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2, | |
550 | &dbot); | |
551 | *to = dtop + dbot; | |
552 | return; | |
553 | } | |
554 | ||
555 | exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start, | |
556 | fmt->exp_len); | |
557 | /* Note that if exponent indicates a NaN, we can't really do anything useful | |
558 | (not knowing if the host has NaN's, or how to build one). So it will | |
559 | end up as an infinity or something close; that is OK. */ | |
560 | ||
561 | mant_bits_left = fmt->man_len; | |
562 | mant_off = fmt->man_start; | |
563 | dto = 0.0; | |
564 | ||
565 | special_exponent = exponent == 0 || exponent == fmt->exp_nan; | |
566 | ||
567 | /* Don't bias NaNs. Use minimum exponent for denorms. For | |
568 | simplicity, we don't check for zero as the exponent doesn't matter. | |
569 | Note the cast to int; exp_bias is unsigned, so it's important to | |
570 | make sure the operation is done in signed arithmetic. */ | |
571 | if (!special_exponent) | |
572 | exponent -= fmt->exp_bias; | |
573 | else if (exponent == 0) | |
574 | exponent = 1 - fmt->exp_bias; | |
575 | ||
576 | /* Build the result algebraically. Might go infinite, underflow, etc; | |
577 | who cares. */ | |
578 | ||
579 | /* If this format uses a hidden bit, explicitly add it in now. Otherwise, | |
580 | increment the exponent by one to account for the integer bit. */ | |
581 | ||
582 | if (!special_exponent) | |
583 | { | |
584 | if (fmt->intbit == floatformat_intbit_no) | |
585 | dto = ldexp (1.0, exponent); | |
586 | else | |
587 | exponent++; | |
588 | } | |
589 | ||
590 | while (mant_bits_left > 0) | |
591 | { | |
592 | mant_bits = std::min (mant_bits_left, 32); | |
593 | ||
594 | mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits); | |
595 | ||
596 | dto += ldexp ((double) mant, exponent - mant_bits); | |
597 | exponent -= mant_bits; | |
598 | mant_off += mant_bits; | |
599 | mant_bits_left -= mant_bits; | |
600 | } | |
601 | ||
602 | /* Negate it if negative. */ | |
603 | if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1)) | |
604 | dto = -dto; | |
605 | *to = dto; | |
606 | } | |
607 | ||
608 | /* Convert the DOUBLEST *FROM to an extended float in format FMT and | |
609 | store where TO points. */ | |
610 | static void | |
611 | floatformat_from_doublest (const struct floatformat *fmt, | |
612 | const DOUBLEST *from, void *to) | |
613 | { | |
614 | gdb_assert (fmt != NULL); | |
615 | ||
616 | if (fmt == host_float_format) | |
617 | { | |
618 | float val = *from; | |
619 | ||
620 | memcpy (to, &val, floatformat_totalsize_bytes (fmt)); | |
621 | return; | |
622 | } | |
623 | else if (fmt == host_double_format) | |
624 | { | |
625 | double val = *from; | |
626 | ||
627 | memcpy (to, &val, floatformat_totalsize_bytes (fmt)); | |
628 | return; | |
629 | } | |
630 | else if (fmt == host_long_double_format) | |
631 | { | |
632 | long double val = *from; | |
633 | ||
634 | memcpy (to, &val, floatformat_totalsize_bytes (fmt)); | |
635 | return; | |
636 | } | |
637 | ||
638 | DOUBLEST dfrom; | |
639 | int exponent; | |
640 | DOUBLEST mant; | |
641 | unsigned int mant_bits, mant_off; | |
642 | int mant_bits_left; | |
643 | unsigned char *uto = (unsigned char *) to; | |
644 | enum floatformat_byteorders order = fmt->byteorder; | |
645 | unsigned char newto[FLOATFORMAT_LARGEST_BYTES]; | |
646 | ||
647 | if (order != floatformat_little) | |
648 | order = floatformat_big; | |
649 | ||
650 | if (order != fmt->byteorder) | |
651 | uto = newto; | |
652 | ||
653 | memcpy (&dfrom, from, sizeof (dfrom)); | |
654 | memset (uto, 0, floatformat_totalsize_bytes (fmt)); | |
655 | ||
656 | if (fmt->split_half) | |
657 | { | |
658 | /* Use static volatile to ensure that any excess precision is | |
659 | removed via storing in memory, and so the top half really is | |
660 | the result of converting to double. */ | |
661 | static volatile double dtop, dbot; | |
662 | DOUBLEST dtopnv, dbotnv; | |
663 | ||
664 | dtop = (double) dfrom; | |
665 | /* If the rounded top half is Inf, the bottom must be 0 not NaN | |
666 | or Inf. */ | |
667 | if (dtop + dtop == dtop && dtop != 0.0) | |
668 | dbot = 0.0; | |
669 | else | |
670 | dbot = (double) (dfrom - (DOUBLEST) dtop); | |
671 | dtopnv = dtop; | |
672 | dbotnv = dbot; | |
673 | floatformat_from_doublest (fmt->split_half, &dtopnv, uto); | |
674 | floatformat_from_doublest (fmt->split_half, &dbotnv, | |
675 | (uto | |
676 | + fmt->totalsize / FLOATFORMAT_CHAR_BIT / 2)); | |
677 | return; | |
678 | } | |
679 | ||
680 | if (dfrom == 0) | |
681 | goto finalize_byteorder; /* Result is zero */ | |
682 | if (dfrom != dfrom) /* Result is NaN */ | |
683 | { | |
684 | /* From is NaN */ | |
685 | put_field (uto, order, fmt->totalsize, fmt->exp_start, | |
686 | fmt->exp_len, fmt->exp_nan); | |
687 | /* Be sure it's not infinity, but NaN value is irrel. */ | |
688 | put_field (uto, order, fmt->totalsize, fmt->man_start, | |
689 | fmt->man_len, 1); | |
690 | goto finalize_byteorder; | |
691 | } | |
692 | ||
693 | /* If negative, set the sign bit. */ | |
694 | if (dfrom < 0) | |
695 | { | |
696 | put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1); | |
697 | dfrom = -dfrom; | |
698 | } | |
699 | ||
700 | if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity. */ | |
701 | { | |
702 | /* Infinity exponent is same as NaN's. */ | |
703 | put_field (uto, order, fmt->totalsize, fmt->exp_start, | |
704 | fmt->exp_len, fmt->exp_nan); | |
705 | /* Infinity mantissa is all zeroes. */ | |
706 | put_field (uto, order, fmt->totalsize, fmt->man_start, | |
707 | fmt->man_len, 0); | |
708 | goto finalize_byteorder; | |
709 | } | |
710 | ||
711 | #ifdef HAVE_LONG_DOUBLE | |
712 | mant = frexpl (dfrom, &exponent); | |
713 | #else | |
714 | mant = frexp (dfrom, &exponent); | |
715 | #endif | |
716 | ||
717 | if (exponent + fmt->exp_bias <= 0) | |
718 | { | |
719 | /* The value is too small to be expressed in the destination | |
720 | type (not enough bits in the exponent. Treat as 0. */ | |
721 | put_field (uto, order, fmt->totalsize, fmt->exp_start, | |
722 | fmt->exp_len, 0); | |
723 | put_field (uto, order, fmt->totalsize, fmt->man_start, | |
724 | fmt->man_len, 0); | |
725 | goto finalize_byteorder; | |
726 | } | |
727 | ||
728 | if (exponent + fmt->exp_bias >= (1 << fmt->exp_len)) | |
729 | { | |
730 | /* The value is too large to fit into the destination. | |
731 | Treat as infinity. */ | |
732 | put_field (uto, order, fmt->totalsize, fmt->exp_start, | |
733 | fmt->exp_len, fmt->exp_nan); | |
734 | put_field (uto, order, fmt->totalsize, fmt->man_start, | |
735 | fmt->man_len, 0); | |
736 | goto finalize_byteorder; | |
737 | } | |
738 | ||
739 | put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len, | |
740 | exponent + fmt->exp_bias - 1); | |
741 | ||
742 | mant_bits_left = fmt->man_len; | |
743 | mant_off = fmt->man_start; | |
744 | while (mant_bits_left > 0) | |
745 | { | |
746 | unsigned long mant_long; | |
747 | ||
748 | mant_bits = mant_bits_left < 32 ? mant_bits_left : 32; | |
749 | ||
750 | mant *= 4294967296.0; | |
751 | mant_long = ((unsigned long) mant) & 0xffffffffL; | |
752 | mant -= mant_long; | |
753 | ||
754 | /* If the integer bit is implicit, then we need to discard it. | |
755 | If we are discarding a zero, we should be (but are not) creating | |
756 | a denormalized number which means adjusting the exponent | |
757 | (I think). */ | |
758 | if (mant_bits_left == fmt->man_len | |
759 | && fmt->intbit == floatformat_intbit_no) | |
760 | { | |
761 | mant_long <<= 1; | |
762 | mant_long &= 0xffffffffL; | |
763 | /* If we are processing the top 32 mantissa bits of a doublest | |
764 | so as to convert to a float value with implied integer bit, | |
765 | we will only be putting 31 of those 32 bits into the | |
766 | final value due to the discarding of the top bit. In the | |
767 | case of a small float value where the number of mantissa | |
768 | bits is less than 32, discarding the top bit does not alter | |
769 | the number of bits we will be adding to the result. */ | |
770 | if (mant_bits == 32) | |
771 | mant_bits -= 1; | |
772 | } | |
773 | ||
774 | if (mant_bits < 32) | |
775 | { | |
776 | /* The bits we want are in the most significant MANT_BITS bits of | |
777 | mant_long. Move them to the least significant. */ | |
778 | mant_long >>= 32 - mant_bits; | |
779 | } | |
780 | ||
781 | put_field (uto, order, fmt->totalsize, | |
782 | mant_off, mant_bits, mant_long); | |
783 | mant_off += mant_bits; | |
784 | mant_bits_left -= mant_bits; | |
785 | } | |
786 | ||
787 | finalize_byteorder: | |
788 | /* Do we need to byte-swap the words in the result? */ | |
789 | if (order != fmt->byteorder) | |
790 | floatformat_normalize_byteorder (fmt, newto, to); | |
791 | } | |
792 | ||
793 | /* Convert the byte-stream ADDR, interpreted as floating-point format FMT, | |
794 | to a string, optionally using the print format FORMAT. */ | |
795 | static std::string | |
796 | floatformat_to_string (const struct floatformat *fmt, | |
797 | const gdb_byte *in, const char *format) | |
798 | { | |
799 | /* Unless we need to adhere to a specific format, provide special | |
800 | output for certain cases. */ | |
801 | if (format == nullptr) | |
802 | { | |
803 | /* Detect invalid representations. */ | |
804 | if (!floatformat_is_valid (fmt, in)) | |
805 | return "<invalid float value>"; | |
806 | ||
807 | /* Handle NaN and Inf. */ | |
808 | enum float_kind kind = floatformat_classify (fmt, in); | |
809 | if (kind == float_nan) | |
810 | { | |
811 | const char *sign = floatformat_is_negative (fmt, in)? "-" : ""; | |
812 | const char *mantissa = floatformat_mantissa (fmt, in); | |
813 | return string_printf ("%snan(0x%s)", sign, mantissa); | |
814 | } | |
815 | else if (kind == float_infinite) | |
816 | { | |
817 | const char *sign = floatformat_is_negative (fmt, in)? "-" : ""; | |
818 | return string_printf ("%sinf", sign); | |
819 | } | |
820 | } | |
821 | ||
822 | /* Determine the format string to use on the host side. */ | |
823 | std::string host_format; | |
824 | char conversion; | |
825 | ||
826 | if (format == nullptr) | |
827 | { | |
828 | /* If no format was specified, print the number using a format string | |
829 | where the precision is set to the DECIMAL_DIG value for the given | |
830 | floating-point format. This value is computed as | |
831 | ||
832 | ceil(1 + p * log10(b)), | |
833 | ||
834 | where p is the precision of the floating-point format in bits, and | |
835 | b is the base (which is always 2 for the formats we support). */ | |
836 | const double log10_2 = .30102999566398119521; | |
837 | double d_decimal_dig = 1 + floatformat_precision (fmt) * log10_2; | |
838 | int decimal_dig = d_decimal_dig; | |
839 | if (decimal_dig < d_decimal_dig) | |
840 | decimal_dig++; | |
841 | ||
842 | host_format = string_printf ("%%.%d", decimal_dig); | |
843 | conversion = 'g'; | |
844 | } | |
845 | else | |
846 | { | |
847 | /* Use the specified format, stripping out the conversion character | |
848 | and length modifier, if present. */ | |
849 | size_t len = strlen (format); | |
850 | gdb_assert (len > 1); | |
851 | conversion = format[--len]; | |
852 | gdb_assert (conversion == 'e' || conversion == 'f' || conversion == 'g' | |
853 | || conversion == 'E' || conversion == 'G'); | |
854 | if (format[len - 1] == 'L') | |
855 | len--; | |
856 | ||
857 | host_format = std::string (format, len); | |
858 | } | |
859 | ||
860 | /* Add the length modifier and conversion character appropriate for | |
861 | handling the host DOUBLEST type. */ | |
862 | #ifdef HAVE_LONG_DOUBLE | |
863 | host_format += 'L'; | |
864 | #endif | |
865 | host_format += conversion; | |
866 | ||
867 | DOUBLEST doub; | |
868 | floatformat_to_doublest (fmt, in, &doub); | |
869 | return string_printf (host_format.c_str (), doub); | |
870 | } | |
871 | ||
872 | /* Parse string STRING into a target floating-number of format FMT and | |
873 | store it as byte-stream ADDR. Return whether parsing succeeded. */ | |
874 | static bool | |
875 | floatformat_from_string (const struct floatformat *fmt, gdb_byte *out, | |
876 | const std::string &in) | |
877 | { | |
878 | DOUBLEST doub; | |
879 | int n, num; | |
880 | #ifdef HAVE_LONG_DOUBLE | |
881 | const char *scan_format = "%Lg%n"; | |
882 | #else | |
883 | const char *scan_format = "%lg%n"; | |
884 | #endif | |
885 | num = sscanf (in.c_str (), scan_format, &doub, &n); | |
886 | ||
887 | /* The sscanf man page suggests not making any assumptions on the effect | |
888 | of %n on the result, so we don't. | |
889 | That is why we simply test num == 0. */ | |
890 | if (num == 0) | |
891 | return false; | |
892 | ||
893 | /* We only accept the whole string. */ | |
894 | if (in[n]) | |
895 | return false; | |
896 | ||
897 | floatformat_from_doublest (fmt, &doub, out); | |
898 | return true; | |
899 | } | |
900 | ||
50637b26 UW |
901 | /* Convert the byte-stream ADDR, interpreted as floating-point format FMT, |
902 | to an integer value (rounding towards zero). */ | |
903 | static LONGEST | |
904 | floatformat_to_longest (const struct floatformat *fmt, const gdb_byte *addr) | |
905 | { | |
906 | DOUBLEST d; | |
907 | floatformat_to_doublest (fmt, addr, &d); | |
908 | return (LONGEST) d; | |
909 | } | |
910 | ||
911 | /* Convert signed integer VAL to a target floating-number of format FMT | |
912 | and store it as byte-stream ADDR. */ | |
913 | static void | |
914 | floatformat_from_longest (const struct floatformat *fmt, gdb_byte *addr, | |
915 | LONGEST val) | |
916 | { | |
917 | DOUBLEST d = (DOUBLEST) val; | |
918 | floatformat_from_doublest (fmt, &d, addr); | |
919 | } | |
920 | ||
921 | /* Convert unsigned integer VAL to a target floating-number of format FMT | |
922 | and store it as byte-stream ADDR. */ | |
923 | static void | |
924 | floatformat_from_ulongest (const struct floatformat *fmt, gdb_byte *addr, | |
925 | ULONGEST val) | |
926 | { | |
927 | DOUBLEST d = (DOUBLEST) val; | |
928 | floatformat_from_doublest (fmt, &d, addr); | |
929 | } | |
930 | ||
14ad9311 UW |
931 | /* Convert the byte-stream ADDR, interpreted as floating-point format FMT, |
932 | to a floating-point value in the host "double" format. */ | |
933 | static double | |
934 | floatformat_to_host_double (const struct floatformat *fmt, | |
935 | const gdb_byte *addr) | |
936 | { | |
937 | DOUBLEST d; | |
938 | floatformat_to_doublest (fmt, addr, &d); | |
939 | return (double) d; | |
940 | } | |
941 | ||
942 | /* Convert floating-point value VAL in the host "double" format to a target | |
943 | floating-number of format FMT and store it as byte-stream ADDR. */ | |
944 | static void | |
945 | floatformat_from_host_double (const struct floatformat *fmt, gdb_byte *addr, | |
946 | double val) | |
947 | { | |
948 | DOUBLEST d = (DOUBLEST) val; | |
949 | floatformat_from_doublest (fmt, &d, addr); | |
950 | } | |
951 | ||
50637b26 UW |
952 | /* Convert a floating-point number of format FROM_FMT from the target |
953 | byte-stream FROM to a floating-point number of format TO_FMT, and | |
954 | store it to the target byte-stream TO. */ | |
955 | static void | |
956 | floatformat_convert (const gdb_byte *from, const struct floatformat *from_fmt, | |
957 | gdb_byte *to, const struct floatformat *to_fmt) | |
958 | { | |
959 | if (from_fmt == to_fmt) | |
960 | { | |
961 | /* The floating-point formats match, so we simply copy the data. */ | |
962 | memcpy (to, from, floatformat_totalsize_bytes (to_fmt)); | |
963 | } | |
964 | else | |
965 | { | |
966 | /* The floating-point formats don't match. The best we can do | |
967 | (apart from simulating the target FPU) is converting to the | |
968 | widest floating-point type supported by the host, and then | |
969 | again to the desired type. */ | |
970 | DOUBLEST d; | |
971 | ||
972 | floatformat_to_doublest (from_fmt, from, &d); | |
973 | floatformat_from_doublest (to_fmt, &d, to); | |
974 | } | |
975 | } | |
976 | ||
66c02b9e UW |
977 | /* Perform the binary operation indicated by OPCODE, using as operands the |
978 | target byte streams X and Y, interpreted as floating-point numbers of | |
979 | formats FMT_X and FMT_Y, respectively. Convert the result to format | |
980 | FMT_RES and store it into the byte-stream RES. */ | |
981 | static void | |
982 | floatformat_binop (enum exp_opcode op, | |
983 | const struct floatformat *fmt_x, const gdb_byte *x, | |
984 | const struct floatformat *fmt_y, const gdb_byte *y, | |
985 | const struct floatformat *fmt_result, gdb_byte *result) | |
986 | { | |
987 | DOUBLEST v1, v2, v = 0; | |
988 | ||
989 | floatformat_to_doublest (fmt_x, x, &v1); | |
990 | floatformat_to_doublest (fmt_y, y, &v2); | |
991 | ||
992 | switch (op) | |
993 | { | |
994 | case BINOP_ADD: | |
995 | v = v1 + v2; | |
996 | break; | |
997 | ||
998 | case BINOP_SUB: | |
999 | v = v1 - v2; | |
1000 | break; | |
1001 | ||
1002 | case BINOP_MUL: | |
1003 | v = v1 * v2; | |
1004 | break; | |
1005 | ||
1006 | case BINOP_DIV: | |
1007 | v = v1 / v2; | |
1008 | break; | |
1009 | ||
1010 | case BINOP_EXP: | |
1011 | errno = 0; | |
1012 | v = pow (v1, v2); | |
1013 | if (errno) | |
1014 | error (_("Cannot perform exponentiation: %s"), | |
1015 | safe_strerror (errno)); | |
1016 | break; | |
1017 | ||
1018 | case BINOP_MIN: | |
1019 | v = v1 < v2 ? v1 : v2; | |
1020 | break; | |
1021 | ||
1022 | case BINOP_MAX: | |
1023 | v = v1 > v2 ? v1 : v2; | |
1024 | break; | |
1025 | ||
1026 | default: | |
1027 | error (_("Integer-only operation on floating point number.")); | |
1028 | break; | |
1029 | } | |
1030 | ||
1031 | floatformat_from_doublest (fmt_result, &v, result); | |
1032 | } | |
1033 | ||
1034 | /* Compare the two target byte streams X and Y, interpreted as floating-point | |
1035 | numbers of formats FMT_X and FMT_Y, respectively. Return zero if X and Y | |
1036 | are equal, -1 if X is less than Y, and 1 otherwise. */ | |
1037 | static int | |
1038 | floatformat_compare (const struct floatformat *fmt_x, const gdb_byte *x, | |
1039 | const struct floatformat *fmt_y, const gdb_byte *y) | |
1040 | { | |
1041 | DOUBLEST v1, v2; | |
1042 | ||
1043 | floatformat_to_doublest (fmt_x, x, &v1); | |
1044 | floatformat_to_doublest (fmt_y, y, &v2); | |
1045 | ||
1046 | if (v1 == v2) | |
1047 | return 0; | |
1048 | if (v1 < v2) | |
1049 | return -1; | |
1050 | return 1; | |
1051 | } | |
1052 | ||
50637b26 | 1053 | |
1cfb73db UW |
1054 | /* Helper routines operating on decimal floating-point data. */ |
1055 | ||
1056 | /* Decimal floating point is one of the extension to IEEE 754, which is | |
1057 | described in http://grouper.ieee.org/groups/754/revision.html and | |
1058 | http://www2.hursley.ibm.com/decimal/. It completes binary floating | |
1059 | point by representing floating point more exactly. */ | |
1060 | ||
1061 | /* The order of the following headers is important for making sure | |
1062 | decNumber structure is large enough to hold decimal128 digits. */ | |
1063 | ||
1064 | #include "dpd/decimal128.h" | |
1065 | #include "dpd/decimal64.h" | |
1066 | #include "dpd/decimal32.h" | |
1067 | ||
1068 | /* When using decimal128, this is the maximum string length + 1 | |
1069 | (value comes from libdecnumber's DECIMAL128_String constant). */ | |
1070 | #define MAX_DECIMAL_STRING 43 | |
1071 | ||
1072 | /* In GDB, we are using an array of gdb_byte to represent decimal values. | |
1073 | They are stored in host byte order. This routine does the conversion if | |
1074 | the target byte order is different. */ | |
1075 | static void | |
1076 | match_endianness (const gdb_byte *from, int len, enum bfd_endian byte_order, | |
1077 | gdb_byte *to) | |
1078 | { | |
1079 | int i; | |
1080 | ||
1081 | #if WORDS_BIGENDIAN | |
1082 | #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_LITTLE | |
1083 | #else | |
1084 | #define OPPOSITE_BYTE_ORDER BFD_ENDIAN_BIG | |
1085 | #endif | |
1086 | ||
1087 | if (byte_order == OPPOSITE_BYTE_ORDER) | |
1088 | for (i = 0; i < len; i++) | |
1089 | to[i] = from[len - i - 1]; | |
1090 | else | |
1091 | for (i = 0; i < len; i++) | |
1092 | to[i] = from[i]; | |
1093 | ||
1094 | return; | |
1095 | } | |
1096 | ||
1097 | /* Helper function to get the appropriate libdecnumber context for each size | |
1098 | of decimal float. */ | |
1099 | static void | |
1100 | set_decnumber_context (decContext *ctx, int len) | |
1101 | { | |
1102 | switch (len) | |
1103 | { | |
1104 | case 4: | |
1105 | decContextDefault (ctx, DEC_INIT_DECIMAL32); | |
1106 | break; | |
1107 | case 8: | |
1108 | decContextDefault (ctx, DEC_INIT_DECIMAL64); | |
1109 | break; | |
1110 | case 16: | |
1111 | decContextDefault (ctx, DEC_INIT_DECIMAL128); | |
1112 | break; | |
1113 | } | |
1114 | ||
1115 | ctx->traps = 0; | |
1116 | } | |
1117 | ||
1118 | /* Check for errors signaled in the decimal context structure. */ | |
1119 | static void | |
1120 | decimal_check_errors (decContext *ctx) | |
1121 | { | |
1122 | /* An error here could be a division by zero, an overflow, an underflow or | |
1123 | an invalid operation (from the DEC_Errors constant in decContext.h). | |
1124 | Since GDB doesn't complain about division by zero, overflow or underflow | |
1125 | errors for binary floating, we won't complain about them for decimal | |
1126 | floating either. */ | |
1127 | if (ctx->status & DEC_IEEE_854_Invalid_operation) | |
1128 | { | |
1129 | /* Leave only the error bits in the status flags. */ | |
1130 | ctx->status &= DEC_IEEE_854_Invalid_operation; | |
1131 | error (_("Cannot perform operation: %s"), | |
1132 | decContextStatusToString (ctx)); | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | /* Helper function to convert from libdecnumber's appropriate representation | |
1137 | for computation to each size of decimal float. */ | |
1138 | static void | |
1139 | decimal_from_number (const decNumber *from, gdb_byte *to, int len) | |
1140 | { | |
1141 | decContext set; | |
1142 | ||
1143 | set_decnumber_context (&set, len); | |
1144 | ||
1145 | switch (len) | |
1146 | { | |
1147 | case 4: | |
1148 | decimal32FromNumber ((decimal32 *) to, from, &set); | |
1149 | break; | |
1150 | case 8: | |
1151 | decimal64FromNumber ((decimal64 *) to, from, &set); | |
1152 | break; | |
1153 | case 16: | |
1154 | decimal128FromNumber ((decimal128 *) to, from, &set); | |
1155 | break; | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | /* Helper function to convert each size of decimal float to libdecnumber's | |
1160 | appropriate representation for computation. */ | |
1161 | static void | |
1162 | decimal_to_number (const gdb_byte *from, int len, decNumber *to) | |
1163 | { | |
1164 | switch (len) | |
1165 | { | |
1166 | case 4: | |
1167 | decimal32ToNumber ((decimal32 *) from, to); | |
1168 | break; | |
1169 | case 8: | |
1170 | decimal64ToNumber ((decimal64 *) from, to); | |
1171 | break; | |
1172 | case 16: | |
1173 | decimal128ToNumber ((decimal128 *) from, to); | |
1174 | break; | |
1175 | default: | |
1176 | error (_("Unknown decimal floating point type.")); | |
1177 | break; | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | /* Convert decimal type to its string representation. LEN is the length | |
1182 | of the decimal type, 4 bytes for decimal32, 8 bytes for decimal64 and | |
1183 | 16 bytes for decimal128. */ | |
1184 | static std::string | |
1185 | decimal_to_string (const gdb_byte *decbytes, int len, | |
1186 | enum bfd_endian byte_order, const char *format = nullptr) | |
1187 | { | |
1188 | gdb_byte dec[16]; | |
1189 | ||
1190 | match_endianness (decbytes, len, byte_order, dec); | |
1191 | ||
1192 | if (format != nullptr) | |
1193 | { | |
1194 | /* We don't handle format strings (yet). If the host printf supports | |
1195 | decimal floating point types, just use this. Otherwise, fall back | |
1196 | to printing the number while ignoring the format string. */ | |
1197 | #if defined (PRINTF_HAS_DECFLOAT) | |
1198 | /* FIXME: This makes unwarranted assumptions about the host ABI! */ | |
1199 | return string_printf (format, dec); | |
1200 | #endif | |
1201 | } | |
1202 | ||
1203 | std::string result; | |
1204 | result.resize (MAX_DECIMAL_STRING); | |
1205 | ||
1206 | switch (len) | |
1207 | { | |
1208 | case 4: | |
1209 | decimal32ToString ((decimal32 *) dec, &result[0]); | |
1210 | break; | |
1211 | case 8: | |
1212 | decimal64ToString ((decimal64 *) dec, &result[0]); | |
1213 | break; | |
1214 | case 16: | |
1215 | decimal128ToString ((decimal128 *) dec, &result[0]); | |
1216 | break; | |
1217 | default: | |
1218 | error (_("Unknown decimal floating point type.")); | |
1219 | break; | |
1220 | } | |
1221 | ||
1222 | return result; | |
1223 | } | |
1224 | ||
1225 | /* Convert the string form of a decimal value to its decimal representation. | |
1226 | LEN is the length of the decimal type, 4 bytes for decimal32, 8 bytes for | |
1227 | decimal64 and 16 bytes for decimal128. */ | |
1228 | static bool | |
1229 | decimal_from_string (gdb_byte *decbytes, int len, enum bfd_endian byte_order, | |
1230 | const std::string &string) | |
1231 | { | |
1232 | decContext set; | |
1233 | gdb_byte dec[16]; | |
1234 | ||
1235 | set_decnumber_context (&set, len); | |
1236 | ||
1237 | switch (len) | |
1238 | { | |
1239 | case 4: | |
1240 | decimal32FromString ((decimal32 *) dec, string.c_str (), &set); | |
1241 | break; | |
1242 | case 8: | |
1243 | decimal64FromString ((decimal64 *) dec, string.c_str (), &set); | |
1244 | break; | |
1245 | case 16: | |
1246 | decimal128FromString ((decimal128 *) dec, string.c_str (), &set); | |
1247 | break; | |
1248 | default: | |
1249 | error (_("Unknown decimal floating point type.")); | |
1250 | break; | |
1251 | } | |
1252 | ||
1253 | match_endianness (dec, len, byte_order, decbytes); | |
1254 | ||
1255 | /* Check for errors in the DFP operation. */ | |
1256 | decimal_check_errors (&set); | |
1257 | ||
1258 | return true; | |
1259 | } | |
1260 | ||
1261 | /* Converts a LONGEST to a decimal float of specified LEN bytes. */ | |
1262 | static void | |
1263 | decimal_from_longest (LONGEST from, | |
1264 | gdb_byte *to, int len, enum bfd_endian byte_order) | |
1265 | { | |
1266 | gdb_byte dec[16]; | |
1267 | decNumber number; | |
1268 | if ((int32_t) from != from) | |
1269 | /* libdecnumber can convert only 32-bit integers. */ | |
1270 | error (_("Conversion of large integer to a " | |
1271 | "decimal floating type is not supported.")); | |
1272 | ||
1273 | decNumberFromInt32 (&number, (int32_t) from); | |
1274 | ||
1275 | decimal_from_number (&number, dec, len); | |
1276 | match_endianness (dec, len, byte_order, to); | |
1277 | } | |
1278 | ||
1279 | /* Converts a ULONGEST to a decimal float of specified LEN bytes. */ | |
1280 | static void | |
1281 | decimal_from_ulongest (ULONGEST from, | |
1282 | gdb_byte *to, int len, enum bfd_endian byte_order) | |
1283 | { | |
1284 | gdb_byte dec[16]; | |
1285 | decNumber number; | |
1286 | ||
1287 | if ((uint32_t) from != from) | |
1288 | /* libdecnumber can convert only 32-bit integers. */ | |
1289 | error (_("Conversion of large integer to a " | |
1290 | "decimal floating type is not supported.")); | |
1291 | ||
1292 | decNumberFromUInt32 (&number, (uint32_t) from); | |
1293 | ||
1294 | decimal_from_number (&number, dec, len); | |
1295 | match_endianness (dec, len, byte_order, to); | |
1296 | } | |
1297 | ||
1298 | /* Converts a decimal float of LEN bytes to a LONGEST. */ | |
1299 | static LONGEST | |
1300 | decimal_to_longest (const gdb_byte *from, int len, enum bfd_endian byte_order) | |
1301 | { | |
1302 | /* libdecnumber has a function to convert from decimal to integer, but | |
1303 | it doesn't work when the decimal number has a fractional part. */ | |
1304 | std::string str = decimal_to_string (from, len, byte_order); | |
1305 | return strtoll (str.c_str (), NULL, 10); | |
1306 | } | |
1307 | ||
1308 | /* Perform operation OP with operands X and Y with sizes LEN_X and LEN_Y | |
1309 | and byte orders BYTE_ORDER_X and BYTE_ORDER_Y, and store value in | |
1310 | RESULT with size LEN_RESULT and byte order BYTE_ORDER_RESULT. */ | |
1311 | static void | |
1312 | decimal_binop (enum exp_opcode op, | |
1313 | const gdb_byte *x, int len_x, enum bfd_endian byte_order_x, | |
1314 | const gdb_byte *y, int len_y, enum bfd_endian byte_order_y, | |
1315 | gdb_byte *result, int len_result, | |
1316 | enum bfd_endian byte_order_result) | |
1317 | { | |
1318 | decContext set; | |
1319 | decNumber number1, number2, number3; | |
1320 | gdb_byte dec1[16], dec2[16], dec3[16]; | |
1321 | ||
1322 | match_endianness (x, len_x, byte_order_x, dec1); | |
1323 | match_endianness (y, len_y, byte_order_y, dec2); | |
1324 | ||
1325 | decimal_to_number (dec1, len_x, &number1); | |
1326 | decimal_to_number (dec2, len_y, &number2); | |
1327 | ||
1328 | set_decnumber_context (&set, len_result); | |
1329 | ||
1330 | switch (op) | |
1331 | { | |
1332 | case BINOP_ADD: | |
1333 | decNumberAdd (&number3, &number1, &number2, &set); | |
1334 | break; | |
1335 | case BINOP_SUB: | |
1336 | decNumberSubtract (&number3, &number1, &number2, &set); | |
1337 | break; | |
1338 | case BINOP_MUL: | |
1339 | decNumberMultiply (&number3, &number1, &number2, &set); | |
1340 | break; | |
1341 | case BINOP_DIV: | |
1342 | decNumberDivide (&number3, &number1, &number2, &set); | |
1343 | break; | |
1344 | case BINOP_EXP: | |
1345 | decNumberPower (&number3, &number1, &number2, &set); | |
1346 | break; | |
1347 | default: | |
1348 | error (_("Operation not valid for decimal floating point number.")); | |
1349 | break; | |
1350 | } | |
1351 | ||
1352 | /* Check for errors in the DFP operation. */ | |
1353 | decimal_check_errors (&set); | |
1354 | ||
1355 | decimal_from_number (&number3, dec3, len_result); | |
1356 | ||
1357 | match_endianness (dec3, len_result, byte_order_result, result); | |
1358 | } | |
1359 | ||
1360 | /* Returns true if X (which is LEN bytes wide) is the number zero. */ | |
1361 | static int | |
1362 | decimal_is_zero (const gdb_byte *x, int len, enum bfd_endian byte_order) | |
1363 | { | |
1364 | decNumber number; | |
1365 | gdb_byte dec[16]; | |
1366 | ||
1367 | match_endianness (x, len, byte_order, dec); | |
1368 | decimal_to_number (dec, len, &number); | |
1369 | ||
1370 | return decNumberIsZero (&number); | |
1371 | } | |
1372 | ||
1373 | /* Compares two numbers numerically. If X is less than Y then the return value | |
1374 | will be -1. If they are equal, then the return value will be 0. If X is | |
1375 | greater than the Y then the return value will be 1. */ | |
1376 | static int | |
1377 | decimal_compare (const gdb_byte *x, int len_x, enum bfd_endian byte_order_x, | |
1378 | const gdb_byte *y, int len_y, enum bfd_endian byte_order_y) | |
1379 | { | |
1380 | decNumber number1, number2, result; | |
1381 | decContext set; | |
1382 | gdb_byte dec1[16], dec2[16]; | |
1383 | int len_result; | |
1384 | ||
1385 | match_endianness (x, len_x, byte_order_x, dec1); | |
1386 | match_endianness (y, len_y, byte_order_y, dec2); | |
1387 | ||
1388 | decimal_to_number (dec1, len_x, &number1); | |
1389 | decimal_to_number (dec2, len_y, &number2); | |
1390 | ||
1391 | /* Perform the comparison in the larger of the two sizes. */ | |
1392 | len_result = len_x > len_y ? len_x : len_y; | |
1393 | set_decnumber_context (&set, len_result); | |
1394 | ||
1395 | decNumberCompare (&result, &number1, &number2, &set); | |
1396 | ||
1397 | /* Check for errors in the DFP operation. */ | |
1398 | decimal_check_errors (&set); | |
1399 | ||
1400 | if (decNumberIsNaN (&result)) | |
1401 | error (_("Comparison with an invalid number (NaN).")); | |
1402 | else if (decNumberIsZero (&result)) | |
1403 | return 0; | |
1404 | else if (decNumberIsNegative (&result)) | |
1405 | return -1; | |
1406 | else | |
1407 | return 1; | |
1408 | } | |
1409 | ||
1410 | /* Convert a decimal value from a decimal type with LEN_FROM bytes to a | |
1411 | decimal type with LEN_TO bytes. */ | |
1412 | static void | |
1413 | decimal_convert (const gdb_byte *from, int len_from, | |
1414 | enum bfd_endian byte_order_from, gdb_byte *to, int len_to, | |
1415 | enum bfd_endian byte_order_to) | |
1416 | { | |
1417 | decNumber number; | |
1418 | gdb_byte dec[16]; | |
1419 | ||
1420 | match_endianness (from, len_from, byte_order_from, dec); | |
1421 | ||
1422 | decimal_to_number (dec, len_from, &number); | |
1423 | decimal_from_number (&number, dec, len_to); | |
1424 | ||
1425 | match_endianness (dec, len_to, byte_order_to, to); | |
1426 | } | |
1427 | ||
1428 | ||
70100014 UW |
1429 | /* Typed floating-point routines. These routines operate on floating-point |
1430 | values in target format, represented by a byte buffer interpreted as a | |
1431 | "struct type", which may be either a binary or decimal floating-point | |
1432 | type (TYPE_CODE_FLT or TYPE_CODE_DECFLOAT). */ | |
1433 | ||
1434 | /* Return whether the byte-stream ADDR holds a valid value of | |
1435 | floating-point type TYPE. */ | |
1436 | bool | |
1437 | target_float_is_valid (const gdb_byte *addr, const struct type *type) | |
1438 | { | |
1439 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1440 | return floatformat_is_valid (floatformat_from_type (type), addr); | |
1441 | ||
1442 | if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1443 | return true; | |
1444 | ||
1445 | gdb_assert_not_reached ("unexpected type code"); | |
1446 | } | |
1447 | ||
1448 | /* Return whether the byte-stream ADDR, interpreted as floating-point | |
1449 | type TYPE, is numerically equal to zero (of either sign). */ | |
1450 | bool | |
1451 | target_float_is_zero (const gdb_byte *addr, const struct type *type) | |
1452 | { | |
1453 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1454 | return (floatformat_classify (floatformat_from_type (type), addr) | |
1455 | == float_zero); | |
1456 | ||
1457 | if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1458 | return decimal_is_zero (addr, TYPE_LENGTH (type), | |
1459 | gdbarch_byte_order (get_type_arch (type))); | |
1460 | ||
1461 | gdb_assert_not_reached ("unexpected type code"); | |
1462 | } | |
1463 | ||
f69fdf9b UW |
1464 | /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, |
1465 | to a string, optionally using the print format FORMAT. */ | |
1466 | std::string | |
1467 | target_float_to_string (const gdb_byte *addr, const struct type *type, | |
1468 | const char *format) | |
1469 | { | |
1470 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1471 | return floatformat_to_string (floatformat_from_type (type), addr, format); | |
1472 | ||
1473 | if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1474 | return decimal_to_string (addr, TYPE_LENGTH (type), | |
1475 | gdbarch_byte_order (get_type_arch (type)), | |
1476 | format); | |
1477 | ||
1478 | gdb_assert_not_reached ("unexpected type code"); | |
1479 | } | |
1480 | ||
1481 | /* Parse string STRING into a target floating-number of type TYPE and | |
1482 | store it as byte-stream ADDR. Return whether parsing succeeded. */ | |
1483 | bool | |
1484 | target_float_from_string (gdb_byte *addr, const struct type *type, | |
1485 | const std::string &string) | |
1486 | { | |
1487 | /* Ensure possible padding bytes in the target buffer are zeroed out. */ | |
1488 | memset (addr, 0, TYPE_LENGTH (type)); | |
1489 | ||
1490 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1491 | return floatformat_from_string (floatformat_from_type (type), addr, | |
1492 | string); | |
1493 | ||
1494 | if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1495 | return decimal_from_string (addr, TYPE_LENGTH (type), | |
1496 | gdbarch_byte_order (get_type_arch (type)), | |
1497 | string); | |
1498 | ||
1499 | gdb_assert_not_reached ("unexpected type code"); | |
1500 | } | |
50637b26 UW |
1501 | |
1502 | /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, | |
1503 | to an integer value (rounding towards zero). */ | |
1504 | LONGEST | |
1505 | target_float_to_longest (const gdb_byte *addr, const struct type *type) | |
1506 | { | |
1507 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1508 | return floatformat_to_longest (floatformat_from_type (type), addr); | |
1509 | ||
1510 | if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1511 | return decimal_to_longest (addr, TYPE_LENGTH (type), | |
1512 | gdbarch_byte_order (get_type_arch (type))); | |
1513 | ||
1514 | gdb_assert_not_reached ("unexpected type code"); | |
1515 | } | |
1516 | ||
1517 | /* Convert signed integer VAL to a target floating-number of type TYPE | |
1518 | and store it as byte-stream ADDR. */ | |
1519 | void | |
1520 | target_float_from_longest (gdb_byte *addr, const struct type *type, | |
1521 | LONGEST val) | |
1522 | { | |
1523 | /* Ensure possible padding bytes in the target buffer are zeroed out. */ | |
1524 | memset (addr, 0, TYPE_LENGTH (type)); | |
1525 | ||
1526 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1527 | { | |
1528 | floatformat_from_longest (floatformat_from_type (type), addr, val); | |
1529 | return; | |
1530 | } | |
1531 | ||
1532 | if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1533 | { | |
1534 | decimal_from_longest (val, addr, TYPE_LENGTH (type), | |
1535 | gdbarch_byte_order (get_type_arch (type))); | |
1536 | return; | |
1537 | } | |
1538 | ||
1539 | gdb_assert_not_reached ("unexpected type code"); | |
1540 | } | |
1541 | ||
1542 | /* Convert unsigned integer VAL to a target floating-number of type TYPE | |
1543 | and store it as byte-stream ADDR. */ | |
1544 | void | |
1545 | target_float_from_ulongest (gdb_byte *addr, const struct type *type, | |
1546 | ULONGEST val) | |
1547 | { | |
1548 | /* Ensure possible padding bytes in the target buffer are zeroed out. */ | |
1549 | memset (addr, 0, TYPE_LENGTH (type)); | |
1550 | ||
1551 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1552 | { | |
1553 | floatformat_from_ulongest (floatformat_from_type (type), addr, val); | |
1554 | return; | |
1555 | } | |
1556 | ||
1557 | if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
1558 | { | |
1559 | decimal_from_ulongest (val, addr, TYPE_LENGTH (type), | |
1560 | gdbarch_byte_order (get_type_arch (type))); | |
1561 | return; | |
1562 | } | |
1563 | ||
1564 | gdb_assert_not_reached ("unexpected type code"); | |
1565 | } | |
1566 | ||
14ad9311 UW |
1567 | /* Convert the byte-stream ADDR, interpreted as floating-point type TYPE, |
1568 | to a floating-point value in the host "double" format. */ | |
1569 | double | |
1570 | target_float_to_host_double (const gdb_byte *addr, | |
1571 | const struct type *type) | |
1572 | { | |
1573 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1574 | return floatformat_to_host_double (floatformat_from_type (type), addr); | |
1575 | ||
1576 | /* We don't support conversions between target decimal floating-point | |
1577 | types and the host double type here. */ | |
1578 | ||
1579 | gdb_assert_not_reached ("unexpected type code"); | |
1580 | } | |
1581 | ||
1582 | /* Convert floating-point value VAL in the host "double" format to a target | |
1583 | floating-number of type TYPE and store it as byte-stream ADDR. */ | |
1584 | void | |
1585 | target_float_from_host_double (gdb_byte *addr, const struct type *type, | |
1586 | double val) | |
1587 | { | |
1588 | /* Ensure possible padding bytes in the target buffer are zeroed out. */ | |
1589 | memset (addr, 0, TYPE_LENGTH (type)); | |
1590 | ||
1591 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1592 | { | |
1593 | floatformat_from_host_double (floatformat_from_type (type), addr, val); | |
1594 | return; | |
1595 | } | |
1596 | ||
1597 | /* We don't support conversions between target decimal floating-point | |
1598 | types and the host double type here. */ | |
1599 | ||
1600 | gdb_assert_not_reached ("unexpected type code"); | |
1601 | } | |
1602 | ||
50637b26 UW |
1603 | /* Convert a floating-point number of type FROM_TYPE from the target |
1604 | byte-stream FROM to a floating-point number of type TO_TYPE, and | |
1605 | store it to the target byte-stream TO. */ | |
1606 | void | |
1607 | target_float_convert (const gdb_byte *from, const struct type *from_type, | |
1608 | gdb_byte *to, const struct type *to_type) | |
1609 | { | |
1610 | /* Ensure possible padding bytes in the target buffer are zeroed out. */ | |
1611 | memset (to, 0, TYPE_LENGTH (to_type)); | |
1612 | ||
1613 | /* Use direct conversion routines if we have them. */ | |
1614 | ||
1615 | if (TYPE_CODE (from_type) == TYPE_CODE_FLT | |
1616 | && TYPE_CODE (to_type) == TYPE_CODE_FLT) | |
1617 | { | |
1618 | floatformat_convert (from, floatformat_from_type (from_type), | |
1619 | to, floatformat_from_type (to_type)); | |
1620 | return; | |
1621 | } | |
1622 | ||
1623 | if (TYPE_CODE (from_type) == TYPE_CODE_DECFLOAT | |
1624 | && TYPE_CODE (to_type) == TYPE_CODE_DECFLOAT) | |
1625 | { | |
1626 | decimal_convert (from, TYPE_LENGTH (from_type), | |
1627 | gdbarch_byte_order (get_type_arch (from_type)), | |
1628 | to, TYPE_LENGTH (to_type), | |
1629 | gdbarch_byte_order (get_type_arch (to_type))); | |
1630 | return; | |
1631 | } | |
1632 | ||
1633 | /* We cannot directly convert between binary and decimal floating-point | |
1634 | types, so go via an intermediary string. */ | |
1635 | ||
1636 | if ((TYPE_CODE (from_type) == TYPE_CODE_FLT | |
1637 | && TYPE_CODE (to_type) == TYPE_CODE_DECFLOAT) | |
1638 | || (TYPE_CODE (from_type) == TYPE_CODE_DECFLOAT | |
1639 | && TYPE_CODE (to_type) == TYPE_CODE_FLT)) | |
1640 | { | |
1641 | std::string str = target_float_to_string (from, from_type); | |
1642 | target_float_from_string (to, to_type, str); | |
1643 | return; | |
1644 | } | |
1645 | ||
1646 | gdb_assert_not_reached ("unexpected type code"); | |
1647 | } | |
66c02b9e UW |
1648 | |
1649 | /* Perform the binary operation indicated by OPCODE, using as operands the | |
1650 | target byte streams X and Y, interpreted as floating-point numbers of | |
1651 | types TYPE_X and TYPE_Y, respectively. Convert the result to type | |
1652 | TYPE_RES and store it into the byte-stream RES. | |
1653 | ||
1654 | The three types must either be all binary floating-point types, or else | |
1655 | all decimal floating-point types. Binary and decimal floating-point | |
1656 | types cannot be mixed within a single operation. */ | |
1657 | void | |
1658 | target_float_binop (enum exp_opcode opcode, | |
1659 | const gdb_byte *x, const struct type *type_x, | |
1660 | const gdb_byte *y, const struct type *type_y, | |
1661 | gdb_byte *res, const struct type *type_res) | |
1662 | { | |
1663 | /* Ensure possible padding bytes in the target buffer are zeroed out. */ | |
1664 | memset (res, 0, TYPE_LENGTH (type_res)); | |
1665 | ||
1666 | if (TYPE_CODE (type_res) == TYPE_CODE_FLT) | |
1667 | { | |
1668 | gdb_assert (TYPE_CODE (type_x) == TYPE_CODE_FLT); | |
1669 | gdb_assert (TYPE_CODE (type_y) == TYPE_CODE_FLT); | |
1670 | return floatformat_binop (opcode, | |
1671 | floatformat_from_type (type_x), x, | |
1672 | floatformat_from_type (type_y), y, | |
1673 | floatformat_from_type (type_res), res); | |
1674 | } | |
1675 | ||
1676 | if (TYPE_CODE (type_res) == TYPE_CODE_DECFLOAT) | |
1677 | { | |
1678 | gdb_assert (TYPE_CODE (type_x) == TYPE_CODE_DECFLOAT); | |
1679 | gdb_assert (TYPE_CODE (type_y) == TYPE_CODE_DECFLOAT); | |
1680 | return decimal_binop (opcode, | |
1681 | x, TYPE_LENGTH (type_x), | |
1682 | gdbarch_byte_order (get_type_arch (type_x)), | |
1683 | y, TYPE_LENGTH (type_y), | |
1684 | gdbarch_byte_order (get_type_arch (type_y)), | |
1685 | res, TYPE_LENGTH (type_res), | |
1686 | gdbarch_byte_order (get_type_arch (type_res))); | |
1687 | } | |
1688 | ||
1689 | gdb_assert_not_reached ("unexpected type code"); | |
1690 | } | |
1691 | ||
1692 | /* Compare the two target byte streams X and Y, interpreted as floating-point | |
1693 | numbers of types TYPE_X and TYPE_Y, respectively. Return zero if X and Y | |
1694 | are equal, -1 if X is less than Y, and 1 otherwise. | |
1695 | ||
1696 | The two types must either both be binary floating-point types, or else | |
1697 | both be decimal floating-point types. Binary and decimal floating-point | |
1698 | types cannot compared directly against each other. */ | |
1699 | int | |
1700 | target_float_compare (const gdb_byte *x, const struct type *type_x, | |
1701 | const gdb_byte *y, const struct type *type_y) | |
1702 | { | |
1703 | if (TYPE_CODE (type_x) == TYPE_CODE_FLT) | |
1704 | { | |
1705 | gdb_assert (TYPE_CODE (type_y) == TYPE_CODE_FLT); | |
1706 | return floatformat_compare (floatformat_from_type (type_x), x, | |
1707 | floatformat_from_type (type_y), y); | |
1708 | } | |
1709 | ||
1710 | if (TYPE_CODE (type_x) == TYPE_CODE_DECFLOAT) | |
1711 | { | |
1712 | gdb_assert (TYPE_CODE (type_y) == TYPE_CODE_DECFLOAT); | |
1713 | return decimal_compare (x, TYPE_LENGTH (type_x), | |
1714 | gdbarch_byte_order (get_type_arch (type_x)), | |
1715 | y, TYPE_LENGTH (type_y), | |
1716 | gdbarch_byte_order (get_type_arch (type_y))); | |
1717 | } | |
1718 | ||
1719 | gdb_assert_not_reached ("unexpected type code"); | |
1720 | } | |
1721 |