X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=libiberty%2Ffloatformat.c;h=8f0d7894641702ab6fc04f51058f94ec8060f627;hb=7b96829ccc499ace9643b8e04ff531a4a0985c51;hp=1d8f16df3537795fcd8dc485de9c0804491701fb;hpb=ed288bb597072176e84fc8279707a3f2f475779b;p=deliverable%2Fbinutils-gdb.git diff --git a/libiberty/floatformat.c b/libiberty/floatformat.c index 1d8f16df35..8f0d789464 100644 --- a/libiberty/floatformat.c +++ b/libiberty/floatformat.c @@ -1,5 +1,6 @@ /* IEEE floating point support routines, for GDB, the GNU Debugger. - Copyright (C) 1991, 1994, 1999 Free Software Foundation, Inc. + Copyright 1991, 1994, 1999, 2000, 2003, 2005 + Free Software Foundation, Inc. This file is part of GDB. @@ -15,19 +16,52 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. */ +#define _GNU_SOURCE + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#ifdef HAVE_STRING_H +#include +#endif + +#include "ansidecl.h" +#include "libiberty.h" #include "floatformat.h" -#include /* ldexp */ -#ifdef __STDC__ -#include -extern void *memcpy (void *s1, const void *s2, size_t n); -extern void *memset (void *s, int c, size_t n); + +#ifndef INFINITY +#ifdef HUGE_VAL +#define INFINITY HUGE_VAL #else -extern char *memcpy (); -extern char *memset (); +#define INFINITY (1.0 / 0.0) +#endif #endif +#ifndef NAN +#define NAN (0.0 / 0.0) +#endif + +static unsigned long get_field (const unsigned char *, + enum floatformat_byteorders, + unsigned int, + unsigned int, + unsigned int); +static int floatformat_always_valid (const struct floatformat *fmt, + const void *from); + +static int +floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED, + const void *from ATTRIBUTE_UNUSED) +{ + return 1; +} + /* 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 a system header, what we do if not, etc. */ @@ -36,19 +70,31 @@ extern char *memset (); /* floatformats for IEEE single and double, big and little endian. */ const struct floatformat floatformat_ieee_single_big = { - floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23, floatformat_intbit_no + floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23, + floatformat_intbit_no, + "floatformat_ieee_single_big", + floatformat_always_valid }; const struct floatformat floatformat_ieee_single_little = { - floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23, floatformat_intbit_no + floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23, + floatformat_intbit_no, + "floatformat_ieee_single_little", + floatformat_always_valid }; const struct floatformat floatformat_ieee_double_big = { - floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52, floatformat_intbit_no + floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52, + floatformat_intbit_no, + "floatformat_ieee_double_big", + floatformat_always_valid }; const struct floatformat floatformat_ieee_double_little = { - floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52, floatformat_intbit_no + floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52, + floatformat_intbit_no, + "floatformat_ieee_double_little", + floatformat_always_valid }; /* floatformat for IEEE double, little endian byte order, with big endian word @@ -56,59 +102,125 @@ const struct floatformat floatformat_ieee_double_little = const struct floatformat floatformat_ieee_double_littlebyte_bigword = { - floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52, floatformat_intbit_no + floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52, + floatformat_intbit_no, + "floatformat_ieee_double_littlebyte_bigword", + floatformat_always_valid }; +static int floatformat_i387_ext_is_valid (const struct floatformat *fmt, + const void *from); + +static int +floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from) +{ + /* 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 + 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; + + exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize, + fmt->exp_start, fmt->exp_len); + int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize, + fmt->man_start, 1); + + if ((exponent == 0) != (int_bit == 0)) + return 0; + else + return 1; +} + const struct floatformat floatformat_i387_ext = { floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64, - floatformat_intbit_yes + floatformat_intbit_yes, + "floatformat_i387_ext", + floatformat_i387_ext_is_valid }; const struct floatformat floatformat_m68881_ext = { /* Note that the bits from 16 to 31 are unused. */ - floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64, floatformat_intbit_yes + floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64, + floatformat_intbit_yes, + "floatformat_m68881_ext", + floatformat_always_valid }; const struct floatformat floatformat_i960_ext = { /* Note that the bits from 0 to 15 are unused. */ floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64, - floatformat_intbit_yes + floatformat_intbit_yes, + "floatformat_i960_ext", + floatformat_always_valid }; const struct floatformat floatformat_m88110_ext = { -#ifdef HARRIS_FLOAT_FORMAT + floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64, + floatformat_intbit_yes, + "floatformat_m88110_ext", + floatformat_always_valid +}; +const struct floatformat floatformat_m88110_harris_ext = +{ /* Harris uses raw format 128 bytes long, but the number is just an ieee double, and the last 64 bits are wasted. */ floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52, - floatformat_intbit_no -#else - floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64, - floatformat_intbit_yes -#endif /* HARRIS_FLOAT_FORMAT */ + floatformat_intbit_no, + "floatformat_m88110_ext_harris", + floatformat_always_valid }; -const struct floatformat floatformat_arm_ext = +const struct floatformat floatformat_arm_ext_big = { /* Bits 1 to 16 are unused. */ floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64, - floatformat_intbit_yes + floatformat_intbit_yes, + "floatformat_arm_ext_big", + floatformat_always_valid +}; +const struct floatformat floatformat_arm_ext_littlebyte_bigword = +{ + /* Bits 1 to 16 are unused. */ + floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64, + floatformat_intbit_yes, + "floatformat_arm_ext_littlebyte_bigword", + floatformat_always_valid +}; +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 +}; +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 +}; +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 +}; +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 }; -static unsigned long get_field PARAMS ((unsigned char *, - enum floatformat_byteorders, - unsigned int, - unsigned int, - unsigned int)); - -/* Extract a field which starts at START and is LEN bytes 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. */ static unsigned long -get_field (data, order, total_len, start, len) - unsigned char *data; - enum floatformat_byteorders order; - unsigned int total_len; - unsigned int start; - unsigned int len; +get_field (const unsigned char *data, enum floatformat_byteorders order, + unsigned int total_len, unsigned int start, unsigned int len) { unsigned long result; unsigned int cur_byte; @@ -156,12 +268,10 @@ get_field (data, order, total_len, start, len) Store the double in *TO. */ void -floatformat_to_double (fmt, from, to) - const struct floatformat *fmt; - char *from; - double *to; +floatformat_to_double (const struct floatformat *fmt, + const void *from, double *to) { - unsigned char *ufrom = (unsigned char *)from; + const unsigned char *ufrom = from; double dto; long exponent; unsigned long mant; @@ -171,9 +281,52 @@ floatformat_to_double (fmt, from, to) exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len); - /* Note that if exponent indicates a NaN, we can't really do anything useful - (not knowing if the host has NaN's, or how to build one). So it will - end up as an infinity or something close; that is OK. */ + + /* 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 + 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; + } + + /* On certain systems (such as GNU/Linux), the use of the + INFINITY macro below may generate a warning that can not be + silenced due to a bug in GCC (PR preprocessor/11931). The + preprocessor fails to recognise the __extension__ keyword in + conjunction with the GNU/C99 extension for hexadecimal + floating point constants and will issue a warning when + compiling with -pedantic. */ + if (nan) + dto = NAN; + else + dto = INFINITY; + + if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1)) + dto = -dto; + + *to = dto; + + return; + } mant_bits_left = fmt->man_len; mant_off = fmt->man_start; @@ -206,8 +359,18 @@ floatformat_to_double (fmt, from, to) mant = get_field (ufrom, fmt->byteorder, fmt->totalsize, mant_off, mant_bits); - dto += ldexp ((double)mant, exponent - mant_bits); - exponent -= 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; mant_off += mant_bits; mant_bits_left -= mant_bits; } @@ -218,22 +381,18 @@ floatformat_to_double (fmt, from, to) *to = dto; } -static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders, - unsigned int, - unsigned int, - unsigned int, - unsigned long)); +static void put_field (unsigned char *, enum floatformat_byteorders, + unsigned int, + unsigned int, + unsigned int, + unsigned long); -/* Set a field which starts at START and is LEN bytes 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. */ static void -put_field (data, order, total_len, start, len, stuff_to_put) - unsigned char *data; - enum floatformat_byteorders order; - unsigned int total_len; - unsigned int start; - unsigned int len; - unsigned long stuff_to_put; +put_field (unsigned char *data, enum floatformat_byteorders order, + unsigned int total_len, unsigned int start, unsigned int len, + unsigned long stuff_to_put) { unsigned int cur_byte; int cur_bitshift; @@ -280,45 +439,64 @@ put_field (data, order, total_len, start, len, stuff_to_put) restrictions. */ void -floatformat_from_double (fmt, from, to) - const struct floatformat *fmt; - double *from; - char *to; +floatformat_from_double (const struct floatformat *fmt, + const double *from, void *to) { double dfrom; int exponent; double mant; unsigned int mant_bits, mant_off; int mant_bits_left; - unsigned char *uto = (unsigned char *)to; + unsigned char *uto = to; - memcpy (&dfrom, from, sizeof (dfrom)); + dfrom = *from; memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT); + + /* If negative, set the sign bit. */ + if (dfrom < 0) + { + put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1); + dfrom = -dfrom; + } + if (dfrom == 0) - return; /* Result is zero */ + { + /* 0.0. */ + return; + } + if (dfrom != dfrom) { - /* From is NaN */ + /* NaN. */ put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len, fmt->exp_nan); - /* Be sure it's not infinity, but NaN value is irrel */ + /* Be sure it's not infinity, but NaN value is irrelevant. */ put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start, 32, 1); return; } - /* If negative, set the sign bit. */ - if (dfrom < 0) + if (dfrom + dfrom == dfrom) { - put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1); - dfrom = -dfrom; + /* This can only happen for an infinite value (or zero, which we + already handled above). */ + put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, + fmt->exp_len, fmt->exp_nan); + return; } - /* How to tell an infinity from an ordinary number? FIXME-someday */ - mant = frexp (dfrom, &exponent); - put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len, - exponent + fmt->exp_bias - 1); + if (exponent + fmt->exp_bias - 1 > 0) + put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, + fmt->exp_len, exponent + fmt->exp_bias - 1); + else + { + /* Handle a denormalized number. FIXME: What should we do for + non-IEEE formats? */ + put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, + fmt->exp_len, 0); + mant = ldexp (mant, exponent + fmt->exp_bias - 1); + } mant_bits_left = fmt->man_len; mant_off = fmt->man_start; @@ -331,12 +509,11 @@ floatformat_from_double (fmt, from, to) mant_long = (unsigned long)mant; mant -= mant_long; - /* If the integer bit is implicit, then we need to discard it. - If we are discarding a zero, we should be (but are not) creating - a denormalized number which means adjusting the exponent - (I think). */ + /* If the integer bit is implicit, and we are not creating a + denormalized number, then we need to discard it. */ 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) { mant_long &= 0x7fffffff; mant_bits -= 1; @@ -355,29 +532,48 @@ floatformat_from_double (fmt, from, to) } } +/* Return non-zero iff the data at FROM is a valid number in format FMT. */ + +int +floatformat_is_valid (const struct floatformat *fmt, const void *from) +{ + return fmt->is_valid (fmt, from); +} + #ifdef IEEE_DEBUG +#include + /* This is to be run on a host which uses IEEE floating point. */ void -ieee_test (n) - double n; +ieee_test (double n) { double result; - char exten[16]; - floatformat_to_double (&floatformat_ieee_double_big, &n, &result); - if (n != result) + floatformat_to_double (&floatformat_ieee_double_little, &n, &result); + if ((n != result && (! isnan (n) || ! isnan (result))) + || (n < 0 && result >= 0) + || (n >= 0 && result < 0)) printf ("Differ(to): %.20g -> %.20g\n", n, result); - floatformat_from_double (&floatformat_ieee_double_big, &n, &result); - if (n != result) + + floatformat_from_double (&floatformat_ieee_double_little, &n, &result); + if ((n != result && (! isnan (n) || ! isnan (result))) + || (n < 0 && result >= 0) + || (n >= 0 && result < 0)) printf ("Differ(from): %.20g -> %.20g\n", n, result); - floatformat_from_double (&floatformat_m68881_ext, &n, exten); - floatformat_to_double (&floatformat_m68881_ext, exten, &result); - if (n != result) - printf ("Differ(to+from): %.20g -> %.20g\n", n, result); +#if 0 + { + char exten[16]; + + floatformat_from_double (&floatformat_m68881_ext, &n, exten); + floatformat_to_double (&floatformat_m68881_ext, exten, &result); + if (n != result) + printf ("Differ(to+from): %.20g -> %.20g\n", n, result); + } +#endif #if IEEE_DEBUG > 1 /* This is to be run on a host which uses 68881 format. */ @@ -390,14 +586,24 @@ ieee_test (n) } int -main () +main (void) { + ieee_test (0.0); ieee_test (0.5); ieee_test (256.0); ieee_test (0.12345); ieee_test (234235.78907234); ieee_test (-512.0); ieee_test (-0.004321); + ieee_test (1.2E-70); + ieee_test (1.2E-316); + ieee_test (4.9406564584124654E-324); + ieee_test (- 4.9406564584124654E-324); + ieee_test (- 0.0); + ieee_test (- INFINITY); + ieee_test (- NAN); + ieee_test (INFINITY); + ieee_test (NAN); return 0; } #endif