Switch the license of all .exp files to GPLv3.
[deliverable/binutils-gdb.git] / gdb / doublest.c
CommitLineData
d16aafd8 1/* Floating point routines for GDB, the GNU debugger.
f1908289 2
6aba47ca
DJ
3 Copyright (C) 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2007
0a3e99f6 5 Free Software Foundation, Inc.
d16aafd8
AC
6
7 This file is part of GDB.
8
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
d16aafd8
AC
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d16aafd8
AC
21
22/* Support for converting target fp numbers into host DOUBLEST format. */
23
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. */
27
28#include "defs.h"
29#include "doublest.h"
30#include "floatformat.h"
31#include "gdb_assert.h"
32#include "gdb_string.h"
96d2f608 33#include "gdbtypes.h"
d16aafd8
AC
34#include <math.h> /* ldexp */
35
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
40
fcab3fb5
RE
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
44
d16aafd8
AC
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. */
47static unsigned long
108d6ead 48get_field (const bfd_byte *data, enum floatformat_byteorders order,
d16aafd8
AC
49 unsigned int total_len, unsigned int start, unsigned int len)
50{
51 unsigned long result;
52 unsigned int cur_byte;
53 int cur_bitshift;
54
fcab3fb5
RE
55 /* Caller must byte-swap words before calling this routine. */
56 gdb_assert (order == floatformat_little || order == floatformat_big);
57
d16aafd8 58 /* Start at the least significant part of the field. */
fcab3fb5 59 if (order == floatformat_little)
d16aafd8
AC
60 {
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);
69 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
70 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
71 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
72 - FLOATFORMAT_CHAR_BIT;
73 }
74 else
75 {
76 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
77 cur_bitshift =
78 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
79 }
80 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
81 result = *(data + cur_byte) >> (-cur_bitshift);
82 else
83 result = 0;
84 cur_bitshift += FLOATFORMAT_CHAR_BIT;
fcab3fb5 85 if (order == floatformat_little)
d16aafd8
AC
86 ++cur_byte;
87 else
88 --cur_byte;
89
90 /* Move towards the most significant part of the field. */
91 while (cur_bitshift < len)
92 {
93 result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
94 cur_bitshift += FLOATFORMAT_CHAR_BIT;
c35f4ffc
AC
95 switch (order)
96 {
97 case floatformat_little:
98 ++cur_byte;
99 break;
100 case floatformat_big:
101 --cur_byte;
102 break;
c35f4ffc 103 }
d16aafd8
AC
104 }
105 if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
106 /* Mask out bits which are not part of the field */
107 result &= ((1UL << len) - 1);
108 return result;
109}
110
0a3e99f6
MK
111/* Normalize the byte order of FROM into TO. If no normalization is
112 needed then FMT->byteorder is returned and TO is not changed;
113 otherwise the format of the normalized form in TO is returned. */
114
fcab3fb5
RE
115static enum floatformat_byteorders
116floatformat_normalize_byteorder (const struct floatformat *fmt,
117 const void *from, void *to)
118{
119 const unsigned char *swapin;
120 unsigned char *swapout;
121 int words;
122
123 if (fmt->byteorder == floatformat_little
124 || fmt->byteorder == floatformat_big)
125 return fmt->byteorder;
126
fcab3fb5
RE
127 words = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
128 words >>= 2;
129
130 swapout = (unsigned char *)to;
131 swapin = (const unsigned char *)from;
132
0a3e99f6
MK
133 if (fmt->byteorder == floatformat_vax)
134 {
135 while (words-- > 0)
136 {
137 *swapout++ = swapin[1];
138 *swapout++ = swapin[0];
139 *swapout++ = swapin[3];
140 *swapout++ = swapin[2];
141 swapin += 4;
142 }
143 /* This may look weird, since VAX is little-endian, but it is
144 easier to translate to big-endian than to little-endian. */
145 return floatformat_big;
146 }
147 else
fcab3fb5 148 {
0a3e99f6
MK
149 gdb_assert (fmt->byteorder == floatformat_littlebyte_bigword);
150
151 while (words-- > 0)
152 {
153 *swapout++ = swapin[3];
154 *swapout++ = swapin[2];
155 *swapout++ = swapin[1];
156 *swapout++ = swapin[0];
157 swapin += 4;
158 }
159 return floatformat_big;
fcab3fb5 160 }
fcab3fb5
RE
161}
162
d16aafd8
AC
163/* Convert from FMT to a DOUBLEST.
164 FROM is the address of the extended float.
165 Store the DOUBLEST in *TO. */
166
c422e771
AC
167static void
168convert_floatformat_to_doublest (const struct floatformat *fmt,
169 const void *from,
170 DOUBLEST *to)
d16aafd8
AC
171{
172 unsigned char *ufrom = (unsigned char *) from;
173 DOUBLEST dto;
174 long exponent;
175 unsigned long mant;
176 unsigned int mant_bits, mant_off;
177 int mant_bits_left;
178 int special_exponent; /* It's a NaN, denorm or zero */
fcab3fb5
RE
179 enum floatformat_byteorders order;
180 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
20389057 181 enum float_kind kind;
fcab3fb5
RE
182
183 gdb_assert (fmt->totalsize
184 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
d16aafd8 185
20389057
DJ
186 /* For non-numbers, reuse libiberty's logic to find the correct
187 format. We do not lose any precision in this case by passing
188 through a double. */
189 kind = floatformat_classify (fmt, from);
190 if (kind == float_infinite || kind == float_nan)
191 {
192 double dto;
193 floatformat_to_double (fmt, from, &dto);
194 *to = (DOUBLEST) dto;
195 return;
196 }
197
fcab3fb5 198 order = floatformat_normalize_byteorder (fmt, ufrom, newfrom);
d16aafd8 199
fcab3fb5
RE
200 if (order != fmt->byteorder)
201 ufrom = newfrom;
d16aafd8 202
fcab3fb5
RE
203 exponent = get_field (ufrom, order, fmt->totalsize, fmt->exp_start,
204 fmt->exp_len);
d16aafd8
AC
205 /* Note that if exponent indicates a NaN, we can't really do anything useful
206 (not knowing if the host has NaN's, or how to build one). So it will
207 end up as an infinity or something close; that is OK. */
208
209 mant_bits_left = fmt->man_len;
210 mant_off = fmt->man_start;
211 dto = 0.0;
212
213 special_exponent = exponent == 0 || exponent == fmt->exp_nan;
214
38c52d5a
DJ
215 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
216 we don't check for zero as the exponent doesn't matter. Note the cast
217 to int; exp_bias is unsigned, so it's important to make sure the
218 operation is done in signed arithmetic. */
d16aafd8
AC
219 if (!special_exponent)
220 exponent -= fmt->exp_bias;
221 else if (exponent == 0)
1c704f11 222 exponent = 1 - fmt->exp_bias;
d16aafd8
AC
223
224 /* Build the result algebraically. Might go infinite, underflow, etc;
225 who cares. */
226
227/* If this format uses a hidden bit, explicitly add it in now. Otherwise,
228 increment the exponent by one to account for the integer bit. */
229
230 if (!special_exponent)
231 {
232 if (fmt->intbit == floatformat_intbit_no)
233 dto = ldexp (1.0, exponent);
234 else
235 exponent++;
236 }
237
238 while (mant_bits_left > 0)
239 {
240 mant_bits = min (mant_bits_left, 32);
241
fcab3fb5 242 mant = get_field (ufrom, order, fmt->totalsize, mant_off, mant_bits);
d16aafd8
AC
243
244 dto += ldexp ((double) mant, exponent - mant_bits);
245 exponent -= mant_bits;
246 mant_off += mant_bits;
247 mant_bits_left -= mant_bits;
248 }
249
250 /* Negate it if negative. */
fcab3fb5 251 if (get_field (ufrom, order, fmt->totalsize, fmt->sign_start, 1))
d16aafd8
AC
252 dto = -dto;
253 *to = dto;
254}
255\f
256static void put_field (unsigned char *, enum floatformat_byteorders,
257 unsigned int,
258 unsigned int, unsigned int, unsigned long);
259
260/* Set a field which starts at START and is LEN bytes long. DATA and
261 TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
262static void
263put_field (unsigned char *data, enum floatformat_byteorders order,
264 unsigned int total_len, unsigned int start, unsigned int len,
265 unsigned long stuff_to_put)
266{
267 unsigned int cur_byte;
268 int cur_bitshift;
269
fcab3fb5
RE
270 /* Caller must byte-swap words before calling this routine. */
271 gdb_assert (order == floatformat_little || order == floatformat_big);
272
d16aafd8 273 /* Start at the least significant part of the field. */
fcab3fb5 274 if (order == floatformat_little)
d16aafd8
AC
275 {
276 int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
277 cur_byte = (total_len / FLOATFORMAT_CHAR_BIT)
278 - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
279 cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT)
280 - FLOATFORMAT_CHAR_BIT;
281 }
282 else
283 {
284 cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
285 cur_bitshift =
286 ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
287 }
288 if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
289 {
290 *(data + cur_byte) &=
291 ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
292 << (-cur_bitshift));
293 *(data + cur_byte) |=
294 (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
295 }
296 cur_bitshift += FLOATFORMAT_CHAR_BIT;
fcab3fb5 297 if (order == floatformat_little)
d16aafd8
AC
298 ++cur_byte;
299 else
300 --cur_byte;
301
302 /* Move towards the most significant part of the field. */
303 while (cur_bitshift < len)
304 {
305 if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
306 {
307 /* This is the last byte. */
308 *(data + cur_byte) &=
309 ~((1 << (len - cur_bitshift)) - 1);
310 *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
311 }
312 else
313 *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
314 & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
315 cur_bitshift += FLOATFORMAT_CHAR_BIT;
fcab3fb5 316 if (order == floatformat_little)
d16aafd8
AC
317 ++cur_byte;
318 else
319 --cur_byte;
320 }
321}
322
323#ifdef HAVE_LONG_DOUBLE
324/* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
325 The range of the returned value is >= 0.5 and < 1.0. This is equivalent to
326 frexp, but operates on the long double data type. */
327
328static long double ldfrexp (long double value, int *eptr);
329
330static long double
331ldfrexp (long double value, int *eptr)
332{
333 long double tmp;
334 int exp;
335
336 /* Unfortunately, there are no portable functions for extracting the exponent
337 of a long double, so we have to do it iteratively by multiplying or dividing
338 by two until the fraction is between 0.5 and 1.0. */
339
340 if (value < 0.0l)
341 value = -value;
342
343 tmp = 1.0l;
344 exp = 0;
345
346 if (value >= tmp) /* Value >= 1.0 */
347 while (value >= tmp)
348 {
349 tmp *= 2.0l;
350 exp++;
351 }
352 else if (value != 0.0l) /* Value < 1.0 and > 0.0 */
353 {
354 while (value < tmp)
355 {
356 tmp /= 2.0l;
357 exp--;
358 }
359 tmp *= 2.0l;
360 exp++;
361 }
362
363 *eptr = exp;
364 return value / tmp;
365}
366#endif /* HAVE_LONG_DOUBLE */
367
368
0a3e99f6
MK
369/* The converse: convert the DOUBLEST *FROM to an extended float and
370 store where TO points. Neither FROM nor TO have any alignment
d16aafd8
AC
371 restrictions. */
372
c422e771
AC
373static void
374convert_doublest_to_floatformat (CONST struct floatformat *fmt,
0a3e99f6 375 const DOUBLEST *from, void *to)
d16aafd8
AC
376{
377 DOUBLEST dfrom;
378 int exponent;
379 DOUBLEST mant;
380 unsigned int mant_bits, mant_off;
381 int mant_bits_left;
382 unsigned char *uto = (unsigned char *) to;
fcab3fb5 383 enum floatformat_byteorders order = fmt->byteorder;
0a3e99f6 384 unsigned char newto[FLOATFORMAT_LARGEST_BYTES];
fcab3fb5 385
0a3e99f6 386 if (order != floatformat_little)
fcab3fb5 387 order = floatformat_big;
d16aafd8 388
0a3e99f6
MK
389 if (order != fmt->byteorder)
390 uto = newto;
391
d16aafd8
AC
392 memcpy (&dfrom, from, sizeof (dfrom));
393 memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1)
394 / FLOATFORMAT_CHAR_BIT);
395 if (dfrom == 0)
396 return; /* Result is zero */
397 if (dfrom != dfrom) /* Result is NaN */
398 {
399 /* From is NaN */
fcab3fb5 400 put_field (uto, order, fmt->totalsize, fmt->exp_start,
d16aafd8
AC
401 fmt->exp_len, fmt->exp_nan);
402 /* Be sure it's not infinity, but NaN value is irrel */
fcab3fb5 403 put_field (uto, order, fmt->totalsize, fmt->man_start,
d16aafd8 404 32, 1);
fcab3fb5 405 goto finalize_byteorder;
d16aafd8
AC
406 }
407
408 /* If negative, set the sign bit. */
409 if (dfrom < 0)
410 {
fcab3fb5 411 put_field (uto, order, fmt->totalsize, fmt->sign_start, 1, 1);
d16aafd8
AC
412 dfrom = -dfrom;
413 }
414
415 if (dfrom + dfrom == dfrom && dfrom != 0.0) /* Result is Infinity */
416 {
417 /* Infinity exponent is same as NaN's. */
fcab3fb5 418 put_field (uto, order, fmt->totalsize, fmt->exp_start,
d16aafd8
AC
419 fmt->exp_len, fmt->exp_nan);
420 /* Infinity mantissa is all zeroes. */
fcab3fb5 421 put_field (uto, order, fmt->totalsize, fmt->man_start,
d16aafd8 422 fmt->man_len, 0);
fcab3fb5 423 goto finalize_byteorder;
d16aafd8
AC
424 }
425
426#ifdef HAVE_LONG_DOUBLE
427 mant = ldfrexp (dfrom, &exponent);
428#else
429 mant = frexp (dfrom, &exponent);
430#endif
431
fcab3fb5 432 put_field (uto, order, fmt->totalsize, fmt->exp_start, fmt->exp_len,
d16aafd8
AC
433 exponent + fmt->exp_bias - 1);
434
435 mant_bits_left = fmt->man_len;
436 mant_off = fmt->man_start;
437 while (mant_bits_left > 0)
438 {
439 unsigned long mant_long;
440 mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
441
442 mant *= 4294967296.0;
443 mant_long = ((unsigned long) mant) & 0xffffffffL;
444 mant -= mant_long;
445
446 /* If the integer bit is implicit, then we need to discard it.
447 If we are discarding a zero, we should be (but are not) creating
448 a denormalized number which means adjusting the exponent
449 (I think). */
450 if (mant_bits_left == fmt->man_len
451 && fmt->intbit == floatformat_intbit_no)
452 {
453 mant_long <<= 1;
454 mant_long &= 0xffffffffL;
06194148
JJ
455 /* If we are processing the top 32 mantissa bits of a doublest
456 so as to convert to a float value with implied integer bit,
457 we will only be putting 31 of those 32 bits into the
458 final value due to the discarding of the top bit. In the
459 case of a small float value where the number of mantissa
460 bits is less than 32, discarding the top bit does not alter
461 the number of bits we will be adding to the result. */
462 if (mant_bits == 32)
463 mant_bits -= 1;
d16aafd8
AC
464 }
465
466 if (mant_bits < 32)
467 {
468 /* The bits we want are in the most significant MANT_BITS bits of
469 mant_long. Move them to the least significant. */
470 mant_long >>= 32 - mant_bits;
471 }
472
fcab3fb5 473 put_field (uto, order, fmt->totalsize,
d16aafd8
AC
474 mant_off, mant_bits, mant_long);
475 mant_off += mant_bits;
476 mant_bits_left -= mant_bits;
477 }
fcab3fb5
RE
478
479 finalize_byteorder:
480 /* Do we need to byte-swap the words in the result? */
481 if (order != fmt->byteorder)
0a3e99f6 482 floatformat_normalize_byteorder (fmt, newto, to);
d16aafd8
AC
483}
484
485/* Check if VAL (which is assumed to be a floating point number whose
486 format is described by FMT) is negative. */
487
488int
108d6ead
AC
489floatformat_is_negative (const struct floatformat *fmt,
490 const bfd_byte *uval)
d16aafd8 491{
fcab3fb5
RE
492 enum floatformat_byteorders order;
493 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
494
069e84fd 495 gdb_assert (fmt != NULL);
fcab3fb5
RE
496 gdb_assert (fmt->totalsize
497 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
498
499 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
500
501 if (order != fmt->byteorder)
502 uval = newfrom;
503
504 return get_field (uval, order, fmt->totalsize, fmt->sign_start, 1);
d16aafd8
AC
505}
506
507/* Check if VAL is "not a number" (NaN) for FMT. */
508
20389057
DJ
509enum float_kind
510floatformat_classify (const struct floatformat *fmt,
511 const bfd_byte *uval)
d16aafd8 512{
d16aafd8
AC
513 long exponent;
514 unsigned long mant;
515 unsigned int mant_bits, mant_off;
516 int mant_bits_left;
fcab3fb5
RE
517 enum floatformat_byteorders order;
518 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
20389057 519 int mant_zero;
fcab3fb5 520
069e84fd 521 gdb_assert (fmt != NULL);
fcab3fb5
RE
522 gdb_assert (fmt->totalsize
523 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
524
525 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
526
527 if (order != fmt->byteorder)
528 uval = newfrom;
069e84fd 529
fcab3fb5
RE
530 exponent = get_field (uval, order, fmt->totalsize, fmt->exp_start,
531 fmt->exp_len);
d16aafd8 532
d16aafd8
AC
533 mant_bits_left = fmt->man_len;
534 mant_off = fmt->man_start;
535
20389057 536 mant_zero = 1;
d16aafd8
AC
537 while (mant_bits_left > 0)
538 {
539 mant_bits = min (mant_bits_left, 32);
540
fcab3fb5 541 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
d16aafd8
AC
542
543 /* If there is an explicit integer bit, mask it off. */
544 if (mant_off == fmt->man_start
545 && fmt->intbit == floatformat_intbit_yes)
546 mant &= ~(1 << (mant_bits - 1));
547
548 if (mant)
20389057
DJ
549 {
550 mant_zero = 0;
551 break;
552 }
d16aafd8
AC
553
554 mant_off += mant_bits;
555 mant_bits_left -= mant_bits;
556 }
557
20389057
DJ
558 /* If exp_nan is not set, assume that inf, NaN, and subnormals are not
559 supported. */
560 if (! fmt->exp_nan)
561 {
562 if (mant_zero)
563 return float_zero;
564 else
565 return float_normal;
566 }
567
568 if (exponent == 0 && !mant_zero)
569 return float_subnormal;
570
571 if (exponent == fmt->exp_nan)
572 {
573 if (mant_zero)
574 return float_infinite;
575 else
576 return float_nan;
577 }
578
579 if (mant_zero)
580 return float_zero;
581
582 return float_normal;
d16aafd8
AC
583}
584
585/* Convert the mantissa of VAL (which is assumed to be a floating
586 point number whose format is described by FMT) into a hexadecimal
587 and store it in a static string. Return a pointer to that string. */
588
108d6ead
AC
589const char *
590floatformat_mantissa (const struct floatformat *fmt,
591 const bfd_byte *val)
d16aafd8
AC
592{
593 unsigned char *uval = (unsigned char *) val;
594 unsigned long mant;
595 unsigned int mant_bits, mant_off;
596 int mant_bits_left;
597 static char res[50];
598 char buf[9];
27df76f3 599 int len;
fcab3fb5
RE
600 enum floatformat_byteorders order;
601 unsigned char newfrom[FLOATFORMAT_LARGEST_BYTES];
602
603 gdb_assert (fmt != NULL);
604 gdb_assert (fmt->totalsize
605 <= FLOATFORMAT_LARGEST_BYTES * FLOATFORMAT_CHAR_BIT);
606
607 order = floatformat_normalize_byteorder (fmt, uval, newfrom);
608
609 if (order != fmt->byteorder)
610 uval = newfrom;
611
612 if (! fmt->exp_nan)
613 return 0;
d16aafd8
AC
614
615 /* Make sure we have enough room to store the mantissa. */
616 gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2);
617
618 mant_off = fmt->man_start;
619 mant_bits_left = fmt->man_len;
620 mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32;
621
fcab3fb5 622 mant = get_field (uval, order, fmt->totalsize, mant_off, mant_bits);
d16aafd8 623
27df76f3 624 len = xsnprintf (res, sizeof res, "%lx", mant);
d16aafd8
AC
625
626 mant_off += mant_bits;
627 mant_bits_left -= mant_bits;
27df76f3 628
d16aafd8
AC
629 while (mant_bits_left > 0)
630 {
fcab3fb5 631 mant = get_field (uval, order, fmt->totalsize, mant_off, 32);
d16aafd8 632
27df76f3
MK
633 xsnprintf (buf, sizeof buf, "%08lx", mant);
634 gdb_assert (len + strlen (buf) <= sizeof res);
d16aafd8
AC
635 strcat (res, buf);
636
637 mant_off += 32;
638 mant_bits_left -= 32;
639 }
640
641 return res;
642}
643
d16aafd8 644\f
c422e771
AC
645/* Convert TO/FROM target to the hosts DOUBLEST floating-point format.
646
647 If the host and target formats agree, we just copy the raw data
648 into the appropriate type of variable and return, letting the host
649 increase precision as necessary. Otherwise, we call the conversion
650 routine and let it do the dirty work. */
651
c35f4ffc
AC
652static const struct floatformat *host_float_format = GDB_HOST_FLOAT_FORMAT;
653static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT;
654static const struct floatformat *host_long_double_format = GDB_HOST_LONG_DOUBLE_FORMAT;
c422e771
AC
655
656void
657floatformat_to_doublest (const struct floatformat *fmt,
658 const void *in, DOUBLEST *out)
659{
660 gdb_assert (fmt != NULL);
661 if (fmt == host_float_format)
662 {
663 float val;
664 memcpy (&val, in, sizeof (val));
665 *out = val;
666 }
667 else if (fmt == host_double_format)
668 {
669 double val;
670 memcpy (&val, in, sizeof (val));
671 *out = val;
672 }
673 else if (fmt == host_long_double_format)
674 {
675 long double val;
676 memcpy (&val, in, sizeof (val));
677 *out = val;
678 }
679 else
680 convert_floatformat_to_doublest (fmt, in, out);
681}
682
683void
684floatformat_from_doublest (const struct floatformat *fmt,
685 const DOUBLEST *in, void *out)
686{
687 gdb_assert (fmt != NULL);
688 if (fmt == host_float_format)
689 {
690 float val = *in;
691 memcpy (out, &val, sizeof (val));
692 }
693 else if (fmt == host_double_format)
694 {
695 double val = *in;
696 memcpy (out, &val, sizeof (val));
697 }
698 else if (fmt == host_long_double_format)
699 {
700 long double val = *in;
701 memcpy (out, &val, sizeof (val));
702 }
703 else
704 convert_doublest_to_floatformat (fmt, in, out);
705}
d16aafd8 706
c422e771 707\f
87ffba60 708/* Return a floating-point format for a floating-point variable of
47b3f456
AC
709 length LEN. If no suitable floating-point format is found, an
710 error is thrown.
d16aafd8 711
87ffba60
MK
712 We need this functionality since information about the
713 floating-point format of a type is not always available to GDB; the
714 debug information typically only tells us the size of a
715 floating-point type.
716
717 FIXME: kettenis/2001-10-28: In many places, particularly in
718 target-dependent code, the format of floating-point types is known,
719 but not passed on by GDB. This should be fixed. */
720
b9362cc7 721static const struct floatformat *
87ffba60 722floatformat_from_length (int len)
d16aafd8 723{
47b3f456 724 const struct floatformat *format;
ea06eb3d
UW
725 if (len * TARGET_CHAR_BIT == gdbarch_float_bit (current_gdbarch))
726 format = gdbarch_float_format (current_gdbarch)
727 [gdbarch_byte_order (current_gdbarch)];
728 else if (len * TARGET_CHAR_BIT == gdbarch_double_bit (current_gdbarch))
729 format = gdbarch_double_format (current_gdbarch)
730 [gdbarch_byte_order (current_gdbarch)];
731 else if (len * TARGET_CHAR_BIT == gdbarch_long_double_bit (current_gdbarch))
732 format = gdbarch_long_double_format (current_gdbarch)
733 [gdbarch_byte_order (current_gdbarch)];
ddbfdd06
PM
734 /* On i386 the 'long double' type takes 96 bits,
735 while the real number of used bits is only 80,
736 both in processor and in memory.
737 The code below accepts the real bit size. */
ea06eb3d 738 else if ((gdbarch_long_double_format (current_gdbarch) != NULL)
ddbfdd06 739 && (len * TARGET_CHAR_BIT ==
ea06eb3d
UW
740 gdbarch_long_double_format (current_gdbarch)[0]->totalsize))
741 format = gdbarch_long_double_format (current_gdbarch)
742 [gdbarch_byte_order (current_gdbarch)];
47b3f456
AC
743 else
744 format = NULL;
745 if (format == NULL)
8a3fe4f8 746 error (_("Unrecognized %d-bit floating-point type."),
9b0dea39 747 len * TARGET_CHAR_BIT);
47b3f456 748 return format;
87ffba60
MK
749}
750
c2f05ac9
AC
751const struct floatformat *
752floatformat_from_type (const struct type *type)
753{
754 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
755 if (TYPE_FLOATFORMAT (type) != NULL)
0d20ae72 756 return TYPE_FLOATFORMAT (type)[gdbarch_byte_order (current_gdbarch)];
c2f05ac9
AC
757 else
758 return floatformat_from_length (TYPE_LENGTH (type));
759}
760
87ffba60
MK
761/* If the host doesn't define NAN, use zero instead. */
762#ifndef NAN
763#define NAN 0.0
764#endif
765
766/* Extract a floating-point number of length LEN from a target-order
767 byte-stream at ADDR. Returns the value as type DOUBLEST. */
768
f1908289
AC
769static DOUBLEST
770extract_floating_by_length (const void *addr, int len)
87ffba60
MK
771{
772 const struct floatformat *fmt = floatformat_from_length (len);
773 DOUBLEST val;
774
87ffba60
MK
775 floatformat_to_doublest (fmt, addr, &val);
776 return val;
d16aafd8
AC
777}
778
f1908289
AC
779DOUBLEST
780deprecated_extract_floating (const void *addr, int len)
781{
782 return extract_floating_by_length (addr, len);
783}
784
87ffba60
MK
785/* Store VAL as a floating-point number of length LEN to a
786 target-order byte-stream at ADDR. */
787
f1908289
AC
788static void
789store_floating_by_length (void *addr, int len, DOUBLEST val)
d16aafd8 790{
87ffba60
MK
791 const struct floatformat *fmt = floatformat_from_length (len);
792
87ffba60 793 floatformat_from_doublest (fmt, &val, addr);
d16aafd8 794}
96d2f608 795
f1908289
AC
796void
797deprecated_store_floating (void *addr, int len, DOUBLEST val)
798{
799 store_floating_by_length (addr, len, val);
800}
801
87ffba60
MK
802/* Extract a floating-point number of type TYPE from a target-order
803 byte-stream at ADDR. Returns the value as type DOUBLEST. */
96d2f608
AC
804
805DOUBLEST
806extract_typed_floating (const void *addr, const struct type *type)
807{
808 DOUBLEST retval;
87ffba60 809
96d2f608 810 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
87ffba60 811
96d2f608 812 if (TYPE_FLOATFORMAT (type) == NULL)
f1908289
AC
813 /* Not all code remembers to set the FLOATFORMAT (language
814 specific code? stabs?) so handle that here as a special case. */
815 return extract_floating_by_length (addr, TYPE_LENGTH (type));
87ffba60 816
0d20ae72
UW
817 floatformat_to_doublest
818 (TYPE_FLOATFORMAT (type)[gdbarch_byte_order (current_gdbarch)],
8da61cc4 819 addr, &retval);
96d2f608
AC
820 return retval;
821}
822
87ffba60
MK
823/* Store VAL as a floating-point number of type TYPE to a target-order
824 byte-stream at ADDR. */
825
96d2f608
AC
826void
827store_typed_floating (void *addr, const struct type *type, DOUBLEST val)
828{
829 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
87ffba60
MK
830
831 /* FIXME: kettenis/2001-10-28: It is debatable whether we should
832 zero out any remaining bytes in the target buffer when TYPE is
833 longer than the actual underlying floating-point format. Perhaps
834 we should store a fixed bitpattern in those remaining bytes,
835 instead of zero, or perhaps we shouldn't touch those remaining
836 bytes at all.
837
838 NOTE: cagney/2001-10-28: With the way things currently work, it
839 isn't a good idea to leave the end bits undefined. This is
840 because GDB writes out the entire sizeof(<floating>) bits of the
841 floating-point type even though the value might only be stored
842 in, and the target processor may only refer to, the first N <
843 TYPE_LENGTH (type) bits. If the end of the buffer wasn't
844 initialized, GDB would write undefined data to the target. An
845 errant program, refering to that undefined data, would then
43686d64
MK
846 become non-deterministic.
847
848 See also the function convert_typed_floating below. */
96d2f608 849 memset (addr, 0, TYPE_LENGTH (type));
87ffba60 850
96d2f608 851 if (TYPE_FLOATFORMAT (type) == NULL)
f1908289
AC
852 /* Not all code remembers to set the FLOATFORMAT (language
853 specific code? stabs?) so handle that here as a special case. */
854 store_floating_by_length (addr, TYPE_LENGTH (type), val);
0b87a11d 855 else
0d20ae72
UW
856 floatformat_from_doublest
857 (TYPE_FLOATFORMAT (type)[gdbarch_byte_order (current_gdbarch)],
858 &val, addr);
96d2f608 859}
43686d64
MK
860
861/* Convert a floating-point number of type FROM_TYPE from a
862 target-order byte-stream at FROM to a floating-point number of type
863 TO_TYPE, and store it to a target-order byte-stream at TO. */
864
865void
866convert_typed_floating (const void *from, const struct type *from_type,
867 void *to, const struct type *to_type)
868{
c2f05ac9
AC
869 const struct floatformat *from_fmt = floatformat_from_type (from_type);
870 const struct floatformat *to_fmt = floatformat_from_type (to_type);
43686d64
MK
871
872 gdb_assert (TYPE_CODE (from_type) == TYPE_CODE_FLT);
873 gdb_assert (TYPE_CODE (to_type) == TYPE_CODE_FLT);
874
43686d64
MK
875 if (from_fmt == NULL || to_fmt == NULL)
876 {
877 /* If we don't know the floating-point format of FROM_TYPE or
878 TO_TYPE, there's not much we can do. We might make the
879 assumption that if the length of FROM_TYPE and TO_TYPE match,
880 their floating-point format would match too, but that
881 assumption might be wrong on targets that support
882 floating-point types that only differ in endianness for
883 example. So we warn instead, and zero out the target buffer. */
8a3fe4f8 884 warning (_("Can't convert floating-point number to desired type."));
43686d64
MK
885 memset (to, 0, TYPE_LENGTH (to_type));
886 }
887 else if (from_fmt == to_fmt)
888 {
889 /* We're in business. The floating-point format of FROM_TYPE
890 and TO_TYPE match. However, even though the floating-point
891 format matches, the length of the type might still be
892 different. Make sure we don't overrun any buffers. See
893 comment in store_typed_floating for a discussion about
894 zeroing out remaining bytes in the target buffer. */
895 memset (to, 0, TYPE_LENGTH (to_type));
896 memcpy (to, from, min (TYPE_LENGTH (from_type), TYPE_LENGTH (to_type)));
897 }
898 else
899 {
900 /* The floating-point types don't match. The best we can do
901 (aport from simulating the target FPU) is converting to the
902 widest floating-point type supported by the host, and then
903 again to the desired type. */
904 DOUBLEST d;
905
906 floatformat_to_doublest (from_fmt, from, &d);
907 floatformat_from_doublest (to_fmt, &d, to);
908 }
909}
5ef2d0aa
AC
910
911const struct floatformat *floatformat_ieee_single[BFD_ENDIAN_UNKNOWN];
912const struct floatformat *floatformat_ieee_double[BFD_ENDIAN_UNKNOWN];
49c54768 913const struct floatformat *floatformat_ieee_quad[BFD_ENDIAN_UNKNOWN];
5ef2d0aa
AC
914const struct floatformat *floatformat_arm_ext[BFD_ENDIAN_UNKNOWN];
915const struct floatformat *floatformat_ia64_spill[BFD_ENDIAN_UNKNOWN];
5ef2d0aa
AC
916
917extern void _initialize_doublest (void);
918
919extern void
920_initialize_doublest (void)
921{
922 floatformat_ieee_single[BFD_ENDIAN_LITTLE] = &floatformat_ieee_single_little;
923 floatformat_ieee_single[BFD_ENDIAN_BIG] = &floatformat_ieee_single_big;
924 floatformat_ieee_double[BFD_ENDIAN_LITTLE] = &floatformat_ieee_double_little;
925 floatformat_ieee_double[BFD_ENDIAN_BIG] = &floatformat_ieee_double_big;
926 floatformat_arm_ext[BFD_ENDIAN_LITTLE] = &floatformat_arm_ext_littlebyte_bigword;
927 floatformat_arm_ext[BFD_ENDIAN_BIG] = &floatformat_arm_ext_big;
928 floatformat_ia64_spill[BFD_ENDIAN_LITTLE] = &floatformat_ia64_spill_little;
929 floatformat_ia64_spill[BFD_ENDIAN_BIG] = &floatformat_ia64_spill_big;
49c54768
AC
930 floatformat_ieee_quad[BFD_ENDIAN_LITTLE] = &floatformat_ia64_quad_little;
931 floatformat_ieee_quad[BFD_ENDIAN_BIG] = &floatformat_ia64_quad_big;
5ef2d0aa 932}
This page took 0.558746 seconds and 4 git commands to generate.