1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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
23 /* Flonums returned here. */
24 extern FLONUM_TYPE generic_floating_point_number
;
26 /* Precision in LittleNums. */
27 /* Don't count the gap in the m68k extended precision format. */
28 #define MAX_PRECISION 5
35 /* Length in LittleNums of guard bits. */
38 #ifndef TC_LARGEST_EXPONENT_IS_NORMAL
39 #define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) 0
42 static const unsigned long mask
[] =
79 static int bits_left_in_littlenum
;
80 static int littlenums_left
;
81 static LITTLENUM_TYPE
*littlenum_pointer
;
84 next_bits (int number_of_bits
)
91 if (number_of_bits
>= bits_left_in_littlenum
)
93 return_value
= mask
[bits_left_in_littlenum
] & *littlenum_pointer
;
94 number_of_bits
-= bits_left_in_littlenum
;
95 return_value
<<= number_of_bits
;
97 if (--littlenums_left
)
99 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
- number_of_bits
;
102 (*littlenum_pointer
>> bits_left_in_littlenum
)
103 & mask
[number_of_bits
];
108 bits_left_in_littlenum
-= number_of_bits
;
110 mask
[number_of_bits
] & (*littlenum_pointer
>> bits_left_in_littlenum
);
115 /* Num had better be less than LITTLENUM_NUMBER_OF_BITS. */
120 if (!littlenums_left
)
124 bits_left_in_littlenum
= num
;
126 else if (bits_left_in_littlenum
+ num
> LITTLENUM_NUMBER_OF_BITS
)
128 bits_left_in_littlenum
=
129 num
- (LITTLENUM_NUMBER_OF_BITS
- bits_left_in_littlenum
);
134 bits_left_in_littlenum
+= num
;
138 make_invalid_floating_point_number (LITTLENUM_TYPE
*words
)
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;
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.
156 If generic_float_info is not NULL then it will be set to contain generic
157 infomation about the parsed floating point number.
159 Returns pointer past text consumed. */
161 atof_ieee_detail (char * str
,
164 LITTLENUM_TYPE
* words
,
165 FLONUM_TYPE
* generic_float_info
)
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
];
172 /* Number of 16-bit words in the format. */
173 FLONUM_TYPE save_gen_flonum
;
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
;
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';
188 /* Use more LittleNums than seems necessary: the highest flonum may
189 have 15 leading 0 bits, so could be useless. */
191 memset (bits
, '\0', sizeof (LITTLENUM_TYPE
) * MAX_PRECISION
);
193 generic_floating_point_number
.high
194 = generic_floating_point_number
.low
+ precision
- 1 + GUARD
;
196 if (atof_generic (&return_value
, ".", EXP_CHARS
,
197 &generic_floating_point_number
))
199 make_invalid_floating_point_number (words
);
203 if (generic_float_info
)
204 *generic_float_info
= generic_floating_point_number
;
206 gen_to_words (words
, precision
, exponent_bits
);
208 /* Restore the generic_floating_point_number's storage alloc (and
210 generic_floating_point_number
= save_gen_flonum
;
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! */
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
223 /* Returns pointer past text consumed. */
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. */
236 precision
= H_PRECISION
;
244 precision
= F_PRECISION
;
252 precision
= D_PRECISION
;
260 precision
= X_PRECISION
;
266 precision
= P_PRECISION
;
271 make_invalid_floating_point_number (words
);
275 return atof_ieee_detail (str
, precision
, exponent_bits
, words
, NULL
);
278 /* Turn generic_floating_point_number into a real float/double/extended. */
281 gen_to_words (LITTLENUM_TYPE
*words
, int precision
, long exponent_bits
)
283 int return_value
= 0;
289 int exponent_skippage
;
290 LITTLENUM_TYPE word1
;
292 LITTLENUM_TYPE
*words_end
;
294 words_end
= words
+ precision
;
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. */
302 if (generic_floating_point_number
.low
> generic_floating_point_number
.leader
)
305 if (generic_floating_point_number
.sign
== '+')
309 memset (&words
[1], '\0',
310 (words_end
- words
- 1) * sizeof (LITTLENUM_TYPE
));
314 /* NaN: Do the right thing. */
315 if (generic_floating_point_number
.sign
== 0)
317 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
318 as_warn (_("NaNs are not supported by this target"));
320 if (precision
== H_PRECISION
)
324 else if (precision
== F_PRECISION
)
329 else if (precision
== X_PRECISION
)
338 #else /* ! TC_M68K */
345 #else /* ! TC_I386 */
347 #endif /* ! TC_I386 */
348 #endif /* ! TC_M68K */
359 else if (generic_floating_point_number
.sign
== 'P')
361 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
362 as_warn (_("Infinities are not supported by this target"));
364 /* +INF: Do the right thing. */
365 if (precision
== H_PRECISION
)
369 else if (precision
== F_PRECISION
)
374 else if (precision
== X_PRECISION
)
383 #else /* ! TC_M68K */
390 #else /* ! TC_I386 */
392 #endif /* ! TC_I386 */
393 #endif /* ! TC_M68K */
404 else if (generic_floating_point_number
.sign
== 'N')
406 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
407 as_warn (_("Infinities are not supported by this target"));
410 if (precision
== H_PRECISION
)
414 else if (precision
== F_PRECISION
)
419 else if (precision
== X_PRECISION
)
428 #else /* ! TC_M68K */
435 #else /* ! TC_I386 */
437 #endif /* ! TC_I386 */
438 #endif /* ! TC_M68K */
450 /* The floating point formats we support have:
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.
456 So we need: number of bits of exponent, number of bits of
458 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
;
459 littlenum_pointer
= generic_floating_point_number
.leader
;
461 + generic_floating_point_number
.leader
462 - generic_floating_point_number
.low
);
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
469 - generic_floating_point_number
.low
);
471 /* Radix LITTLENUM_RADIX, point just higher than
472 generic_floating_point_number.leader. */
473 exponent_2
= exponent_1
* LITTLENUM_NUMBER_OF_BITS
;
476 exponent_3
= exponent_2
- exponent_skippage
;
478 /* Forget leading zeros, forget 1st bit. */
479 exponent_4
= exponent_3
+ ((1 << (exponent_bits
- 1)) - 2);
481 /* Offset exponent. */
484 /* Word 1. Sign, exponent and perhaps high bits. */
485 word1
= ((generic_floating_point_number
.sign
== '+')
487 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
489 /* Assume 2's complement integers. */
496 num_bits
= -exponent_4
;
498 LITTLENUM_NUMBER_OF_BITS
* precision
- (exponent_bits
+ 1 + num_bits
);
500 if (precision
== X_PRECISION
&& exponent_bits
== 15)
502 /* On the i386 a denormalized extended precision float is
503 shifted down by one, effectively decreasing the exponent
510 if (num_bits
>= LITTLENUM_NUMBER_OF_BITS
- exponent_bits
)
512 /* Bigger than one littlenum. */
513 num_bits
-= (LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
;
515 if (num_bits
+ exponent_bits
+ 1
516 > precision
* LITTLENUM_NUMBER_OF_BITS
)
518 /* Exponent overflow. */
519 make_invalid_floating_point_number (words
);
523 if (precision
== X_PRECISION
&& exponent_bits
== 15)
526 while (num_bits
>= LITTLENUM_NUMBER_OF_BITS
)
528 num_bits
-= LITTLENUM_NUMBER_OF_BITS
;
532 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- (num_bits
));
536 if (precision
== X_PRECISION
&& exponent_bits
== 15)
542 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- num_bits
);
546 word1
|= next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1)
547 - (exponent_bits
+ num_bits
));
551 while (lp
< words_end
)
552 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
554 /* Round the mantissa up, but don't change the number. */
558 if (prec_bits
>= LITTLENUM_NUMBER_OF_BITS
)
564 tmp_bits
= prec_bits
;
565 while (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
)
567 if (lp
[n
] != (LITTLENUM_TYPE
) - 1)
570 tmp_bits
-= LITTLENUM_NUMBER_OF_BITS
;
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
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))
588 for (carry
= 1; carry
&& (lp
>= words
); lp
--)
592 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
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. */
601 word1
= ((generic_floating_point_number
.sign
== '+')
603 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
605 << ((LITTLENUM_NUMBER_OF_BITS
- 1)
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);
615 while (lp
< words_end
)
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
]))
629 /* Exponent overflow. Lose immediately. */
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
);
639 word1
|= (exponent_4
<< ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
))
640 | next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
);
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
)
652 *lp
++ = (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)
653 | next_bits (LITTLENUM_NUMBER_OF_BITS
- 1));
656 /* The rest of the words are just mantissa bits. */
657 while (lp
< words_end
)
658 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
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.
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.
677 for (carry
= 1, lp
--; carry
; lp
--)
681 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
685 if (precision
== X_PRECISION
&& exponent_bits
== 15)
687 /* Extended precision numbers have an explicit integer bit
688 that we may have to restore. */
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];
698 /* Put back the integer bit. */
699 lp
[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
702 if ((word1
^ *words
) & (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)))
704 /* We leave return_value alone: admit we read the number,
705 but return a floating exception because we can't encode
707 *words
&= ~(1 << (LITTLENUM_NUMBER_OF_BITS
- 1));
719 LITTLENUM_TYPE arr
[10];
722 static char sbuf
[40];
726 f
= generic_floating_point_number
;
727 generic_floating_point_number
= *gen
;
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
);
737 generic_floating_point_number
= f
;
743 /* This is a utility function called from various tc-*.c files. It
744 is here in order to reduce code duplication.
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.
752 An error message is returned, or a NULL pointer if everything went OK. */
755 ieee_md_atof (int type
,
758 bfd_boolean big_wordian
)
760 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
761 LITTLENUM_TYPE
*wordP
;
765 if (strchr (FLT_CHARS
, type
) != NULL
)
791 type
= 'x'; /* This is what atof_ieee() understands. */
799 /* Note: on the m68k there is a gap of 16 bits (one littlenum)
800 between the exponent and mantissa. Hence the precision is
802 prec
= P_PRECISION
+ 1;
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.
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')
823 else if (type
== 'd')
829 return _("Unrecognized or unsupported floating point constant");
832 gas_assert (prec
<= MAX_LITTLENUMS
);
834 t
= atof_ieee (input_line_pointer
, type
, words
);
836 input_line_pointer
= t
;
838 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
842 for (wordP
= words
; prec
--;)
844 md_number_to_chars (litP
, (valueT
) (* wordP
++), sizeof (LITTLENUM_TYPE
));
845 litP
+= sizeof (LITTLENUM_TYPE
);
850 for (wordP
= words
+ prec
; prec
--;)
852 md_number_to_chars (litP
, (valueT
) (* -- wordP
), sizeof (LITTLENUM_TYPE
));
853 litP
+= sizeof (LITTLENUM_TYPE
);