1 /* Floating point routines for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 /* Support for converting target fp numbers into host DOUBLEST format. */
22 /* XXX - This code should really be in libiberty/floatformat.c,
23 however configuration issues with libiberty made this very
24 difficult to do in the available time. */
28 #include "floatformat.h"
30 #include <math.h> /* ldexp */
33 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
34 going to bother with trying to muck around with whether it is defined in
35 a system header, what we do if not, etc. */
36 #define FLOATFORMAT_CHAR_BIT 8
38 /* The number of bytes that the largest floating-point type that we
39 can convert to doublest will need. */
40 #define FLOATFORMAT_LARGEST_BYTES 16
42 /* Extract a field which starts at START and is LEN bytes long. DATA and
43 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
45 get_field (const bfd_byte
*data
, enum floatformat_byteorders order
,
46 unsigned int total_len
, unsigned int start
, unsigned int len
)
49 unsigned int cur_byte
;
52 /* Caller must byte-swap words before calling this routine. */
53 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
55 /* Start at the least significant part of the field. */
56 if (order
== floatformat_little
)
58 /* We start counting from the other end (i.e, from the high bytes
59 rather than the low bytes). As such, we need to be concerned
60 with what happens if bit 0 doesn't start on a byte boundary.
61 I.e, we need to properly handle the case where total_len is
62 not evenly divisible by 8. So we compute ``excess'' which
63 represents the number of bits from the end of our starting
64 byte needed to get to bit 0. */
65 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
67 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
68 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
69 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
70 - FLOATFORMAT_CHAR_BIT
;
74 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
76 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
78 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
79 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
82 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
83 if (order
== floatformat_little
)
88 /* Move towards the most significant part of the field. */
89 while (cur_bitshift
< len
)
91 result
|= (unsigned long)*(data
+ cur_byte
) << cur_bitshift
;
92 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
95 case floatformat_little
:
103 if (len
< sizeof(result
) * FLOATFORMAT_CHAR_BIT
)
104 /* Mask out bits which are not part of the field. */
105 result
&= ((1UL << len
) - 1);
109 /* Normalize the byte order of FROM into TO. If no normalization is
110 needed then FMT->byteorder is returned and TO is not changed;
111 otherwise the format of the normalized form in TO is returned. */
113 static enum floatformat_byteorders
114 floatformat_normalize_byteorder (const struct floatformat
*fmt
,
115 const void *from
, void *to
)
117 const unsigned char *swapin
;
118 unsigned char *swapout
;
121 if (fmt
->byteorder
== floatformat_little
122 || fmt
->byteorder
== floatformat_big
)
123 return fmt
->byteorder
;
125 words
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
128 swapout
= (unsigned char *)to
;
129 swapin
= (const unsigned char *)from
;
131 if (fmt
->byteorder
== floatformat_vax
)
135 *swapout
++ = swapin
[1];
136 *swapout
++ = swapin
[0];
137 *swapout
++ = swapin
[3];
138 *swapout
++ = swapin
[2];
141 /* This may look weird, since VAX is little-endian, but it is
142 easier to translate to big-endian than to little-endian. */
143 return floatformat_big
;
147 gdb_assert (fmt
->byteorder
== floatformat_littlebyte_bigword
);
151 *swapout
++ = swapin
[3];
152 *swapout
++ = swapin
[2];
153 *swapout
++ = swapin
[1];
154 *swapout
++ = swapin
[0];
157 return floatformat_big
;
161 /* Convert from FMT to a DOUBLEST.
162 FROM is the address of the extended float.
163 Store the DOUBLEST in *TO. */
166 convert_floatformat_to_doublest (const struct floatformat
*fmt
,
170 unsigned char *ufrom
= (unsigned char *) from
;
174 unsigned int mant_bits
, mant_off
;
176 int special_exponent
; /* It's a NaN, denorm or zero. */
177 enum floatformat_byteorders order
;
178 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
179 enum float_kind kind
;
181 gdb_assert (fmt
->totalsize
182 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
184 /* For non-numbers, reuse libiberty's logic to find the correct
185 format. We do not lose any precision in this case by passing
187 kind
= floatformat_classify (fmt
, (const bfd_byte
*) from
);
188 if (kind
== float_infinite
|| kind
== float_nan
)
192 floatformat_to_double (fmt
->split_half
? fmt
->split_half
: fmt
,
194 *to
= (DOUBLEST
) dto
;
198 order
= floatformat_normalize_byteorder (fmt
, ufrom
, newfrom
);
200 if (order
!= fmt
->byteorder
)
207 floatformat_to_doublest (fmt
->split_half
, ufrom
, &dtop
);
208 /* Preserve the sign of 0, which is the sign of the top
215 floatformat_to_doublest (fmt
->split_half
,
216 ufrom
+ fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
/ 2,
222 exponent
= get_field (ufrom
, order
, fmt
->totalsize
, fmt
->exp_start
,
224 /* Note that if exponent indicates a NaN, we can't really do anything useful
225 (not knowing if the host has NaN's, or how to build one). So it will
226 end up as an infinity or something close; that is OK. */
228 mant_bits_left
= fmt
->man_len
;
229 mant_off
= fmt
->man_start
;
232 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
234 /* Don't bias NaNs. Use minimum exponent for denorms. For
235 simplicity, we don't check for zero as the exponent doesn't matter.
236 Note the cast to int; exp_bias is unsigned, so it's important to
237 make sure the operation is done in signed arithmetic. */
238 if (!special_exponent
)
239 exponent
-= fmt
->exp_bias
;
240 else if (exponent
== 0)
241 exponent
= 1 - fmt
->exp_bias
;
243 /* Build the result algebraically. Might go infinite, underflow, etc;
246 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
247 increment the exponent by one to account for the integer bit. */
249 if (!special_exponent
)
251 if (fmt
->intbit
== floatformat_intbit_no
)
252 dto
= ldexp (1.0, exponent
);
257 while (mant_bits_left
> 0)
259 mant_bits
= std::min (mant_bits_left
, 32);
261 mant
= get_field (ufrom
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
263 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
264 exponent
-= mant_bits
;
265 mant_off
+= mant_bits
;
266 mant_bits_left
-= mant_bits
;
269 /* Negate it if negative. */
270 if (get_field (ufrom
, order
, fmt
->totalsize
, fmt
->sign_start
, 1))
275 /* Set a field which starts at START and is LEN bytes long. DATA and
276 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
278 put_field (unsigned char *data
, enum floatformat_byteorders order
,
279 unsigned int total_len
, unsigned int start
, unsigned int len
,
280 unsigned long stuff_to_put
)
282 unsigned int cur_byte
;
285 /* Caller must byte-swap words before calling this routine. */
286 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
288 /* Start at the least significant part of the field. */
289 if (order
== floatformat_little
)
291 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
293 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
294 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
295 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
296 - FLOATFORMAT_CHAR_BIT
;
300 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
302 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
304 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
306 *(data
+ cur_byte
) &=
307 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1)
309 *(data
+ cur_byte
) |=
310 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
312 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
313 if (order
== floatformat_little
)
318 /* Move towards the most significant part of the field. */
319 while (cur_bitshift
< len
)
321 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
323 /* This is the last byte. */
324 *(data
+ cur_byte
) &=
325 ~((1 << (len
- cur_bitshift
)) - 1);
326 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
329 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
330 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
331 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
332 if (order
== floatformat_little
)
339 /* The converse: convert the DOUBLEST *FROM to an extended float and
340 store where TO points. Neither FROM nor TO have any alignment
344 convert_doublest_to_floatformat (const struct floatformat
*fmt
,
345 const DOUBLEST
*from
, void *to
)
350 unsigned int mant_bits
, mant_off
;
352 unsigned char *uto
= (unsigned char *) to
;
353 enum floatformat_byteorders order
= fmt
->byteorder
;
354 unsigned char newto
[FLOATFORMAT_LARGEST_BYTES
];
356 if (order
!= floatformat_little
)
357 order
= floatformat_big
;
359 if (order
!= fmt
->byteorder
)
362 memcpy (&dfrom
, from
, sizeof (dfrom
));
363 memset (uto
, 0, floatformat_totalsize_bytes (fmt
));
367 /* Use static volatile to ensure that any excess precision is
368 removed via storing in memory, and so the top half really is
369 the result of converting to double. */
370 static volatile double dtop
, dbot
;
371 DOUBLEST dtopnv
, dbotnv
;
373 dtop
= (double) dfrom
;
374 /* If the rounded top half is Inf, the bottom must be 0 not NaN
376 if (dtop
+ dtop
== dtop
&& dtop
!= 0.0)
379 dbot
= (double) (dfrom
- (DOUBLEST
) dtop
);
382 floatformat_from_doublest (fmt
->split_half
, &dtopnv
, uto
);
383 floatformat_from_doublest (fmt
->split_half
, &dbotnv
,
385 + fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
/ 2));
390 goto finalize_byteorder
; /* Result is zero */
391 if (dfrom
!= dfrom
) /* Result is NaN */
394 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
395 fmt
->exp_len
, fmt
->exp_nan
);
396 /* Be sure it's not infinity, but NaN value is irrel. */
397 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
399 goto finalize_byteorder
;
402 /* If negative, set the sign bit. */
405 put_field (uto
, order
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
409 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity. */
411 /* Infinity exponent is same as NaN's. */
412 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
413 fmt
->exp_len
, fmt
->exp_nan
);
414 /* Infinity mantissa is all zeroes. */
415 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
417 goto finalize_byteorder
;
420 #ifdef HAVE_LONG_DOUBLE
421 mant
= frexpl (dfrom
, &exponent
);
423 mant
= frexp (dfrom
, &exponent
);
426 if (exponent
+ fmt
->exp_bias
<= 0)
428 /* The value is too small to be expressed in the destination
429 type (not enough bits in the exponent. Treat as 0. */
430 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
432 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
434 goto finalize_byteorder
;
437 if (exponent
+ fmt
->exp_bias
>= (1 << fmt
->exp_len
))
439 /* The value is too large to fit into the destination.
440 Treat as infinity. */
441 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
442 fmt
->exp_len
, fmt
->exp_nan
);
443 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
445 goto finalize_byteorder
;
448 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
449 exponent
+ fmt
->exp_bias
- 1);
451 mant_bits_left
= fmt
->man_len
;
452 mant_off
= fmt
->man_start
;
453 while (mant_bits_left
> 0)
455 unsigned long mant_long
;
457 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
459 mant
*= 4294967296.0;
460 mant_long
= ((unsigned long) mant
) & 0xffffffffL
;
463 /* If the integer bit is implicit, then we need to discard it.
464 If we are discarding a zero, we should be (but are not) creating
465 a denormalized number which means adjusting the exponent
467 if (mant_bits_left
== fmt
->man_len
468 && fmt
->intbit
== floatformat_intbit_no
)
471 mant_long
&= 0xffffffffL
;
472 /* If we are processing the top 32 mantissa bits of a doublest
473 so as to convert to a float value with implied integer bit,
474 we will only be putting 31 of those 32 bits into the
475 final value due to the discarding of the top bit. In the
476 case of a small float value where the number of mantissa
477 bits is less than 32, discarding the top bit does not alter
478 the number of bits we will be adding to the result. */
485 /* The bits we want are in the most significant MANT_BITS bits of
486 mant_long. Move them to the least significant. */
487 mant_long
>>= 32 - mant_bits
;
490 put_field (uto
, order
, fmt
->totalsize
,
491 mant_off
, mant_bits
, mant_long
);
492 mant_off
+= mant_bits
;
493 mant_bits_left
-= mant_bits
;
497 /* Do we need to byte-swap the words in the result? */
498 if (order
!= fmt
->byteorder
)
499 floatformat_normalize_byteorder (fmt
, newto
, to
);
502 /* Check if VAL (which is assumed to be a floating point number whose
503 format is described by FMT) is negative. */
506 floatformat_is_negative (const struct floatformat
*fmt
,
507 const bfd_byte
*uval
)
509 enum floatformat_byteorders order
;
510 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
512 gdb_assert (fmt
!= NULL
);
513 gdb_assert (fmt
->totalsize
514 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
516 /* An IBM long double (a two element array of double) always takes the
517 sign of the first double. */
519 fmt
= fmt
->split_half
;
521 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
523 if (order
!= fmt
->byteorder
)
526 return get_field (uval
, order
, fmt
->totalsize
, fmt
->sign_start
, 1);
529 /* Check if VAL is "not a number" (NaN) for FMT. */
532 floatformat_classify (const struct floatformat
*fmt
,
533 const bfd_byte
*uval
)
537 unsigned int mant_bits
, mant_off
;
539 enum floatformat_byteorders order
;
540 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
543 gdb_assert (fmt
!= NULL
);
544 gdb_assert (fmt
->totalsize
545 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
547 /* An IBM long double (a two element array of double) can be classified
548 by looking at the first double. inf and nan are specified as
549 ignoring the second double. zero and subnormal will always have
550 the second double 0.0 if the long double is correctly rounded. */
552 fmt
= fmt
->split_half
;
554 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
556 if (order
!= fmt
->byteorder
)
559 exponent
= get_field (uval
, order
, fmt
->totalsize
, fmt
->exp_start
,
562 mant_bits_left
= fmt
->man_len
;
563 mant_off
= fmt
->man_start
;
566 while (mant_bits_left
> 0)
568 mant_bits
= std::min (mant_bits_left
, 32);
570 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
572 /* If there is an explicit integer bit, mask it off. */
573 if (mant_off
== fmt
->man_start
574 && fmt
->intbit
== floatformat_intbit_yes
)
575 mant
&= ~(1 << (mant_bits
- 1));
583 mant_off
+= mant_bits
;
584 mant_bits_left
-= mant_bits
;
587 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
602 return float_subnormal
;
605 if (exponent
== fmt
->exp_nan
)
608 return float_infinite
;
616 /* Convert the mantissa of VAL (which is assumed to be a floating
617 point number whose format is described by FMT) into a hexadecimal
618 and store it in a static string. Return a pointer to that string. */
621 floatformat_mantissa (const struct floatformat
*fmt
,
624 unsigned char *uval
= (unsigned char *) val
;
626 unsigned int mant_bits
, mant_off
;
631 enum floatformat_byteorders order
;
632 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
634 gdb_assert (fmt
!= NULL
);
635 gdb_assert (fmt
->totalsize
636 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
638 /* For IBM long double (a two element array of double), return the
639 mantissa of the first double. The problem with returning the
640 actual mantissa from both doubles is that there can be an
641 arbitrary number of implied 0's or 1's between the mantissas
642 of the first and second double. In any case, this function
643 is only used for dumping out nans, and a nan is specified to
644 ignore the value in the second double. */
646 fmt
= fmt
->split_half
;
648 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
650 if (order
!= fmt
->byteorder
)
656 /* Make sure we have enough room to store the mantissa. */
657 gdb_assert (sizeof res
> ((fmt
->man_len
+ 7) / 8) * 2);
659 mant_off
= fmt
->man_start
;
660 mant_bits_left
= fmt
->man_len
;
661 mant_bits
= (mant_bits_left
% 32) > 0 ? mant_bits_left
% 32 : 32;
663 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
665 len
= xsnprintf (res
, sizeof res
, "%lx", mant
);
667 mant_off
+= mant_bits
;
668 mant_bits_left
-= mant_bits
;
670 while (mant_bits_left
> 0)
672 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, 32);
674 xsnprintf (buf
, sizeof buf
, "%08lx", mant
);
675 gdb_assert (len
+ strlen (buf
) <= sizeof res
);
679 mant_bits_left
-= 32;
685 /* Return the precision of the floating point format FMT. */
688 floatformat_precision (const struct floatformat
*fmt
)
690 /* Assume the precision of and IBM long double is twice the precision
691 of the underlying double. This matches what GCC does. */
693 return 2 * floatformat_precision (fmt
->split_half
);
695 /* Otherwise, the precision is the size of mantissa in bits,
696 including the implicit bit if present. */
697 int prec
= fmt
->man_len
;
698 if (fmt
->intbit
== floatformat_intbit_no
)
705 /* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
707 If the host and target formats agree, we just copy the raw data
708 into the appropriate type of variable and return, letting the host
709 increase precision as necessary. Otherwise, we call the conversion
710 routine and let it do the dirty work. Note that even if the target
711 and host floating-point formats match, the length of the types
712 might still be different, so the conversion routines must make sure
713 to not overrun any buffers. For example, on x86, long double is
714 the 80-bit extended precision type on both 32-bit and 64-bit ABIs,
715 but by default it is stored as 12 bytes on 32-bit, and 16 bytes on
716 64-bit, for alignment reasons. See comment in store_typed_floating
717 for a discussion about zeroing out remaining bytes in the target
720 static const struct floatformat
*host_float_format
= GDB_HOST_FLOAT_FORMAT
;
721 static const struct floatformat
*host_double_format
= GDB_HOST_DOUBLE_FORMAT
;
722 static const struct floatformat
*host_long_double_format
723 = GDB_HOST_LONG_DOUBLE_FORMAT
;
725 /* See doublest.h. */
728 floatformat_totalsize_bytes (const struct floatformat
*fmt
)
730 return ((fmt
->totalsize
+ FLOATFORMAT_CHAR_BIT
- 1)
731 / FLOATFORMAT_CHAR_BIT
);
735 floatformat_to_doublest (const struct floatformat
*fmt
,
736 const void *in
, DOUBLEST
*out
)
738 gdb_assert (fmt
!= NULL
);
740 if (fmt
== host_float_format
)
744 memcpy (&val
, in
, floatformat_totalsize_bytes (fmt
));
747 else if (fmt
== host_double_format
)
751 memcpy (&val
, in
, floatformat_totalsize_bytes (fmt
));
754 else if (fmt
== host_long_double_format
)
758 memcpy (&val
, in
, floatformat_totalsize_bytes (fmt
));
762 convert_floatformat_to_doublest (fmt
, in
, out
);
766 floatformat_from_doublest (const struct floatformat
*fmt
,
767 const DOUBLEST
*in
, void *out
)
769 gdb_assert (fmt
!= NULL
);
771 if (fmt
== host_float_format
)
775 memcpy (out
, &val
, floatformat_totalsize_bytes (fmt
));
777 else if (fmt
== host_double_format
)
781 memcpy (out
, &val
, floatformat_totalsize_bytes (fmt
));
783 else if (fmt
== host_long_double_format
)
785 long double val
= *in
;
787 memcpy (out
, &val
, floatformat_totalsize_bytes (fmt
));
790 convert_doublest_to_floatformat (fmt
, in
, out
);
793 /* Convert the byte-stream ADDR, interpreted as floating-point format FMT,
794 to a string, optionally using the print format FORMAT. */
796 floatformat_to_string (const struct floatformat
*fmt
,
797 const gdb_byte
*in
, const char *format
)
799 /* Unless we need to adhere to a specific format, provide special
800 output for certain cases. */
801 if (format
== nullptr)
803 /* Detect invalid representations. */
804 if (!floatformat_is_valid (fmt
, in
))
805 return "<invalid float value>";
807 /* Handle NaN and Inf. */
808 enum float_kind kind
= floatformat_classify (fmt
, in
);
809 if (kind
== float_nan
)
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
);
815 else if (kind
== float_infinite
)
817 const char *sign
= floatformat_is_negative (fmt
, in
)? "-" : "";
818 return string_printf ("%sinf", sign
);
822 /* Determine the format string to use on the host side. */
823 std::string host_format
;
826 if (format
== nullptr)
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
832 ceil(1 + p * log10(b)),
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
)
842 host_format
= string_printf ("%%.%d", decimal_dig
);
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')
857 host_format
= std::string (format
, len
);
860 /* Add the length modifier and conversion character appropriate for
861 handling the host DOUBLEST type. */
862 #ifdef HAVE_LONG_DOUBLE
865 host_format
+= conversion
;
868 floatformat_to_doublest (fmt
, in
, &doub
);
869 return string_printf (host_format
.c_str (), doub
);
872 /* Parse string STRING into a target floating-number of format FMT and
873 store it as byte-stream ADDR. Return whether parsing succeeded. */
875 floatformat_from_string (const struct floatformat
*fmt
, gdb_byte
*out
,
876 const std::string
&in
)
880 #ifdef HAVE_LONG_DOUBLE
881 const char *scan_format
= "%Lg%n";
883 const char *scan_format
= "%lg%n";
885 num
= sscanf (in
.c_str (), scan_format
, &doub
, &n
);
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. */
893 /* We only accept the whole string. */
897 floatformat_from_doublest (fmt
, &doub
, out
);
901 /* Extract a floating-point number of type TYPE from a target-order
902 byte-stream at ADDR. Returns the value as type DOUBLEST. */
905 extract_typed_floating (const void *addr
, const struct type
*type
)
907 const struct floatformat
*fmt
= floatformat_from_type (type
);
910 floatformat_to_doublest (fmt
, addr
, &retval
);
914 /* Store VAL as a floating-point number of type TYPE to a target-order
915 byte-stream at ADDR. */
918 store_typed_floating (void *addr
, const struct type
*type
, DOUBLEST val
)
920 const struct floatformat
*fmt
= floatformat_from_type (type
);
922 /* FIXME: kettenis/2001-10-28: It is debatable whether we should
923 zero out any remaining bytes in the target buffer when TYPE is
924 longer than the actual underlying floating-point format. Perhaps
925 we should store a fixed bitpattern in those remaining bytes,
926 instead of zero, or perhaps we shouldn't touch those remaining
929 NOTE: cagney/2001-10-28: With the way things currently work, it
930 isn't a good idea to leave the end bits undefined. This is
931 because GDB writes out the entire sizeof(<floating>) bits of the
932 floating-point type even though the value might only be stored
933 in, and the target processor may only refer to, the first N <
934 TYPE_LENGTH (type) bits. If the end of the buffer wasn't
935 initialized, GDB would write undefined data to the target. An
936 errant program, refering to that undefined data, would then
937 become non-deterministic.
939 See also the function convert_typed_floating below. */
940 memset (addr
, 0, TYPE_LENGTH (type
));
942 floatformat_from_doublest (fmt
, &val
, addr
);
945 /* Convert a floating-point number of type FROM_TYPE from a
946 target-order byte-stream at FROM to a floating-point number of type
947 TO_TYPE, and store it to a target-order byte-stream at TO. */
950 convert_typed_floating (const void *from
, const struct type
*from_type
,
951 void *to
, const struct type
*to_type
)
953 const struct floatformat
*from_fmt
= floatformat_from_type (from_type
);
954 const struct floatformat
*to_fmt
= floatformat_from_type (to_type
);
956 if (from_fmt
== NULL
|| to_fmt
== NULL
)
958 /* If we don't know the floating-point format of FROM_TYPE or
959 TO_TYPE, there's not much we can do. We might make the
960 assumption that if the length of FROM_TYPE and TO_TYPE match,
961 their floating-point format would match too, but that
962 assumption might be wrong on targets that support
963 floating-point types that only differ in endianness for
964 example. So we warn instead, and zero out the target buffer. */
965 warning (_("Can't convert floating-point number to desired type."));
966 memset (to
, 0, TYPE_LENGTH (to_type
));
968 else if (from_fmt
== to_fmt
)
970 /* We're in business. The floating-point format of FROM_TYPE
971 and TO_TYPE match. However, even though the floating-point
972 format matches, the length of the type might still be
973 different. Make sure we don't overrun any buffers. See
974 comment in store_typed_floating for a discussion about
975 zeroing out remaining bytes in the target buffer. */
976 memset (to
, 0, TYPE_LENGTH (to_type
));
977 memcpy (to
, from
, std::min (TYPE_LENGTH (from_type
),
978 TYPE_LENGTH (to_type
)));
982 /* The floating-point types don't match. The best we can do
983 (apart from simulating the target FPU) is converting to the
984 widest floating-point type supported by the host, and then
985 again to the desired type. */
988 floatformat_to_doublest (from_fmt
, from
, &d
);
989 floatformat_from_doublest (to_fmt
, &d
, to
);