Home | History | Annotate | Download | only in config
      1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number
      2    Copyright (C) 1987-2016 Free Software Foundation, Inc.
      3 
      4    This file is part of GAS, the GNU Assembler.
      5 
      6    GAS is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by
      8    the Free Software Foundation; either version 3, or (at your option)
      9    any later version.
     10 
     11    GAS is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14    GNU General Public License for more details.
     15 
     16    You should have received a copy of the GNU General Public License
     17    along with GAS; see the file COPYING.  If not, write to the Free
     18    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
     19    02110-1301, USA.  */
     20 
     21 #include "as.h"
     22 
     23 /* Flonums returned here.  */
     24 extern FLONUM_TYPE generic_floating_point_number;
     25 
     26 /* Precision in LittleNums.  */
     27 /* Don't count the gap in the m68k extended precision format.  */
     28 #define MAX_PRECISION  5
     29 #define F_PRECISION    2
     30 #define D_PRECISION    4
     31 #define X_PRECISION    5
     32 #define P_PRECISION    5
     33 
     34 /* Length in LittleNums of guard bits.  */
     35 #define GUARD          2
     36 
     37 #ifndef TC_LARGEST_EXPONENT_IS_NORMAL
     38 #define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) 0
     39 #endif
     40 
     41 static const unsigned long mask[] =
     42 {
     43   0x00000000,
     44   0x00000001,
     45   0x00000003,
     46   0x00000007,
     47   0x0000000f,
     48   0x0000001f,
     49   0x0000003f,
     50   0x0000007f,
     51   0x000000ff,
     52   0x000001ff,
     53   0x000003ff,
     54   0x000007ff,
     55   0x00000fff,
     56   0x00001fff,
     57   0x00003fff,
     58   0x00007fff,
     59   0x0000ffff,
     60   0x0001ffff,
     61   0x0003ffff,
     62   0x0007ffff,
     63   0x000fffff,
     64   0x001fffff,
     65   0x003fffff,
     66   0x007fffff,
     67   0x00ffffff,
     68   0x01ffffff,
     69   0x03ffffff,
     70   0x07ffffff,
     71   0x0fffffff,
     72   0x1fffffff,
     73   0x3fffffff,
     74   0x7fffffff,
     75   0xffffffff,
     76 };
     77 
     78 static int bits_left_in_littlenum;
     80 static int littlenums_left;
     81 static LITTLENUM_TYPE *littlenum_pointer;
     82 
     83 static int
     84 next_bits (int number_of_bits)
     85 {
     86   int return_value;
     87 
     88   if (!littlenums_left)
     89     return 0;
     90 
     91   if (number_of_bits >= bits_left_in_littlenum)
     92     {
     93       return_value = mask[bits_left_in_littlenum] & *littlenum_pointer;
     94       number_of_bits -= bits_left_in_littlenum;
     95       return_value <<= number_of_bits;
     96 
     97       if (--littlenums_left)
     98 	{
     99 	  bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits;
    100 	  --littlenum_pointer;
    101 	  return_value |=
    102 	    (*littlenum_pointer >> bits_left_in_littlenum)
    103 	    & mask[number_of_bits];
    104 	}
    105     }
    106   else
    107     {
    108       bits_left_in_littlenum -= number_of_bits;
    109       return_value =
    110 	mask[number_of_bits] & (*littlenum_pointer >> bits_left_in_littlenum);
    111     }
    112   return return_value;
    113 }
    114 
    115 /* Num had better be less than LITTLENUM_NUMBER_OF_BITS.  */
    116 
    117 static void
    118 unget_bits (int num)
    119 {
    120   if (!littlenums_left)
    121     {
    122       ++littlenum_pointer;
    123       ++littlenums_left;
    124       bits_left_in_littlenum = num;
    125     }
    126   else if (bits_left_in_littlenum + num > LITTLENUM_NUMBER_OF_BITS)
    127     {
    128       bits_left_in_littlenum =
    129 	num - (LITTLENUM_NUMBER_OF_BITS - bits_left_in_littlenum);
    130       ++littlenum_pointer;
    131       ++littlenums_left;
    132     }
    133   else
    134     bits_left_in_littlenum += num;
    135 }
    136 
    137 static void
    138 make_invalid_floating_point_number (LITTLENUM_TYPE *words)
    139 {
    140   as_bad (_("cannot create floating-point number"));
    141   /* Zero the leftmost bit.  */
    142   words[0] = (LITTLENUM_TYPE) ((unsigned) -1) >> 1;
    143   words[1] = (LITTLENUM_TYPE) -1;
    144   words[2] = (LITTLENUM_TYPE) -1;
    145   words[3] = (LITTLENUM_TYPE) -1;
    146   words[4] = (LITTLENUM_TYPE) -1;
    147   words[5] = (LITTLENUM_TYPE) -1;
    148 }
    149 
    150 /* Warning: This returns 16-bit LITTLENUMs.  It is up to the caller to
    152    figure out any alignment problems and to conspire for the
    153    bytes/word to be emitted in the right order.  Bigendians beware!  */
    154 
    155 /* Note that atof-ieee always has X and P precisions enabled.  it is up
    156    to md_atof to filter them out if the target machine does not support
    157    them.  */
    158 
    159 /* Returns pointer past text consumed.  */
    160 
    161 char *
    162 atof_ieee (char *str,			/* Text to convert to binary.  */
    163 	   int what_kind,		/* 'd', 'f', 'x', 'p'.  */
    164 	   LITTLENUM_TYPE *words)	/* Build the binary here.  */
    165 {
    166   /* Extra bits for zeroed low-order bits.
    167      The 1st MAX_PRECISION are zeroed, the last contain flonum bits.  */
    168   static LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD];
    169   char *return_value;
    170   /* Number of 16-bit words in the format.  */
    171   int precision;
    172   long exponent_bits;
    173   FLONUM_TYPE save_gen_flonum;
    174 
    175   /* We have to save the generic_floating_point_number because it
    176      contains storage allocation about the array of LITTLENUMs where
    177      the value is actually stored.  We will allocate our own array of
    178      littlenums below, but have to restore the global one on exit.  */
    179   save_gen_flonum = generic_floating_point_number;
    180 
    181   return_value = str;
    182   generic_floating_point_number.low = bits + MAX_PRECISION;
    183   generic_floating_point_number.high = NULL;
    184   generic_floating_point_number.leader = NULL;
    185   generic_floating_point_number.exponent = 0;
    186   generic_floating_point_number.sign = '\0';
    187 
    188   /* Use more LittleNums than seems necessary: the highest flonum may
    189      have 15 leading 0 bits, so could be useless.  */
    190 
    191   memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION);
    192 
    193   switch (what_kind)
    194     {
    195     case 'f':
    196     case 'F':
    197     case 's':
    198     case 'S':
    199       precision = F_PRECISION;
    200       exponent_bits = 8;
    201       break;
    202 
    203     case 'd':
    204     case 'D':
    205     case 'r':
    206     case 'R':
    207       precision = D_PRECISION;
    208       exponent_bits = 11;
    209       break;
    210 
    211     case 'x':
    212     case 'X':
    213     case 'e':
    214     case 'E':
    215       precision = X_PRECISION;
    216       exponent_bits = 15;
    217       break;
    218 
    219     case 'p':
    220     case 'P':
    221       precision = P_PRECISION;
    222       exponent_bits = -1;
    223       break;
    224 
    225     default:
    226       make_invalid_floating_point_number (words);
    227       return (NULL);
    228     }
    229 
    230   generic_floating_point_number.high
    231     = generic_floating_point_number.low + precision - 1 + GUARD;
    232 
    233   if (atof_generic (&return_value, ".", EXP_CHARS,
    234 		    &generic_floating_point_number))
    235     {
    236       make_invalid_floating_point_number (words);
    237       return NULL;
    238     }
    239   gen_to_words (words, precision, exponent_bits);
    240 
    241   /* Restore the generic_floating_point_number's storage alloc (and
    242      everything else).  */
    243   generic_floating_point_number = save_gen_flonum;
    244 
    245   return return_value;
    246 }
    247 
    248 /* Turn generic_floating_point_number into a real float/double/extended.  */
    249 
    250 int
    251 gen_to_words (LITTLENUM_TYPE *words, int precision, long exponent_bits)
    252 {
    253   int return_value = 0;
    254 
    255   long exponent_1;
    256   long exponent_2;
    257   long exponent_3;
    258   long exponent_4;
    259   int exponent_skippage;
    260   LITTLENUM_TYPE word1;
    261   LITTLENUM_TYPE *lp;
    262   LITTLENUM_TYPE *words_end;
    263 
    264   words_end = words + precision;
    265 #ifdef TC_M68K
    266   if (precision == X_PRECISION)
    267     /* On the m68k the extended precision format has a gap of 16 bits
    268        between the exponent and the mantissa.  */
    269     words_end++;
    270 #endif
    271 
    272   if (generic_floating_point_number.low > generic_floating_point_number.leader)
    273     {
    274       /* 0.0e0 seen.  */
    275       if (generic_floating_point_number.sign == '+')
    276 	words[0] = 0x0000;
    277       else
    278 	words[0] = 0x8000;
    279       memset (&words[1], '\0',
    280 	      (words_end - words - 1) * sizeof (LITTLENUM_TYPE));
    281       return return_value;
    282     }
    283 
    284   /* NaN:  Do the right thing.  */
    285   if (generic_floating_point_number.sign == 0)
    286     {
    287       if (TC_LARGEST_EXPONENT_IS_NORMAL (precision))
    288 	as_warn (_("NaNs are not supported by this target\n"));
    289       if (precision == F_PRECISION)
    290 	{
    291 	  words[0] = 0x7fff;
    292 	  words[1] = 0xffff;
    293 	}
    294       else if (precision == X_PRECISION)
    295 	{
    296 #ifdef TC_M68K
    297 	  words[0] = 0x7fff;
    298 	  words[1] = 0;
    299 	  words[2] = 0xffff;
    300 	  words[3] = 0xffff;
    301 	  words[4] = 0xffff;
    302 	  words[5] = 0xffff;
    303 #else /* ! TC_M68K  */
    304 #ifdef TC_I386
    305 	  words[0] = 0xffff;
    306 	  words[1] = 0xc000;
    307 	  words[2] = 0;
    308 	  words[3] = 0;
    309 	  words[4] = 0;
    310 #else /* ! TC_I386  */
    311 	  abort ();
    312 #endif /* ! TC_I386  */
    313 #endif /* ! TC_M68K  */
    314 	}
    315       else
    316 	{
    317 	  words[0] = 0x7fff;
    318 	  words[1] = 0xffff;
    319 	  words[2] = 0xffff;
    320 	  words[3] = 0xffff;
    321 	}
    322       return return_value;
    323     }
    324   else if (generic_floating_point_number.sign == 'P')
    325     {
    326       if (TC_LARGEST_EXPONENT_IS_NORMAL (precision))
    327 	as_warn (_("Infinities are not supported by this target\n"));
    328 
    329       /* +INF:  Do the right thing.  */
    330       if (precision == F_PRECISION)
    331 	{
    332 	  words[0] = 0x7f80;
    333 	  words[1] = 0;
    334 	}
    335       else if (precision == X_PRECISION)
    336 	{
    337 #ifdef TC_M68K
    338 	  words[0] = 0x7fff;
    339 	  words[1] = 0;
    340 	  words[2] = 0;
    341 	  words[3] = 0;
    342 	  words[4] = 0;
    343 	  words[5] = 0;
    344 #else /* ! TC_M68K  */
    345 #ifdef TC_I386
    346 	  words[0] = 0x7fff;
    347 	  words[1] = 0x8000;
    348 	  words[2] = 0;
    349 	  words[3] = 0;
    350 	  words[4] = 0;
    351 #else /* ! TC_I386  */
    352 	  abort ();
    353 #endif /* ! TC_I386  */
    354 #endif /* ! TC_M68K  */
    355 	}
    356       else
    357 	{
    358 	  words[0] = 0x7ff0;
    359 	  words[1] = 0;
    360 	  words[2] = 0;
    361 	  words[3] = 0;
    362 	}
    363       return return_value;
    364     }
    365   else if (generic_floating_point_number.sign == 'N')
    366     {
    367       if (TC_LARGEST_EXPONENT_IS_NORMAL (precision))
    368 	as_warn (_("Infinities are not supported by this target\n"));
    369 
    370       /* Negative INF.  */
    371       if (precision == F_PRECISION)
    372 	{
    373 	  words[0] = 0xff80;
    374 	  words[1] = 0x0;
    375 	}
    376       else if (precision == X_PRECISION)
    377 	{
    378 #ifdef TC_M68K
    379 	  words[0] = 0xffff;
    380 	  words[1] = 0;
    381 	  words[2] = 0;
    382 	  words[3] = 0;
    383 	  words[4] = 0;
    384 	  words[5] = 0;
    385 #else /* ! TC_M68K  */
    386 #ifdef TC_I386
    387 	  words[0] = 0xffff;
    388 	  words[1] = 0x8000;
    389 	  words[2] = 0;
    390 	  words[3] = 0;
    391 	  words[4] = 0;
    392 #else /* ! TC_I386  */
    393 	  abort ();
    394 #endif /* ! TC_I386  */
    395 #endif /* ! TC_M68K  */
    396 	}
    397       else
    398 	{
    399 	  words[0] = 0xfff0;
    400 	  words[1] = 0x0;
    401 	  words[2] = 0x0;
    402 	  words[3] = 0x0;
    403 	}
    404       return return_value;
    405     }
    406 
    407   /* The floating point formats we support have:
    408      Bit 15 is sign bit.
    409      Bits 14:n are excess-whatever exponent.
    410      Bits n-1:0 (if any) are most significant bits of fraction.
    411      Bits 15:0 of the next word(s) are the next most significant bits.
    412 
    413      So we need: number of bits of exponent, number of bits of
    414      mantissa.  */
    415   bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS;
    416   littlenum_pointer = generic_floating_point_number.leader;
    417   littlenums_left = (1
    418 		     + generic_floating_point_number.leader
    419 		     - generic_floating_point_number.low);
    420 
    421   /* Seek (and forget) 1st significant bit.  */
    422   for (exponent_skippage = 0; !next_bits (1); ++exponent_skippage);
    423   exponent_1 = (generic_floating_point_number.exponent
    424 		+ generic_floating_point_number.leader
    425 		+ 1
    426 		- generic_floating_point_number.low);
    427 
    428   /* Radix LITTLENUM_RADIX, point just higher than
    429      generic_floating_point_number.leader.  */
    430   exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS;
    431 
    432   /* Radix 2.  */
    433   exponent_3 = exponent_2 - exponent_skippage;
    434 
    435   /* Forget leading zeros, forget 1st bit.  */
    436   exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2);
    437 
    438   /* Offset exponent.  */
    439   lp = words;
    440 
    441   /* Word 1.  Sign, exponent and perhaps high bits.  */
    442   word1 = ((generic_floating_point_number.sign == '+')
    443 	   ? 0
    444 	   : (1 << (LITTLENUM_NUMBER_OF_BITS - 1)));
    445 
    446   /* Assume 2's complement integers.  */
    447   if (exponent_4 <= 0)
    448     {
    449       int prec_bits;
    450       int num_bits;
    451 
    452       unget_bits (1);
    453       num_bits = -exponent_4;
    454       prec_bits =
    455 	LITTLENUM_NUMBER_OF_BITS * precision - (exponent_bits + 1 + num_bits);
    456 #ifdef TC_I386
    457       if (precision == X_PRECISION && exponent_bits == 15)
    458 	{
    459 	  /* On the i386 a denormalized extended precision float is
    460 	     shifted down by one, effectively decreasing the exponent
    461 	     bias by one.  */
    462 	  prec_bits -= 1;
    463 	  num_bits += 1;
    464 	}
    465 #endif
    466 
    467       if (num_bits >= LITTLENUM_NUMBER_OF_BITS - exponent_bits)
    468 	{
    469 	  /* Bigger than one littlenum.  */
    470 	  num_bits -= (LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits;
    471 	  *lp++ = word1;
    472 	  if (num_bits + exponent_bits + 1
    473 	      > precision * LITTLENUM_NUMBER_OF_BITS)
    474 	    {
    475 	      /* Exponent overflow.  */
    476 	      make_invalid_floating_point_number (words);
    477 	      return return_value;
    478 	    }
    479 #ifdef TC_M68K
    480 	  if (precision == X_PRECISION && exponent_bits == 15)
    481 	    *lp++ = 0;
    482 #endif
    483 	  while (num_bits >= LITTLENUM_NUMBER_OF_BITS)
    484 	    {
    485 	      num_bits -= LITTLENUM_NUMBER_OF_BITS;
    486 	      *lp++ = 0;
    487 	    }
    488 	  if (num_bits)
    489 	    *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - (num_bits));
    490 	}
    491       else
    492 	{
    493 	  if (precision == X_PRECISION && exponent_bits == 15)
    494 	    {
    495 	      *lp++ = word1;
    496 #ifdef TC_M68K
    497 	      *lp++ = 0;
    498 #endif
    499 	      *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - num_bits);
    500 	    }
    501 	  else
    502 	    {
    503 	      word1 |= next_bits ((LITTLENUM_NUMBER_OF_BITS - 1)
    504 				  - (exponent_bits + num_bits));
    505 	      *lp++ = word1;
    506 	    }
    507 	}
    508       while (lp < words_end)
    509 	*lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
    510 
    511       /* Round the mantissa up, but don't change the number.  */
    512       if (next_bits (1))
    513 	{
    514 	  --lp;
    515 	  if (prec_bits >= LITTLENUM_NUMBER_OF_BITS)
    516 	    {
    517 	      int n = 0;
    518 	      int tmp_bits;
    519 
    520 	      n = 0;
    521 	      tmp_bits = prec_bits;
    522 	      while (tmp_bits > LITTLENUM_NUMBER_OF_BITS)
    523 		{
    524 		  if (lp[n] != (LITTLENUM_TYPE) - 1)
    525 		    break;
    526 		  --n;
    527 		  tmp_bits -= LITTLENUM_NUMBER_OF_BITS;
    528 		}
    529 	      if (tmp_bits > LITTLENUM_NUMBER_OF_BITS
    530 		  || (lp[n] & mask[tmp_bits]) != mask[tmp_bits]
    531 		  || (prec_bits != (precision * LITTLENUM_NUMBER_OF_BITS
    532 				    - exponent_bits - 1)
    533 #ifdef TC_I386
    534 		      /* An extended precision float with only the integer
    535 			 bit set would be invalid.  That must be converted
    536 			 to the smallest normalized number.  */
    537 		      && !(precision == X_PRECISION
    538 			   && prec_bits == (precision * LITTLENUM_NUMBER_OF_BITS
    539 					    - exponent_bits - 2))
    540 #endif
    541 		      ))
    542 		{
    543 		  unsigned long carry;
    544 
    545 		  for (carry = 1; carry && (lp >= words); lp--)
    546 		    {
    547 		      carry = *lp + carry;
    548 		      *lp = carry;
    549 		      carry >>= LITTLENUM_NUMBER_OF_BITS;
    550 		    }
    551 		}
    552 	      else
    553 		{
    554 		  /* This is an overflow of the denormal numbers.  We
    555                      need to forget what we have produced, and instead
    556                      generate the smallest normalized number.  */
    557 		  lp = words;
    558 		  word1 = ((generic_floating_point_number.sign == '+')
    559 			   ? 0
    560 			   : (1 << (LITTLENUM_NUMBER_OF_BITS - 1)));
    561 		  word1 |= (1
    562 			    << ((LITTLENUM_NUMBER_OF_BITS - 1)
    563 				- exponent_bits));
    564 		  *lp++ = word1;
    565 #ifdef TC_I386
    566 		  /* Set the integer bit in the extended precision format.
    567 		     This cannot happen on the m68k where the mantissa
    568 		     just overflows into the integer bit above.  */
    569 		  if (precision == X_PRECISION)
    570 		    *lp++ = 1 << (LITTLENUM_NUMBER_OF_BITS - 1);
    571 #endif
    572 		  while (lp < words_end)
    573 		    *lp++ = 0;
    574 		}
    575 	    }
    576 	  else
    577 	    *lp += 1;
    578 	}
    579 
    580       return return_value;
    581     }
    582   else if ((unsigned long) exponent_4 > mask[exponent_bits]
    583 	   || (! TC_LARGEST_EXPONENT_IS_NORMAL (precision)
    584 	       && (unsigned long) exponent_4 == mask[exponent_bits]))
    585     {
    586       /* Exponent overflow.  Lose immediately.  */
    587 
    588       /* We leave return_value alone: admit we read the
    589 	 number, but return a floating exception
    590 	 because we can't encode the number.  */
    591       make_invalid_floating_point_number (words);
    592       return return_value;
    593     }
    594   else
    595     {
    596       word1 |= (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits))
    597 	| next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits);
    598     }
    599 
    600   *lp++ = word1;
    601 
    602   /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the
    603      middle.  Either way, it is then followed by a 1 bit.  */
    604   if (exponent_bits == 15 && precision == X_PRECISION)
    605     {
    606 #ifdef TC_M68K
    607       *lp++ = 0;
    608 #endif
    609       *lp++ = (1 << (LITTLENUM_NUMBER_OF_BITS - 1)
    610 	       | next_bits (LITTLENUM_NUMBER_OF_BITS - 1));
    611     }
    612 
    613   /* The rest of the words are just mantissa bits.  */
    614   while (lp < words_end)
    615     *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS);
    616 
    617   if (next_bits (1))
    618     {
    619       unsigned long carry;
    620       /* Since the NEXT bit is a 1, round UP the mantissa.
    621 	 The cunning design of these hidden-1 floats permits
    622 	 us to let the mantissa overflow into the exponent, and
    623 	 it 'does the right thing'. However, we lose if the
    624 	 highest-order bit of the lowest-order word flips.
    625 	 Is that clear?  */
    626 
    627       /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
    628 	 Please allow at least 1 more bit in carry than is in a LITTLENUM.
    629 	 We need that extra bit to hold a carry during a LITTLENUM carry
    630 	 propagation. Another extra bit (kept 0) will assure us that we
    631 	 don't get a sticky sign bit after shifting right, and that
    632 	 permits us to propagate the carry without any masking of bits.
    633 	 #endif */
    634       for (carry = 1, lp--; carry; lp--)
    635 	{
    636 	  carry = *lp + carry;
    637 	  *lp = carry;
    638 	  carry >>= LITTLENUM_NUMBER_OF_BITS;
    639 	  if (lp == words)
    640 	    break;
    641 	}
    642       if (precision == X_PRECISION && exponent_bits == 15)
    643 	{
    644 	  /* Extended precision numbers have an explicit integer bit
    645 	     that we may have to restore.  */
    646 	  if (lp == words)
    647 	    {
    648 #ifdef TC_M68K
    649 	      /* On the m68k there is a gap of 16 bits.  We must
    650 		 explicitly propagate the carry into the exponent.  */
    651 	      words[0] += words[1];
    652 	      words[1] = 0;
    653 	      lp++;
    654 #endif
    655 	      /* Put back the integer bit.  */
    656 	      lp[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS - 1);
    657 	    }
    658 	}
    659       if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1)))
    660 	{
    661 	  /* We leave return_value alone: admit we read the number,
    662 	     but return a floating exception because we can't encode
    663 	     the number.  */
    664 	  *words &= ~(1 << (LITTLENUM_NUMBER_OF_BITS - 1));
    665 	}
    666     }
    667   return return_value;
    668 }
    669 
    670 #ifdef TEST
    671 char *
    672 print_gen (gen)
    673      FLONUM_TYPE *gen;
    674 {
    675   FLONUM_TYPE f;
    676   LITTLENUM_TYPE arr[10];
    677   double dv;
    678   float fv;
    679   static char sbuf[40];
    680 
    681   if (gen)
    682     {
    683       f = generic_floating_point_number;
    684       generic_floating_point_number = *gen;
    685     }
    686   gen_to_words (&arr[0], 4, 11);
    687   memcpy (&dv, &arr[0], sizeof (double));
    688   sprintf (sbuf, "%x %x %x %x %.14G   ", arr[0], arr[1], arr[2], arr[3], dv);
    689   gen_to_words (&arr[0], 2, 8);
    690   memcpy (&fv, &arr[0], sizeof (float));
    691   sprintf (sbuf + strlen (sbuf), "%x %x %.12g\n", arr[0], arr[1], fv);
    692 
    693   if (gen)
    694     generic_floating_point_number = f;
    695 
    696   return (sbuf);
    697 }
    698 #endif
    699 
    700 #define MAX_LITTLENUMS 6
    701 
    702 /* This is a utility function called from various tc-*.c files.  It
    703    is here in order to reduce code duplication.
    704 
    705    Turn a string at input_line_pointer into a floating point constant
    706    of type TYPE (a character found in the FLT_CHARS macro), and store
    707    it as LITTLENUMS in the bytes buffer LITP.  The number of chars
    708    emitted is stored in *SIZEP.  BIG_WORDIAN is TRUE if the littlenums
    709    should be emitted most significant littlenum first.
    710 
    711    An error message is returned, or a NULL pointer if everything went OK.  */
    712 
    713 const char *
    714 ieee_md_atof (int type,
    715 	      char *litP,
    716 	      int *sizeP,
    717 	      bfd_boolean big_wordian)
    718 {
    719   LITTLENUM_TYPE words[MAX_LITTLENUMS];
    720   LITTLENUM_TYPE *wordP;
    721   char *t;
    722   int prec = 0;
    723 
    724   if (strchr (FLT_CHARS, type) != NULL)
    725     {
    726       switch (type)
    727 	{
    728 	case 'f':
    729 	case 'F':
    730 	case 's':
    731 	case 'S':
    732 	  prec = F_PRECISION;
    733 	  break;
    734 
    735 	case 'd':
    736 	case 'D':
    737 	case 'r':
    738 	case 'R':
    739 	  prec = D_PRECISION;
    740 	  break;
    741 
    742 	case 't':
    743 	case 'T':
    744 	  prec = X_PRECISION;
    745 	  type = 'x';		/* This is what atof_ieee() understands.  */
    746 	  break;
    747 
    748 	case 'x':
    749 	case 'X':
    750 	case 'p':
    751 	case 'P':
    752 #ifdef TC_M68K
    753 	  /* Note: on the m68k there is a gap of 16 bits (one littlenum)
    754 	     between the exponent and mantissa.  Hence the precision is
    755 	     6 and not 5.  */
    756 	  prec = P_PRECISION + 1;
    757 #else
    758 	  prec = P_PRECISION;
    759 #endif
    760 	  break;
    761 
    762 	default:
    763 	  break;
    764 	}
    765     }
    766   /* The 'f' and 'd' types are always recognised, even if the target has
    767      not put them into the FLT_CHARS macro.  This is because the 'f' type
    768      can come from the .dc.s, .dcb.s, .float or .single pseudo-ops and the
    769      'd' type from the .dc.d, .dbc.d or .double pseudo-ops.
    770 
    771      The 'x' type is not implicitly recongised however, even though it can
    772      be generated by the .dc.x and .dbc.x pseudo-ops because not all targets
    773      can support floating point values that big.  ie the target has to
    774      explicitly allow them by putting them into FLT_CHARS.  */
    775   else if (type == 'f')
    776     prec = F_PRECISION;
    777   else if (type == 'd')
    778     prec = D_PRECISION;
    779 
    780   if (prec == 0)
    781     {
    782       *sizeP = 0;
    783       return _("Unrecognized or unsupported floating point constant");
    784     }
    785 
    786   gas_assert (prec <= MAX_LITTLENUMS);
    787 
    788   t = atof_ieee (input_line_pointer, type, words);
    789   if (t)
    790     input_line_pointer = t;
    791 
    792   *sizeP = prec * sizeof (LITTLENUM_TYPE);
    793 
    794   if (big_wordian)
    795     {
    796       for (wordP = words; prec --;)
    797 	{
    798 	  md_number_to_chars (litP, (valueT) (* wordP ++), sizeof (LITTLENUM_TYPE));
    799 	  litP += sizeof (LITTLENUM_TYPE);
    800 	}
    801     }
    802   else
    803     {
    804       for (wordP = words + prec; prec --;)
    805 	{
    806 	  md_number_to_chars (litP, (valueT) (* -- wordP), sizeof (LITTLENUM_TYPE));
    807 	  litP += sizeof (LITTLENUM_TYPE);
    808 	}
    809     }
    810 
    811   return NULL;
    812 }
    813