OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [libiberty/] [floatformat.c] - Diff between revs 154 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 154 Rev 816
/* 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, 2006
   Copyright 1991, 1994, 1999, 2000, 2003, 2005, 2006
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
 
 
This file is part of GDB.
This file is part of GDB.
 
 
This program is free software; you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
(at your option) any later version.
 
 
This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 
/* This is needed to pick up the NAN macro on some systems.  */
/* This is needed to pick up the NAN macro on some systems.  */
#define _GNU_SOURCE
#define _GNU_SOURCE
 
 
#ifdef HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#include "config.h"
#endif
#endif
 
 
#include <math.h>
#include <math.h>
 
 
#ifdef HAVE_STRING_H
#ifdef HAVE_STRING_H
#include <string.h>
#include <string.h>
#endif
#endif
 
 
/* On some platforms, <float.h> provides DBL_QNAN.  */
/* On some platforms, <float.h> provides DBL_QNAN.  */
#ifdef STDC_HEADERS
#ifdef STDC_HEADERS
#include <float.h>
#include <float.h>
#endif
#endif
 
 
#include "ansidecl.h"
#include "ansidecl.h"
#include "libiberty.h"
#include "libiberty.h"
#include "floatformat.h"
#include "floatformat.h"
 
 
#ifndef INFINITY
#ifndef INFINITY
#ifdef HUGE_VAL
#ifdef HUGE_VAL
#define INFINITY HUGE_VAL
#define INFINITY HUGE_VAL
#else
#else
#define INFINITY (1.0 / 0.0)
#define INFINITY (1.0 / 0.0)
#endif
#endif
#endif
#endif
 
 
#ifndef NAN
#ifndef NAN
#ifdef DBL_QNAN
#ifdef DBL_QNAN
#define NAN DBL_QNAN
#define NAN DBL_QNAN
#else
#else
#define NAN (0.0 / 0.0)
#define NAN (0.0 / 0.0)
#endif
#endif
#endif
#endif
 
 
static unsigned long get_field (const unsigned char *,
static unsigned long get_field (const unsigned char *,
                                enum floatformat_byteorders,
                                enum floatformat_byteorders,
                                unsigned int,
                                unsigned int,
                                unsigned int,
                                unsigned int,
                                unsigned int);
                                unsigned int);
static int floatformat_always_valid (const struct floatformat *fmt,
static int floatformat_always_valid (const struct floatformat *fmt,
                                     const void *from);
                                     const void *from);
 
 
static int
static int
floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
                          const void *from ATTRIBUTE_UNUSED)
                          const void *from ATTRIBUTE_UNUSED)
{
{
  return 1;
  return 1;
}
}
 
 
/* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
/* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
   going to bother with trying to muck around with whether it is defined in
   going to bother with trying to muck around with whether it is defined in
   a system header, what we do if not, etc.  */
   a system header, what we do if not, etc.  */
#define FLOATFORMAT_CHAR_BIT 8
#define FLOATFORMAT_CHAR_BIT 8
 
 
/* floatformats for IEEE single and double, big and little endian.  */
/* floatformats for IEEE single and double, big and little endian.  */
const struct floatformat floatformat_ieee_single_big =
const struct floatformat floatformat_ieee_single_big =
{
{
  floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
  floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_ieee_single_big",
  "floatformat_ieee_single_big",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_ieee_single_little =
const struct floatformat floatformat_ieee_single_little =
{
{
  floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
  floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_ieee_single_little",
  "floatformat_ieee_single_little",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_ieee_double_big =
const struct floatformat floatformat_ieee_double_big =
{
{
  floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
  floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_ieee_double_big",
  "floatformat_ieee_double_big",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_ieee_double_little =
const struct floatformat floatformat_ieee_double_little =
{
{
  floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
  floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_ieee_double_little",
  "floatformat_ieee_double_little",
  floatformat_always_valid
  floatformat_always_valid
};
};
 
 
/* floatformat for IEEE double, little endian byte order, with big endian word
/* floatformat for IEEE double, little endian byte order, with big endian word
   ordering, as on the ARM.  */
   ordering, as on the ARM.  */
 
 
const struct floatformat floatformat_ieee_double_littlebyte_bigword =
const struct floatformat floatformat_ieee_double_littlebyte_bigword =
{
{
  floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
  floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_ieee_double_littlebyte_bigword",
  "floatformat_ieee_double_littlebyte_bigword",
  floatformat_always_valid
  floatformat_always_valid
};
};
 
 
/* floatformat for VAX.  Not quite IEEE, but close enough.  */
/* floatformat for VAX.  Not quite IEEE, but close enough.  */
 
 
const struct floatformat floatformat_vax_f =
const struct floatformat floatformat_vax_f =
{
{
  floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
  floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_vax_f",
  "floatformat_vax_f",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_vax_d =
const struct floatformat floatformat_vax_d =
{
{
  floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
  floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_vax_d",
  "floatformat_vax_d",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_vax_g =
const struct floatformat floatformat_vax_g =
{
{
  floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
  floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_vax_g",
  "floatformat_vax_g",
  floatformat_always_valid
  floatformat_always_valid
};
};
 
 
static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
                                          const void *from);
                                          const void *from);
 
 
static int
static int
floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
{
{
  /* In the i387 double-extended format, if the exponent is all ones,
  /* In the i387 double-extended format, if the exponent is all ones,
     then the integer bit must be set.  If the exponent is neither 0
     then the integer bit must be set.  If the exponent is neither 0
     nor ~0, the intbit must also be set.  Only if the exponent is
     nor ~0, the intbit must also be set.  Only if the exponent is
     zero can it be zero, and then it must be zero.  */
     zero can it be zero, and then it must be zero.  */
  unsigned long exponent, int_bit;
  unsigned long exponent, int_bit;
  const unsigned char *ufrom = (const unsigned char *) from;
  const unsigned char *ufrom = (const unsigned char *) from;
 
 
  exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
  exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                        fmt->exp_start, fmt->exp_len);
                        fmt->exp_start, fmt->exp_len);
  int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
  int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                       fmt->man_start, 1);
                       fmt->man_start, 1);
 
 
  if ((exponent == 0) != (int_bit == 0))
  if ((exponent == 0) != (int_bit == 0))
    return 0;
    return 0;
  else
  else
    return 1;
    return 1;
}
}
 
 
const struct floatformat floatformat_i387_ext =
const struct floatformat floatformat_i387_ext =
{
{
  floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
  floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
  floatformat_intbit_yes,
  floatformat_intbit_yes,
  "floatformat_i387_ext",
  "floatformat_i387_ext",
  floatformat_i387_ext_is_valid
  floatformat_i387_ext_is_valid
};
};
const struct floatformat floatformat_m68881_ext =
const struct floatformat floatformat_m68881_ext =
{
{
  /* Note that the bits from 16 to 31 are unused.  */
  /* Note that the bits from 16 to 31 are unused.  */
  floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
  floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
  floatformat_intbit_yes,
  floatformat_intbit_yes,
  "floatformat_m68881_ext",
  "floatformat_m68881_ext",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_i960_ext =
const struct floatformat floatformat_i960_ext =
{
{
  /* Note that the bits from 0 to 15 are unused.  */
  /* Note that the bits from 0 to 15 are unused.  */
  floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
  floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
  floatformat_intbit_yes,
  floatformat_intbit_yes,
  "floatformat_i960_ext",
  "floatformat_i960_ext",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_m88110_ext =
const struct floatformat floatformat_m88110_ext =
{
{
  floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
  floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
  floatformat_intbit_yes,
  floatformat_intbit_yes,
  "floatformat_m88110_ext",
  "floatformat_m88110_ext",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_m88110_harris_ext =
const struct floatformat floatformat_m88110_harris_ext =
{
{
  /* Harris uses raw format 128 bytes long, but the number is just an ieee
  /* Harris uses raw format 128 bytes long, but the number is just an ieee
     double, and the last 64 bits are wasted. */
     double, and the last 64 bits are wasted. */
  floatformat_big,128, 0, 1, 11,  0x3ff,  0x7ff, 12, 52,
  floatformat_big,128, 0, 1, 11,  0x3ff,  0x7ff, 12, 52,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_m88110_ext_harris",
  "floatformat_m88110_ext_harris",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_arm_ext_big =
const struct floatformat floatformat_arm_ext_big =
{
{
  /* Bits 1 to 16 are unused.  */
  /* Bits 1 to 16 are unused.  */
  floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
  floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
  floatformat_intbit_yes,
  floatformat_intbit_yes,
  "floatformat_arm_ext_big",
  "floatformat_arm_ext_big",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_arm_ext_littlebyte_bigword =
const struct floatformat floatformat_arm_ext_littlebyte_bigword =
{
{
  /* Bits 1 to 16 are unused.  */
  /* Bits 1 to 16 are unused.  */
  floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
  floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
  floatformat_intbit_yes,
  floatformat_intbit_yes,
  "floatformat_arm_ext_littlebyte_bigword",
  "floatformat_arm_ext_littlebyte_bigword",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_ia64_spill_big =
const struct floatformat floatformat_ia64_spill_big =
{
{
  floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
  floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
  floatformat_intbit_yes,
  floatformat_intbit_yes,
  "floatformat_ia64_spill_big",
  "floatformat_ia64_spill_big",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_ia64_spill_little =
const struct floatformat floatformat_ia64_spill_little =
{
{
  floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
  floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
  floatformat_intbit_yes,
  floatformat_intbit_yes,
  "floatformat_ia64_spill_little",
  "floatformat_ia64_spill_little",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_ia64_quad_big =
const struct floatformat floatformat_ia64_quad_big =
{
{
  floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
  floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_ia64_quad_big",
  "floatformat_ia64_quad_big",
  floatformat_always_valid
  floatformat_always_valid
};
};
const struct floatformat floatformat_ia64_quad_little =
const struct floatformat floatformat_ia64_quad_little =
{
{
  floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
  floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
  floatformat_intbit_no,
  floatformat_intbit_no,
  "floatformat_ia64_quad_little",
  "floatformat_ia64_quad_little",
  floatformat_always_valid
  floatformat_always_valid
};
};


/* Extract a field which starts at START and is LEN bits long.  DATA and
/* 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.  */
   TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
static unsigned long
static unsigned long
get_field (const unsigned char *data, enum floatformat_byteorders order,
get_field (const unsigned char *data, enum floatformat_byteorders order,
           unsigned int total_len, unsigned int start, unsigned int len)
           unsigned int total_len, unsigned int start, unsigned int len)
{
{
  unsigned long result;
  unsigned long result;
  unsigned int cur_byte;
  unsigned int cur_byte;
  int cur_bitshift;
  int cur_bitshift;
 
 
  /* 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;
  cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
  if (order == floatformat_little)
  if (order == floatformat_little)
    cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
    cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
  cur_bitshift =
  cur_bitshift =
    ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
    ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
  result = *(data + cur_byte) >> (-cur_bitshift);
  result = *(data + cur_byte) >> (-cur_bitshift);
  cur_bitshift += FLOATFORMAT_CHAR_BIT;
  cur_bitshift += FLOATFORMAT_CHAR_BIT;
  if (order == floatformat_little)
  if (order == floatformat_little)
    ++cur_byte;
    ++cur_byte;
  else
  else
    --cur_byte;
    --cur_byte;
 
 
  /* Move towards the most significant part of the field.  */
  /* Move towards the most significant part of the field.  */
  while ((unsigned int) cur_bitshift < len)
  while ((unsigned int) cur_bitshift < len)
    {
    {
      if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
      if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
        /* This is the last byte; zero out the bits which are not part of
        /* This is the last byte; zero out the bits which are not part of
           this field.  */
           this field.  */
        result |=
        result |=
          (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
          (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
            << cur_bitshift;
            << cur_bitshift;
      else
      else
        result |= *(data + cur_byte) << cur_bitshift;
        result |= *(data + cur_byte) << cur_bitshift;
      cur_bitshift += FLOATFORMAT_CHAR_BIT;
      cur_bitshift += FLOATFORMAT_CHAR_BIT;
      if (order == floatformat_little)
      if (order == floatformat_little)
        ++cur_byte;
        ++cur_byte;
      else
      else
        --cur_byte;
        --cur_byte;
    }
    }
  return result;
  return result;
}
}
 
 
#ifndef min
#ifndef min
#define min(a, b) ((a) < (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif
#endif
 
 
/* Convert from FMT to a double.
/* Convert from FMT to a double.
   FROM is the address of the extended float.
   FROM is the address of the extended float.
   Store the double in *TO.  */
   Store the double in *TO.  */
 
 
void
void
floatformat_to_double (const struct floatformat *fmt,
floatformat_to_double (const struct floatformat *fmt,
                       const void *from, double *to)
                       const void *from, double *to)
{
{
  const unsigned char *ufrom = (const unsigned char *) from;
  const unsigned char *ufrom = (const unsigned char *) from;
  double dto;
  double dto;
  long exponent;
  long exponent;
  unsigned long mant;
  unsigned long mant;
  unsigned int mant_bits, mant_off;
  unsigned int mant_bits, mant_off;
  int mant_bits_left;
  int mant_bits_left;
  int special_exponent;         /* It's a NaN, denorm or zero */
  int special_exponent;         /* It's a NaN, denorm or zero */
 
 
  exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
  exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                        fmt->exp_start, fmt->exp_len);
                        fmt->exp_start, fmt->exp_len);
 
 
  /* If the exponent indicates a NaN, we don't have information to
  /* If the exponent indicates a NaN, we don't have information to
     decide what to do.  So we handle it like IEEE, except that we
     decide what to do.  So we handle it like IEEE, except that we
     don't try to preserve the type of NaN.  FIXME.  */
     don't try to preserve the type of NaN.  FIXME.  */
  if ((unsigned long) exponent == fmt->exp_nan)
  if ((unsigned long) exponent == fmt->exp_nan)
    {
    {
      int nan;
      int nan;
 
 
      mant_off = fmt->man_start;
      mant_off = fmt->man_start;
      mant_bits_left = fmt->man_len;
      mant_bits_left = fmt->man_len;
      nan = 0;
      nan = 0;
      while (mant_bits_left > 0)
      while (mant_bits_left > 0)
        {
        {
          mant_bits = min (mant_bits_left, 32);
          mant_bits = min (mant_bits_left, 32);
 
 
          if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
          if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
                         mant_off, mant_bits) != 0)
                         mant_off, mant_bits) != 0)
            {
            {
              /* This is a NaN.  */
              /* This is a NaN.  */
              nan = 1;
              nan = 1;
              break;
              break;
            }
            }
 
 
          mant_off += mant_bits;
          mant_off += mant_bits;
          mant_bits_left -= mant_bits;
          mant_bits_left -= mant_bits;
        }
        }
 
 
      /* On certain systems (such as GNU/Linux), the use of the
      /* On certain systems (such as GNU/Linux), the use of the
         INFINITY macro below may generate a warning that can not be
         INFINITY macro below may generate a warning that can not be
         silenced due to a bug in GCC (PR preprocessor/11931).  The
         silenced due to a bug in GCC (PR preprocessor/11931).  The
         preprocessor fails to recognise the __extension__ keyword in
         preprocessor fails to recognise the __extension__ keyword in
         conjunction with the GNU/C99 extension for hexadecimal
         conjunction with the GNU/C99 extension for hexadecimal
         floating point constants and will issue a warning when
         floating point constants and will issue a warning when
         compiling with -pedantic.  */
         compiling with -pedantic.  */
      if (nan)
      if (nan)
        dto = NAN;
        dto = NAN;
      else
      else
        dto = INFINITY;
        dto = INFINITY;
 
 
      if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
      if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
        dto = -dto;
        dto = -dto;
 
 
      *to = dto;
      *to = dto;
 
 
      return;
      return;
    }
    }
 
 
  mant_bits_left = fmt->man_len;
  mant_bits_left = fmt->man_len;
  mant_off = fmt->man_start;
  mant_off = fmt->man_start;
  dto = 0.0;
  dto = 0.0;
 
 
  special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
  special_exponent = exponent == 0 || (unsigned long) exponent == fmt->exp_nan;
 
 
  /* Don't bias zero's, denorms or NaNs.  */
  /* Don't bias zero's, denorms or NaNs.  */
  if (!special_exponent)
  if (!special_exponent)
    exponent -= fmt->exp_bias;
    exponent -= fmt->exp_bias;
 
 
  /* Build the result algebraically.  Might go infinite, underflow, etc;
  /* Build the result algebraically.  Might go infinite, underflow, etc;
     who cares. */
     who cares. */
 
 
  /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
  /* If this format uses a hidden bit, explicitly add it in now.  Otherwise,
     increment the exponent by one to account for the integer bit.  */
     increment the exponent by one to account for the integer bit.  */
 
 
  if (!special_exponent)
  if (!special_exponent)
    {
    {
      if (fmt->intbit == floatformat_intbit_no)
      if (fmt->intbit == floatformat_intbit_no)
        dto = ldexp (1.0, exponent);
        dto = ldexp (1.0, exponent);
      else
      else
        exponent++;
        exponent++;
    }
    }
 
 
  while (mant_bits_left > 0)
  while (mant_bits_left > 0)
    {
    {
      mant_bits = min (mant_bits_left, 32);
      mant_bits = min (mant_bits_left, 32);
 
 
      mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
      mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
                         mant_off, mant_bits);
                         mant_off, mant_bits);
 
 
      /* Handle denormalized numbers.  FIXME: What should we do for
      /* Handle denormalized numbers.  FIXME: What should we do for
         non-IEEE formats?  */
         non-IEEE formats?  */
      if (special_exponent && exponent == 0 && mant != 0)
      if (special_exponent && exponent == 0 && mant != 0)
        dto += ldexp ((double)mant,
        dto += ldexp ((double)mant,
                      (- fmt->exp_bias
                      (- fmt->exp_bias
                       - mant_bits
                       - mant_bits
                       - (mant_off - fmt->man_start)
                       - (mant_off - fmt->man_start)
                       + 1));
                       + 1));
      else
      else
        dto += ldexp ((double)mant, exponent - mant_bits);
        dto += ldexp ((double)mant, exponent - mant_bits);
      if (exponent != 0)
      if (exponent != 0)
        exponent -= mant_bits;
        exponent -= mant_bits;
      mant_off += mant_bits;
      mant_off += mant_bits;
      mant_bits_left -= mant_bits;
      mant_bits_left -= mant_bits;
    }
    }
 
 
  /* Negate it if negative.  */
  /* Negate it if negative.  */
  if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
  if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
    dto = -dto;
    dto = -dto;
  *to = dto;
  *to = dto;
}
}


static void put_field (unsigned char *, enum floatformat_byteorders,
static void put_field (unsigned char *, enum floatformat_byteorders,
                       unsigned int,
                       unsigned int,
                       unsigned int,
                       unsigned int,
                       unsigned int,
                       unsigned int,
                       unsigned long);
                       unsigned long);
 
 
/* Set a field which starts at START and is LEN bits long.  DATA and
/* Set 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.  */
   TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
static void
static void
put_field (unsigned char *data, enum floatformat_byteorders order,
put_field (unsigned char *data, enum floatformat_byteorders order,
           unsigned int total_len, unsigned int start, unsigned int len,
           unsigned int total_len, unsigned int start, unsigned int len,
           unsigned long stuff_to_put)
           unsigned long stuff_to_put)
{
{
  unsigned int cur_byte;
  unsigned int cur_byte;
  int cur_bitshift;
  int cur_bitshift;
 
 
  /* 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;
  cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
  if (order == floatformat_little)
  if (order == floatformat_little)
    cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
    cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
  cur_bitshift =
  cur_bitshift =
    ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
    ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
  *(data + cur_byte) &=
  *(data + cur_byte) &=
    ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
    ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
  *(data + cur_byte) |=
  *(data + cur_byte) |=
    (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
    (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
  cur_bitshift += FLOATFORMAT_CHAR_BIT;
  cur_bitshift += FLOATFORMAT_CHAR_BIT;
  if (order == floatformat_little)
  if (order == floatformat_little)
    ++cur_byte;
    ++cur_byte;
  else
  else
    --cur_byte;
    --cur_byte;
 
 
  /* Move towards the most significant part of the field.  */
  /* Move towards the most significant part of the field.  */
  while ((unsigned int) cur_bitshift < len)
  while ((unsigned int) cur_bitshift < len)
    {
    {
      if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
      if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
        {
        {
          /* This is the last byte.  */
          /* This is the last byte.  */
          *(data + cur_byte) &=
          *(data + cur_byte) &=
            ~((1 << (len - cur_bitshift)) - 1);
            ~((1 << (len - cur_bitshift)) - 1);
          *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
          *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
        }
        }
      else
      else
        *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
        *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
                              & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
                              & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
      cur_bitshift += FLOATFORMAT_CHAR_BIT;
      cur_bitshift += FLOATFORMAT_CHAR_BIT;
      if (order == floatformat_little)
      if (order == floatformat_little)
        ++cur_byte;
        ++cur_byte;
      else
      else
        --cur_byte;
        --cur_byte;
    }
    }
}
}
 
 
/* The converse: convert the double *FROM to an extended float
/* The converse: convert the double *FROM to an extended float
   and store where TO points.  Neither FROM nor TO have any alignment
   and store where TO points.  Neither FROM nor TO have any alignment
   restrictions.  */
   restrictions.  */
 
 
void
void
floatformat_from_double (const struct floatformat *fmt,
floatformat_from_double (const struct floatformat *fmt,
                         const double *from, void *to)
                         const double *from, void *to)
{
{
  double dfrom;
  double dfrom;
  int exponent;
  int exponent;
  double mant;
  double mant;
  unsigned int mant_bits, mant_off;
  unsigned int mant_bits, mant_off;
  int mant_bits_left;
  int mant_bits_left;
  unsigned char *uto = (unsigned char *) to;
  unsigned char *uto = (unsigned char *) to;
 
 
  dfrom = *from;
  dfrom = *from;
  memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
  memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
 
 
  /* If negative, set the sign bit.  */
  /* If negative, set the sign bit.  */
  if (dfrom < 0)
  if (dfrom < 0)
    {
    {
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
      dfrom = -dfrom;
      dfrom = -dfrom;
    }
    }
 
 
  if (dfrom == 0)
  if (dfrom == 0)
    {
    {
      /* 0.0.  */
      /* 0.0.  */
      return;
      return;
    }
    }
 
 
  if (dfrom != dfrom)
  if (dfrom != dfrom)
    {
    {
      /* NaN.  */
      /* NaN.  */
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
                 fmt->exp_len, fmt->exp_nan);
                 fmt->exp_len, fmt->exp_nan);
      /* Be sure it's not infinity, but NaN value is irrelevant.  */
      /* Be sure it's not infinity, but NaN value is irrelevant.  */
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
                 32, 1);
                 32, 1);
      return;
      return;
    }
    }
 
 
  if (dfrom + dfrom == dfrom)
  if (dfrom + dfrom == dfrom)
    {
    {
      /* This can only happen for an infinite value (or zero, which we
      /* This can only happen for an infinite value (or zero, which we
         already handled above).  */
         already handled above).  */
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
                 fmt->exp_len, fmt->exp_nan);
                 fmt->exp_len, fmt->exp_nan);
      return;
      return;
    }
    }
 
 
  mant = frexp (dfrom, &exponent);
  mant = frexp (dfrom, &exponent);
  if (exponent + fmt->exp_bias - 1 > 0)
  if (exponent + fmt->exp_bias - 1 > 0)
    put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
    put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
               fmt->exp_len, exponent + fmt->exp_bias - 1);
               fmt->exp_len, exponent + fmt->exp_bias - 1);
  else
  else
    {
    {
      /* Handle a denormalized number.  FIXME: What should we do for
      /* Handle a denormalized number.  FIXME: What should we do for
         non-IEEE formats?  */
         non-IEEE formats?  */
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
      put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
                 fmt->exp_len, 0);
                 fmt->exp_len, 0);
      mant = ldexp (mant, exponent + fmt->exp_bias - 1);
      mant = ldexp (mant, exponent + fmt->exp_bias - 1);
    }
    }
 
 
  mant_bits_left = fmt->man_len;
  mant_bits_left = fmt->man_len;
  mant_off = fmt->man_start;
  mant_off = fmt->man_start;
  while (mant_bits_left > 0)
  while (mant_bits_left > 0)
    {
    {
      unsigned long mant_long;
      unsigned long mant_long;
      mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
      mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
 
 
      mant *= 4294967296.0;
      mant *= 4294967296.0;
      mant_long = (unsigned long)mant;
      mant_long = (unsigned long)mant;
      mant -= mant_long;
      mant -= mant_long;
 
 
      /* If the integer bit is implicit, and we are not creating a
      /* If the integer bit is implicit, and we are not creating a
         denormalized number, then we need to discard it.  */
         denormalized number, then we need to discard it.  */
      if ((unsigned int) mant_bits_left == fmt->man_len
      if ((unsigned int) mant_bits_left == fmt->man_len
          && fmt->intbit == floatformat_intbit_no
          && fmt->intbit == floatformat_intbit_no
          && exponent + fmt->exp_bias - 1 > 0)
          && exponent + fmt->exp_bias - 1 > 0)
        {
        {
          mant_long &= 0x7fffffff;
          mant_long &= 0x7fffffff;
          mant_bits -= 1;
          mant_bits -= 1;
        }
        }
      else if (mant_bits < 32)
      else if (mant_bits < 32)
        {
        {
          /* The bits we want are in the most significant MANT_BITS bits of
          /* The bits we want are in the most significant MANT_BITS bits of
             mant_long.  Move them to the least significant.  */
             mant_long.  Move them to the least significant.  */
          mant_long >>= 32 - mant_bits;
          mant_long >>= 32 - mant_bits;
        }
        }
 
 
      put_field (uto, fmt->byteorder, fmt->totalsize,
      put_field (uto, fmt->byteorder, fmt->totalsize,
                 mant_off, mant_bits, mant_long);
                 mant_off, mant_bits, mant_long);
      mant_off += mant_bits;
      mant_off += mant_bits;
      mant_bits_left -= mant_bits;
      mant_bits_left -= mant_bits;
    }
    }
}
}
 
 
/* Return non-zero iff the data at FROM is a valid number in format FMT.  */
/* Return non-zero iff the data at FROM is a valid number in format FMT.  */
 
 
int
int
floatformat_is_valid (const struct floatformat *fmt, const void *from)
floatformat_is_valid (const struct floatformat *fmt, const void *from)
{
{
  return fmt->is_valid (fmt, from);
  return fmt->is_valid (fmt, from);
}
}
 
 
 
 
#ifdef IEEE_DEBUG
#ifdef IEEE_DEBUG
 
 
#include <stdio.h>
#include <stdio.h>
 
 
/* This is to be run on a host which uses IEEE floating point.  */
/* This is to be run on a host which uses IEEE floating point.  */
 
 
void
void
ieee_test (double n)
ieee_test (double n)
{
{
  double result;
  double result;
 
 
  floatformat_to_double (&floatformat_ieee_double_little, &n, &result);
  floatformat_to_double (&floatformat_ieee_double_little, &n, &result);
  if ((n != result && (! isnan (n) || ! isnan (result)))
  if ((n != result && (! isnan (n) || ! isnan (result)))
      || (n < 0 && result >= 0)
      || (n < 0 && result >= 0)
      || (n >= 0 && result < 0))
      || (n >= 0 && result < 0))
    printf ("Differ(to): %.20g -> %.20g\n", n, result);
    printf ("Differ(to): %.20g -> %.20g\n", n, result);
 
 
  floatformat_from_double (&floatformat_ieee_double_little, &n, &result);
  floatformat_from_double (&floatformat_ieee_double_little, &n, &result);
  if ((n != result && (! isnan (n) || ! isnan (result)))
  if ((n != result && (! isnan (n) || ! isnan (result)))
      || (n < 0 && result >= 0)
      || (n < 0 && result >= 0)
      || (n >= 0 && result < 0))
      || (n >= 0 && result < 0))
    printf ("Differ(from): %.20g -> %.20g\n", n, result);
    printf ("Differ(from): %.20g -> %.20g\n", n, result);
 
 
#if 0
#if 0
  {
  {
    char exten[16];
    char exten[16];
 
 
    floatformat_from_double (&floatformat_m68881_ext, &n, exten);
    floatformat_from_double (&floatformat_m68881_ext, &n, exten);
    floatformat_to_double (&floatformat_m68881_ext, exten, &result);
    floatformat_to_double (&floatformat_m68881_ext, exten, &result);
    if (n != result)
    if (n != result)
      printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
      printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
  }
  }
#endif
#endif
 
 
#if IEEE_DEBUG > 1
#if IEEE_DEBUG > 1
  /* This is to be run on a host which uses 68881 format.  */
  /* This is to be run on a host which uses 68881 format.  */
  {
  {
    long double ex = *(long double *)exten;
    long double ex = *(long double *)exten;
    if (ex != n)
    if (ex != n)
      printf ("Differ(from vs. extended): %.20g\n", n);
      printf ("Differ(from vs. extended): %.20g\n", n);
  }
  }
#endif
#endif
}
}
 
 
int
int
main (void)
main (void)
{
{
  ieee_test (0.0);
  ieee_test (0.0);
  ieee_test (0.5);
  ieee_test (0.5);
  ieee_test (256.0);
  ieee_test (256.0);
  ieee_test (0.12345);
  ieee_test (0.12345);
  ieee_test (234235.78907234);
  ieee_test (234235.78907234);
  ieee_test (-512.0);
  ieee_test (-512.0);
  ieee_test (-0.004321);
  ieee_test (-0.004321);
  ieee_test (1.2E-70);
  ieee_test (1.2E-70);
  ieee_test (1.2E-316);
  ieee_test (1.2E-316);
  ieee_test (4.9406564584124654E-324);
  ieee_test (4.9406564584124654E-324);
  ieee_test (- 4.9406564584124654E-324);
  ieee_test (- 4.9406564584124654E-324);
  ieee_test (- 0.0);
  ieee_test (- 0.0);
  ieee_test (- INFINITY);
  ieee_test (- INFINITY);
  ieee_test (- NAN);
  ieee_test (- NAN);
  ieee_test (INFINITY);
  ieee_test (INFINITY);
  ieee_test (NAN);
  ieee_test (NAN);
  return 0;
  return 0;
}
}
#endif
#endif
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.