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