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