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, 2008, 2009, 2010
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 3 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, see <http://www.gnu.org/licenses/>. */
22 /* Support for converting target fp numbers into host DOUBLEST format. */
24 /* XXX - This code should really be in libiberty/floatformat.c,
25 however configuration issues with libiberty made this very
26 difficult to do in the available time. */
30 #include "floatformat.h"
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
34 #include <math.h> /* ldexp */
36 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
37 going to bother with trying to muck around with whether it is defined in
38 a system header, what we do if not, etc. */
39 #define FLOATFORMAT_CHAR_BIT 8
41 /* The number of bytes that the largest floating-point type that we
42 can convert to doublest will need. */
43 #define FLOATFORMAT_LARGEST_BYTES 16
45 /* Extract a field which starts at START and is LEN bytes long. DATA and
46 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
48 get_field (const bfd_byte
*data
, enum floatformat_byteorders order
,
49 unsigned int total_len
, unsigned int start
, unsigned int len
)
52 unsigned int cur_byte
;
55 /* Caller must byte-swap words before calling this routine. */
56 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
58 /* Start at the least significant part of the field. */
59 if (order
== floatformat_little
)
61 /* We start counting from the other end (i.e, from the high bytes
62 rather than the low bytes). As such, we need to be concerned
63 with what happens if bit 0 doesn't start on a byte boundary.
64 I.e, we need to properly handle the case where total_len is
65 not evenly divisible by 8. So we compute ``excess'' which
66 represents the number of bits from the end of our starting
67 byte needed to get to bit 0. */
68 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
70 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
71 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
72 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
73 - FLOATFORMAT_CHAR_BIT
;
77 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
79 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
81 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
82 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
85 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
86 if (order
== floatformat_little
)
91 /* Move towards the most significant part of the field. */
92 while (cur_bitshift
< len
)
94 result
|= (unsigned long)*(data
+ cur_byte
) << cur_bitshift
;
95 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
98 case floatformat_little
:
101 case floatformat_big
:
106 if (len
< sizeof(result
) * FLOATFORMAT_CHAR_BIT
)
107 /* Mask out bits which are not part of the field */
108 result
&= ((1UL << len
) - 1);
112 /* Normalize the byte order of FROM into TO. If no normalization is
113 needed then FMT->byteorder is returned and TO is not changed;
114 otherwise the format of the normalized form in TO is returned. */
116 static enum floatformat_byteorders
117 floatformat_normalize_byteorder (const struct floatformat
*fmt
,
118 const void *from
, void *to
)
120 const unsigned char *swapin
;
121 unsigned char *swapout
;
124 if (fmt
->byteorder
== floatformat_little
125 || fmt
->byteorder
== floatformat_big
)
126 return fmt
->byteorder
;
128 words
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
131 swapout
= (unsigned char *)to
;
132 swapin
= (const unsigned char *)from
;
134 if (fmt
->byteorder
== floatformat_vax
)
138 *swapout
++ = swapin
[1];
139 *swapout
++ = swapin
[0];
140 *swapout
++ = swapin
[3];
141 *swapout
++ = swapin
[2];
144 /* This may look weird, since VAX is little-endian, but it is
145 easier to translate to big-endian than to little-endian. */
146 return floatformat_big
;
150 gdb_assert (fmt
->byteorder
== floatformat_littlebyte_bigword
);
154 *swapout
++ = swapin
[3];
155 *swapout
++ = swapin
[2];
156 *swapout
++ = swapin
[1];
157 *swapout
++ = swapin
[0];
160 return floatformat_big
;
164 /* Convert from FMT to a DOUBLEST.
165 FROM is the address of the extended float.
166 Store the DOUBLEST in *TO. */
169 convert_floatformat_to_doublest (const struct floatformat
*fmt
,
173 unsigned char *ufrom
= (unsigned char *) from
;
177 unsigned int mant_bits
, mant_off
;
179 int special_exponent
; /* It's a NaN, denorm or zero */
180 enum floatformat_byteorders order
;
181 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
182 enum float_kind kind
;
184 gdb_assert (fmt
->totalsize
185 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
187 /* For non-numbers, reuse libiberty's logic to find the correct
188 format. We do not lose any precision in this case by passing
190 kind
= floatformat_classify (fmt
, from
);
191 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
)
209 floatformat_to_doublest (fmt
->split_half
, ufrom
, &dtop
);
210 /* Preserve the sign of 0, which is the sign of the top
217 floatformat_to_doublest (fmt
->split_half
,
218 ufrom
+ fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
/ 2,
224 exponent
= get_field (ufrom
, order
, fmt
->totalsize
, fmt
->exp_start
,
226 /* Note that if exponent indicates a NaN, we can't really do anything useful
227 (not knowing if the host has NaN's, or how to build one). So it will
228 end up as an infinity or something close; that is OK. */
230 mant_bits_left
= fmt
->man_len
;
231 mant_off
= fmt
->man_start
;
234 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
236 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
237 we don't check for zero as the exponent doesn't matter. Note the cast
238 to int; exp_bias is unsigned, so it's important to make sure the
239 operation is done in signed arithmetic. */
240 if (!special_exponent
)
241 exponent
-= fmt
->exp_bias
;
242 else if (exponent
== 0)
243 exponent
= 1 - fmt
->exp_bias
;
245 /* Build the result algebraically. Might go infinite, underflow, etc;
248 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
249 increment the exponent by one to account for the integer bit. */
251 if (!special_exponent
)
253 if (fmt
->intbit
== floatformat_intbit_no
)
254 dto
= ldexp (1.0, exponent
);
259 while (mant_bits_left
> 0)
261 mant_bits
= min (mant_bits_left
, 32);
263 mant
= get_field (ufrom
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
265 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
266 exponent
-= mant_bits
;
267 mant_off
+= mant_bits
;
268 mant_bits_left
-= mant_bits
;
271 /* Negate it if negative. */
272 if (get_field (ufrom
, order
, fmt
->totalsize
, fmt
->sign_start
, 1))
277 static void put_field (unsigned char *, enum floatformat_byteorders
,
279 unsigned int, unsigned int, unsigned long);
281 /* Set a field which starts at START and is LEN bytes long. DATA and
282 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
284 put_field (unsigned char *data
, enum floatformat_byteorders order
,
285 unsigned int total_len
, unsigned int start
, unsigned int len
,
286 unsigned long stuff_to_put
)
288 unsigned int cur_byte
;
291 /* Caller must byte-swap words before calling this routine. */
292 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
294 /* Start at the least significant part of the field. */
295 if (order
== floatformat_little
)
297 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
299 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
300 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
301 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
302 - FLOATFORMAT_CHAR_BIT
;
306 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
308 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
310 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
312 *(data
+ cur_byte
) &=
313 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1)
315 *(data
+ cur_byte
) |=
316 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
318 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
319 if (order
== floatformat_little
)
324 /* Move towards the most significant part of the field. */
325 while (cur_bitshift
< len
)
327 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
329 /* This is the last byte. */
330 *(data
+ cur_byte
) &=
331 ~((1 << (len
- cur_bitshift
)) - 1);
332 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
335 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
336 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
337 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
338 if (order
== floatformat_little
)
345 #ifdef HAVE_LONG_DOUBLE
346 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
347 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
348 frexp, but operates on the long double data type. */
350 static long double ldfrexp (long double value
, int *eptr
);
353 ldfrexp (long double value
, int *eptr
)
358 /* Unfortunately, there are no portable functions for extracting the exponent
359 of a long double, so we have to do it iteratively by multiplying or dividing
360 by two until the fraction is between 0.5 and 1.0. */
368 if (value
>= tmp
) /* Value >= 1.0 */
374 else if (value
!= 0.0l) /* Value < 1.0 and > 0.0 */
388 #endif /* HAVE_LONG_DOUBLE */
391 /* The converse: convert the DOUBLEST *FROM to an extended float and
392 store where TO points. Neither FROM nor TO have any alignment
396 convert_doublest_to_floatformat (CONST
struct floatformat
*fmt
,
397 const DOUBLEST
*from
, void *to
)
402 unsigned int mant_bits
, mant_off
;
404 unsigned char *uto
= (unsigned char *) to
;
405 enum floatformat_byteorders order
= fmt
->byteorder
;
406 unsigned char newto
[FLOATFORMAT_LARGEST_BYTES
];
408 if (order
!= floatformat_little
)
409 order
= floatformat_big
;
411 if (order
!= fmt
->byteorder
)
414 memcpy (&dfrom
, from
, sizeof (dfrom
));
415 memset (uto
, 0, (fmt
->totalsize
+ FLOATFORMAT_CHAR_BIT
- 1)
416 / FLOATFORMAT_CHAR_BIT
);
420 /* Use static volatile to ensure that any excess precision is
421 removed via storing in memory, and so the top half really is
422 the result of converting to double. */
423 static volatile double dtop
, dbot
;
424 DOUBLEST dtopnv
, dbotnv
;
426 dtop
= (double) dfrom
;
427 /* If the rounded top half is Inf, the bottom must be 0 not NaN
429 if (dtop
+ dtop
== dtop
&& dtop
!= 0.0)
432 dbot
= (double) (dfrom
- (DOUBLEST
) dtop
);
435 floatformat_from_doublest (fmt
->split_half
, &dtopnv
, uto
);
436 floatformat_from_doublest (fmt
->split_half
, &dbotnv
,
438 + fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
/ 2));
443 return; /* Result is zero */
444 if (dfrom
!= dfrom
) /* Result is NaN */
447 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
448 fmt
->exp_len
, fmt
->exp_nan
);
449 /* Be sure it's not infinity, but NaN value is irrel */
450 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
452 goto finalize_byteorder
;
455 /* If negative, set the sign bit. */
458 put_field (uto
, order
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
462 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity */
464 /* Infinity exponent is same as NaN's. */
465 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
466 fmt
->exp_len
, fmt
->exp_nan
);
467 /* Infinity mantissa is all zeroes. */
468 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
470 goto finalize_byteorder
;
473 #ifdef HAVE_LONG_DOUBLE
474 mant
= ldfrexp (dfrom
, &exponent
);
476 mant
= frexp (dfrom
, &exponent
);
479 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
480 exponent
+ fmt
->exp_bias
- 1);
482 mant_bits_left
= fmt
->man_len
;
483 mant_off
= fmt
->man_start
;
484 while (mant_bits_left
> 0)
486 unsigned long mant_long
;
488 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
490 mant
*= 4294967296.0;
491 mant_long
= ((unsigned long) mant
) & 0xffffffffL
;
494 /* If the integer bit is implicit, then we need to discard it.
495 If we are discarding a zero, we should be (but are not) creating
496 a denormalized number which means adjusting the exponent
498 if (mant_bits_left
== fmt
->man_len
499 && fmt
->intbit
== floatformat_intbit_no
)
502 mant_long
&= 0xffffffffL
;
503 /* If we are processing the top 32 mantissa bits of a doublest
504 so as to convert to a float value with implied integer bit,
505 we will only be putting 31 of those 32 bits into the
506 final value due to the discarding of the top bit. In the
507 case of a small float value where the number of mantissa
508 bits is less than 32, discarding the top bit does not alter
509 the number of bits we will be adding to the result. */
516 /* The bits we want are in the most significant MANT_BITS bits of
517 mant_long. Move them to the least significant. */
518 mant_long
>>= 32 - mant_bits
;
521 put_field (uto
, order
, fmt
->totalsize
,
522 mant_off
, mant_bits
, mant_long
);
523 mant_off
+= mant_bits
;
524 mant_bits_left
-= mant_bits
;
528 /* Do we need to byte-swap the words in the result? */
529 if (order
!= fmt
->byteorder
)
530 floatformat_normalize_byteorder (fmt
, newto
, to
);
533 /* Check if VAL (which is assumed to be a floating point number whose
534 format is described by FMT) is negative. */
537 floatformat_is_negative (const struct floatformat
*fmt
,
538 const bfd_byte
*uval
)
540 enum floatformat_byteorders order
;
541 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
543 gdb_assert (fmt
!= NULL
);
544 gdb_assert (fmt
->totalsize
545 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
547 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
549 if (order
!= fmt
->byteorder
)
552 return get_field (uval
, order
, fmt
->totalsize
, fmt
->sign_start
, 1);
555 /* Check if VAL is "not a number" (NaN) for FMT. */
558 floatformat_classify (const struct floatformat
*fmt
,
559 const bfd_byte
*uval
)
563 unsigned int mant_bits
, mant_off
;
565 enum floatformat_byteorders order
;
566 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
569 gdb_assert (fmt
!= NULL
);
570 gdb_assert (fmt
->totalsize
571 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
573 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
575 if (order
!= fmt
->byteorder
)
578 exponent
= get_field (uval
, order
, fmt
->totalsize
, fmt
->exp_start
,
581 mant_bits_left
= fmt
->man_len
;
582 mant_off
= fmt
->man_start
;
585 while (mant_bits_left
> 0)
587 mant_bits
= min (mant_bits_left
, 32);
589 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
591 /* If there is an explicit integer bit, mask it off. */
592 if (mant_off
== fmt
->man_start
593 && fmt
->intbit
== floatformat_intbit_yes
)
594 mant
&= ~(1 << (mant_bits
- 1));
602 mant_off
+= mant_bits
;
603 mant_bits_left
-= mant_bits
;
606 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
616 if (exponent
== 0 && !mant_zero
)
617 return float_subnormal
;
619 if (exponent
== fmt
->exp_nan
)
622 return float_infinite
;
633 /* Convert the mantissa of VAL (which is assumed to be a floating
634 point number whose format is described by FMT) into a hexadecimal
635 and store it in a static string. Return a pointer to that string. */
638 floatformat_mantissa (const struct floatformat
*fmt
,
641 unsigned char *uval
= (unsigned char *) val
;
643 unsigned int mant_bits
, mant_off
;
648 enum floatformat_byteorders order
;
649 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
651 gdb_assert (fmt
!= NULL
);
652 gdb_assert (fmt
->totalsize
653 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
655 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
657 if (order
!= fmt
->byteorder
)
663 /* Make sure we have enough room to store the mantissa. */
664 gdb_assert (sizeof res
> ((fmt
->man_len
+ 7) / 8) * 2);
666 mant_off
= fmt
->man_start
;
667 mant_bits_left
= fmt
->man_len
;
668 mant_bits
= (mant_bits_left
% 32) > 0 ? mant_bits_left
% 32 : 32;
670 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
672 len
= xsnprintf (res
, sizeof res
, "%lx", mant
);
674 mant_off
+= mant_bits
;
675 mant_bits_left
-= mant_bits
;
677 while (mant_bits_left
> 0)
679 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, 32);
681 xsnprintf (buf
, sizeof buf
, "%08lx", mant
);
682 gdb_assert (len
+ strlen (buf
) <= sizeof res
);
686 mant_bits_left
-= 32;
693 /* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
695 If the host and target formats agree, we just copy the raw data
696 into the appropriate type of variable and return, letting the host
697 increase precision as necessary. Otherwise, we call the conversion
698 routine and let it do the dirty work. */
700 static const struct floatformat
*host_float_format
= GDB_HOST_FLOAT_FORMAT
;
701 static const struct floatformat
*host_double_format
= GDB_HOST_DOUBLE_FORMAT
;
702 static const struct floatformat
*host_long_double_format
= GDB_HOST_LONG_DOUBLE_FORMAT
;
705 floatformat_to_doublest (const struct floatformat
*fmt
,
706 const void *in
, DOUBLEST
*out
)
708 gdb_assert (fmt
!= NULL
);
709 if (fmt
== host_float_format
)
713 memcpy (&val
, in
, sizeof (val
));
716 else if (fmt
== host_double_format
)
720 memcpy (&val
, in
, sizeof (val
));
723 else if (fmt
== host_long_double_format
)
727 memcpy (&val
, in
, sizeof (val
));
731 convert_floatformat_to_doublest (fmt
, in
, out
);
735 floatformat_from_doublest (const struct floatformat
*fmt
,
736 const DOUBLEST
*in
, void *out
)
738 gdb_assert (fmt
!= NULL
);
739 if (fmt
== host_float_format
)
743 memcpy (out
, &val
, sizeof (val
));
745 else if (fmt
== host_double_format
)
749 memcpy (out
, &val
, sizeof (val
));
751 else if (fmt
== host_long_double_format
)
753 long double val
= *in
;
755 memcpy (out
, &val
, sizeof (val
));
758 convert_doublest_to_floatformat (fmt
, in
, out
);
762 /* Return a floating-point format for a floating-point variable of
763 length LEN. If no suitable floating-point format is found, an
766 We need this functionality since information about the
767 floating-point format of a type is not always available to GDB; the
768 debug information typically only tells us the size of a
771 FIXME: kettenis/2001-10-28: In many places, particularly in
772 target-dependent code, the format of floating-point types is known,
773 but not passed on by GDB. This should be fixed. */
775 static const struct floatformat
*
776 floatformat_from_length (struct gdbarch
*gdbarch
, int len
)
778 const struct floatformat
*format
;
780 if (len
* TARGET_CHAR_BIT
== gdbarch_half_bit (gdbarch
))
781 format
= gdbarch_half_format (gdbarch
)
782 [gdbarch_byte_order (gdbarch
)];
783 else if (len
* TARGET_CHAR_BIT
== gdbarch_float_bit (gdbarch
))
784 format
= gdbarch_float_format (gdbarch
)
785 [gdbarch_byte_order (gdbarch
)];
786 else if (len
* TARGET_CHAR_BIT
== gdbarch_double_bit (gdbarch
))
787 format
= gdbarch_double_format (gdbarch
)
788 [gdbarch_byte_order (gdbarch
)];
789 else if (len
* TARGET_CHAR_BIT
== gdbarch_long_double_bit (gdbarch
))
790 format
= gdbarch_long_double_format (gdbarch
)
791 [gdbarch_byte_order (gdbarch
)];
792 /* On i386 the 'long double' type takes 96 bits,
793 while the real number of used bits is only 80,
794 both in processor and in memory.
795 The code below accepts the real bit size. */
796 else if ((gdbarch_long_double_format (gdbarch
) != NULL
)
797 && (len
* TARGET_CHAR_BIT
798 == gdbarch_long_double_format (gdbarch
)[0]->totalsize
))
799 format
= gdbarch_long_double_format (gdbarch
)
800 [gdbarch_byte_order (gdbarch
)];
804 error (_("Unrecognized %d-bit floating-point type."),
805 len
* TARGET_CHAR_BIT
);
809 const struct floatformat
*
810 floatformat_from_type (const struct type
*type
)
812 struct gdbarch
*gdbarch
= get_type_arch (type
);
814 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FLT
);
815 if (TYPE_FLOATFORMAT (type
) != NULL
)
816 return TYPE_FLOATFORMAT (type
)[gdbarch_byte_order (gdbarch
)];
818 return floatformat_from_length (gdbarch
, TYPE_LENGTH (type
));
821 /* Extract a floating-point number of type TYPE from a target-order
822 byte-stream at ADDR. Returns the value as type DOUBLEST. */
825 extract_typed_floating (const void *addr
, const struct type
*type
)
827 const struct floatformat
*fmt
= floatformat_from_type (type
);
830 floatformat_to_doublest (fmt
, addr
, &retval
);
834 /* Store VAL as a floating-point number of type TYPE to a target-order
835 byte-stream at ADDR. */
838 store_typed_floating (void *addr
, const struct type
*type
, DOUBLEST val
)
840 const struct floatformat
*fmt
= floatformat_from_type (type
);
842 /* FIXME: kettenis/2001-10-28: It is debatable whether we should
843 zero out any remaining bytes in the target buffer when TYPE is
844 longer than the actual underlying floating-point format. Perhaps
845 we should store a fixed bitpattern in those remaining bytes,
846 instead of zero, or perhaps we shouldn't touch those remaining
849 NOTE: cagney/2001-10-28: With the way things currently work, it
850 isn't a good idea to leave the end bits undefined. This is
851 because GDB writes out the entire sizeof(<floating>) bits of the
852 floating-point type even though the value might only be stored
853 in, and the target processor may only refer to, the first N <
854 TYPE_LENGTH (type) bits. If the end of the buffer wasn't
855 initialized, GDB would write undefined data to the target. An
856 errant program, refering to that undefined data, would then
857 become non-deterministic.
859 See also the function convert_typed_floating below. */
860 memset (addr
, 0, TYPE_LENGTH (type
));
862 floatformat_from_doublest (fmt
, &val
, addr
);
865 /* Convert a floating-point number of type FROM_TYPE from a
866 target-order byte-stream at FROM to a floating-point number of type
867 TO_TYPE, and store it to a target-order byte-stream at TO. */
870 convert_typed_floating (const void *from
, const struct type
*from_type
,
871 void *to
, const struct type
*to_type
)
873 const struct floatformat
*from_fmt
= floatformat_from_type (from_type
);
874 const struct floatformat
*to_fmt
= floatformat_from_type (to_type
);
876 if (from_fmt
== NULL
|| to_fmt
== NULL
)
878 /* If we don't know the floating-point format of FROM_TYPE or
879 TO_TYPE, there's not much we can do. We might make the
880 assumption that if the length of FROM_TYPE and TO_TYPE match,
881 their floating-point format would match too, but that
882 assumption might be wrong on targets that support
883 floating-point types that only differ in endianness for
884 example. So we warn instead, and zero out the target buffer. */
885 warning (_("Can't convert floating-point number to desired type."));
886 memset (to
, 0, TYPE_LENGTH (to_type
));
888 else if (from_fmt
== to_fmt
)
890 /* We're in business. The floating-point format of FROM_TYPE
891 and TO_TYPE match. However, even though the floating-point
892 format matches, the length of the type might still be
893 different. Make sure we don't overrun any buffers. See
894 comment in store_typed_floating for a discussion about
895 zeroing out remaining bytes in the target buffer. */
896 memset (to
, 0, TYPE_LENGTH (to_type
));
897 memcpy (to
, from
, min (TYPE_LENGTH (from_type
), TYPE_LENGTH (to_type
)));
901 /* The floating-point types don't match. The best we can do
902 (apart from simulating the target FPU) is converting to the
903 widest floating-point type supported by the host, and then
904 again to the desired type. */
907 floatformat_to_doublest (from_fmt
, from
, &d
);
908 floatformat_from_doublest (to_fmt
, &d
, to
);
912 const struct floatformat
*floatformat_ieee_single
[BFD_ENDIAN_UNKNOWN
];
913 const struct floatformat
*floatformat_ieee_double
[BFD_ENDIAN_UNKNOWN
];
914 const struct floatformat
*floatformat_ieee_quad
[BFD_ENDIAN_UNKNOWN
];
915 const struct floatformat
*floatformat_arm_ext
[BFD_ENDIAN_UNKNOWN
];
916 const struct floatformat
*floatformat_ia64_spill
[BFD_ENDIAN_UNKNOWN
];
918 extern void _initialize_doublest (void);
921 _initialize_doublest (void)
923 floatformat_ieee_single
[BFD_ENDIAN_LITTLE
] = &floatformat_ieee_single_little
;
924 floatformat_ieee_single
[BFD_ENDIAN_BIG
] = &floatformat_ieee_single_big
;
925 floatformat_ieee_double
[BFD_ENDIAN_LITTLE
] = &floatformat_ieee_double_little
;
926 floatformat_ieee_double
[BFD_ENDIAN_BIG
] = &floatformat_ieee_double_big
;
927 floatformat_arm_ext
[BFD_ENDIAN_LITTLE
] = &floatformat_arm_ext_littlebyte_bigword
;
928 floatformat_arm_ext
[BFD_ENDIAN_BIG
] = &floatformat_arm_ext_big
;
929 floatformat_ia64_spill
[BFD_ENDIAN_LITTLE
] = &floatformat_ia64_spill_little
;
930 floatformat_ia64_spill
[BFD_ENDIAN_BIG
] = &floatformat_ia64_spill_big
;
931 floatformat_ieee_quad
[BFD_ENDIAN_LITTLE
] = &floatformat_ia64_quad_little
;
932 floatformat_ieee_quad
[BFD_ENDIAN_BIG
] = &floatformat_ia64_quad_big
;