1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number
2 Copyright (C) 1987, 92, 93, 94, 95, 96, 97, 98, 99, 2000
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 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 (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. */
122 if (!littlenums_left
)
126 bits_left_in_littlenum
= num
;
128 else if (bits_left_in_littlenum
+ num
> LITTLENUM_NUMBER_OF_BITS
)
130 bits_left_in_littlenum
=
131 num
- (LITTLENUM_NUMBER_OF_BITS
- bits_left_in_littlenum
);
136 bits_left_in_littlenum
+= num
;
140 make_invalid_floating_point_number (words
)
141 LITTLENUM_TYPE
*words
;
143 as_bad (_("cannot create floating-point number"));
144 /* Zero the leftmost bit. */
145 words
[0] = (LITTLENUM_TYPE
) ((unsigned) -1) >> 1;
146 words
[1] = (LITTLENUM_TYPE
) -1;
147 words
[2] = (LITTLENUM_TYPE
) -1;
148 words
[3] = (LITTLENUM_TYPE
) -1;
149 words
[4] = (LITTLENUM_TYPE
) -1;
150 words
[5] = (LITTLENUM_TYPE
) -1;
153 /* Warning: This returns 16-bit LITTLENUMs. It is up to the caller to
154 figure out any alignment problems and to conspire for the
155 bytes/word to be emitted in the right order. Bigendians beware! */
157 /* Note that atof-ieee always has X and P precisions enabled. it is up
158 to md_atof to filter them out if the target machine does not support
161 /* Returns pointer past text consumed. */
164 atof_ieee (str
, what_kind
, words
)
165 char *str
; /* Text to convert to binary. */
166 int what_kind
; /* 'd', 'f', 'g', 'h'. */
167 LITTLENUM_TYPE
*words
; /* Build the binary here. */
169 /* Extra bits for zeroed low-order bits.
170 The 1st MAX_PRECISION are zeroed, the last contain flonum bits. */
171 static LITTLENUM_TYPE bits
[MAX_PRECISION
+ MAX_PRECISION
+ GUARD
];
173 /* Number of 16-bit words in the format. */
176 FLONUM_TYPE save_gen_flonum
;
178 /* We have to save the generic_floating_point_number because it
179 contains storage allocation about the array of LITTLENUMs where
180 the value is actually stored. We will allocate our own array of
181 littlenums below, but have to restore the global one on exit. */
182 save_gen_flonum
= generic_floating_point_number
;
185 generic_floating_point_number
.low
= bits
+ MAX_PRECISION
;
186 generic_floating_point_number
.high
= NULL
;
187 generic_floating_point_number
.leader
= NULL
;
188 generic_floating_point_number
.exponent
= 0;
189 generic_floating_point_number
.sign
= '\0';
191 /* Use more LittleNums than seems necessary: the highest flonum may
192 have 15 leading 0 bits, so could be useless. */
194 memset (bits
, '\0', sizeof (LITTLENUM_TYPE
) * MAX_PRECISION
);
202 precision
= F_PRECISION
;
210 precision
= D_PRECISION
;
218 precision
= X_PRECISION
;
225 precision
= P_PRECISION
;
230 make_invalid_floating_point_number (words
);
234 generic_floating_point_number
.high
235 = generic_floating_point_number
.low
+ precision
- 1 + GUARD
;
237 if (atof_generic (&return_value
, ".", EXP_CHARS
,
238 &generic_floating_point_number
))
240 make_invalid_floating_point_number (words
);
243 gen_to_words (words
, precision
, exponent_bits
);
245 /* Restore the generic_floating_point_number's storage alloc (and
247 generic_floating_point_number
= save_gen_flonum
;
252 /* Turn generic_floating_point_number into a real float/double/extended. */
255 gen_to_words (words
, precision
, exponent_bits
)
256 LITTLENUM_TYPE
*words
;
260 int return_value
= 0;
266 int exponent_skippage
;
267 LITTLENUM_TYPE word1
;
269 LITTLENUM_TYPE
*words_end
;
271 words_end
= words
+ precision
;
273 if (precision
== X_PRECISION
)
274 /* On the m68k the extended precision format has a gap of 16 bits
275 between the exponent and the mantissa. */
279 if (generic_floating_point_number
.low
> generic_floating_point_number
.leader
)
282 if (generic_floating_point_number
.sign
== '+')
286 memset (&words
[1], '\0',
287 (words_end
- words
- 1) * sizeof (LITTLENUM_TYPE
));
291 /* NaN: Do the right thing. */
292 if (generic_floating_point_number
.sign
== 0)
294 if (precision
== F_PRECISION
)
299 else if (precision
== X_PRECISION
)
308 #else /* ! TC_M68K */
315 #else /* ! TC_I386 */
317 #endif /* ! TC_I386 */
318 #endif /* ! TC_M68K */
329 else if (generic_floating_point_number
.sign
== 'P')
331 /* +INF: Do the right thing. */
332 if (precision
== F_PRECISION
)
337 else if (precision
== X_PRECISION
)
346 #else /* ! TC_M68K */
353 #else /* ! TC_I386 */
355 #endif /* ! TC_I386 */
356 #endif /* ! TC_M68K */
367 else if (generic_floating_point_number
.sign
== 'N')
370 if (precision
== F_PRECISION
)
375 else if (precision
== X_PRECISION
)
384 #else /* ! TC_M68K */
391 #else /* ! TC_I386 */
393 #endif /* ! TC_I386 */
394 #endif /* ! TC_M68K */
406 /* The floating point formats we support have:
408 Bits 14:n are excess-whatever exponent.
409 Bits n-1:0 (if any) are most significant bits of fraction.
410 Bits 15:0 of the next word(s) are the next most significant bits.
412 So we need: number of bits of exponent, number of bits of
414 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
;
415 littlenum_pointer
= generic_floating_point_number
.leader
;
417 + generic_floating_point_number
.leader
418 - generic_floating_point_number
.low
);
420 /* Seek (and forget) 1st significant bit. */
421 for (exponent_skippage
= 0; !next_bits (1); ++exponent_skippage
);;
422 exponent_1
= (generic_floating_point_number
.exponent
423 + generic_floating_point_number
.leader
425 - generic_floating_point_number
.low
);
427 /* Radix LITTLENUM_RADIX, point just higher than
428 generic_floating_point_number.leader. */
429 exponent_2
= exponent_1
* LITTLENUM_NUMBER_OF_BITS
;
432 exponent_3
= exponent_2
- exponent_skippage
;
434 /* Forget leading zeros, forget 1st bit. */
435 exponent_4
= exponent_3
+ ((1 << (exponent_bits
- 1)) - 2);
437 /* Offset exponent. */
440 /* Word 1. Sign, exponent and perhaps high bits. */
441 word1
= ((generic_floating_point_number
.sign
== '+')
443 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
445 /* Assume 2's complement integers. */
452 num_bits
= -exponent_4
;
454 LITTLENUM_NUMBER_OF_BITS
* precision
- (exponent_bits
+ 1 + num_bits
);
456 if (precision
== X_PRECISION
&& exponent_bits
== 15)
458 /* On the i386 a denormalized extended precision float is
459 shifted down by one, effectively decreasing the exponent
466 if (num_bits
>= LITTLENUM_NUMBER_OF_BITS
- exponent_bits
)
468 /* Bigger than one littlenum. */
469 num_bits
-= (LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
;
471 if (num_bits
+ exponent_bits
+ 1
472 > precision
* LITTLENUM_NUMBER_OF_BITS
)
474 /* Exponent overflow. */
475 make_invalid_floating_point_number (words
);
479 if (precision
== X_PRECISION
&& exponent_bits
== 15)
482 while (num_bits
>= LITTLENUM_NUMBER_OF_BITS
)
484 num_bits
-= LITTLENUM_NUMBER_OF_BITS
;
488 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- (num_bits
));
492 if (precision
== X_PRECISION
&& exponent_bits
== 15)
498 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- num_bits
);
502 word1
|= next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1)
503 - (exponent_bits
+ num_bits
));
507 while (lp
< words_end
)
508 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
510 /* Round the mantissa up, but don't change the number. */
514 if (prec_bits
>= LITTLENUM_NUMBER_OF_BITS
)
520 tmp_bits
= prec_bits
;
521 while (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
)
523 if (lp
[n
] != (LITTLENUM_TYPE
) - 1)
526 tmp_bits
-= LITTLENUM_NUMBER_OF_BITS
;
528 if (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
529 || (lp
[n
] & mask
[tmp_bits
]) != mask
[tmp_bits
]
530 || (prec_bits
!= (precision
* LITTLENUM_NUMBER_OF_BITS
533 /* An extended precision float with only the integer
534 bit set would be invalid. That must be converted
535 to the smallest normalized number. */
536 && !(precision
== X_PRECISION
537 && prec_bits
== (precision
* LITTLENUM_NUMBER_OF_BITS
538 - exponent_bits
- 2))
544 for (carry
= 1; carry
&& (lp
>= words
); lp
--)
548 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
553 /* This is an overflow of the denormal numbers. We
554 need to forget what we have produced, and instead
555 generate the smallest normalized number. */
557 word1
= ((generic_floating_point_number
.sign
== '+')
559 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)));
561 << ((LITTLENUM_NUMBER_OF_BITS
- 1)
565 /* Set the integer bit in the extended precision format.
566 This cannot happen on the m68k where the mantissa
567 just overflows into the integer bit above. */
568 if (precision
== X_PRECISION
)
569 *lp
++ = 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
571 while (lp
< words_end
)
581 else if ((unsigned long) exponent_4
>= mask
[exponent_bits
])
583 /* Exponent overflow. Lose immediately. */
585 /* We leave return_value alone: admit we read the
586 number, but return a floating exception
587 because we can't encode the number. */
588 make_invalid_floating_point_number (words
);
593 word1
|= (exponent_4
<< ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
))
594 | next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
);
599 /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the
600 middle. Either way, it is then followed by a 1 bit. */
601 if (exponent_bits
== 15 && precision
== X_PRECISION
)
606 *lp
++ = (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)
607 | next_bits (LITTLENUM_NUMBER_OF_BITS
- 1));
610 /* The rest of the words are just mantissa bits. */
611 while (lp
< words_end
)
612 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
617 /* Since the NEXT bit is a 1, round UP the mantissa.
618 The cunning design of these hidden-1 floats permits
619 us to let the mantissa overflow into the exponent, and
620 it 'does the right thing'. However, we lose if the
621 highest-order bit of the lowest-order word flips.
624 /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
625 Please allow at least 1 more bit in carry than is in a LITTLENUM.
626 We need that extra bit to hold a carry during a LITTLENUM carry
627 propagation. Another extra bit (kept 0) will assure us that we
628 don't get a sticky sign bit after shifting right, and that
629 permits us to propagate the carry without any masking of bits.
631 for (carry
= 1, lp
--; carry
; lp
--)
635 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
639 if (precision
== X_PRECISION
&& exponent_bits
== 15)
641 /* Extended precision numbers have an explicit integer bit
642 that we may have to restore. */
646 /* On the m68k there is a gap of 16 bits. We must
647 explicitly propagate the carry into the exponent. */
648 words
[0] += words
[1];
652 /* Put back the integer bit. */
653 lp
[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS
- 1);
656 if ((word1
^ *words
) & (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)))
658 /* We leave return_value alone: admit we read the number,
659 but return a floating exception because we can't encode
661 *words
&= ~(1 << (LITTLENUM_NUMBER_OF_BITS
- 1));
663 make_invalid_floating_point_number (words
);
673 /* This routine is a real kludge. Someone really should do it better,
674 but I'm too lazy, and I don't understand this stuff all too well
684 sprintf (buf
, "%ld", x
);
686 if (atof_generic (&bufp
, ".", EXP_CHARS
, &generic_floating_point_number
))
687 as_bad (_("Error converting number to floating point (Exponent overflow?)"));
697 LITTLENUM_TYPE arr
[10];
700 static char sbuf
[40];
704 f
= generic_floating_point_number
;
705 generic_floating_point_number
= *gen
;
707 gen_to_words (&arr
[0], 4, 11);
708 memcpy (&dv
, &arr
[0], sizeof (double));
709 sprintf (sbuf
, "%x %x %x %x %.14G ", arr
[0], arr
[1], arr
[2], arr
[3], dv
);
710 gen_to_words (&arr
[0], 2, 8);
711 memcpy (&fv
, &arr
[0], sizeof (float));
712 sprintf (sbuf
+ strlen (sbuf
), "%x %x %.12g\n", arr
[0], arr
[1], fv
);
715 generic_floating_point_number
= f
;