* defs.h (HOST_FLOAT_FORMAT, HOST_DOUBLE_FORMAT)
[platform/upstream/binutils.git] / gdb / doublest.c
1 /* Floating point routines for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
3    1997, 1998, 1999, 2000, 2001
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 /* Support for converting target fp numbers into host DOUBLEST format.  */
24
25 /* XXX - This code should really be in libiberty/floatformat.c,
26    however configuration issues with libiberty made this very
27    difficult to do in the available time.  */
28
29 #include "defs.h"
30 #include "doublest.h"
31 #include "floatformat.h"
32 #include "gdb_assert.h"
33 #include "gdb_string.h"
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
41 static unsigned long get_field (unsigned char *,
42                                 enum floatformat_byteorders,
43                                 unsigned int, unsigned int, unsigned int);
44
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.  */
47 static unsigned long
48 get_field (unsigned char *data, enum floatformat_byteorders order,
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
55   /* Start at the least significant part of the field.  */
56   if (order == floatformat_little || order == floatformat_littlebyte_bigword)
57     {
58       /* We start counting from the other end (i.e, from the high bytes
59          rather than the low bytes).  As such, we need to be concerned
60          with what happens if bit 0 doesn't start on a byte boundary. 
61          I.e, we need to properly handle the case where total_len is
62          not evenly divisible by 8.  So we compute ``excess'' which
63          represents the number of bits from the end of our starting
64          byte needed to get to bit 0. */
65       int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
66       cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) 
67                  - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
68       cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) 
69                      - FLOATFORMAT_CHAR_BIT;
70     }
71   else
72     {
73       cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
74       cur_bitshift =
75         ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
76     }
77   if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
78     result = *(data + cur_byte) >> (-cur_bitshift);
79   else
80     result = 0;
81   cur_bitshift += FLOATFORMAT_CHAR_BIT;
82   if (order == floatformat_little || order == floatformat_littlebyte_bigword)
83     ++cur_byte;
84   else
85     --cur_byte;
86
87   /* Move towards the most significant part of the field.  */
88   while (cur_bitshift < len)
89     {
90       result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
91       cur_bitshift += FLOATFORMAT_CHAR_BIT;
92       if (order == floatformat_little || order == floatformat_littlebyte_bigword)
93         ++cur_byte;
94       else
95         --cur_byte;
96     }
97   if (len < sizeof(result) * FLOATFORMAT_CHAR_BIT)
98     /* Mask out bits which are not part of the field */
99     result &= ((1UL << len) - 1);
100   return result;
101 }
102
103 /* Convert from FMT to a DOUBLEST.
104    FROM is the address of the extended float.
105    Store the DOUBLEST in *TO.  */
106
107 void
108 floatformat_to_doublest (const struct floatformat *fmt, char *from,
109                          DOUBLEST *to)
110 {
111   unsigned char *ufrom = (unsigned char *) from;
112   DOUBLEST dto;
113   long exponent;
114   unsigned long mant;
115   unsigned int mant_bits, mant_off;
116   int mant_bits_left;
117   int special_exponent;         /* It's a NaN, denorm or zero */
118
119   /* If the mantissa bits are not contiguous from one end of the
120      mantissa to the other, we need to make a private copy of the
121      source bytes that is in the right order since the unpacking
122      algorithm assumes that the bits are contiguous.
123
124      Swap the bytes individually rather than accessing them through
125      "long *" since we have no guarantee that they start on a long
126      alignment, and also sizeof(long) for the host could be different
127      than sizeof(long) for the target.  FIXME: Assumes sizeof(long)
128      for the target is 4. */
129
130   if (fmt->byteorder == floatformat_littlebyte_bigword)
131     {
132       static unsigned char *newfrom;
133       unsigned char *swapin, *swapout;
134       int longswaps;
135
136       longswaps = fmt->totalsize / FLOATFORMAT_CHAR_BIT;
137       longswaps >>= 3;
138
139       if (newfrom == NULL)
140         {
141           newfrom = (unsigned char *) xmalloc (fmt->totalsize);
142         }
143       swapout = newfrom;
144       swapin = ufrom;
145       ufrom = newfrom;
146       while (longswaps-- > 0)
147         {
148           /* This is ugly, but efficient */
149           *swapout++ = swapin[4];
150           *swapout++ = swapin[5];
151           *swapout++ = swapin[6];
152           *swapout++ = swapin[7];
153           *swapout++ = swapin[0];
154           *swapout++ = swapin[1];
155           *swapout++ = swapin[2];
156           *swapout++ = swapin[3];
157           swapin += 8;
158         }
159     }
160
161   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
162                         fmt->exp_start, fmt->exp_len);
163   /* Note that if exponent indicates a NaN, we can't really do anything useful
164      (not knowing if the host has NaN's, or how to build one).  So it will
165      end up as an infinity or something close; that is OK.  */
166
167   mant_bits_left = fmt->man_len;
168   mant_off = fmt->man_start;
169   dto = 0.0;
170
171   special_exponent = exponent == 0 || exponent == fmt->exp_nan;
172
173 /* Don't bias NaNs. Use minimum exponent for denorms. For simplicity,
174    we don't check for zero as the exponent doesn't matter. */
175   if (!special_exponent)
176     exponent -= fmt->exp_bias;
177   else if (exponent == 0)
178     exponent = 1 - fmt->exp_bias;
179
180   /* Build the result algebraically.  Might go infinite, underflow, etc;
181      who cares. */
182
183 /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
184    increment the exponent by one to account for the integer bit.  */
185
186   if (!special_exponent)
187     {
188       if (fmt->intbit == floatformat_intbit_no)
189         dto = ldexp (1.0, exponent);
190       else
191         exponent++;
192     }
193
194   while (mant_bits_left > 0)
195     {
196       mant_bits = min (mant_bits_left, 32);
197
198       mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
199                         mant_off, mant_bits);
200
201       dto += ldexp ((double) mant, exponent - mant_bits);
202       exponent -= mant_bits;
203       mant_off += mant_bits;
204       mant_bits_left -= mant_bits;
205     }
206
207   /* Negate it if negative.  */
208   if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
209     dto = -dto;
210   *to = dto;
211 }
212 \f
213 static void put_field (unsigned char *, enum floatformat_byteorders,
214                        unsigned int,
215                        unsigned int, unsigned int, unsigned long);
216
217 /* Set a field which starts at START and is LEN bytes long.  DATA and
218    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
219 static void
220 put_field (unsigned char *data, enum floatformat_byteorders order,
221            unsigned int total_len, unsigned int start, unsigned int len,
222            unsigned long stuff_to_put)
223 {
224   unsigned int cur_byte;
225   int cur_bitshift;
226
227   /* Start at the least significant part of the field.  */
228   if (order == floatformat_little || order == floatformat_littlebyte_bigword)
229     {
230       int excess = FLOATFORMAT_CHAR_BIT - (total_len % FLOATFORMAT_CHAR_BIT);
231       cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) 
232                  - ((start + len + excess) / FLOATFORMAT_CHAR_BIT);
233       cur_bitshift = ((start + len + excess) % FLOATFORMAT_CHAR_BIT) 
234                      - FLOATFORMAT_CHAR_BIT;
235     }
236   else
237     {
238       cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
239       cur_bitshift =
240         ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
241     }
242   if (cur_bitshift > -FLOATFORMAT_CHAR_BIT)
243     {
244       *(data + cur_byte) &=
245         ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1)
246           << (-cur_bitshift));
247       *(data + cur_byte) |=
248         (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
249     }
250   cur_bitshift += FLOATFORMAT_CHAR_BIT;
251   if (order == floatformat_little || order == floatformat_littlebyte_bigword)
252     ++cur_byte;
253   else
254     --cur_byte;
255
256   /* Move towards the most significant part of the field.  */
257   while (cur_bitshift < len)
258     {
259       if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
260         {
261           /* This is the last byte.  */
262           *(data + cur_byte) &=
263             ~((1 << (len - cur_bitshift)) - 1);
264           *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
265         }
266       else
267         *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
268                               & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
269       cur_bitshift += FLOATFORMAT_CHAR_BIT;
270       if (order == floatformat_little || order == floatformat_littlebyte_bigword)
271         ++cur_byte;
272       else
273         --cur_byte;
274     }
275 }
276
277 #ifdef HAVE_LONG_DOUBLE
278 /* Return the fractional part of VALUE, and put the exponent of VALUE in *EPTR.
279    The range of the returned value is >= 0.5 and < 1.0.  This is equivalent to
280    frexp, but operates on the long double data type.  */
281
282 static long double ldfrexp (long double value, int *eptr);
283
284 static long double
285 ldfrexp (long double value, int *eptr)
286 {
287   long double tmp;
288   int exp;
289
290   /* Unfortunately, there are no portable functions for extracting the exponent
291      of a long double, so we have to do it iteratively by multiplying or dividing
292      by two until the fraction is between 0.5 and 1.0.  */
293
294   if (value < 0.0l)
295     value = -value;
296
297   tmp = 1.0l;
298   exp = 0;
299
300   if (value >= tmp)             /* Value >= 1.0 */
301     while (value >= tmp)
302       {
303         tmp *= 2.0l;
304         exp++;
305       }
306   else if (value != 0.0l)       /* Value < 1.0  and > 0.0 */
307     {
308       while (value < tmp)
309         {
310           tmp /= 2.0l;
311           exp--;
312         }
313       tmp *= 2.0l;
314       exp++;
315     }
316
317   *eptr = exp;
318   return value / tmp;
319 }
320 #endif /* HAVE_LONG_DOUBLE */
321
322
323 /* The converse: convert the DOUBLEST *FROM to an extended float
324    and store where TO points.  Neither FROM nor TO have any alignment
325    restrictions.  */
326
327 void
328 floatformat_from_doublest (CONST struct floatformat *fmt, DOUBLEST *from,
329                            char *to)
330 {
331   DOUBLEST dfrom;
332   int exponent;
333   DOUBLEST mant;
334   unsigned int mant_bits, mant_off;
335   int mant_bits_left;
336   unsigned char *uto = (unsigned char *) to;
337
338   memcpy (&dfrom, from, sizeof (dfrom));
339   memset (uto, 0, (fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1) 
340                     / FLOATFORMAT_CHAR_BIT);
341   if (dfrom == 0)
342     return;                     /* Result is zero */
343   if (dfrom != dfrom)           /* Result is NaN */
344     {
345       /* From is NaN */
346       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
347                  fmt->exp_len, fmt->exp_nan);
348       /* Be sure it's not infinity, but NaN value is irrel */
349       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
350                  32, 1);
351       return;
352     }
353
354   /* If negative, set the sign bit.  */
355   if (dfrom < 0)
356     {
357       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
358       dfrom = -dfrom;
359     }
360
361   if (dfrom + dfrom == dfrom && dfrom != 0.0)   /* Result is Infinity */
362     {
363       /* Infinity exponent is same as NaN's.  */
364       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
365                  fmt->exp_len, fmt->exp_nan);
366       /* Infinity mantissa is all zeroes.  */
367       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
368                  fmt->man_len, 0);
369       return;
370     }
371
372 #ifdef HAVE_LONG_DOUBLE
373   mant = ldfrexp (dfrom, &exponent);
374 #else
375   mant = frexp (dfrom, &exponent);
376 #endif
377
378   put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
379              exponent + fmt->exp_bias - 1);
380
381   mant_bits_left = fmt->man_len;
382   mant_off = fmt->man_start;
383   while (mant_bits_left > 0)
384     {
385       unsigned long mant_long;
386       mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
387
388       mant *= 4294967296.0;
389       mant_long = ((unsigned long) mant) & 0xffffffffL;
390       mant -= mant_long;
391
392       /* If the integer bit is implicit, then we need to discard it.
393          If we are discarding a zero, we should be (but are not) creating
394          a denormalized number which means adjusting the exponent
395          (I think).  */
396       if (mant_bits_left == fmt->man_len
397           && fmt->intbit == floatformat_intbit_no)
398         {
399           mant_long <<= 1;
400           mant_long &= 0xffffffffL;
401           mant_bits -= 1;
402         }
403
404       if (mant_bits < 32)
405         {
406           /* The bits we want are in the most significant MANT_BITS bits of
407              mant_long.  Move them to the least significant.  */
408           mant_long >>= 32 - mant_bits;
409         }
410
411       put_field (uto, fmt->byteorder, fmt->totalsize,
412                  mant_off, mant_bits, mant_long);
413       mant_off += mant_bits;
414       mant_bits_left -= mant_bits;
415     }
416   if (fmt->byteorder == floatformat_littlebyte_bigword)
417     {
418       int count;
419       unsigned char *swaplow = uto;
420       unsigned char *swaphigh = uto + 4;
421       unsigned char tmp;
422
423       for (count = 0; count < 4; count++)
424         {
425           tmp = *swaplow;
426           *swaplow++ = *swaphigh;
427           *swaphigh++ = tmp;
428         }
429     }
430 }
431
432 /* Check if VAL (which is assumed to be a floating point number whose
433    format is described by FMT) is negative.  */
434
435 int
436 floatformat_is_negative (const struct floatformat *fmt, char *val)
437 {
438   unsigned char *uval = (unsigned char *) val;
439
440   return get_field (uval, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1);
441 }
442
443 /* Check if VAL is "not a number" (NaN) for FMT.  */
444
445 int
446 floatformat_is_nan (const struct floatformat *fmt, char *val)
447 {
448   unsigned char *uval = (unsigned char *) val;
449   long exponent;
450   unsigned long mant;
451   unsigned int mant_bits, mant_off;
452   int mant_bits_left;
453
454   if (! fmt->exp_nan)
455     return 0;
456
457   exponent = get_field (uval, fmt->byteorder, fmt->totalsize,
458                         fmt->exp_start, fmt->exp_len);
459
460   if (exponent != fmt->exp_nan)
461     return 0;
462
463   mant_bits_left = fmt->man_len;
464   mant_off = fmt->man_start;
465
466   while (mant_bits_left > 0)
467     {
468       mant_bits = min (mant_bits_left, 32);
469
470       mant = get_field (uval, fmt->byteorder, fmt->totalsize,
471                         mant_off, mant_bits);
472
473       /* If there is an explicit integer bit, mask it off.  */
474       if (mant_off == fmt->man_start
475           && fmt->intbit == floatformat_intbit_yes)
476         mant &= ~(1 << (mant_bits - 1));
477
478       if (mant)
479         return 1;
480
481       mant_off += mant_bits;
482       mant_bits_left -= mant_bits;
483     }
484
485   return 0;
486 }
487
488 /* Convert the mantissa of VAL (which is assumed to be a floating
489    point number whose format is described by FMT) into a hexadecimal
490    and store it in a static string.  Return a pointer to that string.  */
491
492 char *
493 floatformat_mantissa (const struct floatformat *fmt, char *val)
494 {
495   unsigned char *uval = (unsigned char *) val;
496   unsigned long mant;
497   unsigned int mant_bits, mant_off;
498   int mant_bits_left;
499   static char res[50];
500   char buf[9];
501
502   /* Make sure we have enough room to store the mantissa.  */
503   gdb_assert (sizeof res > ((fmt->man_len + 7) / 8) * 2);
504
505   mant_off = fmt->man_start;
506   mant_bits_left = fmt->man_len;
507   mant_bits = (mant_bits_left % 32) > 0 ? mant_bits_left % 32 : 32;
508
509   mant = get_field (uval, fmt->byteorder, fmt->totalsize,
510                     mant_off, mant_bits);
511
512   sprintf (res, "%lx", mant);
513
514   mant_off += mant_bits;
515   mant_bits_left -= mant_bits;
516   
517   while (mant_bits_left > 0)
518     {
519       mant = get_field (uval, fmt->byteorder, fmt->totalsize,
520                         mant_off, 32);
521
522       sprintf (buf, "%08lx", mant);
523       strcat (res, buf);
524
525       mant_off += 32;
526       mant_bits_left -= 32;
527     }
528
529   return res;
530 }
531
532
533 \f
534 /* Extract a floating-point number from a target-order byte-stream at ADDR.
535    Returns the value as type DOUBLEST.
536
537    If the host and target formats agree, we just copy the raw data into the
538    appropriate type of variable and return, letting the host increase precision
539    as necessary.  Otherwise, we call the conversion routine and let it do the
540    dirty work.  */
541
542 DOUBLEST
543 extract_floating (void *addr, int len)
544 {
545   DOUBLEST dretval;
546
547   if (len * TARGET_CHAR_BIT == TARGET_FLOAT_BIT)
548     {
549       if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
550         {
551           float retval;
552
553           memcpy (&retval, addr, sizeof (retval));
554           return retval;
555         }
556       else
557         floatformat_to_doublest (TARGET_FLOAT_FORMAT, addr, &dretval);
558     }
559   else if (len * TARGET_CHAR_BIT == TARGET_DOUBLE_BIT)
560     {
561       if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
562         {
563           double retval;
564
565           memcpy (&retval, addr, sizeof (retval));
566           return retval;
567         }
568       else
569         floatformat_to_doublest (TARGET_DOUBLE_FORMAT, addr, &dretval);
570     }
571   else if (len * TARGET_CHAR_BIT == TARGET_LONG_DOUBLE_BIT)
572     {
573       if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
574         {
575           DOUBLEST retval;
576
577           memcpy (&retval, addr, sizeof (retval));
578           return retval;
579         }
580       else
581         floatformat_to_doublest (TARGET_LONG_DOUBLE_FORMAT, addr, &dretval);
582     }
583   else
584     {
585       error ("Can't deal with a floating point number of %d bytes.", len);
586     }
587
588   return dretval;
589 }
590
591 void
592 store_floating (void *addr, int len, DOUBLEST val)
593 {
594   if (len * TARGET_CHAR_BIT == TARGET_FLOAT_BIT)
595     {
596       if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT)
597         {
598           float floatval = val;
599
600           memcpy (addr, &floatval, sizeof (floatval));
601         }
602       else
603         floatformat_from_doublest (TARGET_FLOAT_FORMAT, &val, addr);
604     }
605   else if (len * TARGET_CHAR_BIT == TARGET_DOUBLE_BIT)
606     {
607       if (HOST_DOUBLE_FORMAT == TARGET_DOUBLE_FORMAT)
608         {
609           double doubleval = val;
610
611           memcpy (addr, &doubleval, sizeof (doubleval));
612         }
613       else
614         floatformat_from_doublest (TARGET_DOUBLE_FORMAT, &val, addr);
615     }
616   else if (len * TARGET_CHAR_BIT == TARGET_LONG_DOUBLE_BIT)
617     {
618       if (HOST_LONG_DOUBLE_FORMAT == TARGET_LONG_DOUBLE_FORMAT)
619         memcpy (addr, &val, sizeof (val));
620       else
621         floatformat_from_doublest (TARGET_LONG_DOUBLE_FORMAT, &val, addr);
622     }
623   else
624     {
625       error ("Can't deal with a floating point number of %d bytes.", len);
626     }
627 }