1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number
2 Copyright 1987, 1992, 1994, 1996, 1997, 1998, 1999, 2000, 2001, 2005,
3 2007, 2009 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 /* Flonums returned here. */
25 extern FLONUM_TYPE generic_floating_point_number
;
27 extern const char EXP_CHARS
[];
28 /* Precision in LittleNums. */
29 /* Don't count the gap in the m68k extended precision format. */
30 #define MAX_PRECISION 5
36 /* Length in LittleNums of guard bits. */
39 #ifndef TC_LARGEST_EXPONENT_IS_NORMAL
40 #define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) 0
43 static const unsigned long mask
[] =
80 static int bits_left_in_littlenum
;
81 static int littlenums_left
;
82 static LITTLENUM_TYPE
*littlenum_pointer
;
85 next_bits (int number_of_bits
)
92 if (number_of_bits
>= bits_left_in_littlenum
)
94 return_value
= mask
[bits_left_in_littlenum
] & *littlenum_pointer
;
95 number_of_bits
-= bits_left_in_littlenum
;
96 return_value
<<= number_of_bits
;
98 if (--littlenums_left
)
100 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
- number_of_bits
;
103 (*littlenum_pointer
>> bits_left_in_littlenum
)
104 & mask
[number_of_bits
];
109 bits_left_in_littlenum
-= number_of_bits
;
111 mask
[number_of_bits
] & (*littlenum_pointer
>> bits_left_in_littlenum
);
116 /* Num had better be less than LITTLENUM_NUMBER_OF_BITS. */
121 if (!littlenums_left
)
125 bits_left_in_littlenum
= num
;
127 else if (bits_left_in_littlenum
+ num
> LITTLENUM_NUMBER_OF_BITS
)
129 bits_left_in_littlenum
=
130 num
- (LITTLENUM_NUMBER_OF_BITS
- bits_left_in_littlenum
);
135 bits_left_in_littlenum
+= num
;
139 make_invalid_floating_point_number (LITTLENUM_TYPE
*words
)
141 as_bad (_("cannot create floating-point number"));
142 /* Zero the leftmost bit. */
143 words
[0] = (LITTLENUM_TYPE
) ((unsigned) -1) >> 1;
144 words
[1] = (LITTLENUM_TYPE
) -1;
145 words
[2] = (LITTLENUM_TYPE
) -1;
146 words
[3] = (LITTLENUM_TYPE
) -1;
147 words
[4] = (LITTLENUM_TYPE
) -1;
148 words
[5] = (LITTLENUM_TYPE
) -1;
151 /* 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! */
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
159 /* Returns pointer past text consumed. */
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. */
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
];
170 /* 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
);
199 precision
= F_PRECISION
;
207 precision
= D_PRECISION
;
215 precision
= X_PRECISION
;
221 precision
= P_PRECISION
;
226 make_invalid_floating_point_number (words
);
230 generic_floating_point_number
.high
231 = generic_floating_point_number
.low
+ precision
- 1 + GUARD
;
233 if (atof_generic (&return_value
, ".", EXP_CHARS
,
234 &generic_floating_point_number
))
236 make_invalid_floating_point_number (words
);
239 gen_to_words (words
, precision
, exponent_bits
);
241 /* Restore the generic_floating_point_number's storage alloc (and
243 generic_floating_point_number
= save_gen_flonum
;
248 /* Turn generic_floating_point_number into a real float/double/extended. */
251 gen_to_words (LITTLENUM_TYPE
*words
, int precision
, long exponent_bits
)
253 int return_value
= 0;
259 int exponent_skippage
;
260 LITTLENUM_TYPE word1
;
262 LITTLENUM_TYPE
*words_end
;
264 words_end
= words
+ precision
;
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. */
272 if (generic_floating_point_number
.low
> generic_floating_point_number
.leader
)
275 if (generic_floating_point_number
.sign
== '+')
279 memset (&words
[1], '\0',
280 (words_end
- words
- 1) * sizeof (LITTLENUM_TYPE
));
284 /* NaN: Do the right thing. */
285 if (generic_floating_point_number
.sign
== 0)
287 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
288 as_warn (_("NaNs are not supported by this target\n"));
289 if (precision
== F_PRECISION
)
294 else if (precision
== X_PRECISION
)
303 #else /* ! TC_M68K */
310 #else /* ! TC_I386 */
312 #endif /* ! TC_I386 */
313 #endif /* ! TC_M68K */
324 else if (generic_floating_point_number
.sign
== 'P')
326 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
327 as_warn (_("Infinities are not supported by this target\n"));
329 /* +INF: Do the right thing. */
330 if (precision
== F_PRECISION
)
335 else if (precision
== X_PRECISION
)
344 #else /* ! TC_M68K */
351 #else /* ! TC_I386 */
353 #endif /* ! TC_I386 */
354 #endif /* ! TC_M68K */
365 else if (generic_floating_point_number
.sign
== 'N')
367 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
368 as_warn (_("Infinities are not supported by this target\n"));
371 if (precision
== F_PRECISION
)
376 else if (precision
== X_PRECISION
)
385 #else /* ! TC_M68K */
392 #else /* ! TC_I386 */
394 #endif /* ! TC_I386 */
395 #endif /* ! TC_M68K */
407 /* The floating point formats we support have:
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.
413 So we need: number of bits of exponent, number of bits of
415 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
;
416 littlenum_pointer
= generic_floating_point_number
.leader
;
418 + generic_floating_point_number
.leader
419 - generic_floating_point_number
.low
);
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
426 - generic_floating_point_number
.low
);
428 /* Radix LITTLENUM_RADIX, point just higher than
429 generic_floating_point_number.leader. */
430 exponent_2
= exponent_1
* LITTLENUM_NUMBER_OF_BITS
;
433 exponent_3
= exponent_2
- exponent_skippage
;
435 /* Forget leading zeros, forget 1st bit. */
436 exponent_4
= exponent_3
+ ((1 << (exponent_bits
- 1)) - 2);
438 /* Offset exponent. */
441 /* Word 1. Sign, exponent and perhaps high bits. */
442 word1
= ((generic_floating_point_number
.sign
== '+')
444 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
446 /* Assume 2's complement integers. */
453 num_bits
= -exponent_4
;
455 LITTLENUM_NUMBER_OF_BITS
* precision
- (exponent_bits
+ 1 + num_bits
);
457 if (precision
== X_PRECISION
&& exponent_bits
== 15)
459 /* On the i386 a denormalized extended precision float is
460 shifted down by one, effectively decreasing the exponent
467 if (num_bits
>= LITTLENUM_NUMBER_OF_BITS
- exponent_bits
)
469 /* Bigger than one littlenum. */
470 num_bits
-= (LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
;
472 if (num_bits
+ exponent_bits
+ 1
473 > precision
* LITTLENUM_NUMBER_OF_BITS
)
475 /* Exponent overflow. */
476 make_invalid_floating_point_number (words
);
480 if (precision
== X_PRECISION
&& exponent_bits
== 15)
483 while (num_bits
>= LITTLENUM_NUMBER_OF_BITS
)
485 num_bits
-= LITTLENUM_NUMBER_OF_BITS
;
489 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- (num_bits
));
493 if (precision
== X_PRECISION
&& exponent_bits
== 15)
499 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- num_bits
);
503 word1
|= next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1)
504 - (exponent_bits
+ num_bits
));
508 while (lp
< words_end
)
509 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
511 /* Round the mantissa up, but don't change the number. */
515 if (prec_bits
>= LITTLENUM_NUMBER_OF_BITS
)
521 tmp_bits
= prec_bits
;
522 while (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
)
524 if (lp
[n
] != (LITTLENUM_TYPE
) - 1)
527 tmp_bits
-= LITTLENUM_NUMBER_OF_BITS
;
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
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))
545 for (carry
= 1; carry
&& (lp
>= words
); lp
--)
549 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
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. */
558 word1
= ((generic_floating_point_number
.sign
== '+')
560 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
562 << ((LITTLENUM_NUMBER_OF_BITS
- 1)
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);
572 while (lp
< words_end
)
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
]))
586 /* Exponent overflow. Lose immediately. */
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
);
596 word1
|= (exponent_4
<< ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
))
597 | next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
);
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
)
609 *lp
++ = (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)
610 | next_bits (LITTLENUM_NUMBER_OF_BITS
- 1));
613 /* The rest of the words are just mantissa bits. */
614 while (lp
< words_end
)
615 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
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.
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.
634 for (carry
= 1, lp
--; carry
; lp
--)
638 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
642 if (precision
== X_PRECISION
&& exponent_bits
== 15)
644 /* Extended precision numbers have an explicit integer bit
645 that we may have to restore. */
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];
655 /* Put back the integer bit. */
656 lp
[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
659 if ((word1
^ *words
) & (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)))
661 /* We leave return_value alone: admit we read the number,
662 but return a floating exception because we can't encode
664 *words
&= ~(1 << (LITTLENUM_NUMBER_OF_BITS
- 1));
676 LITTLENUM_TYPE arr
[10];
679 static char sbuf
[40];
683 f
= generic_floating_point_number
;
684 generic_floating_point_number
= *gen
;
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
);
694 generic_floating_point_number
= f
;
700 extern const char FLT_CHARS
[];
701 #define MAX_LITTLENUMS 6
703 /* This is a utility function called from various tc-*.c files. It
704 is here in order to reduce code duplication.
706 Turn a string at input_line_pointer into a floating point constant
707 of type TYPE (a character found in the FLT_CHARS macro), and store
708 it as LITTLENUMS in the bytes buffer LITP. The number of chars
709 emitted is stored in *SIZEP. BIG_WORDIAN is TRUE if the littlenums
710 should be emitted most significant littlenum first.
712 An error message is returned, or a NULL pointer if everything went OK. */
715 ieee_md_atof (int type
,
718 bfd_boolean big_wordian
)
720 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
721 LITTLENUM_TYPE
*wordP
;
725 if (strchr (FLT_CHARS
, type
) != NULL
)
746 type
= 'x'; /* This is what atof_ieee() understands. */
754 /* Note: on the m68k there is a gap of 16 bits (one littlenum)
755 between the exponent and mantissa. Hence the precision is
757 prec
= P_PRECISION
+ 1;
767 /* The 'f' and 'd' types are always recognised, even if the target has
768 not put them into the FLT_CHARS macro. This is because the 'f' type
769 can come from the .dc.s, .dcb.s, .float or .single pseudo-ops and the
770 'd' type from the .dc.d, .dbc.d or .double pseudo-ops.
772 The 'x' type is not implicitly recongised however, even though it can
773 be generated by the .dc.x and .dbc.x pseudo-ops because not all targets
774 can support floating point values that big. ie the target has to
775 explicitly allow them by putting them into FLT_CHARS. */
776 else if (type
== 'f')
778 else if (type
== 'd')
784 return _("Unrecognized or unsupported floating point constant");
787 gas_assert (prec
<= MAX_LITTLENUMS
);
789 t
= atof_ieee (input_line_pointer
, type
, words
);
791 input_line_pointer
= t
;
793 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
797 for (wordP
= words
; prec
--;)
799 md_number_to_chars (litP
, (valueT
) (* wordP
++), sizeof (LITTLENUM_TYPE
));
800 litP
+= sizeof (LITTLENUM_TYPE
);
805 for (wordP
= words
+ prec
; prec
--;)
807 md_number_to_chars (litP
, (valueT
) (* -- wordP
), sizeof (LITTLENUM_TYPE
));
808 litP
+= sizeof (LITTLENUM_TYPE
);