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
, char *from
,
111 unsigned char *ufrom
= (unsigned char *) from
;
115 unsigned int mant_bits
, mant_off
;
117 int special_exponent
; /* It's a NaN, denorm or zero */
119 /* If the mantissa bits are not contiguous from one end of the
120 mantissa to the other, we need to make a private copy of the
121 source bytes that is in the right order since the unpacking
122 algorithm assumes that the bits are contiguous.
124 Swap the bytes individually rather than accessing them through
125 "long *" since we have no guarantee that they start on a long
126 alignment, and also sizeof(long) for the host could be different
127 than sizeof(long) for the target. FIXME: Assumes sizeof(long)
128 for the target is 4. */
130 if (fmt
->byteorder
== floatformat_littlebyte_bigword
)
132 static unsigned char *newfrom
;
133 unsigned char *swapin
, *swapout
;
136 longswaps
= fmt
->totalsize
/ FLOATFORMAT_CHAR_BIT
;
141 newfrom
= (unsigned char *) xmalloc (fmt
->totalsize
);
146 while (longswaps
-- > 0)
148 /* This is ugly, but efficient */
149 *swapout
++ = swapin
[4];
150 *swapout
++ = swapin
[5];
151 *swapout
++ = swapin
[6];
152 *swapout
++ = swapin
[7];
153 *swapout
++ = swapin
[0];
154 *swapout
++ = swapin
[1];
155 *swapout
++ = swapin
[2];
156 *swapout
++ = swapin
[3];
161 exponent
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
162 fmt
->exp_start
, fmt
->exp_len
);
163 /* Note that if exponent indicates a NaN, we can't really do anything useful
164 (not knowing if the host has NaN's, or how to build one). So it will
165 end up as an infinity or something close; that is OK. */
167 mant_bits_left
= fmt
->man_len
;
168 mant_off
= fmt
->man_start
;
171 special_exponent
= exponent
== 0 || exponent
== fmt
->exp_nan
;
173 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
174 we don't check for zero as the exponent doesn't matter. */
175 if (!special_exponent
)
176 exponent
-= fmt
->exp_bias
;
177 else if (exponent
== 0)
178 exponent
= 1 - fmt
->exp_bias
;
180 /* Build the result algebraically. Might go infinite, underflow, etc;
183 /* If this format uses a hidden bit, explicitly add it in now. Otherwise,
184 increment the exponent by one to account for the integer bit. */
186 if (!special_exponent
)
188 if (fmt
->intbit
== floatformat_intbit_no
)
189 dto
= ldexp (1.0, exponent
);
194 while (mant_bits_left
> 0)
196 mant_bits
= min (mant_bits_left
, 32);
198 mant
= get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
,
199 mant_off
, mant_bits
);
201 dto
+= ldexp ((double) mant
, exponent
- mant_bits
);
202 exponent
-= mant_bits
;
203 mant_off
+= mant_bits
;
204 mant_bits_left
-= mant_bits
;
207 /* Negate it if negative. */
208 if (get_field (ufrom
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1))
213 static void put_field (unsigned char *, enum floatformat_byteorders
,
215 unsigned int, unsigned int, unsigned long);
217 /* Set a field which starts at START and is LEN bytes long. DATA and
218 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
220 put_field (unsigned char *data
, enum floatformat_byteorders order
,
221 unsigned int total_len
, unsigned int start
, unsigned int len
,
222 unsigned long stuff_to_put
)
224 unsigned int cur_byte
;
227 /* Start at the least significant part of the field. */
228 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
230 int excess
= FLOATFORMAT_CHAR_BIT
- (total_len
% FLOATFORMAT_CHAR_BIT
);
231 cur_byte
= (total_len
/ FLOATFORMAT_CHAR_BIT
)
232 - ((start
+ len
+ excess
) / FLOATFORMAT_CHAR_BIT
);
233 cur_bitshift
= ((start
+ len
+ excess
) % FLOATFORMAT_CHAR_BIT
)
234 - FLOATFORMAT_CHAR_BIT
;
238 cur_byte
= (start
+ len
) / FLOATFORMAT_CHAR_BIT
;
240 ((start
+ len
) % FLOATFORMAT_CHAR_BIT
) - FLOATFORMAT_CHAR_BIT
;
242 if (cur_bitshift
> -FLOATFORMAT_CHAR_BIT
)
244 *(data
+ cur_byte
) &=
245 ~(((1 << ((start
+ len
) % FLOATFORMAT_CHAR_BIT
)) - 1)
247 *(data
+ cur_byte
) |=
248 (stuff_to_put
& ((1 << FLOATFORMAT_CHAR_BIT
) - 1)) << (-cur_bitshift
);
250 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
251 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
256 /* Move towards the most significant part of the field. */
257 while (cur_bitshift
< len
)
259 if (len
- cur_bitshift
< FLOATFORMAT_CHAR_BIT
)
261 /* This is the last byte. */
262 *(data
+ cur_byte
) &=
263 ~((1 << (len
- cur_bitshift
)) - 1);
264 *(data
+ cur_byte
) |= (stuff_to_put
>> cur_bitshift
);
267 *(data
+ cur_byte
) = ((stuff_to_put
>> cur_bitshift
)
268 & ((1 << FLOATFORMAT_CHAR_BIT
) - 1));
269 cur_bitshift
+= FLOATFORMAT_CHAR_BIT
;
270 if (order
== floatformat_little
|| order
== floatformat_littlebyte_bigword
)
277 #ifdef HAVE_LONG_DOUBLE
278 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
279 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
280 frexp, but operates on the long double data type. */
282 static long double ldfrexp (long double value
, int *eptr
);
285 ldfrexp (long double value
, int *eptr
)
290 /* Unfortunately, there are no portable functions for extracting the exponent
291 of a long double, so we have to do it iteratively by multiplying or dividing
292 by two until the fraction is between 0.5 and 1.0. */
300 if (value
>= tmp
) /* Value >= 1.0 */
306 else if (value
!= 0.0l) /* Value < 1.0 and > 0.0 */
320 #endif /* HAVE_LONG_DOUBLE */
323 /* The converse: convert the DOUBLEST *FROM to an extended float
324 and store where TO points. Neither FROM nor TO have any alignment
328 floatformat_from_doublest (CONST
struct floatformat
*fmt
, DOUBLEST
*from
,
334 unsigned int mant_bits
, mant_off
;
336 unsigned char *uto
= (unsigned char *) to
;
338 memcpy (&dfrom
, from
, sizeof (dfrom
));
339 memset (uto
, 0, (fmt
->totalsize
+ FLOATFORMAT_CHAR_BIT
- 1)
340 / FLOATFORMAT_CHAR_BIT
);
342 return; /* Result is zero */
343 if (dfrom
!= dfrom
) /* Result is NaN */
346 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
347 fmt
->exp_len
, fmt
->exp_nan
);
348 /* Be sure it's not infinity, but NaN value is irrel */
349 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
354 /* If negative, set the sign bit. */
357 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1, 1);
361 if (dfrom
+ dfrom
== dfrom
&& dfrom
!= 0.0) /* Result is Infinity */
363 /* Infinity exponent is same as NaN's. */
364 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
,
365 fmt
->exp_len
, fmt
->exp_nan
);
366 /* Infinity mantissa is all zeroes. */
367 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->man_start
,
372 #ifdef HAVE_LONG_DOUBLE
373 mant
= ldfrexp (dfrom
, &exponent
);
375 mant
= frexp (dfrom
, &exponent
);
378 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
, fmt
->exp_start
, fmt
->exp_len
,
379 exponent
+ fmt
->exp_bias
- 1);
381 mant_bits_left
= fmt
->man_len
;
382 mant_off
= fmt
->man_start
;
383 while (mant_bits_left
> 0)
385 unsigned long mant_long
;
386 mant_bits
= mant_bits_left
< 32 ? mant_bits_left
: 32;
388 mant
*= 4294967296.0;
389 mant_long
= ((unsigned long) mant
) & 0xffffffffL
;
392 /* If the integer bit is implicit, then we need to discard it.
393 If we are discarding a zero, we should be (but are not) creating
394 a denormalized number which means adjusting the exponent
396 if (mant_bits_left
== fmt
->man_len
397 && fmt
->intbit
== floatformat_intbit_no
)
400 mant_long
&= 0xffffffffL
;
406 /* The bits we want are in the most significant MANT_BITS bits of
407 mant_long. Move them to the least significant. */
408 mant_long
>>= 32 - mant_bits
;
411 put_field (uto
, fmt
->byteorder
, fmt
->totalsize
,
412 mant_off
, mant_bits
, mant_long
);
413 mant_off
+= mant_bits
;
414 mant_bits_left
-= mant_bits
;
416 if (fmt
->byteorder
== floatformat_littlebyte_bigword
)
419 unsigned char *swaplow
= uto
;
420 unsigned char *swaphigh
= uto
+ 4;
423 for (count
= 0; count
< 4; count
++)
426 *swaplow
++ = *swaphigh
;
432 /* Check if VAL (which is assumed to be a floating point number whose
433 format is described by FMT) is negative. */
436 floatformat_is_negative (const struct floatformat
*fmt
, char *val
)
438 unsigned char *uval
= (unsigned char *) val
;
440 return get_field (uval
, fmt
->byteorder
, fmt
->totalsize
, fmt
->sign_start
, 1);
443 /* Check if VAL is "not a number" (NaN) for FMT. */
446 floatformat_is_nan (const struct floatformat
*fmt
, char *val
)
448 unsigned char *uval
= (unsigned char *) val
;
451 unsigned int mant_bits
, mant_off
;
457 exponent
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
458 fmt
->exp_start
, fmt
->exp_len
);
460 if (exponent
!= fmt
->exp_nan
)
463 mant_bits_left
= fmt
->man_len
;
464 mant_off
= fmt
->man_start
;
466 while (mant_bits_left
> 0)
468 mant_bits
= min (mant_bits_left
, 32);
470 mant
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
471 mant_off
, mant_bits
);
473 /* If there is an explicit integer bit, mask it off. */
474 if (mant_off
== fmt
->man_start
475 && fmt
->intbit
== floatformat_intbit_yes
)
476 mant
&= ~(1 << (mant_bits
- 1));
481 mant_off
+= mant_bits
;
482 mant_bits_left
-= mant_bits
;
488 /* Convert the mantissa of VAL (which is assumed to be a floating
489 point number whose format is described by FMT) into a hexadecimal
490 and store it in a static string. Return a pointer to that string. */
493 floatformat_mantissa (const struct floatformat
*fmt
, char *val
)
495 unsigned char *uval
= (unsigned char *) val
;
497 unsigned int mant_bits
, mant_off
;
502 /* Make sure we have enough room to store the mantissa. */
503 gdb_assert (sizeof res
> ((fmt
->man_len
+ 7) / 8) * 2);
505 mant_off
= fmt
->man_start
;
506 mant_bits_left
= fmt
->man_len
;
507 mant_bits
= (mant_bits_left
% 32) > 0 ? mant_bits_left
% 32 : 32;
509 mant
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
510 mant_off
, mant_bits
);
512 sprintf (res
, "%lx", mant
);
514 mant_off
+= mant_bits
;
515 mant_bits_left
-= mant_bits
;
517 while (mant_bits_left
> 0)
519 mant
= get_field (uval
, fmt
->byteorder
, fmt
->totalsize
,
522 sprintf (buf
, "%08lx", mant
);
526 mant_bits_left
-= 32;
534 /* Extract a floating-point number from a target-order byte-stream at ADDR.
535 Returns the value as type DOUBLEST.
537 If the host and target formats agree, we just copy the raw data into the
538 appropriate type of variable and return, letting the host increase precision
539 as necessary. Otherwise, we call the conversion routine and let it do the
543 extract_floating (void *addr
, int len
)
547 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
549 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
553 memcpy (&retval
, addr
, sizeof (retval
));
557 floatformat_to_doublest (TARGET_FLOAT_FORMAT
, addr
, &dretval
);
559 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
561 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
565 memcpy (&retval
, addr
, sizeof (retval
));
569 floatformat_to_doublest (TARGET_DOUBLE_FORMAT
, addr
, &dretval
);
571 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
573 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
577 memcpy (&retval
, addr
, sizeof (retval
));
581 floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT
, addr
, &dretval
);
585 error ("Can't deal with a floating point number of %d bytes.", len
);
592 store_floating (void *addr
, int len
, DOUBLEST val
)
594 if (len
* TARGET_CHAR_BIT
== TARGET_FLOAT_BIT
)
596 if (HOST_FLOAT_FORMAT
== TARGET_FLOAT_FORMAT
)
598 float floatval
= val
;
600 memcpy (addr
, &floatval
, sizeof (floatval
));
603 floatformat_from_doublest (TARGET_FLOAT_FORMAT
, &val
, addr
);
605 else if (len
* TARGET_CHAR_BIT
== TARGET_DOUBLE_BIT
)
607 if (HOST_DOUBLE_FORMAT
== TARGET_DOUBLE_FORMAT
)
609 double doubleval
= val
;
611 memcpy (addr
, &doubleval
, sizeof (doubleval
));
614 floatformat_from_doublest (TARGET_DOUBLE_FORMAT
, &val
, addr
);
616 else if (len
* TARGET_CHAR_BIT
== TARGET_LONG_DOUBLE_BIT
)
618 if (HOST_LONG_DOUBLE_FORMAT
== TARGET_LONG_DOUBLE_FORMAT
)
619 memcpy (addr
, &val
, sizeof (val
));
621 floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT
, &val
, addr
);
625 error ("Can't deal with a floating point number of %d bytes.", len
);