1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number
2 Copyright (C) 1987-2023 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
22 #include "safe-ctype.h"
24 /* Flonums returned here. */
25 extern FLONUM_TYPE generic_floating_point_number
;
27 /* Precision in LittleNums. */
28 /* Don't count the gap in the m68k extended precision format. */
29 #define MAX_PRECISION 5
31 #define B_PRECISION 1 /* Not strictly IEEE, but handled here anyway. */
35 #ifndef X_PRECISION_PAD
36 #define X_PRECISION_PAD 0
39 #ifndef P_PRECISION_PAD
40 #define P_PRECISION_PAD X_PRECISION_PAD
43 /* Length in LittleNums of guard bits. */
46 #ifndef TC_LARGEST_EXPONENT_IS_NORMAL
47 #define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) 0
50 static const unsigned long mask
[] =
87 static int bits_left_in_littlenum
;
88 static int littlenums_left
;
89 static LITTLENUM_TYPE
*littlenum_pointer
;
92 next_bits (int number_of_bits
)
99 if (number_of_bits
>= bits_left_in_littlenum
)
101 return_value
= mask
[bits_left_in_littlenum
] & *littlenum_pointer
;
102 number_of_bits
-= bits_left_in_littlenum
;
103 return_value
<<= number_of_bits
;
105 if (--littlenums_left
)
107 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
- number_of_bits
;
110 (*littlenum_pointer
>> bits_left_in_littlenum
)
111 & mask
[number_of_bits
];
116 bits_left_in_littlenum
-= number_of_bits
;
118 mask
[number_of_bits
] & (*littlenum_pointer
>> bits_left_in_littlenum
);
123 /* Num had better be less than LITTLENUM_NUMBER_OF_BITS. */
128 if (!littlenums_left
)
132 bits_left_in_littlenum
= num
;
134 else if (bits_left_in_littlenum
+ num
> LITTLENUM_NUMBER_OF_BITS
)
136 bits_left_in_littlenum
=
137 num
- (LITTLENUM_NUMBER_OF_BITS
- bits_left_in_littlenum
);
142 bits_left_in_littlenum
+= num
;
146 make_invalid_floating_point_number (LITTLENUM_TYPE
*words
)
148 as_bad (_("cannot create floating-point number"));
149 /* Zero the leftmost bit. */
150 words
[0] = (LITTLENUM_TYPE
) ((unsigned) -1) >> 1;
151 words
[1] = (LITTLENUM_TYPE
) -1;
152 words
[2] = (LITTLENUM_TYPE
) -1;
153 words
[3] = (LITTLENUM_TYPE
) -1;
154 words
[4] = (LITTLENUM_TYPE
) -1;
155 words
[5] = (LITTLENUM_TYPE
) -1;
158 /* Build a floating point constant at str into a IEEE floating
159 point number. This function does the same thing as atof_ieee
160 however it allows more control over the exact format, i.e.
161 explicitly specifying the precision and number of exponent bits
162 instead of relying on this infomation being deduced from a given type.
164 If generic_float_info is not NULL then it will be set to contain generic
165 infomation about the parsed floating point number.
167 Returns pointer past text consumed. */
169 atof_ieee_detail (char * str
,
172 LITTLENUM_TYPE
* words
,
173 FLONUM_TYPE
* generic_float_info
)
175 /* Extra bits for zeroed low-order bits.
176 The 1st MAX_PRECISION are zeroed, the last contain flonum bits. */
177 static LITTLENUM_TYPE bits
[MAX_PRECISION
+ MAX_PRECISION
+ GUARD
];
180 /* Number of 16-bit words in the format. */
181 FLONUM_TYPE save_gen_flonum
;
183 /* We have to save the generic_floating_point_number because it
184 contains storage allocation about the array of LITTLENUMs where
185 the value is actually stored. We will allocate our own array of
186 littlenums below, but have to restore the global one on exit. */
187 save_gen_flonum
= generic_floating_point_number
;
190 generic_floating_point_number
.low
= bits
+ MAX_PRECISION
;
191 generic_floating_point_number
.high
= NULL
;
192 generic_floating_point_number
.leader
= NULL
;
193 generic_floating_point_number
.exponent
= 0;
194 generic_floating_point_number
.sign
= '\0';
196 /* Use more LittleNums than seems necessary: the highest flonum may
197 have 15 leading 0 bits, so could be useless. */
199 memset (bits
, '\0', sizeof (LITTLENUM_TYPE
) * MAX_PRECISION
);
201 generic_floating_point_number
.high
202 = generic_floating_point_number
.low
+ precision
- 1 + GUARD
;
204 if (atof_generic (&return_value
, ".", EXP_CHARS
,
205 &generic_floating_point_number
))
207 make_invalid_floating_point_number (words
);
211 if (generic_float_info
)
212 *generic_float_info
= generic_floating_point_number
;
214 gen_to_words (words
, precision
, exponent_bits
);
216 /* Restore the generic_floating_point_number's storage alloc (and
218 generic_floating_point_number
= save_gen_flonum
;
223 /* Warning: This returns 16-bit LITTLENUMs. It is up to the caller to
224 figure out any alignment problems and to conspire for the
225 bytes/word to be emitted in the right order. Bigendians beware! */
227 /* Note that atof-ieee always has X and P precisions enabled. it is up
228 to md_atof to filter them out if the target machine does not support
231 /* Returns pointer past text consumed. */
233 atof_ieee (char *str
, /* Text to convert to binary. */
234 int what_kind
, /* 'd', 'f', 'x', 'p'. */
235 LITTLENUM_TYPE
*words
) /* Build the binary here. */
244 precision
= H_PRECISION
;
250 precision
= B_PRECISION
;
258 precision
= F_PRECISION
;
266 precision
= D_PRECISION
;
274 precision
= X_PRECISION
;
280 precision
= P_PRECISION
;
285 make_invalid_floating_point_number (words
);
289 return atof_ieee_detail (str
, precision
, exponent_bits
, words
, NULL
);
292 /* Turn generic_floating_point_number into a real float/double/extended. */
295 gen_to_words (LITTLENUM_TYPE
*words
, int precision
, long exponent_bits
)
297 int return_value
= 0;
303 int exponent_skippage
;
304 LITTLENUM_TYPE word1
;
306 LITTLENUM_TYPE
*words_end
;
308 words_end
= words
+ precision
;
310 if (precision
== X_PRECISION
)
311 /* On the m68k the extended precision format has a gap of 16 bits
312 between the exponent and the mantissa. */
316 if (generic_floating_point_number
.low
> generic_floating_point_number
.leader
)
319 if (generic_floating_point_number
.sign
== '+')
323 memset (&words
[1], '\0',
324 (words_end
- words
- 1) * sizeof (LITTLENUM_TYPE
));
328 switch (generic_floating_point_number
.sign
)
330 /* NaN: Do the right thing. */
334 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
335 as_warn (_("NaNs are not supported by this target"));
337 if (precision
== H_PRECISION
)
339 if (TOUPPER (generic_floating_point_number
.sign
) != 'S')
342 words
[0] = exponent_bits
== 5 ? 0x7dff : 0x7fbf;
344 else if (precision
== F_PRECISION
)
346 words
[0] = TOUPPER (generic_floating_point_number
.sign
) == 'S'
350 else if (precision
== X_PRECISION
)
353 if (generic_floating_point_number
.sign
)
354 as_warn (_("NaN flavors are not supported by this target"));
362 #else /* ! TC_M68K */
365 words
[1] = TOUPPER (generic_floating_point_number
.sign
) == 'S'
370 #else /* ! TC_I386 */
372 #endif /* ! TC_I386 */
373 #endif /* ! TC_M68K */
377 words
[0] = TOUPPER (generic_floating_point_number
.sign
) == 'S'
384 if (ISLOWER (generic_floating_point_number
.sign
))
391 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
392 as_warn (_("Infinities are not supported by this target"));
394 /* +INF: Do the right thing. */
395 if (precision
== H_PRECISION
/* also B_PRECISION */)
397 words
[0] = exponent_bits
== 5 ? 0x7c00 : 0x7f80;
399 else if (precision
== F_PRECISION
)
404 else if (precision
== X_PRECISION
)
413 #else /* ! TC_M68K */
420 #else /* ! TC_I386 */
422 #endif /* ! TC_I386 */
423 #endif /* ! TC_M68K */
433 if (generic_floating_point_number
.sign
== 'N')
439 /* The floating point formats we support have:
441 Bits 14:n are excess-whatever exponent.
442 Bits n-1:0 (if any) are most significant bits of fraction.
443 Bits 15:0 of the next word(s) are the next most significant bits.
445 So we need: number of bits of exponent, number of bits of
447 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
;
448 littlenum_pointer
= generic_floating_point_number
.leader
;
450 + generic_floating_point_number
.leader
451 - generic_floating_point_number
.low
);
453 /* Seek (and forget) 1st significant bit. */
454 for (exponent_skippage
= 0; !next_bits (1); ++exponent_skippage
);
455 exponent_1
= (generic_floating_point_number
.exponent
456 + generic_floating_point_number
.leader
458 - generic_floating_point_number
.low
);
460 /* Radix LITTLENUM_RADIX, point just higher than
461 generic_floating_point_number.leader. */
462 exponent_2
= exponent_1
* LITTLENUM_NUMBER_OF_BITS
;
465 exponent_3
= exponent_2
- exponent_skippage
;
467 /* Forget leading zeros, forget 1st bit. */
468 exponent_4
= exponent_3
+ ((1 << (exponent_bits
- 1)) - 2);
470 /* Offset exponent. */
473 /* Word 1. Sign, exponent and perhaps high bits. */
474 word1
= ((generic_floating_point_number
.sign
== '+')
476 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
478 /* Assume 2's complement integers. */
485 num_bits
= -exponent_4
;
487 LITTLENUM_NUMBER_OF_BITS
* precision
- (exponent_bits
+ 1 + num_bits
);
489 if (precision
== X_PRECISION
&& exponent_bits
== 15)
491 /* On the i386 a denormalized extended precision float is
492 shifted down by one, effectively decreasing the exponent
499 if (num_bits
>= LITTLENUM_NUMBER_OF_BITS
- exponent_bits
)
501 /* Bigger than one littlenum. */
502 num_bits
-= (LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
;
504 if (num_bits
+ exponent_bits
+ 1
505 > precision
* LITTLENUM_NUMBER_OF_BITS
)
507 /* Exponent overflow. */
508 make_invalid_floating_point_number (words
);
512 if (precision
== X_PRECISION
&& exponent_bits
== 15)
515 while (num_bits
>= LITTLENUM_NUMBER_OF_BITS
)
517 num_bits
-= LITTLENUM_NUMBER_OF_BITS
;
521 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- (num_bits
));
525 if (precision
== X_PRECISION
&& exponent_bits
== 15)
531 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- num_bits
);
535 word1
|= next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1)
536 - (exponent_bits
+ num_bits
));
540 while (lp
< words_end
)
541 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
543 /* Round the mantissa up, but don't change the number. */
547 if (prec_bits
>= LITTLENUM_NUMBER_OF_BITS
)
553 tmp_bits
= prec_bits
;
554 while (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
)
556 if (lp
[n
] != (LITTLENUM_TYPE
) - 1)
559 tmp_bits
-= LITTLENUM_NUMBER_OF_BITS
;
561 if (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
562 || (lp
[n
] & mask
[tmp_bits
]) != mask
[tmp_bits
]
563 || (prec_bits
!= (precision
* LITTLENUM_NUMBER_OF_BITS
566 /* An extended precision float with only the integer
567 bit set would be invalid. That must be converted
568 to the smallest normalized number. */
569 && !(precision
== X_PRECISION
570 && prec_bits
== (precision
* LITTLENUM_NUMBER_OF_BITS
571 - exponent_bits
- 2))
577 for (carry
= 1; carry
&& (lp
>= words
); lp
--)
581 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
586 /* This is an overflow of the denormal numbers. We
587 need to forget what we have produced, and instead
588 generate the smallest normalized number. */
590 word1
= ((generic_floating_point_number
.sign
== '+')
592 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
594 << ((LITTLENUM_NUMBER_OF_BITS
- 1)
598 /* Set the integer bit in the extended precision format.
599 This cannot happen on the m68k where the mantissa
600 just overflows into the integer bit above. */
601 if (precision
== X_PRECISION
)
602 *lp
++ = 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
604 while (lp
< words_end
)
614 else if ((unsigned long) exponent_4
> mask
[exponent_bits
]
615 || (! TC_LARGEST_EXPONENT_IS_NORMAL (precision
)
616 && (unsigned long) exponent_4
== mask
[exponent_bits
]))
618 /* Exponent overflow. Lose immediately. */
620 /* We leave return_value alone: admit we read the
621 number, but return a floating exception
622 because we can't encode the number. */
623 make_invalid_floating_point_number (words
);
628 word1
|= (exponent_4
<< ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
))
629 | next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
);
634 /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the
635 middle. Either way, it is then followed by a 1 bit. */
636 if (exponent_bits
== 15 && precision
== X_PRECISION
)
641 *lp
++ = (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)
642 | next_bits (LITTLENUM_NUMBER_OF_BITS
- 1));
645 /* The rest of the words are just mantissa bits. */
646 while (lp
< words_end
)
647 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
652 /* Since the NEXT bit is a 1, round UP the mantissa.
653 The cunning design of these hidden-1 floats permits
654 us to let the mantissa overflow into the exponent, and
655 it 'does the right thing'. However, we lose if the
656 highest-order bit of the lowest-order word flips.
659 /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
660 Please allow at least 1 more bit in carry than is in a LITTLENUM.
661 We need that extra bit to hold a carry during a LITTLENUM carry
662 propagation. Another extra bit (kept 0) will assure us that we
663 don't get a sticky sign bit after shifting right, and that
664 permits us to propagate the carry without any masking of bits.
666 for (carry
= 1, lp
--; carry
; lp
--)
670 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
674 if (precision
== X_PRECISION
&& exponent_bits
== 15)
676 /* Extended precision numbers have an explicit integer bit
677 that we may have to restore. */
681 /* On the m68k there is a gap of 16 bits. We must
682 explicitly propagate the carry into the exponent. */
683 words
[0] += words
[1];
687 /* Put back the integer bit. */
688 lp
[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
691 if ((word1
^ *words
) & (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)))
693 /* We leave return_value alone: admit we read the number,
694 but return a floating exception because we can't encode
696 *words
&= ~(1 << (LITTLENUM_NUMBER_OF_BITS
- 1));
708 LITTLENUM_TYPE arr
[10];
711 static char sbuf
[40];
715 f
= generic_floating_point_number
;
716 generic_floating_point_number
= *gen
;
718 gen_to_words (&arr
[0], 4, 11);
719 memcpy (&dv
, &arr
[0], sizeof (double));
720 sprintf (sbuf
, "%x %x %x %x %.14G ", arr
[0], arr
[1], arr
[2], arr
[3], dv
);
721 gen_to_words (&arr
[0], 2, 8);
722 memcpy (&fv
, &arr
[0], sizeof (float));
723 sprintf (sbuf
+ strlen (sbuf
), "%x %x %.12g\n", arr
[0], arr
[1], fv
);
726 generic_floating_point_number
= f
;
732 /* This is a utility function called from various tc-*.c files. It
733 is here in order to reduce code duplication.
735 Turn a string at input_line_pointer into a floating point constant
736 of type TYPE (a character found in the FLT_CHARS macro), and store
737 it as LITTLENUMS in the bytes buffer LITP. The number of chars
738 emitted is stored in *SIZEP. BIG_WORDIAN is TRUE if the littlenums
739 should be emitted most significant littlenum first.
741 An error message is returned, or a NULL pointer if everything went OK. */
744 ieee_md_atof (int type
,
749 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
750 LITTLENUM_TYPE
*wordP
;
752 int prec
= 0, pad
= 0;
754 if (strchr (FLT_CHARS
, type
) != NULL
)
785 pad
= X_PRECISION_PAD
;
786 type
= 'x'; /* This is what atof_ieee() understands. */
794 /* Note: on the m68k there is a gap of 16 bits (one littlenum)
795 between the exponent and mantissa. Hence the precision is
797 prec
= P_PRECISION
+ 1;
801 pad
= P_PRECISION_PAD
;
808 /* The 'f' and 'd' types are always recognised, even if the target has
809 not put them into the FLT_CHARS macro. This is because the 'f' type
810 can come from the .dc.s, .dcb.s, .float or .single pseudo-ops and the
811 'd' type from the .dc.d, .dbc.d or .double pseudo-ops.
813 The 'x' type is not implicitly recognised however, even though it can
814 be generated by the .dc.x and .dbc.x pseudo-ops because not all targets
815 can support floating point values that big. ie the target has to
816 explicitly allow them by putting them into FLT_CHARS. */
817 else if (type
== 'f')
819 else if (type
== 'd')
825 return _("Unrecognized or unsupported floating point constant");
828 gas_assert (prec
<= MAX_LITTLENUMS
);
830 t
= atof_ieee (input_line_pointer
, type
, words
);
832 input_line_pointer
= t
;
834 *sizeP
= (prec
+ pad
) * sizeof (LITTLENUM_TYPE
);
838 for (wordP
= words
; prec
--;)
840 md_number_to_chars (litP
, (valueT
) (* wordP
++), sizeof (LITTLENUM_TYPE
));
841 litP
+= sizeof (LITTLENUM_TYPE
);
846 for (wordP
= words
+ prec
; prec
--;)
848 md_number_to_chars (litP
, (valueT
) (* -- wordP
), sizeof (LITTLENUM_TYPE
));
849 litP
+= sizeof (LITTLENUM_TYPE
);
853 memset (litP
, 0, pad
* sizeof (LITTLENUM_TYPE
));
854 litP
+= pad
* sizeof (LITTLENUM_TYPE
);