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