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