/* atof_ieee.c - turn a Flonum into an IEEE floating point number
- Copyright (C) 1987, 1992 Free Software Foundation, Inc.
+ Copyright (C) 1987, 92, 93, 94, 95, 96, 97, 98, 1999
+ Free Software Foundation, Inc.
This file is part of GAS, the GNU Assembler.
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with GAS; see the file COPYING. If not, write to
- the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+ along with GAS; see the file COPYING. If not, write to the Free
+ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+ 02111-1307, USA. */
#include "as.h"
-extern FLONUM_TYPE generic_floating_point_number; /* Flonums returned here. */
+/* Flonums returned here. */
+extern FLONUM_TYPE generic_floating_point_number;
-#ifndef NULL
-#define NULL (0)
-#endif
+static int next_bits PARAMS ((int));
+static void unget_bits PARAMS ((int));
+static void make_invalid_floating_point_number PARAMS ((LITTLENUM_TYPE *));
extern const char EXP_CHARS[];
/* Precision in LittleNums. */
-#define MAX_PRECISION (6)
+/* Don't count the gap in the m68k extended precision format. */
+#define MAX_PRECISION (5)
#define F_PRECISION (2)
#define D_PRECISION (4)
-#define X_PRECISION (6)
-#define P_PRECISION (6)
+#define X_PRECISION (5)
+#define P_PRECISION (5)
/* Length in LittleNums of guard bits. */
#define GUARD (2)
-static unsigned long mask[] =
+static const unsigned long mask[] =
{
0x00000000,
0x00000001,
make_invalid_floating_point_number (words)
LITTLENUM_TYPE *words;
{
- as_bad ("cannot create floating-point number");
- words[0] = ((unsigned) -1) >> 1; /* Zero the leftmost bit */
- words[1] = -1;
- words[2] = -1;
- words[3] = -1;
- words[4] = -1;
- words[5] = -1;
+ as_bad (_("cannot create floating-point number"));
+ words[0] = (LITTLENUM_TYPE) ((unsigned) -1) >> 1; /* Zero the leftmost bit */
+ words[1] = (LITTLENUM_TYPE) -1;
+ words[2] = (LITTLENUM_TYPE) -1;
+ words[3] = (LITTLENUM_TYPE) -1;
+ words[4] = (LITTLENUM_TYPE) -1;
+ words[5] = (LITTLENUM_TYPE) -1;
}
\f
-/***********************************************************************\
+/************************************************************************\
* Warning: this returns 16-bit LITTLENUMs. It is up to the caller *
* to figure out any alignment problems and to conspire for the *
* bytes/word to be emitted in the right order. Bigendians beware! *
* *
- \***********************************************************************/
+\************************************************************************/
/* Note that atof-ieee always has X and P precisions enabled. it is up
to md_atof to filter them out if the target machine does not support
them. */
-char * /* Return pointer past text consumed. */
+/* Returns pointer past text consumed. */
+char *
atof_ieee (str, what_kind, words)
char *str; /* Text to convert to binary. */
- char what_kind; /* 'd', 'f', 'g', 'h' */
+ int what_kind; /* 'd', 'f', 'g', 'h' */
LITTLENUM_TYPE *words; /* Build the binary here. */
{
+ /* Extra bits for zeroed low-order bits. The 1st MAX_PRECISION are
+ zeroed, the last contain flonum bits. */
static LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
- /* Extra bits for zeroed low-order bits. */
- /* The 1st MAX_PRECISION are zeroed, */
- /* the last contain flonum bits. */
char *return_value;
- int precision; /* Number of 16-bit words in the format. */
+ /* Number of 16-bit words in the format. */
+ int precision;
long exponent_bits;
FLONUM_TYPE save_gen_flonum;
/* We have to save the generic_floating_point_number because it
- contains storage allocation about the array of LITTLENUMs
- where the value is actually stored. We will allocate our
- own array of littlenums below, but have to restore the global
- one on exit. */
+ contains storage allocation about the array of LITTLENUMs where
+ the value is actually stored. We will allocate our own array of
+ littlenums below, but have to restore the global one on exit. */
save_gen_flonum = generic_floating_point_number;
return_value = str;
generic_floating_point_number.low = bits + MAX_PRECISION;
generic_floating_point_number.high = NULL;
generic_floating_point_number.leader = NULL;
- generic_floating_point_number.exponent = NULL;
+ generic_floating_point_number.exponent = 0;
generic_floating_point_number.sign = '\0';
- /* Use more LittleNums than seems */
- /* necessary: the highest flonum may have */
- /* 15 leading 0 bits, so could be useless. */
+ /* Use more LittleNums than seems necessary: the highest flonum may
+ have 15 leading 0 bits, so could be useless. */
memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);
return (NULL);
}
- generic_floating_point_number.high = generic_floating_point_number.low + precision - 1 + GUARD;
+ generic_floating_point_number.high
+ = generic_floating_point_number.low + precision - 1 + GUARD;
- if (atof_generic (&return_value, ".", EXP_CHARS, &generic_floating_point_number))
+ if (atof_generic (&return_value, ".", EXP_CHARS,
+ &generic_floating_point_number))
{
- /* as_bad("Error converting floating point number (Exponent overflow?)"); */
make_invalid_floating_point_number (words);
return (NULL);
}
gen_to_words (words, precision, exponent_bits);
- /* Restore the generic_floating_point_number's storage alloc
- (and everything else). */
+ /* Restore the generic_floating_point_number's storage alloc (and
+ everything else). */
generic_floating_point_number = save_gen_flonum;
- return (return_value);
+ return return_value;
}
-/* Turn generic_floating_point_number into a real float/double/extended */
+/* Turn generic_floating_point_number into a real float/double/extended. */
int
gen_to_words (words, precision, exponent_bits)
LITTLENUM_TYPE *words;
int exponent_skippage;
LITTLENUM_TYPE word1;
LITTLENUM_TYPE *lp;
+ LITTLENUM_TYPE *words_end;
+
+ words_end = words + precision;
+#ifdef TC_M68K
+ if (precision == X_PRECISION)
+ /* On the m68k the extended precision format has a gap of 16 bits
+ between the exponent and the mantissa. */
+ words_end++;
+#endif
if (generic_floating_point_number.low > generic_floating_point_number.leader)
{
words[0] = 0x0000;
else
words[0] = 0x8000;
- memset (&words[1], '\0', sizeof (LITTLENUM_TYPE) * (precision - 1));
+ memset (&words[1], '\0',
+ (words_end - words - 1) * sizeof (LITTLENUM_TYPE));
return (return_value);
}
words[0] = 0x7fff;
words[1] = 0xffff;
}
+ else if (precision == X_PRECISION)
+ {
+#ifdef TC_M68K
+ words[0] = 0x7fff;
+ words[1] = 0;
+ words[2] = 0xffff;
+ words[3] = 0xffff;
+ words[4] = 0xffff;
+ words[5] = 0xffff;
+#else /* ! TC_M68K */
+#ifdef TC_I386
+ words[0] = 0xffff;
+ words[1] = 0xc000;
+ words[2] = 0;
+ words[3] = 0;
+ words[4] = 0;
+#else /* ! TC_I386 */
+ abort ();
+#endif /* ! TC_I386 */
+#endif /* ! TC_M68K */
+ }
else
{
words[0] = 0x7fff;
words[0] = 0x7f80;
words[1] = 0;
}
+ else if (precision == X_PRECISION)
+ {
+#ifdef TC_M68K
+ words[0] = 0x7fff;
+ words[1] = 0;
+ words[2] = 0;
+ words[3] = 0;
+ words[4] = 0;
+ words[5] = 0;
+#else /* ! TC_M68K */
+#ifdef TC_I386
+ words[0] = 0x7fff;
+ words[1] = 0x8000;
+ words[2] = 0;
+ words[3] = 0;
+ words[4] = 0;
+#else /* ! TC_I386 */
+ abort ();
+#endif /* ! TC_I386 */
+#endif /* ! TC_M68K */
+ }
else
{
words[0] = 0x7ff0;
words[0] = 0xff80;
words[1] = 0x0;
}
+ else if (precision == X_PRECISION)
+ {
+#ifdef TC_M68K
+ words[0] = 0xffff;
+ words[1] = 0;
+ words[2] = 0;
+ words[3] = 0;
+ words[4] = 0;
+ words[5] = 0;
+#else /* ! TC_M68K */
+#ifdef TC_I386
+ words[0] = 0xffff;
+ words[1] = 0x8000;
+ words[2] = 0;
+ words[3] = 0;
+ words[4] = 0;
+#else /* ! TC_I386 */
+ abort ();
+#endif /* ! TC_I386 */
+#endif /* ! TC_M68K */
+ }
else
{
words[0] = 0xfff0;
return (return_value);
}
/*
- * The floating point formats we support have:
- * Bit 15 is sign bit.
- * Bits 14:n are excess-whatever exponent.
- * Bits n-1:0 (if any) are most significant bits of fraction.
- * Bits 15:0 of the next word(s) are the next most significant bits.
- *
- * So we need: number of bits of exponent, number of bits of
- * mantissa.
- */
+ * The floating point formats we support have:
+ * Bit 15 is sign bit.
+ * Bits 14:n are excess-whatever exponent.
+ * Bits n-1:0 (if any) are most significant bits of fraction.
+ * Bits 15:0 of the next word(s) are the next most significant bits.
+ *
+ * So we need: number of bits of exponent, number of bits of
+ * mantissa.
+ */
bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
littlenum_pointer = generic_floating_point_number.leader;
- littlenums_left = 1 + generic_floating_point_number.leader - generic_floating_point_number.low;
+ littlenums_left = (1
+ + generic_floating_point_number.leader
+ - generic_floating_point_number.low);
/* Seek (and forget) 1st significant bit */
for (exponent_skippage = 0; !next_bits (1); ++exponent_skippage);;
- exponent_1 = generic_floating_point_number.exponent + generic_floating_point_number.leader
- + 1 - generic_floating_point_number.low;
- /* Radix LITTLENUM_RADIX, point just higher than generic_floating_point_number.leader. */
+ exponent_1 = (generic_floating_point_number.exponent
+ + generic_floating_point_number.leader
+ + 1
+ - generic_floating_point_number.low);
+ /* Radix LITTLENUM_RADIX, point just higher than
+ generic_floating_point_number.leader. */
exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
/* Radix 2. */
exponent_3 = exponent_2 - exponent_skippage;
lp = words;
/* Word 1. Sign, exponent and perhaps high bits. */
- word1 = (generic_floating_point_number.sign == '+') ? 0 : (1 << (LITTLENUM_NUMBER_OF_BITS - 1));
+ word1 = ((generic_floating_point_number.sign == '+')
+ ? 0
+ : (1 << (LITTLENUM_NUMBER_OF_BITS - 1)));
/* Assume 2's complement integers. */
- if (exponent_4 < 1 && exponent_4 >= -62)
+ if (exponent_4 <= 0)
{
int prec_bits;
int num_bits;
unget_bits (1);
num_bits = -exponent_4;
prec_bits = LITTLENUM_NUMBER_OF_BITS * precision - (exponent_bits + 1 + num_bits);
+#ifdef TC_I386
if (precision == X_PRECISION && exponent_bits == 15)
- prec_bits -= LITTLENUM_NUMBER_OF_BITS + 1;
+ {
+ /* On the i386 a denormalized extended precision float is
+ shifted down by one, effectively decreasing the exponent
+ bias by one. */
+ prec_bits -= 1;
+ num_bits += 1;
+ }
+#endif
if (num_bits >= LITTLENUM_NUMBER_OF_BITS - exponent_bits)
{
/* Bigger than one littlenum */
num_bits -= (LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits;
*lp++ = word1;
- if (num_bits + exponent_bits + 1 >= precision * LITTLENUM_NUMBER_OF_BITS)
+ if (num_bits + exponent_bits + 1 > precision * LITTLENUM_NUMBER_OF_BITS)
{
/* Exponent overflow */
make_invalid_floating_point_number (words);
return (return_value);
}
+#ifdef TC_M68K
if (precision == X_PRECISION && exponent_bits == 15)
- {
- *lp++ = 0;
- *lp++ = 0;
- num_bits -= LITTLENUM_NUMBER_OF_BITS - 1;
- }
+ *lp++ = 0;
+#endif
while (num_bits >= LITTLENUM_NUMBER_OF_BITS)
{
num_bits -= LITTLENUM_NUMBER_OF_BITS;
if (precision == X_PRECISION && exponent_bits == 15)
{
*lp++ = word1;
+#ifdef TC_M68K
*lp++ = 0;
- if (num_bits == LITTLENUM_NUMBER_OF_BITS)
- {
- *lp++ = 0;
- *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - 1);
- }
- else if (num_bits == LITTLENUM_NUMBER_OF_BITS - 1)
- *lp++ = 0;
- else
- *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - 1 - num_bits);
- num_bits = 0;
+#endif
+ *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - num_bits);
}
else
{
*lp++ = word1;
}
}
- while (lp < words + precision)
+ while (lp < words_end)
*lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
/* Round the mantissa up, but don't change the number */
if (next_bits (1))
{
--lp;
- if (prec_bits > LITTLENUM_NUMBER_OF_BITS)
+ if (prec_bits >= LITTLENUM_NUMBER_OF_BITS)
{
int n = 0;
int tmp_bits;
--n;
tmp_bits -= LITTLENUM_NUMBER_OF_BITS;
}
- if (tmp_bits > LITTLENUM_NUMBER_OF_BITS || (lp[n] & mask[tmp_bits]) != mask[tmp_bits])
+ if (tmp_bits > LITTLENUM_NUMBER_OF_BITS
+ || (lp[n] & mask[tmp_bits]) != mask[tmp_bits]
+ || (prec_bits != (precision * LITTLENUM_NUMBER_OF_BITS
+ - exponent_bits - 1)
+#ifdef TC_I386
+ /* An extended precision float with only the integer
+ bit set would be invalid. That must be converted
+ to the smallest normalized number. */
+ && !(precision == X_PRECISION
+ && prec_bits == (precision * LITTLENUM_NUMBER_OF_BITS
+ - exponent_bits - 2))
+#endif
+ ))
{
unsigned long carry;
carry >>= LITTLENUM_NUMBER_OF_BITS;
}
}
+ else
+ {
+ /* This is an overflow of the denormal numbers. We
+ need to forget what we have produced, and instead
+ generate the smallest normalized number. */
+ lp = words;
+ word1 = ((generic_floating_point_number.sign == '+')
+ ? 0
+ : (1 << (LITTLENUM_NUMBER_OF_BITS - 1)));
+ word1 |= (1
+ << ((LITTLENUM_NUMBER_OF_BITS - 1)
+ - exponent_bits));
+ *lp++ = word1;
+#ifdef TC_I386
+ /* Set the integer bit in the extended precision format.
+ This cannot happen on the m68k where the mantissa
+ just overflows into the integer bit above. */
+ if (precision == X_PRECISION)
+ *lp++ = 1 << (LITTLENUM_NUMBER_OF_BITS - 1);
+#endif
+ while (lp < words_end)
+ *lp++ = 0;
+ }
}
- else if ((*lp & mask[prec_bits]) != mask[prec_bits])
- lp++;
+ else
+ *lp += 1;
}
return return_value;
}
- else if (exponent_4 & ~mask[exponent_bits])
+ else if ((unsigned long) exponent_4 >= mask[exponent_bits])
{
/*
- * Exponent overflow. Lose immediately.
- */
+ * Exponent overflow. Lose immediately.
+ */
/*
- * We leave return_value alone: admit we read the
- * number, but return a floating exception
- * because we can't encode the number.
- */
+ * We leave return_value alone: admit we read the
+ * number, but return a floating exception
+ * because we can't encode the number.
+ */
make_invalid_floating_point_number (words);
return return_value;
}
*lp++ = word1;
- /* X_PRECISION is special: it has 16 bits of zero in the middle,
- followed by a 1 bit. */
+ /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the
+ middle. Either way, it is then followed by a 1 bit. */
if (exponent_bits == 15 && precision == X_PRECISION)
{
+#ifdef TC_M68K
*lp++ = 0;
- *lp++ = 1 << (LITTLENUM_NUMBER_OF_BITS) | next_bits (LITTLENUM_NUMBER_OF_BITS - 1);
+#endif
+ *lp++ = (1 << (LITTLENUM_NUMBER_OF_BITS - 1)
+ | next_bits (LITTLENUM_NUMBER_OF_BITS - 1));
}
/* The rest of the words are just mantissa bits. */
- while (lp < words + precision)
+ while (lp < words_end)
*lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
if (next_bits (1))
{
unsigned long carry;
/*
- * Since the NEXT bit is a 1, round UP the mantissa.
- * The cunning design of these hidden-1 floats permits
- * us to let the mantissa overflow into the exponent, and
- * it 'does the right thing'. However, we lose if the
- * highest-order bit of the lowest-order word flips.
- * Is that clear?
- */
+ * Since the NEXT bit is a 1, round UP the mantissa.
+ * The cunning design of these hidden-1 floats permits
+ * us to let the mantissa overflow into the exponent, and
+ * it 'does the right thing'. However, we lose if the
+ * highest-order bit of the lowest-order word flips.
+ * Is that clear?
+ */
/* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
- Please allow at least 1 more bit in carry than is in a LITTLENUM.
- We need that extra bit to hold a carry during a LITTLENUM carry
- propagation. Another extra bit (kept 0) will assure us that we
- don't get a sticky sign bit after shifting right, and that
- permits us to propagate the carry without any masking of bits.
- #endif */
+ Please allow at least 1 more bit in carry than is in a LITTLENUM.
+ We need that extra bit to hold a carry during a LITTLENUM carry
+ propagation. Another extra bit (kept 0) will assure us that we
+ don't get a sticky sign bit after shifting right, and that
+ permits us to propagate the carry without any masking of bits.
+ #endif */
for (carry = 1, lp--; carry && (lp >= words); lp--)
{
carry = *lp + carry;
*lp = carry;
carry >>= LITTLENUM_NUMBER_OF_BITS;
}
+ if (precision == X_PRECISION && exponent_bits == 15)
+ {
+ /* Extended precision numbers have an explicit integer bit
+ that we may have to restore. */
+ if (lp == words)
+ {
+#ifdef TC_M68K
+ /* On the m68k there is a gap of 16 bits. We must
+ explicitly propagate the carry into the exponent. */
+ words[0] += words[1];
+ words[1] = 0;
+ lp++;
+#endif
+ /* Put back the integer bit. */
+ lp[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS - 1);
+ }
+ }
if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
{
/* We leave return_value alone: admit we read the
- * number, but return a floating exception
- * because we can't encode the number.
- */
+ * number, but return a floating exception
+ * because we can't encode the number.
+ */
*words &= ~(1 << (LITTLENUM_NUMBER_OF_BITS - 1));
/* make_invalid_floating_point_number (words); */
/* return return_value; */
return (return_value);
}
-/* This routine is a real kludge. Someone really should do it better, but
- I'm too lazy, and I don't understand this stuff all too well anyway
- (JF)
- */
-void
+#if 0 /* unused */
+/* This routine is a real kludge. Someone really should do it better,
+ but I'm too lazy, and I don't understand this stuff all too well
+ anyway. (JF) */
+static void
int_to_gen (x)
long x;
{
sprintf (buf, "%ld", x);
bufp = &buf[0];
if (atof_generic (&bufp, ".", EXP_CHARS, &generic_floating_point_number))
- as_bad ("Error converting number to floating point (Exponent overflow?)");
+ as_bad (_("Error converting number to floating point (Exponent overflow?)"));
}
+#endif
#ifdef TEST
char *