1 /* atof_ieee.c - turn a Flonum into an IEEE floating point number
2 Copyright (C) 1987, 1992 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 2, 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
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 extern FLONUM_TYPE generic_floating_point_number
; /* Flonums returned here. */
28 extern const char EXP_CHARS
[];
29 /* Precision in LittleNums. */
30 #define MAX_PRECISION (6)
31 #define F_PRECISION (2)
32 #define D_PRECISION (4)
33 #define X_PRECISION (6)
34 #define P_PRECISION (6)
36 /* Length in LittleNums of guard bits. */
39 static unsigned long mask
[] =
77 static int bits_left_in_littlenum
;
78 static int littlenums_left
;
79 static LITTLENUM_TYPE
*littlenum_pointer
;
82 next_bits (number_of_bits
)
89 if (number_of_bits
>= bits_left_in_littlenum
)
91 return_value
= mask
[bits_left_in_littlenum
] & *littlenum_pointer
;
92 number_of_bits
-= bits_left_in_littlenum
;
93 return_value
<<= number_of_bits
;
95 if (--littlenums_left
)
97 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
- number_of_bits
;
99 return_value
|= (*littlenum_pointer
>> bits_left_in_littlenum
) & mask
[number_of_bits
];
104 bits_left_in_littlenum
-= number_of_bits
;
105 return_value
= mask
[number_of_bits
] & (*littlenum_pointer
>> bits_left_in_littlenum
);
107 return (return_value
);
110 /* Num had better be less than LITTLENUM_NUMBER_OF_BITS */
115 if (!littlenums_left
)
119 bits_left_in_littlenum
= num
;
121 else if (bits_left_in_littlenum
+ num
> LITTLENUM_NUMBER_OF_BITS
)
123 bits_left_in_littlenum
= num
- (LITTLENUM_NUMBER_OF_BITS
- bits_left_in_littlenum
);
128 bits_left_in_littlenum
+= num
;
132 make_invalid_floating_point_number (words
)
133 LITTLENUM_TYPE
*words
;
135 as_bad ("cannot create floating-point number");
136 words
[0] = ((unsigned) -1) >> 1; /* Zero the leftmost bit */
144 /***********************************************************************\
145 * Warning: this returns 16-bit LITTLENUMs. It is up to the caller *
146 * to figure out any alignment problems and to conspire for the *
147 * bytes/word to be emitted in the right order. Bigendians beware! *
149 \***********************************************************************/
151 /* Note that atof-ieee always has X and P precisions enabled. it is up
152 to md_atof to filter them out if the target machine does not support
155 char * /* Return pointer past text consumed. */
156 atof_ieee (str
, what_kind
, words
)
157 char *str
; /* Text to convert to binary. */
158 char what_kind
; /* 'd', 'f', 'g', 'h' */
159 LITTLENUM_TYPE
*words
; /* Build the binary here. */
161 static LITTLENUM_TYPE bits
[MAX_PRECISION
+ MAX_PRECISION
+ GUARD
];
162 /* Extra bits for zeroed low-order bits. */
163 /* The 1st MAX_PRECISION are zeroed, */
164 /* the last contain flonum bits. */
166 int precision
; /* Number of 16-bit words in the format. */
168 FLONUM_TYPE save_gen_flonum
;
170 /* We have to save the generic_floating_point_number because it
171 contains storage allocation about the array of LITTLENUMs
172 where the value is actually stored. We will allocate our
173 own array of littlenums below, but have to restore the global
175 save_gen_flonum
= generic_floating_point_number
;
178 generic_floating_point_number
.low
= bits
+ MAX_PRECISION
;
179 generic_floating_point_number
.high
= NULL
;
180 generic_floating_point_number
.leader
= NULL
;
181 generic_floating_point_number
.exponent
= NULL
;
182 generic_floating_point_number
.sign
= '\0';
184 /* Use more LittleNums than seems */
185 /* necessary: the highest flonum may have */
186 /* 15 leading 0 bits, so could be useless. */
188 memset (bits
, '\0', sizeof (LITTLENUM_TYPE
) * MAX_PRECISION
);
196 precision
= F_PRECISION
;
204 precision
= D_PRECISION
;
212 precision
= X_PRECISION
;
219 precision
= P_PRECISION
;
224 make_invalid_floating_point_number (words
);
228 generic_floating_point_number
.high
= generic_floating_point_number
.low
+ precision
- 1 + GUARD
;
230 if (atof_generic (&return_value
, ".", EXP_CHARS
, &generic_floating_point_number
))
232 /* as_bad("Error converting floating point number (Exponent overflow?)"); */
233 make_invalid_floating_point_number (words
);
236 gen_to_words (words
, precision
, exponent_bits
);
238 /* Restore the generic_floating_point_number's storage alloc
239 (and everything else). */
240 generic_floating_point_number
= save_gen_flonum
;
242 return (return_value
);
245 /* Turn generic_floating_point_number into a real float/double/extended */
247 gen_to_words (words
, precision
, exponent_bits
)
248 LITTLENUM_TYPE
*words
;
252 int return_value
= 0;
258 int exponent_skippage
;
259 LITTLENUM_TYPE word1
;
262 if (generic_floating_point_number
.low
> generic_floating_point_number
.leader
)
265 if (generic_floating_point_number
.sign
== '+')
269 memset (&words
[1], '\0', sizeof (LITTLENUM_TYPE
) * (precision
- 1));
270 return (return_value
);
273 /* NaN: Do the right thing */
274 if (generic_floating_point_number
.sign
== 0)
276 if (precision
== F_PRECISION
)
290 else if (generic_floating_point_number
.sign
== 'P')
292 /* +INF: Do the right thing */
293 if (precision
== F_PRECISION
)
305 return (return_value
);
307 else if (generic_floating_point_number
.sign
== 'N')
310 if (precision
== F_PRECISION
)
322 return (return_value
);
325 * The floating point formats we support have:
326 * Bit 15 is sign bit.
327 * Bits 14:n are excess-whatever exponent.
328 * Bits n-1:0 (if any) are most significant bits of fraction.
329 * Bits 15:0 of the next word(s) are the next most significant bits.
331 * So we need: number of bits of exponent, number of bits of
334 bits_left_in_littlenum
= LITTLENUM_NUMBER_OF_BITS
;
335 littlenum_pointer
= generic_floating_point_number
.leader
;
336 littlenums_left
= 1 + generic_floating_point_number
.leader
- generic_floating_point_number
.low
;
337 /* Seek (and forget) 1st significant bit */
338 for (exponent_skippage
= 0; !next_bits (1); ++exponent_skippage
);;
339 exponent_1
= generic_floating_point_number
.exponent
+ generic_floating_point_number
.leader
340 + 1 - generic_floating_point_number
.low
;
341 /* Radix LITTLENUM_RADIX, point just higher than generic_floating_point_number.leader. */
342 exponent_2
= exponent_1
* LITTLENUM_NUMBER_OF_BITS
;
344 exponent_3
= exponent_2
- exponent_skippage
;
345 /* Forget leading zeros, forget 1st bit. */
346 exponent_4
= exponent_3
+ ((1 << (exponent_bits
- 1)) - 2);
347 /* Offset exponent. */
351 /* Word 1. Sign, exponent and perhaps high bits. */
352 word1
= (generic_floating_point_number
.sign
== '+') ? 0 : (1 << (LITTLENUM_NUMBER_OF_BITS
- 1));
354 /* Assume 2's complement integers. */
355 if (exponent_4
< 1 && exponent_4
>= -62)
361 num_bits
= -exponent_4
;
362 prec_bits
= LITTLENUM_NUMBER_OF_BITS
* precision
- (exponent_bits
+ 1 + num_bits
);
363 if (precision
== X_PRECISION
&& exponent_bits
== 15)
364 prec_bits
-= LITTLENUM_NUMBER_OF_BITS
+ 1;
366 if (num_bits
>= LITTLENUM_NUMBER_OF_BITS
- exponent_bits
)
368 /* Bigger than one littlenum */
369 num_bits
-= (LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
;
371 if (num_bits
+ exponent_bits
+ 1 >= precision
* LITTLENUM_NUMBER_OF_BITS
)
373 /* Exponent overflow */
374 make_invalid_floating_point_number (words
);
375 return (return_value
);
377 if (precision
== X_PRECISION
&& exponent_bits
== 15)
381 num_bits
-= LITTLENUM_NUMBER_OF_BITS
- 1;
383 while (num_bits
>= LITTLENUM_NUMBER_OF_BITS
)
385 num_bits
-= LITTLENUM_NUMBER_OF_BITS
;
389 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- (num_bits
));
393 if (precision
== X_PRECISION
&& exponent_bits
== 15)
397 if (num_bits
== LITTLENUM_NUMBER_OF_BITS
)
400 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- 1);
402 else if (num_bits
== LITTLENUM_NUMBER_OF_BITS
- 1)
405 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
- 1 - num_bits
);
410 word1
|= next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1) - (exponent_bits
+ num_bits
));
414 while (lp
< words
+ precision
)
415 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
417 /* Round the mantissa up, but don't change the number */
421 if (prec_bits
> LITTLENUM_NUMBER_OF_BITS
)
427 tmp_bits
= prec_bits
;
428 while (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
)
430 if (lp
[n
] != (LITTLENUM_TYPE
) - 1)
433 tmp_bits
-= LITTLENUM_NUMBER_OF_BITS
;
435 if (tmp_bits
> LITTLENUM_NUMBER_OF_BITS
|| (lp
[n
] & mask
[tmp_bits
]) != mask
[tmp_bits
])
439 for (carry
= 1; carry
&& (lp
>= words
); lp
--)
443 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
447 else if ((*lp
& mask
[prec_bits
]) != mask
[prec_bits
])
453 else if (exponent_4
& ~mask
[exponent_bits
])
456 * Exponent overflow. Lose immediately.
460 * We leave return_value alone: admit we read the
461 * number, but return a floating exception
462 * because we can't encode the number.
464 make_invalid_floating_point_number (words
);
469 word1
|= (exponent_4
<< ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
))
470 | next_bits ((LITTLENUM_NUMBER_OF_BITS
- 1) - exponent_bits
);
475 /* X_PRECISION is special: it has 16 bits of zero in the middle,
476 followed by a 1 bit. */
477 if (exponent_bits
== 15 && precision
== X_PRECISION
)
480 *lp
++ = 1 << (LITTLENUM_NUMBER_OF_BITS
) | next_bits (LITTLENUM_NUMBER_OF_BITS
- 1);
483 /* The rest of the words are just mantissa bits. */
484 while (lp
< words
+ precision
)
485 *lp
++ = next_bits (LITTLENUM_NUMBER_OF_BITS
);
491 * Since the NEXT bit is a 1, round UP the mantissa.
492 * The cunning design of these hidden-1 floats permits
493 * us to let the mantissa overflow into the exponent, and
494 * it 'does the right thing'. However, we lose if the
495 * highest-order bit of the lowest-order word flips.
499 /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2)
500 Please allow at least 1 more bit in carry than is in a LITTLENUM.
501 We need that extra bit to hold a carry during a LITTLENUM carry
502 propagation. Another extra bit (kept 0) will assure us that we
503 don't get a sticky sign bit after shifting right, and that
504 permits us to propagate the carry without any masking of bits.
506 for (carry
= 1, lp
--; carry
&& (lp
>= words
); lp
--)
510 carry
>>= LITTLENUM_NUMBER_OF_BITS
;
512 if ((word1
^ *words
) & (1 << (LITTLENUM_NUMBER_OF_BITS
- 1)))
514 /* We leave return_value alone: admit we read the
515 * number, but return a floating exception
516 * because we can't encode the number.
518 *words
&= ~(1 << (LITTLENUM_NUMBER_OF_BITS
- 1));
519 /* make_invalid_floating_point_number (words); */
520 /* return return_value; */
523 return (return_value
);
526 /* This routine is a real kludge. Someone really should do it better, but
527 I'm too lazy, and I don't understand this stuff all too well anyway
537 sprintf (buf
, "%ld", x
);
539 if (atof_generic (&bufp
, ".", EXP_CHARS
, &generic_floating_point_number
))
540 as_bad ("Error converting number to floating point (Exponent overflow?)");
549 LITTLENUM_TYPE arr
[10];
552 static char sbuf
[40];
556 f
= generic_floating_point_number
;
557 generic_floating_point_number
= *gen
;
559 gen_to_words (&arr
[0], 4, 11);
560 memcpy (&dv
, &arr
[0], sizeof (double));
561 sprintf (sbuf
, "%x %x %x %x %.14G ", arr
[0], arr
[1], arr
[2], arr
[3], dv
);
562 gen_to_words (&arr
[0], 2, 8);
563 memcpy (&fv
, &arr
[0], sizeof (float));
564 sprintf (sbuf
+ strlen (sbuf
), "%x %x %.12g\n", arr
[0], arr
[1], fv
);
568 generic_floating_point_number
= f
;
576 /* end of atof-ieee.c */