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