Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* atof_ieee.c - turn a Flonum into an IEEE floating point number |
6f2750fe | 2 | Copyright (C) 1987-2016 Free Software Foundation, Inc. |
252b5132 RH |
3 | |
4 | This file is part of GAS, the GNU Assembler. | |
5 | ||
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 | |
ec2655a6 | 8 | the Free Software Foundation; either version 3, or (at your option) |
252b5132 RH |
9 | any later version. |
10 | ||
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. | |
15 | ||
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 | |
4b4da160 NC |
18 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA |
19 | 02110-1301, USA. */ | |
252b5132 RH |
20 | |
21 | #include "as.h" | |
22 | ||
23 | /* Flonums returned here. */ | |
24 | extern FLONUM_TYPE generic_floating_point_number; | |
25 | ||
252b5132 | 26 | extern const char EXP_CHARS[]; |
2d484c7f | 27 | /* Precision in LittleNums. */ |
252b5132 | 28 | /* Don't count the gap in the m68k extended precision format. */ |
ea1562b3 NC |
29 | #define MAX_PRECISION 5 |
30 | #define F_PRECISION 2 | |
31 | #define D_PRECISION 4 | |
32 | #define X_PRECISION 5 | |
33 | #define P_PRECISION 5 | |
252b5132 | 34 | |
2d484c7f | 35 | /* Length in LittleNums of guard bits. */ |
ea1562b3 | 36 | #define GUARD 2 |
252b5132 | 37 | |
3a18fa4f | 38 | #ifndef TC_LARGEST_EXPONENT_IS_NORMAL |
580a832e | 39 | #define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) 0 |
e103941e NC |
40 | #endif |
41 | ||
252b5132 RH |
42 | static const unsigned long mask[] = |
43 | { | |
44 | 0x00000000, | |
45 | 0x00000001, | |
46 | 0x00000003, | |
47 | 0x00000007, | |
48 | 0x0000000f, | |
49 | 0x0000001f, | |
50 | 0x0000003f, | |
51 | 0x0000007f, | |
52 | 0x000000ff, | |
53 | 0x000001ff, | |
54 | 0x000003ff, | |
55 | 0x000007ff, | |
56 | 0x00000fff, | |
57 | 0x00001fff, | |
58 | 0x00003fff, | |
59 | 0x00007fff, | |
60 | 0x0000ffff, | |
61 | 0x0001ffff, | |
62 | 0x0003ffff, | |
63 | 0x0007ffff, | |
64 | 0x000fffff, | |
65 | 0x001fffff, | |
66 | 0x003fffff, | |
67 | 0x007fffff, | |
68 | 0x00ffffff, | |
69 | 0x01ffffff, | |
70 | 0x03ffffff, | |
71 | 0x07ffffff, | |
72 | 0x0fffffff, | |
73 | 0x1fffffff, | |
74 | 0x3fffffff, | |
75 | 0x7fffffff, | |
76 | 0xffffffff, | |
77 | }; | |
78 | \f | |
252b5132 RH |
79 | static int bits_left_in_littlenum; |
80 | static int littlenums_left; | |
81 | static LITTLENUM_TYPE *littlenum_pointer; | |
82 | ||
83 | static int | |
ea1562b3 | 84 | next_bits (int number_of_bits) |
252b5132 RH |
85 | { |
86 | int return_value; | |
87 | ||
88 | if (!littlenums_left) | |
ea1562b3 NC |
89 | return 0; |
90 | ||
252b5132 RH |
91 | if (number_of_bits >= bits_left_in_littlenum) |
92 | { | |
93 | return_value = mask[bits_left_in_littlenum] & *littlenum_pointer; | |
94 | number_of_bits -= bits_left_in_littlenum; | |
95 | return_value <<= number_of_bits; | |
96 | ||
97 | if (--littlenums_left) | |
98 | { | |
99 | bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS - number_of_bits; | |
100 | --littlenum_pointer; | |
2d484c7f KH |
101 | return_value |= |
102 | (*littlenum_pointer >> bits_left_in_littlenum) | |
103 | & mask[number_of_bits]; | |
252b5132 RH |
104 | } |
105 | } | |
106 | else | |
107 | { | |
108 | bits_left_in_littlenum -= number_of_bits; | |
2d484c7f KH |
109 | return_value = |
110 | mask[number_of_bits] & (*littlenum_pointer >> bits_left_in_littlenum); | |
252b5132 | 111 | } |
2d484c7f | 112 | return return_value; |
252b5132 RH |
113 | } |
114 | ||
2d484c7f KH |
115 | /* Num had better be less than LITTLENUM_NUMBER_OF_BITS. */ |
116 | ||
252b5132 | 117 | static void |
ea1562b3 | 118 | unget_bits (int num) |
252b5132 RH |
119 | { |
120 | if (!littlenums_left) | |
121 | { | |
122 | ++littlenum_pointer; | |
123 | ++littlenums_left; | |
124 | bits_left_in_littlenum = num; | |
125 | } | |
126 | else if (bits_left_in_littlenum + num > LITTLENUM_NUMBER_OF_BITS) | |
127 | { | |
2d484c7f KH |
128 | bits_left_in_littlenum = |
129 | num - (LITTLENUM_NUMBER_OF_BITS - bits_left_in_littlenum); | |
252b5132 RH |
130 | ++littlenum_pointer; |
131 | ++littlenums_left; | |
132 | } | |
133 | else | |
134 | bits_left_in_littlenum += num; | |
135 | } | |
136 | ||
137 | static void | |
ea1562b3 | 138 | make_invalid_floating_point_number (LITTLENUM_TYPE *words) |
252b5132 RH |
139 | { |
140 | as_bad (_("cannot create floating-point number")); | |
2d484c7f KH |
141 | /* Zero the leftmost bit. */ |
142 | words[0] = (LITTLENUM_TYPE) ((unsigned) -1) >> 1; | |
252b5132 RH |
143 | words[1] = (LITTLENUM_TYPE) -1; |
144 | words[2] = (LITTLENUM_TYPE) -1; | |
145 | words[3] = (LITTLENUM_TYPE) -1; | |
146 | words[4] = (LITTLENUM_TYPE) -1; | |
147 | words[5] = (LITTLENUM_TYPE) -1; | |
148 | } | |
149 | \f | |
2d484c7f KH |
150 | /* Warning: This returns 16-bit LITTLENUMs. It is up to the caller to |
151 | figure out any alignment problems and to conspire for the | |
152 | bytes/word to be emitted in the right order. Bigendians beware! */ | |
252b5132 RH |
153 | |
154 | /* Note that atof-ieee always has X and P precisions enabled. it is up | |
155 | to md_atof to filter them out if the target machine does not support | |
156 | them. */ | |
157 | ||
2d484c7f KH |
158 | /* Returns pointer past text consumed. */ |
159 | ||
252b5132 | 160 | char * |
ea1562b3 | 161 | atof_ieee (char *str, /* Text to convert to binary. */ |
499ac353 | 162 | int what_kind, /* 'd', 'f', 'x', 'p'. */ |
ea1562b3 | 163 | LITTLENUM_TYPE *words) /* Build the binary here. */ |
252b5132 | 164 | { |
2d484c7f KH |
165 | /* Extra bits for zeroed low-order bits. |
166 | The 1st MAX_PRECISION are zeroed, the last contain flonum bits. */ | |
252b5132 RH |
167 | static LITTLENUM_TYPE bits[MAX_PRECISION + MAX_PRECISION + GUARD]; |
168 | char *return_value; | |
2d484c7f | 169 | /* Number of 16-bit words in the format. */ |
252b5132 RH |
170 | int precision; |
171 | long exponent_bits; | |
172 | FLONUM_TYPE save_gen_flonum; | |
173 | ||
174 | /* We have to save the generic_floating_point_number because it | |
175 | contains storage allocation about the array of LITTLENUMs where | |
176 | the value is actually stored. We will allocate our own array of | |
177 | littlenums below, but have to restore the global one on exit. */ | |
178 | save_gen_flonum = generic_floating_point_number; | |
179 | ||
180 | return_value = str; | |
181 | generic_floating_point_number.low = bits + MAX_PRECISION; | |
182 | generic_floating_point_number.high = NULL; | |
183 | generic_floating_point_number.leader = NULL; | |
184 | generic_floating_point_number.exponent = 0; | |
185 | generic_floating_point_number.sign = '\0'; | |
186 | ||
187 | /* Use more LittleNums than seems necessary: the highest flonum may | |
2d484c7f | 188 | have 15 leading 0 bits, so could be useless. */ |
252b5132 RH |
189 | |
190 | memset (bits, '\0', sizeof (LITTLENUM_TYPE) * MAX_PRECISION); | |
191 | ||
192 | switch (what_kind) | |
193 | { | |
194 | case 'f': | |
195 | case 'F': | |
196 | case 's': | |
197 | case 'S': | |
198 | precision = F_PRECISION; | |
199 | exponent_bits = 8; | |
200 | break; | |
201 | ||
202 | case 'd': | |
203 | case 'D': | |
204 | case 'r': | |
205 | case 'R': | |
206 | precision = D_PRECISION; | |
207 | exponent_bits = 11; | |
208 | break; | |
209 | ||
210 | case 'x': | |
211 | case 'X': | |
212 | case 'e': | |
213 | case 'E': | |
214 | precision = X_PRECISION; | |
215 | exponent_bits = 15; | |
216 | break; | |
217 | ||
218 | case 'p': | |
219 | case 'P': | |
252b5132 RH |
220 | precision = P_PRECISION; |
221 | exponent_bits = -1; | |
222 | break; | |
223 | ||
224 | default: | |
225 | make_invalid_floating_point_number (words); | |
226 | return (NULL); | |
227 | } | |
228 | ||
229 | generic_floating_point_number.high | |
230 | = generic_floating_point_number.low + precision - 1 + GUARD; | |
231 | ||
232 | if (atof_generic (&return_value, ".", EXP_CHARS, | |
233 | &generic_floating_point_number)) | |
234 | { | |
235 | make_invalid_floating_point_number (words); | |
ea1562b3 | 236 | return NULL; |
252b5132 RH |
237 | } |
238 | gen_to_words (words, precision, exponent_bits); | |
239 | ||
240 | /* Restore the generic_floating_point_number's storage alloc (and | |
241 | everything else). */ | |
242 | generic_floating_point_number = save_gen_flonum; | |
243 | ||
244 | return return_value; | |
245 | } | |
246 | ||
247 | /* Turn generic_floating_point_number into a real float/double/extended. */ | |
2d484c7f | 248 | |
252b5132 | 249 | int |
ea1562b3 | 250 | gen_to_words (LITTLENUM_TYPE *words, int precision, long exponent_bits) |
252b5132 RH |
251 | { |
252 | int return_value = 0; | |
253 | ||
254 | long exponent_1; | |
255 | long exponent_2; | |
256 | long exponent_3; | |
257 | long exponent_4; | |
258 | int exponent_skippage; | |
259 | LITTLENUM_TYPE word1; | |
260 | LITTLENUM_TYPE *lp; | |
261 | LITTLENUM_TYPE *words_end; | |
262 | ||
263 | words_end = words + precision; | |
264 | #ifdef TC_M68K | |
265 | if (precision == X_PRECISION) | |
266 | /* On the m68k the extended precision format has a gap of 16 bits | |
267 | between the exponent and the mantissa. */ | |
268 | words_end++; | |
269 | #endif | |
270 | ||
271 | if (generic_floating_point_number.low > generic_floating_point_number.leader) | |
272 | { | |
2d484c7f | 273 | /* 0.0e0 seen. */ |
252b5132 RH |
274 | if (generic_floating_point_number.sign == '+') |
275 | words[0] = 0x0000; | |
276 | else | |
277 | words[0] = 0x8000; | |
278 | memset (&words[1], '\0', | |
279 | (words_end - words - 1) * sizeof (LITTLENUM_TYPE)); | |
2d484c7f | 280 | return return_value; |
252b5132 RH |
281 | } |
282 | ||
2d484c7f | 283 | /* NaN: Do the right thing. */ |
252b5132 RH |
284 | if (generic_floating_point_number.sign == 0) |
285 | { | |
580a832e | 286 | if (TC_LARGEST_EXPONENT_IS_NORMAL (precision)) |
20203fb9 | 287 | as_warn (_("NaNs are not supported by this target\n")); |
252b5132 RH |
288 | if (precision == F_PRECISION) |
289 | { | |
290 | words[0] = 0x7fff; | |
291 | words[1] = 0xffff; | |
292 | } | |
293 | else if (precision == X_PRECISION) | |
294 | { | |
295 | #ifdef TC_M68K | |
296 | words[0] = 0x7fff; | |
297 | words[1] = 0; | |
298 | words[2] = 0xffff; | |
299 | words[3] = 0xffff; | |
300 | words[4] = 0xffff; | |
301 | words[5] = 0xffff; | |
2d484c7f | 302 | #else /* ! TC_M68K */ |
252b5132 RH |
303 | #ifdef TC_I386 |
304 | words[0] = 0xffff; | |
305 | words[1] = 0xc000; | |
306 | words[2] = 0; | |
307 | words[3] = 0; | |
308 | words[4] = 0; | |
2d484c7f | 309 | #else /* ! TC_I386 */ |
252b5132 | 310 | abort (); |
2d484c7f KH |
311 | #endif /* ! TC_I386 */ |
312 | #endif /* ! TC_M68K */ | |
252b5132 RH |
313 | } |
314 | else | |
315 | { | |
316 | words[0] = 0x7fff; | |
317 | words[1] = 0xffff; | |
318 | words[2] = 0xffff; | |
319 | words[3] = 0xffff; | |
320 | } | |
321 | return return_value; | |
322 | } | |
323 | else if (generic_floating_point_number.sign == 'P') | |
324 | { | |
580a832e | 325 | if (TC_LARGEST_EXPONENT_IS_NORMAL (precision)) |
20203fb9 | 326 | as_warn (_("Infinities are not supported by this target\n")); |
e103941e | 327 | |
2d484c7f | 328 | /* +INF: Do the right thing. */ |
252b5132 RH |
329 | if (precision == F_PRECISION) |
330 | { | |
331 | words[0] = 0x7f80; | |
332 | words[1] = 0; | |
333 | } | |
334 | else if (precision == X_PRECISION) | |
335 | { | |
336 | #ifdef TC_M68K | |
337 | words[0] = 0x7fff; | |
338 | words[1] = 0; | |
339 | words[2] = 0; | |
340 | words[3] = 0; | |
341 | words[4] = 0; | |
342 | words[5] = 0; | |
2d484c7f | 343 | #else /* ! TC_M68K */ |
252b5132 RH |
344 | #ifdef TC_I386 |
345 | words[0] = 0x7fff; | |
346 | words[1] = 0x8000; | |
347 | words[2] = 0; | |
348 | words[3] = 0; | |
349 | words[4] = 0; | |
2d484c7f | 350 | #else /* ! TC_I386 */ |
252b5132 | 351 | abort (); |
2d484c7f KH |
352 | #endif /* ! TC_I386 */ |
353 | #endif /* ! TC_M68K */ | |
252b5132 RH |
354 | } |
355 | else | |
356 | { | |
357 | words[0] = 0x7ff0; | |
358 | words[1] = 0; | |
359 | words[2] = 0; | |
360 | words[3] = 0; | |
361 | } | |
2d484c7f | 362 | return return_value; |
252b5132 RH |
363 | } |
364 | else if (generic_floating_point_number.sign == 'N') | |
365 | { | |
580a832e | 366 | if (TC_LARGEST_EXPONENT_IS_NORMAL (precision)) |
20203fb9 | 367 | as_warn (_("Infinities are not supported by this target\n")); |
e103941e | 368 | |
2d484c7f | 369 | /* Negative INF. */ |
252b5132 RH |
370 | if (precision == F_PRECISION) |
371 | { | |
372 | words[0] = 0xff80; | |
373 | words[1] = 0x0; | |
374 | } | |
375 | else if (precision == X_PRECISION) | |
376 | { | |
377 | #ifdef TC_M68K | |
378 | words[0] = 0xffff; | |
379 | words[1] = 0; | |
380 | words[2] = 0; | |
381 | words[3] = 0; | |
382 | words[4] = 0; | |
383 | words[5] = 0; | |
2d484c7f | 384 | #else /* ! TC_M68K */ |
252b5132 RH |
385 | #ifdef TC_I386 |
386 | words[0] = 0xffff; | |
387 | words[1] = 0x8000; | |
388 | words[2] = 0; | |
389 | words[3] = 0; | |
390 | words[4] = 0; | |
2d484c7f | 391 | #else /* ! TC_I386 */ |
252b5132 | 392 | abort (); |
2d484c7f KH |
393 | #endif /* ! TC_I386 */ |
394 | #endif /* ! TC_M68K */ | |
252b5132 RH |
395 | } |
396 | else | |
397 | { | |
398 | words[0] = 0xfff0; | |
399 | words[1] = 0x0; | |
400 | words[2] = 0x0; | |
401 | words[3] = 0x0; | |
402 | } | |
2d484c7f | 403 | return return_value; |
252b5132 | 404 | } |
2d484c7f KH |
405 | |
406 | /* The floating point formats we support have: | |
407 | Bit 15 is sign bit. | |
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. | |
411 | ||
412 | So we need: number of bits of exponent, number of bits of | |
413 | mantissa. */ | |
252b5132 RH |
414 | bits_left_in_littlenum = LITTLENUM_NUMBER_OF_BITS; |
415 | littlenum_pointer = generic_floating_point_number.leader; | |
416 | littlenums_left = (1 | |
417 | + generic_floating_point_number.leader | |
418 | - generic_floating_point_number.low); | |
2d484c7f KH |
419 | |
420 | /* Seek (and forget) 1st significant bit. */ | |
5bb3703f | 421 | for (exponent_skippage = 0; !next_bits (1); ++exponent_skippage); |
252b5132 RH |
422 | exponent_1 = (generic_floating_point_number.exponent |
423 | + generic_floating_point_number.leader | |
424 | + 1 | |
425 | - generic_floating_point_number.low); | |
2d484c7f | 426 | |
252b5132 | 427 | /* Radix LITTLENUM_RADIX, point just higher than |
2d484c7f | 428 | generic_floating_point_number.leader. */ |
252b5132 | 429 | exponent_2 = exponent_1 * LITTLENUM_NUMBER_OF_BITS; |
2d484c7f KH |
430 | |
431 | /* Radix 2. */ | |
252b5132 | 432 | exponent_3 = exponent_2 - exponent_skippage; |
2d484c7f KH |
433 | |
434 | /* Forget leading zeros, forget 1st bit. */ | |
252b5132 | 435 | exponent_4 = exponent_3 + ((1 << (exponent_bits - 1)) - 2); |
252b5132 | 436 | |
2d484c7f | 437 | /* Offset exponent. */ |
252b5132 RH |
438 | lp = words; |
439 | ||
2d484c7f | 440 | /* Word 1. Sign, exponent and perhaps high bits. */ |
252b5132 RH |
441 | word1 = ((generic_floating_point_number.sign == '+') |
442 | ? 0 | |
443 | : (1 << (LITTLENUM_NUMBER_OF_BITS - 1))); | |
444 | ||
2d484c7f | 445 | /* Assume 2's complement integers. */ |
252b5132 RH |
446 | if (exponent_4 <= 0) |
447 | { | |
448 | int prec_bits; | |
449 | int num_bits; | |
450 | ||
451 | unget_bits (1); | |
452 | num_bits = -exponent_4; | |
2d484c7f KH |
453 | prec_bits = |
454 | LITTLENUM_NUMBER_OF_BITS * precision - (exponent_bits + 1 + num_bits); | |
252b5132 RH |
455 | #ifdef TC_I386 |
456 | if (precision == X_PRECISION && exponent_bits == 15) | |
457 | { | |
458 | /* On the i386 a denormalized extended precision float is | |
459 | shifted down by one, effectively decreasing the exponent | |
460 | bias by one. */ | |
461 | prec_bits -= 1; | |
462 | num_bits += 1; | |
463 | } | |
464 | #endif | |
465 | ||
466 | if (num_bits >= LITTLENUM_NUMBER_OF_BITS - exponent_bits) | |
467 | { | |
2d484c7f | 468 | /* Bigger than one littlenum. */ |
252b5132 RH |
469 | num_bits -= (LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits; |
470 | *lp++ = word1; | |
2d484c7f KH |
471 | if (num_bits + exponent_bits + 1 |
472 | > precision * LITTLENUM_NUMBER_OF_BITS) | |
252b5132 | 473 | { |
2d484c7f | 474 | /* Exponent overflow. */ |
252b5132 | 475 | make_invalid_floating_point_number (words); |
2d484c7f | 476 | return return_value; |
252b5132 RH |
477 | } |
478 | #ifdef TC_M68K | |
479 | if (precision == X_PRECISION && exponent_bits == 15) | |
480 | *lp++ = 0; | |
481 | #endif | |
482 | while (num_bits >= LITTLENUM_NUMBER_OF_BITS) | |
483 | { | |
484 | num_bits -= LITTLENUM_NUMBER_OF_BITS; | |
485 | *lp++ = 0; | |
486 | } | |
487 | if (num_bits) | |
488 | *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - (num_bits)); | |
489 | } | |
490 | else | |
491 | { | |
492 | if (precision == X_PRECISION && exponent_bits == 15) | |
493 | { | |
494 | *lp++ = word1; | |
495 | #ifdef TC_M68K | |
496 | *lp++ = 0; | |
497 | #endif | |
498 | *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS - num_bits); | |
499 | } | |
500 | else | |
501 | { | |
2d484c7f KH |
502 | word1 |= next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) |
503 | - (exponent_bits + num_bits)); | |
252b5132 RH |
504 | *lp++ = word1; |
505 | } | |
506 | } | |
507 | while (lp < words_end) | |
508 | *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS); | |
509 | ||
2d484c7f | 510 | /* Round the mantissa up, but don't change the number. */ |
252b5132 RH |
511 | if (next_bits (1)) |
512 | { | |
513 | --lp; | |
19b34177 | 514 | if (prec_bits >= LITTLENUM_NUMBER_OF_BITS) |
252b5132 RH |
515 | { |
516 | int n = 0; | |
517 | int tmp_bits; | |
518 | ||
519 | n = 0; | |
520 | tmp_bits = prec_bits; | |
521 | while (tmp_bits > LITTLENUM_NUMBER_OF_BITS) | |
522 | { | |
523 | if (lp[n] != (LITTLENUM_TYPE) - 1) | |
524 | break; | |
525 | --n; | |
526 | tmp_bits -= LITTLENUM_NUMBER_OF_BITS; | |
527 | } | |
19b34177 AS |
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 | |
531 | - exponent_bits - 1) | |
532 | #ifdef TC_I386 | |
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)) | |
539 | #endif | |
540 | )) | |
252b5132 RH |
541 | { |
542 | unsigned long carry; | |
543 | ||
544 | for (carry = 1; carry && (lp >= words); lp--) | |
545 | { | |
546 | carry = *lp + carry; | |
547 | *lp = carry; | |
548 | carry >>= LITTLENUM_NUMBER_OF_BITS; | |
549 | } | |
550 | } | |
551 | else | |
552 | { | |
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. */ | |
556 | lp = words; | |
557 | word1 = ((generic_floating_point_number.sign == '+') | |
558 | ? 0 | |
559 | : (1 << (LITTLENUM_NUMBER_OF_BITS - 1))); | |
560 | word1 |= (1 | |
561 | << ((LITTLENUM_NUMBER_OF_BITS - 1) | |
562 | - exponent_bits)); | |
563 | *lp++ = word1; | |
19b34177 AS |
564 | #ifdef TC_I386 |
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); | |
570 | #endif | |
252b5132 RH |
571 | while (lp < words_end) |
572 | *lp++ = 0; | |
573 | } | |
574 | } | |
19b34177 | 575 | else |
252b5132 RH |
576 | *lp += 1; |
577 | } | |
578 | ||
579 | return return_value; | |
580 | } | |
e103941e | 581 | else if ((unsigned long) exponent_4 > mask[exponent_bits] |
580a832e | 582 | || (! TC_LARGEST_EXPONENT_IS_NORMAL (precision) |
e103941e | 583 | && (unsigned long) exponent_4 == mask[exponent_bits])) |
252b5132 | 584 | { |
2d484c7f KH |
585 | /* Exponent overflow. Lose immediately. */ |
586 | ||
587 | /* We leave return_value alone: admit we read the | |
588 | number, but return a floating exception | |
589 | because we can't encode the number. */ | |
252b5132 RH |
590 | make_invalid_floating_point_number (words); |
591 | return return_value; | |
592 | } | |
593 | else | |
594 | { | |
595 | word1 |= (exponent_4 << ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits)) | |
596 | | next_bits ((LITTLENUM_NUMBER_OF_BITS - 1) - exponent_bits); | |
597 | } | |
598 | ||
599 | *lp++ = word1; | |
600 | ||
601 | /* X_PRECISION is special: on the 68k, it has 16 bits of zero in the | |
2d484c7f | 602 | middle. Either way, it is then followed by a 1 bit. */ |
252b5132 RH |
603 | if (exponent_bits == 15 && precision == X_PRECISION) |
604 | { | |
605 | #ifdef TC_M68K | |
606 | *lp++ = 0; | |
607 | #endif | |
608 | *lp++ = (1 << (LITTLENUM_NUMBER_OF_BITS - 1) | |
609 | | next_bits (LITTLENUM_NUMBER_OF_BITS - 1)); | |
610 | } | |
611 | ||
2d484c7f | 612 | /* The rest of the words are just mantissa bits. */ |
252b5132 RH |
613 | while (lp < words_end) |
614 | *lp++ = next_bits (LITTLENUM_NUMBER_OF_BITS); | |
615 | ||
616 | if (next_bits (1)) | |
617 | { | |
618 | unsigned long carry; | |
2d484c7f KH |
619 | /* Since the NEXT bit is a 1, round UP the mantissa. |
620 | The cunning design of these hidden-1 floats permits | |
621 | us to let the mantissa overflow into the exponent, and | |
622 | it 'does the right thing'. However, we lose if the | |
623 | highest-order bit of the lowest-order word flips. | |
624 | Is that clear? */ | |
252b5132 RH |
625 | |
626 | /* #if (sizeof(carry)) < ((sizeof(bits[0]) * BITS_PER_CHAR) + 2) | |
627 | Please allow at least 1 more bit in carry than is in a LITTLENUM. | |
628 | We need that extra bit to hold a carry during a LITTLENUM carry | |
629 | propagation. Another extra bit (kept 0) will assure us that we | |
630 | don't get a sticky sign bit after shifting right, and that | |
631 | permits us to propagate the carry without any masking of bits. | |
632 | #endif */ | |
44877466 | 633 | for (carry = 1, lp--; carry; lp--) |
252b5132 RH |
634 | { |
635 | carry = *lp + carry; | |
636 | *lp = carry; | |
637 | carry >>= LITTLENUM_NUMBER_OF_BITS; | |
44877466 ILT |
638 | if (lp == words) |
639 | break; | |
252b5132 RH |
640 | } |
641 | if (precision == X_PRECISION && exponent_bits == 15) | |
642 | { | |
643 | /* Extended precision numbers have an explicit integer bit | |
644 | that we may have to restore. */ | |
645 | if (lp == words) | |
646 | { | |
647 | #ifdef TC_M68K | |
648 | /* On the m68k there is a gap of 16 bits. We must | |
2d484c7f | 649 | explicitly propagate the carry into the exponent. */ |
252b5132 RH |
650 | words[0] += words[1]; |
651 | words[1] = 0; | |
652 | lp++; | |
653 | #endif | |
2d484c7f | 654 | /* Put back the integer bit. */ |
252b5132 RH |
655 | lp[1] |= 1 << (LITTLENUM_NUMBER_OF_BITS - 1); |
656 | } | |
2d484c7f | 657 | } |
252b5132 RH |
658 | if ((word1 ^ *words) & (1 << (LITTLENUM_NUMBER_OF_BITS - 1))) |
659 | { | |
2d484c7f KH |
660 | /* We leave return_value alone: admit we read the number, |
661 | but return a floating exception because we can't encode | |
662 | the number. */ | |
252b5132 | 663 | *words &= ~(1 << (LITTLENUM_NUMBER_OF_BITS - 1)); |
252b5132 RH |
664 | } |
665 | } | |
2d484c7f | 666 | return return_value; |
252b5132 RH |
667 | } |
668 | ||
252b5132 RH |
669 | #ifdef TEST |
670 | char * | |
671 | print_gen (gen) | |
672 | FLONUM_TYPE *gen; | |
673 | { | |
674 | FLONUM_TYPE f; | |
675 | LITTLENUM_TYPE arr[10]; | |
676 | double dv; | |
677 | float fv; | |
678 | static char sbuf[40]; | |
679 | ||
680 | if (gen) | |
681 | { | |
682 | f = generic_floating_point_number; | |
683 | generic_floating_point_number = *gen; | |
684 | } | |
685 | gen_to_words (&arr[0], 4, 11); | |
686 | memcpy (&dv, &arr[0], sizeof (double)); | |
687 | sprintf (sbuf, "%x %x %x %x %.14G ", arr[0], arr[1], arr[2], arr[3], dv); | |
688 | gen_to_words (&arr[0], 2, 8); | |
689 | memcpy (&fv, &arr[0], sizeof (float)); | |
690 | sprintf (sbuf + strlen (sbuf), "%x %x %.12g\n", arr[0], arr[1], fv); | |
691 | ||
692 | if (gen) | |
2d484c7f | 693 | generic_floating_point_number = f; |
252b5132 RH |
694 | |
695 | return (sbuf); | |
696 | } | |
499ac353 NC |
697 | #endif |
698 | ||
699 | extern const char FLT_CHARS[]; | |
700 | #define MAX_LITTLENUMS 6 | |
701 | ||
702 | /* This is a utility function called from various tc-*.c files. It | |
703 | is here in order to reduce code duplication. | |
3739860c | 704 | |
499ac353 NC |
705 | Turn a string at input_line_pointer into a floating point constant |
706 | of type TYPE (a character found in the FLT_CHARS macro), and store | |
707 | it as LITTLENUMS in the bytes buffer LITP. The number of chars | |
708 | emitted is stored in *SIZEP. BIG_WORDIAN is TRUE if the littlenums | |
709 | should be emitted most significant littlenum first. | |
710 | ||
711 | An error message is returned, or a NULL pointer if everything went OK. */ | |
712 | ||
6d4af3c2 | 713 | const char * |
499ac353 NC |
714 | ieee_md_atof (int type, |
715 | char *litP, | |
716 | int *sizeP, | |
717 | bfd_boolean big_wordian) | |
718 | { | |
719 | LITTLENUM_TYPE words[MAX_LITTLENUMS]; | |
720 | LITTLENUM_TYPE *wordP; | |
721 | char *t; | |
722 | int prec = 0; | |
252b5132 | 723 | |
499ac353 NC |
724 | if (strchr (FLT_CHARS, type) != NULL) |
725 | { | |
726 | switch (type) | |
727 | { | |
728 | case 'f': | |
729 | case 'F': | |
730 | case 's': | |
731 | case 'S': | |
732 | prec = F_PRECISION; | |
733 | break; | |
734 | ||
735 | case 'd': | |
736 | case 'D': | |
737 | case 'r': | |
738 | case 'R': | |
739 | prec = D_PRECISION; | |
740 | break; | |
741 | ||
742 | case 't': | |
743 | case 'T': | |
744 | prec = X_PRECISION; | |
745 | type = 'x'; /* This is what atof_ieee() understands. */ | |
746 | break; | |
747 | ||
748 | case 'x': | |
749 | case 'X': | |
750 | case 'p': | |
751 | case 'P': | |
752 | #ifdef TC_M68K | |
753 | /* Note: on the m68k there is a gap of 16 bits (one littlenum) | |
754 | between the exponent and mantissa. Hence the precision is | |
755 | 6 and not 5. */ | |
756 | prec = P_PRECISION + 1; | |
757 | #else | |
758 | prec = P_PRECISION; | |
252b5132 | 759 | #endif |
499ac353 NC |
760 | break; |
761 | ||
762 | default: | |
763 | break; | |
764 | } | |
765 | } | |
766 | /* The 'f' and 'd' types are always recognised, even if the target has | |
767 | not put them into the FLT_CHARS macro. This is because the 'f' type | |
768 | can come from the .dc.s, .dcb.s, .float or .single pseudo-ops and the | |
769 | 'd' type from the .dc.d, .dbc.d or .double pseudo-ops. | |
770 | ||
771 | The 'x' type is not implicitly recongised however, even though it can | |
772 | be generated by the .dc.x and .dbc.x pseudo-ops because not all targets | |
773 | can support floating point values that big. ie the target has to | |
774 | explicitly allow them by putting them into FLT_CHARS. */ | |
775 | else if (type == 'f') | |
776 | prec = F_PRECISION; | |
777 | else if (type == 'd') | |
778 | prec = D_PRECISION; | |
779 | ||
780 | if (prec == 0) | |
781 | { | |
782 | *sizeP = 0; | |
783 | return _("Unrecognized or unsupported floating point constant"); | |
784 | } | |
785 | ||
9c2799c2 | 786 | gas_assert (prec <= MAX_LITTLENUMS); |
499ac353 NC |
787 | |
788 | t = atof_ieee (input_line_pointer, type, words); | |
789 | if (t) | |
790 | input_line_pointer = t; | |
791 | ||
792 | *sizeP = prec * sizeof (LITTLENUM_TYPE); | |
793 | ||
794 | if (big_wordian) | |
795 | { | |
796 | for (wordP = words; prec --;) | |
797 | { | |
798 | md_number_to_chars (litP, (valueT) (* wordP ++), sizeof (LITTLENUM_TYPE)); | |
799 | litP += sizeof (LITTLENUM_TYPE); | |
800 | } | |
801 | } | |
802 | else | |
803 | { | |
804 | for (wordP = words + prec; prec --;) | |
805 | { | |
806 | md_number_to_chars (litP, (valueT) (* -- wordP), sizeof (LITTLENUM_TYPE)); | |
807 | litP += sizeof (LITTLENUM_TYPE); | |
808 | } | |
809 | } | |
810 | ||
811 | return NULL; | |
812 | } |