📄 atof-ieee.c
字号:
/* atof_ieee.c - turn a Flonum into an IEEE floating point number Copyright 1987, 1992, 1994, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. This file is part of GAS, the GNU Assembler. GAS is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GAS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* Some float formats are based on the IEEE standard, but use the largest exponent for normal numbers instead of NaNs and infinites. The macro TC_LARGEST_EXPONENT_IS_NORMAL should evaluate to true if the target machine uses such a format. The macro can depend on command line flags if necessary. There is no need to define the macro if it would always be 0. */#include "as.h"/* Flonums returned here. */extern FLONUM_TYPE generic_floating_point_number;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. *//* 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 (5)#define P_PRECISION (5)/* Length in LittleNums of guard bits. */#define GUARD (2)#ifndef TC_LARGEST_EXPONENT_IS_NORMAL#define TC_LARGEST_EXPONENT_IS_NORMAL 0#endifstatic const unsigned long mask[] ={ 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff, 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff, 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff, 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff,};static int bits_left_in_littlenum;static int littlenums_left;static LITTLENUM_TYPE *littlenum_pointer;static intnext_bits (number_of_bits) int number_of_bits;{ int return_value; if (!littlenums_left) return (0); if (number_of_bits >= bits_left_in_littlenum) { return_value = mask[bits_left_in_littlenum] & *littlenum_pointer; number_of_bits -= bits_left_in_littlenum; return_value <<= number_of_bits; if (--littlenums_left) { bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits; --littlenum_pointer; return_value |= (*littlenum_pointer >> bits_left_in_littlenum) & mask[number_of_bits]; } } else { bits_left_in_littlenum -= number_of_bits; return_value = mask[number_of_bits] & (*littlenum_pointer >> bits_left_in_littlenum); } return return_value;}/* Num had better be less than LITTLENUM_NUMBER_OF_BITS. */static voidunget_bits (num) int num;{ if (!littlenums_left) { ++littlenum_pointer; ++littlenums_left; bits_left_in_littlenum = num; } else if (bits_left_in_littlenum + num > LITTLENUM_NUMBER_OF_BITS) { bits_left_in_littlenum = num - (LITTLENUM_NUMBER_OF_BITS - bits_left_in_littlenum); ++littlenum_pointer; ++littlenums_left; } else bits_left_in_littlenum += num;}static voidmake_invalid_floating_point_number (words) LITTLENUM_TYPE *words;{ as_bad (_("cannot create floating-point number")); /* Zero the leftmost bit. */ words[0] = (LITTLENUM_TYPE) ((unsigned) -1) >> 1; 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;}/* 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. *//* Returns pointer past text consumed. */char *atof_ieee (str, what_kind, words) char *str; /* Text to convert to binary. */ 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]; char *return_value; /* 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. */ 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 = 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. */ memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION); switch (what_kind) { case 'f': case 'F': case 's': case 'S': precision = F_PRECISION; exponent_bits = 8; break; case 'd': case 'D': case 'r': case 'R': precision = D_PRECISION; exponent_bits = 11; break; case 'x': case 'X': case 'e': case 'E': precision = X_PRECISION; exponent_bits = 15; break; case 'p': case 'P': precision = P_PRECISION; exponent_bits = -1; break; default: make_invalid_floating_point_number (words); return (NULL); } generic_floating_point_number.high = generic_floating_point_number.low + precision - 1 + GUARD; if (atof_generic (&return_value, ".", EXP_CHARS, &generic_floating_point_number)) { 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). */ generic_floating_point_number = save_gen_flonum; return return_value;}/* Turn generic_floating_point_number into a real float/double/extended. */intgen_to_words (words, precision, exponent_bits) LITTLENUM_TYPE *words; int precision; long exponent_bits;{ int return_value = 0; long exponent_1; long exponent_2; long exponent_3; long exponent_4; 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) { /* 0.0e0 seen. */ if (generic_floating_point_number.sign == '+') words[0] = 0x0000; else words[0] = 0x8000; memset (&words[1], '\0', (words_end - words - 1) * sizeof (LITTLENUM_TYPE)); return return_value; } /* NaN: Do the right thing. */ if (generic_floating_point_number.sign == 0) { if (TC_LARGEST_EXPONENT_IS_NORMAL) as_warn ("NaNs are not supported by this target\n"); if (precision == F_PRECISION) { 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[1] = 0xffff; words[2] = 0xffff; words[3] = 0xffff; } return return_value; } else if (generic_floating_point_number.sign == 'P') { if (TC_LARGEST_EXPONENT_IS_NORMAL) as_warn ("Infinities are not supported by this target\n"); /* +INF: Do the right thing. */ if (precision == F_PRECISION) { 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 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -