Home | History | Annotate | Line # | Download | only in libiberty
floatformat.c revision 1.3
      1  1.1  mrg /* IEEE floating point support routines, for GDB, the GNU Debugger.
      2  1.3  mrg    Copyright 1991, 1994, 1999, 2000, 2003, 2005, 2006, 2010, 2012
      3  1.1  mrg    Free Software Foundation, Inc.
      4  1.1  mrg 
      5  1.1  mrg This file is part of GDB.
      6  1.1  mrg 
      7  1.1  mrg This program is free software; you can redistribute it and/or modify
      8  1.1  mrg it under the terms of the GNU General Public License as published by
      9  1.1  mrg the Free Software Foundation; either version 2 of the License, or
     10  1.1  mrg (at your option) any later version.
     11  1.1  mrg 
     12  1.1  mrg This program is distributed in the hope that it will be useful,
     13  1.1  mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  mrg GNU General Public License for more details.
     16  1.1  mrg 
     17  1.1  mrg You should have received a copy of the GNU General Public License
     18  1.1  mrg along with this program; if not, write to the Free Software
     19  1.1  mrg Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
     20  1.1  mrg 
     21  1.1  mrg /* This is needed to pick up the NAN macro on some systems.  */
     22  1.1  mrg #define _GNU_SOURCE
     23  1.1  mrg 
     24  1.1  mrg #ifdef HAVE_CONFIG_H
     25  1.1  mrg #include "config.h"
     26  1.1  mrg #endif
     27  1.1  mrg 
     28  1.1  mrg #include <math.h>
     29  1.1  mrg 
     30  1.1  mrg #ifdef HAVE_STRING_H
     31  1.1  mrg #include <string.h>
     32  1.1  mrg #endif
     33  1.1  mrg 
     34  1.1  mrg /* On some platforms, <float.h> provides DBL_QNAN.  */
     35  1.1  mrg #ifdef STDC_HEADERS
     36  1.1  mrg #include <float.h>
     37  1.1  mrg #endif
     38  1.1  mrg 
     39  1.1  mrg #include "ansidecl.h"
     40  1.1  mrg #include "libiberty.h"
     41  1.1  mrg #include "floatformat.h"
     42  1.1  mrg 
     43  1.1  mrg #ifndef INFINITY
     44  1.1  mrg #ifdef HUGE_VAL
     45  1.1  mrg #define INFINITY HUGE_VAL
     46  1.1  mrg #else
     47  1.1  mrg #define INFINITY (1.0 / 0.0)
     48  1.1  mrg #endif
     49  1.1  mrg #endif
     50  1.1  mrg 
     51  1.1  mrg #ifndef NAN
     52  1.1  mrg #ifdef DBL_QNAN
     53  1.1  mrg #define NAN DBL_QNAN
     54  1.1  mrg #else
     55  1.1  mrg #define NAN (0.0 / 0.0)
     56  1.1  mrg #endif
     57  1.1  mrg #endif
     58  1.1  mrg 
     59  1.1  mrg static int mant_bits_set (const struct floatformat *, const unsigned char *);
     60  1.1  mrg static unsigned long get_field (const unsigned char *,
     61  1.1  mrg                                 enum floatformat_byteorders,
     62  1.1  mrg                                 unsigned int,
     63  1.1  mrg                                 unsigned int,
     64  1.1  mrg                                 unsigned int);
     65  1.1  mrg static int floatformat_always_valid (const struct floatformat *fmt,
     66  1.1  mrg                                      const void *from);
     67  1.1  mrg 
     68  1.1  mrg static int
     69  1.1  mrg floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
     70  1.1  mrg                           const void *from ATTRIBUTE_UNUSED)
     71  1.1  mrg {
     72  1.1  mrg   return 1;
     73  1.1  mrg }
     74  1.1  mrg 
     75  1.1  mrg /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
     76  1.1  mrg    going to bother with trying to muck around with whether it is defined in
     77  1.1  mrg    a system header, what we do if not, etc.  */
     78  1.1  mrg #define FLOATFORMAT_CHAR_BIT 8
     79  1.1  mrg 
     80  1.3  mrg /* floatformats for IEEE half, single and double, big and little endian.  */
     81  1.3  mrg const struct floatformat floatformat_ieee_half_big =
     82  1.3  mrg {
     83  1.3  mrg   floatformat_big, 16, 0, 1, 5, 15, 31, 6, 10,
     84  1.3  mrg   floatformat_intbit_no,
     85  1.3  mrg   "floatformat_ieee_half_big",
     86  1.3  mrg   floatformat_always_valid,
     87  1.3  mrg   NULL
     88  1.3  mrg };
     89  1.3  mrg const struct floatformat floatformat_ieee_half_little =
     90  1.3  mrg {
     91  1.3  mrg   floatformat_little, 16, 0, 1, 5, 15, 31, 6, 10,
     92  1.3  mrg   floatformat_intbit_no,
     93  1.3  mrg   "floatformat_ieee_half_little",
     94  1.3  mrg   floatformat_always_valid,
     95  1.3  mrg   NULL
     96  1.3  mrg };
     97  1.1  mrg const struct floatformat floatformat_ieee_single_big =
     98  1.1  mrg {
     99  1.1  mrg   floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
    100  1.1  mrg   floatformat_intbit_no,
    101  1.1  mrg   "floatformat_ieee_single_big",
    102  1.1  mrg   floatformat_always_valid,
    103  1.1  mrg   NULL
    104  1.1  mrg };
    105  1.1  mrg const struct floatformat floatformat_ieee_single_little =
    106  1.1  mrg {
    107  1.1  mrg   floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
    108  1.1  mrg   floatformat_intbit_no,
    109  1.1  mrg   "floatformat_ieee_single_little",
    110  1.1  mrg   floatformat_always_valid,
    111  1.1  mrg   NULL
    112  1.1  mrg };
    113  1.1  mrg const struct floatformat floatformat_ieee_double_big =
    114  1.1  mrg {
    115  1.1  mrg   floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
    116  1.1  mrg   floatformat_intbit_no,
    117  1.1  mrg   "floatformat_ieee_double_big",
    118  1.1  mrg   floatformat_always_valid,
    119  1.1  mrg   NULL
    120  1.1  mrg };
    121  1.1  mrg const struct floatformat floatformat_ieee_double_little =
    122  1.1  mrg {
    123  1.1  mrg   floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
    124  1.1  mrg   floatformat_intbit_no,
    125  1.1  mrg   "floatformat_ieee_double_little",
    126  1.1  mrg   floatformat_always_valid,
    127  1.1  mrg   NULL
    128  1.1  mrg };
    129  1.1  mrg 
    130  1.1  mrg /* floatformat for IEEE double, little endian byte order, with big endian word
    131  1.1  mrg    ordering, as on the ARM.  */
    132  1.1  mrg 
    133  1.1  mrg const struct floatformat floatformat_ieee_double_littlebyte_bigword =
    134  1.1  mrg {
    135  1.1  mrg   floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
    136  1.1  mrg   floatformat_intbit_no,
    137  1.1  mrg   "floatformat_ieee_double_littlebyte_bigword",
    138  1.1  mrg   floatformat_always_valid,
    139  1.1  mrg   NULL
    140  1.1  mrg };
    141  1.1  mrg 
    142  1.1  mrg /* floatformat for VAX.  Not quite IEEE, but close enough.  */
    143  1.1  mrg 
    144  1.1  mrg const struct floatformat floatformat_vax_f =
    145  1.1  mrg {
    146  1.1  mrg   floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
    147  1.1  mrg   floatformat_intbit_no,
    148  1.1  mrg   "floatformat_vax_f",
    149  1.1  mrg   floatformat_always_valid,
    150  1.1  mrg   NULL
    151  1.1  mrg };
    152  1.1  mrg const struct floatformat floatformat_vax_d =
    153  1.1  mrg {
    154  1.1  mrg   floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
    155  1.1  mrg   floatformat_intbit_no,
    156  1.1  mrg   "floatformat_vax_d",
    157  1.1  mrg   floatformat_always_valid,
    158  1.1  mrg   NULL
    159  1.1  mrg };
    160  1.1  mrg const struct floatformat floatformat_vax_g =
    161  1.1  mrg {
    162  1.1  mrg   floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
    163  1.1  mrg   floatformat_intbit_no,
    164  1.1  mrg   "floatformat_vax_g",
    165  1.1  mrg   floatformat_always_valid,
    166  1.1  mrg   NULL
    167  1.1  mrg };
    168  1.1  mrg 
    169  1.1  mrg static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
    170  1.1  mrg 					  const void *from);
    171  1.1  mrg 
    172  1.1  mrg static int
    173  1.1  mrg floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
    174  1.1  mrg {
    175  1.1  mrg   /* In the i387 double-extended format, if the exponent is all ones,
    176  1.1  mrg      then the integer bit must be set.  If the exponent is neither 0
    177  1.1  mrg      nor ~0, the intbit must also be set.  Only if the exponent is
    178  1.1  mrg      zero can it be zero, and then it must be zero.  */
    179  1.1  mrg   unsigned long exponent, int_bit;
    180  1.1  mrg   const unsigned char *ufrom = (const unsigned char *) from;
    181  1.1  mrg 
    182  1.1  mrg   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
    183  1.1  mrg 			fmt->exp_start, fmt->exp_len);
    184  1.1  mrg   int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
    185  1.1  mrg 		       fmt->man_start, 1);
    186  1.1  mrg 
    187  1.1  mrg   if ((exponent == 0) != (int_bit == 0))
    188  1.1  mrg     return 0;
    189  1.1  mrg   else
    190  1.1  mrg     return 1;
    191  1.1  mrg }
    192  1.1  mrg 
    193  1.1  mrg const struct floatformat floatformat_i387_ext =
    194  1.1  mrg {
    195  1.1  mrg   floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
    196  1.1  mrg   floatformat_intbit_yes,
    197  1.1  mrg   "floatformat_i387_ext",
    198  1.1  mrg   floatformat_i387_ext_is_valid,
    199  1.1  mrg   NULL
    200  1.1  mrg };
    201  1.1  mrg const struct floatformat floatformat_m68881_ext =
    202  1.1  mrg {
    203  1.1  mrg   /* Note that the bits from 16 to 31 are unused.  */
    204  1.1  mrg   floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
    205  1.1  mrg   floatformat_intbit_yes,
    206  1.1  mrg   "floatformat_m68881_ext",
    207  1.1  mrg   floatformat_always_valid,
    208  1.1  mrg   NULL
    209  1.1  mrg };
    210  1.1  mrg const struct floatformat floatformat_i960_ext =
    211  1.1  mrg {
    212  1.1  mrg   /* Note that the bits from 0 to 15 are unused.  */
    213  1.1  mrg   floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
    214  1.1  mrg   floatformat_intbit_yes,
    215  1.1  mrg   "floatformat_i960_ext",
    216  1.1  mrg   floatformat_always_valid,
    217  1.1  mrg   NULL
    218  1.1  mrg };
    219  1.1  mrg const struct floatformat floatformat_m88110_ext =
    220  1.1  mrg {
    221  1.1  mrg   floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
    222  1.1  mrg   floatformat_intbit_yes,
    223  1.1  mrg   "floatformat_m88110_ext",
    224  1.1  mrg   floatformat_always_valid,
    225  1.1  mrg   NULL
    226  1.1  mrg };
    227  1.1  mrg const struct floatformat floatformat_m88110_harris_ext =
    228  1.1  mrg {
    229  1.1  mrg   /* Harris uses raw format 128 bytes long, but the number is just an ieee
    230  1.1  mrg      double, and the last 64 bits are wasted. */
    231  1.1  mrg   floatformat_big,128, 0, 1, 11,  0x3ff,  0x7ff, 12, 52,
    232  1.1  mrg   floatformat_intbit_no,
    233  1.1  mrg   "floatformat_m88110_ext_harris",
    234  1.1  mrg   floatformat_always_valid,
    235  1.1  mrg   NULL
    236  1.1  mrg };
    237  1.1  mrg const struct floatformat floatformat_arm_ext_big =
    238  1.1  mrg {
    239  1.1  mrg   /* Bits 1 to 16 are unused.  */
    240  1.1  mrg   floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
    241  1.1  mrg   floatformat_intbit_yes,
    242  1.1  mrg   "floatformat_arm_ext_big",
    243  1.1  mrg   floatformat_always_valid,
    244  1.1  mrg   NULL
    245  1.1  mrg };
    246  1.1  mrg const struct floatformat floatformat_arm_ext_littlebyte_bigword =
    247  1.1  mrg {
    248  1.1  mrg   /* Bits 1 to 16 are unused.  */
    249  1.1  mrg   floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
    250  1.1  mrg   floatformat_intbit_yes,
    251  1.1  mrg   "floatformat_arm_ext_littlebyte_bigword",
    252  1.1  mrg   floatformat_always_valid,
    253  1.1  mrg   NULL
    254  1.1  mrg };
    255  1.1  mrg const struct floatformat floatformat_ia64_spill_big =
    256  1.1  mrg {
    257  1.1  mrg   floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
    258  1.1  mrg   floatformat_intbit_yes,
    259  1.1  mrg   "floatformat_ia64_spill_big",
    260  1.1  mrg   floatformat_always_valid,
    261  1.1  mrg   NULL
    262  1.1  mrg };
    263  1.1  mrg const struct floatformat floatformat_ia64_spill_little =
    264  1.1  mrg {
    265  1.1  mrg   floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
    266  1.1  mrg   floatformat_intbit_yes,
    267  1.1  mrg   "floatformat_ia64_spill_little",
    268  1.1  mrg   floatformat_always_valid,
    269  1.1  mrg   NULL
    270  1.1  mrg };
    271  1.1  mrg const struct floatformat floatformat_ia64_quad_big =
    272  1.1  mrg {
    273  1.1  mrg   floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
    274  1.1  mrg   floatformat_intbit_no,
    275  1.1  mrg   "floatformat_ia64_quad_big",
    276  1.1  mrg   floatformat_always_valid,
    277  1.1  mrg   NULL
    278  1.1  mrg };
    279  1.1  mrg const struct floatformat floatformat_ia64_quad_little =
    280  1.1  mrg {
    281  1.1  mrg   floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
    282  1.1  mrg   floatformat_intbit_no,
    283  1.1  mrg   "floatformat_ia64_quad_little",
    284  1.1  mrg   floatformat_always_valid,
    285  1.1  mrg   NULL
    286  1.1  mrg };
    287  1.1  mrg 
    288  1.1  mrg static int
    289  1.1  mrg floatformat_ibm_long_double_is_valid (const struct floatformat *fmt,
    290  1.1  mrg 				      const void *from)
    291  1.1  mrg {
    292  1.1  mrg   const unsigned char *ufrom = (const unsigned char *) from;
    293  1.1  mrg   const struct floatformat *hfmt = fmt->split_half;
    294  1.1  mrg   long top_exp, bot_exp;
    295  1.1  mrg   int top_nan = 0;
    296  1.1  mrg 
    297  1.1  mrg   top_exp = get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
    298  1.1  mrg 		       hfmt->exp_start, hfmt->exp_len);
    299  1.1  mrg   bot_exp = get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
    300  1.1  mrg 		       hfmt->exp_start, hfmt->exp_len);
    301  1.1  mrg 
    302  1.1  mrg   if ((unsigned long) top_exp == hfmt->exp_nan)
    303  1.1  mrg     top_nan = mant_bits_set (hfmt, ufrom);
    304  1.1  mrg 
    305  1.1  mrg   /* A NaN is valid with any low part.  */
    306  1.1  mrg   if (top_nan)
    307  1.1  mrg     return 1;
    308  1.1  mrg 
    309  1.1  mrg   /* An infinity, zero or denormal requires low part 0 (positive or
    310  1.1  mrg      negative).  */
    311  1.1  mrg   if ((unsigned long) top_exp == hfmt->exp_nan || top_exp == 0)
    312  1.1  mrg     {
    313  1.1  mrg       if (bot_exp != 0)
    314  1.1  mrg 	return 0;
    315  1.1  mrg 
    316  1.1  mrg       return !mant_bits_set (hfmt, ufrom + 8);
    317  1.1  mrg     }
    318  1.1  mrg 
    319  1.1  mrg   /* The top part is now a finite normal value.  The long double value
    320  1.1  mrg      is the sum of the two parts, and the top part must equal the
    321  1.1  mrg      result of rounding the long double value to nearest double.  Thus
    322  1.1  mrg      the bottom part must be <= 0.5ulp of the top part in absolute
    323  1.1  mrg      value, and if it is < 0.5ulp then the long double is definitely
    324  1.1  mrg      valid.  */
    325  1.1  mrg   if (bot_exp < top_exp - 53)
    326  1.1  mrg     return 1;
    327  1.1  mrg   if (bot_exp > top_exp - 53 && bot_exp != 0)
    328  1.1  mrg     return 0;
    329  1.1  mrg   if (bot_exp == 0)
    330  1.1  mrg     {
    331  1.1  mrg       /* The bottom part is 0 or denormal.  Determine which, and if
    332  1.1  mrg 	 denormal the first two set bits.  */
    333  1.1  mrg       int first_bit = -1, second_bit = -1, cur_bit;
    334  1.1  mrg       for (cur_bit = 0; (unsigned int) cur_bit < hfmt->man_len; cur_bit++)
    335  1.1  mrg 	if (get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
    336  1.1  mrg 		       hfmt->man_start + cur_bit, 1))
    337  1.1  mrg 	  {
    338  1.1  mrg 	    if (first_bit == -1)
    339  1.1  mrg 	      first_bit = cur_bit;
    340  1.1  mrg 	    else
    341  1.1  mrg 	      {
    342  1.1  mrg 		second_bit = cur_bit;
    343  1.1  mrg 		break;
    344  1.1  mrg 	      }
    345  1.1  mrg 	  }
    346  1.1  mrg       /* Bottom part 0 is OK.  */
    347  1.1  mrg       if (first_bit == -1)
    348  1.1  mrg 	return 1;
    349  1.1  mrg       /* The real exponent of the bottom part is -first_bit.  */
    350  1.1  mrg       if (-first_bit < top_exp - 53)
    351  1.1  mrg 	return 1;
    352  1.1  mrg       if (-first_bit > top_exp - 53)
    353  1.1  mrg 	return 0;
    354  1.1  mrg       /* The bottom part is at least 0.5ulp of the top part.  For this
    355  1.1  mrg 	 to be OK, the bottom part must be exactly 0.5ulp (i.e. no
    356  1.1  mrg 	 more bits set) and the top part must have last bit 0.  */
    357  1.1  mrg       if (second_bit != -1)
    358  1.1  mrg 	return 0;
    359  1.1  mrg       return !get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
    360  1.1  mrg 			 hfmt->man_start + hfmt->man_len - 1, 1);
    361  1.1  mrg     }
    362  1.1  mrg   else
    363  1.1  mrg     {
    364  1.1  mrg       /* The bottom part is at least 0.5ulp of the top part.  For this
    365  1.1  mrg 	 to be OK, it must be exactly 0.5ulp (i.e. no explicit bits
    366  1.1  mrg 	 set) and the top part must have last bit 0.  */
    367  1.1  mrg       if (get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
    368  1.1  mrg 		     hfmt->man_start + hfmt->man_len - 1, 1))
    369  1.1  mrg 	return 0;
    370  1.1  mrg       return !mant_bits_set (hfmt, ufrom + 8);
    371  1.1  mrg     }
    372  1.1  mrg }
    373  1.1  mrg 
    374  1.1  mrg const struct floatformat floatformat_ibm_long_double =
    375  1.1  mrg {
    376  1.1  mrg   floatformat_big, 128, 0, 1, 11, 1023, 2047, 12, 52,
    377  1.1  mrg   floatformat_intbit_no,
    378  1.1  mrg   "floatformat_ibm_long_double",
    379  1.1  mrg   floatformat_ibm_long_double_is_valid,
    380  1.1  mrg   &floatformat_ieee_double_big
    381  1.1  mrg };
    382  1.1  mrg 
    383  1.1  mrg 
    385  1.1  mrg #ifndef min
    386  1.1  mrg #define min(a, b) ((a) < (b) ? (a) : (b))
    387  1.1  mrg #endif
    388  1.1  mrg 
    389  1.1  mrg /* Return 1 if any bits are explicitly set in the mantissa of UFROM,
    390  1.1  mrg    format FMT, 0 otherwise.  */
    391  1.1  mrg static int
    392  1.1  mrg mant_bits_set (const struct floatformat *fmt, const unsigned char *ufrom)
    393  1.1  mrg {
    394  1.1  mrg   unsigned int mant_bits, mant_off;
    395  1.1  mrg   int mant_bits_left;
    396  1.1  mrg 
    397  1.1  mrg   mant_off = fmt->man_start;
    398  1.1  mrg   mant_bits_left = fmt->man_len;
    399  1.1  mrg   while (mant_bits_left > 0)
    400  1.1  mrg     {
    401  1.1  mrg       mant_bits = min (mant_bits_left, 32);
    402  1.1  mrg 
    403  1.1  mrg       if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
    404  1.1  mrg 		     mant_off, mant_bits) != 0)
    405  1.1  mrg 	return 1;
    406  1.1  mrg 
    407  1.1  mrg       mant_off += mant_bits;
    408  1.1  mrg       mant_bits_left -= mant_bits;
    409  1.1  mrg     }
    410  1.1  mrg   return 0;
    411  1.1  mrg }
    412  1.1  mrg 
    413  1.1  mrg /* Extract a field which starts at START and is LEN bits long.  DATA and
    414  1.1  mrg    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
    415  1.1  mrg static unsigned long
    416  1.1  mrg get_field (const unsigned char *data, enum floatformat_byteorders order,
    417  1.1  mrg            unsigned int total_len, unsigned int start, unsigned int len)
    418  1.1  mrg {
    419  1.1  mrg   unsigned long result = 0;
    420  1.1  mrg   unsigned int cur_byte;
    421  1.1  mrg   int lo_bit, hi_bit, cur_bitshift = 0;
    422  1.1  mrg   int nextbyte = (order == floatformat_little) ? 1 : -1;
    423  1.1  mrg 
    424  1.1  mrg   /* Start is in big-endian bit order!  Fix that first.  */
    425  1.1  mrg   start = total_len - (start + len);
    426  1.1  mrg 
    427  1.1  mrg   /* Start at the least significant part of the field.  */
    428  1.1  mrg   if (order == floatformat_little)
    429  1.1  mrg     cur_byte = start / FLOATFORMAT_CHAR_BIT;
    430  1.1  mrg   else
    431  1.1  mrg     cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
    432  1.1  mrg 
    433  1.1  mrg   lo_bit = start % FLOATFORMAT_CHAR_BIT;
    434  1.1  mrg   hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
    435  1.1  mrg 
    436  1.1  mrg   do
    437  1.1  mrg     {
    438  1.1  mrg       unsigned int shifted = *(data + cur_byte) >> lo_bit;
    439  1.1  mrg       unsigned int bits = hi_bit - lo_bit;
    440  1.1  mrg       unsigned int mask = (1 << bits) - 1;
    441  1.1  mrg       result |= (shifted & mask) << cur_bitshift;
    442  1.1  mrg       len -= bits;
    443  1.1  mrg       cur_bitshift += bits;
    444  1.1  mrg       cur_byte += nextbyte;
    445  1.1  mrg       lo_bit = 0;
    446  1.1  mrg       hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
    447  1.1  mrg     }
    448  1.1  mrg   while (len != 0);
    449  1.1  mrg 
    450  1.1  mrg   return result;
    451  1.1  mrg }
    452  1.1  mrg 
    453  1.1  mrg /* Convert from FMT to a double.
    454  1.1  mrg    FROM is the address of the extended float.
    455  1.1  mrg    Store the double in *TO.  */
    456  1.1  mrg 
    457  1.1  mrg void
    458  1.1  mrg floatformat_to_double (const struct floatformat *fmt,
    459  1.1  mrg                        const void *from, double *to)
    460  1.1  mrg {
    461  1.1  mrg   const unsigned char *ufrom = (const unsigned char *) from;
    462  1.1  mrg   double dto;
    463  1.1  mrg   long exponent;
    464  1.1  mrg   unsigned long mant;
    465  1.1  mrg   unsigned int mant_bits, mant_off;
    466  1.1  mrg   int mant_bits_left;
    467  1.1  mrg 
    468  1.1  mrg   /* Split values are not handled specially, since the top half has
    469  1.1  mrg      the correctly rounded double value (in the only supported case of
    470  1.1  mrg      split values).  */
    471  1.1  mrg 
    472  1.1  mrg   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
    473  1.1  mrg 			fmt->exp_start, fmt->exp_len);
    474  1.1  mrg 
    475  1.1  mrg   /* If the exponent indicates a NaN, we don't have information to
    476  1.1  mrg      decide what to do.  So we handle it like IEEE, except that we
    477  1.1  mrg      don't try to preserve the type of NaN.  FIXME.  */
    478  1.1  mrg   if ((unsigned long) exponent == fmt->exp_nan)
    479  1.1  mrg     {
    480  1.1  mrg       int nan = mant_bits_set (fmt, ufrom);
    481  1.1  mrg 
    482  1.1  mrg       /* On certain systems (such as GNU/Linux), the use of the
    483  1.1  mrg 	 INFINITY macro below may generate a warning that can not be
    484  1.1  mrg 	 silenced due to a bug in GCC (PR preprocessor/11931).  The
    485  1.1  mrg 	 preprocessor fails to recognise the __extension__ keyword in
    486  1.1  mrg 	 conjunction with the GNU/C99 extension for hexadecimal
    487  1.1  mrg 	 floating point constants and will issue a warning when
    488  1.1  mrg 	 compiling with -pedantic.  */
    489  1.1  mrg       if (nan)
    490  1.1  mrg 	dto = NAN;
    491  1.2  mrg       else
    492  1.2  mrg #ifdef __vax__
    493  1.2  mrg 	dto = HUGE_VAL;
    494  1.1  mrg #else
    495  1.2  mrg 	dto = INFINITY;
    496  1.1  mrg #endif
    497  1.1  mrg 
    498  1.1  mrg       if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
    499  1.1  mrg 	dto = -dto;
    500  1.1  mrg 
    501  1.1  mrg       *to = dto;
    502  1.1  mrg 
    503  1.1  mrg       return;
    504  1.1  mrg     }
    505  1.1  mrg 
    506  1.1  mrg   mant_bits_left = fmt->man_len;
    507  1.1  mrg   mant_off = fmt->man_start;
    508  1.1  mrg   dto = 0.0;
    509  1.1  mrg 
    510  1.1  mrg   /* Build the result algebraically.  Might go infinite, underflow, etc;
    511  1.1  mrg      who cares. */
    512  1.3  mrg 
    513  1.3  mrg   /* For denorms use minimum exponent.  */
    514  1.3  mrg   if (exponent == 0)
    515  1.3  mrg     exponent = 1 - fmt->exp_bias;
    516  1.3  mrg   else
    517  1.3  mrg     {
    518  1.3  mrg       exponent -= fmt->exp_bias;
    519  1.3  mrg 
    520  1.3  mrg       /* If this format uses a hidden bit, explicitly add it in now.
    521  1.3  mrg 	 Otherwise, increment the exponent by one to account for the
    522  1.1  mrg 	 integer bit.  */
    523  1.1  mrg 
    524  1.1  mrg       if (fmt->intbit == floatformat_intbit_no)
    525  1.1  mrg 	dto = ldexp (1.0, exponent);
    526  1.1  mrg       else
    527  1.1  mrg 	exponent++;
    528  1.1  mrg     }
    529  1.1  mrg 
    530  1.1  mrg   while (mant_bits_left > 0)
    531  1.1  mrg     {
    532  1.1  mrg       mant_bits = min (mant_bits_left, 32);
    533  1.1  mrg 
    534  1.1  mrg       mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
    535  1.1  mrg 			 mant_off, mant_bits);
    536  1.3  mrg 
    537  1.3  mrg       dto += ldexp ((double) mant, exponent - mant_bits);
    538  1.1  mrg       exponent -= mant_bits;
    539  1.1  mrg       mant_off += mant_bits;
    540  1.1  mrg       mant_bits_left -= mant_bits;
    541  1.1  mrg     }
    542  1.1  mrg 
    543  1.1  mrg   /* Negate it if negative.  */
    544  1.1  mrg   if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
    545  1.1  mrg     dto = -dto;
    546  1.1  mrg   *to = dto;
    547  1.1  mrg }
    548  1.1  mrg 
    549  1.1  mrg static void put_field (unsigned char *, enum floatformat_byteorders,
    551  1.1  mrg                        unsigned int,
    552  1.1  mrg                        unsigned int,
    553  1.1  mrg                        unsigned int,
    554  1.1  mrg                        unsigned long);
    555  1.1  mrg 
    556  1.1  mrg /* Set a field which starts at START and is LEN bits long.  DATA and
    557  1.1  mrg    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
    558  1.1  mrg static void
    559  1.1  mrg put_field (unsigned char *data, enum floatformat_byteorders order,
    560  1.1  mrg            unsigned int total_len, unsigned int start, unsigned int len,
    561  1.1  mrg            unsigned long stuff_to_put)
    562  1.1  mrg {
    563  1.1  mrg   unsigned int cur_byte;
    564  1.1  mrg   int lo_bit, hi_bit;
    565  1.1  mrg   int nextbyte = (order == floatformat_little) ? 1 : -1;
    566  1.1  mrg 
    567  1.1  mrg   /* Start is in big-endian bit order!  Fix that first.  */
    568  1.1  mrg   start = total_len - (start + len);
    569  1.1  mrg 
    570  1.1  mrg   /* Start at the least significant part of the field.  */
    571  1.1  mrg   if (order == floatformat_little)
    572  1.1  mrg     cur_byte = start / FLOATFORMAT_CHAR_BIT;
    573  1.1  mrg   else
    574  1.1  mrg     cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
    575  1.1  mrg 
    576  1.1  mrg   lo_bit = start % FLOATFORMAT_CHAR_BIT;
    577  1.1  mrg   hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
    578  1.1  mrg 
    579  1.1  mrg   do
    580  1.1  mrg     {
    581  1.1  mrg       unsigned char *byte_ptr = data + cur_byte;
    582  1.1  mrg       unsigned int bits = hi_bit - lo_bit;
    583  1.1  mrg       unsigned int mask = ((1 << bits) - 1) << lo_bit;
    584  1.1  mrg       *byte_ptr = (*byte_ptr & ~mask) | ((stuff_to_put << lo_bit) & mask);
    585  1.1  mrg       stuff_to_put >>= bits;
    586  1.1  mrg       len -= bits;
    587  1.1  mrg       cur_byte += nextbyte;
    588  1.1  mrg       lo_bit = 0;
    589  1.1  mrg       hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
    590  1.1  mrg     }
    591  1.1  mrg   while (len != 0);
    592  1.1  mrg }
    593  1.1  mrg 
    594  1.1  mrg /* The converse: convert the double *FROM to an extended float
    595  1.1  mrg    and store where TO points.  Neither FROM nor TO have any alignment
    596  1.1  mrg    restrictions.  */
    597  1.1  mrg 
    598  1.1  mrg void
    599  1.1  mrg floatformat_from_double (const struct floatformat *fmt,
    600  1.1  mrg                          const double *from, void *to)
    601  1.1  mrg {
    602  1.1  mrg   double dfrom;
    603  1.1  mrg   int exponent;
    604  1.1  mrg   double mant;
    605  1.1  mrg   unsigned int mant_bits, mant_off;
    606  1.1  mrg   int mant_bits_left;
    607  1.1  mrg   unsigned char *uto = (unsigned char *) to;
    608  1.1  mrg 
    609  1.1  mrg   dfrom = *from;
    610  1.1  mrg   memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
    611  1.1  mrg 
    612  1.1  mrg   /* Split values are not handled specially, since a bottom half of
    613  1.1  mrg      zero is correct for any value representable as double (in the
    614  1.1  mrg      only supported case of split values).  */
    615  1.1  mrg 
    616  1.1  mrg   /* If negative, set the sign bit.  */
    617  1.1  mrg   if (dfrom < 0)
    618  1.1  mrg     {
    619  1.1  mrg       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
    620  1.1  mrg       dfrom = -dfrom;
    621  1.1  mrg     }
    622  1.1  mrg 
    623  1.1  mrg   if (dfrom == 0)
    624  1.1  mrg     {
    625  1.1  mrg       /* 0.0.  */
    626  1.1  mrg       return;
    627  1.1  mrg     }
    628  1.1  mrg 
    629  1.1  mrg   if (dfrom != dfrom)
    630  1.1  mrg     {
    631  1.1  mrg       /* NaN.  */
    632  1.1  mrg       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
    633  1.1  mrg 		 fmt->exp_len, fmt->exp_nan);
    634  1.1  mrg       /* Be sure it's not infinity, but NaN value is irrelevant.  */
    635  1.1  mrg       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
    636  1.1  mrg 		 32, 1);
    637  1.1  mrg       return;
    638  1.1  mrg     }
    639  1.1  mrg 
    640  1.1  mrg   if (dfrom + dfrom == dfrom)
    641  1.1  mrg     {
    642  1.1  mrg       /* This can only happen for an infinite value (or zero, which we
    643  1.1  mrg 	 already handled above).  */
    644  1.1  mrg       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
    645  1.1  mrg 		 fmt->exp_len, fmt->exp_nan);
    646  1.1  mrg       return;
    647  1.1  mrg     }
    648  1.1  mrg 
    649  1.1  mrg   mant = frexp (dfrom, &exponent);
    650  1.1  mrg   if (exponent + fmt->exp_bias - 1 > 0)
    651  1.1  mrg     put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
    652  1.1  mrg 	       fmt->exp_len, exponent + fmt->exp_bias - 1);
    653  1.1  mrg   else
    654  1.1  mrg     {
    655  1.1  mrg       /* Handle a denormalized number.  FIXME: What should we do for
    656  1.1  mrg 	 non-IEEE formats?  */
    657  1.1  mrg       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
    658  1.1  mrg 		 fmt->exp_len, 0);
    659  1.1  mrg       mant = ldexp (mant, exponent + fmt->exp_bias - 1);
    660  1.1  mrg     }
    661  1.1  mrg 
    662  1.1  mrg   mant_bits_left = fmt->man_len;
    663  1.1  mrg   mant_off = fmt->man_start;
    664  1.1  mrg   while (mant_bits_left > 0)
    665  1.1  mrg     {
    666  1.1  mrg       unsigned long mant_long;
    667  1.1  mrg       mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
    668  1.1  mrg 
    669  1.1  mrg       mant *= 4294967296.0;
    670  1.1  mrg       mant_long = (unsigned long)mant;
    671  1.1  mrg       mant -= mant_long;
    672  1.1  mrg 
    673  1.1  mrg       /* If the integer bit is implicit, and we are not creating a
    674  1.1  mrg 	 denormalized number, then we need to discard it.  */
    675  1.1  mrg       if ((unsigned int) mant_bits_left == fmt->man_len
    676  1.1  mrg 	  && fmt->intbit == floatformat_intbit_no
    677  1.1  mrg 	  && exponent + fmt->exp_bias - 1 > 0)
    678  1.1  mrg 	{
    679  1.1  mrg 	  mant_long &= 0x7fffffff;
    680  1.1  mrg 	  mant_bits -= 1;
    681  1.1  mrg 	}
    682  1.1  mrg       else if (mant_bits < 32)
    683  1.1  mrg 	{
    684  1.1  mrg 	  /* The bits we want are in the most significant MANT_BITS bits of
    685  1.1  mrg 	     mant_long.  Move them to the least significant.  */
    686  1.1  mrg 	  mant_long >>= 32 - mant_bits;
    687  1.1  mrg 	}
    688  1.1  mrg 
    689  1.1  mrg       put_field (uto, fmt->byteorder, fmt->totalsize,
    690  1.1  mrg 		 mant_off, mant_bits, mant_long);
    691  1.1  mrg       mant_off += mant_bits;
    692  1.1  mrg       mant_bits_left -= mant_bits;
    693  1.1  mrg     }
    694  1.1  mrg }
    695  1.1  mrg 
    696  1.1  mrg /* Return non-zero iff the data at FROM is a valid number in format FMT.  */
    697  1.1  mrg 
    698  1.1  mrg int
    699  1.1  mrg floatformat_is_valid (const struct floatformat *fmt, const void *from)
    700  1.1  mrg {
    701  1.1  mrg   return fmt->is_valid (fmt, from);
    702  1.1  mrg }
    703  1.1  mrg 
    704  1.1  mrg 
    705  1.1  mrg #ifdef IEEE_DEBUG
    706  1.1  mrg 
    707  1.1  mrg #include <stdio.h>
    708  1.1  mrg 
    709  1.1  mrg /* This is to be run on a host which uses IEEE floating point.  */
    710  1.1  mrg 
    711  1.1  mrg void
    712  1.1  mrg ieee_test (double n)
    713  1.1  mrg {
    714  1.1  mrg   double result;
    715  1.1  mrg 
    716  1.1  mrg   floatformat_to_double (&floatformat_ieee_double_little, &n, &result);
    717  1.1  mrg   if ((n != result && (! isnan (n) || ! isnan (result)))
    718  1.1  mrg       || (n < 0 && result >= 0)
    719  1.1  mrg       || (n >= 0 && result < 0))
    720  1.1  mrg     printf ("Differ(to): %.20g -> %.20g\n", n, result);
    721  1.1  mrg 
    722  1.1  mrg   floatformat_from_double (&floatformat_ieee_double_little, &n, &result);
    723  1.1  mrg   if ((n != result && (! isnan (n) || ! isnan (result)))
    724  1.1  mrg       || (n < 0 && result >= 0)
    725  1.1  mrg       || (n >= 0 && result < 0))
    726  1.1  mrg     printf ("Differ(from): %.20g -> %.20g\n", n, result);
    727  1.1  mrg 
    728  1.1  mrg #if 0
    729  1.1  mrg   {
    730  1.1  mrg     char exten[16];
    731  1.1  mrg 
    732  1.1  mrg     floatformat_from_double (&floatformat_m68881_ext, &n, exten);
    733  1.1  mrg     floatformat_to_double (&floatformat_m68881_ext, exten, &result);
    734  1.1  mrg     if (n != result)
    735  1.1  mrg       printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
    736  1.1  mrg   }
    737  1.1  mrg #endif
    738  1.1  mrg 
    739  1.1  mrg #if IEEE_DEBUG > 1
    740  1.1  mrg   /* This is to be run on a host which uses 68881 format.  */
    741  1.1  mrg   {
    742  1.1  mrg     long double ex = *(long double *)exten;
    743  1.1  mrg     if (ex != n)
    744  1.1  mrg       printf ("Differ(from vs. extended): %.20g\n", n);
    745  1.1  mrg   }
    746  1.1  mrg #endif
    747  1.1  mrg }
    748  1.1  mrg 
    749  1.1  mrg int
    750  1.1  mrg main (void)
    751  1.1  mrg {
    752  1.3  mrg   ieee_test (0.0);
    753  1.1  mrg   ieee_test (0.5);
    754  1.1  mrg   ieee_test (1.1);
    755  1.1  mrg   ieee_test (256.0);
    756  1.1  mrg   ieee_test (0.12345);
    757  1.1  mrg   ieee_test (234235.78907234);
    758  1.1  mrg   ieee_test (-512.0);
    759  1.1  mrg   ieee_test (-0.004321);
    760  1.1  mrg   ieee_test (1.2E-70);
    761  1.1  mrg   ieee_test (1.2E-316);
    762  1.1  mrg   ieee_test (4.9406564584124654E-324);
    763  1.1  mrg   ieee_test (- 4.9406564584124654E-324);
    764  1.1  mrg   ieee_test (- 0.0);
    765  1.1  mrg   ieee_test (- INFINITY);
    766  1.1  mrg   ieee_test (- NAN);
    767  1.1  mrg   ieee_test (INFINITY);
    768  1.1  mrg   ieee_test (NAN);
    769  1.1  mrg   return 0;
    770           }
    771           #endif
    772