1 /* Floating point routines for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2007
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* Support for converting target fp numbers into host DOUBLEST format. */
26 /* XXX - This code should really be in libiberty/floatformat.c,
27 however configuration issues with libiberty made this very
28 difficult to do in the available time. */
32 #include "floatformat.h"
33 #include "gdb_assert.h"
34 #include "gdb_string.h"
36 #include <math.h> /* ldexp */
38 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
39 going to bother with trying to muck around with whether it is defined in
40 a system header, what we do if not, etc. */
41 #define FLOATFORMAT_CHAR_BIT 8
43 /* The number of bytes that the largest floating-point type that we
44 can convert to doublest will need. */
45 #define FLOATFORMAT_LARGEST_BYTES 16
47 /* Extract a field which starts at START and is LEN bytes long. DATA and
48 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
50 get_field (const bfd_byte
*data
, enum floatformat_byteorders order
,
51 unsigned int total_len
, unsigned int start
, unsigned int len
)
54 unsigned int cur_byte
;
57 /* Caller must byte-swap words before calling this routine. */
58 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
60 /* Start at the least significant part of the field. */
61 if (order
== floatformat_little
)
63 /* We start counting from the other end (i.e, from the high bytes
64 rather than the low bytes). As such, we need to be concerned
65 with what happens if bit 0 doesn't start on a byte boundary.
66 I.e, we need to properly handle the case where total_len is
67 not evenly divisible by 8. So we compute ``excess'' which
68 represents the number of bits from the end of our starting
69 byte needed to get to bit 0. */
70 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
71 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
72 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
73 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
74 - FLOATFORMAT_CHAR_BIT
;
78 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
80 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
82 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
83 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
86 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
87 if (order
== floatformat_little
)
92 /* Move towards the most significant part of the field. */
93 while (cur_bitshift
< len
)
95 result
|= (unsigned long)*(data
+ cur_byte
) << cur_bitshift
;
96 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
99 case floatformat_little
:
102 case floatformat_big
:
107 if (len
< sizeof(result
) * FLOATFORMAT_CHAR_BIT
)
108 /* Mask out bits which are not part of the field */
109 result
&= ((1UL << len
) - 1);
113 /* Normalize the byte order of FROM into TO. If no normalization is
114 needed then FMT->byteorder is returned and TO is not changed;
115 otherwise the format of the normalized form in TO is returned. */
117 static enum floatformat_byteorders
118 floatformat_normalize_byteorder (const struct floatformat
*fmt
,
119 const void *from
, void *to
)
121 const unsigned char *swapin
;
122 unsigned char *swapout
;
125 if (fmt
->byteorder
== floatformat_little
126 || fmt
->byteorder
== floatformat_big
)
127 return fmt
->byteorder
;
129 words
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
132 swapout
= (unsigned char *)to
;
133 swapin
= (const unsigned char *)from
;
135 if (fmt
->byteorder
== floatformat_vax
)
139 *swapout
++ = swapin
[1];
140 *swapout
++ = swapin
[0];
141 *swapout
++ = swapin
[3];
142 *swapout
++ = swapin
[2];
145 /* This may look weird, since VAX is little-endian, but it is
146 easier to translate to big-endian than to little-endian. */
147 return floatformat_big
;
151 gdb_assert (fmt
->byteorder
== floatformat_littlebyte_bigword
);
155 *swapout
++ = swapin
[3];
156 *swapout
++ = swapin
[2];
157 *swapout
++ = swapin
[1];
158 *swapout
++ = swapin
[0];
161 return floatformat_big
;
165 /* Convert from FMT to a DOUBLEST.
166 FROM is the address of the extended float.
167 Store the DOUBLEST in *TO. */
170 convert_floatformat_to_doublest (const struct floatformat
*fmt
,
174 unsigned char *ufrom
= (unsigned char *) from
;
178 unsigned int mant_bits
, mant_off
;
180 int special_exponent
; /* It's a NaN, denorm or zero */
181 enum floatformat_byteorders order
;
182 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
183 enum float_kind kind
;
185 gdb_assert (fmt
->totalsize
186 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
188 /* For non-numbers, reuse libiberty's logic to find the correct
189 format. We do not lose any precision in this case by passing
191 kind
= floatformat_classify (fmt
, from
);
192 if (kind
== float_infinite
|| kind
== float_nan
)
195 floatformat_to_double (fmt
, from
, &dto
);
196 *to
= (DOUBLEST
) dto
;
200 order
= floatformat_normalize_byteorder (fmt
, ufrom
, newfrom
);
202 if (order
!= fmt
->byteorder
)
205 exponent
= get_field (ufrom
, order
, fmt
->totalsize
, fmt
->exp_start
,
207 /* Note that if exponent indicates a NaN, we can't really do anything useful
208 (not knowing if the host has NaN's, or how to build one). So it will
209 end up as an infinity or something close; that is OK. */
211 mant_bits_left
= fmt
->man_len
;
212 mant_off
= fmt
->man_start
;
215 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
217 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
218 we don't check for zero as the exponent doesn't matter. Note the cast
219 to int; exp_bias is unsigned, so it's important to make sure the
220 operation is done in signed arithmetic. */
221 if (!special_exponent
)
222 exponent
-= fmt
->exp_bias
;
223 else if (exponent
== 0)
224 exponent
= 1 - fmt
->exp_bias
;
226 /* Build the result algebraically. Might go infinite, underflow, etc;
229 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
230 increment the exponent by one to account for the integer bit. */
232 if (!special_exponent
)
234 if (fmt
->intbit
== floatformat_intbit_no
)
235 dto
= ldexp (1.0, exponent
);
240 while (mant_bits_left
> 0)
242 mant_bits
= min (mant_bits_left
, 32);
244 mant
= get_field (ufrom
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
246 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
247 exponent
-= mant_bits
;
248 mant_off
+= mant_bits
;
249 mant_bits_left
-= mant_bits
;
252 /* Negate it if negative. */
253 if (get_field (ufrom
, order
, fmt
->totalsize
, fmt
->sign_start
, 1))
258 static void put_field (unsigned char *, enum floatformat_byteorders
,
260 unsigned int, unsigned int, unsigned long);
262 /* Set a field which starts at START and is LEN bytes long. DATA and
263 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
265 put_field (unsigned char *data
, enum floatformat_byteorders order
,
266 unsigned int total_len
, unsigned int start
, unsigned int len
,
267 unsigned long stuff_to_put
)
269 unsigned int cur_byte
;
272 /* Caller must byte-swap words before calling this routine. */
273 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
275 /* Start at the least significant part of the field. */
276 if (order
== floatformat_little
)
278 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
279 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
280 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
281 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
282 - FLOATFORMAT_CHAR_BIT
;
286 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
288 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
290 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
292 *(data
+ cur_byte
) &=
293 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1)
295 *(data
+ cur_byte
) |=
296 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
298 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
299 if (order
== floatformat_little
)
304 /* Move towards the most significant part of the field. */
305 while (cur_bitshift
< len
)
307 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
309 /* This is the last byte. */
310 *(data
+ cur_byte
) &=
311 ~((1 << (len
- cur_bitshift
)) - 1);
312 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
315 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
316 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
317 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
318 if (order
== floatformat_little
)
325 #ifdef HAVE_LONG_DOUBLE
326 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
327 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
328 frexp, but operates on the long double data type. */
330 static long double ldfrexp (long double value
, int *eptr
);
333 ldfrexp (long double value
, int *eptr
)
338 /* Unfortunately, there are no portable functions for extracting the exponent
339 of a long double, so we have to do it iteratively by multiplying or dividing
340 by two until the fraction is between 0.5 and 1.0. */
348 if (value
>= tmp
) /* Value >= 1.0 */
354 else if (value
!= 0.0l) /* Value < 1.0 and > 0.0 */
368 #endif /* HAVE_LONG_DOUBLE */
371 /* The converse: convert the DOUBLEST *FROM to an extended float and
372 store where TO points. Neither FROM nor TO have any alignment
376 convert_doublest_to_floatformat (CONST
struct floatformat
*fmt
,
377 const DOUBLEST
*from
, void *to
)
382 unsigned int mant_bits
, mant_off
;
384 unsigned char *uto
= (unsigned char *) to
;
385 enum floatformat_byteorders order
= fmt
->byteorder
;
386 unsigned char newto
[FLOATFORMAT_LARGEST_BYTES
];
388 if (order
!= floatformat_little
)
389 order
= floatformat_big
;
391 if (order
!= fmt
->byteorder
)
394 memcpy (&dfrom
, from
, sizeof (dfrom
));
395 memset (uto
, 0, (fmt
->totalsize
+ FLOATFORMAT_CHAR_BIT
- 1)
396 / FLOATFORMAT_CHAR_BIT
);
398 return; /* Result is zero */
399 if (dfrom
!= dfrom
) /* Result is NaN */
402 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
403 fmt
->exp_len
, fmt
->exp_nan
);
404 /* Be sure it's not infinity, but NaN value is irrel */
405 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
407 goto finalize_byteorder
;
410 /* If negative, set the sign bit. */
413 put_field (uto
, order
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
417 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity */
419 /* Infinity exponent is same as NaN's. */
420 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
421 fmt
->exp_len
, fmt
->exp_nan
);
422 /* Infinity mantissa is all zeroes. */
423 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
425 goto finalize_byteorder
;
428 #ifdef HAVE_LONG_DOUBLE
429 mant
= ldfrexp (dfrom
, &exponent
);
431 mant
= frexp (dfrom
, &exponent
);
434 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
435 exponent
+ fmt
->exp_bias
- 1);
437 mant_bits_left
= fmt
->man_len
;
438 mant_off
= fmt
->man_start
;
439 while (mant_bits_left
> 0)
441 unsigned long mant_long
;
442 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
444 mant
*= 4294967296.0;
445 mant_long
= ((unsigned long) mant
) & 0xffffffffL
;
448 /* If the integer bit is implicit, then we need to discard it.
449 If we are discarding a zero, we should be (but are not) creating
450 a denormalized number which means adjusting the exponent
452 if (mant_bits_left
== fmt
->man_len
453 && fmt
->intbit
== floatformat_intbit_no
)
456 mant_long
&= 0xffffffffL
;
457 /* If we are processing the top 32 mantissa bits of a doublest
458 so as to convert to a float value with implied integer bit,
459 we will only be putting 31 of those 32 bits into the
460 final value due to the discarding of the top bit. In the
461 case of a small float value where the number of mantissa
462 bits is less than 32, discarding the top bit does not alter
463 the number of bits we will be adding to the result. */
470 /* The bits we want are in the most significant MANT_BITS bits of
471 mant_long. Move them to the least significant. */
472 mant_long
>>= 32 - mant_bits
;
475 put_field (uto
, order
, fmt
->totalsize
,
476 mant_off
, mant_bits
, mant_long
);
477 mant_off
+= mant_bits
;
478 mant_bits_left
-= mant_bits
;
482 /* Do we need to byte-swap the words in the result? */
483 if (order
!= fmt
->byteorder
)
484 floatformat_normalize_byteorder (fmt
, newto
, to
);
487 /* Check if VAL (which is assumed to be a floating point number whose
488 format is described by FMT) is negative. */
491 floatformat_is_negative (const struct floatformat
*fmt
,
492 const bfd_byte
*uval
)
494 enum floatformat_byteorders order
;
495 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
497 gdb_assert (fmt
!= NULL
);
498 gdb_assert (fmt
->totalsize
499 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
501 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
503 if (order
!= fmt
->byteorder
)
506 return get_field (uval
, order
, fmt
->totalsize
, fmt
->sign_start
, 1);
509 /* Check if VAL is "not a number" (NaN) for FMT. */
512 floatformat_classify (const struct floatformat
*fmt
,
513 const bfd_byte
*uval
)
517 unsigned int mant_bits
, mant_off
;
519 enum floatformat_byteorders order
;
520 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
523 gdb_assert (fmt
!= NULL
);
524 gdb_assert (fmt
->totalsize
525 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
527 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
529 if (order
!= fmt
->byteorder
)
532 exponent
= get_field (uval
, order
, fmt
->totalsize
, fmt
->exp_start
,
535 mant_bits_left
= fmt
->man_len
;
536 mant_off
= fmt
->man_start
;
539 while (mant_bits_left
> 0)
541 mant_bits
= min (mant_bits_left
, 32);
543 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
545 /* If there is an explicit integer bit, mask it off. */
546 if (mant_off
== fmt
->man_start
547 && fmt
->intbit
== floatformat_intbit_yes
)
548 mant
&= ~(1 << (mant_bits
- 1));
556 mant_off
+= mant_bits
;
557 mant_bits_left
-= mant_bits
;
560 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
570 if (exponent
== 0 && !mant_zero
)
571 return float_subnormal
;
573 if (exponent
== fmt
->exp_nan
)
576 return float_infinite
;
587 /* Convert the mantissa of VAL (which is assumed to be a floating
588 point number whose format is described by FMT) into a hexadecimal
589 and store it in a static string. Return a pointer to that string. */
592 floatformat_mantissa (const struct floatformat
*fmt
,
595 unsigned char *uval
= (unsigned char *) val
;
597 unsigned int mant_bits
, mant_off
;
602 enum floatformat_byteorders order
;
603 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
605 gdb_assert (fmt
!= NULL
);
606 gdb_assert (fmt
->totalsize
607 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
609 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
611 if (order
!= fmt
->byteorder
)
617 /* Make sure we have enough room to store the mantissa. */
618 gdb_assert (sizeof res
> ((fmt
->man_len
+ 7) / 8) * 2);
620 mant_off
= fmt
->man_start
;
621 mant_bits_left
= fmt
->man_len
;
622 mant_bits
= (mant_bits_left
% 32) > 0 ? mant_bits_left
% 32 : 32;
624 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
626 len
= xsnprintf (res
, sizeof res
, "%lx", mant
);
628 mant_off
+= mant_bits
;
629 mant_bits_left
-= mant_bits
;
631 while (mant_bits_left
> 0)
633 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, 32);
635 xsnprintf (buf
, sizeof buf
, "%08lx", mant
);
636 gdb_assert (len
+ strlen (buf
) <= sizeof res
);
640 mant_bits_left
-= 32;
647 /* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
649 If the host and target formats agree, we just copy the raw data
650 into the appropriate type of variable and return, letting the host
651 increase precision as necessary. Otherwise, we call the conversion
652 routine and let it do the dirty work. */
654 static const struct floatformat
*host_float_format
= GDB_HOST_FLOAT_FORMAT
;
655 static const struct floatformat
*host_double_format
= GDB_HOST_DOUBLE_FORMAT
;
656 static const struct floatformat
*host_long_double_format
= GDB_HOST_LONG_DOUBLE_FORMAT
;
659 floatformat_to_doublest (const struct floatformat
*fmt
,
660 const void *in
, DOUBLEST
*out
)
662 gdb_assert (fmt
!= NULL
);
663 if (fmt
== host_float_format
)
666 memcpy (&val
, in
, sizeof (val
));
669 else if (fmt
== host_double_format
)
672 memcpy (&val
, in
, sizeof (val
));
675 else if (fmt
== host_long_double_format
)
678 memcpy (&val
, in
, sizeof (val
));
682 convert_floatformat_to_doublest (fmt
, in
, out
);
686 floatformat_from_doublest (const struct floatformat
*fmt
,
687 const DOUBLEST
*in
, void *out
)
689 gdb_assert (fmt
!= NULL
);
690 if (fmt
== host_float_format
)
693 memcpy (out
, &val
, sizeof (val
));
695 else if (fmt
== host_double_format
)
698 memcpy (out
, &val
, sizeof (val
));
700 else if (fmt
== host_long_double_format
)
702 long double val
= *in
;
703 memcpy (out
, &val
, sizeof (val
));
706 convert_doublest_to_floatformat (fmt
, in
, out
);
710 /* Return a floating-point format for a floating-point variable of
711 length LEN. If no suitable floating-point format is found, an
714 We need this functionality since information about the
715 floating-point format of a type is not always available to GDB; the
716 debug information typically only tells us the size of a
719 FIXME: kettenis/2001-10-28: In many places, particularly in
720 target-dependent code, the format of floating-point types is known,
721 but not passed on by GDB. This should be fixed. */
723 static const struct floatformat
*
724 floatformat_from_length (int len
)
726 const struct floatformat
*format
;
727 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
728 format
= TARGET_FLOAT_FORMAT
[TARGET_BYTE_ORDER
];
729 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
730 format
= TARGET_DOUBLE_FORMAT
[TARGET_BYTE_ORDER
];
731 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
732 format
= TARGET_LONG_DOUBLE_FORMAT
[TARGET_BYTE_ORDER
];
733 /* On i386 the 'long double' type takes 96 bits,
734 while the real number of used bits is only 80,
735 both in processor and in memory.
736 The code below accepts the real bit size. */
737 else if ((TARGET_LONG_DOUBLE_FORMAT
!= NULL
)
738 && (len
* TARGET_CHAR_BIT
==
739 TARGET_LONG_DOUBLE_FORMAT
[0]->totalsize
))
740 format
= TARGET_LONG_DOUBLE_FORMAT
[TARGET_BYTE_ORDER
];
744 error (_("Unrecognized %d-bit floating-point type."),
745 len
* TARGET_CHAR_BIT
);
749 const struct floatformat
*
750 floatformat_from_type (const struct type
*type
)
752 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FLT
);
753 if (TYPE_FLOATFORMAT (type
) != NULL
)
754 return TYPE_FLOATFORMAT (type
)[TARGET_BYTE_ORDER
];
756 return floatformat_from_length (TYPE_LENGTH (type
));
759 /* If the host doesn't define NAN, use zero instead. */
764 /* Extract a floating-point number of length LEN from a target-order
765 byte-stream at ADDR. Returns the value as type DOUBLEST. */
768 extract_floating_by_length (const void *addr
, int len
)
770 const struct floatformat
*fmt
= floatformat_from_length (len
);
773 floatformat_to_doublest (fmt
, addr
, &val
);
778 deprecated_extract_floating (const void *addr
, int len
)
780 return extract_floating_by_length (addr
, len
);
783 /* Store VAL as a floating-point number of length LEN to a
784 target-order byte-stream at ADDR. */
787 store_floating_by_length (void *addr
, int len
, DOUBLEST val
)
789 const struct floatformat
*fmt
= floatformat_from_length (len
);
791 floatformat_from_doublest (fmt
, &val
, addr
);
795 deprecated_store_floating (void *addr
, int len
, DOUBLEST val
)
797 store_floating_by_length (addr
, len
, val
);
800 /* Extract a floating-point number of type TYPE from a target-order
801 byte-stream at ADDR. Returns the value as type DOUBLEST. */
804 extract_typed_floating (const void *addr
, const struct type
*type
)
808 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FLT
);
810 if (TYPE_FLOATFORMAT (type
) == NULL
)
811 /* Not all code remembers to set the FLOATFORMAT (language
812 specific code? stabs?) so handle that here as a special case. */
813 return extract_floating_by_length (addr
, TYPE_LENGTH (type
));
815 floatformat_to_doublest (TYPE_FLOATFORMAT (type
)[TARGET_BYTE_ORDER
],
820 /* Store VAL as a floating-point number of type TYPE to a target-order
821 byte-stream at ADDR. */
824 store_typed_floating (void *addr
, const struct type
*type
, DOUBLEST val
)
826 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FLT
);
828 /* FIXME: kettenis/2001-10-28: It is debatable whether we should
829 zero out any remaining bytes in the target buffer when TYPE is
830 longer than the actual underlying floating-point format. Perhaps
831 we should store a fixed bitpattern in those remaining bytes,
832 instead of zero, or perhaps we shouldn't touch those remaining
835 NOTE: cagney/2001-10-28: With the way things currently work, it
836 isn't a good idea to leave the end bits undefined. This is
837 because GDB writes out the entire sizeof(<floating>) bits of the
838 floating-point type even though the value might only be stored
839 in, and the target processor may only refer to, the first N <
840 TYPE_LENGTH (type) bits. If the end of the buffer wasn't
841 initialized, GDB would write undefined data to the target. An
842 errant program, refering to that undefined data, would then
843 become non-deterministic.
845 See also the function convert_typed_floating below. */
846 memset (addr
, 0, TYPE_LENGTH (type
));
848 if (TYPE_FLOATFORMAT (type
) == NULL
)
849 /* Not all code remembers to set the FLOATFORMAT (language
850 specific code? stabs?) so handle that here as a special case. */
851 store_floating_by_length (addr
, TYPE_LENGTH (type
), val
);
853 floatformat_from_doublest (TYPE_FLOATFORMAT (type
)[TARGET_BYTE_ORDER
],
857 /* Convert a floating-point number of type FROM_TYPE from a
858 target-order byte-stream at FROM to a floating-point number of type
859 TO_TYPE, and store it to a target-order byte-stream at TO. */
862 convert_typed_floating (const void *from
, const struct type
*from_type
,
863 void *to
, const struct type
*to_type
)
865 const struct floatformat
*from_fmt
= floatformat_from_type (from_type
);
866 const struct floatformat
*to_fmt
= floatformat_from_type (to_type
);
868 gdb_assert (TYPE_CODE (from_type
) == TYPE_CODE_FLT
);
869 gdb_assert (TYPE_CODE (to_type
) == TYPE_CODE_FLT
);
871 if (from_fmt
== NULL
|| to_fmt
== NULL
)
873 /* If we don't know the floating-point format of FROM_TYPE or
874 TO_TYPE, there's not much we can do. We might make the
875 assumption that if the length of FROM_TYPE and TO_TYPE match,
876 their floating-point format would match too, but that
877 assumption might be wrong on targets that support
878 floating-point types that only differ in endianness for
879 example. So we warn instead, and zero out the target buffer. */
880 warning (_("Can't convert floating-point number to desired type."));
881 memset (to
, 0, TYPE_LENGTH (to_type
));
883 else if (from_fmt
== to_fmt
)
885 /* We're in business. The floating-point format of FROM_TYPE
886 and TO_TYPE match. However, even though the floating-point
887 format matches, the length of the type might still be
888 different. Make sure we don't overrun any buffers. See
889 comment in store_typed_floating for a discussion about
890 zeroing out remaining bytes in the target buffer. */
891 memset (to
, 0, TYPE_LENGTH (to_type
));
892 memcpy (to
, from
, min (TYPE_LENGTH (from_type
), TYPE_LENGTH (to_type
)));
896 /* The floating-point types don't match. The best we can do
897 (aport from simulating the target FPU) is converting to the
898 widest floating-point type supported by the host, and then
899 again to the desired type. */
902 floatformat_to_doublest (from_fmt
, from
, &d
);
903 floatformat_from_doublest (to_fmt
, &d
, to
);
907 const struct floatformat
*floatformat_ieee_single
[BFD_ENDIAN_UNKNOWN
];
908 const struct floatformat
*floatformat_ieee_double
[BFD_ENDIAN_UNKNOWN
];
909 const struct floatformat
*floatformat_ieee_quad
[BFD_ENDIAN_UNKNOWN
];
910 const struct floatformat
*floatformat_arm_ext
[BFD_ENDIAN_UNKNOWN
];
911 const struct floatformat
*floatformat_ia64_spill
[BFD_ENDIAN_UNKNOWN
];
913 extern void _initialize_doublest (void);
916 _initialize_doublest (void)
918 floatformat_ieee_single
[BFD_ENDIAN_LITTLE
] = &floatformat_ieee_single_little
;
919 floatformat_ieee_single
[BFD_ENDIAN_BIG
] = &floatformat_ieee_single_big
;
920 floatformat_ieee_double
[BFD_ENDIAN_LITTLE
] = &floatformat_ieee_double_little
;
921 floatformat_ieee_double
[BFD_ENDIAN_BIG
] = &floatformat_ieee_double_big
;
922 floatformat_arm_ext
[BFD_ENDIAN_LITTLE
] = &floatformat_arm_ext_littlebyte_bigword
;
923 floatformat_arm_ext
[BFD_ENDIAN_BIG
] = &floatformat_arm_ext_big
;
924 floatformat_ia64_spill
[BFD_ENDIAN_LITTLE
] = &floatformat_ia64_spill_little
;
925 floatformat_ia64_spill
[BFD_ENDIAN_BIG
] = &floatformat_ia64_spill_big
;
926 floatformat_ieee_quad
[BFD_ENDIAN_LITTLE
] = &floatformat_ia64_quad_little
;
927 floatformat_ieee_quad
[BFD_ENDIAN_BIG
] = &floatformat_ia64_quad_big
;