1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number
2 Copyright (C) 1987-2018 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
34 /* Length in LittleNums of guard bits. */
37 #ifndef TC_LARGEST_EXPONENT_IS_NORMAL
38 #define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) 0
41 static const unsigned long mask
[] =
78 static int bits_left_in_littlenum
;
79 static int littlenums_left
;
80 static LITTLENUM_TYPE
*littlenum_pointer
;
83 next_bits (int number_of_bits
)
90 if (number_of_bits
>= bits_left_in_littlenum
)
92 return_value
= mask
[bits_left_in_littlenum
] & *littlenum_pointer
;
93 number_of_bits
-= bits_left_in_littlenum
;
94 return_value
<<= number_of_bits
;
96 if (--littlenums_left
)
98 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
- number_of_bits
;
101 (*littlenum_pointer
>> bits_left_in_littlenum
)
102 & mask
[number_of_bits
];
107 bits_left_in_littlenum
-= number_of_bits
;
109 mask
[number_of_bits
] & (*littlenum_pointer
>> bits_left_in_littlenum
);
114 /* Num had better be less than LITTLENUM_NUMBER_OF_BITS. */
119 if (!littlenums_left
)
123 bits_left_in_littlenum
= num
;
125 else if (bits_left_in_littlenum
+ num
> LITTLENUM_NUMBER_OF_BITS
)
127 bits_left_in_littlenum
=
128 num
- (LITTLENUM_NUMBER_OF_BITS
- bits_left_in_littlenum
);
133 bits_left_in_littlenum
+= num
;
137 make_invalid_floating_point_number (LITTLENUM_TYPE
*words
)
139 as_bad (_("cannot create floating-point number"));
140 /* Zero the leftmost bit. */
141 words
[0] = (LITTLENUM_TYPE
) ((unsigned) -1) >> 1;
142 words
[1] = (LITTLENUM_TYPE
) -1;
143 words
[2] = (LITTLENUM_TYPE
) -1;
144 words
[3] = (LITTLENUM_TYPE
) -1;
145 words
[4] = (LITTLENUM_TYPE
) -1;
146 words
[5] = (LITTLENUM_TYPE
) -1;
149 /* Warning: This returns 16-bit LITTLENUMs. It is up to the caller to
150 figure out any alignment problems and to conspire for the
151 bytes/word to be emitted in the right order. Bigendians beware! */
153 /* Note that atof-ieee always has X and P precisions enabled. it is up
154 to md_atof to filter them out if the target machine does not support
157 /* Returns pointer past text consumed. */
160 atof_ieee (char *str
, /* Text to convert to binary. */
161 int what_kind
, /* 'd', 'f', 'x', 'p'. */
162 LITTLENUM_TYPE
*words
) /* Build the binary here. */
164 /* Extra bits for zeroed low-order bits.
165 The 1st MAX_PRECISION are zeroed, the last contain flonum bits. */
166 static LITTLENUM_TYPE bits
[MAX_PRECISION
+ MAX_PRECISION
+ GUARD
];
168 /* Number of 16-bit words in the format. */
171 FLONUM_TYPE save_gen_flonum
;
173 /* We have to save the generic_floating_point_number because it
174 contains storage allocation about the array of LITTLENUMs where
175 the value is actually stored. We will allocate our own array of
176 littlenums below, but have to restore the global one on exit. */
177 save_gen_flonum
= generic_floating_point_number
;
180 generic_floating_point_number
.low
= bits
+ MAX_PRECISION
;
181 generic_floating_point_number
.high
= NULL
;
182 generic_floating_point_number
.leader
= NULL
;
183 generic_floating_point_number
.exponent
= 0;
184 generic_floating_point_number
.sign
= '\0';
186 /* Use more LittleNums than seems necessary: the highest flonum may
187 have 15 leading 0 bits, so could be useless. */
189 memset (bits
, '\0', sizeof (LITTLENUM_TYPE
) * MAX_PRECISION
);
197 precision
= F_PRECISION
;
205 precision
= D_PRECISION
;
213 precision
= X_PRECISION
;
219 precision
= P_PRECISION
;
224 make_invalid_floating_point_number (words
);
228 generic_floating_point_number
.high
229 = generic_floating_point_number
.low
+ precision
- 1 + GUARD
;
231 if (atof_generic (&return_value
, ".", EXP_CHARS
,
232 &generic_floating_point_number
))
234 make_invalid_floating_point_number (words
);
237 gen_to_words (words
, precision
, exponent_bits
);
239 /* Restore the generic_floating_point_number's storage alloc (and
241 generic_floating_point_number
= save_gen_flonum
;
246 /* Turn generic_floating_point_number into a real float/double/extended. */
249 gen_to_words (LITTLENUM_TYPE
*words
, int precision
, long exponent_bits
)
251 int return_value
= 0;
257 int exponent_skippage
;
258 LITTLENUM_TYPE word1
;
260 LITTLENUM_TYPE
*words_end
;
262 words_end
= words
+ precision
;
264 if (precision
== X_PRECISION
)
265 /* On the m68k the extended precision format has a gap of 16 bits
266 between the exponent and the mantissa. */
270 if (generic_floating_point_number
.low
> generic_floating_point_number
.leader
)
273 if (generic_floating_point_number
.sign
== '+')
277 memset (&words
[1], '\0',
278 (words_end
- words
- 1) * sizeof (LITTLENUM_TYPE
));
282 /* NaN: Do the right thing. */
283 if (generic_floating_point_number
.sign
== 0)
285 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
286 as_warn (_("NaNs are not supported by this target\n"));
287 if (precision
== F_PRECISION
)
292 else if (precision
== X_PRECISION
)
301 #else /* ! TC_M68K */
308 #else /* ! TC_I386 */
310 #endif /* ! TC_I386 */
311 #endif /* ! TC_M68K */
322 else if (generic_floating_point_number
.sign
== 'P')
324 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
325 as_warn (_("Infinities are not supported by this target\n"));
327 /* +INF: Do the right thing. */
328 if (precision
== F_PRECISION
)
333 else if (precision
== X_PRECISION
)
342 #else /* ! TC_M68K */
349 #else /* ! TC_I386 */
351 #endif /* ! TC_I386 */
352 #endif /* ! TC_M68K */
363 else if (generic_floating_point_number
.sign
== 'N')
365 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
366 as_warn (_("Infinities are not supported by this target\n"));
369 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 */
405 /* The floating point formats we support have:
407 Bits 14:n are excess-whatever exponent.
408 Bits n-1:0 (if any) are most significant bits of fraction.
409 Bits 15:0 of the next word(s) are the next most significant bits.
411 So we need: number of bits of exponent, number of bits of
413 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
;
414 littlenum_pointer
= generic_floating_point_number
.leader
;
416 + generic_floating_point_number
.leader
417 - generic_floating_point_number
.low
);
419 /* Seek (and forget) 1st significant bit. */
420 for (exponent_skippage
= 0; !next_bits (1); ++exponent_skippage
);
421 exponent_1
= (generic_floating_point_number
.exponent
422 + generic_floating_point_number
.leader
424 - generic_floating_point_number
.low
);
426 /* Radix LITTLENUM_RADIX, point just higher than
427 generic_floating_point_number.leader. */
428 exponent_2
= exponent_1
* LITTLENUM_NUMBER_OF_BITS
;
431 exponent_3
= exponent_2
- exponent_skippage
;
433 /* Forget leading zeros, forget 1st bit. */
434 exponent_4
= exponent_3
+ ((1 << (exponent_bits
- 1)) - 2);
436 /* Offset exponent. */
439 /* Word 1. Sign, exponent and perhaps high bits. */
440 word1
= ((generic_floating_point_number
.sign
== '+')
442 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
444 /* Assume 2's complement integers. */
451 num_bits
= -exponent_4
;
453 LITTLENUM_NUMBER_OF_BITS
* precision
- (exponent_bits
+ 1 + num_bits
);
455 if (precision
== X_PRECISION
&& exponent_bits
== 15)
457 /* On the i386 a denormalized extended precision float is
458 shifted down by one, effectively decreasing the exponent
465 if (num_bits
>= LITTLENUM_NUMBER_OF_BITS
- exponent_bits
)
467 /* Bigger than one littlenum. */
468 num_bits
-= (LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
;
470 if (num_bits
+ exponent_bits
+ 1
471 > precision
* LITTLENUM_NUMBER_OF_BITS
)
473 /* Exponent overflow. */
474 make_invalid_floating_point_number (words
);
478 if (precision
== X_PRECISION
&& exponent_bits
== 15)
481 while (num_bits
>= LITTLENUM_NUMBER_OF_BITS
)
483 num_bits
-= LITTLENUM_NUMBER_OF_BITS
;
487 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- (num_bits
));
491 if (precision
== X_PRECISION
&& exponent_bits
== 15)
497 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- num_bits
);
501 word1
|= next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1)
502 - (exponent_bits
+ num_bits
));
506 while (lp
< words_end
)
507 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
509 /* Round the mantissa up, but don't change the number. */
513 if (prec_bits
>= LITTLENUM_NUMBER_OF_BITS
)
519 tmp_bits
= prec_bits
;
520 while (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
)
522 if (lp
[n
] != (LITTLENUM_TYPE
) - 1)
525 tmp_bits
-= LITTLENUM_NUMBER_OF_BITS
;
527 if (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
528 || (lp
[n
] & mask
[tmp_bits
]) != mask
[tmp_bits
]
529 || (prec_bits
!= (precision
* LITTLENUM_NUMBER_OF_BITS
532 /* An extended precision float with only the integer
533 bit set would be invalid. That must be converted
534 to the smallest normalized number. */
535 && !(precision
== X_PRECISION
536 && prec_bits
== (precision
* LITTLENUM_NUMBER_OF_BITS
537 - exponent_bits
- 2))
543 for (carry
= 1; carry
&& (lp
>= words
); lp
--)
547 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
552 /* This is an overflow of the denormal numbers. We
553 need to forget what we have produced, and instead
554 generate the smallest normalized number. */
556 word1
= ((generic_floating_point_number
.sign
== '+')
558 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
560 << ((LITTLENUM_NUMBER_OF_BITS
- 1)
564 /* Set the integer bit in the extended precision format.
565 This cannot happen on the m68k where the mantissa
566 just overflows into the integer bit above. */
567 if (precision
== X_PRECISION
)
568 *lp
++ = 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
570 while (lp
< words_end
)
580 else if ((unsigned long) exponent_4
> mask
[exponent_bits
]
581 || (! TC_LARGEST_EXPONENT_IS_NORMAL (precision
)
582 && (unsigned long) exponent_4
== mask
[exponent_bits
]))
584 /* Exponent overflow. Lose immediately. */
586 /* We leave return_value alone: admit we read the
587 number, but return a floating exception
588 because we can't encode the number. */
589 make_invalid_floating_point_number (words
);
594 word1
|= (exponent_4
<< ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
))
595 | next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
);
600 /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the
601 middle. Either way, it is then followed by a 1 bit. */
602 if (exponent_bits
== 15 && precision
== X_PRECISION
)
607 *lp
++ = (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)
608 | next_bits (LITTLENUM_NUMBER_OF_BITS
- 1));
611 /* The rest of the words are just mantissa bits. */
612 while (lp
< words_end
)
613 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
618 /* Since the NEXT bit is a 1, round UP the mantissa.
619 The cunning design of these hidden-1 floats permits
620 us to let the mantissa overflow into the exponent, and
621 it 'does the right thing'. However, we lose if the
622 highest-order bit of the lowest-order word flips.
625 /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
626 Please allow at least 1 more bit in carry than is in a LITTLENUM.
627 We need that extra bit to hold a carry during a LITTLENUM carry
628 propagation. Another extra bit (kept 0) will assure us that we
629 don't get a sticky sign bit after shifting right, and that
630 permits us to propagate the carry without any masking of bits.
632 for (carry
= 1, lp
--; carry
; lp
--)
636 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
640 if (precision
== X_PRECISION
&& exponent_bits
== 15)
642 /* Extended precision numbers have an explicit integer bit
643 that we may have to restore. */
647 /* On the m68k there is a gap of 16 bits. We must
648 explicitly propagate the carry into the exponent. */
649 words
[0] += words
[1];
653 /* Put back the integer bit. */
654 lp
[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
657 if ((word1
^ *words
) & (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)))
659 /* We leave return_value alone: admit we read the number,
660 but return a floating exception because we can't encode
662 *words
&= ~(1 << (LITTLENUM_NUMBER_OF_BITS
- 1));
674 LITTLENUM_TYPE arr
[10];
677 static char sbuf
[40];
681 f
= generic_floating_point_number
;
682 generic_floating_point_number
= *gen
;
684 gen_to_words (&arr
[0], 4, 11);
685 memcpy (&dv
, &arr
[0], sizeof (double));
686 sprintf (sbuf
, "%x %x %x %x %.14G ", arr
[0], arr
[1], arr
[2], arr
[3], dv
);
687 gen_to_words (&arr
[0], 2, 8);
688 memcpy (&fv
, &arr
[0], sizeof (float));
689 sprintf (sbuf
+ strlen (sbuf
), "%x %x %.12g\n", arr
[0], arr
[1], fv
);
692 generic_floating_point_number
= f
;
698 #define MAX_LITTLENUMS 6
700 /* This is a utility function called from various tc-*.c files. It
701 is here in order to reduce code duplication.
703 Turn a string at input_line_pointer into a floating point constant
704 of type TYPE (a character found in the FLT_CHARS macro), and store
705 it as LITTLENUMS in the bytes buffer LITP. The number of chars
706 emitted is stored in *SIZEP. BIG_WORDIAN is TRUE if the littlenums
707 should be emitted most significant littlenum first.
709 An error message is returned, or a NULL pointer if everything went OK. */
712 ieee_md_atof (int type
,
715 bfd_boolean big_wordian
)
717 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
718 LITTLENUM_TYPE
*wordP
;
722 if (strchr (FLT_CHARS
, type
) != NULL
)
743 type
= 'x'; /* This is what atof_ieee() understands. */
751 /* Note: on the m68k there is a gap of 16 bits (one littlenum)
752 between the exponent and mantissa. Hence the precision is
754 prec
= P_PRECISION
+ 1;
764 /* The 'f' and 'd' types are always recognised, even if the target has
765 not put them into the FLT_CHARS macro. This is because the 'f' type
766 can come from the .dc.s, .dcb.s, .float or .single pseudo-ops and the
767 'd' type from the .dc.d, .dbc.d or .double pseudo-ops.
769 The 'x' type is not implicitly recognised however, even though it can
770 be generated by the .dc.x and .dbc.x pseudo-ops because not all targets
771 can support floating point values that big. ie the target has to
772 explicitly allow them by putting them into FLT_CHARS. */
773 else if (type
== 'f')
775 else if (type
== 'd')
781 return _("Unrecognized or unsupported floating point constant");
784 gas_assert (prec
<= MAX_LITTLENUMS
);
786 t
= atof_ieee (input_line_pointer
, type
, words
);
788 input_line_pointer
= t
;
790 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
794 for (wordP
= words
; prec
--;)
796 md_number_to_chars (litP
, (valueT
) (* wordP
++), sizeof (LITTLENUM_TYPE
));
797 litP
+= sizeof (LITTLENUM_TYPE
);
802 for (wordP
= words
+ prec
; prec
--;)
804 md_number_to_chars (litP
, (valueT
) (* -- wordP
), sizeof (LITTLENUM_TYPE
));
805 litP
+= sizeof (LITTLENUM_TYPE
);