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"
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 static unsigned long get_field (unsigned char *,
42 enum floatformat_byteorders
,
43 unsigned int, unsigned int, unsigned int);
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 (unsigned char *data
, enum floatformat_byteorders order
,
49 unsigned int total_len
, unsigned int start
, unsigned int len
)
52 unsigned int cur_byte
;
55 /* Start at the least significant part of the field. */
56 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
58 /* We start counting from the other end (i.e, from the high bytes
59 rather than the low bytes). As such, we need to be concerned
60 with what happens if bit 0 doesn't start on a byte boundary.
61 I.e, we need to properly handle the case where total_len is
62 not evenly divisible by 8. So we compute ``excess'' which
63 represents the number of bits from the end of our starting
64 byte needed to get to bit 0. */
65 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
66 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
67 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
68 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
69 - FLOATFORMAT_CHAR_BIT
;
73 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
75 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
77 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
78 result
= *(data
+ cur_byte
) >> (-cur_bitshift
);
81 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
82 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
87 /* Move towards the most significant part of the field. */
88 while (cur_bitshift
< len
)
90 result
|= (unsigned long)*(data
+ cur_byte
) << cur_bitshift
;
91 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
92 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
97 if (len
< sizeof(result
) * FLOATFORMAT_CHAR_BIT
)
98 /* Mask out bits which are not part of the field */
99 result
&= ((1UL << len
) - 1);
103 /* Convert from FMT to a DOUBLEST.
104 FROM is the address of the extended float.
105 Store the DOUBLEST in *TO. */
108 floatformat_to_doublest (const struct floatformat
*fmt
,
112 unsigned char *ufrom
= (unsigned char *) from
;
116 unsigned int mant_bits
, mant_off
;
118 int special_exponent
; /* It's a NaN, denorm or zero */
120 /* If the mantissa bits are not contiguous from one end of the
121 mantissa to the other, we need to make a private copy of the
122 source bytes that is in the right order since the unpacking
123 algorithm assumes that the bits are contiguous.
125 Swap the bytes individually rather than accessing them through
126 "long *" since we have no guarantee that they start on a long
127 alignment, and also sizeof(long) for the host could be different
128 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
129 for the target is 4. */
131 if (fmt
->byteorder
== floatformat_littlebyte_bigword
)
133 static unsigned char *newfrom
;
134 unsigned char *swapin
, *swapout
;
137 longswaps
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
142 newfrom
= (unsigned char *) xmalloc (fmt
->totalsize
);
147 while (longswaps
-- > 0)
149 /* This is ugly, but efficient */
150 *swapout
++ = swapin
[4];
151 *swapout
++ = swapin
[5];
152 *swapout
++ = swapin
[6];
153 *swapout
++ = swapin
[7];
154 *swapout
++ = swapin
[0];
155 *swapout
++ = swapin
[1];
156 *swapout
++ = swapin
[2];
157 *swapout
++ = swapin
[3];
162 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
163 fmt
->exp_start
, fmt
->exp_len
);
164 /* Note that if exponent indicates a NaN, we can't really do anything useful
165 (not knowing if the host has NaN's, or how to build one). So it will
166 end up as an infinity or something close; that is OK. */
168 mant_bits_left
= fmt
->man_len
;
169 mant_off
= fmt
->man_start
;
172 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
174 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
175 we don't check for zero as the exponent doesn't matter. */
176 if (!special_exponent
)
177 exponent
-= fmt
->exp_bias
;
178 else if (exponent
== 0)
179 exponent
= 1 - fmt
->exp_bias
;
181 /* Build the result algebraically. Might go infinite, underflow, etc;
184 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
185 increment the exponent by one to account for the integer bit. */
187 if (!special_exponent
)
189 if (fmt
->intbit
== floatformat_intbit_no
)
190 dto
= ldexp (1.0, exponent
);
195 while (mant_bits_left
> 0)
197 mant_bits
= min (mant_bits_left
, 32);
199 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
200 mant_off
, mant_bits
);
202 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
203 exponent
-= mant_bits
;
204 mant_off
+= mant_bits
;
205 mant_bits_left
-= mant_bits
;
208 /* Negate it if negative. */
209 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
214 static void put_field (unsigned char *, enum floatformat_byteorders
,
216 unsigned int, unsigned int, unsigned long);
218 /* Set a field which starts at START and is LEN bytes long. DATA and
219 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
221 put_field (unsigned char *data
, enum floatformat_byteorders order
,
222 unsigned int total_len
, unsigned int start
, unsigned int len
,
223 unsigned long stuff_to_put
)
225 unsigned int cur_byte
;
228 /* Start at the least significant part of the field. */
229 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
231 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
232 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
233 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
234 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
235 - FLOATFORMAT_CHAR_BIT
;
239 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
241 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
243 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
245 *(data
+ cur_byte
) &=
246 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1)
248 *(data
+ cur_byte
) |=
249 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
251 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
252 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
257 /* Move towards the most significant part of the field. */
258 while (cur_bitshift
< len
)
260 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
262 /* This is the last byte. */
263 *(data
+ cur_byte
) &=
264 ~((1 << (len
- cur_bitshift
)) - 1);
265 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
268 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
269 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
270 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
271 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
278 #ifdef HAVE_LONG_DOUBLE
279 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
280 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
281 frexp, but operates on the long double data type. */
283 static long double ldfrexp (long double value
, int *eptr
);
286 ldfrexp (long double value
, int *eptr
)
291 /* Unfortunately, there are no portable functions for extracting the exponent
292 of a long double, so we have to do it iteratively by multiplying or dividing
293 by two until the fraction is between 0.5 and 1.0. */
301 if (value
>= tmp
) /* Value >= 1.0 */
307 else if (value
!= 0.0l) /* Value < 1.0 and > 0.0 */
321 #endif /* HAVE_LONG_DOUBLE */
324 /* The converse: convert the DOUBLEST *FROM to an extended float
325 and store where TO points. Neither FROM nor TO have any alignment
329 floatformat_from_doublest (CONST
struct floatformat
*fmt
,
330 const DOUBLEST
*from
,
336 unsigned int mant_bits
, mant_off
;
338 unsigned char *uto
= (unsigned char *) to
;
340 memcpy (&dfrom
, from
, sizeof (dfrom
));
341 memset (uto
, 0, (fmt
->totalsize
+ FLOATFORMAT_CHAR_BIT
- 1)
342 / FLOATFORMAT_CHAR_BIT
);
344 return; /* Result is zero */
345 if (dfrom
!= dfrom
) /* Result is NaN */
348 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
349 fmt
->exp_len
, fmt
->exp_nan
);
350 /* Be sure it's not infinity, but NaN value is irrel */
351 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
356 /* If negative, set the sign bit. */
359 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
363 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity */
365 /* Infinity exponent is same as NaN's. */
366 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
367 fmt
->exp_len
, fmt
->exp_nan
);
368 /* Infinity mantissa is all zeroes. */
369 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
374 #ifdef HAVE_LONG_DOUBLE
375 mant
= ldfrexp (dfrom
, &exponent
);
377 mant
= frexp (dfrom
, &exponent
);
380 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
381 exponent
+ fmt
->exp_bias
- 1);
383 mant_bits_left
= fmt
->man_len
;
384 mant_off
= fmt
->man_start
;
385 while (mant_bits_left
> 0)
387 unsigned long mant_long
;
388 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
390 mant
*= 4294967296.0;
391 mant_long
= ((unsigned long) mant
) & 0xffffffffL
;
394 /* If the integer bit is implicit, then we need to discard it.
395 If we are discarding a zero, we should be (but are not) creating
396 a denormalized number which means adjusting the exponent
398 if (mant_bits_left
== fmt
->man_len
399 && fmt
->intbit
== floatformat_intbit_no
)
402 mant_long
&= 0xffffffffL
;
408 /* The bits we want are in the most significant MANT_BITS bits of
409 mant_long. Move them to the least significant. */
410 mant_long
>>= 32 - mant_bits
;
413 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
414 mant_off
, mant_bits
, mant_long
);
415 mant_off
+= mant_bits
;
416 mant_bits_left
-= mant_bits
;
418 if (fmt
->byteorder
== floatformat_littlebyte_bigword
)
421 unsigned char *swaplow
= uto
;
422 unsigned char *swaphigh
= uto
+ 4;
425 for (count
= 0; count
< 4; count
++)
428 *swaplow
++ = *swaphigh
;
434 /* Check if VAL (which is assumed to be a floating point number whose
435 format is described by FMT) is negative. */
438 floatformat_is_negative (const struct floatformat
*fmt
, char *val
)
440 unsigned char *uval
= (unsigned char *) val
;
442 return get_field (uval
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1);
445 /* Check if VAL is "not a number" (NaN) for FMT. */
448 floatformat_is_nan (const struct floatformat
*fmt
, char *val
)
450 unsigned char *uval
= (unsigned char *) val
;
453 unsigned int mant_bits
, mant_off
;
459 exponent
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
460 fmt
->exp_start
, fmt
->exp_len
);
462 if (exponent
!= fmt
->exp_nan
)
465 mant_bits_left
= fmt
->man_len
;
466 mant_off
= fmt
->man_start
;
468 while (mant_bits_left
> 0)
470 mant_bits
= min (mant_bits_left
, 32);
472 mant
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
473 mant_off
, mant_bits
);
475 /* If there is an explicit integer bit, mask it off. */
476 if (mant_off
== fmt
->man_start
477 && fmt
->intbit
== floatformat_intbit_yes
)
478 mant
&= ~(1 << (mant_bits
- 1));
483 mant_off
+= mant_bits
;
484 mant_bits_left
-= mant_bits
;
490 /* Convert the mantissa of VAL (which is assumed to be a floating
491 point number whose format is described by FMT) into a hexadecimal
492 and store it in a static string. Return a pointer to that string. */
495 floatformat_mantissa (const struct floatformat
*fmt
, char *val
)
497 unsigned char *uval
= (unsigned char *) val
;
499 unsigned int mant_bits
, mant_off
;
504 /* Make sure we have enough room to store the mantissa. */
505 gdb_assert (sizeof res
> ((fmt
->man_len
+ 7) / 8) * 2);
507 mant_off
= fmt
->man_start
;
508 mant_bits_left
= fmt
->man_len
;
509 mant_bits
= (mant_bits_left
% 32) > 0 ? mant_bits_left
% 32 : 32;
511 mant
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
512 mant_off
, mant_bits
);
514 sprintf (res
, "%lx", mant
);
516 mant_off
+= mant_bits
;
517 mant_bits_left
-= mant_bits
;
519 while (mant_bits_left
> 0)
521 mant
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
524 sprintf (buf
, "%08lx", mant
);
528 mant_bits_left
-= 32;
536 /* Extract a floating-point number from a target-order byte-stream at ADDR.
537 Returns the value as type DOUBLEST.
539 If the host and target formats agree, we just copy the raw data into the
540 appropriate type of variable and return, letting the host increase precision
541 as necessary. Otherwise, we call the conversion routine and let it do the
545 extract_floating (const void *addr
, int len
)
549 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
551 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
555 memcpy (&retval
, addr
, sizeof (retval
));
559 floatformat_to_doublest (TARGET_FLOAT_FORMAT
, addr
, &dretval
);
561 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
563 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
567 memcpy (&retval
, addr
, sizeof (retval
));
571 floatformat_to_doublest (TARGET_DOUBLE_FORMAT
, addr
, &dretval
);
573 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
575 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
579 memcpy (&retval
, addr
, sizeof (retval
));
583 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT
, addr
, &dretval
);
587 error ("Can't deal with a floating point number of %d bytes.", len
);
594 store_floating (void *addr
, int len
, DOUBLEST val
)
596 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
598 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
600 float floatval
= val
;
602 memcpy (addr
, &floatval
, sizeof (floatval
));
605 floatformat_from_doublest (TARGET_FLOAT_FORMAT
, &val
, addr
);
607 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
609 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
611 double doubleval
= val
;
613 memcpy (addr
, &doubleval
, sizeof (doubleval
));
616 floatformat_from_doublest (TARGET_DOUBLE_FORMAT
, &val
, addr
);
618 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
620 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
621 memcpy (addr
, &val
, sizeof (val
));
623 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT
, &val
, addr
);
627 error ("Can't deal with a floating point number of %d bytes.", len
);