gas: run the hwcaps-bump tests with 64-bit sparc objects only.
[deliverable/binutils-gdb.git] / libiberty / floatformat.c
index 28c9fbf662dbae73270c3b900f7fbe5bd1a7256b..9190e1469d9fdf4587a5ed600197489a684148d9 100644 (file)
@@ -1,5 +1,5 @@
 /* IEEE floating point support routines, for GDB, the GNU Debugger.
 /* IEEE floating point support routines, for GDB, the GNU Debugger.
-   Copyright 1991, 1994, 1999, 2000, 2003, 2005
+   Copyright 1991, 1994, 1999, 2000, 2003, 2005, 2006, 2010, 2012, 2015
    Free Software Foundation, Inc.
 
 This file is part of GDB.
    Free Software Foundation, Inc.
 
 This file is part of GDB.
@@ -19,7 +19,9 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /* This is needed to pick up the NAN macro on some systems.  */
 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 /* This is needed to pick up the NAN macro on some systems.  */
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -31,6 +33,11 @@ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 #include <string.h>
 #endif
 
 #include <string.h>
 #endif
 
+/* On some platforms, <float.h> provides DBL_QNAN.  */
+#ifdef STDC_HEADERS
+#include <float.h>
+#endif
+
 #include "ansidecl.h"
 #include "libiberty.h"
 #include "floatformat.h"
 #include "ansidecl.h"
 #include "libiberty.h"
 #include "floatformat.h"
@@ -44,9 +51,14 @@ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 #endif
 
 #ifndef NAN
 #endif
 
 #ifndef NAN
+#ifdef DBL_QNAN
+#define NAN DBL_QNAN
+#else
 #define NAN (0.0 / 0.0)
 #endif
 #define NAN (0.0 / 0.0)
 #endif
+#endif
 
 
+static int mant_bits_set (const struct floatformat *, const unsigned char *);
 static unsigned long get_field (const unsigned char *,
                                 enum floatformat_byteorders,
                                 unsigned int,
 static unsigned long get_field (const unsigned char *,
                                 enum floatformat_byteorders,
                                 unsigned int,
@@ -67,34 +79,54 @@ floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
    a system header, what we do if not, etc.  */
 #define FLOATFORMAT_CHAR_BIT 8
 
    a system header, what we do if not, etc.  */
 #define FLOATFORMAT_CHAR_BIT 8
 
-/* floatformats for IEEE single and double, big and little endian.  */
+/* floatformats for IEEE half, single and double, big and little endian.  */
+const struct floatformat floatformat_ieee_half_big =
+{
+  floatformat_big, 16, 0, 1, 5, 15, 31, 6, 10,
+  floatformat_intbit_no,
+  "floatformat_ieee_half_big",
+  floatformat_always_valid,
+  NULL
+};
+const struct floatformat floatformat_ieee_half_little =
+{
+  floatformat_little, 16, 0, 1, 5, 15, 31, 6, 10,
+  floatformat_intbit_no,
+  "floatformat_ieee_half_little",
+  floatformat_always_valid,
+  NULL
+};
 const struct floatformat floatformat_ieee_single_big =
 {
   floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
   floatformat_intbit_no,
   "floatformat_ieee_single_big",
 const struct floatformat floatformat_ieee_single_big =
 {
   floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
   floatformat_intbit_no,
   "floatformat_ieee_single_big",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_ieee_single_little =
 {
   floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
   floatformat_intbit_no,
   "floatformat_ieee_single_little",
 };
 const struct floatformat floatformat_ieee_single_little =
 {
   floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
   floatformat_intbit_no,
   "floatformat_ieee_single_little",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_ieee_double_big =
 {
   floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
   floatformat_intbit_no,
   "floatformat_ieee_double_big",
 };
 const struct floatformat floatformat_ieee_double_big =
 {
   floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
   floatformat_intbit_no,
   "floatformat_ieee_double_big",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_ieee_double_little =
 {
   floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
   floatformat_intbit_no,
   "floatformat_ieee_double_little",
 };
 const struct floatformat floatformat_ieee_double_little =
 {
   floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
   floatformat_intbit_no,
   "floatformat_ieee_double_little",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 
 /* floatformat for IEEE double, little endian byte order, with big endian word
 };
 
 /* floatformat for IEEE double, little endian byte order, with big endian word
@@ -105,7 +137,8 @@ const struct floatformat floatformat_ieee_double_littlebyte_bigword =
   floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
   floatformat_intbit_no,
   "floatformat_ieee_double_littlebyte_bigword",
   floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
   floatformat_intbit_no,
   "floatformat_ieee_double_littlebyte_bigword",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 
 /* floatformat for VAX.  Not quite IEEE, but close enough.  */
 };
 
 /* floatformat for VAX.  Not quite IEEE, but close enough.  */
@@ -115,21 +148,24 @@ const struct floatformat floatformat_vax_f =
   floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
   floatformat_intbit_no,
   "floatformat_vax_f",
   floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
   floatformat_intbit_no,
   "floatformat_vax_f",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_vax_d =
 {
   floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
   floatformat_intbit_no,
   "floatformat_vax_d",
 };
 const struct floatformat floatformat_vax_d =
 {
   floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
   floatformat_intbit_no,
   "floatformat_vax_d",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_vax_g =
 {
   floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
   floatformat_intbit_no,
   "floatformat_vax_g",
 };
 const struct floatformat floatformat_vax_g =
 {
   floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
   floatformat_intbit_no,
   "floatformat_vax_g",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 
 static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
 };
 
 static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
@@ -143,7 +179,7 @@ floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
      nor ~0, the intbit must also be set.  Only if the exponent is
      zero can it be zero, and then it must be zero.  */
   unsigned long exponent, int_bit;
      nor ~0, the intbit must also be set.  Only if the exponent is
      zero can it be zero, and then it must be zero.  */
   unsigned long exponent, int_bit;
-  const unsigned char *ufrom = from;
+  const unsigned char *ufrom = (const unsigned char *) from;
 
   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                        fmt->exp_start, fmt->exp_len);
 
   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                        fmt->exp_start, fmt->exp_len);
@@ -161,7 +197,8 @@ const struct floatformat floatformat_i387_ext =
   floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
   floatformat_intbit_yes,
   "floatformat_i387_ext",
   floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
   floatformat_intbit_yes,
   "floatformat_i387_ext",
-  floatformat_i387_ext_is_valid
+  floatformat_i387_ext_is_valid,
+  NULL
 };
 const struct floatformat floatformat_m68881_ext =
 {
 };
 const struct floatformat floatformat_m68881_ext =
 {
@@ -169,7 +206,8 @@ const struct floatformat floatformat_m68881_ext =
   floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
   floatformat_intbit_yes,
   "floatformat_m68881_ext",
   floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
   floatformat_intbit_yes,
   "floatformat_m68881_ext",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_i960_ext =
 {
 };
 const struct floatformat floatformat_i960_ext =
 {
@@ -177,14 +215,16 @@ const struct floatformat floatformat_i960_ext =
   floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
   floatformat_intbit_yes,
   "floatformat_i960_ext",
   floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
   floatformat_intbit_yes,
   "floatformat_i960_ext",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_m88110_ext =
 {
   floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
   floatformat_intbit_yes,
   "floatformat_m88110_ext",
 };
 const struct floatformat floatformat_m88110_ext =
 {
   floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
   floatformat_intbit_yes,
   "floatformat_m88110_ext",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_m88110_harris_ext =
 {
 };
 const struct floatformat floatformat_m88110_harris_ext =
 {
@@ -193,7 +233,8 @@ const struct floatformat floatformat_m88110_harris_ext =
   floatformat_big,128, 0, 1, 11,  0x3ff,  0x7ff, 12, 52,
   floatformat_intbit_no,
   "floatformat_m88110_ext_harris",
   floatformat_big,128, 0, 1, 11,  0x3ff,  0x7ff, 12, 52,
   floatformat_intbit_no,
   "floatformat_m88110_ext_harris",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_arm_ext_big =
 {
 };
 const struct floatformat floatformat_arm_ext_big =
 {
@@ -201,7 +242,8 @@ const struct floatformat floatformat_arm_ext_big =
   floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
   floatformat_intbit_yes,
   "floatformat_arm_ext_big",
   floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
   floatformat_intbit_yes,
   "floatformat_arm_ext_big",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_arm_ext_littlebyte_bigword =
 {
 };
 const struct floatformat floatformat_arm_ext_littlebyte_bigword =
 {
@@ -209,84 +251,215 @@ const struct floatformat floatformat_arm_ext_littlebyte_bigword =
   floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
   floatformat_intbit_yes,
   "floatformat_arm_ext_littlebyte_bigword",
   floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
   floatformat_intbit_yes,
   "floatformat_arm_ext_littlebyte_bigword",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_ia64_spill_big =
 {
   floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
   floatformat_intbit_yes,
   "floatformat_ia64_spill_big",
 };
 const struct floatformat floatformat_ia64_spill_big =
 {
   floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
   floatformat_intbit_yes,
   "floatformat_ia64_spill_big",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_ia64_spill_little =
 {
   floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
   floatformat_intbit_yes,
   "floatformat_ia64_spill_little",
 };
 const struct floatformat floatformat_ia64_spill_little =
 {
   floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
   floatformat_intbit_yes,
   "floatformat_ia64_spill_little",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_ia64_quad_big =
 {
   floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
   floatformat_intbit_no,
   "floatformat_ia64_quad_big",
 };
 const struct floatformat floatformat_ia64_quad_big =
 {
   floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
   floatformat_intbit_no,
   "floatformat_ia64_quad_big",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
 };
 const struct floatformat floatformat_ia64_quad_little =
 {
   floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
   floatformat_intbit_no,
   "floatformat_ia64_quad_little",
 };
 const struct floatformat floatformat_ia64_quad_little =
 {
   floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
   floatformat_intbit_no,
   "floatformat_ia64_quad_little",
-  floatformat_always_valid
+  floatformat_always_valid,
+  NULL
+};
+
+static int
+floatformat_ibm_long_double_is_valid (const struct floatformat *fmt,
+                                     const void *from)
+{
+  const unsigned char *ufrom = (const unsigned char *) from;
+  const struct floatformat *hfmt = fmt->split_half;
+  long top_exp, bot_exp;
+  int top_nan = 0;
+
+  top_exp = get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
+                      hfmt->exp_start, hfmt->exp_len);
+  bot_exp = get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
+                      hfmt->exp_start, hfmt->exp_len);
+
+  if ((unsigned long) top_exp == hfmt->exp_nan)
+    top_nan = mant_bits_set (hfmt, ufrom);
+
+  /* A NaN is valid with any low part.  */
+  if (top_nan)
+    return 1;
+
+  /* An infinity, zero or denormal requires low part 0 (positive or
+     negative).  */
+  if ((unsigned long) top_exp == hfmt->exp_nan || top_exp == 0)
+    {
+      if (bot_exp != 0)
+       return 0;
+
+      return !mant_bits_set (hfmt, ufrom + 8);
+    }
+
+  /* The top part is now a finite normal value.  The long double value
+     is the sum of the two parts, and the top part must equal the
+     result of rounding the long double value to nearest double.  Thus
+     the bottom part must be <= 0.5ulp of the top part in absolute
+     value, and if it is < 0.5ulp then the long double is definitely
+     valid.  */
+  if (bot_exp < top_exp - 53)
+    return 1;
+  if (bot_exp > top_exp - 53 && bot_exp != 0)
+    return 0;
+  if (bot_exp == 0)
+    {
+      /* The bottom part is 0 or denormal.  Determine which, and if
+        denormal the first two set bits.  */
+      int first_bit = -1, second_bit = -1, cur_bit;
+      for (cur_bit = 0; (unsigned int) cur_bit < hfmt->man_len; cur_bit++)
+       if (get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
+                      hfmt->man_start + cur_bit, 1))
+         {
+           if (first_bit == -1)
+             first_bit = cur_bit;
+           else
+             {
+               second_bit = cur_bit;
+               break;
+             }
+         }
+      /* Bottom part 0 is OK.  */
+      if (first_bit == -1)
+       return 1;
+      /* The real exponent of the bottom part is -first_bit.  */
+      if (-first_bit < top_exp - 53)
+       return 1;
+      if (-first_bit > top_exp - 53)
+       return 0;
+      /* The bottom part is at least 0.5ulp of the top part.  For this
+        to be OK, the bottom part must be exactly 0.5ulp (i.e. no
+        more bits set) and the top part must have last bit 0.  */
+      if (second_bit != -1)
+       return 0;
+      return !get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
+                        hfmt->man_start + hfmt->man_len - 1, 1);
+    }
+  else
+    {
+      /* The bottom part is at least 0.5ulp of the top part.  For this
+        to be OK, it must be exactly 0.5ulp (i.e. no explicit bits
+        set) and the top part must have last bit 0.  */
+      if (get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
+                    hfmt->man_start + hfmt->man_len - 1, 1))
+       return 0;
+      return !mant_bits_set (hfmt, ufrom + 8);
+    }
+}
+
+const struct floatformat floatformat_ibm_long_double_big =
+{
+  floatformat_big, 128, 0, 1, 11, 1023, 2047, 12, 52,
+  floatformat_intbit_no,
+  "floatformat_ibm_long_double_big",
+  floatformat_ibm_long_double_is_valid,
+  &floatformat_ieee_double_big
+};
+
+const struct floatformat floatformat_ibm_long_double_little =
+{
+  floatformat_little, 128, 0, 1, 11, 1023, 2047, 12, 52,
+  floatformat_intbit_no,
+  "floatformat_ibm_long_double_little",
+  floatformat_ibm_long_double_is_valid,
+  &floatformat_ieee_double_little
 };
 \f
 };
 \f
+
+#ifndef min
+#define min(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
+/* Return 1 if any bits are explicitly set in the mantissa of UFROM,
+   format FMT, 0 otherwise.  */
+static int
+mant_bits_set (const struct floatformat *fmt, const unsigned char *ufrom)
+{
+  unsigned int mant_bits, mant_off;
+  int mant_bits_left;
+
+  mant_off = fmt->man_start;
+  mant_bits_left = fmt->man_len;
+  while (mant_bits_left > 0)
+    {
+      mant_bits = min (mant_bits_left, 32);
+
+      if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
+                    mant_off, mant_bits) != 0)
+       return 1;
+
+      mant_off += mant_bits;
+      mant_bits_left -= mant_bits;
+    }
+  return 0;
+}
+
 /* Extract a field which starts at START and is LEN bits long.  DATA and
    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
 static unsigned long
 get_field (const unsigned char *data, enum floatformat_byteorders order,
            unsigned int total_len, unsigned int start, unsigned int len)
 {
 /* Extract a field which starts at START and is LEN bits long.  DATA and
    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
 static unsigned long
 get_field (const unsigned char *data, enum floatformat_byteorders order,
            unsigned int total_len, unsigned int start, unsigned int len)
 {
-  unsigned long result;
+  unsigned long result = 0;
   unsigned int cur_byte;
   unsigned int cur_byte;
-  int cur_bitshift;
+  int lo_bit, hi_bit, cur_bitshift = 0;
+  int nextbyte = (order == floatformat_little) ? 1 : -1;
+
+  /* Start is in big-endian bit order!  Fix that first.  */
+  start = total_len - (start + len);
 
   /* Start at the least significant part of the field.  */
 
   /* Start at the least significant part of the field.  */
-  cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
-  if (order == floatformat_little)
-    cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
-  cur_bitshift =
-    ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
-  result = *(data + cur_byte) >> (-cur_bitshift);
-  cur_bitshift += FLOATFORMAT_CHAR_BIT;
   if (order == floatformat_little)
   if (order == floatformat_little)
-    ++cur_byte;
+    cur_byte = start / FLOATFORMAT_CHAR_BIT;
   else
   else
-    --cur_byte;
+    cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
 
 
-  /* Move towards the most significant part of the field.  */
-  while ((unsigned int) cur_bitshift < len)
+  lo_bit = start % FLOATFORMAT_CHAR_BIT;
+  hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
+  
+  do
     {
     {
-      if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
-       /* This is the last byte; zero out the bits which are not part of
-          this field.  */
-       result |=
-         (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
-           << cur_bitshift;
-      else
-       result |= *(data + cur_byte) << cur_bitshift;
-      cur_bitshift += FLOATFORMAT_CHAR_BIT;
-      if (order == floatformat_little)
-       ++cur_byte;
-      else
-       --cur_byte;
+      unsigned int shifted = *(data + cur_byte) >> lo_bit;
+      unsigned int bits = hi_bit - lo_bit;
+      unsigned int mask = (1 << bits) - 1;
+      result |= (shifted & mask) << cur_bitshift;
+      len -= bits;
+      cur_bitshift += bits;
+      cur_byte += nextbyte;
+      lo_bit = 0;
+      hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
     }
     }
+  while (len != 0);
+
   return result;
 }
   
   return result;
 }
   
-#ifndef min
-#define min(a, b) ((a) < (b) ? (a) : (b))
-#endif
-
 /* Convert from FMT to a double.
    FROM is the address of the extended float.
    Store the double in *TO.  */
 /* Convert from FMT to a double.
    FROM is the address of the extended float.
    Store the double in *TO.  */
@@ -295,13 +468,16 @@ void
 floatformat_to_double (const struct floatformat *fmt,
                        const void *from, double *to)
 {
 floatformat_to_double (const struct floatformat *fmt,
                        const void *from, double *to)
 {
-  const unsigned char *ufrom = from;
+  const unsigned char *ufrom = (const unsigned char *) from;
   double dto;
   long exponent;
   unsigned long mant;
   unsigned int mant_bits, mant_off;
   int mant_bits_left;
   double dto;
   long exponent;
   unsigned long mant;
   unsigned int mant_bits, mant_off;
   int mant_bits_left;
-  int special_exponent;                /* It's a NaN, denorm or zero */
+
+  /* Split values are not handled specially, since the top half has
+     the correctly rounded double value (in the only supported case of
+     split values).  */
 
   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                        fmt->exp_start, fmt->exp_len);
 
   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                        fmt->exp_start, fmt->exp_len);
@@ -311,26 +487,7 @@ floatformat_to_double (const struct floatformat *fmt,
      don't try to preserve the type of NaN.  FIXME.  */
   if ((unsigned long) exponent == fmt->exp_nan)
     {
      don't try to preserve the type of NaN.  FIXME.  */
   if ((unsigned long) exponent == fmt->exp_nan)
     {
-      int nan;
-
-      mant_off = fmt->man_start;
-      mant_bits_left = fmt->man_len;
-      nan = 0;
-      while (mant_bits_left > 0)
-       {
-         mant_bits = min (mant_bits_left, 32);
-
-         if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
-                        mant_off, mant_bits) != 0)
-           {
-             /* This is a NaN.  */
-             nan = 1;
-             break;
-           }
-
-         mant_off += mant_bits;
-         mant_bits_left -= mant_bits;
-       }
+      int nan = mant_bits_set (fmt, ufrom);
 
       /* On certain systems (such as GNU/Linux), the use of the
         INFINITY macro below may generate a warning that can not be
 
       /* On certain systems (such as GNU/Linux), the use of the
         INFINITY macro below may generate a warning that can not be
@@ -356,20 +513,20 @@ floatformat_to_double (const struct floatformat *fmt,
   mant_off = fmt->man_start;
   dto = 0.0;
 
   mant_off = fmt->man_start;
   dto = 0.0;
 
-  special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
-
-  /* Don't bias zero's, denorms or NaNs.  */
-  if (!special_exponent)
-    exponent -= fmt->exp_bias;
-
   /* Build the result algebraically.  Might go infinite, underflow, etc;
      who cares. */
 
   /* Build the result algebraically.  Might go infinite, underflow, etc;
      who cares. */
 
-  /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
-     increment the exponent by one to account for the integer bit.  */
-
-  if (!special_exponent)
+  /* For denorms use minimum exponent.  */
+  if (exponent == 0)
+    exponent = 1 - fmt->exp_bias;
+  else
     {
     {
+      exponent -= fmt->exp_bias;
+
+      /* If this format uses a hidden bit, explicitly add it in now.
+        Otherwise, increment the exponent by one to account for the
+        integer bit.  */
+
       if (fmt->intbit == floatformat_intbit_no)
        dto = ldexp (1.0, exponent);
       else
       if (fmt->intbit == floatformat_intbit_no)
        dto = ldexp (1.0, exponent);
       else
@@ -383,18 +540,8 @@ floatformat_to_double (const struct floatformat *fmt,
       mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                         mant_off, mant_bits);
 
       mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                         mant_off, mant_bits);
 
-      /* Handle denormalized numbers.  FIXME: What should we do for
-        non-IEEE formats?  */
-      if (exponent == 0 && mant != 0)
-       dto += ldexp ((double)mant,
-                     (- fmt->exp_bias
-                      - mant_bits
-                      - (mant_off - fmt->man_start)
-                      + 1));
-      else
-       dto += ldexp ((double)mant, exponent - mant_bits);
-      if (exponent != 0)
-       exponent -= mant_bits;
+      dto += ldexp ((double) mant, exponent - mant_bits);
+      exponent -= mant_bits;
       mant_off += mant_bits;
       mant_bits_left -= mant_bits;
     }
       mant_off += mant_bits;
       mant_bits_left -= mant_bits;
     }
@@ -419,43 +566,34 @@ put_field (unsigned char *data, enum floatformat_byteorders order,
            unsigned long stuff_to_put)
 {
   unsigned int cur_byte;
            unsigned long stuff_to_put)
 {
   unsigned int cur_byte;
-  int cur_bitshift;
+  int lo_bit, hi_bit;
+  int nextbyte = (order == floatformat_little) ? 1 : -1;
+
+  /* Start is in big-endian bit order!  Fix that first.  */
+  start = total_len - (start + len);
 
   /* Start at the least significant part of the field.  */
 
   /* Start at the least significant part of the field.  */
-  cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
-  if (order == floatformat_little)
-    cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
-  cur_bitshift =
-    ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
-  *(data + cur_byte) &=
-    ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
-  *(data + cur_byte) |=
-    (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
-  cur_bitshift += FLOATFORMAT_CHAR_BIT;
   if (order == floatformat_little)
   if (order == floatformat_little)
-    ++cur_byte;
+    cur_byte = start / FLOATFORMAT_CHAR_BIT;
   else
   else
-    --cur_byte;
+    cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
 
 
-  /* Move towards the most significant part of the field.  */
-  while ((unsigned int) cur_bitshift < len)
+  lo_bit = start % FLOATFORMAT_CHAR_BIT;
+  hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
+  
+  do
     {
     {
-      if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
-       {
-         /* This is the last byte.  */
-         *(data + cur_byte) &=
-           ~((1 << (len - cur_bitshift)) - 1);
-         *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
-       }
-      else
-       *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
-                             & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
-      cur_bitshift += FLOATFORMAT_CHAR_BIT;
-      if (order == floatformat_little)
-       ++cur_byte;
-      else
-       --cur_byte;
+      unsigned char *byte_ptr = data + cur_byte;
+      unsigned int bits = hi_bit - lo_bit;
+      unsigned int mask = ((1 << bits) - 1) << lo_bit;
+      *byte_ptr = (*byte_ptr & ~mask) | ((stuff_to_put << lo_bit) & mask);
+      stuff_to_put >>= bits;
+      len -= bits;
+      cur_byte += nextbyte;
+      lo_bit = 0;
+      hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
     }
     }
+  while (len != 0);
 }
 
 /* The converse: convert the double *FROM to an extended float
 }
 
 /* The converse: convert the double *FROM to an extended float
@@ -471,11 +609,15 @@ floatformat_from_double (const struct floatformat *fmt,
   double mant;
   unsigned int mant_bits, mant_off;
   int mant_bits_left;
   double mant;
   unsigned int mant_bits, mant_off;
   int mant_bits_left;
-  unsigned char *uto = to;
+  unsigned char *uto = (unsigned char *) to;
 
   dfrom = *from;
   memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
 
 
   dfrom = *from;
   memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
 
+  /* Split values are not handled specially, since a bottom half of
+     zero is correct for any value representable as double (in the
+     only supported case of split values).  */
+
   /* If negative, set the sign bit.  */
   if (dfrom < 0)
     {
   /* If negative, set the sign bit.  */
   if (dfrom < 0)
     {
@@ -614,6 +756,7 @@ main (void)
 {
   ieee_test (0.0);
   ieee_test (0.5);
 {
   ieee_test (0.0);
   ieee_test (0.5);
+  ieee_test (1.1);
   ieee_test (256.0);
   ieee_test (0.12345);
   ieee_test (234235.78907234);
   ieee_test (256.0);
   ieee_test (0.12345);
   ieee_test (234235.78907234);
This page took 0.030305 seconds and 4 git commands to generate.