1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number
2 Copyright 1987, 1992, 1994, 1996, 1997, 1998, 1999, 2000, 2001
3 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 2, 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, 59 Temple Place - Suite 330, Boston, MA
24 /* Flonums returned here. */
25 extern FLONUM_TYPE generic_floating_point_number
;
27 static int next_bits
PARAMS ((int));
28 static void unget_bits
PARAMS ((int));
29 static void make_invalid_floating_point_number
PARAMS ((LITTLENUM_TYPE
*));
31 extern const char EXP_CHARS
[];
32 /* Precision in LittleNums. */
33 /* Don't count the gap in the m68k extended precision format. */
34 #define MAX_PRECISION (5)
35 #define F_PRECISION (2)
36 #define D_PRECISION (4)
37 #define X_PRECISION (5)
38 #define P_PRECISION (5)
40 /* Length in LittleNums of guard bits. */
43 #ifndef TC_LARGEST_EXPONENT_IS_NORMAL
44 #define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) 0
47 static const unsigned long mask
[] =
84 static int bits_left_in_littlenum
;
85 static int littlenums_left
;
86 static LITTLENUM_TYPE
*littlenum_pointer
;
89 next_bits (number_of_bits
)
96 if (number_of_bits
>= bits_left_in_littlenum
)
98 return_value
= mask
[bits_left_in_littlenum
] & *littlenum_pointer
;
99 number_of_bits
-= bits_left_in_littlenum
;
100 return_value
<<= number_of_bits
;
102 if (--littlenums_left
)
104 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
- number_of_bits
;
107 (*littlenum_pointer
>> bits_left_in_littlenum
)
108 & mask
[number_of_bits
];
113 bits_left_in_littlenum
-= number_of_bits
;
115 mask
[number_of_bits
] & (*littlenum_pointer
>> bits_left_in_littlenum
);
120 /* Num had better be less than LITTLENUM_NUMBER_OF_BITS. */
126 if (!littlenums_left
)
130 bits_left_in_littlenum
= num
;
132 else if (bits_left_in_littlenum
+ num
> LITTLENUM_NUMBER_OF_BITS
)
134 bits_left_in_littlenum
=
135 num
- (LITTLENUM_NUMBER_OF_BITS
- bits_left_in_littlenum
);
140 bits_left_in_littlenum
+= num
;
144 make_invalid_floating_point_number (words
)
145 LITTLENUM_TYPE
*words
;
147 as_bad (_("cannot create floating-point number"));
148 /* Zero the leftmost bit. */
149 words
[0] = (LITTLENUM_TYPE
) ((unsigned) -1) >> 1;
150 words
[1] = (LITTLENUM_TYPE
) -1;
151 words
[2] = (LITTLENUM_TYPE
) -1;
152 words
[3] = (LITTLENUM_TYPE
) -1;
153 words
[4] = (LITTLENUM_TYPE
) -1;
154 words
[5] = (LITTLENUM_TYPE
) -1;
157 /* Warning: This returns 16-bit LITTLENUMs. It is up to the caller to
158 figure out any alignment problems and to conspire for the
159 bytes/word to be emitted in the right order. Bigendians beware! */
161 /* Note that atof-ieee always has X and P precisions enabled. it is up
162 to md_atof to filter them out if the target machine does not support
165 /* Returns pointer past text consumed. */
168 atof_ieee (str
, what_kind
, words
)
169 char *str
; /* Text to convert to binary. */
170 int what_kind
; /* 'd', 'f', 'g', 'h'. */
171 LITTLENUM_TYPE
*words
; /* Build the binary here. */
173 /* Extra bits for zeroed low-order bits.
174 The 1st MAX_PRECISION are zeroed, the last contain flonum bits. */
175 static LITTLENUM_TYPE bits
[MAX_PRECISION
+ MAX_PRECISION
+ GUARD
];
177 /* Number of 16-bit words in the format. */
180 FLONUM_TYPE save_gen_flonum
;
182 /* We have to save the generic_floating_point_number because it
183 contains storage allocation about the array of LITTLENUMs where
184 the value is actually stored. We will allocate our own array of
185 littlenums below, but have to restore the global one on exit. */
186 save_gen_flonum
= generic_floating_point_number
;
189 generic_floating_point_number
.low
= bits
+ MAX_PRECISION
;
190 generic_floating_point_number
.high
= NULL
;
191 generic_floating_point_number
.leader
= NULL
;
192 generic_floating_point_number
.exponent
= 0;
193 generic_floating_point_number
.sign
= '\0';
195 /* Use more LittleNums than seems necessary: the highest flonum may
196 have 15 leading 0 bits, so could be useless. */
198 memset (bits
, '\0', sizeof (LITTLENUM_TYPE
) * MAX_PRECISION
);
206 precision
= F_PRECISION
;
214 precision
= D_PRECISION
;
222 precision
= X_PRECISION
;
229 precision
= P_PRECISION
;
234 make_invalid_floating_point_number (words
);
238 generic_floating_point_number
.high
239 = generic_floating_point_number
.low
+ precision
- 1 + GUARD
;
241 if (atof_generic (&return_value
, ".", EXP_CHARS
,
242 &generic_floating_point_number
))
244 make_invalid_floating_point_number (words
);
247 gen_to_words (words
, precision
, exponent_bits
);
249 /* Restore the generic_floating_point_number's storage alloc (and
251 generic_floating_point_number
= save_gen_flonum
;
256 /* Turn generic_floating_point_number into a real float/double/extended. */
259 gen_to_words (words
, precision
, exponent_bits
)
260 LITTLENUM_TYPE
*words
;
264 int return_value
= 0;
270 int exponent_skippage
;
271 LITTLENUM_TYPE word1
;
273 LITTLENUM_TYPE
*words_end
;
275 words_end
= words
+ precision
;
277 if (precision
== X_PRECISION
)
278 /* On the m68k the extended precision format has a gap of 16 bits
279 between the exponent and the mantissa. */
283 if (generic_floating_point_number
.low
> generic_floating_point_number
.leader
)
286 if (generic_floating_point_number
.sign
== '+')
290 memset (&words
[1], '\0',
291 (words_end
- words
- 1) * sizeof (LITTLENUM_TYPE
));
295 /* NaN: Do the right thing. */
296 if (generic_floating_point_number
.sign
== 0)
298 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
299 as_warn ("NaNs are not supported by this target\n");
300 if (precision
== F_PRECISION
)
305 else if (precision
== X_PRECISION
)
314 #else /* ! TC_M68K */
321 #else /* ! TC_I386 */
323 #endif /* ! TC_I386 */
324 #endif /* ! TC_M68K */
335 else if (generic_floating_point_number
.sign
== 'P')
337 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
338 as_warn ("Infinities are not supported by this target\n");
340 /* +INF: Do the right thing. */
341 if (precision
== F_PRECISION
)
346 else if (precision
== X_PRECISION
)
355 #else /* ! TC_M68K */
362 #else /* ! TC_I386 */
364 #endif /* ! TC_I386 */
365 #endif /* ! TC_M68K */
376 else if (generic_floating_point_number
.sign
== 'N')
378 if (TC_LARGEST_EXPONENT_IS_NORMAL (precision
))
379 as_warn ("Infinities are not supported by this target\n");
382 if (precision
== F_PRECISION
)
387 else if (precision
== X_PRECISION
)
396 #else /* ! TC_M68K */
403 #else /* ! TC_I386 */
405 #endif /* ! TC_I386 */
406 #endif /* ! TC_M68K */
418 /* The floating point formats we support have:
420 Bits 14:n are excess-whatever exponent.
421 Bits n-1:0 (if any) are most significant bits of fraction.
422 Bits 15:0 of the next word(s) are the next most significant bits.
424 So we need: number of bits of exponent, number of bits of
426 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
;
427 littlenum_pointer
= generic_floating_point_number
.leader
;
429 + generic_floating_point_number
.leader
430 - generic_floating_point_number
.low
);
432 /* Seek (and forget) 1st significant bit. */
433 for (exponent_skippage
= 0; !next_bits (1); ++exponent_skippage
);;
434 exponent_1
= (generic_floating_point_number
.exponent
435 + generic_floating_point_number
.leader
437 - generic_floating_point_number
.low
);
439 /* Radix LITTLENUM_RADIX, point just higher than
440 generic_floating_point_number.leader. */
441 exponent_2
= exponent_1
* LITTLENUM_NUMBER_OF_BITS
;
444 exponent_3
= exponent_2
- exponent_skippage
;
446 /* Forget leading zeros, forget 1st bit. */
447 exponent_4
= exponent_3
+ ((1 << (exponent_bits
- 1)) - 2);
449 /* Offset exponent. */
452 /* Word 1. Sign, exponent and perhaps high bits. */
453 word1
= ((generic_floating_point_number
.sign
== '+')
455 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
457 /* Assume 2's complement integers. */
464 num_bits
= -exponent_4
;
466 LITTLENUM_NUMBER_OF_BITS
* precision
- (exponent_bits
+ 1 + num_bits
);
468 if (precision
== X_PRECISION
&& exponent_bits
== 15)
470 /* On the i386 a denormalized extended precision float is
471 shifted down by one, effectively decreasing the exponent
478 if (num_bits
>= LITTLENUM_NUMBER_OF_BITS
- exponent_bits
)
480 /* Bigger than one littlenum. */
481 num_bits
-= (LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
;
483 if (num_bits
+ exponent_bits
+ 1
484 > precision
* LITTLENUM_NUMBER_OF_BITS
)
486 /* Exponent overflow. */
487 make_invalid_floating_point_number (words
);
491 if (precision
== X_PRECISION
&& exponent_bits
== 15)
494 while (num_bits
>= LITTLENUM_NUMBER_OF_BITS
)
496 num_bits
-= LITTLENUM_NUMBER_OF_BITS
;
500 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- (num_bits
));
504 if (precision
== X_PRECISION
&& exponent_bits
== 15)
510 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- num_bits
);
514 word1
|= next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1)
515 - (exponent_bits
+ num_bits
));
519 while (lp
< words_end
)
520 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
522 /* Round the mantissa up, but don't change the number. */
526 if (prec_bits
>= LITTLENUM_NUMBER_OF_BITS
)
532 tmp_bits
= prec_bits
;
533 while (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
)
535 if (lp
[n
] != (LITTLENUM_TYPE
) - 1)
538 tmp_bits
-= LITTLENUM_NUMBER_OF_BITS
;
540 if (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
541 || (lp
[n
] & mask
[tmp_bits
]) != mask
[tmp_bits
]
542 || (prec_bits
!= (precision
* LITTLENUM_NUMBER_OF_BITS
545 /* An extended precision float with only the integer
546 bit set would be invalid. That must be converted
547 to the smallest normalized number. */
548 && !(precision
== X_PRECISION
549 && prec_bits
== (precision
* LITTLENUM_NUMBER_OF_BITS
550 - exponent_bits
- 2))
556 for (carry
= 1; carry
&& (lp
>= words
); lp
--)
560 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
565 /* This is an overflow of the denormal numbers. We
566 need to forget what we have produced, and instead
567 generate the smallest normalized number. */
569 word1
= ((generic_floating_point_number
.sign
== '+')
571 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
573 << ((LITTLENUM_NUMBER_OF_BITS
- 1)
577 /* Set the integer bit in the extended precision format.
578 This cannot happen on the m68k where the mantissa
579 just overflows into the integer bit above. */
580 if (precision
== X_PRECISION
)
581 *lp
++ = 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
583 while (lp
< words_end
)
593 else if ((unsigned long) exponent_4
> mask
[exponent_bits
]
594 || (! TC_LARGEST_EXPONENT_IS_NORMAL (precision
)
595 && (unsigned long) exponent_4
== mask
[exponent_bits
]))
597 /* Exponent overflow. Lose immediately. */
599 /* We leave return_value alone: admit we read the
600 number, but return a floating exception
601 because we can't encode the number. */
602 make_invalid_floating_point_number (words
);
607 word1
|= (exponent_4
<< ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
))
608 | next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
);
613 /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the
614 middle. Either way, it is then followed by a 1 bit. */
615 if (exponent_bits
== 15 && precision
== X_PRECISION
)
620 *lp
++ = (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)
621 | next_bits (LITTLENUM_NUMBER_OF_BITS
- 1));
624 /* The rest of the words are just mantissa bits. */
625 while (lp
< words_end
)
626 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
631 /* Since the NEXT bit is a 1, round UP the mantissa.
632 The cunning design of these hidden-1 floats permits
633 us to let the mantissa overflow into the exponent, and
634 it 'does the right thing'. However, we lose if the
635 highest-order bit of the lowest-order word flips.
638 /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
639 Please allow at least 1 more bit in carry than is in a LITTLENUM.
640 We need that extra bit to hold a carry during a LITTLENUM carry
641 propagation. Another extra bit (kept 0) will assure us that we
642 don't get a sticky sign bit after shifting right, and that
643 permits us to propagate the carry without any masking of bits.
645 for (carry
= 1, lp
--; carry
; lp
--)
649 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
653 if (precision
== X_PRECISION
&& exponent_bits
== 15)
655 /* Extended precision numbers have an explicit integer bit
656 that we may have to restore. */
660 /* On the m68k there is a gap of 16 bits. We must
661 explicitly propagate the carry into the exponent. */
662 words
[0] += words
[1];
666 /* Put back the integer bit. */
667 lp
[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
670 if ((word1
^ *words
) & (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)))
672 /* We leave return_value alone: admit we read the number,
673 but return a floating exception because we can't encode
675 *words
&= ~(1 << (LITTLENUM_NUMBER_OF_BITS
- 1));
677 make_invalid_floating_point_number (words
);
687 /* This routine is a real kludge. Someone really should do it better,
688 but I'm too lazy, and I don't understand this stuff all too well
698 sprintf (buf
, "%ld", x
);
700 if (atof_generic (&bufp
, ".", EXP_CHARS
, &generic_floating_point_number
))
701 as_bad (_("Error converting number to floating point (Exponent overflow?)"));
711 LITTLENUM_TYPE arr
[10];
714 static char sbuf
[40];
718 f
= generic_floating_point_number
;
719 generic_floating_point_number
= *gen
;
721 gen_to_words (&arr
[0], 4, 11);
722 memcpy (&dv
, &arr
[0], sizeof (double));
723 sprintf (sbuf
, "%x %x %x %x %.14G ", arr
[0], arr
[1], arr
[2], arr
[3], dv
);
724 gen_to_words (&arr
[0], 2, 8);
725 memcpy (&fv
, &arr
[0], sizeof (float));
726 sprintf (sbuf
+ strlen (sbuf
), "%x %x %.12g\n", arr
[0], arr
[1], fv
);
729 generic_floating_point_number
= f
;