1 /* Floating point routines for GDB, the GNU debugger.
3 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004 Free Software
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., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, 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 static unsigned long get_field (unsigned char *,
48 enum floatformat_byteorders
,
49 unsigned int, unsigned int, unsigned int);
51 /* Extract a field which starts at START and is LEN bytes long. DATA and
52 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
54 get_field (unsigned char *data
, enum floatformat_byteorders order
,
55 unsigned int total_len
, unsigned int start
, unsigned int len
)
58 unsigned int cur_byte
;
61 /* Caller must byte-swap words before calling this routine. */
62 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
64 /* Start at the least significant part of the field. */
65 if (order
== floatformat_little
)
67 /* We start counting from the other end (i.e, from the high bytes
68 rather than the low bytes). As such, we need to be concerned
69 with what happens if bit 0 doesn't start on a byte boundary.
70 I.e, we need to properly handle the case where total_len is
71 not evenly divisible by 8. So we compute ``excess'' which
72 represents the number of bits from the end of our starting
73 byte needed to get to bit 0. */
74 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
75 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
76 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
77 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
78 - FLOATFORMAT_CHAR_BIT
;
82 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
84 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
86 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
87 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
90 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
91 if (order
== floatformat_little
)
96 /* Move towards the most significant part of the field. */
97 while (cur_bitshift
< len
)
99 result
|= (unsigned long)*(data
+ cur_byte
) << cur_bitshift
;
100 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
103 case floatformat_little
:
106 case floatformat_big
:
111 if (len
< sizeof(result
) * FLOATFORMAT_CHAR_BIT
)
112 /* Mask out bits which are not part of the field */
113 result
&= ((1UL << len
) - 1);
117 /* Normalize the byte order of FROM into TO. If no normalization is needed
118 then FMT->byteorder is returned and TO is not changed; otherwise the format
119 of the normalized form in TO is returned. */
120 static enum floatformat_byteorders
121 floatformat_normalize_byteorder (const struct floatformat
*fmt
,
122 const void *from
, void *to
)
124 const unsigned char *swapin
;
125 unsigned char *swapout
;
128 if (fmt
->byteorder
== floatformat_little
129 || fmt
->byteorder
== floatformat_big
)
130 return fmt
->byteorder
;
132 gdb_assert (fmt
->byteorder
== floatformat_littlebyte_bigword
);
134 words
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
137 swapout
= (unsigned char *)to
;
138 swapin
= (const unsigned char *)from
;
142 *swapout
++ = swapin
[3];
143 *swapout
++ = swapin
[2];
144 *swapout
++ = swapin
[1];
145 *swapout
++ = swapin
[0];
148 return floatformat_big
;
151 /* Convert from FMT to a DOUBLEST.
152 FROM is the address of the extended float.
153 Store the DOUBLEST in *TO. */
156 convert_floatformat_to_doublest (const struct floatformat
*fmt
,
160 unsigned char *ufrom
= (unsigned char *) from
;
164 unsigned int mant_bits
, mant_off
;
166 int special_exponent
; /* It's a NaN, denorm or zero */
167 enum floatformat_byteorders order
;
168 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
170 gdb_assert (fmt
->totalsize
171 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
173 order
= floatformat_normalize_byteorder (fmt
, ufrom
, newfrom
);
175 if (order
!= fmt
->byteorder
)
178 exponent
= get_field (ufrom
, order
, fmt
->totalsize
, fmt
->exp_start
,
180 /* Note that if exponent indicates a NaN, we can't really do anything useful
181 (not knowing if the host has NaN's, or how to build one). So it will
182 end up as an infinity or something close; that is OK. */
184 mant_bits_left
= fmt
->man_len
;
185 mant_off
= fmt
->man_start
;
188 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
190 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
191 we don't check for zero as the exponent doesn't matter. Note the cast
192 to int; exp_bias is unsigned, so it's important to make sure the
193 operation is done in signed arithmetic. */
194 if (!special_exponent
)
195 exponent
-= fmt
->exp_bias
;
196 else if (exponent
== 0)
197 exponent
= 1 - fmt
->exp_bias
;
199 /* Build the result algebraically. Might go infinite, underflow, etc;
202 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
203 increment the exponent by one to account for the integer bit. */
205 if (!special_exponent
)
207 if (fmt
->intbit
== floatformat_intbit_no
)
208 dto
= ldexp (1.0, exponent
);
213 while (mant_bits_left
> 0)
215 mant_bits
= min (mant_bits_left
, 32);
217 mant
= get_field (ufrom
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
219 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
220 exponent
-= mant_bits
;
221 mant_off
+= mant_bits
;
222 mant_bits_left
-= mant_bits
;
225 /* Negate it if negative. */
226 if (get_field (ufrom
, order
, fmt
->totalsize
, fmt
->sign_start
, 1))
231 static void put_field (unsigned char *, enum floatformat_byteorders
,
233 unsigned int, unsigned int, unsigned long);
235 /* Set a field which starts at START and is LEN bytes long. DATA and
236 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
238 put_field (unsigned char *data
, enum floatformat_byteorders order
,
239 unsigned int total_len
, unsigned int start
, unsigned int len
,
240 unsigned long stuff_to_put
)
242 unsigned int cur_byte
;
245 /* Caller must byte-swap words before calling this routine. */
246 gdb_assert (order
== floatformat_little
|| order
== floatformat_big
);
248 /* Start at the least significant part of the field. */
249 if (order
== floatformat_little
)
251 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
252 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
253 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
254 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
255 - FLOATFORMAT_CHAR_BIT
;
259 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
261 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
263 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
265 *(data
+ cur_byte
) &=
266 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1)
268 *(data
+ cur_byte
) |=
269 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
271 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
272 if (order
== floatformat_little
)
277 /* Move towards the most significant part of the field. */
278 while (cur_bitshift
< len
)
280 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
282 /* This is the last byte. */
283 *(data
+ cur_byte
) &=
284 ~((1 << (len
- cur_bitshift
)) - 1);
285 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
288 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
289 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
290 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
291 if (order
== floatformat_little
)
298 #ifdef HAVE_LONG_DOUBLE
299 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
300 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
301 frexp, but operates on the long double data type. */
303 static long double ldfrexp (long double value
, int *eptr
);
306 ldfrexp (long double value
, int *eptr
)
311 /* Unfortunately, there are no portable functions for extracting the exponent
312 of a long double, so we have to do it iteratively by multiplying or dividing
313 by two until the fraction is between 0.5 and 1.0. */
321 if (value
>= tmp
) /* Value >= 1.0 */
327 else if (value
!= 0.0l) /* Value < 1.0 and > 0.0 */
341 #endif /* HAVE_LONG_DOUBLE */
344 /* The converse: convert the DOUBLEST *FROM to an extended float
345 and store where TO points. Neither FROM nor TO have any alignment
349 convert_doublest_to_floatformat (CONST
struct floatformat
*fmt
,
350 const DOUBLEST
*from
,
356 unsigned int mant_bits
, mant_off
;
358 unsigned char *uto
= (unsigned char *) to
;
359 enum floatformat_byteorders order
= fmt
->byteorder
;
361 if (order
== floatformat_littlebyte_bigword
)
362 order
= floatformat_big
;
364 memcpy (&dfrom
, from
, sizeof (dfrom
));
365 memset (uto
, 0, (fmt
->totalsize
+ FLOATFORMAT_CHAR_BIT
- 1)
366 / FLOATFORMAT_CHAR_BIT
);
368 return; /* Result is zero */
369 if (dfrom
!= dfrom
) /* Result is NaN */
372 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
373 fmt
->exp_len
, fmt
->exp_nan
);
374 /* Be sure it's not infinity, but NaN value is irrel */
375 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
377 goto finalize_byteorder
;
380 /* If negative, set the sign bit. */
383 put_field (uto
, order
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
387 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity */
389 /* Infinity exponent is same as NaN's. */
390 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
,
391 fmt
->exp_len
, fmt
->exp_nan
);
392 /* Infinity mantissa is all zeroes. */
393 put_field (uto
, order
, fmt
->totalsize
, fmt
->man_start
,
395 goto finalize_byteorder
;
398 #ifdef HAVE_LONG_DOUBLE
399 mant
= ldfrexp (dfrom
, &exponent
);
401 mant
= frexp (dfrom
, &exponent
);
404 put_field (uto
, order
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
405 exponent
+ fmt
->exp_bias
- 1);
407 mant_bits_left
= fmt
->man_len
;
408 mant_off
= fmt
->man_start
;
409 while (mant_bits_left
> 0)
411 unsigned long mant_long
;
412 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
414 mant
*= 4294967296.0;
415 mant_long
= ((unsigned long) mant
) & 0xffffffffL
;
418 /* If the integer bit is implicit, then we need to discard it.
419 If we are discarding a zero, we should be (but are not) creating
420 a denormalized number which means adjusting the exponent
422 if (mant_bits_left
== fmt
->man_len
423 && fmt
->intbit
== floatformat_intbit_no
)
426 mant_long
&= 0xffffffffL
;
427 /* If we are processing the top 32 mantissa bits of a doublest
428 so as to convert to a float value with implied integer bit,
429 we will only be putting 31 of those 32 bits into the
430 final value due to the discarding of the top bit. In the
431 case of a small float value where the number of mantissa
432 bits is less than 32, discarding the top bit does not alter
433 the number of bits we will be adding to the result. */
440 /* The bits we want are in the most significant MANT_BITS bits of
441 mant_long. Move them to the least significant. */
442 mant_long
>>= 32 - mant_bits
;
445 put_field (uto
, order
, fmt
->totalsize
,
446 mant_off
, mant_bits
, mant_long
);
447 mant_off
+= mant_bits
;
448 mant_bits_left
-= mant_bits
;
452 /* Do we need to byte-swap the words in the result? */
453 if (order
!= fmt
->byteorder
)
456 unsigned char *curword
= uto
;
459 words
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
464 curword
[0] = curword
[3];
467 curword
[1] = curword
[2];
474 /* Check if VAL (which is assumed to be a floating point number whose
475 format is described by FMT) is negative. */
478 floatformat_is_negative (const struct floatformat
*fmt
, char *val
)
480 unsigned char *uval
= (unsigned char *) val
;
481 enum floatformat_byteorders order
;
482 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
484 gdb_assert (fmt
!= NULL
);
485 gdb_assert (fmt
->totalsize
486 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
488 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
490 if (order
!= fmt
->byteorder
)
493 return get_field (uval
, order
, fmt
->totalsize
, fmt
->sign_start
, 1);
496 /* Check if VAL is "not a number" (NaN) for FMT. */
499 floatformat_is_nan (const struct floatformat
*fmt
, char *val
)
501 unsigned char *uval
= (unsigned char *) val
;
504 unsigned int mant_bits
, mant_off
;
506 enum floatformat_byteorders order
;
507 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
509 gdb_assert (fmt
!= NULL
);
510 gdb_assert (fmt
->totalsize
511 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
513 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
515 if (order
!= fmt
->byteorder
)
521 exponent
= get_field (uval
, order
, fmt
->totalsize
, fmt
->exp_start
,
524 if (exponent
!= fmt
->exp_nan
)
527 mant_bits_left
= fmt
->man_len
;
528 mant_off
= fmt
->man_start
;
530 while (mant_bits_left
> 0)
532 mant_bits
= min (mant_bits_left
, 32);
534 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
536 /* If there is an explicit integer bit, mask it off. */
537 if (mant_off
== fmt
->man_start
538 && fmt
->intbit
== floatformat_intbit_yes
)
539 mant
&= ~(1 << (mant_bits
- 1));
544 mant_off
+= mant_bits
;
545 mant_bits_left
-= mant_bits
;
551 /* Convert the mantissa of VAL (which is assumed to be a floating
552 point number whose format is described by FMT) into a hexadecimal
553 and store it in a static string. Return a pointer to that string. */
556 floatformat_mantissa (const struct floatformat
*fmt
, char *val
)
558 unsigned char *uval
= (unsigned char *) val
;
560 unsigned int mant_bits
, mant_off
;
564 enum floatformat_byteorders order
;
565 unsigned char newfrom
[FLOATFORMAT_LARGEST_BYTES
];
567 gdb_assert (fmt
!= NULL
);
568 gdb_assert (fmt
->totalsize
569 <= FLOATFORMAT_LARGEST_BYTES
* FLOATFORMAT_CHAR_BIT
);
571 order
= floatformat_normalize_byteorder (fmt
, uval
, newfrom
);
573 if (order
!= fmt
->byteorder
)
579 /* Make sure we have enough room to store the mantissa. */
580 gdb_assert (sizeof res
> ((fmt
->man_len
+ 7) / 8) * 2);
582 mant_off
= fmt
->man_start
;
583 mant_bits_left
= fmt
->man_len
;
584 mant_bits
= (mant_bits_left
% 32) > 0 ? mant_bits_left
% 32 : 32;
586 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, mant_bits
);
588 sprintf (res
, "%lx", mant
);
590 mant_off
+= mant_bits
;
591 mant_bits_left
-= mant_bits
;
593 while (mant_bits_left
> 0)
595 mant
= get_field (uval
, order
, fmt
->totalsize
, mant_off
, 32);
597 sprintf (buf
, "%08lx", mant
);
601 mant_bits_left
-= 32;
608 /* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
610 If the host and target formats agree, we just copy the raw data
611 into the appropriate type of variable and return, letting the host
612 increase precision as necessary. Otherwise, we call the conversion
613 routine and let it do the dirty work. */
615 static const struct floatformat
*host_float_format
= GDB_HOST_FLOAT_FORMAT
;
616 static const struct floatformat
*host_double_format
= GDB_HOST_DOUBLE_FORMAT
;
617 static const struct floatformat
*host_long_double_format
= GDB_HOST_LONG_DOUBLE_FORMAT
;
620 floatformat_to_doublest (const struct floatformat
*fmt
,
621 const void *in
, DOUBLEST
*out
)
623 gdb_assert (fmt
!= NULL
);
624 if (fmt
== host_float_format
)
627 memcpy (&val
, in
, sizeof (val
));
630 else if (fmt
== host_double_format
)
633 memcpy (&val
, in
, sizeof (val
));
636 else if (fmt
== host_long_double_format
)
639 memcpy (&val
, in
, sizeof (val
));
643 convert_floatformat_to_doublest (fmt
, in
, out
);
647 floatformat_from_doublest (const struct floatformat
*fmt
,
648 const DOUBLEST
*in
, void *out
)
650 gdb_assert (fmt
!= NULL
);
651 if (fmt
== host_float_format
)
654 memcpy (out
, &val
, sizeof (val
));
656 else if (fmt
== host_double_format
)
659 memcpy (out
, &val
, sizeof (val
));
661 else if (fmt
== host_long_double_format
)
663 long double val
= *in
;
664 memcpy (out
, &val
, sizeof (val
));
667 convert_doublest_to_floatformat (fmt
, in
, out
);
671 /* Return a floating-point format for a floating-point variable of
672 length LEN. If no suitable floating-point format is found, an
675 We need this functionality since information about the
676 floating-point format of a type is not always available to GDB; the
677 debug information typically only tells us the size of a
680 FIXME: kettenis/2001-10-28: In many places, particularly in
681 target-dependent code, the format of floating-point types is known,
682 but not passed on by GDB. This should be fixed. */
684 static const struct floatformat
*
685 floatformat_from_length (int len
)
687 const struct floatformat
*format
;
688 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
689 format
= TARGET_FLOAT_FORMAT
;
690 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
691 format
= TARGET_DOUBLE_FORMAT
;
692 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
693 format
= TARGET_LONG_DOUBLE_FORMAT
;
694 /* On i386 the 'long double' type takes 96 bits,
695 while the real number of used bits is only 80,
696 both in processor and in memory.
697 The code below accepts the real bit size. */
698 else if ((TARGET_LONG_DOUBLE_FORMAT
!= NULL
)
699 && (len
* TARGET_CHAR_BIT
==
700 TARGET_LONG_DOUBLE_FORMAT
->totalsize
))
701 format
= TARGET_LONG_DOUBLE_FORMAT
;
705 error ("Unrecognized %d-bit floating-point type.",
706 len
* TARGET_CHAR_BIT
);
710 const struct floatformat
*
711 floatformat_from_type (const struct type
*type
)
713 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FLT
);
714 if (TYPE_FLOATFORMAT (type
) != NULL
)
715 return TYPE_FLOATFORMAT (type
);
717 return floatformat_from_length (TYPE_LENGTH (type
));
720 /* If the host doesn't define NAN, use zero instead. */
725 /* Extract a floating-point number of length LEN from a target-order
726 byte-stream at ADDR. Returns the value as type DOUBLEST. */
729 extract_floating_by_length (const void *addr
, int len
)
731 const struct floatformat
*fmt
= floatformat_from_length (len
);
734 floatformat_to_doublest (fmt
, addr
, &val
);
739 deprecated_extract_floating (const void *addr
, int len
)
741 return extract_floating_by_length (addr
, len
);
744 /* Store VAL as a floating-point number of length LEN to a
745 target-order byte-stream at ADDR. */
748 store_floating_by_length (void *addr
, int len
, DOUBLEST val
)
750 const struct floatformat
*fmt
= floatformat_from_length (len
);
752 floatformat_from_doublest (fmt
, &val
, addr
);
756 deprecated_store_floating (void *addr
, int len
, DOUBLEST val
)
758 store_floating_by_length (addr
, len
, val
);
761 /* Extract a floating-point number of type TYPE from a target-order
762 byte-stream at ADDR. Returns the value as type DOUBLEST. */
765 extract_typed_floating (const void *addr
, const struct type
*type
)
769 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FLT
);
771 if (TYPE_FLOATFORMAT (type
) == NULL
)
772 /* Not all code remembers to set the FLOATFORMAT (language
773 specific code? stabs?) so handle that here as a special case. */
774 return extract_floating_by_length (addr
, TYPE_LENGTH (type
));
776 floatformat_to_doublest (TYPE_FLOATFORMAT (type
), addr
, &retval
);
780 /* Store VAL as a floating-point number of type TYPE to a target-order
781 byte-stream at ADDR. */
784 store_typed_floating (void *addr
, const struct type
*type
, DOUBLEST val
)
786 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FLT
);
788 /* FIXME: kettenis/2001-10-28: It is debatable whether we should
789 zero out any remaining bytes in the target buffer when TYPE is
790 longer than the actual underlying floating-point format. Perhaps
791 we should store a fixed bitpattern in those remaining bytes,
792 instead of zero, or perhaps we shouldn't touch those remaining
795 NOTE: cagney/2001-10-28: With the way things currently work, it
796 isn't a good idea to leave the end bits undefined. This is
797 because GDB writes out the entire sizeof(<floating>) bits of the
798 floating-point type even though the value might only be stored
799 in, and the target processor may only refer to, the first N <
800 TYPE_LENGTH (type) bits. If the end of the buffer wasn't
801 initialized, GDB would write undefined data to the target. An
802 errant program, refering to that undefined data, would then
803 become non-deterministic.
805 See also the function convert_typed_floating below. */
806 memset (addr
, 0, TYPE_LENGTH (type
));
808 if (TYPE_FLOATFORMAT (type
) == NULL
)
809 /* Not all code remembers to set the FLOATFORMAT (language
810 specific code? stabs?) so handle that here as a special case. */
811 store_floating_by_length (addr
, TYPE_LENGTH (type
), val
);
813 floatformat_from_doublest (TYPE_FLOATFORMAT (type
), &val
, addr
);
816 /* Convert a floating-point number of type FROM_TYPE from a
817 target-order byte-stream at FROM to a floating-point number of type
818 TO_TYPE, and store it to a target-order byte-stream at TO. */
821 convert_typed_floating (const void *from
, const struct type
*from_type
,
822 void *to
, const struct type
*to_type
)
824 const struct floatformat
*from_fmt
= floatformat_from_type (from_type
);
825 const struct floatformat
*to_fmt
= floatformat_from_type (to_type
);
827 gdb_assert (TYPE_CODE (from_type
) == TYPE_CODE_FLT
);
828 gdb_assert (TYPE_CODE (to_type
) == TYPE_CODE_FLT
);
830 if (from_fmt
== NULL
|| to_fmt
== NULL
)
832 /* If we don't know the floating-point format of FROM_TYPE or
833 TO_TYPE, there's not much we can do. We might make the
834 assumption that if the length of FROM_TYPE and TO_TYPE match,
835 their floating-point format would match too, but that
836 assumption might be wrong on targets that support
837 floating-point types that only differ in endianness for
838 example. So we warn instead, and zero out the target buffer. */
839 warning ("Can't convert floating-point number to desired type.");
840 memset (to
, 0, TYPE_LENGTH (to_type
));
842 else if (from_fmt
== to_fmt
)
844 /* We're in business. The floating-point format of FROM_TYPE
845 and TO_TYPE match. However, even though the floating-point
846 format matches, the length of the type might still be
847 different. Make sure we don't overrun any buffers. See
848 comment in store_typed_floating for a discussion about
849 zeroing out remaining bytes in the target buffer. */
850 memset (to
, 0, TYPE_LENGTH (to_type
));
851 memcpy (to
, from
, min (TYPE_LENGTH (from_type
), TYPE_LENGTH (to_type
)));
855 /* The floating-point types don't match. The best we can do
856 (aport from simulating the target FPU) is converting to the
857 widest floating-point type supported by the host, and then
858 again to the desired type. */
861 floatformat_to_doublest (from_fmt
, from
, &d
);
862 floatformat_from_doublest (to_fmt
, &d
, to
);
866 const struct floatformat
*floatformat_ieee_single
[BFD_ENDIAN_UNKNOWN
];
867 const struct floatformat
*floatformat_ieee_double
[BFD_ENDIAN_UNKNOWN
];
868 const struct floatformat
*floatformat_ieee_quad
[BFD_ENDIAN_UNKNOWN
];
869 const struct floatformat
*floatformat_arm_ext
[BFD_ENDIAN_UNKNOWN
];
870 const struct floatformat
*floatformat_ia64_spill
[BFD_ENDIAN_UNKNOWN
];
872 extern void _initialize_doublest (void);
875 _initialize_doublest (void)
877 floatformat_ieee_single
[BFD_ENDIAN_LITTLE
] = &floatformat_ieee_single_little
;
878 floatformat_ieee_single
[BFD_ENDIAN_BIG
] = &floatformat_ieee_single_big
;
879 floatformat_ieee_double
[BFD_ENDIAN_LITTLE
] = &floatformat_ieee_double_little
;
880 floatformat_ieee_double
[BFD_ENDIAN_BIG
] = &floatformat_ieee_double_big
;
881 floatformat_arm_ext
[BFD_ENDIAN_LITTLE
] = &floatformat_arm_ext_littlebyte_bigword
;
882 floatformat_arm_ext
[BFD_ENDIAN_BIG
] = &floatformat_arm_ext_big
;
883 floatformat_ia64_spill
[BFD_ENDIAN_LITTLE
] = &floatformat_ia64_spill_little
;
884 floatformat_ia64_spill
[BFD_ENDIAN_BIG
] = &floatformat_ia64_spill_big
;
885 floatformat_ieee_quad
[BFD_ENDIAN_LITTLE
] = &floatformat_ia64_quad_little
;
886 floatformat_ieee_quad
[BFD_ENDIAN_BIG
] = &floatformat_ia64_quad_big
;