1 /* Floating point routines for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3 1997, 1998, 1999, 2000, 2001
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Support for converting target fp numbers into host DOUBLEST format. */
25 /* XXX - This code should really be in libiberty/floatformat.c,
26 however configuration issues with libiberty made this very
27 difficult to do in the available time. */
31 #include "floatformat.h"
32 #include "gdb_assert.h"
33 #include "gdb_string.h"
35 #include <math.h> /* ldexp */
37 /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
38 going to bother with trying to muck around with whether it is defined in
39 a system header, what we do if not, etc. */
40 #define FLOATFORMAT_CHAR_BIT 8
42 static unsigned long get_field (unsigned char *,
43 enum floatformat_byteorders
,
44 unsigned int, unsigned int, unsigned int);
46 /* Extract a field which starts at START and is LEN bytes long. DATA and
47 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
49 get_field (unsigned char *data
, enum floatformat_byteorders order
,
50 unsigned int total_len
, unsigned int start
, unsigned int len
)
53 unsigned int cur_byte
;
56 /* Start at the least significant part of the field. */
57 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
59 /* We start counting from the other end (i.e, from the high bytes
60 rather than the low bytes). As such, we need to be concerned
61 with what happens if bit 0 doesn't start on a byte boundary.
62 I.e, we need to properly handle the case where total_len is
63 not evenly divisible by 8. So we compute ``excess'' which
64 represents the number of bits from the end of our starting
65 byte needed to get to bit 0. */
66 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
|| order
== floatformat_littlebyte_bigword
)
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
;
93 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
98 if (len
< sizeof(result
) * FLOATFORMAT_CHAR_BIT
)
99 /* Mask out bits which are not part of the field */
100 result
&= ((1UL << len
) - 1);
104 /* Convert from FMT to a DOUBLEST.
105 FROM is the address of the extended float.
106 Store the DOUBLEST in *TO. */
109 convert_floatformat_to_doublest (const struct floatformat
*fmt
,
113 unsigned char *ufrom
= (unsigned char *) from
;
117 unsigned int mant_bits
, mant_off
;
119 int special_exponent
; /* It's a NaN, denorm or zero */
121 /* If the mantissa bits are not contiguous from one end of the
122 mantissa to the other, we need to make a private copy of the
123 source bytes that is in the right order since the unpacking
124 algorithm assumes that the bits are contiguous.
126 Swap the bytes individually rather than accessing them through
127 "long *" since we have no guarantee that they start on a long
128 alignment, and also sizeof(long) for the host could be different
129 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
130 for the target is 4. */
132 if (fmt
->byteorder
== floatformat_littlebyte_bigword
)
134 static unsigned char *newfrom
;
135 unsigned char *swapin
, *swapout
;
138 longswaps
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
143 newfrom
= (unsigned char *) xmalloc (fmt
->totalsize
);
148 while (longswaps
-- > 0)
150 /* This is ugly, but efficient */
151 *swapout
++ = swapin
[4];
152 *swapout
++ = swapin
[5];
153 *swapout
++ = swapin
[6];
154 *swapout
++ = swapin
[7];
155 *swapout
++ = swapin
[0];
156 *swapout
++ = swapin
[1];
157 *swapout
++ = swapin
[2];
158 *swapout
++ = swapin
[3];
163 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
164 fmt
->exp_start
, fmt
->exp_len
);
165 /* Note that if exponent indicates a NaN, we can't really do anything useful
166 (not knowing if the host has NaN's, or how to build one). So it will
167 end up as an infinity or something close; that is OK. */
169 mant_bits_left
= fmt
->man_len
;
170 mant_off
= fmt
->man_start
;
173 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
175 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
176 we don't check for zero as the exponent doesn't matter. */
177 if (!special_exponent
)
178 exponent
-= fmt
->exp_bias
;
179 else if (exponent
== 0)
180 exponent
= 1 - fmt
->exp_bias
;
182 /* Build the result algebraically. Might go infinite, underflow, etc;
185 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
186 increment the exponent by one to account for the integer bit. */
188 if (!special_exponent
)
190 if (fmt
->intbit
== floatformat_intbit_no
)
191 dto
= ldexp (1.0, exponent
);
196 while (mant_bits_left
> 0)
198 mant_bits
= min (mant_bits_left
, 32);
200 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
201 mant_off
, mant_bits
);
203 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
204 exponent
-= mant_bits
;
205 mant_off
+= mant_bits
;
206 mant_bits_left
-= mant_bits
;
209 /* Negate it if negative. */
210 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
215 static void put_field (unsigned char *, enum floatformat_byteorders
,
217 unsigned int, unsigned int, unsigned long);
219 /* Set a field which starts at START and is LEN bytes long. DATA and
220 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
222 put_field (unsigned char *data
, enum floatformat_byteorders order
,
223 unsigned int total_len
, unsigned int start
, unsigned int len
,
224 unsigned long stuff_to_put
)
226 unsigned int cur_byte
;
229 /* Start at the least significant part of the field. */
230 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
232 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
233 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
234 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
235 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
236 - FLOATFORMAT_CHAR_BIT
;
240 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
242 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
244 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
246 *(data
+ cur_byte
) &=
247 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1)
249 *(data
+ cur_byte
) |=
250 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
252 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
253 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
258 /* Move towards the most significant part of the field. */
259 while (cur_bitshift
< len
)
261 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
263 /* This is the last byte. */
264 *(data
+ cur_byte
) &=
265 ~((1 << (len
- cur_bitshift
)) - 1);
266 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
269 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
270 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
271 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
272 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
279 #ifdef HAVE_LONG_DOUBLE
280 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
281 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
282 frexp, but operates on the long double data type. */
284 static long double ldfrexp (long double value
, int *eptr
);
287 ldfrexp (long double value
, int *eptr
)
292 /* Unfortunately, there are no portable functions for extracting the exponent
293 of a long double, so we have to do it iteratively by multiplying or dividing
294 by two until the fraction is between 0.5 and 1.0. */
302 if (value
>= tmp
) /* Value >= 1.0 */
308 else if (value
!= 0.0l) /* Value < 1.0 and > 0.0 */
322 #endif /* HAVE_LONG_DOUBLE */
325 /* The converse: convert the DOUBLEST *FROM to an extended float
326 and store where TO points. Neither FROM nor TO have any alignment
330 convert_doublest_to_floatformat (CONST
struct floatformat
*fmt
,
331 const DOUBLEST
*from
,
337 unsigned int mant_bits
, mant_off
;
339 unsigned char *uto
= (unsigned char *) to
;
341 memcpy (&dfrom
, from
, sizeof (dfrom
));
342 memset (uto
, 0, (fmt
->totalsize
+ FLOATFORMAT_CHAR_BIT
- 1)
343 / FLOATFORMAT_CHAR_BIT
);
345 return; /* Result is zero */
346 if (dfrom
!= dfrom
) /* Result is NaN */
349 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
350 fmt
->exp_len
, fmt
->exp_nan
);
351 /* Be sure it's not infinity, but NaN value is irrel */
352 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
357 /* If negative, set the sign bit. */
360 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
364 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity */
366 /* Infinity exponent is same as NaN's. */
367 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
368 fmt
->exp_len
, fmt
->exp_nan
);
369 /* Infinity mantissa is all zeroes. */
370 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
375 #ifdef HAVE_LONG_DOUBLE
376 mant
= ldfrexp (dfrom
, &exponent
);
378 mant
= frexp (dfrom
, &exponent
);
381 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
382 exponent
+ fmt
->exp_bias
- 1);
384 mant_bits_left
= fmt
->man_len
;
385 mant_off
= fmt
->man_start
;
386 while (mant_bits_left
> 0)
388 unsigned long mant_long
;
389 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
391 mant
*= 4294967296.0;
392 mant_long
= ((unsigned long) mant
) & 0xffffffffL
;
395 /* If the integer bit is implicit, then we need to discard it.
396 If we are discarding a zero, we should be (but are not) creating
397 a denormalized number which means adjusting the exponent
399 if (mant_bits_left
== fmt
->man_len
400 && fmt
->intbit
== floatformat_intbit_no
)
403 mant_long
&= 0xffffffffL
;
409 /* The bits we want are in the most significant MANT_BITS bits of
410 mant_long. Move them to the least significant. */
411 mant_long
>>= 32 - mant_bits
;
414 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
415 mant_off
, mant_bits
, mant_long
);
416 mant_off
+= mant_bits
;
417 mant_bits_left
-= mant_bits
;
419 if (fmt
->byteorder
== floatformat_littlebyte_bigword
)
422 unsigned char *swaplow
= uto
;
423 unsigned char *swaphigh
= uto
+ 4;
426 for (count
= 0; count
< 4; count
++)
429 *swaplow
++ = *swaphigh
;
435 /* Check if VAL (which is assumed to be a floating point number whose
436 format is described by FMT) is negative. */
439 floatformat_is_negative (const struct floatformat
*fmt
, char *val
)
441 unsigned char *uval
= (unsigned char *) val
;
443 return get_field (uval
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1);
446 /* Check if VAL is "not a number" (NaN) for FMT. */
449 floatformat_is_nan (const struct floatformat
*fmt
, char *val
)
451 unsigned char *uval
= (unsigned char *) val
;
454 unsigned int mant_bits
, mant_off
;
460 exponent
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
461 fmt
->exp_start
, fmt
->exp_len
);
463 if (exponent
!= fmt
->exp_nan
)
466 mant_bits_left
= fmt
->man_len
;
467 mant_off
= fmt
->man_start
;
469 while (mant_bits_left
> 0)
471 mant_bits
= min (mant_bits_left
, 32);
473 mant
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
474 mant_off
, mant_bits
);
476 /* If there is an explicit integer bit, mask it off. */
477 if (mant_off
== fmt
->man_start
478 && fmt
->intbit
== floatformat_intbit_yes
)
479 mant
&= ~(1 << (mant_bits
- 1));
484 mant_off
+= mant_bits
;
485 mant_bits_left
-= mant_bits
;
491 /* Convert the mantissa of VAL (which is assumed to be a floating
492 point number whose format is described by FMT) into a hexadecimal
493 and store it in a static string. Return a pointer to that string. */
496 floatformat_mantissa (const struct floatformat
*fmt
, char *val
)
498 unsigned char *uval
= (unsigned char *) val
;
500 unsigned int mant_bits
, mant_off
;
505 /* Make sure we have enough room to store the mantissa. */
506 gdb_assert (sizeof res
> ((fmt
->man_len
+ 7) / 8) * 2);
508 mant_off
= fmt
->man_start
;
509 mant_bits_left
= fmt
->man_len
;
510 mant_bits
= (mant_bits_left
% 32) > 0 ? mant_bits_left
% 32 : 32;
512 mant
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
513 mant_off
, mant_bits
);
515 sprintf (res
, "%lx", mant
);
517 mant_off
+= mant_bits
;
518 mant_bits_left
-= mant_bits
;
520 while (mant_bits_left
> 0)
522 mant
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
525 sprintf (buf
, "%08lx", mant
);
529 mant_bits_left
-= 32;
537 /* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
539 If the host and target formats agree, we just copy the raw data
540 into the appropriate type of variable and return, letting the host
541 increase precision as necessary. Otherwise, we call the conversion
542 routine and let it do the dirty work. */
544 #ifndef HOST_FLOAT_FORMAT
545 #define HOST_FLOAT_FORMAT 0
547 #ifndef HOST_DOUBLE_FORMAT
548 #define HOST_DOUBLE_FORMAT 0
550 #ifndef HOST_LONG_DOUBLE_FORMAT
551 #define HOST_LONG_DOUBLE_FORMAT 0
554 static const struct floatformat
*host_float_format
= HOST_FLOAT_FORMAT
;
555 static const struct floatformat
*host_double_format
= HOST_DOUBLE_FORMAT
;
556 static const struct floatformat
*host_long_double_format
= HOST_LONG_DOUBLE_FORMAT
;
559 floatformat_to_doublest (const struct floatformat
*fmt
,
560 const void *in
, DOUBLEST
*out
)
562 gdb_assert (fmt
!= NULL
);
563 if (fmt
== host_float_format
)
566 memcpy (&val
, in
, sizeof (val
));
569 else if (fmt
== host_double_format
)
572 memcpy (&val
, in
, sizeof (val
));
575 else if (fmt
== host_long_double_format
)
578 memcpy (&val
, in
, sizeof (val
));
582 convert_floatformat_to_doublest (fmt
, in
, out
);
586 floatformat_from_doublest (const struct floatformat
*fmt
,
587 const DOUBLEST
*in
, void *out
)
589 gdb_assert (fmt
!= NULL
);
590 if (fmt
== host_float_format
)
593 memcpy (out
, &val
, sizeof (val
));
595 else if (fmt
== host_double_format
)
598 memcpy (out
, &val
, sizeof (val
));
600 else if (fmt
== host_long_double_format
)
602 long double val
= *in
;
603 memcpy (out
, &val
, sizeof (val
));
606 convert_doublest_to_floatformat (fmt
, in
, out
);
610 /* Extract/store a target floating-point number from byte-stream at
611 ADDR to/from a DOUBLEST. The LEN is used to select between the
612 pre-defined target type FLOAT, DOUBLE or LONG_DOUBLE. These
613 functions are used when extract/store typed floating() find that
614 the ``struct type'' did not include a FLOATFORMAT (e.g. some symbol
615 table readers and XXX-language support modules). */
618 extract_floating (const void *addr
, int len
)
621 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
623 floatformat_to_doublest (TARGET_FLOAT_FORMAT
, addr
, &dretval
);
625 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
627 floatformat_to_doublest (TARGET_DOUBLE_FORMAT
, addr
, &dretval
);
629 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
631 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT
, addr
, &dretval
);
635 error ("Can't deal with a floating point number of %d bytes.", len
);
641 store_floating (void *addr
, int len
, DOUBLEST val
)
643 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
645 floatformat_from_doublest (TARGET_FLOAT_FORMAT
, &val
, addr
);
647 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
649 floatformat_from_doublest (TARGET_DOUBLE_FORMAT
, &val
, addr
);
651 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
653 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT
, &val
, addr
);
657 error ("Can't deal with a floating point number of %d bytes.", len
);
661 /* Extract/store a floating-point number of format TYPE from a
662 target-ordered byte-stream at ADDR to/from an internal DOUBLEST
663 accroding to its TYPE_FORMAT(). When GDB reads in debug
664 information, it is sometimes only provided with the type name, its
665 length and the fact that it is a float (TYPE_FORMAT() is not set).
666 For such types, the old extract/store floating() functions are
670 extract_typed_floating (const void *addr
, const struct type
*type
)
673 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FLT
);
674 if (TYPE_FLOATFORMAT (type
) == NULL
)
675 retval
= extract_floating (addr
, TYPE_LENGTH (type
));
677 floatformat_to_doublest (TYPE_FLOATFORMAT (type
), addr
, &retval
);
682 store_typed_floating (void *addr
, const struct type
*type
, DOUBLEST val
)
684 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_FLT
);
685 memset (addr
, 0, TYPE_LENGTH (type
));
686 if (TYPE_FLOATFORMAT (type
) == NULL
)
687 store_floating (addr
, TYPE_LENGTH (type
), val
);
689 floatformat_from_doublest (TYPE_FLOATFORMAT (type
), &val
, addr
);